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 #import "ios/net/crn_http_protocol_handler.h"
9 #import "ios/crnet/crnet_environment.h"
11 static CrNetEnvironment* g_chrome_net = NULL;
13 static BOOL g_spdy_enabled = YES;
14 static BOOL g_quic_enabled = NO;
15 static NSString *g_user_agent = nil;
16 static double g_alternate_protocol_threshold = 1.0;
17 static RequestFilterBlock g_request_filter_block = nil;
21 + (void)setSpdyEnabled:(BOOL)spdyEnabled {
22 g_spdy_enabled = spdyEnabled;
25 + (void)setQuicEnabled:(BOOL)quicEnabled {
26 g_quic_enabled = quicEnabled;
29 + (void)setPartialUserAgent:(NSString *)userAgent {
30 g_user_agent = userAgent;
33 + (void)setAlternateProtocolThreshold:(double)alternateProtocolThreshold {
34 g_alternate_protocol_threshold = alternateProtocolThreshold;
37 + (void)installInternal {
38 CrNetEnvironment::Initialize();
39 std::string partial_user_agent =
40 [g_user_agent cStringUsingEncoding:NSASCIIStringEncoding];
41 g_chrome_net = new CrNetEnvironment(partial_user_agent);
43 g_chrome_net->set_spdy_enabled(g_spdy_enabled);
44 g_chrome_net->set_quic_enabled(g_quic_enabled);
45 g_chrome_net->set_alternate_protocol_threshold(
46 g_alternate_protocol_threshold);
48 g_chrome_net->Install();
49 g_chrome_net->SetHTTPProtocolHandlerRegistered(true);
50 g_chrome_net->SetRequestFilterBlock(g_request_filter_block);
54 static dispatch_once_t onceToken;
55 dispatch_once(&onceToken, ^{
56 [self installInternal];
60 + (void)installIntoSessionConfiguration:(NSURLSessionConfiguration*)config {
61 g_chrome_net->InstallIntoSessionConfiguration(config);
64 + (void)installWithPartialUserAgent:(NSString *)partialUserAgent {
65 [self setPartialUserAgent:partialUserAgent];
69 + (void)installWithPartialUserAgent:(NSString *)partialUserAgent
70 enableDataReductionProxy:(BOOL)enableDataReductionProxy {
71 LOG(ERROR) << "enableDataReductionProxy is no longer respected. The "
72 << "functionality has been removed from CrNet.";
74 [self setPartialUserAgent:partialUserAgent];
78 + (void)installWithPartialUserAgent:(NSString *)partialUserAgent
79 withRequestFilterBlock:(RequestFilterBlock)requestFilterBlock {
80 [self setPartialUserAgent:partialUserAgent];
81 [self setRequestFilterBlock:requestFilterBlock];
85 + (void)setRequestFilterBlock:(RequestFilterBlock)block {
87 g_chrome_net->SetRequestFilterBlock(block);
89 g_request_filter_block = block;
94 g_chrome_net->SetHTTPProtocolHandlerRegistered(false);
98 + (void)startNetLogToFile:(NSString *)fileName logBytes:(BOOL)logBytes {
99 if (g_chrome_net && [fileName length]) {
100 g_chrome_net->StartNetLog([fileName UTF8String], logBytes);
106 return g_chrome_net->StopNetLog();
110 + (NSString *)userAgent {
115 std::string user_agent = g_chrome_net->user_agent();
116 return [NSString stringWithCString:user_agent.c_str()
117 encoding:[NSString defaultCStringEncoding]];
120 + (void)closeAllSpdySessions {
122 return g_chrome_net->CloseAllSpdySessions();
126 + (void)clearCacheWithCompletionCallback:(ClearCacheCallback)clearCacheCallback {
128 g_chrome_net->ClearCache(clearCacheCallback);