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"
18 class NetworkChangeNotifier
;
20 class ProxyConfigService
;
22 class URLRequestContextGetter
;
23 class WriteToFileNetLogObserver
;
26 class CrNetHttpProtocolHandlerDelegate
;
28 // CrNetEnvironment contains all the network stack configuration
29 // and initialization.
30 class CrNetEnvironment
{
32 // Must be called on the main thread.
33 static void Initialize();
35 // |user_agent_product_name| will be used to generate the user-agent.
36 CrNetEnvironment(std::string user_agent_product_name
);
39 // Installs this CrNet environment so requests are intercepted.
40 // Can only be called once; to enable/disable CrNet at runtime, use
41 // SetHTTPProtocolHandlerRegistered.
44 // Installs this CrNet environment into the supplied
45 // NSURLSessionConfiguration. Settings are inherited from the shared
46 // NSURLSessionConfiguration, which Install() affects.
47 void InstallIntoSessionConfiguration(NSURLSessionConfiguration
* config
);
49 // The full user-agent.
50 std::string
user_agent();
52 // Returns the global request context getter for use in the network stack.
54 // The request context gathers all the settings needed to do an actual network
55 // request (cache type and path, cookies store, proxy setting ...).
56 // Chrome network stacks supports multiple active contexts, and this is used
57 // for example to separate Incognito data from the main profile data.
58 // CrNetEnvironment only implement one request context for now, but it can be
59 // extended in the future.
60 net::URLRequestContextGetter
* GetMainContextGetter();
62 // Enables or disables the HTTP protocol handler.
64 // When the HTTP protocol handler is registered, it will be used for all the
65 // network requests the application does (including requests from UIWebView).
66 void SetHTTPProtocolHandlerRegistered(bool registered
);
68 // Creates a new net log (overwrites existing file with this name). If
69 // actively logging, this call is ignored.
70 void StartNetLog(base::FilePath::StringType file_name
, bool log_bytes
);
71 // Stops logging and flushes file. If not currently logging this call is
74 // Closes all existing SPDY sessions with ERR_ABORTED.
75 void CloseAllSpdySessions();
77 // Sets the block used to determine whether or not CrNet should handle the
78 // request. If this is not set, CrNet will handle all requests.
79 // Must not be called while requests are in progress.
80 void SetRequestFilterBlock(RequestFilterBlock block
);
82 // Setters and getters for |spdy_enabled_|, |quic_enabled_|, and
83 // |forced_quic_origin_|. These only have any effect before Install() is
85 void set_spdy_enabled(bool enabled
) { spdy_enabled_
= enabled
; }
86 void set_quic_enabled(bool enabled
) { quic_enabled_
= enabled
; }
87 void set_alternate_protocol_threshold(double threshold
) {
88 alternate_protocol_threshold_
= threshold
;
91 bool spdy_enabled() const { return spdy_enabled_
; }
92 bool quic_enabled() const { return quic_enabled_
; }
93 double alternate_protocol_threshold() const {
94 return alternate_protocol_threshold_
;
97 // Clears the network stack's disk cache.
98 void ClearCache(ClearCacheCallback callback
);
101 // Runs a closure on the network thread.
102 void PostToNetworkThread(const tracked_objects::Location
& from_here
,
103 const base::Closure
& task
);
105 // Performs initialization tasks that must happen on the network thread.
106 void InitializeOnNetworkThread();
108 // Runs a closure on the file user blocking thread.
109 void PostToFileUserBlockingThread(const tracked_objects::Location
& from_here
,
110 const base::Closure
& task
);
112 // Helper methods that start/stop net-internals logging on the file
113 // user blocking thread.
114 void StartNetLogInternal(base::FilePath::StringType file_name
,
116 void StopNetLogInternal();
118 // Returns the HttpNeteworkSession object from the passed in
119 // URLRequestContext or NULL if none exists.
120 net::HttpNetworkSession
* GetHttpNetworkSession(
121 net::URLRequestContext
* context
);
123 // Helper method that closes all current SPDY sessions on the network IO
125 void CloseAllSpdySessionsInternal();
129 double alternate_protocol_threshold_
;
131 // Helper method that clears the cache on the network thread.
132 void ClearCacheOnNetworkThread(ClearCacheCallback callback
);
134 static CrNetEnvironment
* chrome_net_
;
135 scoped_ptr
<base::Thread
> network_io_thread_
;
136 scoped_ptr
<base::Thread
> network_cache_thread_
;
137 scoped_ptr
<base::Thread
> file_thread_
;
138 scoped_ptr
<base::Thread
> file_user_blocking_thread_
;
139 scoped_ptr
<net::SdchManager
> sdch_manager_
;
140 scoped_ptr
<net::NetworkChangeNotifier
> network_change_notifier_
;
141 scoped_ptr
<net::ProxyConfigService
> proxy_config_service_
;
142 scoped_ptr
<net::HttpServerProperties
> http_server_properties_
;
143 scoped_refptr
<net::URLRequestContextGetter
> main_context_getter_
;
144 scoped_ptr
<net::URLRequestContext
> main_context_
;
145 scoped_ptr
<CrNetHttpProtocolHandlerDelegate
> http_protocol_handler_delegate_
;
146 std::string user_agent_product_name_
;
147 scoped_ptr
<net::NetLog
> net_log_
;
148 scoped_ptr
<net::WriteToFileNetLogObserver
> net_log_observer_
;
150 DISALLOW_COPY_AND_ASSIGN(CrNetEnvironment
);
153 #endif // IOS_CRNET_CRNET_ENVIRONMENT_H_