Implement proximity_auth::WireMessage::Serialize().
[chromium-blink-merge.git] / ios / crnet / CrNet.mm
blob9ea2dd99e39122150cb2d056306c0953f779cffb
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;
19 @implementation CrNet
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);
53 + (void)install {
54   static dispatch_once_t onceToken;
55   dispatch_once(&onceToken, ^{
56       [self installInternal];
57   });
60 + (void)installIntoSessionConfiguration:(NSURLSessionConfiguration*)config {
61   g_chrome_net->InstallIntoSessionConfiguration(config);
64 + (void)installWithPartialUserAgent:(NSString *)partialUserAgent {
65   [self setPartialUserAgent:partialUserAgent];
66   [self install];
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];
75   [self install];
78 + (void)installWithPartialUserAgent:(NSString *)partialUserAgent
79              withRequestFilterBlock:(RequestFilterBlock)requestFilterBlock {
80   [self setPartialUserAgent:partialUserAgent];
81   [self setRequestFilterBlock:requestFilterBlock];
82   [self install];
85 + (void)setRequestFilterBlock:(RequestFilterBlock)block {
86   if (g_chrome_net)
87     g_chrome_net->SetRequestFilterBlock(block);
88   else
89     g_request_filter_block = block;
92 + (void)uninstall {
93   if (g_chrome_net) {
94     g_chrome_net->SetHTTPProtocolHandlerRegistered(false);
95   }
98 + (void)startNetLogToFile:(NSString *)fileName logBytes:(BOOL)logBytes {
99   if (g_chrome_net && [fileName length]) {
100     g_chrome_net->StartNetLog([fileName UTF8String], logBytes);
101   }
104 + (void)stopNetLog {
105   if (g_chrome_net) {
106     return g_chrome_net->StopNetLog();
107   }
110 + (NSString *)userAgent {
111   if (!g_chrome_net) {
112     return nil;
113   }
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 {
121   if (g_chrome_net) {
122     return g_chrome_net->CloseAllSpdySessions();
123   }
126 + (void)clearCacheWithCompletionCallback:(ClearCacheCallback)clearCacheCallback {
127   if (g_chrome_net) {
128     g_chrome_net->ClearCache(clearCacheCallback);
129   }
132 @end