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

Side by Side 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 unified diff | Download patch
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #import "ios/net/crn_http_protocol_handler.h" 5 #import "ios/net/crn_http_protocol_handler.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/mac/bind_objc_block.h" 9 #include "base/mac/bind_objc_block.h"
10 #include "base/mac/scoped_nsobject.h" 10 #include "base/mac/scoped_nsobject.h"
11 #include "base/memory/ref_counted.h" 11 #include "base/memory/ref_counted.h"
12 #include "base/single_thread_task_runner.h" 12 #include "base/single_thread_task_runner.h"
13 #include "base/strings/string_util.h" 13 #include "base/strings/string_util.h"
14 #include "base/strings/sys_string_conversions.h" 14 #include "base/strings/sys_string_conversions.h"
15 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
16 #import "ios/net/clients/crn_network_client_protocol.h" 16 #import "ios/net/clients/crn_network_client_protocol.h"
17 #import "ios/net/crn_http_protocol_handler_pauseable_proxy.h"
17 #import "ios/net/crn_http_protocol_handler_proxy_with_client_thread.h" 18 #import "ios/net/crn_http_protocol_handler_proxy_with_client_thread.h"
18 #import "ios/net/http_protocol_logging.h" 19 #import "ios/net/http_protocol_logging.h"
19 #include "ios/net/nsurlrequest_util.h" 20 #include "ios/net/nsurlrequest_util.h"
20 #import "ios/net/protocol_handler_util.h" 21 #import "ios/net/protocol_handler_util.h"
21 #include "ios/net/request_tracker.h" 22 #include "ios/net/request_tracker.h"
22 #include "net/base/auth.h" 23 #include "net/base/auth.h"
23 #include "net/base/elements_upload_data_stream.h" 24 #include "net/base/elements_upload_data_stream.h"
24 #include "net/base/io_buffer.h" 25 #include "net/base/io_buffer.h"
25 #include "net/base/load_flags.h" 26 #include "net/base/load_flags.h"
26 #import "net/base/mac/url_conversions.h" 27 #import "net/base/mac/url_conversions.h"
(...skipping 916 matching lines...) Expand 10 before | Expand all | Expand 10 after
943 - (void)stopLoading { 944 - (void)stopLoading {
944 g_protocol_handler_delegate->GetDefaultURLRequestContext() 945 g_protocol_handler_delegate->GetDefaultURLRequestContext()
945 ->GetNetworkTaskRunner() 946 ->GetNetworkTaskRunner()
946 ->PostTask(FROM_HERE, 947 ->PostTask(FROM_HERE,
947 base::Bind(&net::HttpProtocolHandlerCore::Cancel, _core)); 948 base::Bind(&net::HttpProtocolHandlerCore::Cancel, _core));
948 [_protocolProxy invalidate]; 949 [_protocolProxy invalidate];
949 _protocolProxy.reset(); 950 _protocolProxy.reset();
950 } 951 }
951 952
952 @end 953 @end
954
955 #pragma mark -
956 #pragma mark PauseableHttpProtocolHandler
957
958 // The HttpProtocolHandler is called by the iOS system to handle the
959 // NSURLRequest. This HttpProtocolHandler conforms to the observed semantics of
960 // NSURLProtocol when used with NSURLSession on iOS 8 - i.e., |-startLoading|
961 // means "start or resume request" and |-stopLoading| means "pause request".
962 // Since there is no way to actually pause a request in the network stack, this
963 // is implemented using a subclass of CRNHTTPProtocolHandlerProxy that knows how
964 // to defer callbacks.
965 // A bunch of this code is somewhat duplicated from CRNHTTPProtocolHandler.
966 // 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.
967 @implementation CRNPauseableHTTPProtocolHandler {
968 scoped_refptr<net::HttpProtocolHandlerCore> _core;
969 base::scoped_nsobject<CRNHTTPProtocolHandlerPauseableProxy> _protocolProxy;
970 BOOL _supportedURL;
971 BOOL _started;
972 }
973
974 #pragma mark NSURLProtocol methods
975
976 + (BOOL)canInitWithRequest:(NSURLRequest*)request {
977 DVLOG(5) << "canInitWithRequest " << net::FormatUrlRequestForLogging(request);
978 return g_protocol_handler_delegate->CanHandleRequest(request);
979 }
980
981 + (NSURLRequest*)canonicalRequestForRequest:(NSURLRequest*)request {
982 // TODO(droger): Is this used if we disable the cache of UIWebView? If it is,
983 // then we need a real implementation, even though Chrome network stack does
984 // not need it (GURLs are automatically canonized).
985 return request;
986 }
987
988 - (instancetype)initWithRequest:(NSURLRequest*)request
989 cachedResponse:(NSCachedURLResponse*)cachedResponse
990 client:(id<NSURLProtocolClient>)client {
991 DCHECK(!cachedResponse);
992 self = [super initWithRequest:request
993 cachedResponse:cachedResponse
994 client:client];
995 if (self) {
996 _supportedURL = g_protocol_handler_delegate->IsRequestSupported(request);
997 _core = new net::HttpProtocolHandlerCore(request);
998 _started = NO;
999 }
1000 return self;
1001 }
1002
1003 - (void)dealloc {
1004 g_protocol_handler_delegate->GetDefaultURLRequestContext()
1005 ->GetNetworkTaskRunner()
1006 ->PostTask(FROM_HERE,
1007 base::Bind(&net::HttpProtocolHandlerCore::Cancel, _core));
1008 [_protocolProxy invalidate];
1009 [super dealloc];
1010 }
1011
1012 #pragma mark NSURLProtocol overrides.
1013
1014 - (NSCachedURLResponse*)cachedResponse {
1015 // We do not use the UIWebView cache.
1016 // TODO(droger): Disable the UIWebView cache.
1017 return nil;
1018 }
1019
1020 - (void)startLoading {
1021 if (_started) {
1022 [_protocolProxy resume];
1023 return;
1024 }
1025
1026 // If the scheme is not valid, just return an error right away.
1027 if (!_supportedURL) {
1028 NSMutableDictionary* dictionary = [NSMutableDictionary dictionary];
1029
1030 // It is possible for URL to be nil, so check for that
1031 // before creating the error object. See http://crbug/349051
1032 NSURL* url = [[self request] URL];
1033 if (url)
1034 [dictionary setObject:url forKey:NSURLErrorKey];
1035
1036 NSError* error = [NSError errorWithDomain:NSURLErrorDomain
1037 code:NSURLErrorUnsupportedURL
1038 userInfo:dictionary];
1039 [[self client] URLProtocol:self didFailWithError:error];
1040 return;
1041 }
1042
1043 _started = YES;
1044 _protocolProxy.reset([[CRNHTTPProtocolHandlerPauseableProxy alloc]
1045 initWithProtocol:self
1046 clientThread:[NSThread currentThread]
1047 runLoopMode:[[NSRunLoop currentRunLoop] currentMode]]);
1048 g_protocol_handler_delegate->GetDefaultURLRequestContext()
1049 ->GetNetworkTaskRunner()
1050 ->PostTask(FROM_HERE, base::Bind(&net::HttpProtocolHandlerCore::Start,
1051 _core, _protocolProxy));
1052 }
1053
1054 - (void)stopLoading {
1055 [_protocolProxy pause];
1056 }
1057
1058 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698