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