1 // Copyright 2014 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.
5 #import "ios/crnet/CrNet.h"
7 #include "base/logging.h"
8 #include "base/strings/sys_string_conversions.h"
9 #import "ios/net/crn_http_protocol_handler.h"
10 #import "ios/crnet/crnet_environment.h"
12 static CrNetEnvironment* g_chrome_net = NULL;
14 static BOOL g_spdy_enabled = YES;
15 static BOOL g_quic_enabled = NO;
16 static BOOL g_sdch_enabled = NO;
17 static NSString* g_user_agent = nil;
18 static NSString* g_sdch_pref_store_filename = nil;
19 static double g_alternate_protocol_threshold = 1.0;
20 static RequestFilterBlock g_request_filter_block = nil;
24 + (void)setSpdyEnabled:(BOOL)spdyEnabled {
25 g_spdy_enabled = spdyEnabled;
28 + (void)setQuicEnabled:(BOOL)quicEnabled {
29 g_quic_enabled = quicEnabled;
32 + (void)setSDCHEnabled:(BOOL)sdchEnabled
33 withPrefStore:(NSString*)filename {
34 g_sdch_enabled = sdchEnabled;
35 g_sdch_pref_store_filename = filename;
38 + (void)setPartialUserAgent:(NSString *)userAgent {
39 g_user_agent = userAgent;
42 + (void)setAlternateProtocolThreshold:(double)alternateProtocolThreshold {
43 g_alternate_protocol_threshold = alternateProtocolThreshold;
46 + (void)installInternal {
47 CrNetEnvironment::Initialize();
48 std::string partial_user_agent = base::SysNSStringToUTF8(g_user_agent);
49 g_chrome_net = new CrNetEnvironment(partial_user_agent);
51 g_chrome_net->set_spdy_enabled(g_spdy_enabled);
52 g_chrome_net->set_quic_enabled(g_quic_enabled);
53 g_chrome_net->set_sdch_enabled(g_sdch_enabled);
54 if (g_sdch_pref_store_filename) {
55 std::string filename = base::SysNSStringToUTF8(g_sdch_pref_store_filename);
56 g_chrome_net->set_sdch_pref_store_filename(filename);
58 g_chrome_net->set_alternate_protocol_threshold(
59 g_alternate_protocol_threshold);
61 g_chrome_net->Install();
62 g_chrome_net->SetHTTPProtocolHandlerRegistered(true);
63 g_chrome_net->SetRequestFilterBlock(g_request_filter_block);
67 static dispatch_once_t onceToken;
68 dispatch_once(&onceToken, ^{
69 [self installInternal];
73 + (void)installIntoSessionConfiguration:(NSURLSessionConfiguration*)config {
74 g_chrome_net->InstallIntoSessionConfiguration(config);
77 + (void)installWithPartialUserAgent:(NSString *)partialUserAgent {
78 [self setPartialUserAgent:partialUserAgent];
82 + (void)installWithPartialUserAgent:(NSString *)partialUserAgent
83 enableDataReductionProxy:(BOOL)enableDataReductionProxy {
84 LOG(ERROR) << "enableDataReductionProxy is no longer respected. The "
85 << "functionality has been removed from CrNet.";
87 [self setPartialUserAgent:partialUserAgent];
91 + (void)installWithPartialUserAgent:(NSString *)partialUserAgent
92 withRequestFilterBlock:(RequestFilterBlock)requestFilterBlock {
93 [self setPartialUserAgent:partialUserAgent];
94 [self setRequestFilterBlock:requestFilterBlock];
98 + (void)setRequestFilterBlock:(RequestFilterBlock)block {
100 g_chrome_net->SetRequestFilterBlock(block);
102 g_request_filter_block = block;
107 g_chrome_net->SetHTTPProtocolHandlerRegistered(false);
111 + (void)startNetLogToFile:(NSString *)fileName logBytes:(BOOL)logBytes {
112 if (g_chrome_net && [fileName length]) {
113 g_chrome_net->StartNetLog([fileName UTF8String], logBytes);
119 return g_chrome_net->StopNetLog();
123 + (NSString *)userAgent {
128 std::string user_agent = g_chrome_net->user_agent();
129 return [NSString stringWithCString:user_agent.c_str()
130 encoding:[NSString defaultCStringEncoding]];
133 + (void)closeAllSpdySessions {
135 return g_chrome_net->CloseAllSpdySessions();
139 + (void)clearCacheWithCompletionCallback:(ClearCacheCallback)clearCacheCallback {
141 g_chrome_net->ClearCache(clearCacheCallback);