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