blob: 4330c116c0057041f606df321d828589680d1a02 [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>
avi1ed4a4372017-04-25 05:39:419
erge69130f52016-03-02 00:13:2810#include <algorithm>
11#include <limits>
rockot963ad3e82016-04-13 16:33:2312#include <memory>
dcheng36b6aec92015-12-26 06:16:3613#include <utility>
rockot963ad3e82016-04-13 16:33:2314#include <vector>
avib7348942015-12-25 20:57:1015
mmenkec0b2b8b12017-04-21 16:27:5216#include "base/base64.h"
rockot963ad3e82016-04-13 16:33:2317#include "base/command_line.h"
bencccfe2a2016-03-05 16:54:1418#include "base/guid.h"
erge69130f52016-03-02 00:13:2819#include "base/lazy_instance.h"
mmenkec0b2b8b12017-04-21 16:27:5220#include "base/logging.h"
rockot963ad3e82016-04-13 16:33:2321#include "base/macros.h"
avi1ed4a4372017-04-25 05:39:4122#include "base/memory/ptr_util.h"
erge69130f52016-03-02 00:13:2823#include "base/rand_util.h"
mek17399e672017-05-09 03:51:2024#include "base/task_scheduler/post_task.h"
ben21a34c252016-06-29 22:24:3725#include "base/threading/thread_task_runner_handle.h"
avib7348942015-12-25 20:57:1026#include "build/build_config.h"
dmurph7ac019a2016-05-13 00:13:1727#include "content/browser/blob_storage/chrome_blob_storage_context.h"
msrameke169ccb2017-04-26 05:21:4128#include "content/browser/browsing_data/browsing_data_remover_impl.h"
[email protected]b441a8492012-06-06 14:55:5729#include "content/browser/download/download_manager_impl.h"
[email protected]c4d281662013-03-31 00:35:0830#include "content/browser/indexed_db/indexed_db_context_impl.h"
[email protected]678c0362012-12-05 08:02:4431#include "content/browser/loader/resource_dispatcher_host_impl.h"
mvanouwerkerk17205ea2014-11-07 17:30:1532#include "content/browser/push_messaging/push_messaging_router.h"
Ken Rockotc7a279c2017-05-04 23:51:4533#include "content/browser/service_manager/common_browser_interfaces.h"
[email protected]4c3a23582012-08-18 08:54:3434#include "content/browser/storage_partition_impl_map.h"
[email protected]d7c7c98a2012-07-12 21:27:4435#include "content/common/child_process_host_impl.h"
[email protected]393b6cb2014-05-15 00:55:1236#include "content/public/browser/blob_handle.h"
[email protected]55eb70e762012-02-20 17:38:3937#include "content/public/browser/browser_thread.h"
[email protected]b441a8492012-06-06 14:55:5738#include "content/public/browser/content_browser_client.h"
falken04a6912a2016-09-23 21:06:2939#include "content/public/browser/render_process_host.h"
[email protected]536fd0b2013-03-14 17:41:5740#include "content/public/browser/site_instance.h"
rockot963ad3e82016-04-13 16:33:2341#include "content/public/common/content_switches.h"
bend32292b2016-10-07 00:21:5842#include "content/public/common/service_manager_connection.h"
ben51bb6c62016-11-17 20:15:5743#include "content/public/common/service_names.mojom.h"
[email protected]4d7c4ef2012-03-16 01:47:1244#include "net/cookies/cookie_store.h"
[email protected]6b8a3c742014-07-25 00:25:3545#include "net/ssl/channel_id_service.h"
46#include "net/ssl/channel_id_store.h"
[email protected]6e2d3d22012-02-24 18:10:3647#include "net/url_request/url_request_context.h"
[email protected]6939075a2012-08-28 08:35:5348#include "net/url_request/url_request_context_getter.h"
ben768c8dc2016-08-12 00:26:5049#include "services/file/file_service.h"
benaabad0b2016-11-16 23:54:1750#include "services/file/public/interfaces/constants.mojom.h"
ben768c8dc2016-08-12 00:26:5051#include "services/file/user_id_map.h"
rockot734fb662016-10-15 16:41:3052#include "services/service_manager/public/cpp/connector.h"
53#include "services/service_manager/public/interfaces/service.mojom.h"
pilgrime92c5fcd2014-09-10 23:31:2354#include "storage/browser/database/database_tracker.h"
55#include "storage/browser/fileapi/external_mount_points.h"
[email protected]55eb70e762012-02-20 17:38:3956
[email protected]314c3e22012-02-21 03:57:4257using base::UserDataAdapter;
[email protected]55eb70e762012-02-20 17:38:3958
[email protected]55eb70e762012-02-20 17:38:3959namespace content {
60
[email protected]735e20c2012-03-20 01:16:5961namespace {
62
scottmg5e65e3a2017-03-08 08:48:4663base::LazyInstance<std::map<std::string, BrowserContext*>>::DestructorAtExit
ben6c85c4492016-06-16 20:40:5164 g_user_id_to_context = LAZY_INSTANCE_INITIALIZER;
65
bend32292b2016-10-07 00:21:5866class ServiceUserIdHolder : public base::SupportsUserData::Data {
ben6c85c4492016-06-16 20:40:5167 public:
bend32292b2016-10-07 00:21:5868 explicit ServiceUserIdHolder(const std::string& user_id)
69 : user_id_(user_id) {}
70 ~ServiceUserIdHolder() override {}
ben6c85c4492016-06-16 20:40:5171
72 const std::string& user_id() const { return user_id_; }
73
74 private:
75 std::string user_id_;
76
bend32292b2016-10-07 00:21:5877 DISALLOW_COPY_AND_ASSIGN(ServiceUserIdHolder);
ben6c85c4492016-06-16 20:40:5178};
erge69130f52016-03-02 00:13:2879
[email protected]e0ce8a1e2012-09-18 10:26:3680// Key names on BrowserContext.
msrameke169ccb2017-04-26 05:21:4181const char kBrowsingDataRemoverKey[] = "browsing-data-remover";
[email protected]6ef0c3912013-01-25 22:46:3482const char kDownloadManagerKeyName[] = "download_manager";
erge69130f52016-03-02 00:13:2883const char kMojoWasInitialized[] = "mojo-was-initialized";
bend32292b2016-10-07 00:21:5884const char kServiceManagerConnection[] = "service-manager-connection";
85const char kServiceUserId[] = "service-user-id";
rockot963ad3e82016-04-13 16:33:2386const char kStoragePartitionMapKeyName[] = "content_storage_partition_map";
erge69130f52016-03-02 00:13:2887
[email protected]9afc14e22013-09-25 22:34:1488#if defined(OS_CHROMEOS)
89const char kMountPointsKey[] = "mount_points";
90#endif // defined(OS_CHROMEOS)
91
ben6c85c4492016-06-16 20:40:5192void RemoveBrowserContextFromUserIdMap(BrowserContext* browser_context) {
bend32292b2016-10-07 00:21:5893 ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>(
94 browser_context->GetUserData(kServiceUserId));
ben6c85c4492016-06-16 20:40:5195 if (holder) {
96 auto it = g_user_id_to_context.Get().find(holder->user_id());
97 if (it != g_user_id_to_context.Get().end())
98 g_user_id_to_context.Get().erase(it);
99 }
100}
101
[email protected]14acc642012-11-17 12:20:10102StoragePartitionImplMap* GetStoragePartitionMap(
103 BrowserContext* browser_context) {
[email protected]4c3a23582012-08-18 08:54:34104 StoragePartitionImplMap* partition_map =
105 static_cast<StoragePartitionImplMap*>(
a.cavalcantiffab73762015-08-15 02:55:48106 browser_context->GetUserData(kStoragePartitionMapKeyName));
[email protected]d7c7c98a2012-07-12 21:27:44107 if (!partition_map) {
avi1ed4a4372017-04-25 05:39:41108 auto partition_map_owned =
109 base::MakeUnique<StoragePartitionImplMap>(browser_context);
110 partition_map = partition_map_owned.get();
111 browser_context->SetUserData(kStoragePartitionMapKeyName,
112 std::move(partition_map_owned));
[email protected]d7c7c98a2012-07-12 21:27:44113 }
[email protected]14acc642012-11-17 12:20:10114 return partition_map;
115}
116
117StoragePartition* GetStoragePartitionFromConfig(
118 BrowserContext* browser_context,
119 const std::string& partition_domain,
120 const std::string& partition_name,
121 bool in_memory) {
122 StoragePartitionImplMap* partition_map =
123 GetStoragePartitionMap(browser_context);
[email protected]d7c7c98a2012-07-12 21:27:44124
[email protected]1bc28312012-11-08 08:31:53125 if (browser_context->IsOffTheRecord())
126 in_memory = true;
127
[email protected]1bc28312012-11-08 08:31:53128 return partition_map->Get(partition_domain, partition_name, in_memory);
[email protected]d1198fd2012-08-13 22:50:19129}
130
[email protected]6939075a2012-08-28 08:35:53131void SaveSessionStateOnIOThread(
132 const scoped_refptr<net::URLRequestContextGetter>& context_getter,
[email protected]98d6d4562014-06-25 20:57:55133 AppCacheServiceImpl* appcache_service) {
[email protected]6939075a2012-08-28 08:35:53134 net::URLRequestContext* context = context_getter->GetURLRequestContext();
mmenkeded79da2016-02-06 08:28:51135 context->cookie_store()->SetForceKeepSessionState();
[email protected]6b8a3c742014-07-25 00:25:35136 context->channel_id_service()->GetChannelIDStore()->
[email protected]6939075a2012-08-28 08:35:53137 SetForceKeepSessionState();
138 appcache_service->set_force_keep_session_state();
[email protected]6e2d3d22012-02-24 18:10:36139}
140
[email protected]89acda82013-06-25 20:52:50141void SaveSessionStateOnIndexedDBThread(
[email protected]6e2d3d22012-02-24 18:10:36142 scoped_refptr<IndexedDBContextImpl> indexed_db_context) {
[email protected]bf510ed2012-06-05 08:31:43143 indexed_db_context->SetForceKeepSessionState();
[email protected]6e2d3d22012-02-24 18:10:36144}
145
falken41f4175162014-10-29 07:03:41146void ShutdownServiceWorkerContext(StoragePartition* partition) {
147 ServiceWorkerContextWrapper* wrapper =
148 static_cast<ServiceWorkerContextWrapper*>(
149 partition->GetServiceWorkerContext());
150 wrapper->process_manager()->Shutdown();
151}
152
avi1ed4a4372017-04-25 05:39:41153void SetDownloadManager(
154 BrowserContext* context,
155 std::unique_ptr<content::DownloadManager> download_manager) {
ttr31481dc54b2015-08-06 20:11:26156 DCHECK_CURRENTLY_ON(BrowserThread::UI);
157 DCHECK(download_manager);
avi1ed4a4372017-04-25 05:39:41158 context->SetUserData(kDownloadManagerKeyName, std::move(download_manager));
ttr31481dc54b2015-08-06 20:11:26159}
160
bend32292b2016-10-07 00:21:58161class BrowserContextServiceManagerConnectionHolder
rockot963ad3e82016-04-13 16:33:23162 : public base::SupportsUserData::Data {
163 public:
benaad37ecd2017-04-12 22:08:20164 explicit BrowserContextServiceManagerConnectionHolder(
rockot400ea35b2016-10-15 19:15:32165 service_manager::mojom::ServiceRequest request)
benaad37ecd2017-04-12 22:08:20166 : service_manager_connection_(ServiceManagerConnection::Create(
rockotcef38272016-07-15 22:47:47167 std::move(request),
168 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO))) {}
bend32292b2016-10-07 00:21:58169 ~BrowserContextServiceManagerConnectionHolder() override {}
rockot963ad3e82016-04-13 16:33:23170
bend32292b2016-10-07 00:21:58171 ServiceManagerConnection* service_manager_connection() {
172 return service_manager_connection_.get();
173 }
rockot963ad3e82016-04-13 16:33:23174
175 private:
bend32292b2016-10-07 00:21:58176 std::unique_ptr<ServiceManagerConnection> service_manager_connection_;
rockot963ad3e82016-04-13 16:33:23177
bend32292b2016-10-07 00:21:58178 DISALLOW_COPY_AND_ASSIGN(BrowserContextServiceManagerConnectionHolder);
rockot963ad3e82016-04-13 16:33:23179};
180
[email protected]735e20c2012-03-20 01:16:59181} // namespace
182
[email protected]14acc642012-11-17 12:20:10183// static
184void BrowserContext::AsyncObliterateStoragePartition(
185 BrowserContext* browser_context,
[email protected]399583b2012-12-11 09:33:42186 const GURL& site,
187 const base::Closure& on_gc_required) {
188 GetStoragePartitionMap(browser_context)->AsyncObliterate(site,
189 on_gc_required);
190}
191
192// static
193void BrowserContext::GarbageCollectStoragePartitions(
dcheng59716272016-04-09 05:19:08194 BrowserContext* browser_context,
195 std::unique_ptr<base::hash_set<base::FilePath>> active_paths,
196 const base::Closure& done) {
dcheng36b6aec92015-12-26 06:16:36197 GetStoragePartitionMap(browser_context)
198 ->GarbageCollect(std::move(active_paths), done);
[email protected]14acc642012-11-17 12:20:10199}
200
[email protected]b441a8492012-06-06 14:55:57201DownloadManager* BrowserContext::GetDownloadManager(
202 BrowserContext* context) {
mostynbfbcdc27a2015-03-13 17:58:52203 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]b441a8492012-06-06 14:55:57204 if (!context->GetUserData(kDownloadManagerKeyName)) {
[email protected]eba4a4d2013-05-29 02:18:06205 DownloadManager* download_manager =
[email protected]d25fda12012-06-12 17:05:03206 new DownloadManagerImpl(
[email protected]16798692013-04-23 18:08:38207 GetContentClient()->browser()->GetNetLog(), context);
[email protected]d25fda12012-06-12 17:05:03208
avi1ed4a4372017-04-25 05:39:41209 SetDownloadManager(context, base::WrapUnique(download_manager));
[email protected]b441a8492012-06-06 14:55:57210 download_manager->SetDelegate(context->GetDownloadManagerDelegate());
[email protected]b441a8492012-06-06 14:55:57211 }
212
[email protected]eba4a4d2013-05-29 02:18:06213 return static_cast<DownloadManager*>(
214 context->GetUserData(kDownloadManagerKeyName));
[email protected]b441a8492012-06-06 14:55:57215}
216
[email protected]6ef0c3912013-01-25 22:46:34217// static
[email protected]cd501a72014-08-22 19:58:31218storage::ExternalMountPoints* BrowserContext::GetMountPoints(
[email protected]6ef0c3912013-01-25 22:46:34219 BrowserContext* context) {
220 // Ensure that these methods are called on the UI thread, except for
221 // unittests where a UI thread might not have been created.
222 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
223 !BrowserThread::IsMessageLoopValid(BrowserThread::UI));
224
225#if defined(OS_CHROMEOS)
226 if (!context->GetUserData(kMountPointsKey)) {
[email protected]cd501a72014-08-22 19:58:31227 scoped_refptr<storage::ExternalMountPoints> mount_points =
228 storage::ExternalMountPoints::CreateRefCounted();
[email protected]6ef0c3912013-01-25 22:46:34229 context->SetUserData(
230 kMountPointsKey,
avicb129c02017-05-03 06:49:29231 base::MakeUnique<UserDataAdapter<storage::ExternalMountPoints>>(
232 mount_points.get()));
[email protected]6ef0c3912013-01-25 22:46:34233 }
234
[email protected]cd501a72014-08-22 19:58:31235 return UserDataAdapter<storage::ExternalMountPoints>::Get(context,
236 kMountPointsKey);
[email protected]6ef0c3912013-01-25 22:46:34237#else
238 return NULL;
239#endif
240}
241
msrameke169ccb2017-04-26 05:21:41242// static
243content::BrowsingDataRemover* content::BrowserContext::GetBrowsingDataRemover(
244 BrowserContext* context) {
245 DCHECK_CURRENTLY_ON(BrowserThread::UI);
246
247 if (!context->GetUserData(kBrowsingDataRemoverKey)) {
248 std::unique_ptr<BrowsingDataRemoverImpl> remover =
249 base::MakeUnique<BrowsingDataRemoverImpl>(context);
250 remover->SetEmbedderDelegate(context->GetBrowsingDataRemoverDelegate());
251 context->SetUserData(kBrowsingDataRemoverKey, std::move(remover));
252 }
253
254 return static_cast<BrowsingDataRemoverImpl*>(
255 context->GetUserData(kBrowsingDataRemoverKey));
256}
257
[email protected]4c3a23582012-08-18 08:54:34258StoragePartition* BrowserContext::GetStoragePartition(
259 BrowserContext* browser_context,
260 SiteInstance* site_instance) {
[email protected]1bc28312012-11-08 08:31:53261 std::string partition_domain;
262 std::string partition_name;
263 bool in_memory = false;
[email protected]4c3a23582012-08-18 08:54:34264
[email protected]4c3a23582012-08-18 08:54:34265 if (site_instance) {
[email protected]1bc28312012-11-08 08:31:53266 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
[email protected]14acc642012-11-17 12:20:10267 browser_context, site_instance->GetSiteURL(), true,
[email protected]1bc28312012-11-08 08:31:53268 &partition_domain, &partition_name, &in_memory);
[email protected]4c3a23582012-08-18 08:54:34269 }
270
[email protected]1bc28312012-11-08 08:31:53271 return GetStoragePartitionFromConfig(
272 browser_context, partition_domain, partition_name, in_memory);
[email protected]4c3a23582012-08-18 08:54:34273}
274
[email protected]e94bbcb2012-09-07 05:33:57275StoragePartition* BrowserContext::GetStoragePartitionForSite(
276 BrowserContext* browser_context,
277 const GURL& site) {
[email protected]1bc28312012-11-08 08:31:53278 std::string partition_domain;
279 std::string partition_name;
280 bool in_memory;
[email protected]e94bbcb2012-09-07 05:33:57281
[email protected]1bc28312012-11-08 08:31:53282 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
[email protected]14acc642012-11-17 12:20:10283 browser_context, site, true, &partition_domain, &partition_name,
284 &in_memory);
[email protected]1bc28312012-11-08 08:31:53285
286 return GetStoragePartitionFromConfig(
287 browser_context, partition_domain, partition_name, in_memory);
[email protected]e94bbcb2012-09-07 05:33:57288}
289
[email protected]4c3a23582012-08-18 08:54:34290void BrowserContext::ForEachStoragePartition(
291 BrowserContext* browser_context,
292 const StoragePartitionCallback& callback) {
293 StoragePartitionImplMap* partition_map =
294 static_cast<StoragePartitionImplMap*>(
a.cavalcantiffab73762015-08-15 02:55:48295 browser_context->GetUserData(kStoragePartitionMapKeyName));
[email protected]4c3a23582012-08-18 08:54:34296 if (!partition_map)
297 return;
298
299 partition_map->ForEach(callback);
300}
301
302StoragePartition* BrowserContext::GetDefaultStoragePartition(
303 BrowserContext* browser_context) {
304 return GetStoragePartition(browser_context, NULL);
[email protected]55eb70e762012-02-20 17:38:39305}
306
tbarzicdb712682015-03-06 06:05:41307// static
[email protected]393b6cb2014-05-15 00:55:12308void BrowserContext::CreateMemoryBackedBlob(BrowserContext* browser_context,
309 const char* data, size_t length,
310 const BlobCallback& callback) {
mostynbfbcdc27a2015-03-13 17:58:52311 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]393b6cb2014-05-15 00:55:12312
313 ChromeBlobStorageContext* blob_context =
314 ChromeBlobStorageContext::GetFor(browser_context);
315 BrowserThread::PostTaskAndReplyWithResult(
316 BrowserThread::IO, FROM_HERE,
317 base::Bind(&ChromeBlobStorageContext::CreateMemoryBackedBlob,
318 make_scoped_refptr(blob_context), data, length),
319 callback);
320}
321
[email protected]66e53d0282014-08-07 10:04:35322// static
tbarzicdb712682015-03-06 06:05:41323void BrowserContext::CreateFileBackedBlob(
324 BrowserContext* browser_context,
325 const base::FilePath& path,
326 int64_t offset,
327 int64_t size,
328 const base::Time& expected_modification_time,
329 const BlobCallback& callback) {
mostynbfbcdc27a2015-03-13 17:58:52330 DCHECK_CURRENTLY_ON(BrowserThread::UI);
tbarzicdb712682015-03-06 06:05:41331
332 ChromeBlobStorageContext* blob_context =
333 ChromeBlobStorageContext::GetFor(browser_context);
334 BrowserThread::PostTaskAndReplyWithResult(
335 BrowserThread::IO, FROM_HERE,
336 base::Bind(&ChromeBlobStorageContext::CreateFileBackedBlob,
337 make_scoped_refptr(blob_context), path, offset, size,
338 expected_modification_time),
339 callback);
340}
341
342// static
[email protected]66e53d0282014-08-07 10:04:35343void BrowserContext::DeliverPushMessage(
344 BrowserContext* browser_context,
345 const GURL& origin,
avib7348942015-12-25 20:57:10346 int64_t service_worker_registration_id,
harknessdd4d2b22016-01-27 19:26:43347 const PushEventPayload& payload,
johnmea80c2552014-10-17 14:51:40348 const base::Callback<void(PushDeliveryStatus)>& callback) {
mostynbfbcdc27a2015-03-13 17:58:52349 DCHECK_CURRENTLY_ON(BrowserThread::UI);
harknessdd4d2b22016-01-27 19:26:43350 PushMessagingRouter::DeliverMessage(browser_context, origin,
351 service_worker_registration_id, payload,
352 callback);
[email protected]66e53d0282014-08-07 10:04:35353}
354
falken41f4175162014-10-29 07:03:41355// static
356void BrowserContext::NotifyWillBeDestroyed(BrowserContext* browser_context) {
357 // Service Workers must shutdown before the browser context is destroyed,
358 // since they keep render process hosts alive and the codebase assumes that
359 // render process hosts die before their profile (browser context) dies.
360 ForEachStoragePartition(browser_context,
361 base::Bind(ShutdownServiceWorkerContext));
falken04a6912a2016-09-23 21:06:29362
363 // Shared workers also keep render process hosts alive, and are expected to
364 // return ref counts to 0 after documents close. However, shared worker
365 // bookkeeping is done on the IO thread and we want to ensure the hosts are
366 // destructed now, so forcibly release their ref counts here.
367 for (RenderProcessHost::iterator host_iterator =
368 RenderProcessHost::AllHostsIterator();
369 !host_iterator.IsAtEnd(); host_iterator.Advance()) {
370 RenderProcessHost* host = host_iterator.GetCurrentValue();
371 if (host->GetBrowserContext() == browser_context)
372 host->ForceReleaseWorkerRefCounts();
373 }
falken41f4175162014-10-29 07:03:41374}
375
[email protected]314c3e22012-02-21 03:57:42376void BrowserContext::EnsureResourceContextInitialized(BrowserContext* context) {
[email protected]7e26ac92012-02-27 20:15:05377 // This will be enough to tickle initialization of BrowserContext if
378 // necessary, which initializes ResourceContext. The reason we don't call
[email protected]4c3a23582012-08-18 08:54:34379 // ResourceContext::InitializeResourceContext() directly here is that
380 // ResourceContext initialization may call back into BrowserContext
381 // and when that call returns it'll end rewriting its UserData map. It will
382 // end up rewriting the same value but this still causes a race condition.
383 //
384 // See http://crbug.com/115678.
385 GetDefaultStoragePartition(context);
[email protected]55eb70e762012-02-20 17:38:39386}
387
[email protected]6e2d3d22012-02-24 18:10:36388void BrowserContext::SaveSessionState(BrowserContext* browser_context) {
[email protected]5c8e67c2012-08-29 00:48:52389 GetDefaultStoragePartition(browser_context)->GetDatabaseTracker()->
390 SetForceKeepSessionState();
[email protected]b1b502e2012-09-16 07:31:43391 StoragePartition* storage_partition =
392 BrowserContext::GetDefaultStoragePartition(browser_context);
[email protected]6e2d3d22012-02-24 18:10:36393
394 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) {
395 BrowserThread::PostTask(
396 BrowserThread::IO, FROM_HERE,
[email protected]6939075a2012-08-28 08:35:53397 base::Bind(
398 &SaveSessionStateOnIOThread,
jamb84299e2016-04-12 16:58:59399 make_scoped_refptr(BrowserContext::GetDefaultStoragePartition(
400 browser_context)->GetURLRequestContext()),
[email protected]98d6d4562014-06-25 20:57:55401 static_cast<AppCacheServiceImpl*>(
[email protected]63ef85512014-06-05 14:21:26402 storage_partition->GetAppCacheService())));
[email protected]6e2d3d22012-02-24 18:10:36403 }
404
[email protected]5f2aa722013-08-07 16:59:41405 DOMStorageContextWrapper* dom_storage_context_proxy =
406 static_cast<DOMStorageContextWrapper*>(
[email protected]b1b502e2012-09-16 07:31:43407 storage_partition->GetDOMStorageContext());
[email protected]5f2aa722013-08-07 16:59:41408 dom_storage_context_proxy->SetForceKeepSessionState();
[email protected]735e20c2012-03-20 01:16:59409
[email protected]89acda82013-06-25 20:52:50410 IndexedDBContextImpl* indexed_db_context_impl =
411 static_cast<IndexedDBContextImpl*>(
[email protected]b1b502e2012-09-16 07:31:43412 storage_partition->GetIndexedDBContext());
[email protected]89acda82013-06-25 20:52:50413 // No task runner in unit tests.
414 if (indexed_db_context_impl->TaskRunner()) {
415 indexed_db_context_impl->TaskRunner()->PostTask(
416 FROM_HERE,
417 base::Bind(&SaveSessionStateOnIndexedDBThread,
418 make_scoped_refptr(indexed_db_context_impl)));
[email protected]6e2d3d22012-02-24 18:10:36419 }
420}
421
ttr31481dc54b2015-08-06 20:11:26422void BrowserContext::SetDownloadManagerForTesting(
423 BrowserContext* browser_context,
avi1ed4a4372017-04-25 05:39:41424 std::unique_ptr<content::DownloadManager> download_manager) {
425 SetDownloadManager(browser_context, std::move(download_manager));
ttr31481dc54b2015-08-06 20:11:26426}
427
rockot963ad3e82016-04-13 16:33:23428// static
erge69130f52016-03-02 00:13:28429void BrowserContext::Initialize(
430 BrowserContext* browser_context,
431 const base::FilePath& path) {
ben6c85c4492016-06-16 20:40:51432
433 std::string new_id;
434 if (GetContentClient() && GetContentClient()->browser()) {
bend32292b2016-10-07 00:21:58435 new_id = GetContentClient()->browser()->GetServiceUserIdForBrowserContext(
ben6c85c4492016-06-16 20:40:51436 browser_context);
437 } else {
438 // Some test scenarios initialize a BrowserContext without a content client.
bencccfe2a2016-03-05 16:54:14439 new_id = base::GenerateGUID();
ben6c85c4492016-06-16 20:40:51440 }
erge69130f52016-03-02 00:13:28441
bend32292b2016-10-07 00:21:58442 ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>(
443 browser_context->GetUserData(kServiceUserId));
ben6c85c4492016-06-16 20:40:51444 if (holder)
bend32292b2016-10-07 00:21:58445 file::ForgetServiceUserIdUserDirAssociation(holder->user_id());
446 file::AssociateServiceUserIdWithUserDir(new_id, path);
ben6c85c4492016-06-16 20:40:51447 RemoveBrowserContextFromUserIdMap(browser_context);
448 g_user_id_to_context.Get()[new_id] = browser_context;
bend32292b2016-10-07 00:21:58449 browser_context->SetUserData(kServiceUserId,
avi1ed4a4372017-04-25 05:39:41450 base::MakeUnique<ServiceUserIdHolder>(new_id));
erge69130f52016-03-02 00:13:28451
avi1ed4a4372017-04-25 05:39:41452 browser_context->SetUserData(
453 kMojoWasInitialized, base::MakeUnique<base::SupportsUserData::Data>());
rockot963ad3e82016-04-13 16:33:23454
bend32292b2016-10-07 00:21:58455 ServiceManagerConnection* service_manager_connection =
456 ServiceManagerConnection::GetForProcess();
fdoray4f155f02016-10-12 11:28:50457 if (service_manager_connection && base::ThreadTaskRunnerHandle::IsSet()) {
rockot963ad3e82016-04-13 16:33:23458 // NOTE: Many unit tests create a TestBrowserContext without initializing
bend32292b2016-10-07 00:21:58459 // Mojo or the global service manager connection.
rockot963ad3e82016-04-13 16:33:23460
rockot400ea35b2016-10-15 19:15:32461 service_manager::mojom::ServicePtr service;
Ken Rockotf4d8a942017-05-13 00:10:37462 auto service_request = mojo::MakeRequest(&service);
rockot963ad3e82016-04-13 16:33:23463
rockot400ea35b2016-10-15 19:15:32464 service_manager::mojom::PIDReceiverPtr pid_receiver;
bene6a9f012017-01-07 00:43:43465 service_manager::Identity identity(mojom::kBrowserServiceName, new_id);
benbd3c2482017-01-07 05:48:21466 service_manager_connection->GetConnector()->StartService(
bene6a9f012017-01-07 00:43:43467 identity, std::move(service), mojo::MakeRequest(&pid_receiver));
rockot963ad3e82016-04-13 16:33:23468 pid_receiver->SetPID(base::GetCurrentProcId());
469
benaad37ecd2017-04-12 22:08:20470 service_manager_connection->GetConnector()->StartService(identity);
bend32292b2016-10-07 00:21:58471 BrowserContextServiceManagerConnectionHolder* connection_holder =
472 new BrowserContextServiceManagerConnectionHolder(
bend32292b2016-10-07 00:21:58473 std::move(service_request));
avi1ed4a4372017-04-25 05:39:41474 browser_context->SetUserData(kServiceManagerConnection,
475 base::WrapUnique(connection_holder));
rockot963ad3e82016-04-13 16:33:23476
bend32292b2016-10-07 00:21:58477 ServiceManagerConnection* connection =
478 connection_holder->service_manager_connection();
rockot963ad3e82016-04-13 16:33:23479
ben146248de2016-06-14 15:24:59480 // New embedded service factories should be added to |connection| here.
rockot963ad3e82016-04-13 16:33:23481
mekc8e2b272017-06-06 23:18:28482 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
483 switches::kDisableMojoLocalStorage)) {
Colin Blundell812ec6802017-06-27 15:50:49484 service_manager::EmbeddedServiceInfo info;
Marijn Kruisselbrinkcbc3df82017-07-06 19:25:59485 info.factory = base::Bind(&file::CreateFileService);
benaabad0b2016-11-16 23:54:17486 connection->AddEmbeddedService(file::mojom::kServiceName, info);
rockot963ad3e82016-04-13 16:33:23487 }
tibellab3d36b2017-03-10 02:57:25488
489 ContentBrowserClient::StaticServiceMap services;
490 browser_context->RegisterInProcessServices(&services);
491 for (const auto& entry : services) {
492 connection->AddEmbeddedService(entry.first, entry.second);
493 }
Ken Rockotc7a279c2017-05-04 23:51:45494
495 RegisterCommonBrowserInterfaces(connection);
sammc030f5012017-03-13 02:28:45496 connection->Start();
rockot963ad3e82016-04-13 16:33:23497 }
erge69130f52016-03-02 00:13:28498}
499
rockot963ad3e82016-04-13 16:33:23500// static
bend32292b2016-10-07 00:21:58501const std::string& BrowserContext::GetServiceUserIdFor(
bencccfe2a2016-03-05 16:54:14502 BrowserContext* browser_context) {
erge69130f52016-03-02 00:13:28503 CHECK(browser_context->GetUserData(kMojoWasInitialized))
504 << "Attempting to get the mojo user id for a BrowserContext that was "
505 << "never Initialize()ed.";
506
bend32292b2016-10-07 00:21:58507 ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>(
508 browser_context->GetUserData(kServiceUserId));
ben6c85c4492016-06-16 20:40:51509 return holder->user_id();
erge69130f52016-03-02 00:13:28510}
511
rockot963ad3e82016-04-13 16:33:23512// static
bend32292b2016-10-07 00:21:58513BrowserContext* BrowserContext::GetBrowserContextForServiceUserId(
ben6c85c4492016-06-16 20:40:51514 const std::string& user_id) {
515 auto it = g_user_id_to_context.Get().find(user_id);
516 return it != g_user_id_to_context.Get().end() ? it->second : nullptr;
517}
518
519// static
rockot400ea35b2016-10-15 19:15:32520service_manager::Connector* BrowserContext::GetConnectorFor(
rockot963ad3e82016-04-13 16:33:23521 BrowserContext* browser_context) {
bend32292b2016-10-07 00:21:58522 ServiceManagerConnection* connection =
523 GetServiceManagerConnectionFor(browser_context);
ben5be0b9132016-08-03 00:17:18524 return connection ? connection->GetConnector() : nullptr;
525}
526
527// static
bend32292b2016-10-07 00:21:58528ServiceManagerConnection* BrowserContext::GetServiceManagerConnectionFor(
ben5be0b9132016-08-03 00:17:18529 BrowserContext* browser_context) {
bend32292b2016-10-07 00:21:58530 BrowserContextServiceManagerConnectionHolder* connection_holder =
531 static_cast<BrowserContextServiceManagerConnectionHolder*>(
532 browser_context->GetUserData(kServiceManagerConnection));
533 return connection_holder ? connection_holder->service_manager_connection()
534 : nullptr;
rockot963ad3e82016-04-13 16:33:23535}
536
mmenkec0b2b8b12017-04-21 16:27:52537BrowserContext::BrowserContext()
538 : media_device_id_salt_(CreateRandomMediaDeviceIDSalt()) {}
539
[email protected]55eb70e762012-02-20 17:38:39540BrowserContext::~BrowserContext() {
erge69130f52016-03-02 00:13:28541 CHECK(GetUserData(kMojoWasInitialized))
542 << "Attempting to destroy a BrowserContext that never called "
543 << "Initialize()";
544
kinukof6ed359c2016-07-26 13:27:21545 DCHECK(!GetUserData(kStoragePartitionMapKeyName))
546 << "StoragePartitionMap is not shut down properly";
547
ben6c85c4492016-06-16 20:40:51548 RemoveBrowserContextFromUserIdMap(this);
549
[email protected]b441a8492012-06-06 14:55:57550 if (GetUserData(kDownloadManagerKeyName))
551 GetDownloadManager(this)->Shutdown();
[email protected]55eb70e762012-02-20 17:38:39552}
553
kinukof6ed359c2016-07-26 13:27:21554void BrowserContext::ShutdownStoragePartitions() {
555 if (GetUserData(kStoragePartitionMapKeyName))
556 RemoveUserData(kStoragePartitionMapKeyName);
557}
558
mmenkec0b2b8b12017-04-21 16:27:52559std::string BrowserContext::GetMediaDeviceIDSalt() {
560 return media_device_id_salt_;
561}
562
563// static
564std::string BrowserContext::CreateRandomMediaDeviceIDSalt() {
565 std::string salt;
566 base::Base64Encode(base::RandBytesAsString(16), &salt);
567 DCHECK(!salt.empty());
568 return salt;
569}
570
[email protected]55eb70e762012-02-20 17:38:39571} // namespace content