| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/cronet/ios/Cronet.h" | 5 #import "components/cronet/ios/Cronet.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/mac/bundle_locations.h" | 11 #include "base/mac/bundle_locations.h" |
| 12 #include "base/mac/scoped_block.h" | 12 #include "base/mac/scoped_block.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/memory/scoped_vector.h" | 14 #include "base/memory/scoped_vector.h" |
| 15 #include "base/strings/sys_string_conversions.h" | 15 #include "base/strings/sys_string_conversions.h" |
| 16 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
| 17 #include "components/cronet/ios/accept_languages_table.h" |
| 17 #include "components/cronet/ios/cronet_environment.h" | 18 #include "components/cronet/ios/cronet_environment.h" |
| 18 #include "components/cronet/url_request_context_config.h" | 19 #include "components/cronet/url_request_context_config.h" |
| 19 #include "ios/net/crn_http_protocol_handler.h" | 20 #include "ios/net/crn_http_protocol_handler.h" |
| 20 #include "ios/net/empty_nsurlcache.h" | 21 #include "ios/net/empty_nsurlcache.h" |
| 21 #include "net/cert/cert_verifier.h" | 22 #include "net/cert/cert_verifier.h" |
| 22 #include "net/url_request/url_request_context_getter.h" | 23 #include "net/url_request/url_request_context_getter.h" |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 26 class CronetHttpProtocolHandlerDelegate; | 27 class CronetHttpProtocolHandlerDelegate; |
| 27 | 28 |
| 28 // Currently there is one and only one instance of CronetEnvironment, | 29 // Currently there is one and only one instance of CronetEnvironment, |
| 29 // which is leaked at the shutdown. We should consider allowing multiple | 30 // which is leaked at the shutdown. We should consider allowing multiple |
| 30 // instances if that makes sense in the future. | 31 // instances if that makes sense in the future. |
| 31 base::LazyInstance<std::unique_ptr<cronet::CronetEnvironment>>::Leaky | 32 base::LazyInstance<std::unique_ptr<cronet::CronetEnvironment>>::Leaky |
| 32 gChromeNet = LAZY_INSTANCE_INITIALIZER; | 33 gChromeNet = LAZY_INSTANCE_INITIALIZER; |
| 33 | 34 |
| 34 BOOL gHttp2Enabled = YES; | 35 BOOL gHttp2Enabled = YES; |
| 35 BOOL gQuicEnabled = NO; | 36 BOOL gQuicEnabled = NO; |
| 36 cronet::URLRequestContextConfig::HttpCacheType gHttpCache = | 37 cronet::URLRequestContextConfig::HttpCacheType gHttpCache = |
| 37 cronet::URLRequestContextConfig::HttpCacheType::DISK; | 38 cronet::URLRequestContextConfig::HttpCacheType::DISK; |
| 38 ScopedVector<cronet::URLRequestContextConfig::QuicHint> gQuicHints; | 39 ScopedVector<cronet::URLRequestContextConfig::QuicHint> gQuicHints; |
| 39 NSString* gUserAgent = nil; | 40 NSString* gUserAgent = nil; |
| 40 BOOL gUserAgentPartial = NO; | 41 BOOL gUserAgentPartial = NO; |
| 41 NSString* gSslKeyLogFileName = nil; | 42 NSString* gSslKeyLogFileName = nil; |
| 42 RequestFilterBlock gRequestFilterBlock = nil; | 43 RequestFilterBlock gRequestFilterBlock = nil; |
| 43 std::unique_ptr<CronetHttpProtocolHandlerDelegate> gHttpProtocolHandlerDelegate; | 44 std::unique_ptr<CronetHttpProtocolHandlerDelegate> gHttpProtocolHandlerDelegate; |
| 44 NSURLCache* gPreservedSharedURLCache = nil; | 45 NSURLCache* gPreservedSharedURLCache = nil; |
| 45 BOOL gEnableTestCertVerifierForTesting = FALSE; | 46 BOOL gEnableTestCertVerifierForTesting = FALSE; |
| 47 NSString* gAcceptLanguages = nil; |
| 46 | 48 |
| 47 // CertVerifier, which allows any certificates for testing. | 49 // CertVerifier, which allows any certificates for testing. |
| 48 class TestCertVerifier : public net::CertVerifier { | 50 class TestCertVerifier : public net::CertVerifier { |
| 49 int Verify(const RequestParams& params, | 51 int Verify(const RequestParams& params, |
| 50 net::CRLSet* crl_set, | 52 net::CRLSet* crl_set, |
| 51 net::CertVerifyResult* verify_result, | 53 net::CertVerifyResult* verify_result, |
| 52 const net::CompletionCallback& callback, | 54 const net::CompletionCallback& callback, |
| 53 std::unique_ptr<Request>* out_req, | 55 std::unique_ptr<Request>* out_req, |
| 54 const net::NetLogWithSource& net_log) override { | 56 const net::NetLogWithSource& net_log) override { |
| 55 net::Error result = net::OK; | 57 net::Error result = net::OK; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 | 111 |
| 110 + (void)configureCronetEnvironmentForTesting: | 112 + (void)configureCronetEnvironmentForTesting: |
| 111 (cronet::CronetEnvironment*)cronetEnvironment { | 113 (cronet::CronetEnvironment*)cronetEnvironment { |
| 112 if (gEnableTestCertVerifierForTesting) { | 114 if (gEnableTestCertVerifierForTesting) { |
| 113 std::unique_ptr<TestCertVerifier> test_cert_verifier = | 115 std::unique_ptr<TestCertVerifier> test_cert_verifier = |
| 114 base::MakeUnique<TestCertVerifier>(); | 116 base::MakeUnique<TestCertVerifier>(); |
| 115 cronetEnvironment->set_mock_cert_verifier(std::move(test_cert_verifier)); | 117 cronetEnvironment->set_mock_cert_verifier(std::move(test_cert_verifier)); |
| 116 } | 118 } |
| 117 } | 119 } |
| 118 | 120 |
| 121 + (NSString*)getAcceptLanguagesFromPreferredLanguages: |
| 122 (NSArray<NSString*>*)languages { |
| 123 NSMutableArray* acceptLanguages = [NSMutableArray new]; |
| 124 for (NSString* lang_region in languages) { |
| 125 NSString* lang = [lang_region componentsSeparatedByString:@"-"][0]; |
| 126 NSString* localeAcceptLangs = acceptLangs[lang_region] ?: acceptLangs[lang]; |
| 127 if (localeAcceptLangs) |
| 128 [acceptLanguages |
| 129 addObjectsFromArray:[localeAcceptLangs |
| 130 componentsSeparatedByString:@","]]; |
| 131 } |
| 132 |
| 133 NSString* acceptLanguageString = |
| 134 [[[NSOrderedSet orderedSetWithArray:acceptLanguages] array] |
| 135 componentsJoinedByString:@","]; |
| 136 |
| 137 return [acceptLanguageString length] != 0 ? acceptLanguageString |
| 138 : @"en-US,en"; |
| 139 } |
| 140 |
| 119 + (NSString*)getAcceptLanguages { | 141 + (NSString*)getAcceptLanguages { |
| 120 // Use the framework bundle to search for resources. | 142 return [self |
| 121 NSBundle* frameworkBundle = [NSBundle bundleForClass:self]; | 143 getAcceptLanguagesFromPreferredLanguages:[NSLocale preferredLanguages]]; |
| 122 NSString* bundlePath = | 144 } |
| 123 [frameworkBundle pathForResource:@"cronet_resources" ofType:@"bundle"]; | 145 |
| 124 NSBundle* bundle = [NSBundle bundleWithPath:bundlePath]; | 146 + (void)setAcceptLanguages:(NSString*)acceptLanguages { |
| 125 NSString* acceptLanguages = NSLocalizedStringWithDefaultValue( | 147 [self checkNotStarted]; |
| 126 @"IDS_ACCEPT_LANGUAGES", @"Localizable", bundle, @"en-US,en", | 148 gAcceptLanguages = acceptLanguages; |
| 127 @"These values are copied from Chrome's .xtb files, so the same " | |
| 128 "values are used in the |Accept-Language| header. Key name matches " | |
| 129 "Chrome's."); | |
| 130 if (acceptLanguages == Nil) | |
| 131 acceptLanguages = @""; | |
| 132 return acceptLanguages; | |
| 133 } | 149 } |
| 134 | 150 |
| 135 + (void)checkNotStarted { | 151 + (void)checkNotStarted { |
| 136 CHECK(gChromeNet == NULL) << "Cronet is already started."; | 152 CHECK(gChromeNet == NULL) << "Cronet is already started."; |
| 137 } | 153 } |
| 138 | 154 |
| 139 + (void)setHttp2Enabled:(BOOL)http2Enabled { | 155 + (void)setHttp2Enabled:(BOOL)http2Enabled { |
| 140 [self checkNotStarted]; | 156 [self checkNotStarted]; |
| 141 gHttp2Enabled = http2Enabled; | 157 gHttp2Enabled = http2Enabled; |
| 142 } | 158 } |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 else | 202 else |
| 187 gRequestFilterBlock = block; | 203 gRequestFilterBlock = block; |
| 188 } | 204 } |
| 189 | 205 |
| 190 + (void)startInternal { | 206 + (void)startInternal { |
| 191 cronet::CronetEnvironment::Initialize(); | 207 cronet::CronetEnvironment::Initialize(); |
| 192 std::string user_agent = base::SysNSStringToUTF8(gUserAgent); | 208 std::string user_agent = base::SysNSStringToUTF8(gUserAgent); |
| 193 gChromeNet.Get().reset( | 209 gChromeNet.Get().reset( |
| 194 new cronet::CronetEnvironment(user_agent, gUserAgentPartial)); | 210 new cronet::CronetEnvironment(user_agent, gUserAgentPartial)); |
| 195 gChromeNet.Get()->set_accept_language( | 211 gChromeNet.Get()->set_accept_language( |
| 196 base::SysNSStringToUTF8([self getAcceptLanguages])); | 212 base::SysNSStringToUTF8(gAcceptLanguages ?: [self getAcceptLanguages])); |
| 197 | 213 |
| 198 gChromeNet.Get()->set_http2_enabled(gHttp2Enabled); | 214 gChromeNet.Get()->set_http2_enabled(gHttp2Enabled); |
| 199 gChromeNet.Get()->set_quic_enabled(gQuicEnabled); | 215 gChromeNet.Get()->set_quic_enabled(gQuicEnabled); |
| 200 gChromeNet.Get()->set_http_cache(gHttpCache); | 216 gChromeNet.Get()->set_http_cache(gHttpCache); |
| 201 gChromeNet.Get()->set_ssl_key_log_file_name( | 217 gChromeNet.Get()->set_ssl_key_log_file_name( |
| 202 base::SysNSStringToUTF8(gSslKeyLogFileName)); | 218 base::SysNSStringToUTF8(gSslKeyLogFileName)); |
| 203 for (const auto* quicHint : gQuicHints) { | 219 for (const auto* quicHint : gQuicHints) { |
| 204 gChromeNet.Get()->AddQuicHint(quicHint->host, quicHint->port, | 220 gChromeNet.Get()->AddQuicHint(quicHint->host, quicHint->port, |
| 205 quicHint->alternate_port); | 221 quicHint->alternate_port); |
| 206 } | 222 } |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 312 base::SysNSStringToUTF8(hostResolverRulesForTesting)); | 328 base::SysNSStringToUTF8(hostResolverRulesForTesting)); |
| 313 } | 329 } |
| 314 | 330 |
| 315 // This is a non-public dummy method that prevents the linker from stripping out | 331 // This is a non-public dummy method that prevents the linker from stripping out |
| 316 // the otherwise non-referenced methods from 'bidirectional_stream.cc'. | 332 // the otherwise non-referenced methods from 'bidirectional_stream.cc'. |
| 317 + (void)preventStrippingCronetBidirectionalStream { | 333 + (void)preventStrippingCronetBidirectionalStream { |
| 318 bidirectional_stream_create(NULL, 0, 0); | 334 bidirectional_stream_create(NULL, 0, 0); |
| 319 } | 335 } |
| 320 | 336 |
| 321 @end | 337 @end |
| OLD | NEW |