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 "net/url_request/url_request.h"
14 #include "net/url_request/url_request_context.h"
20 class NetworkChangeNotifier
;
22 class ProxyConfigService
;
25 class URLRequestContextGetter
;
26 class WriteToFileNetLogObserver
;
29 class CrNetHttpProtocolHandlerDelegate
;
31 // CrNetEnvironment contains all the network stack configuration
32 // and initialization.
33 class CrNetEnvironment
{
35 // Must be called on the main thread.
36 static void Initialize();
38 // |user_agent_product_name| will be used to generate the user-agent.
39 CrNetEnvironment(std::string user_agent_product_name
);
42 // Installs this CrNet environment so requests are intercepted.
43 // Can only be called once; to enable/disable CrNet at runtime, use
44 // SetHTTPProtocolHandlerRegistered.
47 // Installs this CrNet environment into the supplied
48 // NSURLSessionConfiguration. Settings are inherited from the shared
49 // NSURLSessionConfiguration, which Install() affects.
50 void InstallIntoSessionConfiguration(NSURLSessionConfiguration
* config
);
52 // The full user-agent.
53 std::string
user_agent();
55 // Returns the global request context getter for use in the network stack.
57 // The request context gathers all the settings needed to do an actual network
58 // request (cache type and path, cookies store, proxy setting ...).
59 // Chrome network stacks supports multiple active contexts, and this is used
60 // for example to separate Incognito data from the main profile data.
61 // CrNetEnvironment only implement one request context for now, but it can be
62 // extended in the future.
63 net::URLRequestContextGetter
* GetMainContextGetter();
65 // Enables or disables the HTTP protocol handler.
67 // When the HTTP protocol handler is registered, it will be used for all the
68 // network requests the application does (including requests from UIWebView).
69 void SetHTTPProtocolHandlerRegistered(bool registered
);
71 // Creates a new net log (overwrites existing file with this name). If
72 // actively logging, this call is ignored.
73 void StartNetLog(base::FilePath::StringType file_name
, bool log_bytes
);
74 // Stops logging and flushes file. If not currently logging this call is
77 // Closes all existing SPDY sessions with ERR_ABORTED.
78 void CloseAllSpdySessions();
80 // Sets the block used to determine whether or not CrNet should handle the
81 // request. If this is not set, CrNet will handle all requests.
82 // Must not be called while requests are in progress.
83 void SetRequestFilterBlock(RequestFilterBlock block
);
85 // Setters and getters for |spdy_enabled_|, |quic_enabled_|, and
86 // |forced_quic_origin_|. These only have any effect before Install() is
88 void set_spdy_enabled(bool enabled
) { spdy_enabled_
= enabled
; }
89 void set_quic_enabled(bool enabled
) { quic_enabled_
= enabled
; }
90 void set_sdch_enabled(bool enabled
) { sdch_enabled_
= enabled
; }
91 void set_sdch_pref_store_filename(const std::string
& pref_store
) {
92 sdch_pref_store_filename_
= pref_store
;
94 void set_alternate_protocol_threshold(double threshold
) {
95 alternate_protocol_threshold_
= threshold
;
98 bool spdy_enabled() const { return spdy_enabled_
; }
99 bool quic_enabled() const { return quic_enabled_
; }
100 bool sdch_enabled() const { return sdch_enabled_
; }
101 double alternate_protocol_threshold() const {
102 return alternate_protocol_threshold_
;
105 // Clears the network stack's disk cache.
106 void ClearCache(ClearCacheCallback callback
);
109 // Runs a closure on the network thread.
110 void PostToNetworkThread(const tracked_objects::Location
& from_here
,
111 const base::Closure
& task
);
113 // Configures SDCH on the network thread. If SDCH is enabled, sets up
114 // SdchManager, and configures persistence as needed.
115 void ConfigureSdchOnNetworkThread();
117 // Performs initialization tasks that must happen on the network thread.
118 void InitializeOnNetworkThread();
120 // Runs a closure on the file user blocking thread.
121 void PostToFileUserBlockingThread(const tracked_objects::Location
& from_here
,
122 const base::Closure
& task
);
124 // Helper methods that start/stop net-internals logging on the file
125 // user blocking thread.
126 void StartNetLogInternal(base::FilePath::StringType file_name
,
128 void StopNetLogInternal();
130 // Returns the HttpNeteworkSession object from the passed in
131 // URLRequestContext or NULL if none exists.
132 net::HttpNetworkSession
* GetHttpNetworkSession(
133 net::URLRequestContext
* context
);
135 // Helper method that closes all current SPDY sessions on the network IO
137 void CloseAllSpdySessionsInternal();
142 std::string sdch_pref_store_filename_
;
143 double alternate_protocol_threshold_
;
145 // Helper method that clears the cache on the network thread.
146 void ClearCacheOnNetworkThread(ClearCacheCallback callback
);
148 static CrNetEnvironment
* chrome_net_
;
149 scoped_ptr
<base::Thread
> network_io_thread_
;
150 scoped_ptr
<base::Thread
> network_cache_thread_
;
151 scoped_ptr
<base::Thread
> file_thread_
;
152 scoped_ptr
<base::Thread
> file_user_blocking_thread_
;
153 scoped_ptr
<net::SdchManager
> sdch_manager_
;
154 scoped_ptr
<net::SdchOwner
> sdch_owner_
;
155 scoped_refptr
<base::SequencedTaskRunner
> pref_store_worker_pool_
;
156 scoped_refptr
<JsonPrefStore
> net_pref_store_
;
157 scoped_ptr
<net::NetworkChangeNotifier
> network_change_notifier_
;
158 scoped_ptr
<net::ProxyConfigService
> proxy_config_service_
;
159 scoped_ptr
<net::HttpServerProperties
> http_server_properties_
;
160 scoped_refptr
<net::URLRequestContextGetter
> main_context_getter_
;
161 scoped_ptr
<net::URLRequestContext
> main_context_
;
162 scoped_ptr
<CrNetHttpProtocolHandlerDelegate
> http_protocol_handler_delegate_
;
163 std::string user_agent_product_name_
;
164 scoped_ptr
<net::NetLog
> net_log_
;
165 scoped_ptr
<net::WriteToFileNetLogObserver
> net_log_observer_
;
167 DISALLOW_COPY_AND_ASSIGN(CrNetEnvironment
);
170 #endif // IOS_CRNET_CRNET_ENVIRONMENT_H_