Chromium Code Reviews| Index: components/cronet/ios/Cronet.mm |
| diff --git a/components/cronet/ios/Cronet.mm b/components/cronet/ios/Cronet.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3ee1b802d761084a068bec9d09276783753fdd18 |
| --- /dev/null |
| +++ b/components/cronet/ios/Cronet.mm |
| @@ -0,0 +1,123 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#import "components/cronet/ios/Cronet.h" |
| + |
| +#include "base/lazy_instance.h" |
| +#include "base/logging.h" |
| +#include "base/memory/scoped_ptr.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" |
| + |
| +namespace { |
| + |
| +// 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. |
| +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
|
| + LAZY_INSTANCE_INITIALIZER; |
| + |
| +BOOL gHttp2Enabled = YES; |
| +BOOL gQuicEnabled = NO; |
| +ScopedVector<cronet::URLRequestContextConfig::QuicHint> gQuicHints; |
| +NSString* gUserAgent = nil; |
| +NSString* gSslKeyLogFileName = nil; |
| + |
| +void CheckNotStarted() { |
| + CHECK(gChromeNet == NULL) << "Cronet is already started."; |
| +} |
| + |
| +} // namespace |
| + |
| +@implementation Cronet |
| + |
| ++ (void)setHttp2Enabled:(BOOL)http2Enabled { |
| + CheckNotStarted(); |
| + gHttp2Enabled = http2Enabled; |
| +} |
| + |
| ++ (void)setQuicEnabled:(BOOL)quicEnabled { |
| + CheckNotStarted(); |
| + gQuicEnabled = quicEnabled; |
| +} |
| + |
| ++ (void)addQuicHint:(NSString*)host port:(int)port altPort:(int)altPort { |
| + CheckNotStarted(); |
| + gQuicHints.push_back(new cronet::URLRequestContextConfig::QuicHint( |
| + base::SysNSStringToUTF8(host), port, altPort)); |
| +} |
| + |
| ++ (void)setPartialUserAgent:(NSString*)userAgent { |
| + CheckNotStarted(); |
| + gUserAgent = userAgent; |
| +} |
| + |
| ++ (void)setSslKeyLogFileName:(NSString*)sslKeyLogFileName { |
| + CheckNotStarted(); |
| + gSslKeyLogFileName = sslKeyLogFileName; |
| +} |
| + |
| ++ (void)installInternal { |
| + cronet::CronetEnvironment::Initialize(); |
| + std::string partialUserAgent = base::SysNSStringToUTF8(gUserAgent); |
| + gChromeNet.Get().reset(new cronet::CronetEnvironment(partialUserAgent)); |
| + |
| + gChromeNet.Get()->set_http2_enabled(gHttp2Enabled); |
| + gChromeNet.Get()->set_quic_enabled(gQuicEnabled); |
| + gChromeNet.Get()->set_ssl_key_log_file_name( |
| + base::SysNSStringToUTF8(gSslKeyLogFileName)); |
| + for (const auto& quicHint : gQuicHints) { |
| + gChromeNet.Get()->AddQuicHint(quicHint->host, quicHint->port, |
| + quicHint->alternate_port); |
| + } |
| + gChromeNet.Get()->Install(); |
| +} |
| + |
| ++ (void)start { |
| + static dispatch_once_t onceToken; |
| + dispatch_once(&onceToken, ^{ |
| + if (![NSThread isMainThread]) { |
| + dispatch_sync(dispatch_get_main_queue(), ^(void) { |
| + [self installInternal]; |
| + }); |
| + } else { |
| + [self installInternal]; |
| + } |
| + }); |
| +} |
| + |
| ++ (void)startNetLogToFile:(NSString*)fileName logBytes:(BOOL)logBytes { |
| + if (gChromeNet.Get().get() && [fileName length]) { |
| + gChromeNet.Get()->StartNetLog([fileName UTF8String], logBytes); |
| + } |
| +} |
| + |
| ++ (void)stopNetLog { |
| + if (gChromeNet.Get().get()) { |
| + gChromeNet.Get()->StopNetLog(); |
| + } |
| +} |
| + |
| ++ (NSString*)getUserAgent { |
| + if (!gChromeNet.Get().get()) { |
| + return nil; |
| + } |
| + |
| + return [NSString stringWithCString:gChromeNet.Get()->user_agent().c_str() |
| + encoding:[NSString defaultCStringEncoding]]; |
| +} |
| + |
| ++ (cronet_engine*)getGlobalEngine { |
| + DCHECK(gChromeNet.Get().get()); |
| + if (gChromeNet.Get().get()) { |
| + static cronet_engine engine; |
| + engine.obj = gChromeNet.Get().get(); |
| + return &engine; |
| + } |
| + return nil; |
| +} |
| + |
| +@end |