[Cocoa] Allow the web process plug-in to intercept resource requests
authormitz@apple.com <mitz@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 19 Dec 2013 00:52:47 +0000 (00:52 +0000)
committermitz@apple.com <mitz@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 19 Dec 2013 00:52:47 +0000 (00:52 +0000)
https://bugs.webkit.org/show_bug.cgi?id=125959

Reviewed by Anders Carlsson.

* WebProcess/InjectedBundle/API/Cocoa/WKWebProcessPlugInLoadDelegate.h: Declared new
delegate method.
* WebProcess/InjectedBundle/API/mac/WKWebProcessPlugInBrowserContextController.mm:
(willSendRequestForFrame): Implemented this WKBundlePageResourceClient callback by calling
the load delegate.
(setUpResourceLoadClient): Added. Initializes the resource load client with the above
function.
(-[WKWebProcessPlugInBrowserContextController setLoadDelegate:]): Added calls to initialize
and clear the resource load client.

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

Source/WebKit2/ChangeLog
Source/WebKit2/WebProcess/InjectedBundle/API/Cocoa/WKWebProcessPlugInLoadDelegate.h
Source/WebKit2/WebProcess/InjectedBundle/API/mac/WKWebProcessPlugInBrowserContextController.mm

index ab8118a..370bf88 100644 (file)
@@ -1,3 +1,20 @@
+2013-12-18  Dan Bernstein  <mitz@apple.com>
+
+        [Cocoa] Allow the web process plug-in to intercept resource requests
+        https://bugs.webkit.org/show_bug.cgi?id=125959
+
+        Reviewed by Anders Carlsson.
+
+        * WebProcess/InjectedBundle/API/Cocoa/WKWebProcessPlugInLoadDelegate.h: Declared new
+        delegate method.
+        * WebProcess/InjectedBundle/API/mac/WKWebProcessPlugInBrowserContextController.mm:
+        (willSendRequestForFrame): Implemented this WKBundlePageResourceClient callback by calling
+        the load delegate.
+        (setUpResourceLoadClient): Added. Initializes the resource load client with the above
+        function.
+        (-[WKWebProcessPlugInBrowserContextController setLoadDelegate:]): Added calls to initialize
+        and clear the resource load client.
+
 2013-12-18  Yongjun Zhang  <yongjun_zhang@apple.com>
 
         https://bugs.webkit.org/show_bug.cgi?id=125941
index a92a58e..eeef3f0 100644 (file)
 
 @protocol WKWebProcessPlugInLoadDelegate <NSObject>
 @optional
+
+// Frame loading
+
 - (void)webProcessPlugInBrowserContextController:(WKWebProcessPlugInBrowserContextController*)controller didStartProvisionalLoadForFrame:(WKWebProcessPlugInFrame *)frame;
 - (void)webProcessPlugInBrowserContextController:(WKWebProcessPlugInBrowserContextController*)controller didFinishLoadForFrame:(WKWebProcessPlugInFrame *)frame;
 - (void)webProcessPlugInBrowserContextController:(WKWebProcessPlugInBrowserContextController*)controller globalObjectIsAvailableForFrame:(WKWebProcessPlugInFrame *)frame inScriptWorld:(WKWebProcessPlugInScriptWorld *)scriptWorld;
+
+// Resource loading
+
+- (NSURLRequest *)webProcessPlugInBrowserContextController:(WKWebProcessPlugInBrowserContextController *)controller frame:(WKWebProcessPlugInFrame *)frame willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse;
+
 @end
index 7ead649..dabc0d2 100644 (file)
@@ -37,6 +37,7 @@
 #import "WKDOMInternals.h"
 #import "WKRemoteObjectRegistryInternal.h"
 #import "WKRetainPtr.h"
+#import "WKURLRequestNS.h"
 #import "WKWebProcessPluginFrameInternal.h"
 #import "WKWebProcessPlugInInternal.h"
 #import "WKWebProcessPlugInLoadDelegate.h"
@@ -99,6 +100,36 @@ static void setUpPageLoaderClient(WKWebProcessPlugInBrowserContextController *co
     page.initializeInjectedBundleLoaderClient(&client.base);
 }
 
+static WKURLRequestRef willSendRequestForFrame(WKBundlePageRef page, WKBundleFrameRef frame, uint64_t, WKURLRequestRef request, WKURLResponseRef redirectResponse, const void* clientInfo)
+{
+    WKWebProcessPlugInBrowserContextController *pluginContextController = (WKWebProcessPlugInBrowserContextController *)clientInfo;
+    auto loadDelegate = pluginContextController->_loadDelegate.get();
+
+    if ([loadDelegate respondsToSelector:@selector(webProcessPlugInBrowserContextController:frame:willSendRequest:redirectResponse:)]) {
+        NSURLRequest *originalRequest = toImpl(request)->resourceRequest().nsURLRequest(DoNotUpdateHTTPBody);
+        RetainPtr<NSURLRequest> substituteRequest = [loadDelegate webProcessPlugInBrowserContextController:pluginContextController frame:wrapper(*toImpl(frame)) willSendRequest:originalRequest
+            redirectResponse:toImpl(redirectResponse)->resourceResponse().nsURLResponse()];
+
+        if (substituteRequest != originalRequest)
+            return substituteRequest ? WKURLRequestCreateWithNSURLRequest(substituteRequest.get()) : nullptr;
+    }
+
+    WKRetain(request);
+    return request;
+}
+
+static void setUpResourceLoadClient(WKWebProcessPlugInBrowserContextController *contextController, WebPage& page)
+{
+    WKBundlePageResourceLoadClientV1 client;
+    memset(&client, 0, sizeof(client));
+
+    client.base.version = 1;
+    client.base.clientInfo = contextController;
+    client.willSendRequestForFrame = willSendRequestForFrame;
+
+    page.initializeInjectedBundleResourceLoadClient(&client.base);
+}
+
 - (id <WKWebProcessPlugInLoadDelegate>)loadDelegate
 {
     return _loadDelegate.getAutoreleased();
@@ -108,10 +139,13 @@ static void setUpPageLoaderClient(WKWebProcessPlugInBrowserContextController *co
 {
     _loadDelegate = loadDelegate;
 
-    if (loadDelegate)
+    if (loadDelegate) {
         setUpPageLoaderClient(self, *_page);
-    else
+        setUpResourceLoadClient(self, *_page);
+    } else {
         _page->initializeInjectedBundleLoaderClient(nullptr);
+        _page->initializeInjectedBundleResourceLoadClient(nullptr);
+    }
 }
 
 - (void)dealloc