[Cocoa] WKCustomProtocolLoader should store a WeakPtr to its LegacyCustomProtocolMana...
authoraestes@apple.com <aestes@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 14 Apr 2019 16:08:23 +0000 (16:08 +0000)
committeraestes@apple.com <aestes@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 14 Apr 2019 16:08:23 +0000 (16:08 +0000)
https://bugs.webkit.org/show_bug.cgi?id=196893
<rdar://problem/48318983>

Reviewed by Anders Carlsson.

In addition to manually invalidating each WKCustomProtocolLoader's _customProtocolManagerProxy
pointer when the LegacyCustomProtocolManagerClient is invalidated, use a WeakPtr in case the
LegacyCustomProtocolManagerProxy is ever destroyed without first invalidating the client.
Also add null pointer checks to NSURLConnectionDelegate methods, which might be called after
the LegacyCustomProtocolManagerProxy has been destroyed.

* UIProcess/Cocoa/LegacyCustomProtocolManagerClient.mm:
(-[WKCustomProtocolLoader initWithLegacyCustomProtocolManagerProxy:customProtocolID:request:]):
(-[WKCustomProtocolLoader cancel]):
(-[WKCustomProtocolLoader connection:didFailWithError:]):
(-[WKCustomProtocolLoader connection:didReceiveResponse:]):
(-[WKCustomProtocolLoader connection:didReceiveData:]):
(-[WKCustomProtocolLoader connection:willSendRequest:redirectResponse:]):
(-[WKCustomProtocolLoader connectionDidFinishLoading:]):
(WebKit::LegacyCustomProtocolManagerClient::startLoading):
(WebKit::LegacyCustomProtocolManagerClient::invalidate):
(-[WKCustomProtocolLoader customProtocolManagerProxyDestroyed]): Deleted.
* UIProcess/Network/CustomProtocols/LegacyCustomProtocolManagerProxy.h:

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

Source/WebKit/ChangeLog
Source/WebKit/UIProcess/Cocoa/LegacyCustomProtocolManagerClient.mm
Source/WebKit/UIProcess/Network/CustomProtocols/LegacyCustomProtocolManagerProxy.h

index feb2c79..ba936f2 100644 (file)
@@ -1,3 +1,30 @@
+2019-04-14  Andy Estes  <aestes@apple.com>
+
+        [Cocoa] WKCustomProtocolLoader should store a WeakPtr to its LegacyCustomProtocolManagerProxy
+        https://bugs.webkit.org/show_bug.cgi?id=196893
+        <rdar://problem/48318983>
+
+        Reviewed by Anders Carlsson.
+
+        In addition to manually invalidating each WKCustomProtocolLoader's _customProtocolManagerProxy
+        pointer when the LegacyCustomProtocolManagerClient is invalidated, use a WeakPtr in case the
+        LegacyCustomProtocolManagerProxy is ever destroyed without first invalidating the client.
+        Also add null pointer checks to NSURLConnectionDelegate methods, which might be called after
+        the LegacyCustomProtocolManagerProxy has been destroyed.
+
+        * UIProcess/Cocoa/LegacyCustomProtocolManagerClient.mm:
+        (-[WKCustomProtocolLoader initWithLegacyCustomProtocolManagerProxy:customProtocolID:request:]):
+        (-[WKCustomProtocolLoader cancel]):
+        (-[WKCustomProtocolLoader connection:didFailWithError:]):
+        (-[WKCustomProtocolLoader connection:didReceiveResponse:]):
+        (-[WKCustomProtocolLoader connection:didReceiveData:]):
+        (-[WKCustomProtocolLoader connection:willSendRequest:redirectResponse:]):
+        (-[WKCustomProtocolLoader connectionDidFinishLoading:]):
+        (WebKit::LegacyCustomProtocolManagerClient::startLoading):
+        (WebKit::LegacyCustomProtocolManagerClient::invalidate):
+        (-[WKCustomProtocolLoader customProtocolManagerProxyDestroyed]): Deleted.
+        * UIProcess/Network/CustomProtocols/LegacyCustomProtocolManagerProxy.h:
+
 2019-04-14  Don Olmstead  <don.olmstead@sony.com>
 
         [CMake] JavaScriptCore derived sources should only be referenced inside JavaScriptCore
index 6c12539..7cd3221 100644 (file)
 
 @interface WKCustomProtocolLoader : NSObject <NSURLConnectionDelegate> {
 @private
-    WebKit::LegacyCustomProtocolManagerProxy* _customProtocolManagerProxy;
+    WeakPtr<WebKit::LegacyCustomProtocolManagerProxy> _customProtocolManagerProxy;
     uint64_t _customProtocolID;
     NSURLCacheStoragePolicy _storagePolicy;
     NSURLConnection *_urlConnection;
 }
-- (id)initWithLegacyCustomProtocolManagerProxy:(WebKit::LegacyCustomProtocolManagerProxy*)customProtocolManagerProxy customProtocolID:(uint64_t)customProtocolID request:(NSURLRequest *)request;
-- (void)customProtocolManagerProxyDestroyed;
+- (id)initWithLegacyCustomProtocolManagerProxy:(WebKit::LegacyCustomProtocolManagerProxy&)customProtocolManagerProxy customProtocolID:(uint64_t)customProtocolID request:(NSURLRequest *)request;
+- (void)cancel;
 @end
 
 @implementation WKCustomProtocolLoader
 
-- (id)initWithLegacyCustomProtocolManagerProxy:(WebKit::LegacyCustomProtocolManagerProxy*)customProtocolManagerProxy customProtocolID:(uint64_t)customProtocolID request:(NSURLRequest *)request
+- (id)initWithLegacyCustomProtocolManagerProxy:(WebKit::LegacyCustomProtocolManagerProxy&)customProtocolManagerProxy customProtocolID:(uint64_t)customProtocolID request:(NSURLRequest *)request
 {
     self = [super init];
     if (!self)
         return nil;
 
-    ASSERT(customProtocolManagerProxy);
     ASSERT(request);
-    _customProtocolManagerProxy = customProtocolManagerProxy;
+    _customProtocolManagerProxy = makeWeakPtr(customProtocolManagerProxy);
     _customProtocolID = customProtocolID;
     _storagePolicy = NSURLCacheStorageNotAllowed;
     ALLOW_DEPRECATED_DECLARATIONS_BEGIN
@@ -72,7 +71,7 @@
     [super dealloc];
 }
 
-- (void)customProtocolManagerProxyDestroyed
+- (void)cancel
 {
     ASSERT(_customProtocolManagerProxy);
     _customProtocolManagerProxy = nullptr;
@@ -81,6 +80,9 @@
 
 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
 {
+    if (!_customProtocolManagerProxy)
+        return;
+
     WebCore::ResourceError coreError(error);
     _customProtocolManagerProxy->didFailWithError(_customProtocolID, coreError);
     _customProtocolManagerProxy->stopLoading(_customProtocolID);
 
 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
 {
+    if (!_customProtocolManagerProxy)
+        return;
+
     WebCore::ResourceResponse coreResponse(response);
     _customProtocolManagerProxy->didReceiveResponse(_customProtocolID, coreResponse, _storagePolicy);
 }
 
 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
 {
+    if (!_customProtocolManagerProxy)
+        return;
+
     IPC::DataReference coreData(static_cast<const uint8_t*>([data bytes]), [data length]);
     _customProtocolManagerProxy->didLoadData(_customProtocolID, coreData);
 }
 
 - (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse
 {
+    if (!_customProtocolManagerProxy)
+        return nil;
+
     if (redirectResponse) {
         _customProtocolManagerProxy->wasRedirectedToRequest(_customProtocolID, request, redirectResponse);
         return nil;
 
 - (void)connectionDidFinishLoading:(NSURLConnection *)connection
 {
+    if (!_customProtocolManagerProxy)
+        return;
+
     _customProtocolManagerProxy->didFinishLoading(_customProtocolID);
     _customProtocolManagerProxy->stopLoading(_customProtocolID);
 }
@@ -131,7 +145,7 @@ void LegacyCustomProtocolManagerClient::startLoading(LegacyCustomProtocolManager
     if (!request)
         return;
 
-    WKCustomProtocolLoader *loader = [[WKCustomProtocolLoader alloc] initWithLegacyCustomProtocolManagerProxy:&manager customProtocolID:customProtocolID request:request];
+    WKCustomProtocolLoader *loader = [[WKCustomProtocolLoader alloc] initWithLegacyCustomProtocolManagerProxy:manager customProtocolID:customProtocolID request:request];
     ASSERT(loader);
     ASSERT(!m_loaderMap.contains(customProtocolID));
     m_loaderMap.add(customProtocolID, loader);
@@ -148,7 +162,7 @@ void LegacyCustomProtocolManagerClient::invalidate(LegacyCustomProtocolManagerPr
     while (!m_loaderMap.isEmpty()) {
         auto loader = m_loaderMap.take(m_loaderMap.begin()->key);
         ASSERT(loader);
-        [loader customProtocolManagerProxyDestroyed];
+        [loader cancel];
     }
 }
 
index b76f6ca..b529e4a 100644 (file)
@@ -27,6 +27,8 @@
 
 #include "MessageReceiver.h"
 
+#include <wtf/WeakPtr.h>
+
 #if PLATFORM(COCOA)
 #include <wtf/HashMap.h>
 #include <wtf/RetainPtr.h>
@@ -47,7 +49,7 @@ namespace WebKit {
 
 class NetworkProcessProxy;
 
-class LegacyCustomProtocolManagerProxy : public IPC::MessageReceiver {
+class LegacyCustomProtocolManagerProxy : public CanMakeWeakPtr<LegacyCustomProtocolManagerProxy>, public IPC::MessageReceiver {
 public:
     LegacyCustomProtocolManagerProxy(NetworkProcessProxy&);
     ~LegacyCustomProtocolManagerProxy();