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