blob: fa8b0b7f1e603ea408776987826262e12f6abcdd [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,
Dan Elphick6c0d8492017-09-12 09:35:38121 bool in_memory,
122 bool can_create) {
[email protected]14acc642012-11-17 12:20:10123 StoragePartitionImplMap* partition_map =
124 GetStoragePartitionMap(browser_context);
[email protected]d7c7c98a2012-07-12 21:27:44125
[email protected]1bc28312012-11-08 08:31:53126 if (browser_context->IsOffTheRecord())
127 in_memory = true;
128
Dan Elphick6c0d8492017-09-12 09:35:38129 return partition_map->Get(partition_domain, partition_name, in_memory,
130 can_create);
[email protected]d1198fd2012-08-13 22:50:19131}
132
[email protected]6939075a2012-08-28 08:35:53133void SaveSessionStateOnIOThread(
134 const scoped_refptr<net::URLRequestContextGetter>& context_getter,
[email protected]98d6d4562014-06-25 20:57:55135 AppCacheServiceImpl* appcache_service) {
[email protected]6939075a2012-08-28 08:35:53136 net::URLRequestContext* context = context_getter->GetURLRequestContext();
mmenkeded79da2016-02-06 08:28:51137 context->cookie_store()->SetForceKeepSessionState();
[email protected]6b8a3c742014-07-25 00:25:35138 context->channel_id_service()->GetChannelIDStore()->
[email protected]6939075a2012-08-28 08:35:53139 SetForceKeepSessionState();
140 appcache_service->set_force_keep_session_state();
[email protected]6e2d3d22012-02-24 18:10:36141}
142
[email protected]89acda82013-06-25 20:52:50143void SaveSessionStateOnIndexedDBThread(
[email protected]6e2d3d22012-02-24 18:10:36144 scoped_refptr<IndexedDBContextImpl> indexed_db_context) {
[email protected]bf510ed2012-06-05 08:31:43145 indexed_db_context->SetForceKeepSessionState();
[email protected]6e2d3d22012-02-24 18:10:36146}
147
falken41f4175162014-10-29 07:03:41148void ShutdownServiceWorkerContext(StoragePartition* partition) {
149 ServiceWorkerContextWrapper* wrapper =
150 static_cast<ServiceWorkerContextWrapper*>(
151 partition->GetServiceWorkerContext());
152 wrapper->process_manager()->Shutdown();
153}
154
avi1ed4a4372017-04-25 05:39:41155void SetDownloadManager(
156 BrowserContext* context,
157 std::unique_ptr<content::DownloadManager> download_manager) {
ttr31481dc54b2015-08-06 20:11:26158 DCHECK_CURRENTLY_ON(BrowserThread::UI);
159 DCHECK(download_manager);
avi1ed4a4372017-04-25 05:39:41160 context->SetUserData(kDownloadManagerKeyName, std::move(download_manager));
ttr31481dc54b2015-08-06 20:11:26161}
162
bend32292b2016-10-07 00:21:58163class BrowserContextServiceManagerConnectionHolder
rockot963ad3e82016-04-13 16:33:23164 : public base::SupportsUserData::Data {
165 public:
benaad37ecd2017-04-12 22:08:20166 explicit BrowserContextServiceManagerConnectionHolder(
rockot400ea35b2016-10-15 19:15:32167 service_manager::mojom::ServiceRequest request)
benaad37ecd2017-04-12 22:08:20168 : service_manager_connection_(ServiceManagerConnection::Create(
rockotcef38272016-07-15 22:47:47169 std::move(request),
170 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO))) {}
bend32292b2016-10-07 00:21:58171 ~BrowserContextServiceManagerConnectionHolder() override {}
rockot963ad3e82016-04-13 16:33:23172
bend32292b2016-10-07 00:21:58173 ServiceManagerConnection* service_manager_connection() {
174 return service_manager_connection_.get();
175 }
rockot963ad3e82016-04-13 16:33:23176
177 private:
bend32292b2016-10-07 00:21:58178 std::unique_ptr<ServiceManagerConnection> service_manager_connection_;
rockot963ad3e82016-04-13 16:33:23179
bend32292b2016-10-07 00:21:58180 DISALLOW_COPY_AND_ASSIGN(BrowserContextServiceManagerConnectionHolder);
rockot963ad3e82016-04-13 16:33:23181};
182
[email protected]735e20c2012-03-20 01:16:59183} // namespace
184
[email protected]14acc642012-11-17 12:20:10185// static
186void BrowserContext::AsyncObliterateStoragePartition(
187 BrowserContext* browser_context,
[email protected]399583b2012-12-11 09:33:42188 const GURL& site,
189 const base::Closure& on_gc_required) {
190 GetStoragePartitionMap(browser_context)->AsyncObliterate(site,
191 on_gc_required);
192}
193
194// static
195void BrowserContext::GarbageCollectStoragePartitions(
dcheng59716272016-04-09 05:19:08196 BrowserContext* browser_context,
197 std::unique_ptr<base::hash_set<base::FilePath>> active_paths,
198 const base::Closure& done) {
dcheng36b6aec92015-12-26 06:16:36199 GetStoragePartitionMap(browser_context)
200 ->GarbageCollect(std::move(active_paths), done);
[email protected]14acc642012-11-17 12:20:10201}
202
[email protected]b441a8492012-06-06 14:55:57203DownloadManager* BrowserContext::GetDownloadManager(
204 BrowserContext* context) {
mostynbfbcdc27a2015-03-13 17:58:52205 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]b441a8492012-06-06 14:55:57206 if (!context->GetUserData(kDownloadManagerKeyName)) {
[email protected]eba4a4d2013-05-29 02:18:06207 DownloadManager* download_manager =
[email protected]d25fda12012-06-12 17:05:03208 new DownloadManagerImpl(
[email protected]16798692013-04-23 18:08:38209 GetContentClient()->browser()->GetNetLog(), context);
[email protected]d25fda12012-06-12 17:05:03210
avi1ed4a4372017-04-25 05:39:41211 SetDownloadManager(context, base::WrapUnique(download_manager));
[email protected]b441a8492012-06-06 14:55:57212 download_manager->SetDelegate(context->GetDownloadManagerDelegate());
[email protected]b441a8492012-06-06 14:55:57213 }
214
[email protected]eba4a4d2013-05-29 02:18:06215 return static_cast<DownloadManager*>(
216 context->GetUserData(kDownloadManagerKeyName));
[email protected]b441a8492012-06-06 14:55:57217}
218
[email protected]6ef0c3912013-01-25 22:46:34219// static
[email protected]cd501a72014-08-22 19:58:31220storage::ExternalMountPoints* BrowserContext::GetMountPoints(
[email protected]6ef0c3912013-01-25 22:46:34221 BrowserContext* context) {
222 // Ensure that these methods are called on the UI thread, except for
223 // unittests where a UI thread might not have been created.
224 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
225 !BrowserThread::IsMessageLoopValid(BrowserThread::UI));
226
227#if defined(OS_CHROMEOS)
228 if (!context->GetUserData(kMountPointsKey)) {
[email protected]cd501a72014-08-22 19:58:31229 scoped_refptr<storage::ExternalMountPoints> mount_points =
230 storage::ExternalMountPoints::CreateRefCounted();
[email protected]6ef0c3912013-01-25 22:46:34231 context->SetUserData(
232 kMountPointsKey,
avicb129c02017-05-03 06:49:29233 base::MakeUnique<UserDataAdapter<storage::ExternalMountPoints>>(
234 mount_points.get()));
[email protected]6ef0c3912013-01-25 22:46:34235 }
236
[email protected]cd501a72014-08-22 19:58:31237 return UserDataAdapter<storage::ExternalMountPoints>::Get(context,
238 kMountPointsKey);
[email protected]6ef0c3912013-01-25 22:46:34239#else
240 return NULL;
241#endif
242}
243
msrameke169ccb2017-04-26 05:21:41244// static
245content::BrowsingDataRemover* content::BrowserContext::GetBrowsingDataRemover(
246 BrowserContext* context) {
247 DCHECK_CURRENTLY_ON(BrowserThread::UI);
248
249 if (!context->GetUserData(kBrowsingDataRemoverKey)) {
250 std::unique_ptr<BrowsingDataRemoverImpl> remover =
251 base::MakeUnique<BrowsingDataRemoverImpl>(context);
252 remover->SetEmbedderDelegate(context->GetBrowsingDataRemoverDelegate());
253 context->SetUserData(kBrowsingDataRemoverKey, std::move(remover));
254 }
255
256 return static_cast<BrowsingDataRemoverImpl*>(
257 context->GetUserData(kBrowsingDataRemoverKey));
258}
259
[email protected]4c3a23582012-08-18 08:54:34260StoragePartition* BrowserContext::GetStoragePartition(
261 BrowserContext* browser_context,
Dan Elphick6c0d8492017-09-12 09:35:38262 SiteInstance* site_instance,
263 bool can_create) {
[email protected]1bc28312012-11-08 08:31:53264 std::string partition_domain;
265 std::string partition_name;
266 bool in_memory = false;
[email protected]4c3a23582012-08-18 08:54:34267
[email protected]4c3a23582012-08-18 08:54:34268 if (site_instance) {
[email protected]1bc28312012-11-08 08:31:53269 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
[email protected]14acc642012-11-17 12:20:10270 browser_context, site_instance->GetSiteURL(), true,
[email protected]1bc28312012-11-08 08:31:53271 &partition_domain, &partition_name, &in_memory);
[email protected]4c3a23582012-08-18 08:54:34272 }
273
Dan Elphick6c0d8492017-09-12 09:35:38274 return GetStoragePartitionFromConfig(browser_context, partition_domain,
275 partition_name, in_memory, can_create);
[email protected]4c3a23582012-08-18 08:54:34276}
277
[email protected]e94bbcb2012-09-07 05:33:57278StoragePartition* BrowserContext::GetStoragePartitionForSite(
279 BrowserContext* browser_context,
Dan Elphick6c0d8492017-09-12 09:35:38280 const GURL& site,
281 bool can_create) {
[email protected]1bc28312012-11-08 08:31:53282 std::string partition_domain;
283 std::string partition_name;
284 bool in_memory;
[email protected]e94bbcb2012-09-07 05:33:57285
[email protected]1bc28312012-11-08 08:31:53286 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
[email protected]14acc642012-11-17 12:20:10287 browser_context, site, true, &partition_domain, &partition_name,
288 &in_memory);
[email protected]1bc28312012-11-08 08:31:53289
Dan Elphick6c0d8492017-09-12 09:35:38290 return GetStoragePartitionFromConfig(browser_context, partition_domain,
291 partition_name, in_memory, can_create);
[email protected]e94bbcb2012-09-07 05:33:57292}
293
[email protected]4c3a23582012-08-18 08:54:34294void BrowserContext::ForEachStoragePartition(
295 BrowserContext* browser_context,
296 const StoragePartitionCallback& callback) {
297 StoragePartitionImplMap* partition_map =
298 static_cast<StoragePartitionImplMap*>(
a.cavalcantiffab73762015-08-15 02:55:48299 browser_context->GetUserData(kStoragePartitionMapKeyName));
[email protected]4c3a23582012-08-18 08:54:34300 if (!partition_map)
301 return;
302
303 partition_map->ForEach(callback);
304}
305
306StoragePartition* BrowserContext::GetDefaultStoragePartition(
307 BrowserContext* browser_context) {
308 return GetStoragePartition(browser_context, NULL);
[email protected]55eb70e762012-02-20 17:38:39309}
310
tbarzicdb712682015-03-06 06:05:41311// static
[email protected]393b6cb2014-05-15 00:55:12312void BrowserContext::CreateMemoryBackedBlob(BrowserContext* browser_context,
Marijn Kruisselbrink604fd7e72017-10-26 16:31:05313 const char* data,
314 size_t length,
315 const std::string& content_type,
316 BlobCallback callback) {
mostynbfbcdc27a2015-03-13 17:58:52317 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]393b6cb2014-05-15 00:55:12318
319 ChromeBlobStorageContext* blob_context =
320 ChromeBlobStorageContext::GetFor(browser_context);
321 BrowserThread::PostTaskAndReplyWithResult(
322 BrowserThread::IO, FROM_HERE,
Marijn Kruisselbrink604fd7e72017-10-26 16:31:05323 base::BindOnce(&ChromeBlobStorageContext::CreateMemoryBackedBlob,
324 base::WrapRefCounted(blob_context), data, length,
325 content_type),
326 std::move(callback));
[email protected]393b6cb2014-05-15 00:55:12327}
328
[email protected]66e53d0282014-08-07 10:04:35329// static
tbarzicdb712682015-03-06 06:05:41330void BrowserContext::CreateFileBackedBlob(
331 BrowserContext* browser_context,
332 const base::FilePath& path,
333 int64_t offset,
334 int64_t size,
335 const base::Time& expected_modification_time,
Marijn Kruisselbrink604fd7e72017-10-26 16:31:05336 BlobCallback callback) {
mostynbfbcdc27a2015-03-13 17:58:52337 DCHECK_CURRENTLY_ON(BrowserThread::UI);
tbarzicdb712682015-03-06 06:05:41338
339 ChromeBlobStorageContext* blob_context =
340 ChromeBlobStorageContext::GetFor(browser_context);
341 BrowserThread::PostTaskAndReplyWithResult(
342 BrowserThread::IO, FROM_HERE,
Marijn Kruisselbrink604fd7e72017-10-26 16:31:05343 base::BindOnce(&ChromeBlobStorageContext::CreateFileBackedBlob,
344 base::WrapRefCounted(blob_context), path, offset, size,
345 expected_modification_time),
346 std::move(callback));
tbarzicdb712682015-03-06 06:05:41347}
348
349// static
[email protected]66e53d0282014-08-07 10:04:35350void BrowserContext::DeliverPushMessage(
351 BrowserContext* browser_context,
352 const GURL& origin,
avib7348942015-12-25 20:57:10353 int64_t service_worker_registration_id,
harknessdd4d2b22016-01-27 19:26:43354 const PushEventPayload& payload,
Peter Beverloo7815db1e02017-07-12 19:03:21355 const base::Callback<void(mojom::PushDeliveryStatus)>& callback) {
mostynbfbcdc27a2015-03-13 17:58:52356 DCHECK_CURRENTLY_ON(BrowserThread::UI);
harknessdd4d2b22016-01-27 19:26:43357 PushMessagingRouter::DeliverMessage(browser_context, origin,
358 service_worker_registration_id, payload,
359 callback);
[email protected]66e53d0282014-08-07 10:04:35360}
361
falken41f4175162014-10-29 07:03:41362// static
363void BrowserContext::NotifyWillBeDestroyed(BrowserContext* browser_context) {
364 // Service Workers must shutdown before the browser context is destroyed,
365 // since they keep render process hosts alive and the codebase assumes that
366 // render process hosts die before their profile (browser context) dies.
367 ForEachStoragePartition(browser_context,
368 base::Bind(ShutdownServiceWorkerContext));
falken04a6912a2016-09-23 21:06:29369
370 // Shared workers also keep render process hosts alive, and are expected to
Darin Fisherd3768b2f62017-10-07 01:00:16371 // return ref counts to 0 after documents close. However, to ensure that
372 // hosts are destructed now, forcibly release their ref counts here.
falken04a6912a2016-09-23 21:06:29373 for (RenderProcessHost::iterator host_iterator =
374 RenderProcessHost::AllHostsIterator();
375 !host_iterator.IsAtEnd(); host_iterator.Advance()) {
376 RenderProcessHost* host = host_iterator.GetCurrentValue();
Benoit Lizeab08362c2017-07-19 14:50:54377 if (host->GetBrowserContext() == browser_context) {
378 // This will also clean up spare RPH references.
Yutaka Hirano09a9afb92017-08-16 09:11:45379 host->DisableKeepAliveRefCount();
Benoit Lizeab08362c2017-07-19 14:50:54380 }
falken04a6912a2016-09-23 21:06:29381 }
falken41f4175162014-10-29 07:03:41382}
383
[email protected]314c3e22012-02-21 03:57:42384void BrowserContext::EnsureResourceContextInitialized(BrowserContext* context) {
[email protected]7e26ac92012-02-27 20:15:05385 // This will be enough to tickle initialization of BrowserContext if
386 // necessary, which initializes ResourceContext. The reason we don't call
[email protected]4c3a23582012-08-18 08:54:34387 // ResourceContext::InitializeResourceContext() directly here is that
388 // ResourceContext initialization may call back into BrowserContext
389 // and when that call returns it'll end rewriting its UserData map. It will
390 // end up rewriting the same value but this still causes a race condition.
391 //
392 // See http://crbug.com/115678.
393 GetDefaultStoragePartition(context);
[email protected]55eb70e762012-02-20 17:38:39394}
395
[email protected]6e2d3d22012-02-24 18:10:36396void BrowserContext::SaveSessionState(BrowserContext* browser_context) {
[email protected]b1b502e2012-09-16 07:31:43397 StoragePartition* storage_partition =
398 BrowserContext::GetDefaultStoragePartition(browser_context);
[email protected]6e2d3d22012-02-24 18:10:36399
Joshua Bell607cb142017-07-24 19:17:16400 storage::DatabaseTracker* database_tracker =
401 storage_partition->GetDatabaseTracker();
402 database_tracker->task_runner()->PostTask(
403 FROM_HERE,
404 base::BindOnce(&storage::DatabaseTracker::SetForceKeepSessionState,
kylecharda69d882017-10-04 05:49:52405 base::WrapRefCounted(database_tracker)));
Joshua Bell607cb142017-07-24 19:17:16406
[email protected]6e2d3d22012-02-24 18:10:36407 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) {
408 BrowserThread::PostTask(
409 BrowserThread::IO, FROM_HERE,
tzik4fea24af2017-08-23 11:41:47410 base::BindOnce(
[email protected]6939075a2012-08-28 08:35:53411 &SaveSessionStateOnIOThread,
kylecharda69d882017-10-04 05:49:52412 base::WrapRefCounted(
tzik4fea24af2017-08-23 11:41:47413 BrowserContext::GetDefaultStoragePartition(browser_context)
414 ->GetURLRequestContext()),
[email protected]98d6d4562014-06-25 20:57:55415 static_cast<AppCacheServiceImpl*>(
[email protected]63ef85512014-06-05 14:21:26416 storage_partition->GetAppCacheService())));
[email protected]6e2d3d22012-02-24 18:10:36417 }
418
[email protected]5f2aa722013-08-07 16:59:41419 DOMStorageContextWrapper* dom_storage_context_proxy =
420 static_cast<DOMStorageContextWrapper*>(
[email protected]b1b502e2012-09-16 07:31:43421 storage_partition->GetDOMStorageContext());
[email protected]5f2aa722013-08-07 16:59:41422 dom_storage_context_proxy->SetForceKeepSessionState();
[email protected]735e20c2012-03-20 01:16:59423
[email protected]89acda82013-06-25 20:52:50424 IndexedDBContextImpl* indexed_db_context_impl =
425 static_cast<IndexedDBContextImpl*>(
[email protected]b1b502e2012-09-16 07:31:43426 storage_partition->GetIndexedDBContext());
[email protected]89acda82013-06-25 20:52:50427 // No task runner in unit tests.
428 if (indexed_db_context_impl->TaskRunner()) {
429 indexed_db_context_impl->TaskRunner()->PostTask(
kylecharda69d882017-10-04 05:49:52430 FROM_HERE,
431 base::BindOnce(&SaveSessionStateOnIndexedDBThread,
432 base::WrapRefCounted(indexed_db_context_impl)));
[email protected]6e2d3d22012-02-24 18:10:36433 }
434}
435
ttr31481dc54b2015-08-06 20:11:26436void BrowserContext::SetDownloadManagerForTesting(
437 BrowserContext* browser_context,
avi1ed4a4372017-04-25 05:39:41438 std::unique_ptr<content::DownloadManager> download_manager) {
439 SetDownloadManager(browser_context, std::move(download_manager));
ttr31481dc54b2015-08-06 20:11:26440}
441
rockot963ad3e82016-04-13 16:33:23442// static
erge69130f52016-03-02 00:13:28443void BrowserContext::Initialize(
444 BrowserContext* browser_context,
445 const base::FilePath& path) {
ben6c85c4492016-06-16 20:40:51446
447 std::string new_id;
448 if (GetContentClient() && GetContentClient()->browser()) {
bend32292b2016-10-07 00:21:58449 new_id = GetContentClient()->browser()->GetServiceUserIdForBrowserContext(
ben6c85c4492016-06-16 20:40:51450 browser_context);
451 } else {
452 // Some test scenarios initialize a BrowserContext without a content client.
bencccfe2a2016-03-05 16:54:14453 new_id = base::GenerateGUID();
ben6c85c4492016-06-16 20:40:51454 }
erge69130f52016-03-02 00:13:28455
bend32292b2016-10-07 00:21:58456 ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>(
457 browser_context->GetUserData(kServiceUserId));
ben6c85c4492016-06-16 20:40:51458 if (holder)
bend32292b2016-10-07 00:21:58459 file::ForgetServiceUserIdUserDirAssociation(holder->user_id());
460 file::AssociateServiceUserIdWithUserDir(new_id, path);
ben6c85c4492016-06-16 20:40:51461 RemoveBrowserContextFromUserIdMap(browser_context);
462 g_user_id_to_context.Get()[new_id] = browser_context;
bend32292b2016-10-07 00:21:58463 browser_context->SetUserData(kServiceUserId,
avi1ed4a4372017-04-25 05:39:41464 base::MakeUnique<ServiceUserIdHolder>(new_id));
erge69130f52016-03-02 00:13:28465
avi1ed4a4372017-04-25 05:39:41466 browser_context->SetUserData(
467 kMojoWasInitialized, base::MakeUnique<base::SupportsUserData::Data>());
rockot963ad3e82016-04-13 16:33:23468
bend32292b2016-10-07 00:21:58469 ServiceManagerConnection* service_manager_connection =
470 ServiceManagerConnection::GetForProcess();
fdoray4f155f02016-10-12 11:28:50471 if (service_manager_connection && base::ThreadTaskRunnerHandle::IsSet()) {
rockot963ad3e82016-04-13 16:33:23472 // NOTE: Many unit tests create a TestBrowserContext without initializing
bend32292b2016-10-07 00:21:58473 // Mojo or the global service manager connection.
rockot963ad3e82016-04-13 16:33:23474
rockot400ea35b2016-10-15 19:15:32475 service_manager::mojom::ServicePtr service;
Ken Rockotf4d8a942017-05-13 00:10:37476 auto service_request = mojo::MakeRequest(&service);
rockot963ad3e82016-04-13 16:33:23477
rockot400ea35b2016-10-15 19:15:32478 service_manager::mojom::PIDReceiverPtr pid_receiver;
bene6a9f012017-01-07 00:43:43479 service_manager::Identity identity(mojom::kBrowserServiceName, new_id);
benbd3c2482017-01-07 05:48:21480 service_manager_connection->GetConnector()->StartService(
bene6a9f012017-01-07 00:43:43481 identity, std::move(service), mojo::MakeRequest(&pid_receiver));
rockot963ad3e82016-04-13 16:33:23482 pid_receiver->SetPID(base::GetCurrentProcId());
483
benaad37ecd2017-04-12 22:08:20484 service_manager_connection->GetConnector()->StartService(identity);
bend32292b2016-10-07 00:21:58485 BrowserContextServiceManagerConnectionHolder* connection_holder =
486 new BrowserContextServiceManagerConnectionHolder(
bend32292b2016-10-07 00:21:58487 std::move(service_request));
avi1ed4a4372017-04-25 05:39:41488 browser_context->SetUserData(kServiceManagerConnection,
489 base::WrapUnique(connection_holder));
rockot963ad3e82016-04-13 16:33:23490
bend32292b2016-10-07 00:21:58491 ServiceManagerConnection* connection =
492 connection_holder->service_manager_connection();
rockot963ad3e82016-04-13 16:33:23493
ben146248de2016-06-14 15:24:59494 // New embedded service factories should be added to |connection| here.
rockot963ad3e82016-04-13 16:33:23495
mekc8e2b272017-06-06 23:18:28496 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
497 switches::kDisableMojoLocalStorage)) {
Colin Blundell812ec6802017-06-27 15:50:49498 service_manager::EmbeddedServiceInfo info;
Marijn Kruisselbrinkcbc3df82017-07-06 19:25:59499 info.factory = base::Bind(&file::CreateFileService);
benaabad0b2016-11-16 23:54:17500 connection->AddEmbeddedService(file::mojom::kServiceName, info);
rockot963ad3e82016-04-13 16:33:23501 }
tibellab3d36b2017-03-10 02:57:25502
503 ContentBrowserClient::StaticServiceMap services;
504 browser_context->RegisterInProcessServices(&services);
505 for (const auto& entry : services) {
506 connection->AddEmbeddedService(entry.first, entry.second);
507 }
Ken Rockotc7a279c2017-05-04 23:51:45508
509 RegisterCommonBrowserInterfaces(connection);
sammc030f5012017-03-13 02:28:45510 connection->Start();
rockot963ad3e82016-04-13 16:33:23511 }
erge69130f52016-03-02 00:13:28512}
513
rockot963ad3e82016-04-13 16:33:23514// static
bend32292b2016-10-07 00:21:58515const std::string& BrowserContext::GetServiceUserIdFor(
bencccfe2a2016-03-05 16:54:14516 BrowserContext* browser_context) {
erge69130f52016-03-02 00:13:28517 CHECK(browser_context->GetUserData(kMojoWasInitialized))
518 << "Attempting to get the mojo user id for a BrowserContext that was "
519 << "never Initialize()ed.";
520
bend32292b2016-10-07 00:21:58521 ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>(
522 browser_context->GetUserData(kServiceUserId));
ben6c85c4492016-06-16 20:40:51523 return holder->user_id();
erge69130f52016-03-02 00:13:28524}
525
rockot963ad3e82016-04-13 16:33:23526// static
bend32292b2016-10-07 00:21:58527BrowserContext* BrowserContext::GetBrowserContextForServiceUserId(
ben6c85c4492016-06-16 20:40:51528 const std::string& user_id) {
529 auto it = g_user_id_to_context.Get().find(user_id);
530 return it != g_user_id_to_context.Get().end() ? it->second : nullptr;
531}
532
533// static
rockot400ea35b2016-10-15 19:15:32534service_manager::Connector* BrowserContext::GetConnectorFor(
rockot963ad3e82016-04-13 16:33:23535 BrowserContext* browser_context) {
bend32292b2016-10-07 00:21:58536 ServiceManagerConnection* connection =
537 GetServiceManagerConnectionFor(browser_context);
ben5be0b9132016-08-03 00:17:18538 return connection ? connection->GetConnector() : nullptr;
539}
540
541// static
bend32292b2016-10-07 00:21:58542ServiceManagerConnection* BrowserContext::GetServiceManagerConnectionFor(
ben5be0b9132016-08-03 00:17:18543 BrowserContext* browser_context) {
bend32292b2016-10-07 00:21:58544 BrowserContextServiceManagerConnectionHolder* connection_holder =
545 static_cast<BrowserContextServiceManagerConnectionHolder*>(
546 browser_context->GetUserData(kServiceManagerConnection));
547 return connection_holder ? connection_holder->service_manager_connection()
548 : nullptr;
rockot963ad3e82016-04-13 16:33:23549}
550
mmenkec0b2b8b12017-04-21 16:27:52551BrowserContext::BrowserContext()
552 : media_device_id_salt_(CreateRandomMediaDeviceIDSalt()) {}
553
[email protected]55eb70e762012-02-20 17:38:39554BrowserContext::~BrowserContext() {
erge69130f52016-03-02 00:13:28555 CHECK(GetUserData(kMojoWasInitialized))
556 << "Attempting to destroy a BrowserContext that never called "
557 << "Initialize()";
558
kinukof6ed359c2016-07-26 13:27:21559 DCHECK(!GetUserData(kStoragePartitionMapKeyName))
560 << "StoragePartitionMap is not shut down properly";
561
ben6c85c4492016-06-16 20:40:51562 RemoveBrowserContextFromUserIdMap(this);
563
[email protected]b441a8492012-06-06 14:55:57564 if (GetUserData(kDownloadManagerKeyName))
565 GetDownloadManager(this)->Shutdown();
[email protected]55eb70e762012-02-20 17:38:39566}
567
kinukof6ed359c2016-07-26 13:27:21568void BrowserContext::ShutdownStoragePartitions() {
569 if (GetUserData(kStoragePartitionMapKeyName))
570 RemoveUserData(kStoragePartitionMapKeyName);
571}
572
mmenkec0b2b8b12017-04-21 16:27:52573std::string BrowserContext::GetMediaDeviceIDSalt() {
574 return media_device_id_salt_;
575}
576
577// static
578std::string BrowserContext::CreateRandomMediaDeviceIDSalt() {
579 std::string salt;
580 base::Base64Encode(base::RandBytesAsString(16), &salt);
581 DCHECK(!salt.empty());
582 return salt;
583}
584
[email protected]55eb70e762012-02-20 17:38:39585} // namespace content