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