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

Unified Diff: components/cronet/ios/Cronet.mm

Issue 2146643002: [Cronet] Integrate CrNet functionality into Cronet on iOS. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Sync Created 4 years, 2 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
« no previous file with comments | « components/cronet/ios/Cronet.h ('k') | components/cronet/ios/CronetTestSupport.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/cronet/ios/Cronet.mm
diff --git a/components/cronet/ios/Cronet.mm b/components/cronet/ios/Cronet.mm
index c397eefbdebd9035ae0f0189127e5caa26b961aa..374247b898ae3344ba03cb33a5c76223d1a76cec 100644
--- a/components/cronet/ios/Cronet.mm
+++ b/components/cronet/ios/Cronet.mm
@@ -8,13 +8,22 @@
#include "base/lazy_instance.h"
#include "base/logging.h"
+#include "base/mac/scoped_block.h"
+#include "base/memory/ptr_util.h"
#include "base/memory/scoped_vector.h"
#include "base/strings/sys_string_conversions.h"
#include "components/cronet/ios/cronet_environment.h"
#include "components/cronet/url_request_context_config.h"
+#import "components/cronet/ios/CronetTestSupport.h" // nogncheck
+#include "ios/net/crn_http_protocol_handler.h"
+#include "ios/net/empty_nsurlcache.h"
+#include "net/cert/cert_verifier.h"
+#include "net/url_request/url_request_context_getter.h"
namespace {
+class CronetHttpProtocolHandlerDelegate;
+
// Currently there is one and only one instance of CronetEnvironment,
// which is leaked at the shutdown. We should consider allowing multiple
// instances if that makes sense in the future.
@@ -25,7 +34,67 @@ BOOL gHttp2Enabled = YES;
BOOL gQuicEnabled = NO;
ScopedVector<cronet::URLRequestContextConfig::QuicHint> gQuicHints;
NSString* gUserAgent = nil;
+BOOL gUserAgentPartial = NO;
NSString* gSslKeyLogFileName = nil;
+RequestFilterBlock gRequestFilterBlock = nil;
+std::unique_ptr<CronetHttpProtocolHandlerDelegate> gHttpProtocolHandlerDelegate;
+NSURLCache* gPreservedSharedURLCache = nil;
+BOOL gEnableTestCertVerifierForTesting = FALSE;
+NSString* gHostResolverRulesForTesting = @"";
+
+// CertVerifier, which allows any certificates for testing.
+class TestCertVerifier : public net::CertVerifier {
+ int Verify(const RequestParams& params,
+ net::CRLSet* crl_set,
+ net::CertVerifyResult* verify_result,
+ const net::CompletionCallback& callback,
+ std::unique_ptr<Request>* out_req,
+ const net::NetLogWithSource& net_log) override {
+ net::Error result = net::OK;
+ verify_result->verified_cert = params.certificate();
+ verify_result->cert_status = net::MapNetErrorToCertStatus(result);
+ return result;
+ }
+};
+
+// net::HTTPProtocolHandlerDelegate for Cronet.
+class CronetHttpProtocolHandlerDelegate
+ : public net::HTTPProtocolHandlerDelegate {
+ public:
+ CronetHttpProtocolHandlerDelegate(net::URLRequestContextGetter* getter,
+ RequestFilterBlock filter)
+ : getter_(getter), filter_(filter, base::scoped_policy::RETAIN) {}
+
+ void SetRequestFilterBlock(RequestFilterBlock filter) {
+ filter_.reset(filter);
+ }
+
+ private:
+ // net::HTTPProtocolHandlerDelegate implementation:
+ bool CanHandleRequest(NSURLRequest* request) override {
+ if (filter_) {
+ RequestFilterBlock block = filter_.get();
+ return block(request);
+ }
+ return true;
+ }
+
+ bool IsRequestSupported(NSURLRequest* request) override {
+ NSString* scheme = [[request URL] scheme];
+ if (!scheme)
+ return false;
+ return [scheme caseInsensitiveCompare:@"data"] == NSOrderedSame ||
+ [scheme caseInsensitiveCompare:@"http"] == NSOrderedSame ||
+ [scheme caseInsensitiveCompare:@"https"] == NSOrderedSame;
+ }
+
+ net::URLRequestContextGetter* GetDefaultURLRequestContext() override {
+ return getter_.get();
+ }
+
+ scoped_refptr<net::URLRequestContextGetter> getter_;
+ base::mac::ScopedBlock<RequestFilterBlock> filter_;
+};
} // namespace
@@ -51,9 +120,10 @@ NSString* gSslKeyLogFileName = nil;
base::SysNSStringToUTF8(host), port, altPort));
}
-+ (void)setPartialUserAgent:(NSString*)userAgent {
++ (void)setUserAgent:(NSString*)userAgent partial:(bool)partial {
[self checkNotStarted];
gUserAgent = userAgent;
+ gUserAgentPartial = partial;
}
+ (void)setSslKeyLogFileName:(NSString*)sslKeyLogFileName {
@@ -61,10 +131,30 @@ NSString* gSslKeyLogFileName = nil;
gSslKeyLogFileName = sslKeyLogFileName;
}
++ (void)setRequestFilterBlock:(RequestFilterBlock)block {
+ if (gHttpProtocolHandlerDelegate.get())
+ gHttpProtocolHandlerDelegate.get()->SetRequestFilterBlock(block);
+ else
+ gRequestFilterBlock = block;
+}
+
++ (void)configureCronetEnvironmentForTesting:
+ (cronet::CronetEnvironment*)cronetEnvironment {
+ DLOG(ERROR) << "configureCronetEnvironment for testing";
+ cronetEnvironment->set_host_resolver_rules(
+ [gHostResolverRulesForTesting UTF8String]);
+ if (gEnableTestCertVerifierForTesting) {
+ std::unique_ptr<TestCertVerifier> test_cert_verifier =
+ base::MakeUnique<TestCertVerifier>();
+ cronetEnvironment->set_cert_verifier(std::move(test_cert_verifier));
+ }
+}
+
+ (void)startInternal {
cronet::CronetEnvironment::Initialize();
- std::string partialUserAgent = base::SysNSStringToUTF8(gUserAgent);
- gChromeNet.Get().reset(new cronet::CronetEnvironment(partialUserAgent));
+ std::string user_agent = base::SysNSStringToUTF8(gUserAgent);
+ gChromeNet.Get().reset(
+ new cronet::CronetEnvironment(user_agent, gUserAgentPartial));
gChromeNet.Get()->set_http2_enabled(gHttp2Enabled);
gChromeNet.Get()->set_quic_enabled(gQuicEnabled);
@@ -74,7 +164,14 @@ NSString* gSslKeyLogFileName = nil;
gChromeNet.Get()->AddQuicHint(quicHint->host, quicHint->port,
quicHint->alternate_port);
}
+
+ [self configureCronetEnvironmentForTesting:gChromeNet.Get().get()];
gChromeNet.Get()->Start();
+ gHttpProtocolHandlerDelegate.reset(new CronetHttpProtocolHandlerDelegate(
+ gChromeNet.Get()->GetURLRequestContextGetter(), gRequestFilterBlock));
+ net::HTTPProtocolHandlerDelegate::SetInstance(
+ gHttpProtocolHandlerDelegate.get());
+ gRequestFilterBlock = nil;
}
+ (void)start {
@@ -90,6 +187,31 @@ NSString* gSslKeyLogFileName = nil;
});
}
++ (void)registerHttpProtocolHandler {
+ if (gPreservedSharedURLCache == nil) {
+ gPreservedSharedURLCache = [NSURLCache sharedURLCache];
+ }
+ // Disable the default cache.
+ [NSURLCache setSharedURLCache:[EmptyNSURLCache emptyNSURLCache]];
+ // Register the chrome http protocol handler to replace the default one.
+ BOOL success =
+ [NSURLProtocol registerClass:[CRNPauseableHTTPProtocolHandler class]];
+ DCHECK(success);
+}
+
++ (void)unregisterHttpProtocolHandler {
+ // Set up SharedURLCache preserved in registerHttpProtocolHandler.
+ if (gPreservedSharedURLCache != nil) {
+ [NSURLCache setSharedURLCache:gPreservedSharedURLCache];
+ gPreservedSharedURLCache = nil;
+ }
+ [NSURLProtocol unregisterClass:[CRNPauseableHTTPProtocolHandler class]];
+}
+
++ (void)installIntoSessionConfiguration:(NSURLSessionConfiguration*)config {
+ config.protocolClasses = @[ [CRNPauseableHTTPProtocolHandler class] ];
+}
+
+ (void)startNetLogToFile:(NSString*)fileName logBytes:(BOOL)logBytes {
if (gChromeNet.Get().get() && [fileName length]) {
gChromeNet.Get()->StartNetLog([fileName UTF8String], logBytes);
@@ -121,6 +243,22 @@ NSString* gSslKeyLogFileName = nil;
return nil;
}
++ (NSData*)getGlobalMetricsDeltas {
+ if (!gChromeNet.Get().get()) {
+ return nil;
+ }
+ std::vector<uint8_t> deltas(gChromeNet.Get()->GetHistogramDeltas());
+ return [NSData dataWithBytes:deltas.data() length:deltas.size()];
+}
+
++ (void)enableTestCertVerifierForTesting {
+ gEnableTestCertVerifierForTesting = TRUE;
+}
+
++ (void)setHostResolverRulesForTesting:(NSString*)hostResolverRulesForTesting {
+ gHostResolverRulesForTesting = hostResolverRulesForTesting;
+}
+
// This is a non-public dummy method that prevents the linker from stripping out
// the otherwise non-referenced methods from 'cronet_bidirectional_stream.cc'.
+ (void)preventStrippingCronetBidirectionalStream {
« no previous file with comments | « components/cronet/ios/Cronet.h ('k') | components/cronet/ios/CronetTestSupport.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698