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

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: 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 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"
(...skipping 850 matching lines...) Expand 10 before | Expand all | Expand 10 after
861 } 861 }
862 862
863 - (void)stream:(NSStream*)theStream handleEvent:(NSStreamEvent)streamEvent { 863 - (void)stream:(NSStream*)theStream handleEvent:(NSStreamEvent)streamEvent {
864 _core->HandleStreamEvent(theStream, streamEvent); 864 _core->HandleStreamEvent(theStream, streamEvent);
865 } 865 }
866 @end 866 @end
867 867
868 #pragma mark - 868 #pragma mark -
869 #pragma mark HttpProtocolHandler 869 #pragma mark HttpProtocolHandler
870 870
871 @interface CRNHTTPProtocolHandler (Private) {}
872
873 - (id<CRNHTTPProtocolHandlerProxy>)getProtocolHandlerProxy;
874 - (void)cancelRequest;
875
876 @end
877
871 // The HttpProtocolHandler is called by the iOS system to handle the 878 // The HttpProtocolHandler is called by the iOS system to handle the
872 // NSURLRequest. 879 // NSURLRequest.
873 @implementation CRNHTTPProtocolHandler { 880 @implementation CRNHTTPProtocolHandler {
874 scoped_refptr<net::HttpProtocolHandlerCore> _core; 881 scoped_refptr<net::HttpProtocolHandlerCore> _core;
875 base::scoped_nsprotocol<id<CRNHTTPProtocolHandlerProxy>> _protocolProxy; 882 base::scoped_nsprotocol<id<CRNHTTPProtocolHandlerProxy>> _protocolProxy;
876 BOOL _supportedURL; 883 BOOL _supportedURL;
877 } 884 }
878 885
879 #pragma mark NSURLProtocol methods 886 #pragma mark NSURLProtocol methods
880 887
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
923 if (url) 930 if (url)
924 [dictionary setObject:url forKey:NSURLErrorKey]; 931 [dictionary setObject:url forKey:NSURLErrorKey];
925 932
926 NSError* error = [NSError errorWithDomain:NSURLErrorDomain 933 NSError* error = [NSError errorWithDomain:NSURLErrorDomain
927 code:NSURLErrorUnsupportedURL 934 code:NSURLErrorUnsupportedURL
928 userInfo:dictionary]; 935 userInfo:dictionary];
929 [[self client] URLProtocol:self didFailWithError:error]; 936 [[self client] URLProtocol:self didFailWithError:error];
930 return; 937 return;
931 } 938 }
932 939
933 _protocolProxy.reset([[CRNHTTPProtocolHandlerProxyWithClientThread alloc]
934 initWithProtocol:self
935 clientThread:[NSThread currentThread]
936 runLoopMode:[[NSRunLoop currentRunLoop] currentMode]]);
937 g_protocol_handler_delegate->GetDefaultURLRequestContext() 940 g_protocol_handler_delegate->GetDefaultURLRequestContext()
938 ->GetNetworkTaskRunner() 941 ->GetNetworkTaskRunner()
939 ->PostTask(FROM_HERE, base::Bind(&net::HttpProtocolHandlerCore::Start, 942 ->PostTask(FROM_HERE, base::Bind(&net::HttpProtocolHandlerCore::Start,
940 _core, _protocolProxy)); 943 _core, [self getProtocolHandlerProxy]));
941 } 944 }
942 945
943 - (void)stopLoading { 946 - (id<CRNHTTPProtocolHandlerProxy>)getProtocolHandlerProxy {
947 if (!_protocolProxy.get()) {
948 _protocolProxy.reset([[CRNHTTPProtocolHandlerProxyWithClientThread alloc]
949 initWithProtocol:self
950 clientThread:[NSThread currentThread]
951 runLoopMode:[[NSRunLoop currentRunLoop] currentMode]]);
952 }
953 return _protocolProxy.get();
954 }
955
956 - (void)cancelRequest {
944 g_protocol_handler_delegate->GetDefaultURLRequestContext() 957 g_protocol_handler_delegate->GetDefaultURLRequestContext()
945 ->GetNetworkTaskRunner() 958 ->GetNetworkTaskRunner()
946 ->PostTask(FROM_HERE, 959 ->PostTask(FROM_HERE,
947 base::Bind(&net::HttpProtocolHandlerCore::Cancel, _core)); 960 base::Bind(&net::HttpProtocolHandlerCore::Cancel, _core));
948 [_protocolProxy invalidate]; 961 [_protocolProxy invalidate];
962 }
963
964 - (void)stopLoading {
965 [self cancelRequest];
949 _protocolProxy.reset(); 966 _protocolProxy.reset();
950 } 967 }
951 968
952 @end 969 @end
970
971 #pragma mark -
972 #pragma mark PauseableHttpProtocolHandler
973
974 // The HttpProtocolHandler is called by the iOS system to handle the
975 // NSURLRequest. This HttpProtocolHandler conforms to the observed semantics of
976 // NSURLProtocol when used with NSURLSession on iOS 8 - i.e., |-startLoading|
977 // means "start or resume request" and |-stopLoading| means "pause request".
978 // Since there is no way to actually pause a request in the network stack, this
979 // is implemented using a subclass of CRNHTTPProtocolHandlerProxy that knows how
980 // to defer callbacks.
981 // A bunch of this code is somewhat duplicated from CRNHTTPProtocolHandler.
982 // TODO(ellyjones): restructure so that is not true.
983 @implementation CRNPauseableHTTPProtocolHandler {
984 BOOL _started;
985 }
986
987 #pragma mark NSURLProtocol methods
988
989 - (instancetype)initWithRequest:(NSURLRequest*)request
990 cachedResponse:(NSCachedURLResponse*)cachedResponse
991 client:(id<NSURLProtocolClient>)client {
992 DCHECK(!cachedResponse);
993 self = [super initWithRequest:request
994 cachedResponse:cachedResponse
995 client:client];
996 if (self) {
997 _started = NO;
998 }
999 return self;
1000 }
1001
1002 - (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
1003 [self cancelRequest];
1004 [super dealloc];
1005 }
1006
1007 #pragma mark NSURLProtocol overrides.
1008
1009 - (void)startLoading {
1010 if (_started) {
1011 [[self getProtocolHandlerProxy] resume];
1012 return;
1013 }
1014
1015 _started = YES;
1016 [super startLoading];
1017 }
1018
1019 - (void)stopLoading {
1020 [[self getProtocolHandlerProxy] pause];
1021 }
1022
1023 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698