blob: 10db382998c3675576aa9cbdb49f805c19fe317e [file] [log] [blame]
[email protected]55eb70e762012-02-20 17:38:391// 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#include "content/public/browser/browser_context.h"
6
avib7348942015-12-25 20:57:107#include <stddef.h>
8#include <stdint.h>
erge69130f52016-03-02 00:13:289#include <algorithm>
10#include <limits>
dcheng36b6aec92015-12-26 06:16:3611#include <utility>
avib7348942015-12-25 20:57:1012
bencccfe2a2016-03-05 16:54:1413#include "base/guid.h"
erge69130f52016-03-02 00:13:2814#include "base/lazy_instance.h"
15#include "base/rand_util.h"
avib7348942015-12-25 20:57:1016#include "build/build_config.h"
17
[email protected]e0ce8a1e2012-09-18 10:26:3618#if !defined(OS_IOS)
erge69130f52016-03-02 00:13:2819#include "components/profile_service/profile_app.h"
[email protected]b441a8492012-06-06 14:55:5720#include "content/browser/download/download_manager_impl.h"
[email protected]393b6cb2014-05-15 00:55:1221#include "content/browser/fileapi/chrome_blob_storage_context.h"
[email protected]c4d281662013-03-31 00:35:0822#include "content/browser/indexed_db/indexed_db_context_impl.h"
[email protected]678c0362012-12-05 08:02:4423#include "content/browser/loader/resource_dispatcher_host_impl.h"
mvanouwerkerk17205ea2014-11-07 17:30:1524#include "content/browser/push_messaging/push_messaging_router.h"
[email protected]4c3a23582012-08-18 08:54:3425#include "content/browser/storage_partition_impl_map.h"
[email protected]d7c7c98a2012-07-12 21:27:4426#include "content/common/child_process_host_impl.h"
[email protected]393b6cb2014-05-15 00:55:1227#include "content/public/browser/blob_handle.h"
[email protected]55eb70e762012-02-20 17:38:3928#include "content/public/browser/browser_thread.h"
[email protected]b441a8492012-06-06 14:55:5729#include "content/public/browser/content_browser_client.h"
[email protected]536fd0b2013-03-14 17:41:5730#include "content/public/browser/site_instance.h"
[email protected]4d7c4ef2012-03-16 01:47:1231#include "net/cookies/cookie_store.h"
[email protected]6b8a3c742014-07-25 00:25:3532#include "net/ssl/channel_id_service.h"
33#include "net/ssl/channel_id_store.h"
[email protected]6e2d3d22012-02-24 18:10:3634#include "net/url_request/url_request_context.h"
[email protected]6939075a2012-08-28 08:35:5335#include "net/url_request/url_request_context_getter.h"
pilgrime92c5fcd2014-09-10 23:31:2336#include "storage/browser/database/database_tracker.h"
37#include "storage/browser/fileapi/external_mount_points.h"
[email protected]e0ce8a1e2012-09-18 10:26:3638#endif // !OS_IOS
[email protected]55eb70e762012-02-20 17:38:3939
[email protected]314c3e22012-02-21 03:57:4240using base::UserDataAdapter;
[email protected]55eb70e762012-02-20 17:38:3941
[email protected]55eb70e762012-02-20 17:38:3942namespace content {
43
[email protected]e0ce8a1e2012-09-18 10:26:3644// Only ~BrowserContext() is needed on iOS.
45#if !defined(OS_IOS)
[email protected]735e20c2012-03-20 01:16:5946namespace {
47
bencccfe2a2016-03-05 16:54:1448base::LazyInstance<std::set<std::string>> g_used_user_ids =
erge69130f52016-03-02 00:13:2849 LAZY_INSTANCE_INITIALIZER;
bencccfe2a2016-03-05 16:54:1450base::LazyInstance<std::vector<std::pair<BrowserContext*, std::string>>>
erge69130f52016-03-02 00:13:2851g_context_to_user_id = LAZY_INSTANCE_INITIALIZER;
52
[email protected]e0ce8a1e2012-09-18 10:26:3653// Key names on BrowserContext.
[email protected]6ef0c3912013-01-25 22:46:3454const char kDownloadManagerKeyName[] = "download_manager";
a.cavalcantiffab73762015-08-15 02:55:4855const char kStoragePartitionMapKeyName[] = "content_storage_partition_map";
[email protected]e0ce8a1e2012-09-18 10:26:3656
erge69130f52016-03-02 00:13:2857const char kMojoWasInitialized[] = "mojo-was-initialized";
58
[email protected]9afc14e22013-09-25 22:34:1459#if defined(OS_CHROMEOS)
60const char kMountPointsKey[] = "mount_points";
61#endif // defined(OS_CHROMEOS)
62
[email protected]14acc642012-11-17 12:20:1063StoragePartitionImplMap* GetStoragePartitionMap(
64 BrowserContext* browser_context) {
[email protected]4c3a23582012-08-18 08:54:3465 StoragePartitionImplMap* partition_map =
66 static_cast<StoragePartitionImplMap*>(
a.cavalcantiffab73762015-08-15 02:55:4867 browser_context->GetUserData(kStoragePartitionMapKeyName));
[email protected]d7c7c98a2012-07-12 21:27:4468 if (!partition_map) {
[email protected]4c3a23582012-08-18 08:54:3469 partition_map = new StoragePartitionImplMap(browser_context);
a.cavalcantiffab73762015-08-15 02:55:4870 browser_context->SetUserData(kStoragePartitionMapKeyName, partition_map);
[email protected]d7c7c98a2012-07-12 21:27:4471 }
[email protected]14acc642012-11-17 12:20:1072 return partition_map;
73}
74
75StoragePartition* GetStoragePartitionFromConfig(
76 BrowserContext* browser_context,
77 const std::string& partition_domain,
78 const std::string& partition_name,
79 bool in_memory) {
80 StoragePartitionImplMap* partition_map =
81 GetStoragePartitionMap(browser_context);
[email protected]d7c7c98a2012-07-12 21:27:4482
[email protected]1bc28312012-11-08 08:31:5383 if (browser_context->IsOffTheRecord())
84 in_memory = true;
85
[email protected]1bc28312012-11-08 08:31:5386 return partition_map->Get(partition_domain, partition_name, in_memory);
[email protected]d1198fd2012-08-13 22:50:1987}
88
[email protected]6939075a2012-08-28 08:35:5389void SaveSessionStateOnIOThread(
90 const scoped_refptr<net::URLRequestContextGetter>& context_getter,
[email protected]98d6d4562014-06-25 20:57:5591 AppCacheServiceImpl* appcache_service) {
[email protected]6939075a2012-08-28 08:35:5392 net::URLRequestContext* context = context_getter->GetURLRequestContext();
mmenkeded79da2016-02-06 08:28:5193 context->cookie_store()->SetForceKeepSessionState();
[email protected]6b8a3c742014-07-25 00:25:3594 context->channel_id_service()->GetChannelIDStore()->
[email protected]6939075a2012-08-28 08:35:5395 SetForceKeepSessionState();
96 appcache_service->set_force_keep_session_state();
[email protected]6e2d3d22012-02-24 18:10:3697}
98
[email protected]89acda82013-06-25 20:52:5099void SaveSessionStateOnIndexedDBThread(
[email protected]6e2d3d22012-02-24 18:10:36100 scoped_refptr<IndexedDBContextImpl> indexed_db_context) {
[email protected]bf510ed2012-06-05 08:31:43101 indexed_db_context->SetForceKeepSessionState();
[email protected]6e2d3d22012-02-24 18:10:36102}
103
falken41f4175162014-10-29 07:03:41104void ShutdownServiceWorkerContext(StoragePartition* partition) {
105 ServiceWorkerContextWrapper* wrapper =
106 static_cast<ServiceWorkerContextWrapper*>(
107 partition->GetServiceWorkerContext());
108 wrapper->process_manager()->Shutdown();
109}
110
ttr31481dc54b2015-08-06 20:11:26111void SetDownloadManager(BrowserContext* context,
112 content::DownloadManager* download_manager) {
113 DCHECK_CURRENTLY_ON(BrowserThread::UI);
114 DCHECK(download_manager);
115 context->SetUserData(kDownloadManagerKeyName, download_manager);
116}
117
[email protected]735e20c2012-03-20 01:16:59118} // namespace
119
[email protected]14acc642012-11-17 12:20:10120// static
121void BrowserContext::AsyncObliterateStoragePartition(
122 BrowserContext* browser_context,
[email protected]399583b2012-12-11 09:33:42123 const GURL& site,
124 const base::Closure& on_gc_required) {
125 GetStoragePartitionMap(browser_context)->AsyncObliterate(site,
126 on_gc_required);
127}
128
129// static
130void BrowserContext::GarbageCollectStoragePartitions(
131 BrowserContext* browser_context,
[email protected]2dec8ec2013-02-07 19:20:34132 scoped_ptr<base::hash_set<base::FilePath> > active_paths,
[email protected]399583b2012-12-11 09:33:42133 const base::Closure& done) {
dcheng36b6aec92015-12-26 06:16:36134 GetStoragePartitionMap(browser_context)
135 ->GarbageCollect(std::move(active_paths), done);
[email protected]14acc642012-11-17 12:20:10136}
137
[email protected]b441a8492012-06-06 14:55:57138DownloadManager* BrowserContext::GetDownloadManager(
139 BrowserContext* context) {
mostynbfbcdc27a2015-03-13 17:58:52140 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]b441a8492012-06-06 14:55:57141 if (!context->GetUserData(kDownloadManagerKeyName)) {
[email protected]eba4a4d2013-05-29 02:18:06142 DownloadManager* download_manager =
[email protected]d25fda12012-06-12 17:05:03143 new DownloadManagerImpl(
[email protected]16798692013-04-23 18:08:38144 GetContentClient()->browser()->GetNetLog(), context);
[email protected]d25fda12012-06-12 17:05:03145
ttr31481dc54b2015-08-06 20:11:26146 SetDownloadManager(context, download_manager);
[email protected]b441a8492012-06-06 14:55:57147 download_manager->SetDelegate(context->GetDownloadManagerDelegate());
[email protected]b441a8492012-06-06 14:55:57148 }
149
[email protected]eba4a4d2013-05-29 02:18:06150 return static_cast<DownloadManager*>(
151 context->GetUserData(kDownloadManagerKeyName));
[email protected]b441a8492012-06-06 14:55:57152}
153
[email protected]6ef0c3912013-01-25 22:46:34154// static
[email protected]cd501a72014-08-22 19:58:31155storage::ExternalMountPoints* BrowserContext::GetMountPoints(
[email protected]6ef0c3912013-01-25 22:46:34156 BrowserContext* context) {
157 // Ensure that these methods are called on the UI thread, except for
158 // unittests where a UI thread might not have been created.
159 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
160 !BrowserThread::IsMessageLoopValid(BrowserThread::UI));
161
162#if defined(OS_CHROMEOS)
163 if (!context->GetUserData(kMountPointsKey)) {
[email protected]cd501a72014-08-22 19:58:31164 scoped_refptr<storage::ExternalMountPoints> mount_points =
165 storage::ExternalMountPoints::CreateRefCounted();
[email protected]6ef0c3912013-01-25 22:46:34166 context->SetUserData(
167 kMountPointsKey,
[email protected]cd501a72014-08-22 19:58:31168 new UserDataAdapter<storage::ExternalMountPoints>(mount_points.get()));
[email protected]6ef0c3912013-01-25 22:46:34169 }
170
[email protected]cd501a72014-08-22 19:58:31171 return UserDataAdapter<storage::ExternalMountPoints>::Get(context,
172 kMountPointsKey);
[email protected]6ef0c3912013-01-25 22:46:34173#else
174 return NULL;
175#endif
176}
177
[email protected]4c3a23582012-08-18 08:54:34178StoragePartition* BrowserContext::GetStoragePartition(
179 BrowserContext* browser_context,
180 SiteInstance* site_instance) {
[email protected]1bc28312012-11-08 08:31:53181 std::string partition_domain;
182 std::string partition_name;
183 bool in_memory = false;
[email protected]4c3a23582012-08-18 08:54:34184
185 // TODO(ajwong): After GetDefaultStoragePartition() is removed, get rid of
186 // this conditional and require that |site_instance| is non-NULL.
187 if (site_instance) {
[email protected]1bc28312012-11-08 08:31:53188 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
[email protected]14acc642012-11-17 12:20:10189 browser_context, site_instance->GetSiteURL(), true,
[email protected]1bc28312012-11-08 08:31:53190 &partition_domain, &partition_name, &in_memory);
[email protected]4c3a23582012-08-18 08:54:34191 }
192
[email protected]1bc28312012-11-08 08:31:53193 return GetStoragePartitionFromConfig(
194 browser_context, partition_domain, partition_name, in_memory);
[email protected]4c3a23582012-08-18 08:54:34195}
196
[email protected]e94bbcb2012-09-07 05:33:57197StoragePartition* BrowserContext::GetStoragePartitionForSite(
198 BrowserContext* browser_context,
199 const GURL& site) {
[email protected]1bc28312012-11-08 08:31:53200 std::string partition_domain;
201 std::string partition_name;
202 bool in_memory;
[email protected]e94bbcb2012-09-07 05:33:57203
[email protected]1bc28312012-11-08 08:31:53204 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
[email protected]14acc642012-11-17 12:20:10205 browser_context, site, true, &partition_domain, &partition_name,
206 &in_memory);
[email protected]1bc28312012-11-08 08:31:53207
208 return GetStoragePartitionFromConfig(
209 browser_context, partition_domain, partition_name, in_memory);
[email protected]e94bbcb2012-09-07 05:33:57210}
211
[email protected]4c3a23582012-08-18 08:54:34212void BrowserContext::ForEachStoragePartition(
213 BrowserContext* browser_context,
214 const StoragePartitionCallback& callback) {
215 StoragePartitionImplMap* partition_map =
216 static_cast<StoragePartitionImplMap*>(
a.cavalcantiffab73762015-08-15 02:55:48217 browser_context->GetUserData(kStoragePartitionMapKeyName));
[email protected]4c3a23582012-08-18 08:54:34218 if (!partition_map)
219 return;
220
221 partition_map->ForEach(callback);
222}
223
224StoragePartition* BrowserContext::GetDefaultStoragePartition(
225 BrowserContext* browser_context) {
226 return GetStoragePartition(browser_context, NULL);
[email protected]55eb70e762012-02-20 17:38:39227}
228
tbarzicdb712682015-03-06 06:05:41229// static
[email protected]393b6cb2014-05-15 00:55:12230void BrowserContext::CreateMemoryBackedBlob(BrowserContext* browser_context,
231 const char* data, size_t length,
232 const BlobCallback& callback) {
mostynbfbcdc27a2015-03-13 17:58:52233 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]393b6cb2014-05-15 00:55:12234
235 ChromeBlobStorageContext* blob_context =
236 ChromeBlobStorageContext::GetFor(browser_context);
237 BrowserThread::PostTaskAndReplyWithResult(
238 BrowserThread::IO, FROM_HERE,
239 base::Bind(&ChromeBlobStorageContext::CreateMemoryBackedBlob,
240 make_scoped_refptr(blob_context), data, length),
241 callback);
242}
243
[email protected]66e53d0282014-08-07 10:04:35244// static
tbarzicdb712682015-03-06 06:05:41245void BrowserContext::CreateFileBackedBlob(
246 BrowserContext* browser_context,
247 const base::FilePath& path,
248 int64_t offset,
249 int64_t size,
250 const base::Time& expected_modification_time,
251 const BlobCallback& callback) {
mostynbfbcdc27a2015-03-13 17:58:52252 DCHECK_CURRENTLY_ON(BrowserThread::UI);
tbarzicdb712682015-03-06 06:05:41253
254 ChromeBlobStorageContext* blob_context =
255 ChromeBlobStorageContext::GetFor(browser_context);
256 BrowserThread::PostTaskAndReplyWithResult(
257 BrowserThread::IO, FROM_HERE,
258 base::Bind(&ChromeBlobStorageContext::CreateFileBackedBlob,
259 make_scoped_refptr(blob_context), path, offset, size,
260 expected_modification_time),
261 callback);
262}
263
264// static
[email protected]66e53d0282014-08-07 10:04:35265void BrowserContext::DeliverPushMessage(
266 BrowserContext* browser_context,
267 const GURL& origin,
avib7348942015-12-25 20:57:10268 int64_t service_worker_registration_id,
harknessdd4d2b22016-01-27 19:26:43269 const PushEventPayload& payload,
johnmea80c2552014-10-17 14:51:40270 const base::Callback<void(PushDeliveryStatus)>& callback) {
mostynbfbcdc27a2015-03-13 17:58:52271 DCHECK_CURRENTLY_ON(BrowserThread::UI);
harknessdd4d2b22016-01-27 19:26:43272 PushMessagingRouter::DeliverMessage(browser_context, origin,
273 service_worker_registration_id, payload,
274 callback);
[email protected]66e53d0282014-08-07 10:04:35275}
276
falken41f4175162014-10-29 07:03:41277// static
278void BrowserContext::NotifyWillBeDestroyed(BrowserContext* browser_context) {
279 // Service Workers must shutdown before the browser context is destroyed,
280 // since they keep render process hosts alive and the codebase assumes that
281 // render process hosts die before their profile (browser context) dies.
282 ForEachStoragePartition(browser_context,
283 base::Bind(ShutdownServiceWorkerContext));
284}
285
[email protected]314c3e22012-02-21 03:57:42286void BrowserContext::EnsureResourceContextInitialized(BrowserContext* context) {
[email protected]7e26ac92012-02-27 20:15:05287 // This will be enough to tickle initialization of BrowserContext if
288 // necessary, which initializes ResourceContext. The reason we don't call
[email protected]4c3a23582012-08-18 08:54:34289 // ResourceContext::InitializeResourceContext() directly here is that
290 // ResourceContext initialization may call back into BrowserContext
291 // and when that call returns it'll end rewriting its UserData map. It will
292 // end up rewriting the same value but this still causes a race condition.
293 //
294 // See http://crbug.com/115678.
295 GetDefaultStoragePartition(context);
[email protected]55eb70e762012-02-20 17:38:39296}
297
[email protected]6e2d3d22012-02-24 18:10:36298void BrowserContext::SaveSessionState(BrowserContext* browser_context) {
[email protected]5c8e67c2012-08-29 00:48:52299 GetDefaultStoragePartition(browser_context)->GetDatabaseTracker()->
300 SetForceKeepSessionState();
[email protected]b1b502e2012-09-16 07:31:43301 StoragePartition* storage_partition =
302 BrowserContext::GetDefaultStoragePartition(browser_context);
[email protected]6e2d3d22012-02-24 18:10:36303
304 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) {
305 BrowserThread::PostTask(
306 BrowserThread::IO, FROM_HERE,
[email protected]6939075a2012-08-28 08:35:53307 base::Bind(
308 &SaveSessionStateOnIOThread,
309 make_scoped_refptr(browser_context->GetRequestContext()),
[email protected]98d6d4562014-06-25 20:57:55310 static_cast<AppCacheServiceImpl*>(
[email protected]63ef85512014-06-05 14:21:26311 storage_partition->GetAppCacheService())));
[email protected]6e2d3d22012-02-24 18:10:36312 }
313
[email protected]5f2aa722013-08-07 16:59:41314 DOMStorageContextWrapper* dom_storage_context_proxy =
315 static_cast<DOMStorageContextWrapper*>(
[email protected]b1b502e2012-09-16 07:31:43316 storage_partition->GetDOMStorageContext());
[email protected]5f2aa722013-08-07 16:59:41317 dom_storage_context_proxy->SetForceKeepSessionState();
[email protected]735e20c2012-03-20 01:16:59318
[email protected]89acda82013-06-25 20:52:50319 IndexedDBContextImpl* indexed_db_context_impl =
320 static_cast<IndexedDBContextImpl*>(
[email protected]b1b502e2012-09-16 07:31:43321 storage_partition->GetIndexedDBContext());
[email protected]89acda82013-06-25 20:52:50322 // No task runner in unit tests.
323 if (indexed_db_context_impl->TaskRunner()) {
324 indexed_db_context_impl->TaskRunner()->PostTask(
325 FROM_HERE,
326 base::Bind(&SaveSessionStateOnIndexedDBThread,
327 make_scoped_refptr(indexed_db_context_impl)));
[email protected]6e2d3d22012-02-24 18:10:36328 }
329}
330
ttr31481dc54b2015-08-06 20:11:26331void BrowserContext::SetDownloadManagerForTesting(
332 BrowserContext* browser_context,
333 DownloadManager* download_manager) {
334 SetDownloadManager(browser_context, download_manager);
335}
336
erge69130f52016-03-02 00:13:28337void BrowserContext::Initialize(
338 BrowserContext* browser_context,
339 const base::FilePath& path) {
bencccfe2a2016-03-05 16:54:14340 // Generate a GUID for |browser_context| to use as the Mojo user id.
341 std::string new_id = base::GenerateGUID();
342 while (g_used_user_ids.Get().find(new_id) != g_used_user_ids.Get().end())
343 new_id = base::GenerateGUID();
erge69130f52016-03-02 00:13:28344
345 g_used_user_ids.Get().insert(new_id);
346 g_context_to_user_id.Get().push_back(std::make_pair(browser_context, new_id));
347
348 profile::ProfileApp::AssociateMojoUserIDWithProfileDir(new_id, path);
349 browser_context->SetUserData(kMojoWasInitialized,
350 new base::SupportsUserData::Data);
351}
352
bencccfe2a2016-03-05 16:54:14353const std::string& BrowserContext::GetMojoUserIdFor(
354 BrowserContext* browser_context) {
erge69130f52016-03-02 00:13:28355 CHECK(browser_context->GetUserData(kMojoWasInitialized))
356 << "Attempting to get the mojo user id for a BrowserContext that was "
357 << "never Initialize()ed.";
358
359 auto it = std::find_if(
360 g_context_to_user_id.Get().begin(),
361 g_context_to_user_id.Get().end(),
bencccfe2a2016-03-05 16:54:14362 [&browser_context](const std::pair<BrowserContext*, std::string>& p) {
erge69130f52016-03-02 00:13:28363 return p.first == browser_context; });
364 CHECK(it != g_context_to_user_id.Get().end());
365 return it->second;
366}
367
[email protected]e0ce8a1e2012-09-18 10:26:36368#endif // !OS_IOS
[email protected]6e2d3d22012-02-24 18:10:36369
[email protected]55eb70e762012-02-20 17:38:39370BrowserContext::~BrowserContext() {
erge69130f52016-03-02 00:13:28371 CHECK(GetUserData(kMojoWasInitialized))
372 << "Attempting to destroy a BrowserContext that never called "
373 << "Initialize()";
374
[email protected]e0ce8a1e2012-09-18 10:26:36375#if !defined(OS_IOS)
[email protected]b441a8492012-06-06 14:55:57376 if (GetUserData(kDownloadManagerKeyName))
377 GetDownloadManager(this)->Shutdown();
[email protected]e0ce8a1e2012-09-18 10:26:36378#endif
[email protected]55eb70e762012-02-20 17:38:39379}
380
[email protected]55eb70e762012-02-20 17:38:39381} // namespace content