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"
21 class ExternalMountPoints;
25 class URLRequestContextGetter;
29 class SpecialStoragePolicy;
34 class DownloadManager;
35 class DownloadManagerDelegate;
36 class GeolocationPermissionContext;
37 class IndexedDBContext;
38 class ResourceContext;
40 class StoragePartition;
42 // This class holds the context needed for a browsing session.
43 // It lives on the UI thread. All these methods must only be called on the UI
45 class CONTENT_EXPORT BrowserContext : public base::SupportsUserData {
47 static DownloadManager* GetDownloadManager(BrowserContext* browser_context);
49 // Returns BrowserContext specific external mount points. It may return NULL
50 // if the context doesn't have any BrowserContext specific external mount
51 // points. Currenty, non-NULL value is returned only on ChromeOS.
52 static fileapi::ExternalMountPoints* GetMountPoints(BrowserContext* context);
54 static content::StoragePartition* GetStoragePartition(
55 BrowserContext* browser_context, SiteInstance* site_instance);
56 static content::StoragePartition* GetStoragePartitionForSite(
57 BrowserContext* browser_context, const GURL& site);
58 typedef base::Callback<void(StoragePartition*)> StoragePartitionCallback;
59 static void ForEachStoragePartition(
60 BrowserContext* browser_context,
61 const StoragePartitionCallback& callback);
62 static void AsyncObliterateStoragePartition(
63 BrowserContext* browser_context,
65 const base::Closure& on_gc_required);
67 // This function clears the contents of |active_paths| but does not take
68 // ownership of the pointer.
69 static void GarbageCollectStoragePartitions(
70 BrowserContext* browser_context,
71 scoped_ptr<base::hash_set<base::FilePath> > active_paths,
72 const base::Closure& done);
74 // DON'T USE THIS. GetDefaultStoragePartition() is going away.
75 // Use GetStoragePartition() instead. Ask ajwong@ if you have problems.
76 static content::StoragePartition* GetDefaultStoragePartition(
77 BrowserContext* browser_context);
79 // Ensures that the corresponding ResourceContext is initialized. Normally the
80 // BrowserContext initializs the corresponding getters when its objects are
81 // created, but if the embedder wants to pass the ResourceContext to another
82 // thread before they use BrowserContext, they should call this to make sure
83 // that the ResourceContext is ready.
84 static void EnsureResourceContextInitialized(BrowserContext* browser_context);
86 // Tells the HTML5 objects on this context to persist their session state
87 // across the next restart.
88 static void SaveSessionState(BrowserContext* browser_context);
90 // Tells the HTML5 objects on this context to purge any uneeded memory.
91 static void PurgeMemory(BrowserContext* browser_context);
93 virtual ~BrowserContext();
95 // Returns the path of the directory where this context's data is stored.
96 virtual base::FilePath GetPath() const = 0;
98 // Return whether this context is incognito. Default is false.
99 virtual bool IsOffTheRecord() const = 0;
101 // Returns the request context information associated with this context. Call
102 // this only on the UI thread, since it can send notifications that should
103 // happen on the UI thread.
104 // TODO(creis): Remove this version in favor of the one below.
105 virtual net::URLRequestContextGetter* GetRequestContext() = 0;
107 // Returns the request context appropriate for the given renderer. If the
108 // renderer process doesn't have an associated installed app, or if the
109 // installed app doesn't have isolated storage, this is equivalent to calling
110 // GetRequestContext().
111 virtual net::URLRequestContextGetter* GetRequestContextForRenderProcess(
112 int renderer_child_id) = 0;
114 // Returns the default request context for media resources associated with
116 // TODO(creis): Remove this version in favor of the one below.
117 virtual net::URLRequestContextGetter* GetMediaRequestContext() = 0;
119 // Returns the request context for media resources associated with this
120 // context and renderer process.
121 virtual net::URLRequestContextGetter* GetMediaRequestContextForRenderProcess(
122 int renderer_child_id) = 0;
123 virtual net::URLRequestContextGetter*
124 GetMediaRequestContextForStoragePartition(
125 const base::FilePath& partition_path,
128 typedef base::Callback<void(bool)> MidiSysExPermissionCallback;
130 // Requests a permission to use system exclusive messages in MIDI events.
131 // |callback| will be invoked when the request is resolved.
132 virtual void RequestMidiSysExPermission(
133 int render_process_id,
136 const GURL& requesting_frame,
137 const MidiSysExPermissionCallback& callback) = 0;
139 // Cancels a pending MIDI permission request.
140 virtual void CancelMidiSysExPermissionRequest(
141 int render_process_id,
144 const GURL& requesting_frame) = 0;
146 typedef base::Callback<void(bool)> ProtectedMediaIdentifierPermissionCallback;
148 // Request permission to access protected media identifier. The callback will
149 // tell whether it's permitted.
150 virtual void RequestProtectedMediaIdentifierPermission(
151 int render_process_id,
155 const GURL& requesting_frame,
156 const ProtectedMediaIdentifierPermissionCallback& callback) = 0;
158 // Cancels pending protected media identifier permission requests.
159 virtual void CancelProtectedMediaIdentifierPermissionRequests(
162 // Returns the resource context.
163 virtual ResourceContext* GetResourceContext() = 0;
165 // Returns the DownloadManagerDelegate for this context. This will be called
166 // once per context. The embedder owns the delegate and is responsible for
167 // ensuring that it outlives DownloadManager. It's valid to return NULL.
168 virtual DownloadManagerDelegate* GetDownloadManagerDelegate() = 0;
170 // Returns the geolocation permission context for this context. It's valid to
171 // return NULL, in which case geolocation requests will always be allowed.
172 virtual GeolocationPermissionContext* GetGeolocationPermissionContext() = 0;
174 // Returns a special storage policy implementation, or NULL.
175 virtual quota::SpecialStoragePolicy* GetSpecialStoragePolicy() = 0;
178 } // namespace content
180 #if defined(COMPILER_GCC)
181 namespace BASE_HASH_NAMESPACE {
184 struct hash<content::BrowserContext*> {
185 std::size_t operator()(content::BrowserContext* const& p) const {
186 return reinterpret_cast<std::size_t>(p);
190 } // namespace BASE_HASH_NAMESPACE
193 #endif // CONTENT_PUBLIC_BROWSER_BROWSER_CONTEXT_H_