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

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: 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..9aba06c40e338dec73928c272a5b430714117a60 100644
--- a/ios/net/crn_http_protocol_handler.mm
+++ b/ios/net/crn_http_protocol_handler.mm
@@ -14,6 +14,7 @@
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#import "ios/net/clients/crn_network_client_protocol.h"
+#import "ios/net/crn_http_protocol_handler_pauseable_proxy.h"
#import "ios/net/crn_http_protocol_handler_proxy_with_client_thread.h"
#import "ios/net/http_protocol_logging.h"
#include "ios/net/nsurlrequest_util.h"
@@ -950,3 +951,108 @@ void HttpProtocolHandlerCore::PushClients(NSArray* clients) {
}
@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.
marq (ping after 24h) 2015/05/22 16:56:53 Yes -- can this class just be a specialization of
Elly Fong-Jones 2015/05/26 21:16:09 Done.
+@implementation CRNPauseableHTTPProtocolHandler {
+ scoped_refptr<net::HttpProtocolHandlerCore> _core;
+ base::scoped_nsobject<CRNHTTPProtocolHandlerPauseableProxy> _protocolProxy;
+ BOOL _supportedURL;
+ BOOL _started;
+}
+
+#pragma mark NSURLProtocol methods
+
++ (BOOL)canInitWithRequest:(NSURLRequest*)request {
+ DVLOG(5) << "canInitWithRequest " << net::FormatUrlRequestForLogging(request);
+ return g_protocol_handler_delegate->CanHandleRequest(request);
+}
+
++ (NSURLRequest*)canonicalRequestForRequest:(NSURLRequest*)request {
+ // TODO(droger): Is this used if we disable the cache of UIWebView? If it is,
+ // then we need a real implementation, even though Chrome network stack does
+ // not need it (GURLs are automatically canonized).
+ return request;
+}
+
+- (instancetype)initWithRequest:(NSURLRequest*)request
+ cachedResponse:(NSCachedURLResponse*)cachedResponse
+ client:(id<NSURLProtocolClient>)client {
+ DCHECK(!cachedResponse);
+ self = [super initWithRequest:request
+ cachedResponse:cachedResponse
+ client:client];
+ if (self) {
+ _supportedURL = g_protocol_handler_delegate->IsRequestSupported(request);
+ _core = new net::HttpProtocolHandlerCore(request);
+ _started = NO;
+ }
+ return self;
+}
+
+- (void)dealloc {
+ g_protocol_handler_delegate->GetDefaultURLRequestContext()
+ ->GetNetworkTaskRunner()
+ ->PostTask(FROM_HERE,
+ base::Bind(&net::HttpProtocolHandlerCore::Cancel, _core));
+ [_protocolProxy invalidate];
+ [super dealloc];
+}
+
+#pragma mark NSURLProtocol overrides.
+
+- (NSCachedURLResponse*)cachedResponse {
+ // We do not use the UIWebView cache.
+ // TODO(droger): Disable the UIWebView cache.
+ return nil;
+}
+
+- (void)startLoading {
+ if (_started) {
+ [_protocolProxy resume];
+ return;
+ }
+
+ // If the scheme is not valid, just return an error right away.
+ if (!_supportedURL) {
+ NSMutableDictionary* dictionary = [NSMutableDictionary dictionary];
+
+ // It is possible for URL to be nil, so check for that
+ // before creating the error object. See http://crbug/349051
+ NSURL* url = [[self request] URL];
+ if (url)
+ [dictionary setObject:url forKey:NSURLErrorKey];
+
+ NSError* error = [NSError errorWithDomain:NSURLErrorDomain
+ code:NSURLErrorUnsupportedURL
+ userInfo:dictionary];
+ [[self client] URLProtocol:self didFailWithError:error];
+ return;
+ }
+
+ _started = YES;
+ _protocolProxy.reset([[CRNHTTPProtocolHandlerPauseableProxy 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));
+}
+
+- (void)stopLoading {
+ [_protocolProxy pause];
+}
+
+@end

Powered by Google App Engine
This is Rietveld 408576698