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_IO_DATA_H_
      6 #define CHROME_BROWSER_PROFILES_PROFILE_IO_DATA_H_
      7 
      8 #include <string>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/callback_forward.h"
     12 #include "base/files/file_path.h"
     13 #include "base/memory/ref_counted.h"
     14 #include "base/memory/scoped_ptr.h"
     15 #include "base/memory/weak_ptr.h"
     16 #include "base/prefs/pref_member.h"
     17 #include "base/synchronization/lock.h"
     18 #include "chrome/browser/custom_handlers/protocol_handler_registry.h"
     19 #include "chrome/browser/io_thread.h"
     20 #include "chrome/browser/net/chrome_url_request_context.h"
     21 #include "chrome/browser/profiles/storage_partition_descriptor.h"
     22 #include "chrome/common/content_settings_types.h"
     23 #include "content/public/browser/content_browser_client.h"
     24 #include "content/public/browser/resource_context.h"
     25 #include "net/cookies/cookie_monster.h"
     26 #include "net/http/http_network_session.h"
     27 #include "net/url_request/url_request_job_factory.h"
     28 
     29 class ChromeHttpUserAgentSettings;
     30 class ChromeNetworkDelegate;
     31 class CookieSettings;
     32 class DesktopNotificationService;
     33 class ExtensionInfoMap;
     34 class HostContentSettingsMap;
     35 class ManagedModeURLFilter;
     36 class Profile;
     37 class ProtocolHandlerRegistry;
     38 class SigninNamesOnIOThread;
     39 class TransportSecurityPersister;
     40 
     41 namespace chrome_browser_net {
     42 class LoadTimeStats;
     43 class ResourcePrefetchPredictorObserver;
     44 }
     45 
     46 namespace net {
     47 class CertTrustAnchorProvider;
     48 class CertVerifier;
     49 class CookieStore;
     50 class FraudulentCertificateReporter;
     51 class FtpTransactionFactory;
     52 class HttpServerProperties;
     53 class HttpTransactionFactory;
     54 class ServerBoundCertService;
     55 class ProxyConfigService;
     56 class ProxyService;
     57 class SSLConfigService;
     58 class TransportSecurityState;
     59 class URLRequestJobFactoryImpl;
     60 }  // namespace net
     61 
     62 namespace policy {
     63 class URLBlacklistManager;
     64 }  // namespace policy
     65 
     66 // Conceptually speaking, the ProfileIOData represents data that lives on the IO
     67 // thread that is owned by a Profile, such as, but not limited to, network
     68 // objects like CookieMonster, HttpTransactionFactory, etc.  Profile owns
     69 // ProfileIOData, but will make sure to delete it on the IO thread (except
     70 // possibly in unit tests where there is no IO thread).
     71 class ProfileIOData {
     72  public:
     73   virtual ~ProfileIOData();
     74 
     75   static ProfileIOData* FromResourceContext(content::ResourceContext* rc);
     76 
     77   // Returns true if |scheme| is handled in Chrome, or by default handlers in
     78   // net::URLRequest.
     79   static bool IsHandledProtocol(const std::string& scheme);
     80 
     81   // Returns true if |url| is handled in Chrome, or by default handlers in
     82   // net::URLRequest.
     83   static bool IsHandledURL(const GURL& url);
     84 
     85   // Utility to install additional WebUI handlers into the |job_factory|.
     86   // Ownership of the handlers is transfered from |protocol_handlers|
     87   // to the |job_factory|.
     88   static void InstallProtocolHandlers(
     89       net::URLRequestJobFactoryImpl* job_factory,
     90       content::ProtocolHandlerMap* protocol_handlers);
     91 
     92   // Called by Profile.
     93   content::ResourceContext* GetResourceContext() const;
     94 
     95   // Initializes the ProfileIOData object and primes the RequestContext
     96   // generation. Must be called prior to any of the Get*() methods other than
     97   // GetResouceContext or GetMetricsEnabledStateOnIOThread.
     98   void Init(content::ProtocolHandlerMap* protocol_handlers) const;
     99 
    100   ChromeURLRequestContext* GetMainRequestContext() const;
    101   ChromeURLRequestContext* GetMediaRequestContext() const;
    102   ChromeURLRequestContext* GetExtensionsRequestContext() const;
    103   ChromeURLRequestContext* GetIsolatedAppRequestContext(
    104       ChromeURLRequestContext* main_context,
    105       const StoragePartitionDescriptor& partition_descriptor,
    106       scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
    107           protocol_handler_interceptor,
    108       content::ProtocolHandlerMap* protocol_handlers) const;
    109   ChromeURLRequestContext* GetIsolatedMediaRequestContext(
    110       ChromeURLRequestContext* app_context,
    111       const StoragePartitionDescriptor& partition_descriptor) const;
    112 
    113   // These are useful when the Chrome layer is called from the content layer
    114   // with a content::ResourceContext, and they want access to Chrome data for
    115   // that profile.
    116   ExtensionInfoMap* GetExtensionInfoMap() const;
    117   CookieSettings* GetCookieSettings() const;
    118   HostContentSettingsMap* GetHostContentSettingsMap() const;
    119 
    120 #if defined(ENABLE_NOTIFICATIONS)
    121   DesktopNotificationService* GetNotificationService() const;
    122 #endif
    123 
    124   IntegerPrefMember* session_startup_pref() const {
    125     return &session_startup_pref_;
    126   }
    127 
    128   SigninNamesOnIOThread* signin_names() const {
    129     return signin_names_.get();
    130   }
    131 
    132   StringPrefMember* google_services_username() const {
    133     return &google_services_username_;
    134   }
    135 
    136   StringPrefMember* google_services_username_pattern() const {
    137     return &google_services_username_pattern_;
    138   }
    139 
    140   BooleanPrefMember* reverse_autologin_enabled() const {
    141     return &reverse_autologin_enabled_;
    142   }
    143 
    144   const std::string& reverse_autologin_pending_email() const {
    145     return reverse_autologin_pending_email_;
    146   }
    147 
    148   void set_reverse_autologin_pending_email(const std::string& email) {
    149     reverse_autologin_pending_email_ = email;
    150   }
    151 
    152   StringListPrefMember* one_click_signin_rejected_email_list() const {
    153     return &one_click_signin_rejected_email_list_;
    154   }
    155 
    156   ChromeURLRequestContext* extensions_request_context() const {
    157     return extensions_request_context_.get();
    158   }
    159 
    160   BooleanPrefMember* safe_browsing_enabled() const {
    161     return &safe_browsing_enabled_;
    162   }
    163 
    164   BooleanPrefMember* printing_enabled() const {
    165     return &printing_enabled_;
    166   }
    167 
    168   BooleanPrefMember* sync_disabled() const {
    169     return &sync_disabled_;
    170   }
    171 
    172   BooleanPrefMember* signin_allowed() const {
    173     return &signin_allowed_;
    174   }
    175 
    176   net::TransportSecurityState* transport_security_state() const {
    177     return transport_security_state_.get();
    178   }
    179 
    180   bool is_incognito() const {
    181     return is_incognito_;
    182   }
    183 
    184   chrome_browser_net::ResourcePrefetchPredictorObserver*
    185       resource_prefetch_predictor_observer() const {
    186     return resource_prefetch_predictor_observer_.get();
    187   }
    188 
    189 #if defined(ENABLE_MANAGED_USERS)
    190   const ManagedModeURLFilter* managed_mode_url_filter() const {
    191     return managed_mode_url_filter_.get();
    192   }
    193 #endif
    194 
    195   // Initialize the member needed to track the metrics enabled state. This is
    196   // only to be called on the UI thread.
    197   void InitializeMetricsEnabledStateOnUIThread();
    198 
    199   // Returns whether or not metrics reporting is enabled in the browser instance
    200   // on which this profile resides. This is safe for use from the IO thread, and
    201   // should only be called from there.
    202   bool GetMetricsEnabledStateOnIOThread() const;
    203 
    204  protected:
    205   // A URLRequestContext for media that owns its HTTP factory, to ensure
    206   // it is deleted.
    207   class MediaRequestContext : public ChromeURLRequestContext {
    208    public:
    209     explicit MediaRequestContext(
    210         chrome_browser_net::LoadTimeStats* load_time_stats);
    211 
    212     void SetHttpTransactionFactory(
    213         scoped_ptr<net::HttpTransactionFactory> http_factory);
    214 
    215    private:
    216     virtual ~MediaRequestContext();
    217 
    218     scoped_ptr<net::HttpTransactionFactory> http_factory_;
    219   };
    220 
    221   // A URLRequestContext for apps that owns its cookie store and HTTP factory,
    222   // to ensure they are deleted.
    223   class AppRequestContext : public ChromeURLRequestContext {
    224    public:
    225     explicit AppRequestContext(
    226         chrome_browser_net::LoadTimeStats* load_time_stats);
    227 
    228     void SetCookieStore(net::CookieStore* cookie_store);
    229     void SetHttpTransactionFactory(
    230         scoped_ptr<net::HttpTransactionFactory> http_factory);
    231     void SetJobFactory(scoped_ptr<net::URLRequestJobFactory> job_factory);
    232 
    233    private:
    234     virtual ~AppRequestContext();
    235 
    236     scoped_refptr<net::CookieStore> cookie_store_;
    237     scoped_ptr<net::HttpTransactionFactory> http_factory_;
    238     scoped_ptr<net::URLRequestJobFactory> job_factory_;
    239   };
    240 
    241   // Created on the UI thread, read on the IO thread during ProfileIOData lazy
    242   // initialization.
    243   struct ProfileParams {
    244     ProfileParams();
    245     ~ProfileParams();
    246 
    247     base::FilePath path;
    248     IOThread* io_thread;
    249     scoped_refptr<CookieSettings> cookie_settings;
    250     scoped_refptr<HostContentSettingsMap> host_content_settings_map;
    251     scoped_refptr<net::SSLConfigService> ssl_config_service;
    252     scoped_refptr<net::CookieMonster::Delegate> cookie_monster_delegate;
    253     scoped_refptr<ExtensionInfoMap> extension_info_map;
    254     scoped_ptr<chrome_browser_net::ResourcePrefetchPredictorObserver>
    255         resource_prefetch_predictor_observer_;
    256 
    257 #if defined(ENABLE_NOTIFICATIONS)
    258     DesktopNotificationService* notification_service;
    259 #endif
    260 
    261     // This pointer exists only as a means of conveying a url job factory
    262     // pointer from the protocol handler registry on the UI thread to the
    263     // the URLRequestContext on the IO thread. The consumer MUST take
    264     // ownership of the object by calling release() on this pointer.
    265     scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
    266         protocol_handler_interceptor;
    267 
    268     // We need to initialize the ProxyConfigService from the UI thread
    269     // because on linux it relies on initializing things through gconf,
    270     // and needs to be on the main thread.
    271     scoped_ptr<net::ProxyConfigService> proxy_config_service;
    272 
    273 #if defined(ENABLE_MANAGED_USERS)
    274     scoped_refptr<const ManagedModeURLFilter> managed_mode_url_filter;
    275 #endif
    276 
    277 #if defined(OS_CHROMEOS)
    278     // This is used to build the CertVerifier on the IO thread, and is a shared
    279     // provider used by all profiles for now.
    280     net::CertTrustAnchorProvider* trust_anchor_provider;
    281 #endif
    282 
    283     // The profile this struct was populated from. It's passed as a void* to
    284     // ensure it's not accidently used on the IO thread. Before using it on the
    285     // UI thread, call ProfileManager::IsValidProfile to ensure it's alive.
    286     void* profile;
    287   };
    288 
    289   explicit ProfileIOData(bool is_incognito);
    290 
    291   static std::string GetSSLSessionCacheShard();
    292 
    293   void InitializeOnUIThread(Profile* profile);
    294   void ApplyProfileParamsToContext(ChromeURLRequestContext* context) const;
    295 
    296   scoped_ptr<net::URLRequestJobFactory> SetUpJobFactoryDefaults(
    297       scoped_ptr<net::URLRequestJobFactoryImpl> job_factory,
    298       scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
    299           protocol_handler_interceptor,
    300       net::NetworkDelegate* network_delegate,
    301       net::FtpTransactionFactory* ftp_transaction_factory) const;
    302 
    303   // Called when the profile is destroyed.
    304   void ShutdownOnUIThread();
    305 
    306   // A ServerBoundCertService object is created by a derived class of
    307   // ProfileIOData, and the derived class calls this method to set the
    308   // server_bound_cert_service_ member and transfers ownership to the base
    309   // class.
    310   void set_server_bound_cert_service(
    311       net::ServerBoundCertService* server_bound_cert_service) const;
    312 
    313   ChromeNetworkDelegate* network_delegate() const {
    314     return network_delegate_.get();
    315   }
    316 
    317   net::FraudulentCertificateReporter* fraudulent_certificate_reporter() const {
    318     return fraudulent_certificate_reporter_.get();
    319   }
    320 
    321   net::ProxyService* proxy_service() const {
    322     return proxy_service_.get();
    323   }
    324 
    325   base::WeakPtr<net::HttpServerProperties> http_server_properties() const;
    326 
    327   void set_http_server_properties(
    328       scoped_ptr<net::HttpServerProperties> http_server_properties) const;
    329 
    330   ChromeURLRequestContext* main_request_context() const {
    331     return main_request_context_.get();
    332   }
    333 
    334   chrome_browser_net::LoadTimeStats* load_time_stats() const {
    335     return load_time_stats_;
    336   }
    337 
    338   bool initialized() const {
    339     return initialized_;
    340   }
    341 
    342   // Destroys the ResourceContext first, to cancel any URLRequests that are
    343   // using it still, before we destroy the member variables that those
    344   // URLRequests may be accessing.
    345   void DestroyResourceContext();
    346 
    347   // Fills in fields of params using values from main_request_context_ and the
    348   // IOThread associated with profile_params.
    349   void PopulateNetworkSessionParams(
    350       const ProfileParams* profile_params,
    351       net::HttpNetworkSession::Params* params) const;
    352 
    353   void SetCookieSettingsForTesting(CookieSettings* cookie_settings);
    354 
    355   void set_signin_names_for_testing(SigninNamesOnIOThread* signin_names);
    356 
    357  private:
    358   class ResourceContext : public content::ResourceContext {
    359    public:
    360     explicit ResourceContext(ProfileIOData* io_data);
    361     virtual ~ResourceContext();
    362 
    363     // ResourceContext implementation:
    364     virtual net::HostResolver* GetHostResolver() OVERRIDE;
    365     virtual net::URLRequestContext* GetRequestContext() OVERRIDE;
    366     virtual bool AllowMicAccess(const GURL& origin) OVERRIDE;
    367     virtual bool AllowCameraAccess(const GURL& origin) OVERRIDE;
    368 
    369    private:
    370     friend class ProfileIOData;
    371 
    372     // Helper method that returns true if |type| is allowed for |origin|, false
    373     // otherwise.
    374     bool AllowContentAccess(const GURL& origin, ContentSettingsType type);
    375 
    376     ProfileIOData* const io_data_;
    377 
    378     net::HostResolver* host_resolver_;
    379     net::URLRequestContext* request_context_;
    380   };
    381 
    382   typedef std::map<StoragePartitionDescriptor,
    383                    ChromeURLRequestContext*,
    384                    StoragePartitionDescriptorLess>
    385       URLRequestContextMap;
    386 
    387   // --------------------------------------------
    388   // Virtual interface for subtypes to implement:
    389   // --------------------------------------------
    390 
    391   // Does the actual initialization of the ProfileIOData subtype. Subtypes
    392   // should use the static helper functions above to implement this.
    393   virtual void InitializeInternal(
    394       ProfileParams* profile_params,
    395       content::ProtocolHandlerMap* protocol_handlers) const = 0;
    396 
    397   // Initializes the RequestContext for extensions.
    398   virtual void InitializeExtensionsRequestContext(
    399       ProfileParams* profile_params) const = 0;
    400   // Does an on-demand initialization of a RequestContext for the given
    401   // isolated app.
    402   virtual ChromeURLRequestContext* InitializeAppRequestContext(
    403       ChromeURLRequestContext* main_context,
    404       const StoragePartitionDescriptor& details,
    405       scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
    406           protocol_handler_interceptor,
    407       content::ProtocolHandlerMap* protocol_handlers) const = 0;
    408 
    409   // Does an on-demand initialization of a media RequestContext for the given
    410   // isolated app.
    411   virtual ChromeURLRequestContext* InitializeMediaRequestContext(
    412       ChromeURLRequestContext* original_context,
    413       const StoragePartitionDescriptor& details) const = 0;
    414 
    415   // These functions are used to transfer ownership of the lazily initialized
    416   // context from ProfileIOData to the URLRequestContextGetter.
    417   virtual ChromeURLRequestContext*
    418       AcquireMediaRequestContext() const = 0;
    419   virtual ChromeURLRequestContext*
    420       AcquireIsolatedAppRequestContext(
    421           ChromeURLRequestContext* main_context,
    422           const StoragePartitionDescriptor& partition_descriptor,
    423           scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
    424               protocol_handler_interceptor,
    425           content::ProtocolHandlerMap* protocol_handlers) const = 0;
    426   virtual ChromeURLRequestContext*
    427       AcquireIsolatedMediaRequestContext(
    428           ChromeURLRequestContext* app_context,
    429           const StoragePartitionDescriptor& partition_descriptor) const = 0;
    430 
    431   // Returns the LoadTimeStats object to be used for this profile.
    432   virtual chrome_browser_net::LoadTimeStats* GetLoadTimeStats(
    433       IOThread::Globals* io_thread_globals) const = 0;
    434 
    435   // The order *DOES* matter for the majority of these member variables, so
    436   // don't move them around unless you know what you're doing!
    437   // General rules:
    438   //   * ResourceContext references the URLRequestContexts, so
    439   //   URLRequestContexts must outlive ResourceContext, hence ResourceContext
    440   //   should be destroyed first.
    441   //   * URLRequestContexts reference a whole bunch of members, so
    442   //   URLRequestContext needs to be destroyed before them.
    443   //   * Therefore, ResourceContext should be listed last, and then the
    444   //   URLRequestContexts, and then the URLRequestContext members.
    445   //   * Note that URLRequestContext members have a directed dependency graph
    446   //   too, so they must themselves be ordered correctly.
    447 
    448   // Tracks whether or not we've been lazily initialized.
    449   mutable bool initialized_;
    450 
    451   // Data from the UI thread from the Profile, used to initialize ProfileIOData.
    452   // Deleted after lazy initialization.
    453   mutable scoped_ptr<ProfileParams> profile_params_;
    454 
    455   // Provides access to the email addresses of all signed in profiles.
    456   mutable scoped_ptr<SigninNamesOnIOThread> signin_names_;
    457 
    458   mutable StringPrefMember google_services_username_;
    459   mutable StringPrefMember google_services_username_pattern_;
    460   mutable BooleanPrefMember reverse_autologin_enabled_;
    461 
    462   // During the reverse autologin request chain processing, this member saves
    463   // the email of the google account that is being signed into.
    464   std::string reverse_autologin_pending_email_;
    465 
    466   mutable StringListPrefMember one_click_signin_rejected_email_list_;
    467 
    468   // Member variables which are pointed to by the various context objects.
    469   mutable BooleanPrefMember enable_referrers_;
    470   mutable BooleanPrefMember enable_do_not_track_;
    471   mutable BooleanPrefMember force_safesearch_;
    472   mutable BooleanPrefMember safe_browsing_enabled_;
    473   mutable BooleanPrefMember printing_enabled_;
    474   mutable BooleanPrefMember sync_disabled_;
    475   mutable BooleanPrefMember signin_allowed_;
    476   // TODO(marja): Remove session_startup_pref_ if no longer needed.
    477   mutable IntegerPrefMember session_startup_pref_;
    478 
    479   // The state of metrics reporting in the browser that this profile runs on.
    480   // Unfortunately, since ChromeOS has a separate representation of this state,
    481   // we need to make one available based on the platform.
    482 #if defined(OS_CHROMEOS)
    483   bool enable_metrics_;
    484 #else
    485   BooleanPrefMember enable_metrics_;
    486 #endif
    487 
    488   // Pointed to by NetworkDelegate.
    489   mutable scoped_ptr<policy::URLBlacklistManager> url_blacklist_manager_;
    490 
    491   // Pointed to by URLRequestContext.
    492   mutable scoped_refptr<ExtensionInfoMap> extension_info_map_;
    493   mutable scoped_ptr<net::ServerBoundCertService> server_bound_cert_service_;
    494   mutable scoped_ptr<ChromeNetworkDelegate> network_delegate_;
    495   mutable scoped_ptr<net::FraudulentCertificateReporter>
    496       fraudulent_certificate_reporter_;
    497   mutable scoped_ptr<net::ProxyService> proxy_service_;
    498   mutable scoped_ptr<net::TransportSecurityState> transport_security_state_;
    499   mutable scoped_ptr<net::HttpServerProperties>
    500       http_server_properties_;
    501 #if defined(OS_CHROMEOS)
    502   mutable scoped_ptr<net::CertVerifier> cert_verifier_;
    503 #endif
    504 
    505 #if defined(ENABLE_NOTIFICATIONS)
    506   mutable DesktopNotificationService* notification_service_;
    507 #endif
    508 
    509   mutable scoped_ptr<TransportSecurityPersister>
    510       transport_security_persister_;
    511 
    512   // These are only valid in between LazyInitialize() and their accessor being
    513   // called.
    514   mutable scoped_ptr<ChromeURLRequestContext> main_request_context_;
    515   mutable scoped_ptr<ChromeURLRequestContext> extensions_request_context_;
    516   // One URLRequestContext per isolated app for main and media requests.
    517   mutable URLRequestContextMap app_request_context_map_;
    518   mutable URLRequestContextMap isolated_media_request_context_map_;
    519 
    520   mutable scoped_ptr<ResourceContext> resource_context_;
    521 
    522   mutable scoped_refptr<CookieSettings> cookie_settings_;
    523 
    524   mutable scoped_refptr<HostContentSettingsMap> host_content_settings_map_;
    525 
    526   mutable scoped_ptr<chrome_browser_net::ResourcePrefetchPredictorObserver>
    527       resource_prefetch_predictor_observer_;
    528 
    529   mutable scoped_ptr<ChromeHttpUserAgentSettings>
    530       chrome_http_user_agent_settings_;
    531 
    532   mutable chrome_browser_net::LoadTimeStats* load_time_stats_;
    533 
    534 #if defined(ENABLE_MANAGED_USERS)
    535   mutable scoped_refptr<const ManagedModeURLFilter> managed_mode_url_filter_;
    536 #endif
    537 
    538   // TODO(jhawkins): Remove once crbug.com/102004 is fixed.
    539   bool initialized_on_UI_thread_;
    540 
    541   bool is_incognito_;
    542 
    543   DISALLOW_COPY_AND_ASSIGN(ProfileIOData);
    544 };
    545 
    546 #endif  // CHROME_BROWSER_PROFILES_PROFILE_IO_DATA_H_
    547