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.
5 #ifndef CONTENT_PUBLIC_BROWSER_BROWSER_CONTEXT_H_
6 #define CONTENT_PUBLIC_BROWSER_BROWSER_CONTEXT_H_
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 #include "content/public/common/push_messaging_status.h"
22 class ExternalMountPoints;
26 class URLRequestContextGetter;
30 class SpecialStoragePolicy;
36 class BrowserPluginGuestManager;
37 class DownloadManager;
38 class DownloadManagerDelegate;
39 class IndexedDBContext;
40 class PushMessagingService;
41 class ResourceContext;
43 class StoragePartition;
44 class SSLHostStateDelegate;
46 // This class holds the context needed for a browsing session.
47 // It lives on the UI thread. All these methods must only be called on the UI
49 class CONTENT_EXPORT BrowserContext : public base::SupportsUserData {
51 static DownloadManager* GetDownloadManager(BrowserContext* browser_context);
53 // Returns BrowserContext specific external mount points. It may return NULL
54 // if the context doesn't have any BrowserContext specific external mount
55 // points. Currenty, non-NULL value is returned only on ChromeOS.
56 static storage::ExternalMountPoints* GetMountPoints(BrowserContext* context);
58 static content::StoragePartition* GetStoragePartition(
59 BrowserContext* browser_context, SiteInstance* site_instance);
60 static content::StoragePartition* GetStoragePartitionForSite(
61 BrowserContext* browser_context, const GURL& site);
62 typedef base::Callback<void(StoragePartition*)> StoragePartitionCallback;
63 static void ForEachStoragePartition(
64 BrowserContext* browser_context,
65 const StoragePartitionCallback& callback);
66 static void AsyncObliterateStoragePartition(
67 BrowserContext* browser_context,
69 const base::Closure& on_gc_required);
71 // This function clears the contents of |active_paths| but does not take
72 // ownership of the pointer.
73 static void GarbageCollectStoragePartitions(
74 BrowserContext* browser_context,
75 scoped_ptr<base::hash_set<base::FilePath> > active_paths,
76 const base::Closure& done);
78 // DON'T USE THIS. GetDefaultStoragePartition() is going away.
79 // Use GetStoragePartition() instead. Ask ajwong@ if you have problems.
80 static content::StoragePartition* GetDefaultStoragePartition(
81 BrowserContext* browser_context);
83 typedef base::Callback<void(scoped_ptr<BlobHandle>)> BlobCallback;
85 // |callback| returns a NULL scoped_ptr on failure.
86 static void CreateMemoryBackedBlob(BrowserContext* browser_context,
87 const char* data, size_t length,
88 const BlobCallback& callback);
90 // Delivers a push message with |data| to the Service Worker identified by
91 // |origin| and |service_worker_registration_id|.
92 static void DeliverPushMessage(
93 BrowserContext* browser_context,
95 int64 service_worker_registration_id,
96 const std::string& data,
97 const base::Callback<void(PushMessagingStatus)>& callback);
99 // Ensures that the corresponding ResourceContext is initialized. Normally the
100 // BrowserContext initializs the corresponding getters when its objects are
101 // created, but if the embedder wants to pass the ResourceContext to another
102 // thread before they use BrowserContext, they should call this to make sure
103 // that the ResourceContext is ready.
104 static void EnsureResourceContextInitialized(BrowserContext* browser_context);
106 // Tells the HTML5 objects on this context to persist their session state
107 // across the next restart.
108 static void SaveSessionState(BrowserContext* browser_context);
110 virtual ~BrowserContext();
112 // Returns the path of the directory where this context's data is stored.
113 virtual base::FilePath GetPath() const = 0;
115 // Return whether this context is incognito. Default is false.
116 virtual bool IsOffTheRecord() const = 0;
118 // Returns the request context information associated with this context. Call
119 // this only on the UI thread, since it can send notifications that should
120 // happen on the UI thread.
121 // TODO(creis): Remove this version in favor of the one below.
122 virtual net::URLRequestContextGetter* GetRequestContext() = 0;
124 // Returns the request context appropriate for the given renderer. If the
125 // renderer process doesn't have an associated installed app, or if the
126 // installed app doesn't have isolated storage, this is equivalent to calling
127 // GetRequestContext().
128 virtual net::URLRequestContextGetter* GetRequestContextForRenderProcess(
129 int renderer_child_id) = 0;
131 // Returns the default request context for media resources associated with
133 // TODO(creis): Remove this version in favor of the one below.
134 virtual net::URLRequestContextGetter* GetMediaRequestContext() = 0;
136 // Returns the request context for media resources associated with this
137 // context and renderer process.
138 virtual net::URLRequestContextGetter* GetMediaRequestContextForRenderProcess(
139 int renderer_child_id) = 0;
140 virtual net::URLRequestContextGetter*
141 GetMediaRequestContextForStoragePartition(
142 const base::FilePath& partition_path,
145 // Returns the resource context.
146 virtual ResourceContext* GetResourceContext() = 0;
148 // Returns the DownloadManagerDelegate for this context. This will be called
149 // once per context. The embedder owns the delegate and is responsible for
150 // ensuring that it outlives DownloadManager. It's valid to return NULL.
151 virtual DownloadManagerDelegate* GetDownloadManagerDelegate() = 0;
153 // Returns the guest manager for this context.
154 virtual BrowserPluginGuestManager* GetGuestManager() = 0;
156 // Returns a special storage policy implementation, or NULL.
157 virtual storage::SpecialStoragePolicy* GetSpecialStoragePolicy() = 0;
159 // Returns a push messaging service. The embedder owns the service, and is
160 // responsible for ensuring that it outlives RenderProcessHost. It's valid to
162 virtual PushMessagingService* GetPushMessagingService() = 0;
164 // Returns the SSL host state decisions for this context. The context may
165 // return NULL, implementing the default exception storage strategy.
166 virtual SSLHostStateDelegate* GetSSLHostStateDelegate() = 0;
169 } // namespace content
171 #if defined(COMPILER_GCC)
172 namespace BASE_HASH_NAMESPACE {
175 struct hash<content::BrowserContext*> {
176 std::size_t operator()(content::BrowserContext* const& p) const {
177 return reinterpret_cast<std::size_t>(p);
181 } // namespace BASE_HASH_NAMESPACE
184 #endif // CONTENT_PUBLIC_BROWSER_BROWSER_CONTEXT_H_