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