Home | History | Annotate | Download | only in browser
      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 CONTENT_PUBLIC_BROWSER_BROWSER_CONTEXT_H_
      6 #define CONTENT_PUBLIC_BROWSER_BROWSER_CONTEXT_H_
      7 
      8 #include "base/callback_forward.h"
      9 #include "base/containers/hash_tables.h"
     10 #include "base/memory/scoped_ptr.h"
     11 #include "base/supports_user_data.h"
     12 #include "content/common/content_export.h"
     13 
     14 class GURL;
     15 
     16 namespace base {
     17 class FilePath;
     18 }
     19 
     20 namespace fileapi {
     21 class ExternalMountPoints;
     22 }
     23 
     24 namespace net {
     25 class URLRequestContextGetter;
     26 }
     27 
     28 namespace quota {
     29 class SpecialStoragePolicy;
     30 }
     31 
     32 namespace content {
     33 
     34 class BlobHandle;
     35 class BrowserPluginGuestManager;
     36 class DownloadManager;
     37 class DownloadManagerDelegate;
     38 class IndexedDBContext;
     39 class PushMessagingService;
     40 class ResourceContext;
     41 class SiteInstance;
     42 class StoragePartition;
     43 
     44 // This class holds the context needed for a browsing session.
     45 // It lives on the UI thread. All these methods must only be called on the UI
     46 // thread.
     47 class CONTENT_EXPORT BrowserContext : public base::SupportsUserData {
     48  public:
     49   static DownloadManager* GetDownloadManager(BrowserContext* browser_context);
     50 
     51   // Returns BrowserContext specific external mount points. It may return NULL
     52   // if the context doesn't have any BrowserContext specific external mount
     53   // points. Currenty, non-NULL value is returned only on ChromeOS.
     54   static fileapi::ExternalMountPoints* GetMountPoints(BrowserContext* context);
     55 
     56   static content::StoragePartition* GetStoragePartition(
     57       BrowserContext* browser_context, SiteInstance* site_instance);
     58   static content::StoragePartition* GetStoragePartitionForSite(
     59       BrowserContext* browser_context, const GURL& site);
     60   typedef base::Callback<void(StoragePartition*)> StoragePartitionCallback;
     61   static void ForEachStoragePartition(
     62       BrowserContext* browser_context,
     63       const StoragePartitionCallback& callback);
     64   static void AsyncObliterateStoragePartition(
     65       BrowserContext* browser_context,
     66       const GURL& site,
     67       const base::Closure& on_gc_required);
     68 
     69   // This function clears the contents of |active_paths| but does not take
     70   // ownership of the pointer.
     71   static void GarbageCollectStoragePartitions(
     72       BrowserContext* browser_context,
     73       scoped_ptr<base::hash_set<base::FilePath> > active_paths,
     74       const base::Closure& done);
     75 
     76   // DON'T USE THIS. GetDefaultStoragePartition() is going away.
     77   // Use GetStoragePartition() instead. Ask ajwong@ if you have problems.
     78   static content::StoragePartition* GetDefaultStoragePartition(
     79       BrowserContext* browser_context);
     80 
     81   typedef base::Callback<void(scoped_ptr<BlobHandle>)> BlobCallback;
     82 
     83   // |callback| returns a NULL scoped_ptr on failure.
     84   static void CreateMemoryBackedBlob(BrowserContext* browser_context,
     85                                      const char* data, size_t length,
     86                                      const BlobCallback& callback);
     87 
     88   // Ensures that the corresponding ResourceContext is initialized. Normally the
     89   // BrowserContext initializs the corresponding getters when its objects are
     90   // created, but if the embedder wants to pass the ResourceContext to another
     91   // thread before they use BrowserContext, they should call this to make sure
     92   // that the ResourceContext is ready.
     93   static void EnsureResourceContextInitialized(BrowserContext* browser_context);
     94 
     95   // Tells the HTML5 objects on this context to persist their session state
     96   // across the next restart.
     97   static void SaveSessionState(BrowserContext* browser_context);
     98 
     99   virtual ~BrowserContext();
    100 
    101   // Returns the path of the directory where this context's data is stored.
    102   virtual base::FilePath GetPath() const = 0;
    103 
    104   // Return whether this context is incognito. Default is false.
    105   virtual bool IsOffTheRecord() const = 0;
    106 
    107   // Returns the request context information associated with this context.  Call
    108   // this only on the UI thread, since it can send notifications that should
    109   // happen on the UI thread.
    110   // TODO(creis): Remove this version in favor of the one below.
    111   virtual net::URLRequestContextGetter* GetRequestContext() = 0;
    112 
    113   // Returns the request context appropriate for the given renderer. If the
    114   // renderer process doesn't have an associated installed app, or if the
    115   // installed app doesn't have isolated storage, this is equivalent to calling
    116   // GetRequestContext().
    117   virtual net::URLRequestContextGetter* GetRequestContextForRenderProcess(
    118       int renderer_child_id) = 0;
    119 
    120   // Returns the default request context for media resources associated with
    121   // this context.
    122   // TODO(creis): Remove this version in favor of the one below.
    123   virtual net::URLRequestContextGetter* GetMediaRequestContext() = 0;
    124 
    125   // Returns the request context for media resources associated with this
    126   // context and renderer process.
    127   virtual net::URLRequestContextGetter* GetMediaRequestContextForRenderProcess(
    128       int renderer_child_id) = 0;
    129   virtual net::URLRequestContextGetter*
    130       GetMediaRequestContextForStoragePartition(
    131           const base::FilePath& partition_path,
    132           bool in_memory) = 0;
    133 
    134   // Returns the resource context.
    135   virtual ResourceContext* GetResourceContext() = 0;
    136 
    137   // Returns the DownloadManagerDelegate for this context. This will be called
    138   // once per context. The embedder owns the delegate and is responsible for
    139   // ensuring that it outlives DownloadManager. It's valid to return NULL.
    140   virtual DownloadManagerDelegate* GetDownloadManagerDelegate() = 0;
    141 
    142   // Returns the guest manager for this context.
    143   virtual BrowserPluginGuestManager* GetGuestManager() = 0;
    144 
    145   // Returns a special storage policy implementation, or NULL.
    146   virtual quota::SpecialStoragePolicy* GetSpecialStoragePolicy() = 0;
    147 
    148   // Returns a push messaging service. The embedder owns the service, and is
    149   // responsible for ensuring that it outlives RenderProcessHost. It's valid to
    150   // return NULL.
    151   virtual PushMessagingService* GetPushMessagingService() = 0;
    152 };
    153 
    154 }  // namespace content
    155 
    156 #if defined(COMPILER_GCC)
    157 namespace BASE_HASH_NAMESPACE {
    158 
    159 template<>
    160 struct hash<content::BrowserContext*> {
    161   std::size_t operator()(content::BrowserContext* const& p) const {
    162     return reinterpret_cast<std::size_t>(p);
    163   }
    164 };
    165 
    166 }  // namespace BASE_HASH_NAMESPACE
    167 #endif
    168 
    169 #endif  // CONTENT_PUBLIC_BROWSER_BROWSER_CONTEXT_H_
    170