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/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 "ui/base/clipboard/clipboard_sourcetag.h"
22 class ExternalMountPoints
;
26 class URLRequestContextGetter
;
30 class SpecialStoragePolicy
;
35 class DownloadManager
;
36 class DownloadManagerDelegate
;
37 class GeolocationPermissionContext
;
38 class IndexedDBContext
;
39 class ResourceContext
;
41 class SpeechRecognitionPreferences
;
42 class StoragePartition
;
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
47 class CONTENT_EXPORT BrowserContext
: public base::SupportsUserData
{
49 // Used in ForEachStoragePartition(). The first argument is the partition id.
50 // The second argument is the StoragePartition object for that partition id.
51 typedef base::Callback
<void(StoragePartition
*)> StoragePartitionCallback
;
53 static DownloadManager
* GetDownloadManager(BrowserContext
* browser_context
);
55 // Returns BrowserContext specific external mount points. It may return NULL
56 // if the context doesn't have any BrowserContext specific external mount
57 // points. Currenty, non-NULL value is returned only on ChromeOS.
58 static fileapi::ExternalMountPoints
* GetMountPoints(BrowserContext
* context
);
60 static content::StoragePartition
* GetStoragePartition(
61 BrowserContext
* browser_context
, SiteInstance
* site_instance
);
62 static content::StoragePartition
* GetStoragePartitionForSite(
63 BrowserContext
* browser_context
, const GURL
& site
);
64 static void ForEachStoragePartition(
65 BrowserContext
* browser_context
,
66 const StoragePartitionCallback
& callback
);
67 static void AsyncObliterateStoragePartition(
68 BrowserContext
* browser_context
,
70 const base::Closure
& on_gc_required
);
72 // This function clears the contents of |active_paths| but does not take
73 // ownership of the pointer.
74 static void GarbageCollectStoragePartitions(
75 BrowserContext
* browser_context
,
76 scoped_ptr
<base::hash_set
<base::FilePath
> > active_paths
,
77 const base::Closure
& done
);
79 // DON'T USE THIS. GetDefaultStoragePartition() is going away.
80 // Use GetStoragePartition() instead. Ask ajwong@ if you have problems.
81 static content::StoragePartition
* GetDefaultStoragePartition(
82 BrowserContext
* browser_context
);
84 // Ensures that the corresponding ResourceContext is initialized. Normally the
85 // BrowserContext initializs the corresponding getters when its objects are
86 // created, but if the embedder wants to pass the ResourceContext to another
87 // thread before they use BrowserContext, they should call this to make sure
88 // that the ResourceContext is ready.
89 static void EnsureResourceContextInitialized(BrowserContext
* browser_context
);
91 // Tells the HTML5 objects on this context to persist their session state
92 // across the next restart.
93 static void SaveSessionState(BrowserContext
* browser_context
);
95 // Tells the HTML5 objects on this context to purge any uneeded memory.
96 static void PurgeMemory(BrowserContext
* browser_context
);
98 // Returns a Clipboard::SourceTag (pointer) if |context| is OffTheRecord
99 // context. Otherwise, NULL. If the clipboard contains that SourceTag at the
100 // time of |context| destruction it will be flushed.
101 static ui::SourceTag
GetMarkerForOffTheRecordContext(BrowserContext
* context
);
103 virtual ~BrowserContext();
105 // Returns the path of the directory where this context's data is stored.
106 virtual base::FilePath
GetPath() = 0;
108 // Return whether this context is incognito. Default is false.
109 virtual bool IsOffTheRecord() const = 0;
111 // Returns the request context information associated with this context. Call
112 // this only on the UI thread, since it can send notifications that should
113 // happen on the UI thread.
114 // TODO(creis): Remove this version in favor of the one below.
115 virtual net::URLRequestContextGetter
* GetRequestContext() = 0;
117 // Returns the request context appropriate for the given renderer. If the
118 // renderer process doesn't have an associated installed app, or if the
119 // installed app doesn't have isolated storage, this is equivalent to calling
120 // GetRequestContext().
121 virtual net::URLRequestContextGetter
* GetRequestContextForRenderProcess(
122 int renderer_child_id
) = 0;
124 // Returns the default request context for media resources associated with
126 // TODO(creis): Remove this version in favor of the one below.
127 virtual net::URLRequestContextGetter
* GetMediaRequestContext() = 0;
129 // Returns the request context for media resources associated with this
130 // context and renderer process.
131 virtual net::URLRequestContextGetter
* GetMediaRequestContextForRenderProcess(
132 int renderer_child_id
) = 0;
133 virtual net::URLRequestContextGetter
*
134 GetMediaRequestContextForStoragePartition(
135 const base::FilePath
& partition_path
,
138 // Returns the resource context.
139 virtual ResourceContext
* GetResourceContext() = 0;
141 // Returns the DownloadManagerDelegate for this context. This will be called
142 // once per context. The embedder owns the delegate and is responsible for
143 // ensuring that it outlives DownloadManager. It's valid to return NULL.
144 virtual DownloadManagerDelegate
* GetDownloadManagerDelegate() = 0;
146 // Returns the geolocation permission context for this context. It's valid to
147 // return NULL, in which case geolocation requests will always be allowed.
148 virtual GeolocationPermissionContext
* GetGeolocationPermissionContext() = 0;
150 // Returns the speech input preferences. SpeechRecognitionPreferences is a
151 // ref counted class, so callers should take a reference if needed. It's valid
153 virtual SpeechRecognitionPreferences
* GetSpeechRecognitionPreferences() = 0;
155 // Returns a special storage policy implementation, or NULL.
156 virtual quota::SpecialStoragePolicy
* GetSpecialStoragePolicy() = 0;
159 } // namespace content
161 #if defined(COMPILER_GCC)
162 namespace BASE_HASH_NAMESPACE
{
165 struct hash
<content::BrowserContext
*> {
166 std::size_t operator()(content::BrowserContext
* const& p
) const {
167 return reinterpret_cast<std::size_t>(p
);
171 } // namespace BASE_HASH_NAMESPACE
174 #endif // CONTENT_PUBLIC_BROWSER_BROWSER_CONTEXT_H_