Home | History | Annotate | Download | only in net
      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_NET_CHROME_URL_REQUEST_CONTEXT_H_
      6 #define CHROME_BROWSER_NET_CHROME_URL_REQUEST_CONTEXT_H_
      7 
      8 #include <string>
      9 
     10 #include "base/memory/scoped_ptr.h"
     11 #include "chrome/browser/custom_handlers/protocol_handler_registry.h"
     12 #include "net/url_request/url_request_context.h"
     13 #include "net/url_request/url_request_context_getter.h"
     14 #include "net/url_request/url_request_job_factory.h"
     15 
     16 class ChromeURLRequestContextFactory;
     17 class IOThread;
     18 class Profile;
     19 class ProfileIOData;
     20 struct StoragePartitionDescriptor;
     21 
     22 namespace chrome_browser_net {
     23 class LoadTimeStats;
     24 }
     25 
     26 // Subclass of net::URLRequestContext which can be used to store extra
     27 // information for requests.
     28 //
     29 // All methods of this class must be called from the IO thread,
     30 // including the constructor and destructor.
     31 class ChromeURLRequestContext : public net::URLRequestContext {
     32  public:
     33   enum ContextType {
     34     CONTEXT_TYPE_MAIN,
     35     CONTEXT_TYPE_MEDIA,
     36     CONTEXT_TYPE_EXTENSIONS,
     37     CONTEXT_TYPE_APP
     38   };
     39   ChromeURLRequestContext(ContextType type,
     40                           chrome_browser_net::LoadTimeStats* load_time_stats);
     41   virtual ~ChromeURLRequestContext();
     42 
     43   base::WeakPtr<ChromeURLRequestContext> GetWeakPtr() {
     44     return weak_factory_.GetWeakPtr();
     45   }
     46 
     47   // Copies the state from |other| into this context.
     48   void CopyFrom(ChromeURLRequestContext* other);
     49 
     50  private:
     51   base::WeakPtrFactory<ChromeURLRequestContext> weak_factory_;
     52 
     53   // ---------------------------------------------------------------------------
     54   // Important: When adding any new members below, consider whether they need to
     55   // be added to CopyFrom.
     56   // ---------------------------------------------------------------------------
     57 
     58   chrome_browser_net::LoadTimeStats* load_time_stats_;
     59 
     60   // ---------------------------------------------------------------------------
     61   // Important: When adding any new members above, consider whether they need to
     62   // be added to CopyFrom.
     63   // ---------------------------------------------------------------------------
     64 
     65   DISALLOW_COPY_AND_ASSIGN(ChromeURLRequestContext);
     66 };
     67 
     68 // A net::URLRequestContextGetter subclass used by the browser. This returns a
     69 // subclass of net::URLRequestContext which can be used to store extra
     70 // information about requests.
     71 //
     72 // Most methods are expected to be called on the UI thread, except for
     73 // the destructor and GetURLRequestContext().
     74 class ChromeURLRequestContextGetter : public net::URLRequestContextGetter {
     75  public:
     76   // Constructs a ChromeURLRequestContextGetter that will use |factory| to
     77   // create the ChromeURLRequestContext.
     78   explicit ChromeURLRequestContextGetter(
     79       ChromeURLRequestContextFactory* factory);
     80 
     81   // Note that GetURLRequestContext() can only be called from the IO
     82   // thread (it will assert otherwise).
     83   // GetIOMessageLoopProxy however can be called from any thread.
     84   //
     85   // net::URLRequestContextGetter implementation.
     86   virtual ChromeURLRequestContext* GetURLRequestContext() OVERRIDE;
     87   virtual scoped_refptr<base::SingleThreadTaskRunner>
     88       GetNetworkTaskRunner() const OVERRIDE;
     89 
     90   // Create an instance for use with an 'original' (non-OTR) profile. This is
     91   // expected to get called on the UI thread.
     92   static ChromeURLRequestContextGetter* CreateOriginal(
     93       Profile* profile,
     94       const ProfileIOData* profile_io_data,
     95       content::ProtocolHandlerMap* protocol_handlers);
     96 
     97   // Create an instance for an original profile for media. This is expected to
     98   // get called on UI thread. This method takes a profile and reuses the
     99   // 'original' net::URLRequestContext for common files.
    100   static ChromeURLRequestContextGetter* CreateOriginalForMedia(
    101       Profile* profile, const ProfileIOData* profile_io_data);
    102 
    103   // Create an instance for an original profile for extensions. This is expected
    104   // to get called on UI thread.
    105   static ChromeURLRequestContextGetter* CreateOriginalForExtensions(
    106       Profile* profile, const ProfileIOData* profile_io_data);
    107 
    108   // Create an instance for an original profile for an app with isolated
    109   // storage. This is expected to get called on UI thread.
    110   static ChromeURLRequestContextGetter* CreateOriginalForIsolatedApp(
    111       Profile* profile,
    112       const ProfileIOData* profile_io_data,
    113       const StoragePartitionDescriptor& partition_descriptor,
    114       scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
    115           protocol_handler_interceptor,
    116       content::ProtocolHandlerMap* protocol_handlers);
    117 
    118   // Create an instance for an original profile for media with isolated
    119   // storage. This is expected to get called on UI thread.
    120   static ChromeURLRequestContextGetter* CreateOriginalForIsolatedMedia(
    121       Profile* profile,
    122       ChromeURLRequestContextGetter* app_context,
    123       const ProfileIOData* profile_io_data,
    124       const StoragePartitionDescriptor& partition_descriptor);
    125 
    126   // Create an instance for use with an OTR profile. This is expected to get
    127   // called on the UI thread.
    128   static ChromeURLRequestContextGetter* CreateOffTheRecord(
    129       Profile* profile,
    130       const ProfileIOData* profile_io_data,
    131       content::ProtocolHandlerMap* protocol_handlers);
    132 
    133   // Create an instance for an OTR profile for extensions. This is expected
    134   // to get called on UI thread.
    135   static ChromeURLRequestContextGetter* CreateOffTheRecordForExtensions(
    136       Profile* profile, const ProfileIOData* profile_io_data);
    137 
    138   // Create an instance for an OTR profile for an app with isolated storage.
    139   // This is expected to get called on UI thread.
    140   static ChromeURLRequestContextGetter* CreateOffTheRecordForIsolatedApp(
    141       Profile* profile,
    142       const ProfileIOData* profile_io_data,
    143       const StoragePartitionDescriptor& partition_descriptor,
    144       scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
    145           protocol_handler_interceptor,
    146       content::ProtocolHandlerMap* protocol_handlers);
    147 
    148  private:
    149   virtual ~ChromeURLRequestContextGetter();
    150 
    151   // Deferred logic for creating a ChromeURLRequestContext.
    152   // Access only from the IO thread.
    153   scoped_ptr<ChromeURLRequestContextFactory> factory_;
    154 
    155   // NULL if not yet initialized. Otherwise, it is the ChromeURLRequestContext
    156   // instance that was lazily created by GetURLRequestContext().
    157   // Access only from the IO thread.
    158   base::WeakPtr<ChromeURLRequestContext> url_request_context_;
    159 
    160   DISALLOW_COPY_AND_ASSIGN(ChromeURLRequestContextGetter);
    161 };
    162 
    163 #endif  // CHROME_BROWSER_NET_CHROME_URL_REQUEST_CONTEXT_H_
    164