browser_context.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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/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.h"
14
15class GURL;
16
17namespace base {
18class FilePath;
19}
20
21namespace fileapi {
22class ExternalMountPoints;
23}
24
25namespace net {
26class URLRequestContextGetter;
27}
28
29namespace quota {
30class SpecialStoragePolicy;
31}
32
33namespace content {
34
35class DownloadManager;
36class DownloadManagerDelegate;
37class GeolocationPermissionContext;
38class IndexedDBContext;
39class ResourceContext;
40class SiteInstance;
41class SpeechRecognitionPreferences;
42class 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.
47class CONTENT_EXPORT BrowserContext : public base::SupportsUserData {
48 public:
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;
52
53  static DownloadManager* GetDownloadManager(BrowserContext* browser_context);
54
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);
59
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,
69      const GURL& site,
70      const base::Closure& on_gc_required);
71
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);
78
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);
83
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);
90
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);
94
95  // Tells the HTML5 objects on this context to purge any uneeded memory.
96  static void PurgeMemory(BrowserContext* browser_context);
97
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::Clipboard::SourceTag GetMarkerForOffTheRecordContext(
102      BrowserContext* context);
103
104  virtual ~BrowserContext();
105
106  // Returns the path of the directory where this context's data is stored.
107  virtual base::FilePath GetPath() = 0;
108
109  // Return whether this context is incognito. Default is false.
110  // This doesn't belong here; http://crbug.com/89628
111  virtual bool IsOffTheRecord() const = 0;
112
113  // Returns the request context information associated with this context.  Call
114  // this only on the UI thread, since it can send notifications that should
115  // happen on the UI thread.
116  // TODO(creis): Remove this version in favor of the one below.
117  virtual net::URLRequestContextGetter* GetRequestContext() = 0;
118
119  // Returns the request context appropriate for the given renderer. If the
120  // renderer process doesn't have an associated installed app, or if the
121  // installed app's is_storage_isolated() returns false, this is equivalent to
122  // calling GetRequestContext().
123  virtual net::URLRequestContextGetter* GetRequestContextForRenderProcess(
124      int renderer_child_id) = 0;
125
126  // Returns the default request context for media resources associated with
127  // this context.
128  // TODO(creis): Remove this version in favor of the one below.
129  virtual net::URLRequestContextGetter* GetMediaRequestContext() = 0;
130
131  // Returns the request context for media resources associated with this
132  // context and renderer process.
133  virtual net::URLRequestContextGetter* GetMediaRequestContextForRenderProcess(
134      int renderer_child_id) = 0;
135  virtual net::URLRequestContextGetter*
136      GetMediaRequestContextForStoragePartition(
137          const base::FilePath& partition_path,
138          bool in_memory) = 0;
139
140  // Returns the resource context.
141  virtual ResourceContext* GetResourceContext() = 0;
142
143  // Returns the DownloadManagerDelegate for this context. This will be called
144  // once per context. The embedder owns the delegate and is responsible for
145  // ensuring that it outlives DownloadManager. It's valid to return NULL.
146  virtual DownloadManagerDelegate* GetDownloadManagerDelegate() = 0;
147
148  // Returns the geolocation permission context for this context. It's valid to
149  // return NULL, in which case geolocation requests will always be allowed.
150  virtual GeolocationPermissionContext* GetGeolocationPermissionContext() = 0;
151
152  // Returns the speech input preferences. SpeechRecognitionPreferences is a
153  // ref counted class, so callers should take a reference if needed. It's valid
154  // to return NULL.
155  virtual SpeechRecognitionPreferences* GetSpeechRecognitionPreferences() = 0;
156
157  // Returns a special storage policy implementation, or NULL.
158  virtual quota::SpecialStoragePolicy* GetSpecialStoragePolicy() = 0;
159};
160
161}  // namespace content
162
163#if defined(COMPILER_GCC)
164namespace BASE_HASH_NAMESPACE {
165
166template<>
167struct hash<content::BrowserContext*> {
168  std::size_t operator()(content::BrowserContext* const& p) const {
169    return reinterpret_cast<std::size_t>(p);
170  }
171};
172
173}  // namespace BASE_HASH_NAMESPACE
174#endif
175
176#endif  // CONTENT_PUBLIC_BROWSER_BROWSER_CONTEXT_H_
177