Add ability to set custom userAgent for WebKit2
authorweinig@apple.com <weinig@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 18 Aug 2010 23:46:21 +0000 (23:46 +0000)
committerweinig@apple.com <weinig@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 18 Aug 2010 23:46:21 +0000 (23:46 +0000)
https://bugs.webkit.org/show_bug.cgi?id=44215

Reviewed by Jon Honeycutt.

* Shared/CoreIPCSupport/WebPageMessageKinds.h:
(WebPageMessage::):
* UIProcess/API/C/WKPage.cpp:
(WKPageSetCustomUserAgent):
* UIProcess/API/C/WKPage.h:
* UIProcess/WebPageProxy.cpp:
(WebKit::WebPageProxy::setCustomUserAgent):
* UIProcess/WebPageProxy.h:
* WebProcess/WebCoreSupport/WebFrameLoaderClient.cpp:
(WebKit::WebFrameLoaderClient::userAgent):
* WebProcess/WebPage/WebPage.cpp:
(WebKit::WebPage::setCustomUserAgent):
(WebKit::WebPage::userAgent):
(WebKit::WebPage::didReceiveMessage):
* WebProcess/WebPage/WebPage.h:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@65644 268f45cc-cd09-0410-ab3c-d52691b4dbfc

WebKit2/ChangeLog
WebKit2/Shared/CoreIPCSupport/WebPageMessageKinds.h
WebKit2/UIProcess/API/C/WKPage.cpp
WebKit2/UIProcess/API/C/WKPage.h
WebKit2/UIProcess/WebPageProxy.cpp
WebKit2/UIProcess/WebPageProxy.h
WebKit2/WebProcess/WebCoreSupport/WebFrameLoaderClient.cpp
WebKit2/WebProcess/WebPage/WebPage.cpp
WebKit2/WebProcess/WebPage/WebPage.h

index 1cf11c6..8488080 100644 (file)
@@ -1,5 +1,28 @@
 2010-08-18  Sam Weinig  <sam@webkit.org>
 
+        Reviewed by Jon Honeycutt.
+
+        Add ability to set custom userAgent for WebKit2
+        https://bugs.webkit.org/show_bug.cgi?id=44215
+
+        * Shared/CoreIPCSupport/WebPageMessageKinds.h:
+        (WebPageMessage::):
+        * UIProcess/API/C/WKPage.cpp:
+        (WKPageSetCustomUserAgent):
+        * UIProcess/API/C/WKPage.h:
+        * UIProcess/WebPageProxy.cpp:
+        (WebKit::WebPageProxy::setCustomUserAgent):
+        * UIProcess/WebPageProxy.h:
+        * WebProcess/WebCoreSupport/WebFrameLoaderClient.cpp:
+        (WebKit::WebFrameLoaderClient::userAgent):
+        * WebProcess/WebPage/WebPage.cpp:
+        (WebKit::WebPage::setCustomUserAgent):
+        (WebKit::WebPage::userAgent):
+        (WebKit::WebPage::didReceiveMessage):
+        * WebProcess/WebPage/WebPage.h:
+
+2010-08-18  Sam Weinig  <sam@webkit.org>
+
         Rubber-stamped by John Sullivan.
 
         Make WKBundleNodeHandleRef usable from WKRetainPtr.
index 491902c..439273c 100644 (file)
@@ -46,6 +46,7 @@ enum Kind {
     Reload,
     RunJavaScriptInMainFrame,
     SetActive,
+    SetCustomUserAgent,
     SetFocused,
     SetIsInWindow,
     StopLoading,
index 9883dfa..decb9b8 100644 (file)
@@ -127,6 +127,11 @@ double WKPageGetEstimatedProgress(WKPageRef pageRef)
     return toWK(pageRef)->estimatedProgress();
 }
 
+void WKPageSetCustomUserAgent(WKPageRef pageRef, WKStringRef userAgentRef)
+{
+    toWK(pageRef)->setCustomUserAgent(toWK(userAgentRef)->string());
+}
+
 void WKPageTerminate(WKPageRef pageRef)
 {
     toWK(pageRef)->terminateProcess();
index 072ad61..5352996 100644 (file)
@@ -169,7 +169,9 @@ WK_EXPORT WKStringRef WKPageCopyTitle(WKPageRef page);
 
 WK_EXPORT WKFrameRef WKPageGetMainFrame(WKPageRef page);
 WK_EXPORT double WKPageGetEstimatedProgress(WKPageRef page);
-    
+
+WK_EXPORT void WKPageSetCustomUserAgent(WKPageRef page, WKStringRef userAgent);
+
 WK_EXPORT void WKPageTerminate(WKPageRef page);
 
 WK_EXPORT WKDataRef WKPageCopySessionState(WKPageRef page);
index 5f49a92..fd5a809 100644 (file)
@@ -340,6 +340,14 @@ void WebPageProxy::receivedPolicyDecision(WebCore::PolicyAction action, WebFrame
     process()->send(WebPageMessage::DidReceivePolicyDecision, m_pageID, CoreIPC::In(frame->frameID(), listenerID, (uint32_t)action));
 }
 
+void WebPageProxy::setCustomUserAgent(const String& userAgent)
+{
+    if (!isValid())
+        return;
+
+    process()->send(WebPageMessage::SetCustomUserAgent, m_pageID, CoreIPC::In(userAgent));
+}
+
 void WebPageProxy::terminateProcess()
 {
     if (!isValid())
index 50f020b..b4eff4c 100644 (file)
@@ -132,6 +132,8 @@ public:
 
     double estimatedProgress() const { return m_estimatedProgress; }
 
+    void setCustomUserAgent(const String&);
+
     void terminateProcess();
     
     PassRefPtr<WebData> sessionState() const;
index acd1067..8b8a50b 100644 (file)
@@ -804,8 +804,11 @@ void WebFrameLoaderClient::setTitle(const String& title, const KURL& url)
 
 String WebFrameLoaderClient::userAgent(const KURL&)
 {
-    notImplemented();
-    return "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6; en-us) AppleWebKit/531.4 (KHTML, like Gecko) Version/4.0.3 Safari/531.4";
+    WebPage* webPage = m_frame->page();
+    if (!webPage)
+        return String();
+
+    return webPage->userAgent();
 }
 
 void WebFrameLoaderClient::savePlatformDataToCachedFrame(CachedFrame*)
index 95c163f..4066b44 100644 (file)
@@ -452,6 +452,20 @@ void WebPage::show()
     WebProcess::shared().connection()->send(WebPageProxyMessage::ShowPage, m_pageID, CoreIPC::In());
 }
 
+void WebPage::setCustomUserAgent(const String& customUserAgent)
+{
+    m_customUserAgent = customUserAgent;
+}
+
+String WebPage::userAgent() const
+{
+    if (!m_customUserAgent.isNull())
+        return m_customUserAgent;
+
+    // FIXME: This should be based on an application name.
+    return "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6; en-us) AppleWebKit/531.4 (KHTML, like Gecko) Version/4.0.3 Safari/531.4";
+}
+
 void WebPage::runJavaScriptInMainFrame(const WTF::String& script, uint64_t callbackID)
 {
     // NOTE: We need to be careful when running scripts that the objects we depend on don't
@@ -646,6 +660,13 @@ void WebPage::didReceiveMessage(CoreIPC::Connection* connection, CoreIPC::Messag
             tryClose();
             return;
         }
+        case WebPageMessage::SetCustomUserAgent: {
+            String customUserAgent;
+            if (!arguments->decode(CoreIPC::Out(customUserAgent)))
+                return;
+            setCustomUserAgent(customUserAgent);
+            return;
+        }
     }
 
     ASSERT_NOT_REACHED();
index 2d877ba..9d64ec3 100644 (file)
 #include <WebCore/FrameLoaderTypes.h>
 #include <WebCore/IntRect.h>
 #include <WebCore/ZoomMode.h>
-#include <wtf/Forward.h>
 #include <wtf/HashMap.h>
 #include <wtf/OwnPtr.h>
 #include <wtf/PassRefPtr.h>
 #include <wtf/RefPtr.h>
+#include <wtf/text/WTFString.h>
 
 namespace CoreIPC {
     class ArgumentDecoder;
@@ -92,6 +92,7 @@ public:
     // -- Called from WebCore clients.
     bool handleEditingKeyboardEvent(WebCore::KeyboardEvent*);
     void show();
+    String userAgent() const;
 
     // -- Called from WebProcess.
     void didReceiveMessage(CoreIPC::Connection*, CoreIPC::MessageID, CoreIPC::ArgumentDecoder*);
@@ -109,7 +110,7 @@ public:
 
     WebFrame* mainFrame() const { return m_mainFrame.get(); }
 
-    WTF::String renderTreeExternalRepresentation() const;
+    String renderTreeExternalRepresentation() const;
     void executeEditingCommand(const WTF::String& commandName, const WTF::String& argument);
     bool isEditingCommandEnabled(const WTF::String& commandName);
     void clearMainFrameName();
@@ -156,11 +157,14 @@ private:
     void preferencesDidChange(const WebPreferencesStore&);
     void platformPreferencesDidChange(const WebPreferencesStore&);
     void didReceivePolicyDecision(WebFrame*, uint64_t listenerID, WebCore::PolicyAction policyAction);
+    void setCustomUserAgent(const WTF::String&);
 
     WebCore::Page* m_page;
     RefPtr<WebFrame> m_mainFrame;
     HashMap<uint64_t, WebFrame*> m_frameMap;
 
+    String m_customUserAgent;
+
     WebCore::IntSize m_viewSize;
     RefPtr<DrawingArea> m_drawingArea;