Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(20)

Unified Diff: ios/net/crn_http_protocol_handler.mm

Issue 1142383006: CrNet: add pauseable NSURLProtocol and switch to using it (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fold PauseableProxy into Proxy Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ios/net/crn_http_protocol_handler.mm
diff --git a/ios/net/crn_http_protocol_handler.mm b/ios/net/crn_http_protocol_handler.mm
index 453b3b32f223217df2bf1a6f72b8dc9c7e9194dd..aa3e9e3bce6fafa92e361c6d881e766dc8c2f3cd 100644
--- a/ios/net/crn_http_protocol_handler.mm
+++ b/ios/net/crn_http_protocol_handler.mm
@@ -868,6 +868,13 @@ void HttpProtocolHandlerCore::PushClients(NSArray* clients) {
#pragma mark -
#pragma mark HttpProtocolHandler
+@interface CRNHTTPProtocolHandler (Private) {}
+
+- (id<CRNHTTPProtocolHandlerProxy>)getProtocolHandlerProxy;
+- (void)cancelRequest;
+
+@end
+
// The HttpProtocolHandler is called by the iOS system to handle the
// NSURLRequest.
@implementation CRNHTTPProtocolHandler {
@@ -930,23 +937,87 @@ void HttpProtocolHandlerCore::PushClients(NSArray* clients) {
return;
}
- _protocolProxy.reset([[CRNHTTPProtocolHandlerProxyWithClientThread alloc]
- initWithProtocol:self
- clientThread:[NSThread currentThread]
- runLoopMode:[[NSRunLoop currentRunLoop] currentMode]]);
g_protocol_handler_delegate->GetDefaultURLRequestContext()
->GetNetworkTaskRunner()
->PostTask(FROM_HERE, base::Bind(&net::HttpProtocolHandlerCore::Start,
- _core, _protocolProxy));
+ _core, [self getProtocolHandlerProxy]));
}
-- (void)stopLoading {
+- (id<CRNHTTPProtocolHandlerProxy>)getProtocolHandlerProxy {
+ if (!_protocolProxy.get()) {
+ _protocolProxy.reset([[CRNHTTPProtocolHandlerProxyWithClientThread alloc]
+ initWithProtocol:self
+ clientThread:[NSThread currentThread]
+ runLoopMode:[[NSRunLoop currentRunLoop] currentMode]]);
+ }
+ return _protocolProxy.get();
+}
+
+- (void)cancelRequest {
g_protocol_handler_delegate->GetDefaultURLRequestContext()
->GetNetworkTaskRunner()
->PostTask(FROM_HERE,
base::Bind(&net::HttpProtocolHandlerCore::Cancel, _core));
[_protocolProxy invalidate];
+}
+
+- (void)stopLoading {
+ [self cancelRequest];
_protocolProxy.reset();
}
@end
+
+#pragma mark -
+#pragma mark PauseableHttpProtocolHandler
+
+// The HttpProtocolHandler is called by the iOS system to handle the
+// NSURLRequest. This HttpProtocolHandler conforms to the observed semantics of
+// NSURLProtocol when used with NSURLSession on iOS 8 - i.e., |-startLoading|
+// means "start or resume request" and |-stopLoading| means "pause request".
+// Since there is no way to actually pause a request in the network stack, this
+// is implemented using a subclass of CRNHTTPProtocolHandlerProxy that knows how
+// to defer callbacks.
+// A bunch of this code is somewhat duplicated from CRNHTTPProtocolHandler.
+// TODO(ellyjones): restructure so that is not true.
+@implementation CRNPauseableHTTPProtocolHandler {
+ BOOL _started;
+}
+
+#pragma mark NSURLProtocol methods
+
+- (instancetype)initWithRequest:(NSURLRequest*)request
+ cachedResponse:(NSCachedURLResponse*)cachedResponse
+ client:(id<NSURLProtocolClient>)client {
+ DCHECK(!cachedResponse);
+ self = [super initWithRequest:request
+ cachedResponse:cachedResponse
+ client:client];
+ if (self) {
+ _started = NO;
+ }
+ return self;
+}
+
+- (void)dealloc {
droger 2015/05/27 17:09:00 I am concerned that dealloc might be called from a
Elly Fong-Jones 2015/06/10 17:06:35 Okay, I think I've fixed this. It's somewhat of a
+ [self cancelRequest];
+ [super dealloc];
+}
+
+#pragma mark NSURLProtocol overrides.
+
+- (void)startLoading {
+ if (_started) {
+ [[self getProtocolHandlerProxy] resume];
+ return;
+ }
+
+ _started = YES;
+ [super startLoading];
+}
+
+- (void)stopLoading {
+ [[self getProtocolHandlerProxy] pause];
+}
+
+@end

Powered by Google App Engine
This is Rietveld 408576698