blob: 063b0efaf4013b16468213ec21ce27579b8f4108 [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"
blundell372d47d282016-11-01 01:34:3042#include "services/device/device_service.h"
43#include "services/device/public/cpp/constants.h"
ben768c8dc2016-08-12 00:26:5044#include "services/file/file_service.h"
benaabad0b2016-11-16 23:54:1745#include "services/file/public/interfaces/constants.mojom.h"
ben768c8dc2016-08-12 00:26:5046#include "services/file/user_id_map.h"
rockot734fb662016-10-15 16:41:3047#include "services/service_manager/public/cpp/connection.h"
48#include "services/service_manager/public/cpp/connector.h"
49#include "services/service_manager/public/interfaces/service.mojom.h"
pilgrime92c5fcd2014-09-10 23:31:2350#include "storage/browser/database/database_tracker.h"
51#include "storage/browser/fileapi/external_mount_points.h"
[email protected]55eb70e762012-02-20 17:38:3952
[email protected]314c3e22012-02-21 03:57:4253using base::UserDataAdapter;
[email protected]55eb70e762012-02-20 17:38:3954
[email protected]55eb70e762012-02-20 17:38:3955namespace content {
56
[email protected]735e20c2012-03-20 01:16:5957namespace {
58
ben6c85c4492016-06-16 20:40:5159base::LazyInstance<std::map<std::string, BrowserContext*>>
60 g_user_id_to_context = LAZY_INSTANCE_INITIALIZER;
61
bend32292b2016-10-07 00:21:5862class ServiceUserIdHolder : public base::SupportsUserData::Data {
ben6c85c4492016-06-16 20:40:5163 public:
bend32292b2016-10-07 00:21:5864 explicit ServiceUserIdHolder(const std::string& user_id)
65 : user_id_(user_id) {}
66 ~ServiceUserIdHolder() override {}
ben6c85c4492016-06-16 20:40:5167
68 const std::string& user_id() const { return user_id_; }
69
70 private:
71 std::string user_id_;
72
bend32292b2016-10-07 00:21:5873 DISALLOW_COPY_AND_ASSIGN(ServiceUserIdHolder);
ben6c85c4492016-06-16 20:40:5174};
erge69130f52016-03-02 00:13:2875
[email protected]e0ce8a1e2012-09-18 10:26:3676// Key names on BrowserContext.
[email protected]6ef0c3912013-01-25 22:46:3477const char kDownloadManagerKeyName[] = "download_manager";
erge69130f52016-03-02 00:13:2878const char kMojoWasInitialized[] = "mojo-was-initialized";
bend32292b2016-10-07 00:21:5879const char kServiceManagerConnection[] = "service-manager-connection";
80const char kServiceUserId[] = "service-user-id";
rockot963ad3e82016-04-13 16:33:2381const char kStoragePartitionMapKeyName[] = "content_storage_partition_map";
erge69130f52016-03-02 00:13:2882
[email protected]9afc14e22013-09-25 22:34:1483#if defined(OS_CHROMEOS)
84const char kMountPointsKey[] = "mount_points";
85#endif // defined(OS_CHROMEOS)
86
ben6c85c4492016-06-16 20:40:5187void RemoveBrowserContextFromUserIdMap(BrowserContext* browser_context) {
bend32292b2016-10-07 00:21:5888 ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>(
89 browser_context->GetUserData(kServiceUserId));
ben6c85c4492016-06-16 20:40:5190 if (holder) {
91 auto it = g_user_id_to_context.Get().find(holder->user_id());
92 if (it != g_user_id_to_context.Get().end())
93 g_user_id_to_context.Get().erase(it);
94 }
95}
96
[email protected]14acc642012-11-17 12:20:1097StoragePartitionImplMap* GetStoragePartitionMap(
98 BrowserContext* browser_context) {
[email protected]4c3a23582012-08-18 08:54:3499 StoragePartitionImplMap* partition_map =
100 static_cast<StoragePartitionImplMap*>(
a.cavalcantiffab73762015-08-15 02:55:48101 browser_context->GetUserData(kStoragePartitionMapKeyName));
[email protected]d7c7c98a2012-07-12 21:27:44102 if (!partition_map) {
[email protected]4c3a23582012-08-18 08:54:34103 partition_map = new StoragePartitionImplMap(browser_context);
a.cavalcantiffab73762015-08-15 02:55:48104 browser_context->SetUserData(kStoragePartitionMapKeyName, partition_map);
[email protected]d7c7c98a2012-07-12 21:27:44105 }
[email protected]14acc642012-11-17 12:20:10106 return partition_map;
107}
108
109StoragePartition* GetStoragePartitionFromConfig(
110 BrowserContext* browser_context,
111 const std::string& partition_domain,
112 const std::string& partition_name,
113 bool in_memory) {
114 StoragePartitionImplMap* partition_map =
115 GetStoragePartitionMap(browser_context);
[email protected]d7c7c98a2012-07-12 21:27:44116
[email protected]1bc28312012-11-08 08:31:53117 if (browser_context->IsOffTheRecord())
118 in_memory = true;
119
[email protected]1bc28312012-11-08 08:31:53120 return partition_map->Get(partition_domain, partition_name, in_memory);
[email protected]d1198fd2012-08-13 22:50:19121}
122
[email protected]6939075a2012-08-28 08:35:53123void SaveSessionStateOnIOThread(
124 const scoped_refptr<net::URLRequestContextGetter>& context_getter,
[email protected]98d6d4562014-06-25 20:57:55125 AppCacheServiceImpl* appcache_service) {
[email protected]6939075a2012-08-28 08:35:53126 net::URLRequestContext* context = context_getter->GetURLRequestContext();
mmenkeded79da2016-02-06 08:28:51127 context->cookie_store()->SetForceKeepSessionState();
[email protected]6b8a3c742014-07-25 00:25:35128 context->channel_id_service()->GetChannelIDStore()->
[email protected]6939075a2012-08-28 08:35:53129 SetForceKeepSessionState();
130 appcache_service->set_force_keep_session_state();
[email protected]6e2d3d22012-02-24 18:10:36131}
132
[email protected]89acda82013-06-25 20:52:50133void SaveSessionStateOnIndexedDBThread(
[email protected]6e2d3d22012-02-24 18:10:36134 scoped_refptr<IndexedDBContextImpl> indexed_db_context) {
[email protected]bf510ed2012-06-05 08:31:43135 indexed_db_context->SetForceKeepSessionState();
[email protected]6e2d3d22012-02-24 18:10:36136}
137
falken41f4175162014-10-29 07:03:41138void ShutdownServiceWorkerContext(StoragePartition* partition) {
139 ServiceWorkerContextWrapper* wrapper =
140 static_cast<ServiceWorkerContextWrapper*>(
141 partition->GetServiceWorkerContext());
142 wrapper->process_manager()->Shutdown();
143}
144
ttr31481dc54b2015-08-06 20:11:26145void SetDownloadManager(BrowserContext* context,
146 content::DownloadManager* download_manager) {
147 DCHECK_CURRENTLY_ON(BrowserThread::UI);
148 DCHECK(download_manager);
149 context->SetUserData(kDownloadManagerKeyName, download_manager);
150}
151
bend32292b2016-10-07 00:21:58152class BrowserContextServiceManagerConnectionHolder
rockot963ad3e82016-04-13 16:33:23153 : public base::SupportsUserData::Data {
154 public:
rockot400ea35b2016-10-15 19:15:32155 BrowserContextServiceManagerConnectionHolder(
156 std::unique_ptr<service_manager::Connection> connection,
157 service_manager::mojom::ServiceRequest request)
rockot963ad3e82016-04-13 16:33:23158 : root_connection_(std::move(connection)),
bend32292b2016-10-07 00:21:58159 service_manager_connection_(ServiceManagerConnection::Create(
rockotcef38272016-07-15 22:47:47160 std::move(request),
161 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO))) {}
bend32292b2016-10-07 00:21:58162 ~BrowserContextServiceManagerConnectionHolder() override {}
rockot963ad3e82016-04-13 16:33:23163
bend32292b2016-10-07 00:21:58164 ServiceManagerConnection* service_manager_connection() {
165 return service_manager_connection_.get();
166 }
rockot963ad3e82016-04-13 16:33:23167
168 private:
rockot400ea35b2016-10-15 19:15:32169 std::unique_ptr<service_manager::Connection> root_connection_;
bend32292b2016-10-07 00:21:58170 std::unique_ptr<ServiceManagerConnection> service_manager_connection_;
rockot963ad3e82016-04-13 16:33:23171
bend32292b2016-10-07 00:21:58172 DISALLOW_COPY_AND_ASSIGN(BrowserContextServiceManagerConnectionHolder);
rockot963ad3e82016-04-13 16:33:23173};
174
[email protected]735e20c2012-03-20 01:16:59175} // namespace
176
[email protected]14acc642012-11-17 12:20:10177// static
178void BrowserContext::AsyncObliterateStoragePartition(
179 BrowserContext* browser_context,
[email protected]399583b2012-12-11 09:33:42180 const GURL& site,
181 const base::Closure& on_gc_required) {
182 GetStoragePartitionMap(browser_context)->AsyncObliterate(site,
183 on_gc_required);
184}
185
186// static
187void BrowserContext::GarbageCollectStoragePartitions(
dcheng59716272016-04-09 05:19:08188 BrowserContext* browser_context,
189 std::unique_ptr<base::hash_set<base::FilePath>> active_paths,
190 const base::Closure& done) {
dcheng36b6aec92015-12-26 06:16:36191 GetStoragePartitionMap(browser_context)
192 ->GarbageCollect(std::move(active_paths), done);
[email protected]14acc642012-11-17 12:20:10193}
194
[email protected]b441a8492012-06-06 14:55:57195DownloadManager* BrowserContext::GetDownloadManager(
196 BrowserContext* context) {
mostynbfbcdc27a2015-03-13 17:58:52197 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]b441a8492012-06-06 14:55:57198 if (!context->GetUserData(kDownloadManagerKeyName)) {
[email protected]eba4a4d2013-05-29 02:18:06199 DownloadManager* download_manager =
[email protected]d25fda12012-06-12 17:05:03200 new DownloadManagerImpl(
[email protected]16798692013-04-23 18:08:38201 GetContentClient()->browser()->GetNetLog(), context);
[email protected]d25fda12012-06-12 17:05:03202
ttr31481dc54b2015-08-06 20:11:26203 SetDownloadManager(context, download_manager);
[email protected]b441a8492012-06-06 14:55:57204 download_manager->SetDelegate(context->GetDownloadManagerDelegate());
[email protected]b441a8492012-06-06 14:55:57205 }
206
[email protected]eba4a4d2013-05-29 02:18:06207 return static_cast<DownloadManager*>(
208 context->GetUserData(kDownloadManagerKeyName));
[email protected]b441a8492012-06-06 14:55:57209}
210
[email protected]6ef0c3912013-01-25 22:46:34211// static
[email protected]cd501a72014-08-22 19:58:31212storage::ExternalMountPoints* BrowserContext::GetMountPoints(
[email protected]6ef0c3912013-01-25 22:46:34213 BrowserContext* context) {
214 // Ensure that these methods are called on the UI thread, except for
215 // unittests where a UI thread might not have been created.
216 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
217 !BrowserThread::IsMessageLoopValid(BrowserThread::UI));
218
219#if defined(OS_CHROMEOS)
220 if (!context->GetUserData(kMountPointsKey)) {
[email protected]cd501a72014-08-22 19:58:31221 scoped_refptr<storage::ExternalMountPoints> mount_points =
222 storage::ExternalMountPoints::CreateRefCounted();
[email protected]6ef0c3912013-01-25 22:46:34223 context->SetUserData(
224 kMountPointsKey,
[email protected]cd501a72014-08-22 19:58:31225 new UserDataAdapter<storage::ExternalMountPoints>(mount_points.get()));
[email protected]6ef0c3912013-01-25 22:46:34226 }
227
[email protected]cd501a72014-08-22 19:58:31228 return UserDataAdapter<storage::ExternalMountPoints>::Get(context,
229 kMountPointsKey);
[email protected]6ef0c3912013-01-25 22:46:34230#else
231 return NULL;
232#endif
233}
234
[email protected]4c3a23582012-08-18 08:54:34235StoragePartition* BrowserContext::GetStoragePartition(
236 BrowserContext* browser_context,
237 SiteInstance* site_instance) {
[email protected]1bc28312012-11-08 08:31:53238 std::string partition_domain;
239 std::string partition_name;
240 bool in_memory = false;
[email protected]4c3a23582012-08-18 08:54:34241
242 // TODO(ajwong): After GetDefaultStoragePartition() is removed, get rid of
243 // this conditional and require that |site_instance| is non-NULL.
244 if (site_instance) {
[email protected]1bc28312012-11-08 08:31:53245 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
[email protected]14acc642012-11-17 12:20:10246 browser_context, site_instance->GetSiteURL(), true,
[email protected]1bc28312012-11-08 08:31:53247 &partition_domain, &partition_name, &in_memory);
[email protected]4c3a23582012-08-18 08:54:34248 }
249
[email protected]1bc28312012-11-08 08:31:53250 return GetStoragePartitionFromConfig(
251 browser_context, partition_domain, partition_name, in_memory);
[email protected]4c3a23582012-08-18 08:54:34252}
253
[email protected]e94bbcb2012-09-07 05:33:57254StoragePartition* BrowserContext::GetStoragePartitionForSite(
255 BrowserContext* browser_context,
256 const GURL& site) {
[email protected]1bc28312012-11-08 08:31:53257 std::string partition_domain;
258 std::string partition_name;
259 bool in_memory;
[email protected]e94bbcb2012-09-07 05:33:57260
[email protected]1bc28312012-11-08 08:31:53261 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
[email protected]14acc642012-11-17 12:20:10262 browser_context, site, true, &partition_domain, &partition_name,
263 &in_memory);
[email protected]1bc28312012-11-08 08:31:53264
265 return GetStoragePartitionFromConfig(
266 browser_context, partition_domain, partition_name, in_memory);
[email protected]e94bbcb2012-09-07 05:33:57267}
268
[email protected]4c3a23582012-08-18 08:54:34269void BrowserContext::ForEachStoragePartition(
270 BrowserContext* browser_context,
271 const StoragePartitionCallback& callback) {
272 StoragePartitionImplMap* partition_map =
273 static_cast<StoragePartitionImplMap*>(
a.cavalcantiffab73762015-08-15 02:55:48274 browser_context->GetUserData(kStoragePartitionMapKeyName));
[email protected]4c3a23582012-08-18 08:54:34275 if (!partition_map)
276 return;
277
278 partition_map->ForEach(callback);
279}
280
281StoragePartition* BrowserContext::GetDefaultStoragePartition(
282 BrowserContext* browser_context) {
283 return GetStoragePartition(browser_context, NULL);
[email protected]55eb70e762012-02-20 17:38:39284}
285
tbarzicdb712682015-03-06 06:05:41286// static
[email protected]393b6cb2014-05-15 00:55:12287void BrowserContext::CreateMemoryBackedBlob(BrowserContext* browser_context,
288 const char* data, size_t length,
289 const BlobCallback& callback) {
mostynbfbcdc27a2015-03-13 17:58:52290 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]393b6cb2014-05-15 00:55:12291
292 ChromeBlobStorageContext* blob_context =
293 ChromeBlobStorageContext::GetFor(browser_context);
294 BrowserThread::PostTaskAndReplyWithResult(
295 BrowserThread::IO, FROM_HERE,
296 base::Bind(&ChromeBlobStorageContext::CreateMemoryBackedBlob,
297 make_scoped_refptr(blob_context), data, length),
298 callback);
299}
300
[email protected]66e53d0282014-08-07 10:04:35301// static
tbarzicdb712682015-03-06 06:05:41302void BrowserContext::CreateFileBackedBlob(
303 BrowserContext* browser_context,
304 const base::FilePath& path,
305 int64_t offset,
306 int64_t size,
307 const base::Time& expected_modification_time,
308 const BlobCallback& callback) {
mostynbfbcdc27a2015-03-13 17:58:52309 DCHECK_CURRENTLY_ON(BrowserThread::UI);
tbarzicdb712682015-03-06 06:05:41310
311 ChromeBlobStorageContext* blob_context =
312 ChromeBlobStorageContext::GetFor(browser_context);
313 BrowserThread::PostTaskAndReplyWithResult(
314 BrowserThread::IO, FROM_HERE,
315 base::Bind(&ChromeBlobStorageContext::CreateFileBackedBlob,
316 make_scoped_refptr(blob_context), path, offset, size,
317 expected_modification_time),
318 callback);
319}
320
321// static
[email protected]66e53d0282014-08-07 10:04:35322void BrowserContext::DeliverPushMessage(
323 BrowserContext* browser_context,
324 const GURL& origin,
avib7348942015-12-25 20:57:10325 int64_t service_worker_registration_id,
harknessdd4d2b22016-01-27 19:26:43326 const PushEventPayload& payload,
johnmea80c2552014-10-17 14:51:40327 const base::Callback<void(PushDeliveryStatus)>& callback) {
mostynbfbcdc27a2015-03-13 17:58:52328 DCHECK_CURRENTLY_ON(BrowserThread::UI);
harknessdd4d2b22016-01-27 19:26:43329 PushMessagingRouter::DeliverMessage(browser_context, origin,
330 service_worker_registration_id, payload,
331 callback);
[email protected]66e53d0282014-08-07 10:04:35332}
333
falken41f4175162014-10-29 07:03:41334// static
335void BrowserContext::NotifyWillBeDestroyed(BrowserContext* browser_context) {
336 // Service Workers must shutdown before the browser context is destroyed,
337 // since they keep render process hosts alive and the codebase assumes that
338 // render process hosts die before their profile (browser context) dies.
339 ForEachStoragePartition(browser_context,
340 base::Bind(ShutdownServiceWorkerContext));
falken04a6912a2016-09-23 21:06:29341
342 // Shared workers also keep render process hosts alive, and are expected to
343 // return ref counts to 0 after documents close. However, shared worker
344 // bookkeeping is done on the IO thread and we want to ensure the hosts are
345 // destructed now, so forcibly release their ref counts here.
346 for (RenderProcessHost::iterator host_iterator =
347 RenderProcessHost::AllHostsIterator();
348 !host_iterator.IsAtEnd(); host_iterator.Advance()) {
349 RenderProcessHost* host = host_iterator.GetCurrentValue();
350 if (host->GetBrowserContext() == browser_context)
351 host->ForceReleaseWorkerRefCounts();
352 }
falken41f4175162014-10-29 07:03:41353}
354
[email protected]314c3e22012-02-21 03:57:42355void BrowserContext::EnsureResourceContextInitialized(BrowserContext* context) {
[email protected]7e26ac92012-02-27 20:15:05356 // This will be enough to tickle initialization of BrowserContext if
357 // necessary, which initializes ResourceContext. The reason we don't call
[email protected]4c3a23582012-08-18 08:54:34358 // ResourceContext::InitializeResourceContext() directly here is that
359 // ResourceContext initialization may call back into BrowserContext
360 // and when that call returns it'll end rewriting its UserData map. It will
361 // end up rewriting the same value but this still causes a race condition.
362 //
363 // See http://crbug.com/115678.
364 GetDefaultStoragePartition(context);
[email protected]55eb70e762012-02-20 17:38:39365}
366
[email protected]6e2d3d22012-02-24 18:10:36367void BrowserContext::SaveSessionState(BrowserContext* browser_context) {
[email protected]5c8e67c2012-08-29 00:48:52368 GetDefaultStoragePartition(browser_context)->GetDatabaseTracker()->
369 SetForceKeepSessionState();
[email protected]b1b502e2012-09-16 07:31:43370 StoragePartition* storage_partition =
371 BrowserContext::GetDefaultStoragePartition(browser_context);
[email protected]6e2d3d22012-02-24 18:10:36372
373 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) {
374 BrowserThread::PostTask(
375 BrowserThread::IO, FROM_HERE,
[email protected]6939075a2012-08-28 08:35:53376 base::Bind(
377 &SaveSessionStateOnIOThread,
jamb84299e2016-04-12 16:58:59378 make_scoped_refptr(BrowserContext::GetDefaultStoragePartition(
379 browser_context)->GetURLRequestContext()),
[email protected]98d6d4562014-06-25 20:57:55380 static_cast<AppCacheServiceImpl*>(
[email protected]63ef85512014-06-05 14:21:26381 storage_partition->GetAppCacheService())));
[email protected]6e2d3d22012-02-24 18:10:36382 }
383
[email protected]5f2aa722013-08-07 16:59:41384 DOMStorageContextWrapper* dom_storage_context_proxy =
385 static_cast<DOMStorageContextWrapper*>(
[email protected]b1b502e2012-09-16 07:31:43386 storage_partition->GetDOMStorageContext());
[email protected]5f2aa722013-08-07 16:59:41387 dom_storage_context_proxy->SetForceKeepSessionState();
[email protected]735e20c2012-03-20 01:16:59388
[email protected]89acda82013-06-25 20:52:50389 IndexedDBContextImpl* indexed_db_context_impl =
390 static_cast<IndexedDBContextImpl*>(
[email protected]b1b502e2012-09-16 07:31:43391 storage_partition->GetIndexedDBContext());
[email protected]89acda82013-06-25 20:52:50392 // No task runner in unit tests.
393 if (indexed_db_context_impl->TaskRunner()) {
394 indexed_db_context_impl->TaskRunner()->PostTask(
395 FROM_HERE,
396 base::Bind(&SaveSessionStateOnIndexedDBThread,
397 make_scoped_refptr(indexed_db_context_impl)));
[email protected]6e2d3d22012-02-24 18:10:36398 }
399}
400
ttr31481dc54b2015-08-06 20:11:26401void BrowserContext::SetDownloadManagerForTesting(
402 BrowserContext* browser_context,
403 DownloadManager* download_manager) {
404 SetDownloadManager(browser_context, download_manager);
405}
406
rockot963ad3e82016-04-13 16:33:23407// static
erge69130f52016-03-02 00:13:28408void BrowserContext::Initialize(
409 BrowserContext* browser_context,
410 const base::FilePath& path) {
ben6c85c4492016-06-16 20:40:51411
412 std::string new_id;
413 if (GetContentClient() && GetContentClient()->browser()) {
bend32292b2016-10-07 00:21:58414 new_id = GetContentClient()->browser()->GetServiceUserIdForBrowserContext(
ben6c85c4492016-06-16 20:40:51415 browser_context);
416 } else {
417 // Some test scenarios initialize a BrowserContext without a content client.
bencccfe2a2016-03-05 16:54:14418 new_id = base::GenerateGUID();
ben6c85c4492016-06-16 20:40:51419 }
erge69130f52016-03-02 00:13:28420
bend32292b2016-10-07 00:21:58421 ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>(
422 browser_context->GetUserData(kServiceUserId));
ben6c85c4492016-06-16 20:40:51423 if (holder)
bend32292b2016-10-07 00:21:58424 file::ForgetServiceUserIdUserDirAssociation(holder->user_id());
425 file::AssociateServiceUserIdWithUserDir(new_id, path);
ben6c85c4492016-06-16 20:40:51426 RemoveBrowserContextFromUserIdMap(browser_context);
427 g_user_id_to_context.Get()[new_id] = browser_context;
bend32292b2016-10-07 00:21:58428 browser_context->SetUserData(kServiceUserId,
429 new ServiceUserIdHolder(new_id));
erge69130f52016-03-02 00:13:28430
erge69130f52016-03-02 00:13:28431 browser_context->SetUserData(kMojoWasInitialized,
432 new base::SupportsUserData::Data);
rockot963ad3e82016-04-13 16:33:23433
bend32292b2016-10-07 00:21:58434 ServiceManagerConnection* service_manager_connection =
435 ServiceManagerConnection::GetForProcess();
fdoray4f155f02016-10-12 11:28:50436 if (service_manager_connection && base::ThreadTaskRunnerHandle::IsSet()) {
rockot963ad3e82016-04-13 16:33:23437 // NOTE: Many unit tests create a TestBrowserContext without initializing
bend32292b2016-10-07 00:21:58438 // Mojo or the global service manager connection.
rockot963ad3e82016-04-13 16:33:23439
rockot400ea35b2016-10-15 19:15:32440 service_manager::mojom::ServicePtr service;
441 service_manager::mojom::ServiceRequest service_request =
442 mojo::GetProxy(&service);
rockot963ad3e82016-04-13 16:33:23443
rockot400ea35b2016-10-15 19:15:32444 service_manager::mojom::PIDReceiverPtr pid_receiver;
445 service_manager::Connector::ConnectParams params(
ben51bb6c62016-11-17 20:15:57446 service_manager::Identity(mojom::kBrowserServiceName, new_id));
bene1bbc002016-07-05 16:04:36447 params.set_client_process_connection(std::move(service),
rockot963ad3e82016-04-13 16:33:23448 mojo::GetProxy(&pid_receiver));
449 pid_receiver->SetPID(base::GetCurrentProcId());
450
bend32292b2016-10-07 00:21:58451 BrowserContextServiceManagerConnectionHolder* connection_holder =
452 new BrowserContextServiceManagerConnectionHolder(
453 service_manager_connection->GetConnector()->Connect(&params),
454 std::move(service_request));
455 browser_context->SetUserData(kServiceManagerConnection, connection_holder);
rockot963ad3e82016-04-13 16:33:23456
bend32292b2016-10-07 00:21:58457 ServiceManagerConnection* connection =
458 connection_holder->service_manager_connection();
rockotcef38272016-07-15 22:47:47459 connection->Start();
rockot963ad3e82016-04-13 16:33:23460
ben146248de2016-06-14 15:24:59461 // New embedded service factories should be added to |connection| here.
blundell372d47d282016-11-01 01:34:30462 // TODO(blundell): Does this belong as a global service rather than per
463 // BrowserContext?
464 ServiceInfo info;
465 info.factory =
466 base::Bind(&device::CreateDeviceService,
467 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE));
468 connection->AddEmbeddedService(device::kDeviceServiceName, info);
rockot963ad3e82016-04-13 16:33:23469
470 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
471 switches::kMojoLocalStorage)) {
bend32292b2016-10-07 00:21:58472 ServiceInfo info;
473 info.factory =
ben768c8dc2016-08-12 00:26:50474 base::Bind(&file::CreateFileService,
thestig529ad8a2016-07-08 20:30:12475 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE),
476 BrowserThread::GetTaskRunnerForThread(BrowserThread::DB));
benaabad0b2016-11-16 23:54:17477 connection->AddEmbeddedService(file::mojom::kServiceName, info);
rockot963ad3e82016-04-13 16:33:23478 }
479 }
erge69130f52016-03-02 00:13:28480}
481
rockot963ad3e82016-04-13 16:33:23482// static
bend32292b2016-10-07 00:21:58483const std::string& BrowserContext::GetServiceUserIdFor(
bencccfe2a2016-03-05 16:54:14484 BrowserContext* browser_context) {
erge69130f52016-03-02 00:13:28485 CHECK(browser_context->GetUserData(kMojoWasInitialized))
486 << "Attempting to get the mojo user id for a BrowserContext that was "
487 << "never Initialize()ed.";
488
bend32292b2016-10-07 00:21:58489 ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>(
490 browser_context->GetUserData(kServiceUserId));
ben6c85c4492016-06-16 20:40:51491 return holder->user_id();
erge69130f52016-03-02 00:13:28492}
493
rockot963ad3e82016-04-13 16:33:23494// static
bend32292b2016-10-07 00:21:58495BrowserContext* BrowserContext::GetBrowserContextForServiceUserId(
ben6c85c4492016-06-16 20:40:51496 const std::string& user_id) {
497 auto it = g_user_id_to_context.Get().find(user_id);
498 return it != g_user_id_to_context.Get().end() ? it->second : nullptr;
499}
500
501// static
rockot400ea35b2016-10-15 19:15:32502service_manager::Connector* BrowserContext::GetConnectorFor(
rockot963ad3e82016-04-13 16:33:23503 BrowserContext* browser_context) {
bend32292b2016-10-07 00:21:58504 ServiceManagerConnection* connection =
505 GetServiceManagerConnectionFor(browser_context);
ben5be0b9132016-08-03 00:17:18506 return connection ? connection->GetConnector() : nullptr;
507}
508
509// static
bend32292b2016-10-07 00:21:58510ServiceManagerConnection* BrowserContext::GetServiceManagerConnectionFor(
ben5be0b9132016-08-03 00:17:18511 BrowserContext* browser_context) {
bend32292b2016-10-07 00:21:58512 BrowserContextServiceManagerConnectionHolder* connection_holder =
513 static_cast<BrowserContextServiceManagerConnectionHolder*>(
514 browser_context->GetUserData(kServiceManagerConnection));
515 return connection_holder ? connection_holder->service_manager_connection()
516 : nullptr;
rockot963ad3e82016-04-13 16:33:23517}
518
[email protected]55eb70e762012-02-20 17:38:39519BrowserContext::~BrowserContext() {
erge69130f52016-03-02 00:13:28520 CHECK(GetUserData(kMojoWasInitialized))
521 << "Attempting to destroy a BrowserContext that never called "
522 << "Initialize()";
523
kinukof6ed359c2016-07-26 13:27:21524 DCHECK(!GetUserData(kStoragePartitionMapKeyName))
525 << "StoragePartitionMap is not shut down properly";
526
ben6c85c4492016-06-16 20:40:51527 RemoveBrowserContextFromUserIdMap(this);
528
[email protected]b441a8492012-06-06 14:55:57529 if (GetUserData(kDownloadManagerKeyName))
530 GetDownloadManager(this)->Shutdown();
[email protected]55eb70e762012-02-20 17:38:39531}
532
kinukof6ed359c2016-07-26 13:27:21533void BrowserContext::ShutdownStoragePartitions() {
534 if (GetUserData(kStoragePartitionMapKeyName))
535 RemoveUserData(kStoragePartitionMapKeyName);
536}
537
[email protected]55eb70e762012-02-20 17:38:39538} // namespace content