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..35f8520846ad9c78d2388b5c70b1f8fce88514c3 |
| --- /dev/null |
| +++ b/components/cronet/ios/Cronet.mm |
| @@ -0,0 +1,113 @@ |
| +// 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 <vector> |
| + |
| +#include "base/logging.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" |
| + |
| +static cronet::CronetEnvironment* gChromeNet = NULL; |
| + |
| +static BOOL gHttp2Enabled = YES; |
| +static BOOL gQuicEnabled = NO; |
| +static ScopedVector<cronet::URLRequestContextConfig::QuicHint> gQuicHints; |
| +static NSString* gUserAgent = nil; |
| +static NSString* gSslKeyLogFileName = nil; |
| + |
| +namespace { |
| +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 partial_user_agent = base::SysNSStringToUTF8(gUserAgent); |
|
kapishnikov
2016/04/08 23:54:18
Local variables should use mixed case to delimit w
mef
2016/04/09 00:52:29
Done.
|
| + gChromeNet = new cronet::CronetEnvironment(partial_user_agent); |
| + |
| + gChromeNet->set_http2_enabled(gHttp2Enabled); |
| + gChromeNet->set_quic_enabled(gQuicEnabled); |
| + gChromeNet->set_ssl_key_log_file_name( |
| + base::SysNSStringToUTF8(gSslKeyLogFileName)); |
| + for (auto quicHint : gQuicHints) { |
| + gChromeNet->AddQuicHint(quicHint->host, quicHint->port, |
| + quicHint->alternate_port); |
| + } |
| + gChromeNet->Install(); |
| +} |
| + |
| ++ (void)start { |
| + static dispatch_once_t onceToken; |
| + dispatch_once(&onceToken, ^{ |
| + [self installInternal]; |
|
kapishnikov
2016/04/08 23:54:18
cronet::CronetEnvironment::Initialize() states tha
kapishnikov
2016/04/09 00:02:11
Actually it will cause the deadlock if the method
mef
2016/04/09 00:52:29
Great point! I thought about it, but didn't know h
|
| + }); |
| +} |
| + |
| ++ (void)startNetLogToFile:(NSString*)fileName logBytes:(BOOL)logBytes { |
|
kapishnikov
2016/04/08 23:54:18
Nit: space after 'NSString'
mef
2016/04/09 00:52:29
Acknowledged. git cl format.
|
| + if (gChromeNet && [fileName length]) { |
| + gChromeNet->StartNetLog([fileName UTF8String], logBytes); |
| + } |
| +} |
| + |
| ++ (void)stopNetLog { |
| + if (gChromeNet) { |
| + gChromeNet->StopNetLog(); |
| + } |
| +} |
| + |
| ++ (NSString*)getUserAgent { |
| + if (!gChromeNet) { |
| + return nil; |
| + } |
| + |
| + std::string user_agent = gChromeNet->user_agent(); |
| + return [NSString stringWithCString:user_agent.c_str() |
| + encoding:[NSString defaultCStringEncoding]]; |
| +} |
| + |
| ++ (cronet_engine*)getGlobalEngine { |
| + DCHECK(gChromeNet); |
| + if (gChromeNet) { |
| + static cronet_engine engine; |
| + engine.obj = gChromeNet; |
| + return &engine; |
| + } |
| + return nil; |
| +} |
| + |
| +@end |