blob: 3f68fd7ddb74ddb39d146c12e7841884b94b6507 [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"
ben21a34c252016-06-29 22:24:3724#include "base/threading/thread_task_runner_handle.h"
avib7348942015-12-25 20:57:1025#include "build/build_config.h"
dmurph7ac019a2016-05-13 00:13:1726#include "content/browser/blob_storage/chrome_blob_storage_context.h"
msrameke169ccb2017-04-26 05:21:4127#include "content/browser/browsing_data/browsing_data_remover_impl.h"
[email protected]b441a8492012-06-06 14:55:5728#include "content/browser/download/download_manager_impl.h"
[email protected]c4d281662013-03-31 00:35:0829#include "content/browser/indexed_db/indexed_db_context_impl.h"
[email protected]678c0362012-12-05 08:02:4430#include "content/browser/loader/resource_dispatcher_host_impl.h"
mvanouwerkerk17205ea2014-11-07 17:30:1531#include "content/browser/push_messaging/push_messaging_router.h"
Ken Rockotc7a279c2017-05-04 23:51:4532#include "content/browser/service_manager/common_browser_interfaces.h"
[email protected]4c3a23582012-08-18 08:54:3433#include "content/browser/storage_partition_impl_map.h"
[email protected]d7c7c98a2012-07-12 21:27:4434#include "content/common/child_process_host_impl.h"
[email protected]393b6cb2014-05-15 00:55:1235#include "content/public/browser/blob_handle.h"
[email protected]55eb70e762012-02-20 17:38:3936#include "content/public/browser/browser_thread.h"
[email protected]b441a8492012-06-06 14:55:5737#include "content/public/browser/content_browser_client.h"
falken04a6912a2016-09-23 21:06:2938#include "content/public/browser/render_process_host.h"
[email protected]536fd0b2013-03-14 17:41:5739#include "content/public/browser/site_instance.h"
rockot963ad3e82016-04-13 16:33:2340#include "content/public/common/content_switches.h"
bend32292b2016-10-07 00:21:5841#include "content/public/common/service_manager_connection.h"
ben51bb6c62016-11-17 20:15:5742#include "content/public/common/service_names.mojom.h"
[email protected]4d7c4ef2012-03-16 01:47:1243#include "net/cookies/cookie_store.h"
[email protected]6b8a3c742014-07-25 00:25:3544#include "net/ssl/channel_id_service.h"
45#include "net/ssl/channel_id_store.h"
[email protected]6e2d3d22012-02-24 18:10:3646#include "net/url_request/url_request_context.h"
[email protected]6939075a2012-08-28 08:35:5347#include "net/url_request/url_request_context_getter.h"
ben768c8dc2016-08-12 00:26:5048#include "services/file/file_service.h"
benaabad0b2016-11-16 23:54:1749#include "services/file/public/interfaces/constants.mojom.h"
ben768c8dc2016-08-12 00:26:5050#include "services/file/user_id_map.h"
rockot734fb662016-10-15 16:41:3051#include "services/service_manager/public/cpp/connector.h"
52#include "services/service_manager/public/interfaces/service.mojom.h"
pilgrime92c5fcd2014-09-10 23:31:2353#include "storage/browser/database/database_tracker.h"
54#include "storage/browser/fileapi/external_mount_points.h"
[email protected]55eb70e762012-02-20 17:38:3955
[email protected]314c3e22012-02-21 03:57:4256using base::UserDataAdapter;
[email protected]55eb70e762012-02-20 17:38:3957
[email protected]55eb70e762012-02-20 17:38:3958namespace content {
59
[email protected]735e20c2012-03-20 01:16:5960namespace {
61
scottmg5e65e3a2017-03-08 08:48:4662base::LazyInstance<std::map<std::string, BrowserContext*>>::DestructorAtExit
ben6c85c4492016-06-16 20:40:5163 g_user_id_to_context = LAZY_INSTANCE_INITIALIZER;
64
bend32292b2016-10-07 00:21:5865class ServiceUserIdHolder : public base::SupportsUserData::Data {
ben6c85c4492016-06-16 20:40:5166 public:
bend32292b2016-10-07 00:21:5867 explicit ServiceUserIdHolder(const std::string& user_id)
68 : user_id_(user_id) {}
69 ~ServiceUserIdHolder() override {}
ben6c85c4492016-06-16 20:40:5170
71 const std::string& user_id() const { return user_id_; }
72
73 private:
74 std::string user_id_;
75
bend32292b2016-10-07 00:21:5876 DISALLOW_COPY_AND_ASSIGN(ServiceUserIdHolder);
ben6c85c4492016-06-16 20:40:5177};
erge69130f52016-03-02 00:13:2878
[email protected]e0ce8a1e2012-09-18 10:26:3679// Key names on BrowserContext.
msrameke169ccb2017-04-26 05:21:4180const char kBrowsingDataRemoverKey[] = "browsing-data-remover";
[email protected]6ef0c3912013-01-25 22:46:3481const char kDownloadManagerKeyName[] = "download_manager";
erge69130f52016-03-02 00:13:2882const char kMojoWasInitialized[] = "mojo-was-initialized";
bend32292b2016-10-07 00:21:5883const char kServiceManagerConnection[] = "service-manager-connection";
84const char kServiceUserId[] = "service-user-id";
rockot963ad3e82016-04-13 16:33:2385const char kStoragePartitionMapKeyName[] = "content_storage_partition_map";
erge69130f52016-03-02 00:13:2886
[email protected]9afc14e22013-09-25 22:34:1487#if defined(OS_CHROMEOS)
88const char kMountPointsKey[] = "mount_points";
89#endif // defined(OS_CHROMEOS)
90
ben6c85c4492016-06-16 20:40:5191void RemoveBrowserContextFromUserIdMap(BrowserContext* browser_context) {
bend32292b2016-10-07 00:21:5892 ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>(
93 browser_context->GetUserData(kServiceUserId));
ben6c85c4492016-06-16 20:40:5194 if (holder) {
95 auto it = g_user_id_to_context.Get().find(holder->user_id());
96 if (it != g_user_id_to_context.Get().end())
97 g_user_id_to_context.Get().erase(it);
98 }
99}
100
[email protected]14acc642012-11-17 12:20:10101StoragePartitionImplMap* GetStoragePartitionMap(
102 BrowserContext* browser_context) {
[email protected]4c3a23582012-08-18 08:54:34103 StoragePartitionImplMap* partition_map =
104 static_cast<StoragePartitionImplMap*>(
a.cavalcantiffab73762015-08-15 02:55:48105 browser_context->GetUserData(kStoragePartitionMapKeyName));
[email protected]d7c7c98a2012-07-12 21:27:44106 if (!partition_map) {
avi1ed4a4372017-04-25 05:39:41107 auto partition_map_owned =
108 base::MakeUnique<StoragePartitionImplMap>(browser_context);
109 partition_map = partition_map_owned.get();
110 browser_context->SetUserData(kStoragePartitionMapKeyName,
111 std::move(partition_map_owned));
[email protected]d7c7c98a2012-07-12 21:27:44112 }
[email protected]14acc642012-11-17 12:20:10113 return partition_map;
114}
115
116StoragePartition* GetStoragePartitionFromConfig(
117 BrowserContext* browser_context,
118 const std::string& partition_domain,
119 const std::string& partition_name,
120 bool in_memory) {
121 StoragePartitionImplMap* partition_map =
122 GetStoragePartitionMap(browser_context);
[email protected]d7c7c98a2012-07-12 21:27:44123
[email protected]1bc28312012-11-08 08:31:53124 if (browser_context->IsOffTheRecord())
125 in_memory = true;
126
[email protected]1bc28312012-11-08 08:31:53127 return partition_map->Get(partition_domain, partition_name, in_memory);
[email protected]d1198fd2012-08-13 22:50:19128}
129
[email protected]6939075a2012-08-28 08:35:53130void SaveSessionStateOnIOThread(
131 const scoped_refptr<net::URLRequestContextGetter>& context_getter,
[email protected]98d6d4562014-06-25 20:57:55132 AppCacheServiceImpl* appcache_service) {
[email protected]6939075a2012-08-28 08:35:53133 net::URLRequestContext* context = context_getter->GetURLRequestContext();
mmenkeded79da2016-02-06 08:28:51134 context->cookie_store()->SetForceKeepSessionState();
[email protected]6b8a3c742014-07-25 00:25:35135 context->channel_id_service()->GetChannelIDStore()->
[email protected]6939075a2012-08-28 08:35:53136 SetForceKeepSessionState();
137 appcache_service->set_force_keep_session_state();
[email protected]6e2d3d22012-02-24 18:10:36138}
139
[email protected]89acda82013-06-25 20:52:50140void SaveSessionStateOnIndexedDBThread(
[email protected]6e2d3d22012-02-24 18:10:36141 scoped_refptr<IndexedDBContextImpl> indexed_db_context) {
[email protected]bf510ed2012-06-05 08:31:43142 indexed_db_context->SetForceKeepSessionState();
[email protected]6e2d3d22012-02-24 18:10:36143}
144
falken41f4175162014-10-29 07:03:41145void ShutdownServiceWorkerContext(StoragePartition* partition) {
146 ServiceWorkerContextWrapper* wrapper =
147 static_cast<ServiceWorkerContextWrapper*>(
148 partition->GetServiceWorkerContext());
149 wrapper->process_manager()->Shutdown();
150}
151
avi1ed4a4372017-04-25 05:39:41152void SetDownloadManager(
153 BrowserContext* context,
154 std::unique_ptr<content::DownloadManager> download_manager) {
ttr31481dc54b2015-08-06 20:11:26155 DCHECK_CURRENTLY_ON(BrowserThread::UI);
156 DCHECK(download_manager);
avi1ed4a4372017-04-25 05:39:41157 context->SetUserData(kDownloadManagerKeyName, std::move(download_manager));
ttr31481dc54b2015-08-06 20:11:26158}
159
bend32292b2016-10-07 00:21:58160class BrowserContextServiceManagerConnectionHolder
rockot963ad3e82016-04-13 16:33:23161 : public base::SupportsUserData::Data {
162 public:
benaad37ecd2017-04-12 22:08:20163 explicit BrowserContextServiceManagerConnectionHolder(
rockot400ea35b2016-10-15 19:15:32164 service_manager::mojom::ServiceRequest request)
benaad37ecd2017-04-12 22:08:20165 : service_manager_connection_(ServiceManagerConnection::Create(
rockotcef38272016-07-15 22:47:47166 std::move(request),
167 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO))) {}
bend32292b2016-10-07 00:21:58168 ~BrowserContextServiceManagerConnectionHolder() override {}
rockot963ad3e82016-04-13 16:33:23169
bend32292b2016-10-07 00:21:58170 ServiceManagerConnection* service_manager_connection() {
171 return service_manager_connection_.get();
172 }
rockot963ad3e82016-04-13 16:33:23173
174 private:
bend32292b2016-10-07 00:21:58175 std::unique_ptr<ServiceManagerConnection> service_manager_connection_;
rockot963ad3e82016-04-13 16:33:23176
bend32292b2016-10-07 00:21:58177 DISALLOW_COPY_AND_ASSIGN(BrowserContextServiceManagerConnectionHolder);
rockot963ad3e82016-04-13 16:33:23178};
179
[email protected]735e20c2012-03-20 01:16:59180} // namespace
181
[email protected]14acc642012-11-17 12:20:10182// static
183void BrowserContext::AsyncObliterateStoragePartition(
184 BrowserContext* browser_context,
[email protected]399583b2012-12-11 09:33:42185 const GURL& site,
186 const base::Closure& on_gc_required) {
187 GetStoragePartitionMap(browser_context)->AsyncObliterate(site,
188 on_gc_required);
189}
190
191// static
192void BrowserContext::GarbageCollectStoragePartitions(
dcheng59716272016-04-09 05:19:08193 BrowserContext* browser_context,
194 std::unique_ptr<base::hash_set<base::FilePath>> active_paths,
195 const base::Closure& done) {
dcheng36b6aec92015-12-26 06:16:36196 GetStoragePartitionMap(browser_context)
197 ->GarbageCollect(std::move(active_paths), done);
[email protected]14acc642012-11-17 12:20:10198}
199
[email protected]b441a8492012-06-06 14:55:57200DownloadManager* BrowserContext::GetDownloadManager(
201 BrowserContext* context) {
mostynbfbcdc27a2015-03-13 17:58:52202 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]b441a8492012-06-06 14:55:57203 if (!context->GetUserData(kDownloadManagerKeyName)) {
[email protected]eba4a4d2013-05-29 02:18:06204 DownloadManager* download_manager =
[email protected]d25fda12012-06-12 17:05:03205 new DownloadManagerImpl(
[email protected]16798692013-04-23 18:08:38206 GetContentClient()->browser()->GetNetLog(), context);
[email protected]d25fda12012-06-12 17:05:03207
avi1ed4a4372017-04-25 05:39:41208 SetDownloadManager(context, base::WrapUnique(download_manager));
[email protected]b441a8492012-06-06 14:55:57209 download_manager->SetDelegate(context->GetDownloadManagerDelegate());
[email protected]b441a8492012-06-06 14:55:57210 }
211
[email protected]eba4a4d2013-05-29 02:18:06212 return static_cast<DownloadManager*>(
213 context->GetUserData(kDownloadManagerKeyName));
[email protected]b441a8492012-06-06 14:55:57214}
215
[email protected]6ef0c3912013-01-25 22:46:34216// static
[email protected]cd501a72014-08-22 19:58:31217storage::ExternalMountPoints* BrowserContext::GetMountPoints(
[email protected]6ef0c3912013-01-25 22:46:34218 BrowserContext* context) {
219 // Ensure that these methods are called on the UI thread, except for
220 // unittests where a UI thread might not have been created.
221 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
222 !BrowserThread::IsMessageLoopValid(BrowserThread::UI));
223
224#if defined(OS_CHROMEOS)
225 if (!context->GetUserData(kMountPointsKey)) {
[email protected]cd501a72014-08-22 19:58:31226 scoped_refptr<storage::ExternalMountPoints> mount_points =
227 storage::ExternalMountPoints::CreateRefCounted();
[email protected]6ef0c3912013-01-25 22:46:34228 context->SetUserData(
229 kMountPointsKey,
avicb129c02017-05-03 06:49:29230 base::MakeUnique<UserDataAdapter<storage::ExternalMountPoints>>(
231 mount_points.get()));
[email protected]6ef0c3912013-01-25 22:46:34232 }
233
[email protected]cd501a72014-08-22 19:58:31234 return UserDataAdapter<storage::ExternalMountPoints>::Get(context,
235 kMountPointsKey);
[email protected]6ef0c3912013-01-25 22:46:34236#else
237 return NULL;
238#endif
239}
240
msrameke169ccb2017-04-26 05:21:41241// static
242content::BrowsingDataRemover* content::BrowserContext::GetBrowsingDataRemover(
243 BrowserContext* context) {
244 DCHECK_CURRENTLY_ON(BrowserThread::UI);
245
246 if (!context->GetUserData(kBrowsingDataRemoverKey)) {
247 std::unique_ptr<BrowsingDataRemoverImpl> remover =
248 base::MakeUnique<BrowsingDataRemoverImpl>(context);
249 remover->SetEmbedderDelegate(context->GetBrowsingDataRemoverDelegate());
250 context->SetUserData(kBrowsingDataRemoverKey, std::move(remover));
251 }
252
253 return static_cast<BrowsingDataRemoverImpl*>(
254 context->GetUserData(kBrowsingDataRemoverKey));
255}
256
[email protected]4c3a23582012-08-18 08:54:34257StoragePartition* BrowserContext::GetStoragePartition(
258 BrowserContext* browser_context,
259 SiteInstance* site_instance) {
[email protected]1bc28312012-11-08 08:31:53260 std::string partition_domain;
261 std::string partition_name;
262 bool in_memory = false;
[email protected]4c3a23582012-08-18 08:54:34263
[email protected]4c3a23582012-08-18 08:54:34264 if (site_instance) {
[email protected]1bc28312012-11-08 08:31:53265 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
[email protected]14acc642012-11-17 12:20:10266 browser_context, site_instance->GetSiteURL(), true,
[email protected]1bc28312012-11-08 08:31:53267 &partition_domain, &partition_name, &in_memory);
[email protected]4c3a23582012-08-18 08:54:34268 }
269
[email protected]1bc28312012-11-08 08:31:53270 return GetStoragePartitionFromConfig(
271 browser_context, partition_domain, partition_name, in_memory);
[email protected]4c3a23582012-08-18 08:54:34272}
273
[email protected]e94bbcb2012-09-07 05:33:57274StoragePartition* BrowserContext::GetStoragePartitionForSite(
275 BrowserContext* browser_context,
276 const GURL& site) {
[email protected]1bc28312012-11-08 08:31:53277 std::string partition_domain;
278 std::string partition_name;
279 bool in_memory;
[email protected]e94bbcb2012-09-07 05:33:57280
[email protected]1bc28312012-11-08 08:31:53281 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
[email protected]14acc642012-11-17 12:20:10282 browser_context, site, true, &partition_domain, &partition_name,
283 &in_memory);
[email protected]1bc28312012-11-08 08:31:53284
285 return GetStoragePartitionFromConfig(
286 browser_context, partition_domain, partition_name, in_memory);
[email protected]e94bbcb2012-09-07 05:33:57287}
288
[email protected]4c3a23582012-08-18 08:54:34289void BrowserContext::ForEachStoragePartition(
290 BrowserContext* browser_context,
291 const StoragePartitionCallback& callback) {
292 StoragePartitionImplMap* partition_map =
293 static_cast<StoragePartitionImplMap*>(
a.cavalcantiffab73762015-08-15 02:55:48294 browser_context->GetUserData(kStoragePartitionMapKeyName));
[email protected]4c3a23582012-08-18 08:54:34295 if (!partition_map)
296 return;
297
298 partition_map->ForEach(callback);
299}
300
301StoragePartition* BrowserContext::GetDefaultStoragePartition(
302 BrowserContext* browser_context) {
303 return GetStoragePartition(browser_context, NULL);
[email protected]55eb70e762012-02-20 17:38:39304}
305
tbarzicdb712682015-03-06 06:05:41306// static
[email protected]393b6cb2014-05-15 00:55:12307void BrowserContext::CreateMemoryBackedBlob(BrowserContext* browser_context,
308 const char* data, size_t length,
309 const BlobCallback& callback) {
mostynbfbcdc27a2015-03-13 17:58:52310 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]393b6cb2014-05-15 00:55:12311
312 ChromeBlobStorageContext* blob_context =
313 ChromeBlobStorageContext::GetFor(browser_context);
314 BrowserThread::PostTaskAndReplyWithResult(
315 BrowserThread::IO, FROM_HERE,
316 base::Bind(&ChromeBlobStorageContext::CreateMemoryBackedBlob,
317 make_scoped_refptr(blob_context), data, length),
318 callback);
319}
320
[email protected]66e53d0282014-08-07 10:04:35321// static
tbarzicdb712682015-03-06 06:05:41322void BrowserContext::CreateFileBackedBlob(
323 BrowserContext* browser_context,
324 const base::FilePath& path,
325 int64_t offset,
326 int64_t size,
327 const base::Time& expected_modification_time,
328 const BlobCallback& callback) {
mostynbfbcdc27a2015-03-13 17:58:52329 DCHECK_CURRENTLY_ON(BrowserThread::UI);
tbarzicdb712682015-03-06 06:05:41330
331 ChromeBlobStorageContext* blob_context =
332 ChromeBlobStorageContext::GetFor(browser_context);
333 BrowserThread::PostTaskAndReplyWithResult(
334 BrowserThread::IO, FROM_HERE,
335 base::Bind(&ChromeBlobStorageContext::CreateFileBackedBlob,
336 make_scoped_refptr(blob_context), path, offset, size,
337 expected_modification_time),
338 callback);
339}
340
341// static
[email protected]66e53d0282014-08-07 10:04:35342void BrowserContext::DeliverPushMessage(
343 BrowserContext* browser_context,
344 const GURL& origin,
avib7348942015-12-25 20:57:10345 int64_t service_worker_registration_id,
harknessdd4d2b22016-01-27 19:26:43346 const PushEventPayload& payload,
johnmea80c2552014-10-17 14:51:40347 const base::Callback<void(PushDeliveryStatus)>& callback) {
mostynbfbcdc27a2015-03-13 17:58:52348 DCHECK_CURRENTLY_ON(BrowserThread::UI);
harknessdd4d2b22016-01-27 19:26:43349 PushMessagingRouter::DeliverMessage(browser_context, origin,
350 service_worker_registration_id, payload,
351 callback);
[email protected]66e53d0282014-08-07 10:04:35352}
353
falken41f4175162014-10-29 07:03:41354// static
355void BrowserContext::NotifyWillBeDestroyed(BrowserContext* browser_context) {
356 // Service Workers must shutdown before the browser context is destroyed,
357 // since they keep render process hosts alive and the codebase assumes that
358 // render process hosts die before their profile (browser context) dies.
359 ForEachStoragePartition(browser_context,
360 base::Bind(ShutdownServiceWorkerContext));
falken04a6912a2016-09-23 21:06:29361
362 // Shared workers also keep render process hosts alive, and are expected to
363 // return ref counts to 0 after documents close. However, shared worker
364 // bookkeeping is done on the IO thread and we want to ensure the hosts are
365 // destructed now, so forcibly release their ref counts here.
366 for (RenderProcessHost::iterator host_iterator =
367 RenderProcessHost::AllHostsIterator();
368 !host_iterator.IsAtEnd(); host_iterator.Advance()) {
369 RenderProcessHost* host = host_iterator.GetCurrentValue();
370 if (host->GetBrowserContext() == browser_context)
371 host->ForceReleaseWorkerRefCounts();
372 }
falken41f4175162014-10-29 07:03:41373}
374
[email protected]314c3e22012-02-21 03:57:42375void BrowserContext::EnsureResourceContextInitialized(BrowserContext* context) {
[email protected]7e26ac92012-02-27 20:15:05376 // This will be enough to tickle initialization of BrowserContext if
377 // necessary, which initializes ResourceContext. The reason we don't call
[email protected]4c3a23582012-08-18 08:54:34378 // ResourceContext::InitializeResourceContext() directly here is that
379 // ResourceContext initialization may call back into BrowserContext
380 // and when that call returns it'll end rewriting its UserData map. It will
381 // end up rewriting the same value but this still causes a race condition.
382 //
383 // See http://crbug.com/115678.
384 GetDefaultStoragePartition(context);
[email protected]55eb70e762012-02-20 17:38:39385}
386
[email protected]6e2d3d22012-02-24 18:10:36387void BrowserContext::SaveSessionState(BrowserContext* browser_context) {
[email protected]5c8e67c2012-08-29 00:48:52388 GetDefaultStoragePartition(browser_context)->GetDatabaseTracker()->
389 SetForceKeepSessionState();
[email protected]b1b502e2012-09-16 07:31:43390 StoragePartition* storage_partition =
391 BrowserContext::GetDefaultStoragePartition(browser_context);
[email protected]6e2d3d22012-02-24 18:10:36392
393 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) {
394 BrowserThread::PostTask(
395 BrowserThread::IO, FROM_HERE,
[email protected]6939075a2012-08-28 08:35:53396 base::Bind(
397 &SaveSessionStateOnIOThread,
jamb84299e2016-04-12 16:58:59398 make_scoped_refptr(BrowserContext::GetDefaultStoragePartition(
399 browser_context)->GetURLRequestContext()),
[email protected]98d6d4562014-06-25 20:57:55400 static_cast<AppCacheServiceImpl*>(
[email protected]63ef85512014-06-05 14:21:26401 storage_partition->GetAppCacheService())));
[email protected]6e2d3d22012-02-24 18:10:36402 }
403
[email protected]5f2aa722013-08-07 16:59:41404 DOMStorageContextWrapper* dom_storage_context_proxy =
405 static_cast<DOMStorageContextWrapper*>(
[email protected]b1b502e2012-09-16 07:31:43406 storage_partition->GetDOMStorageContext());
[email protected]5f2aa722013-08-07 16:59:41407 dom_storage_context_proxy->SetForceKeepSessionState();
[email protected]735e20c2012-03-20 01:16:59408
[email protected]89acda82013-06-25 20:52:50409 IndexedDBContextImpl* indexed_db_context_impl =
410 static_cast<IndexedDBContextImpl*>(
[email protected]b1b502e2012-09-16 07:31:43411 storage_partition->GetIndexedDBContext());
[email protected]89acda82013-06-25 20:52:50412 // No task runner in unit tests.
413 if (indexed_db_context_impl->TaskRunner()) {
414 indexed_db_context_impl->TaskRunner()->PostTask(
415 FROM_HERE,
416 base::Bind(&SaveSessionStateOnIndexedDBThread,
417 make_scoped_refptr(indexed_db_context_impl)));
[email protected]6e2d3d22012-02-24 18:10:36418 }
419}
420
ttr31481dc54b2015-08-06 20:11:26421void BrowserContext::SetDownloadManagerForTesting(
422 BrowserContext* browser_context,
avi1ed4a4372017-04-25 05:39:41423 std::unique_ptr<content::DownloadManager> download_manager) {
424 SetDownloadManager(browser_context, std::move(download_manager));
ttr31481dc54b2015-08-06 20:11:26425}
426
rockot963ad3e82016-04-13 16:33:23427// static
erge69130f52016-03-02 00:13:28428void BrowserContext::Initialize(
429 BrowserContext* browser_context,
430 const base::FilePath& path) {
ben6c85c4492016-06-16 20:40:51431
432 std::string new_id;
433 if (GetContentClient() && GetContentClient()->browser()) {
bend32292b2016-10-07 00:21:58434 new_id = GetContentClient()->browser()->GetServiceUserIdForBrowserContext(
ben6c85c4492016-06-16 20:40:51435 browser_context);
436 } else {
437 // Some test scenarios initialize a BrowserContext without a content client.
bencccfe2a2016-03-05 16:54:14438 new_id = base::GenerateGUID();
ben6c85c4492016-06-16 20:40:51439 }
erge69130f52016-03-02 00:13:28440
bend32292b2016-10-07 00:21:58441 ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>(
442 browser_context->GetUserData(kServiceUserId));
ben6c85c4492016-06-16 20:40:51443 if (holder)
bend32292b2016-10-07 00:21:58444 file::ForgetServiceUserIdUserDirAssociation(holder->user_id());
445 file::AssociateServiceUserIdWithUserDir(new_id, path);
ben6c85c4492016-06-16 20:40:51446 RemoveBrowserContextFromUserIdMap(browser_context);
447 g_user_id_to_context.Get()[new_id] = browser_context;
bend32292b2016-10-07 00:21:58448 browser_context->SetUserData(kServiceUserId,
avi1ed4a4372017-04-25 05:39:41449 base::MakeUnique<ServiceUserIdHolder>(new_id));
erge69130f52016-03-02 00:13:28450
avi1ed4a4372017-04-25 05:39:41451 browser_context->SetUserData(
452 kMojoWasInitialized, base::MakeUnique<base::SupportsUserData::Data>());
rockot963ad3e82016-04-13 16:33:23453
bend32292b2016-10-07 00:21:58454 ServiceManagerConnection* service_manager_connection =
455 ServiceManagerConnection::GetForProcess();
fdoray4f155f02016-10-12 11:28:50456 if (service_manager_connection && base::ThreadTaskRunnerHandle::IsSet()) {
rockot963ad3e82016-04-13 16:33:23457 // NOTE: Many unit tests create a TestBrowserContext without initializing
bend32292b2016-10-07 00:21:58458 // Mojo or the global service manager connection.
rockot963ad3e82016-04-13 16:33:23459
rockot400ea35b2016-10-15 19:15:32460 service_manager::mojom::ServicePtr service;
blundellc17a15d2016-12-21 13:52:10461 service_manager::mojom::ServiceRequest service_request(&service);
rockot963ad3e82016-04-13 16:33:23462
rockot400ea35b2016-10-15 19:15:32463 service_manager::mojom::PIDReceiverPtr pid_receiver;
bene6a9f012017-01-07 00:43:43464 service_manager::Identity identity(mojom::kBrowserServiceName, new_id);
benbd3c2482017-01-07 05:48:21465 service_manager_connection->GetConnector()->StartService(
bene6a9f012017-01-07 00:43:43466 identity, std::move(service), mojo::MakeRequest(&pid_receiver));
rockot963ad3e82016-04-13 16:33:23467 pid_receiver->SetPID(base::GetCurrentProcId());
468
benaad37ecd2017-04-12 22:08:20469 service_manager_connection->GetConnector()->StartService(identity);
bend32292b2016-10-07 00:21:58470 BrowserContextServiceManagerConnectionHolder* connection_holder =
471 new BrowserContextServiceManagerConnectionHolder(
bend32292b2016-10-07 00:21:58472 std::move(service_request));
avi1ed4a4372017-04-25 05:39:41473 browser_context->SetUserData(kServiceManagerConnection,
474 base::WrapUnique(connection_holder));
rockot963ad3e82016-04-13 16:33:23475
bend32292b2016-10-07 00:21:58476 ServiceManagerConnection* connection =
477 connection_holder->service_manager_connection();
rockot963ad3e82016-04-13 16:33:23478
ben146248de2016-06-14 15:24:59479 // New embedded service factories should be added to |connection| here.
rockot963ad3e82016-04-13 16:33:23480
481 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
482 switches::kMojoLocalStorage)) {
bend32292b2016-10-07 00:21:58483 ServiceInfo info;
484 info.factory =
ben768c8dc2016-08-12 00:26:50485 base::Bind(&file::CreateFileService,
thestig529ad8a2016-07-08 20:30:12486 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE),
487 BrowserThread::GetTaskRunnerForThread(BrowserThread::DB));
benaabad0b2016-11-16 23:54:17488 connection->AddEmbeddedService(file::mojom::kServiceName, info);
rockot963ad3e82016-04-13 16:33:23489 }
tibellab3d36b2017-03-10 02:57:25490
491 ContentBrowserClient::StaticServiceMap services;
492 browser_context->RegisterInProcessServices(&services);
493 for (const auto& entry : services) {
494 connection->AddEmbeddedService(entry.first, entry.second);
495 }
Ken Rockotc7a279c2017-05-04 23:51:45496
497 RegisterCommonBrowserInterfaces(connection);
sammc030f5012017-03-13 02:28:45498 connection->Start();
rockot963ad3e82016-04-13 16:33:23499 }
erge69130f52016-03-02 00:13:28500}
501
rockot963ad3e82016-04-13 16:33:23502// static
bend32292b2016-10-07 00:21:58503const std::string& BrowserContext::GetServiceUserIdFor(
bencccfe2a2016-03-05 16:54:14504 BrowserContext* browser_context) {
erge69130f52016-03-02 00:13:28505 CHECK(browser_context->GetUserData(kMojoWasInitialized))
506 << "Attempting to get the mojo user id for a BrowserContext that was "
507 << "never Initialize()ed.";
508
bend32292b2016-10-07 00:21:58509 ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>(
510 browser_context->GetUserData(kServiceUserId));
ben6c85c4492016-06-16 20:40:51511 return holder->user_id();
erge69130f52016-03-02 00:13:28512}
513
rockot963ad3e82016-04-13 16:33:23514// static
bend32292b2016-10-07 00:21:58515BrowserContext* BrowserContext::GetBrowserContextForServiceUserId(
ben6c85c4492016-06-16 20:40:51516 const std::string& user_id) {
517 auto it = g_user_id_to_context.Get().find(user_id);
518 return it != g_user_id_to_context.Get().end() ? it->second : nullptr;
519}
520
521// static
rockot400ea35b2016-10-15 19:15:32522service_manager::Connector* BrowserContext::GetConnectorFor(
rockot963ad3e82016-04-13 16:33:23523 BrowserContext* browser_context) {
bend32292b2016-10-07 00:21:58524 ServiceManagerConnection* connection =
525 GetServiceManagerConnectionFor(browser_context);
ben5be0b9132016-08-03 00:17:18526 return connection ? connection->GetConnector() : nullptr;
527}
528
529// static
bend32292b2016-10-07 00:21:58530ServiceManagerConnection* BrowserContext::GetServiceManagerConnectionFor(
ben5be0b9132016-08-03 00:17:18531 BrowserContext* browser_context) {
bend32292b2016-10-07 00:21:58532 BrowserContextServiceManagerConnectionHolder* connection_holder =
533 static_cast<BrowserContextServiceManagerConnectionHolder*>(
534 browser_context->GetUserData(kServiceManagerConnection));
535 return connection_holder ? connection_holder->service_manager_connection()
536 : nullptr;
rockot963ad3e82016-04-13 16:33:23537}
538
mmenkec0b2b8b12017-04-21 16:27:52539BrowserContext::BrowserContext()
540 : media_device_id_salt_(CreateRandomMediaDeviceIDSalt()) {}
541
[email protected]55eb70e762012-02-20 17:38:39542BrowserContext::~BrowserContext() {
erge69130f52016-03-02 00:13:28543 CHECK(GetUserData(kMojoWasInitialized))
544 << "Attempting to destroy a BrowserContext that never called "
545 << "Initialize()";
546
kinukof6ed359c2016-07-26 13:27:21547 DCHECK(!GetUserData(kStoragePartitionMapKeyName))
548 << "StoragePartitionMap is not shut down properly";
549
ben6c85c4492016-06-16 20:40:51550 RemoveBrowserContextFromUserIdMap(this);
551
[email protected]b441a8492012-06-06 14:55:57552 if (GetUserData(kDownloadManagerKeyName))
553 GetDownloadManager(this)->Shutdown();
[email protected]55eb70e762012-02-20 17:38:39554}
555
kinukof6ed359c2016-07-26 13:27:21556void BrowserContext::ShutdownStoragePartitions() {
557 if (GetUserData(kStoragePartitionMapKeyName))
558 RemoveUserData(kStoragePartitionMapKeyName);
559}
560
mmenkec0b2b8b12017-04-21 16:27:52561std::string BrowserContext::GetMediaDeviceIDSalt() {
562 return media_device_id_salt_;
563}
564
565// static
566std::string BrowserContext::CreateRandomMediaDeviceIDSalt() {
567 std::string salt;
568 base::Base64Encode(base::RandBytesAsString(16), &salt);
569 DCHECK(!salt.empty());
570 return salt;
571}
572
msrameke169ccb2017-04-26 05:21:41573BrowsingDataRemoverDelegate* BrowserContext::GetBrowsingDataRemoverDelegate() {
574 return nullptr;
575}
576
[email protected]55eb70e762012-02-20 17:38:39577} // namespace content