blob: 638e0ecc2ff862914320523ccebf325b8decfda3 [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>
rockot963ad3e82016-04-13 16:33:2311#include <memory>
dcheng36b6aec92015-12-26 06:16:3612#include <utility>
rockot963ad3e82016-04-13 16:33:2313#include <vector>
avib7348942015-12-25 20:57:1014
rockot963ad3e82016-04-13 16:33:2315#include "base/command_line.h"
bencccfe2a2016-03-05 16:54:1416#include "base/guid.h"
erge69130f52016-03-02 00:13:2817#include "base/lazy_instance.h"
rockot963ad3e82016-04-13 16:33:2318#include "base/macros.h"
erge69130f52016-03-02 00:13:2819#include "base/rand_util.h"
ben21a34c252016-06-29 22:24:3720#include "base/threading/thread_task_runner_handle.h"
avib7348942015-12-25 20:57:1021#include "build/build_config.h"
dmurph7ac019a2016-05-13 00:13:1722#include "content/browser/blob_storage/chrome_blob_storage_context.h"
[email protected]b441a8492012-06-06 14:55:5723#include "content/browser/download/download_manager_impl.h"
[email protected]c4d281662013-03-31 00:35:0824#include "content/browser/indexed_db/indexed_db_context_impl.h"
[email protected]678c0362012-12-05 08:02:4425#include "content/browser/loader/resource_dispatcher_host_impl.h"
mvanouwerkerk17205ea2014-11-07 17:30:1526#include "content/browser/push_messaging/push_messaging_router.h"
[email protected]4c3a23582012-08-18 08:54:3427#include "content/browser/storage_partition_impl_map.h"
[email protected]d7c7c98a2012-07-12 21:27:4428#include "content/common/child_process_host_impl.h"
[email protected]393b6cb2014-05-15 00:55:1229#include "content/public/browser/blob_handle.h"
[email protected]55eb70e762012-02-20 17:38:3930#include "content/public/browser/browser_thread.h"
[email protected]b441a8492012-06-06 14:55:5731#include "content/public/browser/content_browser_client.h"
falken04a6912a2016-09-23 21:06:2932#include "content/public/browser/render_process_host.h"
[email protected]536fd0b2013-03-14 17:41:5733#include "content/public/browser/site_instance.h"
rockot963ad3e82016-04-13 16:33:2334#include "content/public/common/content_switches.h"
bend32292b2016-10-07 00:21:5835#include "content/public/common/service_manager_connection.h"
ben51bb6c62016-11-17 20:15:5736#include "content/public/common/service_names.mojom.h"
[email protected]4d7c4ef2012-03-16 01:47:1237#include "net/cookies/cookie_store.h"
[email protected]6b8a3c742014-07-25 00:25:3538#include "net/ssl/channel_id_service.h"
39#include "net/ssl/channel_id_store.h"
[email protected]6e2d3d22012-02-24 18:10:3640#include "net/url_request/url_request_context.h"
[email protected]6939075a2012-08-28 08:35:5341#include "net/url_request/url_request_context_getter.h"
ben768c8dc2016-08-12 00:26:5042#include "services/file/file_service.h"
benaabad0b2016-11-16 23:54:1743#include "services/file/public/interfaces/constants.mojom.h"
ben768c8dc2016-08-12 00:26:5044#include "services/file/user_id_map.h"
rockot734fb662016-10-15 16:41:3045#include "services/service_manager/public/cpp/connection.h"
46#include "services/service_manager/public/cpp/connector.h"
47#include "services/service_manager/public/interfaces/service.mojom.h"
pilgrime92c5fcd2014-09-10 23:31:2348#include "storage/browser/database/database_tracker.h"
49#include "storage/browser/fileapi/external_mount_points.h"
[email protected]55eb70e762012-02-20 17:38:3950
[email protected]314c3e22012-02-21 03:57:4251using base::UserDataAdapter;
[email protected]55eb70e762012-02-20 17:38:3952
[email protected]55eb70e762012-02-20 17:38:3953namespace content {
54
[email protected]735e20c2012-03-20 01:16:5955namespace {
56
scottmg5e65e3a2017-03-08 08:48:4657base::LazyInstance<std::map<std::string, BrowserContext*>>::DestructorAtExit
ben6c85c4492016-06-16 20:40:5158 g_user_id_to_context = LAZY_INSTANCE_INITIALIZER;
59
bend32292b2016-10-07 00:21:5860class ServiceUserIdHolder : public base::SupportsUserData::Data {
ben6c85c4492016-06-16 20:40:5161 public:
bend32292b2016-10-07 00:21:5862 explicit ServiceUserIdHolder(const std::string& user_id)
63 : user_id_(user_id) {}
64 ~ServiceUserIdHolder() override {}
ben6c85c4492016-06-16 20:40:5165
66 const std::string& user_id() const { return user_id_; }
67
68 private:
69 std::string user_id_;
70
bend32292b2016-10-07 00:21:5871 DISALLOW_COPY_AND_ASSIGN(ServiceUserIdHolder);
ben6c85c4492016-06-16 20:40:5172};
erge69130f52016-03-02 00:13:2873
[email protected]e0ce8a1e2012-09-18 10:26:3674// Key names on BrowserContext.
[email protected]6ef0c3912013-01-25 22:46:3475const char kDownloadManagerKeyName[] = "download_manager";
erge69130f52016-03-02 00:13:2876const char kMojoWasInitialized[] = "mojo-was-initialized";
bend32292b2016-10-07 00:21:5877const char kServiceManagerConnection[] = "service-manager-connection";
78const char kServiceUserId[] = "service-user-id";
rockot963ad3e82016-04-13 16:33:2379const char kStoragePartitionMapKeyName[] = "content_storage_partition_map";
erge69130f52016-03-02 00:13:2880
[email protected]9afc14e22013-09-25 22:34:1481#if defined(OS_CHROMEOS)
82const char kMountPointsKey[] = "mount_points";
83#endif // defined(OS_CHROMEOS)
84
ben6c85c4492016-06-16 20:40:5185void RemoveBrowserContextFromUserIdMap(BrowserContext* browser_context) {
bend32292b2016-10-07 00:21:5886 ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>(
87 browser_context->GetUserData(kServiceUserId));
ben6c85c4492016-06-16 20:40:5188 if (holder) {
89 auto it = g_user_id_to_context.Get().find(holder->user_id());
90 if (it != g_user_id_to_context.Get().end())
91 g_user_id_to_context.Get().erase(it);
92 }
93}
94
[email protected]14acc642012-11-17 12:20:1095StoragePartitionImplMap* GetStoragePartitionMap(
96 BrowserContext* browser_context) {
[email protected]4c3a23582012-08-18 08:54:3497 StoragePartitionImplMap* partition_map =
98 static_cast<StoragePartitionImplMap*>(
a.cavalcantiffab73762015-08-15 02:55:4899 browser_context->GetUserData(kStoragePartitionMapKeyName));
[email protected]d7c7c98a2012-07-12 21:27:44100 if (!partition_map) {
[email protected]4c3a23582012-08-18 08:54:34101 partition_map = new StoragePartitionImplMap(browser_context);
a.cavalcantiffab73762015-08-15 02:55:48102 browser_context->SetUserData(kStoragePartitionMapKeyName, partition_map);
[email protected]d7c7c98a2012-07-12 21:27:44103 }
[email protected]14acc642012-11-17 12:20:10104 return partition_map;
105}
106
107StoragePartition* GetStoragePartitionFromConfig(
108 BrowserContext* browser_context,
109 const std::string& partition_domain,
110 const std::string& partition_name,
111 bool in_memory) {
112 StoragePartitionImplMap* partition_map =
113 GetStoragePartitionMap(browser_context);
[email protected]d7c7c98a2012-07-12 21:27:44114
[email protected]1bc28312012-11-08 08:31:53115 if (browser_context->IsOffTheRecord())
116 in_memory = true;
117
[email protected]1bc28312012-11-08 08:31:53118 return partition_map->Get(partition_domain, partition_name, in_memory);
[email protected]d1198fd2012-08-13 22:50:19119}
120
[email protected]6939075a2012-08-28 08:35:53121void SaveSessionStateOnIOThread(
122 const scoped_refptr<net::URLRequestContextGetter>& context_getter,
[email protected]98d6d4562014-06-25 20:57:55123 AppCacheServiceImpl* appcache_service) {
[email protected]6939075a2012-08-28 08:35:53124 net::URLRequestContext* context = context_getter->GetURLRequestContext();
mmenkeded79da2016-02-06 08:28:51125 context->cookie_store()->SetForceKeepSessionState();
[email protected]6b8a3c742014-07-25 00:25:35126 context->channel_id_service()->GetChannelIDStore()->
[email protected]6939075a2012-08-28 08:35:53127 SetForceKeepSessionState();
128 appcache_service->set_force_keep_session_state();
[email protected]6e2d3d22012-02-24 18:10:36129}
130
[email protected]89acda82013-06-25 20:52:50131void SaveSessionStateOnIndexedDBThread(
[email protected]6e2d3d22012-02-24 18:10:36132 scoped_refptr<IndexedDBContextImpl> indexed_db_context) {
[email protected]bf510ed2012-06-05 08:31:43133 indexed_db_context->SetForceKeepSessionState();
[email protected]6e2d3d22012-02-24 18:10:36134}
135
falken41f4175162014-10-29 07:03:41136void ShutdownServiceWorkerContext(StoragePartition* partition) {
137 ServiceWorkerContextWrapper* wrapper =
138 static_cast<ServiceWorkerContextWrapper*>(
139 partition->GetServiceWorkerContext());
140 wrapper->process_manager()->Shutdown();
141}
142
ttr31481dc54b2015-08-06 20:11:26143void SetDownloadManager(BrowserContext* context,
144 content::DownloadManager* download_manager) {
145 DCHECK_CURRENTLY_ON(BrowserThread::UI);
146 DCHECK(download_manager);
147 context->SetUserData(kDownloadManagerKeyName, download_manager);
148}
149
bend32292b2016-10-07 00:21:58150class BrowserContextServiceManagerConnectionHolder
rockot963ad3e82016-04-13 16:33:23151 : public base::SupportsUserData::Data {
152 public:
rockot400ea35b2016-10-15 19:15:32153 BrowserContextServiceManagerConnectionHolder(
154 std::unique_ptr<service_manager::Connection> connection,
155 service_manager::mojom::ServiceRequest request)
rockot963ad3e82016-04-13 16:33:23156 : root_connection_(std::move(connection)),
bend32292b2016-10-07 00:21:58157 service_manager_connection_(ServiceManagerConnection::Create(
rockotcef38272016-07-15 22:47:47158 std::move(request),
159 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO))) {}
bend32292b2016-10-07 00:21:58160 ~BrowserContextServiceManagerConnectionHolder() override {}
rockot963ad3e82016-04-13 16:33:23161
bend32292b2016-10-07 00:21:58162 ServiceManagerConnection* service_manager_connection() {
163 return service_manager_connection_.get();
164 }
rockot963ad3e82016-04-13 16:33:23165
166 private:
rockot400ea35b2016-10-15 19:15:32167 std::unique_ptr<service_manager::Connection> root_connection_;
bend32292b2016-10-07 00:21:58168 std::unique_ptr<ServiceManagerConnection> service_manager_connection_;
rockot963ad3e82016-04-13 16:33:23169
bend32292b2016-10-07 00:21:58170 DISALLOW_COPY_AND_ASSIGN(BrowserContextServiceManagerConnectionHolder);
rockot963ad3e82016-04-13 16:33:23171};
172
[email protected]735e20c2012-03-20 01:16:59173} // namespace
174
[email protected]14acc642012-11-17 12:20:10175// static
176void BrowserContext::AsyncObliterateStoragePartition(
177 BrowserContext* browser_context,
[email protected]399583b2012-12-11 09:33:42178 const GURL& site,
179 const base::Closure& on_gc_required) {
180 GetStoragePartitionMap(browser_context)->AsyncObliterate(site,
181 on_gc_required);
182}
183
184// static
185void BrowserContext::GarbageCollectStoragePartitions(
dcheng59716272016-04-09 05:19:08186 BrowserContext* browser_context,
187 std::unique_ptr<base::hash_set<base::FilePath>> active_paths,
188 const base::Closure& done) {
dcheng36b6aec92015-12-26 06:16:36189 GetStoragePartitionMap(browser_context)
190 ->GarbageCollect(std::move(active_paths), done);
[email protected]14acc642012-11-17 12:20:10191}
192
[email protected]b441a8492012-06-06 14:55:57193DownloadManager* BrowserContext::GetDownloadManager(
194 BrowserContext* context) {
mostynbfbcdc27a2015-03-13 17:58:52195 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]b441a8492012-06-06 14:55:57196 if (!context->GetUserData(kDownloadManagerKeyName)) {
[email protected]eba4a4d2013-05-29 02:18:06197 DownloadManager* download_manager =
[email protected]d25fda12012-06-12 17:05:03198 new DownloadManagerImpl(
[email protected]16798692013-04-23 18:08:38199 GetContentClient()->browser()->GetNetLog(), context);
[email protected]d25fda12012-06-12 17:05:03200
ttr31481dc54b2015-08-06 20:11:26201 SetDownloadManager(context, download_manager);
[email protected]b441a8492012-06-06 14:55:57202 download_manager->SetDelegate(context->GetDownloadManagerDelegate());
[email protected]b441a8492012-06-06 14:55:57203 }
204
[email protected]eba4a4d2013-05-29 02:18:06205 return static_cast<DownloadManager*>(
206 context->GetUserData(kDownloadManagerKeyName));
[email protected]b441a8492012-06-06 14:55:57207}
208
[email protected]6ef0c3912013-01-25 22:46:34209// static
[email protected]cd501a72014-08-22 19:58:31210storage::ExternalMountPoints* BrowserContext::GetMountPoints(
[email protected]6ef0c3912013-01-25 22:46:34211 BrowserContext* context) {
212 // Ensure that these methods are called on the UI thread, except for
213 // unittests where a UI thread might not have been created.
214 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
215 !BrowserThread::IsMessageLoopValid(BrowserThread::UI));
216
217#if defined(OS_CHROMEOS)
218 if (!context->GetUserData(kMountPointsKey)) {
[email protected]cd501a72014-08-22 19:58:31219 scoped_refptr<storage::ExternalMountPoints> mount_points =
220 storage::ExternalMountPoints::CreateRefCounted();
[email protected]6ef0c3912013-01-25 22:46:34221 context->SetUserData(
222 kMountPointsKey,
[email protected]cd501a72014-08-22 19:58:31223 new UserDataAdapter<storage::ExternalMountPoints>(mount_points.get()));
[email protected]6ef0c3912013-01-25 22:46:34224 }
225
[email protected]cd501a72014-08-22 19:58:31226 return UserDataAdapter<storage::ExternalMountPoints>::Get(context,
227 kMountPointsKey);
[email protected]6ef0c3912013-01-25 22:46:34228#else
229 return NULL;
230#endif
231}
232
[email protected]4c3a23582012-08-18 08:54:34233StoragePartition* BrowserContext::GetStoragePartition(
234 BrowserContext* browser_context,
235 SiteInstance* site_instance) {
[email protected]1bc28312012-11-08 08:31:53236 std::string partition_domain;
237 std::string partition_name;
238 bool in_memory = false;
[email protected]4c3a23582012-08-18 08:54:34239
[email protected]4c3a23582012-08-18 08:54:34240 if (site_instance) {
[email protected]1bc28312012-11-08 08:31:53241 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
[email protected]14acc642012-11-17 12:20:10242 browser_context, site_instance->GetSiteURL(), true,
[email protected]1bc28312012-11-08 08:31:53243 &partition_domain, &partition_name, &in_memory);
[email protected]4c3a23582012-08-18 08:54:34244 }
245
[email protected]1bc28312012-11-08 08:31:53246 return GetStoragePartitionFromConfig(
247 browser_context, partition_domain, partition_name, in_memory);
[email protected]4c3a23582012-08-18 08:54:34248}
249
[email protected]e94bbcb2012-09-07 05:33:57250StoragePartition* BrowserContext::GetStoragePartitionForSite(
251 BrowserContext* browser_context,
252 const GURL& site) {
[email protected]1bc28312012-11-08 08:31:53253 std::string partition_domain;
254 std::string partition_name;
255 bool in_memory;
[email protected]e94bbcb2012-09-07 05:33:57256
[email protected]1bc28312012-11-08 08:31:53257 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
[email protected]14acc642012-11-17 12:20:10258 browser_context, site, true, &partition_domain, &partition_name,
259 &in_memory);
[email protected]1bc28312012-11-08 08:31:53260
261 return GetStoragePartitionFromConfig(
262 browser_context, partition_domain, partition_name, in_memory);
[email protected]e94bbcb2012-09-07 05:33:57263}
264
[email protected]4c3a23582012-08-18 08:54:34265void BrowserContext::ForEachStoragePartition(
266 BrowserContext* browser_context,
267 const StoragePartitionCallback& callback) {
268 StoragePartitionImplMap* partition_map =
269 static_cast<StoragePartitionImplMap*>(
a.cavalcantiffab73762015-08-15 02:55:48270 browser_context->GetUserData(kStoragePartitionMapKeyName));
[email protected]4c3a23582012-08-18 08:54:34271 if (!partition_map)
272 return;
273
274 partition_map->ForEach(callback);
275}
276
277StoragePartition* BrowserContext::GetDefaultStoragePartition(
278 BrowserContext* browser_context) {
279 return GetStoragePartition(browser_context, NULL);
[email protected]55eb70e762012-02-20 17:38:39280}
281
tbarzicdb712682015-03-06 06:05:41282// static
[email protected]393b6cb2014-05-15 00:55:12283void BrowserContext::CreateMemoryBackedBlob(BrowserContext* browser_context,
284 const char* data, size_t length,
285 const BlobCallback& callback) {
mostynbfbcdc27a2015-03-13 17:58:52286 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]393b6cb2014-05-15 00:55:12287
288 ChromeBlobStorageContext* blob_context =
289 ChromeBlobStorageContext::GetFor(browser_context);
290 BrowserThread::PostTaskAndReplyWithResult(
291 BrowserThread::IO, FROM_HERE,
292 base::Bind(&ChromeBlobStorageContext::CreateMemoryBackedBlob,
293 make_scoped_refptr(blob_context), data, length),
294 callback);
295}
296
[email protected]66e53d0282014-08-07 10:04:35297// static
tbarzicdb712682015-03-06 06:05:41298void BrowserContext::CreateFileBackedBlob(
299 BrowserContext* browser_context,
300 const base::FilePath& path,
301 int64_t offset,
302 int64_t size,
303 const base::Time& expected_modification_time,
304 const BlobCallback& callback) {
mostynbfbcdc27a2015-03-13 17:58:52305 DCHECK_CURRENTLY_ON(BrowserThread::UI);
tbarzicdb712682015-03-06 06:05:41306
307 ChromeBlobStorageContext* blob_context =
308 ChromeBlobStorageContext::GetFor(browser_context);
309 BrowserThread::PostTaskAndReplyWithResult(
310 BrowserThread::IO, FROM_HERE,
311 base::Bind(&ChromeBlobStorageContext::CreateFileBackedBlob,
312 make_scoped_refptr(blob_context), path, offset, size,
313 expected_modification_time),
314 callback);
315}
316
317// static
[email protected]66e53d0282014-08-07 10:04:35318void BrowserContext::DeliverPushMessage(
319 BrowserContext* browser_context,
320 const GURL& origin,
avib7348942015-12-25 20:57:10321 int64_t service_worker_registration_id,
harknessdd4d2b22016-01-27 19:26:43322 const PushEventPayload& payload,
johnmea80c2552014-10-17 14:51:40323 const base::Callback<void(PushDeliveryStatus)>& callback) {
mostynbfbcdc27a2015-03-13 17:58:52324 DCHECK_CURRENTLY_ON(BrowserThread::UI);
harknessdd4d2b22016-01-27 19:26:43325 PushMessagingRouter::DeliverMessage(browser_context, origin,
326 service_worker_registration_id, payload,
327 callback);
[email protected]66e53d0282014-08-07 10:04:35328}
329
falken41f4175162014-10-29 07:03:41330// static
331void BrowserContext::NotifyWillBeDestroyed(BrowserContext* browser_context) {
332 // Service Workers must shutdown before the browser context is destroyed,
333 // since they keep render process hosts alive and the codebase assumes that
334 // render process hosts die before their profile (browser context) dies.
335 ForEachStoragePartition(browser_context,
336 base::Bind(ShutdownServiceWorkerContext));
falken04a6912a2016-09-23 21:06:29337
338 // Shared workers also keep render process hosts alive, and are expected to
339 // return ref counts to 0 after documents close. However, shared worker
340 // bookkeeping is done on the IO thread and we want to ensure the hosts are
341 // destructed now, so forcibly release their ref counts here.
342 for (RenderProcessHost::iterator host_iterator =
343 RenderProcessHost::AllHostsIterator();
344 !host_iterator.IsAtEnd(); host_iterator.Advance()) {
345 RenderProcessHost* host = host_iterator.GetCurrentValue();
346 if (host->GetBrowserContext() == browser_context)
347 host->ForceReleaseWorkerRefCounts();
348 }
falken41f4175162014-10-29 07:03:41349}
350
[email protected]314c3e22012-02-21 03:57:42351void BrowserContext::EnsureResourceContextInitialized(BrowserContext* context) {
[email protected]7e26ac92012-02-27 20:15:05352 // This will be enough to tickle initialization of BrowserContext if
353 // necessary, which initializes ResourceContext. The reason we don't call
[email protected]4c3a23582012-08-18 08:54:34354 // ResourceContext::InitializeResourceContext() directly here is that
355 // ResourceContext initialization may call back into BrowserContext
356 // and when that call returns it'll end rewriting its UserData map. It will
357 // end up rewriting the same value but this still causes a race condition.
358 //
359 // See http://crbug.com/115678.
360 GetDefaultStoragePartition(context);
[email protected]55eb70e762012-02-20 17:38:39361}
362
[email protected]6e2d3d22012-02-24 18:10:36363void BrowserContext::SaveSessionState(BrowserContext* browser_context) {
[email protected]5c8e67c2012-08-29 00:48:52364 GetDefaultStoragePartition(browser_context)->GetDatabaseTracker()->
365 SetForceKeepSessionState();
[email protected]b1b502e2012-09-16 07:31:43366 StoragePartition* storage_partition =
367 BrowserContext::GetDefaultStoragePartition(browser_context);
[email protected]6e2d3d22012-02-24 18:10:36368
369 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) {
370 BrowserThread::PostTask(
371 BrowserThread::IO, FROM_HERE,
[email protected]6939075a2012-08-28 08:35:53372 base::Bind(
373 &SaveSessionStateOnIOThread,
jamb84299e2016-04-12 16:58:59374 make_scoped_refptr(BrowserContext::GetDefaultStoragePartition(
375 browser_context)->GetURLRequestContext()),
[email protected]98d6d4562014-06-25 20:57:55376 static_cast<AppCacheServiceImpl*>(
[email protected]63ef85512014-06-05 14:21:26377 storage_partition->GetAppCacheService())));
[email protected]6e2d3d22012-02-24 18:10:36378 }
379
[email protected]5f2aa722013-08-07 16:59:41380 DOMStorageContextWrapper* dom_storage_context_proxy =
381 static_cast<DOMStorageContextWrapper*>(
[email protected]b1b502e2012-09-16 07:31:43382 storage_partition->GetDOMStorageContext());
[email protected]5f2aa722013-08-07 16:59:41383 dom_storage_context_proxy->SetForceKeepSessionState();
[email protected]735e20c2012-03-20 01:16:59384
[email protected]89acda82013-06-25 20:52:50385 IndexedDBContextImpl* indexed_db_context_impl =
386 static_cast<IndexedDBContextImpl*>(
[email protected]b1b502e2012-09-16 07:31:43387 storage_partition->GetIndexedDBContext());
[email protected]89acda82013-06-25 20:52:50388 // No task runner in unit tests.
389 if (indexed_db_context_impl->TaskRunner()) {
390 indexed_db_context_impl->TaskRunner()->PostTask(
391 FROM_HERE,
392 base::Bind(&SaveSessionStateOnIndexedDBThread,
393 make_scoped_refptr(indexed_db_context_impl)));
[email protected]6e2d3d22012-02-24 18:10:36394 }
395}
396
ttr31481dc54b2015-08-06 20:11:26397void BrowserContext::SetDownloadManagerForTesting(
398 BrowserContext* browser_context,
399 DownloadManager* download_manager) {
400 SetDownloadManager(browser_context, download_manager);
401}
402
rockot963ad3e82016-04-13 16:33:23403// static
erge69130f52016-03-02 00:13:28404void BrowserContext::Initialize(
405 BrowserContext* browser_context,
406 const base::FilePath& path) {
ben6c85c4492016-06-16 20:40:51407
408 std::string new_id;
409 if (GetContentClient() && GetContentClient()->browser()) {
bend32292b2016-10-07 00:21:58410 new_id = GetContentClient()->browser()->GetServiceUserIdForBrowserContext(
ben6c85c4492016-06-16 20:40:51411 browser_context);
412 } else {
413 // Some test scenarios initialize a BrowserContext without a content client.
bencccfe2a2016-03-05 16:54:14414 new_id = base::GenerateGUID();
ben6c85c4492016-06-16 20:40:51415 }
erge69130f52016-03-02 00:13:28416
bend32292b2016-10-07 00:21:58417 ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>(
418 browser_context->GetUserData(kServiceUserId));
ben6c85c4492016-06-16 20:40:51419 if (holder)
bend32292b2016-10-07 00:21:58420 file::ForgetServiceUserIdUserDirAssociation(holder->user_id());
421 file::AssociateServiceUserIdWithUserDir(new_id, path);
ben6c85c4492016-06-16 20:40:51422 RemoveBrowserContextFromUserIdMap(browser_context);
423 g_user_id_to_context.Get()[new_id] = browser_context;
bend32292b2016-10-07 00:21:58424 browser_context->SetUserData(kServiceUserId,
425 new ServiceUserIdHolder(new_id));
erge69130f52016-03-02 00:13:28426
erge69130f52016-03-02 00:13:28427 browser_context->SetUserData(kMojoWasInitialized,
428 new base::SupportsUserData::Data);
rockot963ad3e82016-04-13 16:33:23429
bend32292b2016-10-07 00:21:58430 ServiceManagerConnection* service_manager_connection =
431 ServiceManagerConnection::GetForProcess();
fdoray4f155f02016-10-12 11:28:50432 if (service_manager_connection && base::ThreadTaskRunnerHandle::IsSet()) {
rockot963ad3e82016-04-13 16:33:23433 // NOTE: Many unit tests create a TestBrowserContext without initializing
bend32292b2016-10-07 00:21:58434 // Mojo or the global service manager connection.
rockot963ad3e82016-04-13 16:33:23435
rockot400ea35b2016-10-15 19:15:32436 service_manager::mojom::ServicePtr service;
blundellc17a15d2016-12-21 13:52:10437 service_manager::mojom::ServiceRequest service_request(&service);
rockot963ad3e82016-04-13 16:33:23438
rockot400ea35b2016-10-15 19:15:32439 service_manager::mojom::PIDReceiverPtr pid_receiver;
bene6a9f012017-01-07 00:43:43440 service_manager::Identity identity(mojom::kBrowserServiceName, new_id);
benbd3c2482017-01-07 05:48:21441 service_manager_connection->GetConnector()->StartService(
bene6a9f012017-01-07 00:43:43442 identity, std::move(service), mojo::MakeRequest(&pid_receiver));
rockot963ad3e82016-04-13 16:33:23443 pid_receiver->SetPID(base::GetCurrentProcId());
444
bend32292b2016-10-07 00:21:58445 BrowserContextServiceManagerConnectionHolder* connection_holder =
446 new BrowserContextServiceManagerConnectionHolder(
bene6a9f012017-01-07 00:43:43447 service_manager_connection->GetConnector()->Connect(identity),
bend32292b2016-10-07 00:21:58448 std::move(service_request));
449 browser_context->SetUserData(kServiceManagerConnection, connection_holder);
rockot963ad3e82016-04-13 16:33:23450
bend32292b2016-10-07 00:21:58451 ServiceManagerConnection* connection =
452 connection_holder->service_manager_connection();
rockot963ad3e82016-04-13 16:33:23453
ben146248de2016-06-14 15:24:59454 // New embedded service factories should be added to |connection| here.
rockot963ad3e82016-04-13 16:33:23455
456 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
457 switches::kMojoLocalStorage)) {
bend32292b2016-10-07 00:21:58458 ServiceInfo info;
459 info.factory =
ben768c8dc2016-08-12 00:26:50460 base::Bind(&file::CreateFileService,
thestig529ad8a2016-07-08 20:30:12461 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE),
462 BrowserThread::GetTaskRunnerForThread(BrowserThread::DB));
benaabad0b2016-11-16 23:54:17463 connection->AddEmbeddedService(file::mojom::kServiceName, info);
rockot963ad3e82016-04-13 16:33:23464 }
tibellab3d36b2017-03-10 02:57:25465
466 ContentBrowserClient::StaticServiceMap services;
467 browser_context->RegisterInProcessServices(&services);
468 for (const auto& entry : services) {
469 connection->AddEmbeddedService(entry.first, entry.second);
470 }
sammc030f5012017-03-13 02:28:45471 connection->Start();
rockot963ad3e82016-04-13 16:33:23472 }
erge69130f52016-03-02 00:13:28473}
474
rockot963ad3e82016-04-13 16:33:23475// static
bend32292b2016-10-07 00:21:58476const std::string& BrowserContext::GetServiceUserIdFor(
bencccfe2a2016-03-05 16:54:14477 BrowserContext* browser_context) {
erge69130f52016-03-02 00:13:28478 CHECK(browser_context->GetUserData(kMojoWasInitialized))
479 << "Attempting to get the mojo user id for a BrowserContext that was "
480 << "never Initialize()ed.";
481
bend32292b2016-10-07 00:21:58482 ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>(
483 browser_context->GetUserData(kServiceUserId));
ben6c85c4492016-06-16 20:40:51484 return holder->user_id();
erge69130f52016-03-02 00:13:28485}
486
rockot963ad3e82016-04-13 16:33:23487// static
bend32292b2016-10-07 00:21:58488BrowserContext* BrowserContext::GetBrowserContextForServiceUserId(
ben6c85c4492016-06-16 20:40:51489 const std::string& user_id) {
490 auto it = g_user_id_to_context.Get().find(user_id);
491 return it != g_user_id_to_context.Get().end() ? it->second : nullptr;
492}
493
494// static
rockot400ea35b2016-10-15 19:15:32495service_manager::Connector* BrowserContext::GetConnectorFor(
rockot963ad3e82016-04-13 16:33:23496 BrowserContext* browser_context) {
bend32292b2016-10-07 00:21:58497 ServiceManagerConnection* connection =
498 GetServiceManagerConnectionFor(browser_context);
ben5be0b9132016-08-03 00:17:18499 return connection ? connection->GetConnector() : nullptr;
500}
501
502// static
bend32292b2016-10-07 00:21:58503ServiceManagerConnection* BrowserContext::GetServiceManagerConnectionFor(
ben5be0b9132016-08-03 00:17:18504 BrowserContext* browser_context) {
bend32292b2016-10-07 00:21:58505 BrowserContextServiceManagerConnectionHolder* connection_holder =
506 static_cast<BrowserContextServiceManagerConnectionHolder*>(
507 browser_context->GetUserData(kServiceManagerConnection));
508 return connection_holder ? connection_holder->service_manager_connection()
509 : nullptr;
rockot963ad3e82016-04-13 16:33:23510}
511
[email protected]55eb70e762012-02-20 17:38:39512BrowserContext::~BrowserContext() {
erge69130f52016-03-02 00:13:28513 CHECK(GetUserData(kMojoWasInitialized))
514 << "Attempting to destroy a BrowserContext that never called "
515 << "Initialize()";
516
kinukof6ed359c2016-07-26 13:27:21517 DCHECK(!GetUserData(kStoragePartitionMapKeyName))
518 << "StoragePartitionMap is not shut down properly";
519
ben6c85c4492016-06-16 20:40:51520 RemoveBrowserContextFromUserIdMap(this);
521
[email protected]b441a8492012-06-06 14:55:57522 if (GetUserData(kDownloadManagerKeyName))
523 GetDownloadManager(this)->Shutdown();
[email protected]55eb70e762012-02-20 17:38:39524}
525
kinukof6ed359c2016-07-26 13:27:21526void BrowserContext::ShutdownStoragePartitions() {
527 if (GetUserData(kStoragePartitionMapKeyName))
528 RemoveUserData(kStoragePartitionMapKeyName);
529}
530
[email protected]55eb70e762012-02-20 17:38:39531} // namespace content