Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #import "components/cronet/ios/Cronet.h" | |
| 6 | |
| 7 #include "base/lazy_instance.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "base/memory/scoped_vector.h" | |
| 11 #include "base/strings/sys_string_conversions.h" | |
| 12 #include "components/cronet/ios/cronet_environment.h" | |
| 13 #include "components/cronet/url_request_context_config.h" | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 // Currently there is one and only one instance of CronetEnvironment, | |
| 18 // which is leaked at the shutdown. We should consider allowing multiple | |
| 19 // instances if that makes sense in the future. | |
| 20 base::LazyInstance<scoped_ptr<cronet::CronetEnvironment>>::Leaky gChromeNet = | |
|
xunjieli
2016/04/13 15:28:23
Hmm.. There isn't an example of using LazyInstance
mef
2016/04/14 14:07:24
There are a few with scoped_refptr though, for exa
xunjieli
2016/04/14 15:43:39
I see. base/lazy_instance.h doesn't mention this u
mef
2016/04/14 21:17:35
It is possible, I'm just not sure whether it is wo
| |
| 21 LAZY_INSTANCE_INITIALIZER; | |
| 22 | |
| 23 BOOL gHttp2Enabled = YES; | |
| 24 BOOL gQuicEnabled = NO; | |
| 25 ScopedVector<cronet::URLRequestContextConfig::QuicHint> gQuicHints; | |
| 26 NSString* gUserAgent = nil; | |
| 27 NSString* gSslKeyLogFileName = nil; | |
| 28 | |
| 29 void CheckNotStarted() { | |
| 30 CHECK(gChromeNet == NULL) << "Cronet is already started."; | |
| 31 } | |
| 32 | |
| 33 } // namespace | |
| 34 | |
| 35 @implementation Cronet | |
| 36 | |
| 37 + (void)setHttp2Enabled:(BOOL)http2Enabled { | |
| 38 CheckNotStarted(); | |
| 39 gHttp2Enabled = http2Enabled; | |
| 40 } | |
| 41 | |
| 42 + (void)setQuicEnabled:(BOOL)quicEnabled { | |
| 43 CheckNotStarted(); | |
| 44 gQuicEnabled = quicEnabled; | |
| 45 } | |
| 46 | |
| 47 + (void)addQuicHint:(NSString*)host port:(int)port altPort:(int)altPort { | |
| 48 CheckNotStarted(); | |
| 49 gQuicHints.push_back(new cronet::URLRequestContextConfig::QuicHint( | |
| 50 base::SysNSStringToUTF8(host), port, altPort)); | |
| 51 } | |
| 52 | |
| 53 + (void)setPartialUserAgent:(NSString*)userAgent { | |
| 54 CheckNotStarted(); | |
| 55 gUserAgent = userAgent; | |
| 56 } | |
| 57 | |
| 58 + (void)setSslKeyLogFileName:(NSString*)sslKeyLogFileName { | |
| 59 CheckNotStarted(); | |
| 60 gSslKeyLogFileName = sslKeyLogFileName; | |
| 61 } | |
| 62 | |
| 63 + (void)installInternal { | |
| 64 cronet::CronetEnvironment::Initialize(); | |
| 65 std::string partialUserAgent = base::SysNSStringToUTF8(gUserAgent); | |
| 66 gChromeNet.Get().reset(new cronet::CronetEnvironment(partialUserAgent)); | |
| 67 | |
| 68 gChromeNet.Get()->set_http2_enabled(gHttp2Enabled); | |
| 69 gChromeNet.Get()->set_quic_enabled(gQuicEnabled); | |
| 70 gChromeNet.Get()->set_ssl_key_log_file_name( | |
| 71 base::SysNSStringToUTF8(gSslKeyLogFileName)); | |
| 72 for (const auto& quicHint : gQuicHints) { | |
| 73 gChromeNet.Get()->AddQuicHint(quicHint->host, quicHint->port, | |
| 74 quicHint->alternate_port); | |
| 75 } | |
| 76 gChromeNet.Get()->Install(); | |
| 77 } | |
| 78 | |
| 79 + (void)start { | |
| 80 static dispatch_once_t onceToken; | |
| 81 dispatch_once(&onceToken, ^{ | |
| 82 if (![NSThread isMainThread]) { | |
| 83 dispatch_sync(dispatch_get_main_queue(), ^(void) { | |
| 84 [self installInternal]; | |
| 85 }); | |
| 86 } else { | |
| 87 [self installInternal]; | |
| 88 } | |
| 89 }); | |
| 90 } | |
| 91 | |
| 92 + (void)startNetLogToFile:(NSString*)fileName logBytes:(BOOL)logBytes { | |
| 93 if (gChromeNet.Get().get() && [fileName length]) { | |
| 94 gChromeNet.Get()->StartNetLog([fileName UTF8String], logBytes); | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 + (void)stopNetLog { | |
| 99 if (gChromeNet.Get().get()) { | |
| 100 gChromeNet.Get()->StopNetLog(); | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 + (NSString*)getUserAgent { | |
| 105 if (!gChromeNet.Get().get()) { | |
| 106 return nil; | |
| 107 } | |
| 108 | |
| 109 return [NSString stringWithCString:gChromeNet.Get()->user_agent().c_str() | |
| 110 encoding:[NSString defaultCStringEncoding]]; | |
| 111 } | |
| 112 | |
| 113 + (cronet_engine*)getGlobalEngine { | |
| 114 DCHECK(gChromeNet.Get().get()); | |
| 115 if (gChromeNet.Get().get()) { | |
| 116 static cronet_engine engine; | |
| 117 engine.obj = gChromeNet.Get().get(); | |
| 118 return &engine; | |
| 119 } | |
| 120 return nil; | |
| 121 } | |
| 122 | |
| 123 @end | |
| OLD | NEW |