forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrNet.mm
144 lines (117 loc) · 3.89 KB
/
CrNet.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2014 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 "ios/crnet/CrNet.h"
#include "base/logging.h"
#include "base/strings/sys_string_conversions.h"
#import "ios/net/crn_http_protocol_handler.h"
#import "ios/crnet/crnet_environment.h"
static CrNetEnvironment* g_chrome_net = NULL;
static BOOL g_http2_enabled = YES;
static BOOL g_quic_enabled = NO;
static BOOL g_sdch_enabled = NO;
static NSString* g_user_agent = nil;
static NSString* g_sdch_pref_store_filename = nil;
static RequestFilterBlock g_request_filter_block = nil;
@implementation CrNet
+ (void)setHttp2Enabled:(BOOL)http2Enabled {
g_http2_enabled = http2Enabled;
}
+ (void)setQuicEnabled:(BOOL)quicEnabled {
g_quic_enabled = quicEnabled;
}
+ (void)setSDCHEnabled:(BOOL)sdchEnabled
withPrefStore:(NSString*)filename {
g_sdch_enabled = sdchEnabled;
g_sdch_pref_store_filename = filename;
}
+ (void)setPartialUserAgent:(NSString *)userAgent {
g_user_agent = userAgent;
}
+ (void)installInternal {
CrNetEnvironment::Initialize();
std::string partial_user_agent = base::SysNSStringToUTF8(g_user_agent);
g_chrome_net = new CrNetEnvironment(partial_user_agent);
g_chrome_net->set_spdy_enabled(g_http2_enabled);
g_chrome_net->set_quic_enabled(g_quic_enabled);
g_chrome_net->set_sdch_enabled(g_sdch_enabled);
if (g_sdch_pref_store_filename) {
std::string filename = base::SysNSStringToUTF8(g_sdch_pref_store_filename);
g_chrome_net->set_sdch_pref_store_filename(filename);
}
g_chrome_net->Install();
g_chrome_net->SetHTTPProtocolHandlerRegistered(true);
g_chrome_net->SetRequestFilterBlock(g_request_filter_block);
}
+ (void)install {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (![NSThread isMainThread]) {
dispatch_sync(dispatch_get_main_queue(), ^(void) {
[self installInternal];
});
} else {
[self installInternal];
}
});
}
+ (void)installIntoSessionConfiguration:(NSURLSessionConfiguration*)config {
g_chrome_net->InstallIntoSessionConfiguration(config);
}
+ (void)installWithPartialUserAgent:(NSString *)partialUserAgent {
[self setPartialUserAgent:partialUserAgent];
[self install];
}
+ (void)installWithPartialUserAgent:(NSString *)partialUserAgent
enableDataReductionProxy:(BOOL)enableDataReductionProxy {
LOG(ERROR) << "enableDataReductionProxy is no longer respected. The "
<< "functionality has been removed from CrNet.";
[self setPartialUserAgent:partialUserAgent];
[self install];
}
+ (void)installWithPartialUserAgent:(NSString *)partialUserAgent
withRequestFilterBlock:(RequestFilterBlock)requestFilterBlock {
[self setPartialUserAgent:partialUserAgent];
[self setRequestFilterBlock:requestFilterBlock];
[self install];
}
+ (void)setRequestFilterBlock:(RequestFilterBlock)block {
if (g_chrome_net)
g_chrome_net->SetRequestFilterBlock(block);
else
g_request_filter_block = block;
}
+ (void)uninstall {
if (g_chrome_net) {
g_chrome_net->SetHTTPProtocolHandlerRegistered(false);
}
}
+ (void)startNetLogToFile:(NSString *)fileName logBytes:(BOOL)logBytes {
if (g_chrome_net && [fileName length]) {
g_chrome_net->StartNetLog([fileName UTF8String], logBytes);
}
}
+ (void)stopNetLog {
if (g_chrome_net) {
return g_chrome_net->StopNetLog();
}
}
+ (NSString *)userAgent {
if (!g_chrome_net) {
return nil;
}
std::string user_agent = g_chrome_net->user_agent();
return [NSString stringWithCString:user_agent.c_str()
encoding:[NSString defaultCStringEncoding]];
}
+ (void)closeAllSpdySessions {
if (g_chrome_net) {
return g_chrome_net->CloseAllSpdySessions();
}
}
+ (void)clearCacheWithCompletionCallback:(ClearCacheCallback)clearCacheCallback {
if (g_chrome_net) {
g_chrome_net->ClearCache(clearCacheCallback);
}
}
@end