Home | History | Annotate | Download | only in net
      1 // Copyright (c) 2011 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_NET_CHROME_URL_REQUEST_CONTEXT_H_
      6 #define CHROME_BROWSER_NET_CHROME_URL_REQUEST_CONTEXT_H_
      7 #pragma once
      8 
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "base/file_path.h"
     13 #include "chrome/browser/content_settings/host_content_settings_map.h"
     14 #include "chrome/browser/extensions/extension_info_map.h"
     15 #include "chrome/browser/extensions/extension_webrequest_api.h"
     16 #include "chrome/browser/prefs/pref_change_registrar.h"
     17 #include "chrome/browser/prefs/pref_service.h"
     18 #include "chrome/browser/prerender/prerender_manager.h"
     19 #include "chrome/common/extensions/extension_icon_set.h"
     20 #include "content/browser/appcache/chrome_appcache_service.h"
     21 #include "content/browser/chrome_blob_storage_context.h"
     22 #include "content/browser/host_zoom_map.h"
     23 #include "net/base/cookie_policy.h"
     24 #include "net/url_request/url_request_context.h"
     25 #include "net/url_request/url_request_context_getter.h"
     26 #include "webkit/fileapi/file_system_context.h"
     27 
     28 class ChromeURLDataManagerBackend;
     29 class ChromeURLRequestContextFactory;
     30 class IOThread;
     31 namespace net {
     32 class DnsCertProvenanceChecker;
     33 class NetworkDelegate;
     34 }
     35 class PrefService;
     36 class Profile;
     37 class ProfileIOData;
     38 
     39 // Subclass of net::URLRequestContext which can be used to store extra
     40 // information for requests.
     41 //
     42 // All methods of this class must be called from the IO thread,
     43 // including the constructor and destructor.
     44 class ChromeURLRequestContext : public net::URLRequestContext {
     45  public:
     46   ChromeURLRequestContext();
     47 
     48   // Copies the state from |other| into this context.
     49   void CopyFrom(ChromeURLRequestContext* other);
     50 
     51   // Gets the path to the directory user scripts are stored in.
     52   FilePath user_script_dir_path() const {
     53     return user_script_dir_path_;
     54   }
     55 
     56   // Gets the appcache service to be used for requests in this context.
     57   // May be NULL if requests for this context aren't subject to appcaching.
     58   ChromeAppCacheService* appcache_service() const {
     59     return appcache_service_.get();
     60   }
     61 
     62   // Gets the blob storage context associated with this context's profile.
     63   ChromeBlobStorageContext* blob_storage_context() const {
     64     return blob_storage_context_.get();
     65   }
     66 
     67   // Gets the file system host context with this context's profile.
     68   fileapi::FileSystemContext* file_system_context() const {
     69     return file_system_context_.get();
     70   }
     71 
     72   bool is_incognito() const {
     73     return is_incognito_;
     74   }
     75 
     76   virtual const std::string& GetUserAgent(const GURL& url) const;
     77 
     78   HostContentSettingsMap* host_content_settings_map() {
     79     return host_content_settings_map_;
     80   }
     81 
     82   const HostZoomMap* host_zoom_map() const { return host_zoom_map_; }
     83 
     84   const ExtensionInfoMap* extension_info_map() const {
     85     return extension_info_map_;
     86   }
     87 
     88   prerender::PrerenderManager* prerender_manager() {
     89     return prerender_manager_.get();
     90   }
     91 
     92   ChromeURLDataManagerBackend* GetChromeURLDataManagerBackend();
     93 
     94   // Setters to simplify initializing from factory objects.
     95   void set_user_script_dir_path(const FilePath& path) {
     96     user_script_dir_path_ = path;
     97   }
     98   void set_is_incognito(bool is_incognito) {
     99     is_incognito_ = is_incognito;
    100   }
    101   void set_host_content_settings_map(
    102       HostContentSettingsMap* host_content_settings_map) {
    103     host_content_settings_map_ = host_content_settings_map;
    104   }
    105   void set_host_zoom_map(HostZoomMap* host_zoom_map) {
    106     host_zoom_map_ = host_zoom_map;
    107   }
    108   void set_appcache_service(ChromeAppCacheService* service) {
    109     appcache_service_ = service;
    110   }
    111   void set_blob_storage_context(ChromeBlobStorageContext* context) {
    112     blob_storage_context_ = context;
    113   }
    114   void set_file_system_context(fileapi::FileSystemContext* context) {
    115     file_system_context_ = context;
    116   }
    117   void set_extension_info_map(ExtensionInfoMap* map) {
    118     extension_info_map_ = map;
    119   }
    120   void set_prerender_manager(prerender::PrerenderManager* prerender_manager) {
    121     prerender_manager_ = prerender_manager;
    122   }
    123 
    124   // Callback for when the accept language changes.
    125   void OnAcceptLanguageChange(const std::string& accept_language);
    126 
    127   // Callback for when the default charset changes.
    128   void OnDefaultCharsetChange(const std::string& default_charset);
    129 
    130  protected:
    131   virtual ~ChromeURLRequestContext();
    132 
    133  private:
    134   // ---------------------------------------------------------------------------
    135   // Important: When adding any new members below, consider whether they need to
    136   // be added to CopyFrom.
    137   // ---------------------------------------------------------------------------
    138 
    139   // Path to the directory user scripts are stored in.
    140   FilePath user_script_dir_path_;
    141 
    142   // TODO(willchan): Make these non-refcounted.
    143   scoped_refptr<ChromeAppCacheService> appcache_service_;
    144   scoped_refptr<HostContentSettingsMap> host_content_settings_map_;
    145   scoped_refptr<HostZoomMap> host_zoom_map_;
    146   scoped_refptr<ChromeBlobStorageContext> blob_storage_context_;
    147   scoped_refptr<fileapi::FileSystemContext> file_system_context_;
    148   // TODO(aa): This should use chrome/common/extensions/extension_set.h.
    149   scoped_refptr<ExtensionInfoMap> extension_info_map_;
    150   scoped_refptr<prerender::PrerenderManager> prerender_manager_;
    151   scoped_ptr<ChromeURLDataManagerBackend> chrome_url_data_manager_backend_;
    152 
    153   bool is_incognito_;
    154 
    155   // ---------------------------------------------------------------------------
    156   // Important: When adding any new members above, consider whether they need to
    157   // be added to CopyFrom.
    158   // ---------------------------------------------------------------------------
    159 
    160   DISALLOW_COPY_AND_ASSIGN(ChromeURLRequestContext);
    161 };
    162 
    163 // A net::URLRequestContextGetter subclass used by the browser. This returns a
    164 // subclass of net::URLRequestContext which can be used to store extra
    165 // information about requests.
    166 //
    167 // Most methods are expected to be called on the UI thread, except for
    168 // the destructor and GetURLRequestContext().
    169 class ChromeURLRequestContextGetter : public net::URLRequestContextGetter,
    170                                       public NotificationObserver {
    171  public:
    172   // Constructs a ChromeURLRequestContextGetter that will use |factory| to
    173   // create the ChromeURLRequestContext. If |profile| is non-NULL, then the
    174   // ChromeURLRequestContextGetter will additionally watch the preferences for
    175   // changes to charset/language and CleanupOnUIThread() will need to be
    176   // called to unregister.
    177   ChromeURLRequestContextGetter(Profile* profile,
    178                                 ChromeURLRequestContextFactory* factory);
    179 
    180   // Note that GetURLRequestContext() can only be called from the IO
    181   // thread (it will assert otherwise). DONTUSEME_GetCookieStore() and
    182   // GetIOMessageLoopProxy however can be called from any thread.
    183   //
    184   // net::URLRequestContextGetter implementation.
    185   virtual net::URLRequestContext* GetURLRequestContext();
    186   virtual net::CookieStore* DONTUSEME_GetCookieStore();
    187   virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy() const;
    188 
    189   // Releases |url_request_context_|.  It's invalid to call
    190   // GetURLRequestContext() after this point.
    191   void ReleaseURLRequestContext();
    192 
    193   // Convenience overload of GetURLRequestContext() that returns a
    194   // ChromeURLRequestContext* rather than a net::URLRequestContext*.
    195   ChromeURLRequestContext* GetIOContext() {
    196     return reinterpret_cast<ChromeURLRequestContext*>(GetURLRequestContext());
    197   }
    198 
    199   // Create an instance for use with an 'original' (non-OTR) profile. This is
    200   // expected to get called on the UI thread.
    201   static ChromeURLRequestContextGetter* CreateOriginal(
    202       Profile* profile, const ProfileIOData* profile_io_data);
    203 
    204   // Create an instance for an original profile for media. This is expected to
    205   // get called on UI thread. This method takes a profile and reuses the
    206   // 'original' net::URLRequestContext for common files.
    207   static ChromeURLRequestContextGetter* CreateOriginalForMedia(
    208       Profile* profile, const ProfileIOData* profile_io_data);
    209 
    210   // Create an instance for an original profile for extensions. This is expected
    211   // to get called on UI thread.
    212   static ChromeURLRequestContextGetter* CreateOriginalForExtensions(
    213       Profile* profile, const ProfileIOData* profile_io_data);
    214 
    215   // Create an instance for an original profile for an app with isolated
    216   // storage. This is expected to get called on UI thread.
    217   static ChromeURLRequestContextGetter* CreateOriginalForIsolatedApp(
    218       Profile* profile,
    219       const ProfileIOData* profile_io_data,
    220       const std::string& app_id);
    221 
    222   // Create an instance for use with an OTR profile. This is expected to get
    223   // called on the UI thread.
    224   static ChromeURLRequestContextGetter* CreateOffTheRecord(
    225       Profile* profile, const ProfileIOData* profile_io_data);
    226 
    227   // Create an instance for an OTR profile for extensions. This is expected
    228   // to get called on UI thread.
    229   static ChromeURLRequestContextGetter* CreateOffTheRecordForExtensions(
    230       Profile* profile, const ProfileIOData* profile_io_data);
    231 
    232   // Create an instance for an OTR profile for an app with isolated storage.
    233   // This is expected to get called on UI thread.
    234   static ChromeURLRequestContextGetter* CreateOffTheRecordForIsolatedApp(
    235       Profile* profile,
    236       const ProfileIOData* profile_io_data,
    237       const std::string& app_id);
    238 
    239   // Clean up UI thread resources. This is expected to get called on the UI
    240   // thread before the instance is deleted on the IO thread.
    241   void CleanupOnUIThread();
    242 
    243   // NotificationObserver implementation.
    244   virtual void Observe(NotificationType type,
    245                        const NotificationSource& source,
    246                        const NotificationDetails& details);
    247 
    248  private:
    249   // Must be called on the IO thread.
    250   virtual ~ChromeURLRequestContextGetter();
    251 
    252   // Registers an observer on |profile|'s preferences which will be used
    253   // to update the context when the default language and charset change.
    254   void RegisterPrefsObserver(Profile* profile);
    255 
    256   // These methods simply forward to the corresponding method on
    257   // ChromeURLRequestContext.
    258   void OnAcceptLanguageChange(const std::string& accept_language);
    259   void OnDefaultCharsetChange(const std::string& default_charset);
    260   void OnClearSiteDataOnExitChange(bool clear_site_data);
    261 
    262   // Saves the cookie store to |result| and signals |completion|.
    263   void GetCookieStoreAsyncHelper(base::WaitableEvent* completion,
    264                                  net::CookieStore** result);
    265 
    266   PrefChangeRegistrar registrar_;
    267 
    268   // |io_thread_| is always valid during the lifetime of |this| since |this| is
    269   // deleted on the IO thread.
    270   IOThread* const io_thread_;
    271 
    272   // Deferred logic for creating a ChromeURLRequestContext.
    273   // Access only from the IO thread.
    274   scoped_ptr<ChromeURLRequestContextFactory> factory_;
    275 
    276   // NULL if not yet initialized. Otherwise, it is the net::URLRequestContext
    277   // instance that was lazilly created by GetURLRequestContext.
    278   // Access only from the IO thread.
    279   scoped_refptr<net::URLRequestContext> url_request_context_;
    280 
    281   DISALLOW_COPY_AND_ASSIGN(ChromeURLRequestContextGetter);
    282 };
    283 
    284 #endif  // CHROME_BROWSER_NET_CHROME_URL_REQUEST_CONTEXT_H_
    285