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 #ifndef IOS_CRNET_CRNET_ENVIRONMENT_H_
6 #define IOS_CRNET_CRNET_ENVIRONMENT_H_
8 #include "base/files/file_path.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/synchronization/waitable_event.h"
11 #include "base/threading/thread.h"
12 #import "ios/crnet/CrNet.h"
13 #include "ios/crnet/crnet_net_log.h"
14 #include "net/url_request/url_request.h"
15 #include "net/url_request/url_request_context.h"
19 class NetworkChangeNotifier
;
20 class ProxyConfigService
;
22 class URLRequestContextGetter
;
25 class CrNetHttpProtocolHandlerDelegate
;
27 // CrNetEnvironment contains all the network stack configuration
28 // and initialization.
29 class CrNetEnvironment
{
31 // Must be called on the main thread.
32 static void Initialize();
34 // |user_agent_product_name| will be used to generate the user-agent.
35 CrNetEnvironment(std::string user_agent_product_name
);
38 // Installs this CrNet environment so requests are intercepted.
39 // Can only be called once; to enable/disable CrNet at runtime, use
40 // SetHTTPProtocolHandlerRegistered.
43 // Installs this CrNet environment into the supplied
44 // NSURLSessionConfiguration. Settings are inherited from the shared
45 // NSURLSessionConfiguration, which Install() affects.
46 void InstallIntoSessionConfiguration(NSURLSessionConfiguration
* config
);
48 // The full user-agent.
49 std::string
user_agent();
51 // Returns the global request context getter for use in the network stack.
53 // The request context gathers all the settings needed to do an actual network
54 // request (cache type and path, cookies store, proxy setting ...).
55 // Chrome network stacks supports multiple active contexts, and this is used
56 // for example to separate Incognito data from the main profile data.
57 // CrNetEnvironment only implement one request context for now, but it can be
58 // extended in the future.
59 net::URLRequestContextGetter
* GetMainContextGetter();
61 // Enables or disables the HTTP protocol handler.
63 // When the HTTP protocol handler is registered, it will be used for all the
64 // network requests the application does (including requests from UIWebView).
65 void SetHTTPProtocolHandlerRegistered(bool registered
);
67 // Creates a new net log (overwrites existing file with this name). If
68 // actively logging, this call is ignored.
69 void StartNetLog(base::FilePath::StringType file_name
, bool log_bytes
);
70 // Stops logging and flushes file. If not currently logging this call is
73 // Closes all existing SPDY sessions with ERR_ABORTED.
74 void CloseAllSpdySessions();
76 // Sets the block used to determine whether or not CrNet should handle the
77 // request. If this is not set, CrNet will handle all requests.
78 // Must not be called while requests are in progress.
79 void SetRequestFilterBlock(RequestFilterBlock block
);
81 // Setters and getters for |spdy_enabled_|, |quic_enabled_|, and
82 // |forced_quic_origin_|. These only have any effect before Install() is
84 void set_spdy_enabled(bool enabled
) { spdy_enabled_
= enabled
; }
85 void set_quic_enabled(bool enabled
) { quic_enabled_
= enabled
; }
86 void set_alternate_protocol_threshold(double threshold
) {
87 alternate_protocol_threshold_
= threshold
;
90 bool spdy_enabled() const { return spdy_enabled_
; }
91 bool quic_enabled() const { return quic_enabled_
; }
92 double alternate_protocol_threshold() const {
93 return alternate_protocol_threshold_
;
96 // Clears the network stack's disk cache.
97 void ClearCache(ClearCacheCallback callback
);
100 // Runs a closure on the network thread.
101 void PostToNetworkThread(const tracked_objects::Location
& from_here
,
102 const base::Closure
& task
);
104 // Performs initialization tasks that must happen on the network thread.
105 void InitializeOnNetworkThread();
107 // Runs a closure on the file user blocking thread.
108 void PostToFileUserBlockingThread(const tracked_objects::Location
& from_here
,
109 const base::Closure
& task
);
111 // Helper methods that start/stop net-internals logging on the file
112 // user blocking thread.
113 void StartNetLogInternal(base::FilePath::StringType file_name
,
115 void StopNetLogInternal();
117 // Returns the HttpNeteworkSession object from the passed in
118 // URLRequestContext or NULL if none exists.
119 net::HttpNetworkSession
* GetHttpNetworkSession(
120 net::URLRequestContext
* context
);
122 // Helper method that closes all current SPDY sessions on the network IO
124 void CloseAllSpdySessionsInternal();
128 double alternate_protocol_threshold_
;
130 // Helper method that clears the cache on the network thread.
131 void ClearCacheOnNetworkThread(ClearCacheCallback callback
);
133 static CrNetEnvironment
* chrome_net_
;
134 scoped_ptr
<base::Thread
> network_io_thread_
;
135 scoped_ptr
<base::Thread
> network_cache_thread_
;
136 scoped_ptr
<base::Thread
> file_thread_
;
137 scoped_ptr
<base::Thread
> file_user_blocking_thread_
;
138 scoped_ptr
<net::SdchManager
> sdch_manager_
;
139 scoped_ptr
<net::NetworkChangeNotifier
> network_change_notifier_
;
140 scoped_ptr
<net::ProxyConfigService
> proxy_config_service_
;
141 scoped_ptr
<net::HttpServerProperties
> http_server_properties_
;
142 scoped_refptr
<net::URLRequestContextGetter
> main_context_getter_
;
143 scoped_ptr
<net::URLRequestContext
> main_context_
;
144 scoped_ptr
<CrNetHttpProtocolHandlerDelegate
> http_protocol_handler_delegate_
;
145 std::string user_agent_product_name_
;
146 base::Lock net_log_lock_
;
147 bool net_log_started_
;
148 scoped_ptr
<CrNetNetLog
> net_log_
;
150 DISALLOW_COPY_AND_ASSIGN(CrNetEnvironment
);
153 #endif // IOS_CRNET_CRNET_ENVIRONMENT_H_