gn: 'platform_impl' should be a group instead of component (since it has no code...
[chromium-blink-merge.git] / content / browser / browser_context.cc
bloba7394834cc21304a26293c7e56cc12a46e8e13ff
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 #include "content/public/browser/browser_context.h"
7 #if !defined(OS_IOS)
8 #include "content/browser/download/download_manager_impl.h"
9 #include "content/browser/fileapi/chrome_blob_storage_context.h"
10 #include "content/browser/indexed_db/indexed_db_context_impl.h"
11 #include "content/browser/loader/resource_dispatcher_host_impl.h"
12 #include "content/browser/push_messaging/push_messaging_router.h"
13 #include "content/browser/storage_partition_impl_map.h"
14 #include "content/common/child_process_host_impl.h"
15 #include "content/public/browser/blob_handle.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/content_browser_client.h"
18 #include "content/public/browser/site_instance.h"
19 #include "net/cookies/cookie_monster.h"
20 #include "net/cookies/cookie_store.h"
21 #include "net/ssl/channel_id_service.h"
22 #include "net/ssl/channel_id_store.h"
23 #include "net/url_request/url_request_context.h"
24 #include "net/url_request/url_request_context_getter.h"
25 #include "storage/browser/database/database_tracker.h"
26 #include "storage/browser/fileapi/external_mount_points.h"
27 #endif // !OS_IOS
29 using base::UserDataAdapter;
31 namespace content {
33 // Only ~BrowserContext() is needed on iOS.
34 #if !defined(OS_IOS)
35 namespace {
37 // Key names on BrowserContext.
38 const char kDownloadManagerKeyName[] = "download_manager";
39 const char kStorageParitionMapKeyName[] = "content_storage_partition_map";
41 #if defined(OS_CHROMEOS)
42 const char kMountPointsKey[] = "mount_points";
43 #endif // defined(OS_CHROMEOS)
45 StoragePartitionImplMap* GetStoragePartitionMap(
46 BrowserContext* browser_context) {
47 StoragePartitionImplMap* partition_map =
48 static_cast<StoragePartitionImplMap*>(
49 browser_context->GetUserData(kStorageParitionMapKeyName));
50 if (!partition_map) {
51 partition_map = new StoragePartitionImplMap(browser_context);
52 browser_context->SetUserData(kStorageParitionMapKeyName, partition_map);
54 return partition_map;
57 StoragePartition* GetStoragePartitionFromConfig(
58 BrowserContext* browser_context,
59 const std::string& partition_domain,
60 const std::string& partition_name,
61 bool in_memory) {
62 StoragePartitionImplMap* partition_map =
63 GetStoragePartitionMap(browser_context);
65 if (browser_context->IsOffTheRecord())
66 in_memory = true;
68 return partition_map->Get(partition_domain, partition_name, in_memory);
71 void SaveSessionStateOnIOThread(
72 const scoped_refptr<net::URLRequestContextGetter>& context_getter,
73 AppCacheServiceImpl* appcache_service) {
74 net::URLRequestContext* context = context_getter->GetURLRequestContext();
75 context->cookie_store()->GetCookieMonster()->
76 SetForceKeepSessionState();
77 context->channel_id_service()->GetChannelIDStore()->
78 SetForceKeepSessionState();
79 appcache_service->set_force_keep_session_state();
82 void SaveSessionStateOnIndexedDBThread(
83 scoped_refptr<IndexedDBContextImpl> indexed_db_context) {
84 indexed_db_context->SetForceKeepSessionState();
87 void ShutdownServiceWorkerContext(StoragePartition* partition) {
88 ServiceWorkerContextWrapper* wrapper =
89 static_cast<ServiceWorkerContextWrapper*>(
90 partition->GetServiceWorkerContext());
91 wrapper->process_manager()->Shutdown();
94 } // namespace
96 // static
97 void BrowserContext::AsyncObliterateStoragePartition(
98 BrowserContext* browser_context,
99 const GURL& site,
100 const base::Closure& on_gc_required) {
101 GetStoragePartitionMap(browser_context)->AsyncObliterate(site,
102 on_gc_required);
105 // static
106 void BrowserContext::GarbageCollectStoragePartitions(
107 BrowserContext* browser_context,
108 scoped_ptr<base::hash_set<base::FilePath> > active_paths,
109 const base::Closure& done) {
110 GetStoragePartitionMap(browser_context)->GarbageCollect(
111 active_paths.Pass(), done);
114 DownloadManager* BrowserContext::GetDownloadManager(
115 BrowserContext* context) {
116 DCHECK_CURRENTLY_ON(BrowserThread::UI);
117 if (!context->GetUserData(kDownloadManagerKeyName)) {
118 ResourceDispatcherHostImpl* rdh = ResourceDispatcherHostImpl::Get();
119 DCHECK(rdh);
120 DownloadManager* download_manager =
121 new DownloadManagerImpl(
122 GetContentClient()->browser()->GetNetLog(), context);
124 context->SetUserData(
125 kDownloadManagerKeyName,
126 download_manager);
127 download_manager->SetDelegate(context->GetDownloadManagerDelegate());
130 return static_cast<DownloadManager*>(
131 context->GetUserData(kDownloadManagerKeyName));
134 // static
135 storage::ExternalMountPoints* BrowserContext::GetMountPoints(
136 BrowserContext* context) {
137 // Ensure that these methods are called on the UI thread, except for
138 // unittests where a UI thread might not have been created.
139 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
140 !BrowserThread::IsMessageLoopValid(BrowserThread::UI));
142 #if defined(OS_CHROMEOS)
143 if (!context->GetUserData(kMountPointsKey)) {
144 scoped_refptr<storage::ExternalMountPoints> mount_points =
145 storage::ExternalMountPoints::CreateRefCounted();
146 context->SetUserData(
147 kMountPointsKey,
148 new UserDataAdapter<storage::ExternalMountPoints>(mount_points.get()));
151 return UserDataAdapter<storage::ExternalMountPoints>::Get(context,
152 kMountPointsKey);
153 #else
154 return NULL;
155 #endif
158 StoragePartition* BrowserContext::GetStoragePartition(
159 BrowserContext* browser_context,
160 SiteInstance* site_instance) {
161 std::string partition_domain;
162 std::string partition_name;
163 bool in_memory = false;
165 // TODO(ajwong): After GetDefaultStoragePartition() is removed, get rid of
166 // this conditional and require that |site_instance| is non-NULL.
167 if (site_instance) {
168 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
169 browser_context, site_instance->GetSiteURL(), true,
170 &partition_domain, &partition_name, &in_memory);
173 return GetStoragePartitionFromConfig(
174 browser_context, partition_domain, partition_name, in_memory);
177 StoragePartition* BrowserContext::GetStoragePartitionForSite(
178 BrowserContext* browser_context,
179 const GURL& site) {
180 std::string partition_domain;
181 std::string partition_name;
182 bool in_memory;
184 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
185 browser_context, site, true, &partition_domain, &partition_name,
186 &in_memory);
188 return GetStoragePartitionFromConfig(
189 browser_context, partition_domain, partition_name, in_memory);
192 void BrowserContext::ForEachStoragePartition(
193 BrowserContext* browser_context,
194 const StoragePartitionCallback& callback) {
195 StoragePartitionImplMap* partition_map =
196 static_cast<StoragePartitionImplMap*>(
197 browser_context->GetUserData(kStorageParitionMapKeyName));
198 if (!partition_map)
199 return;
201 partition_map->ForEach(callback);
204 StoragePartition* BrowserContext::GetDefaultStoragePartition(
205 BrowserContext* browser_context) {
206 return GetStoragePartition(browser_context, NULL);
209 // static
210 void BrowserContext::CreateMemoryBackedBlob(BrowserContext* browser_context,
211 const char* data, size_t length,
212 const BlobCallback& callback) {
213 DCHECK_CURRENTLY_ON(BrowserThread::UI);
215 ChromeBlobStorageContext* blob_context =
216 ChromeBlobStorageContext::GetFor(browser_context);
217 BrowserThread::PostTaskAndReplyWithResult(
218 BrowserThread::IO, FROM_HERE,
219 base::Bind(&ChromeBlobStorageContext::CreateMemoryBackedBlob,
220 make_scoped_refptr(blob_context), data, length),
221 callback);
224 // static
225 void BrowserContext::CreateFileBackedBlob(
226 BrowserContext* browser_context,
227 const base::FilePath& path,
228 int64_t offset,
229 int64_t size,
230 const base::Time& expected_modification_time,
231 const BlobCallback& callback) {
232 DCHECK_CURRENTLY_ON(BrowserThread::UI);
234 ChromeBlobStorageContext* blob_context =
235 ChromeBlobStorageContext::GetFor(browser_context);
236 BrowserThread::PostTaskAndReplyWithResult(
237 BrowserThread::IO, FROM_HERE,
238 base::Bind(&ChromeBlobStorageContext::CreateFileBackedBlob,
239 make_scoped_refptr(blob_context), path, offset, size,
240 expected_modification_time),
241 callback);
244 // static
245 void BrowserContext::DeliverPushMessage(
246 BrowserContext* browser_context,
247 const GURL& origin,
248 int64 service_worker_registration_id,
249 const std::string& data,
250 const base::Callback<void(PushDeliveryStatus)>& callback) {
251 DCHECK_CURRENTLY_ON(BrowserThread::UI);
252 PushMessagingRouter::DeliverMessage(
253 browser_context, origin, service_worker_registration_id, data, callback);
256 // static
257 void BrowserContext::NotifyWillBeDestroyed(BrowserContext* browser_context) {
258 // Service Workers must shutdown before the browser context is destroyed,
259 // since they keep render process hosts alive and the codebase assumes that
260 // render process hosts die before their profile (browser context) dies.
261 ForEachStoragePartition(browser_context,
262 base::Bind(ShutdownServiceWorkerContext));
265 void BrowserContext::EnsureResourceContextInitialized(BrowserContext* context) {
266 // This will be enough to tickle initialization of BrowserContext if
267 // necessary, which initializes ResourceContext. The reason we don't call
268 // ResourceContext::InitializeResourceContext() directly here is that
269 // ResourceContext initialization may call back into BrowserContext
270 // and when that call returns it'll end rewriting its UserData map. It will
271 // end up rewriting the same value but this still causes a race condition.
273 // See http://crbug.com/115678.
274 GetDefaultStoragePartition(context);
277 void BrowserContext::SaveSessionState(BrowserContext* browser_context) {
278 GetDefaultStoragePartition(browser_context)->GetDatabaseTracker()->
279 SetForceKeepSessionState();
280 StoragePartition* storage_partition =
281 BrowserContext::GetDefaultStoragePartition(browser_context);
283 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) {
284 BrowserThread::PostTask(
285 BrowserThread::IO, FROM_HERE,
286 base::Bind(
287 &SaveSessionStateOnIOThread,
288 make_scoped_refptr(browser_context->GetRequestContext()),
289 static_cast<AppCacheServiceImpl*>(
290 storage_partition->GetAppCacheService())));
293 DOMStorageContextWrapper* dom_storage_context_proxy =
294 static_cast<DOMStorageContextWrapper*>(
295 storage_partition->GetDOMStorageContext());
296 dom_storage_context_proxy->SetForceKeepSessionState();
298 IndexedDBContextImpl* indexed_db_context_impl =
299 static_cast<IndexedDBContextImpl*>(
300 storage_partition->GetIndexedDBContext());
301 // No task runner in unit tests.
302 if (indexed_db_context_impl->TaskRunner()) {
303 indexed_db_context_impl->TaskRunner()->PostTask(
304 FROM_HERE,
305 base::Bind(&SaveSessionStateOnIndexedDBThread,
306 make_scoped_refptr(indexed_db_context_impl)));
310 #endif // !OS_IOS
312 BrowserContext::~BrowserContext() {
313 #if !defined(OS_IOS)
314 if (GetUserData(kDownloadManagerKeyName))
315 GetDownloadManager(this)->Shutdown();
316 #endif
319 } // namespace content