1// Copyright (c) 2012 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.
4
5#ifndef CHROME_BROWSER_PROFILES_PROFILE_IMPL_IO_DATA_H_
6#define CHROME_BROWSER_PROFILES_PROFILE_IMPL_IO_DATA_H_
7
8#include "base/basictypes.h"
9#include "base/callback.h"
10#include "base/containers/hash_tables.h"
11#include "base/memory/ref_counted.h"
12#include "chrome/browser/custom_handlers/protocol_handler_registry.h"
13#include "chrome/browser/net/chrome_url_request_context_getter.h"
14#include "chrome/browser/profiles/profile_io_data.h"
15#include "content/public/browser/cookie_store_factory.h"
16
17namespace chrome_browser_net {
18class Predictor;
19}  // namespace chrome_browser_net
20
21namespace content {
22class CookieCryptoDelegate;
23}  // namespace content
24
25namespace domain_reliability {
26class DomainReliabilityMonitor;
27}  // namespace domain_reliability
28
29namespace net {
30class FtpTransactionFactory;
31class HttpServerProperties;
32class HttpServerPropertiesManager;
33class HttpTransactionFactory;
34class ProxyConfig;
35class SDCHManager;
36}  // namespace net
37
38namespace storage {
39class SpecialStoragePolicy;
40}  // namespace storage
41
42class ProfileImplIOData : public ProfileIOData {
43 public:
44  class Handle {
45   public:
46    explicit Handle(Profile* profile);
47    ~Handle();
48
49    // Init() must be called before ~Handle(). It records most of the
50    // parameters needed to construct a ChromeURLRequestContextGetter.
51    void Init(
52        const base::FilePath& cookie_path,
53        const base::FilePath& channel_id_path,
54        const base::FilePath& cache_path,
55        int cache_max_size,
56        const base::FilePath& media_cache_path,
57        int media_cache_max_size,
58        const base::FilePath& extensions_cookie_path,
59        const base::FilePath& profile_path,
60        const base::FilePath& infinite_cache_path,
61        chrome_browser_net::Predictor* predictor,
62        content::CookieStoreConfig::SessionCookieMode session_cookie_mode,
63        storage::SpecialStoragePolicy* special_storage_policy,
64        scoped_ptr<domain_reliability::DomainReliabilityMonitor>
65            domain_reliability_monitor,
66        const base::Callback<void(bool)>& data_reduction_proxy_unavailable,
67        scoped_ptr<DataReductionProxyChromeConfigurator>
68            data_reduction_proxy_chrome_configurator,
69        scoped_ptr<data_reduction_proxy::DataReductionProxyParams>
70            data_reduction_proxy_params,
71        scoped_ptr<data_reduction_proxy::DataReductionProxyStatisticsPrefs>
72            data_reduction_proxy_statistics_prefs);
73
74    // These Create*ContextGetter() functions are only exposed because the
75    // circular relationship between Profile, ProfileIOData::Handle, and the
76    // ChromeURLRequestContextGetter factories requires Profile be able to call
77    // these functions.
78    scoped_refptr<ChromeURLRequestContextGetter> CreateMainRequestContextGetter(
79        content::ProtocolHandlerMap* protocol_handlers,
80        content::URLRequestInterceptorScopedVector request_interceptors,
81        PrefService* local_state,
82        IOThread* io_thread) const;
83    scoped_refptr<ChromeURLRequestContextGetter>
84        CreateIsolatedAppRequestContextGetter(
85            const base::FilePath& partition_path,
86            bool in_memory,
87            content::ProtocolHandlerMap* protocol_handlers,
88            content::URLRequestInterceptorScopedVector
89                request_interceptors) const;
90
91    content::ResourceContext* GetResourceContext() const;
92    // GetResourceContextNoInit() does not call LazyInitialize() so it can be
93    // safely be used during initialization.
94    content::ResourceContext* GetResourceContextNoInit() const;
95    scoped_refptr<ChromeURLRequestContextGetter>
96        GetMediaRequestContextGetter() const;
97    scoped_refptr<ChromeURLRequestContextGetter>
98        GetExtensionsRequestContextGetter() const;
99    scoped_refptr<ChromeURLRequestContextGetter>
100        GetIsolatedMediaRequestContextGetter(
101            const base::FilePath& partition_path,
102            bool in_memory) const;
103
104    // Returns the DevToolsNetworkController attached to ProfileIOData.
105    DevToolsNetworkController* GetDevToolsNetworkController() const;
106
107    // Deletes all network related data since |time|. It deletes transport
108    // security state since |time| and also deletes HttpServerProperties data.
109    // Works asynchronously, however if the |completion| callback is non-null,
110    // it will be posted on the UI thread once the removal process completes.
111    void ClearNetworkingHistorySince(base::Time time,
112                                     const base::Closure& completion);
113
114   private:
115    typedef std::map<StoragePartitionDescriptor,
116                     scoped_refptr<ChromeURLRequestContextGetter>,
117                     StoragePartitionDescriptorLess>
118      ChromeURLRequestContextGetterMap;
119
120    // Lazily initialize ProfileParams. We do this on the calls to
121    // Get*RequestContextGetter(), so we only initialize ProfileParams right
122    // before posting a task to the IO thread to start using them. This prevents
123    // objects that are supposed to be deleted on the IO thread, but are created
124    // on the UI thread from being unnecessarily initialized.
125    void LazyInitialize() const;
126
127    // Collect references to context getters in reverse order, i.e. last item
128    // will be main request getter. This list is passed to |io_data_|
129    // for invalidation on IO thread.
130    scoped_ptr<ChromeURLRequestContextGetterVector> GetAllContextGetters();
131
132    // The getters will be invalidated on the IO thread before
133    // ProfileIOData instance is deleted.
134    mutable scoped_refptr<ChromeURLRequestContextGetter>
135        main_request_context_getter_;
136    mutable scoped_refptr<ChromeURLRequestContextGetter>
137        media_request_context_getter_;
138    mutable scoped_refptr<ChromeURLRequestContextGetter>
139        extensions_request_context_getter_;
140    mutable ChromeURLRequestContextGetterMap app_request_context_getter_map_;
141    mutable ChromeURLRequestContextGetterMap
142        isolated_media_request_context_getter_map_;
143    ProfileImplIOData* const io_data_;
144
145    Profile* const profile_;
146
147    mutable bool initialized_;
148
149    DISALLOW_COPY_AND_ASSIGN(Handle);
150  };
151
152 private:
153  friend class base::RefCountedThreadSafe<ProfileImplIOData>;
154
155  struct LazyParams {
156    LazyParams();
157    ~LazyParams();
158
159    // All of these parameters are intended to be read on the IO thread.
160    base::FilePath cookie_path;
161    base::FilePath channel_id_path;
162    base::FilePath cache_path;
163    int cache_max_size;
164    base::FilePath media_cache_path;
165    int media_cache_max_size;
166    base::FilePath extensions_cookie_path;
167    base::FilePath infinite_cache_path;
168    content::CookieStoreConfig::SessionCookieMode session_cookie_mode;
169    scoped_refptr<storage::SpecialStoragePolicy> special_storage_policy;
170  };
171
172  ProfileImplIOData();
173  virtual ~ProfileImplIOData();
174
175  virtual void InitializeInternal(
176      ProfileParams* profile_params,
177      content::ProtocolHandlerMap* protocol_handlers,
178      content::URLRequestInterceptorScopedVector request_interceptors)
179          const OVERRIDE;
180  virtual void InitializeExtensionsRequestContext(
181      ProfileParams* profile_params) const OVERRIDE;
182  virtual net::URLRequestContext* InitializeAppRequestContext(
183      net::URLRequestContext* main_context,
184      const StoragePartitionDescriptor& partition_descriptor,
185      scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
186          protocol_handler_interceptor,
187      content::ProtocolHandlerMap* protocol_handlers,
188      content::URLRequestInterceptorScopedVector request_interceptors)
189          const OVERRIDE;
190  virtual net::URLRequestContext* InitializeMediaRequestContext(
191      net::URLRequestContext* original_context,
192      const StoragePartitionDescriptor& partition_descriptor) const OVERRIDE;
193  virtual net::URLRequestContext*
194      AcquireMediaRequestContext() const OVERRIDE;
195  virtual net::URLRequestContext* AcquireIsolatedAppRequestContext(
196      net::URLRequestContext* main_context,
197      const StoragePartitionDescriptor& partition_descriptor,
198      scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
199          protocol_handler_interceptor,
200      content::ProtocolHandlerMap* protocol_handlers,
201      content::URLRequestInterceptorScopedVector request_interceptors)
202          const OVERRIDE;
203  virtual net::URLRequestContext*
204      AcquireIsolatedMediaRequestContext(
205          net::URLRequestContext* app_context,
206          const StoragePartitionDescriptor& partition_descriptor)
207              const OVERRIDE;
208
209  // Deletes all network related data since |time|. It deletes transport
210  // security state since |time| and also deletes HttpServerProperties data.
211  // Works asynchronously, however if the |completion| callback is non-null,
212  // it will be posted on the UI thread once the removal process completes.
213  void ClearNetworkingHistorySinceOnIOThread(base::Time time,
214                                             const base::Closure& completion);
215
216  // Lazy initialization params.
217  mutable scoped_ptr<LazyParams> lazy_params_;
218
219  mutable scoped_ptr<net::HttpTransactionFactory> main_http_factory_;
220  mutable scoped_ptr<net::FtpTransactionFactory> ftp_factory_;
221
222  // Same as |ProfileIOData::http_server_properties_|, owned there to maintain
223  // destruction ordering.
224  mutable net::HttpServerPropertiesManager* http_server_properties_manager_;
225
226  mutable scoped_ptr<chrome_browser_net::Predictor> predictor_;
227
228  mutable scoped_ptr<net::URLRequestContext> media_request_context_;
229
230  mutable scoped_ptr<net::URLRequestJobFactory> main_job_factory_;
231  mutable scoped_ptr<net::URLRequestJobFactory> extensions_job_factory_;
232
233  mutable scoped_ptr<domain_reliability::DomainReliabilityMonitor>
234      domain_reliability_monitor_;
235
236  mutable scoped_ptr<net::SdchManager> sdch_manager_;
237
238  // Parameters needed for isolated apps.
239  base::FilePath profile_path_;
240  int app_cache_max_size_;
241  int app_media_cache_max_size_;
242
243  DISALLOW_COPY_AND_ASSIGN(ProfileImplIOData);
244};
245
246#endif  // CHROME_BROWSER_PROFILES_PROFILE_IMPL_IO_DATA_H_
247