blob: 0c57b56c05afee8924b2bdc388f1b4e32b11297b [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"
avib7348942015-12-25 20:57:1020#include "build/build_config.h"
rockot963ad3e82016-04-13 16:33:2321#include "components/profile_service/profile_app.h"
22#include "components/profile_service/public/cpp/constants.h"
ergc1127942016-03-21 18:55:1923#include "components/profile_service/user_id_map.h"
[email protected]b441a8492012-06-06 14:55:5724#include "content/browser/download/download_manager_impl.h"
[email protected]393b6cb2014-05-15 00:55:1225#include "content/browser/fileapi/chrome_blob_storage_context.h"
[email protected]c4d281662013-03-31 00:35:0826#include "content/browser/indexed_db/indexed_db_context_impl.h"
[email protected]678c0362012-12-05 08:02:4427#include "content/browser/loader/resource_dispatcher_host_impl.h"
rockot963ad3e82016-04-13 16:33:2328#include "content/browser/mojo/browser_shell_connection.h"
29#include "content/browser/mojo/constants.h"
mvanouwerkerk17205ea2014-11-07 17:30:1530#include "content/browser/push_messaging/push_messaging_router.h"
[email protected]4c3a23582012-08-18 08:54:3431#include "content/browser/storage_partition_impl_map.h"
[email protected]d7c7c98a2012-07-12 21:27:4432#include "content/common/child_process_host_impl.h"
[email protected]393b6cb2014-05-15 00:55:1233#include "content/public/browser/blob_handle.h"
[email protected]55eb70e762012-02-20 17:38:3934#include "content/public/browser/browser_thread.h"
[email protected]b441a8492012-06-06 14:55:5735#include "content/public/browser/content_browser_client.h"
[email protected]536fd0b2013-03-14 17:41:5736#include "content/public/browser/site_instance.h"
rockot963ad3e82016-04-13 16:33:2337#include "content/public/common/content_switches.h"
38#include "content/public/common/mojo_shell_connection.h"
[email protected]4d7c4ef2012-03-16 01:47:1239#include "net/cookies/cookie_store.h"
[email protected]6b8a3c742014-07-25 00:25:3540#include "net/ssl/channel_id_service.h"
41#include "net/ssl/channel_id_store.h"
[email protected]6e2d3d22012-02-24 18:10:3642#include "net/url_request/url_request_context.h"
[email protected]6939075a2012-08-28 08:35:5343#include "net/url_request/url_request_context_getter.h"
rockot963ad3e82016-04-13 16:33:2344#include "services/shell/public/cpp/connection.h"
45#include "services/shell/public/cpp/connector.h"
46#include "services/shell/public/interfaces/shell_client.mojom.h"
pilgrime92c5fcd2014-09-10 23:31:2347#include "storage/browser/database/database_tracker.h"
48#include "storage/browser/fileapi/external_mount_points.h"
[email protected]55eb70e762012-02-20 17:38:3949
[email protected]314c3e22012-02-21 03:57:4250using base::UserDataAdapter;
[email protected]55eb70e762012-02-20 17:38:3951
[email protected]55eb70e762012-02-20 17:38:3952namespace content {
53
[email protected]735e20c2012-03-20 01:16:5954namespace {
55
bencccfe2a2016-03-05 16:54:1456base::LazyInstance<std::set<std::string>> g_used_user_ids =
erge69130f52016-03-02 00:13:2857 LAZY_INSTANCE_INITIALIZER;
bencccfe2a2016-03-05 16:54:1458base::LazyInstance<std::vector<std::pair<BrowserContext*, std::string>>>
erge69130f52016-03-02 00:13:2859g_context_to_user_id = LAZY_INSTANCE_INITIALIZER;
60
[email protected]e0ce8a1e2012-09-18 10:26:3661// Key names on BrowserContext.
[email protected]6ef0c3912013-01-25 22:46:3462const char kDownloadManagerKeyName[] = "download_manager";
rockot963ad3e82016-04-13 16:33:2363const char kMojoShellConnection[] = "mojo-shell-connection";
erge69130f52016-03-02 00:13:2864const char kMojoWasInitialized[] = "mojo-was-initialized";
rockot963ad3e82016-04-13 16:33:2365const char kStoragePartitionMapKeyName[] = "content_storage_partition_map";
erge69130f52016-03-02 00:13:2866
[email protected]9afc14e22013-09-25 22:34:1467#if defined(OS_CHROMEOS)
68const char kMountPointsKey[] = "mount_points";
69#endif // defined(OS_CHROMEOS)
70
[email protected]14acc642012-11-17 12:20:1071StoragePartitionImplMap* GetStoragePartitionMap(
72 BrowserContext* browser_context) {
[email protected]4c3a23582012-08-18 08:54:3473 StoragePartitionImplMap* partition_map =
74 static_cast<StoragePartitionImplMap*>(
a.cavalcantiffab73762015-08-15 02:55:4875 browser_context->GetUserData(kStoragePartitionMapKeyName));
[email protected]d7c7c98a2012-07-12 21:27:4476 if (!partition_map) {
[email protected]4c3a23582012-08-18 08:54:3477 partition_map = new StoragePartitionImplMap(browser_context);
a.cavalcantiffab73762015-08-15 02:55:4878 browser_context->SetUserData(kStoragePartitionMapKeyName, partition_map);
[email protected]d7c7c98a2012-07-12 21:27:4479 }
[email protected]14acc642012-11-17 12:20:1080 return partition_map;
81}
82
83StoragePartition* GetStoragePartitionFromConfig(
84 BrowserContext* browser_context,
85 const std::string& partition_domain,
86 const std::string& partition_name,
87 bool in_memory) {
88 StoragePartitionImplMap* partition_map =
89 GetStoragePartitionMap(browser_context);
[email protected]d7c7c98a2012-07-12 21:27:4490
[email protected]1bc28312012-11-08 08:31:5391 if (browser_context->IsOffTheRecord())
92 in_memory = true;
93
[email protected]1bc28312012-11-08 08:31:5394 return partition_map->Get(partition_domain, partition_name, in_memory);
[email protected]d1198fd2012-08-13 22:50:1995}
96
[email protected]6939075a2012-08-28 08:35:5397void SaveSessionStateOnIOThread(
98 const scoped_refptr<net::URLRequestContextGetter>& context_getter,
[email protected]98d6d4562014-06-25 20:57:5599 AppCacheServiceImpl* appcache_service) {
[email protected]6939075a2012-08-28 08:35:53100 net::URLRequestContext* context = context_getter->GetURLRequestContext();
mmenkeded79da2016-02-06 08:28:51101 context->cookie_store()->SetForceKeepSessionState();
[email protected]6b8a3c742014-07-25 00:25:35102 context->channel_id_service()->GetChannelIDStore()->
[email protected]6939075a2012-08-28 08:35:53103 SetForceKeepSessionState();
104 appcache_service->set_force_keep_session_state();
[email protected]6e2d3d22012-02-24 18:10:36105}
106
[email protected]89acda82013-06-25 20:52:50107void SaveSessionStateOnIndexedDBThread(
[email protected]6e2d3d22012-02-24 18:10:36108 scoped_refptr<IndexedDBContextImpl> indexed_db_context) {
[email protected]bf510ed2012-06-05 08:31:43109 indexed_db_context->SetForceKeepSessionState();
[email protected]6e2d3d22012-02-24 18:10:36110}
111
falken41f4175162014-10-29 07:03:41112void ShutdownServiceWorkerContext(StoragePartition* partition) {
113 ServiceWorkerContextWrapper* wrapper =
114 static_cast<ServiceWorkerContextWrapper*>(
115 partition->GetServiceWorkerContext());
116 wrapper->process_manager()->Shutdown();
117}
118
ttr31481dc54b2015-08-06 20:11:26119void SetDownloadManager(BrowserContext* context,
120 content::DownloadManager* download_manager) {
121 DCHECK_CURRENTLY_ON(BrowserThread::UI);
122 DCHECK(download_manager);
123 context->SetUserData(kDownloadManagerKeyName, download_manager);
124}
125
rockot963ad3e82016-04-13 16:33:23126class BrowserContextShellConnectionHolder
127 : public base::SupportsUserData::Data {
128 public:
129 BrowserContextShellConnectionHolder(
130 std::unique_ptr<mojo::Connection> connection,
131 mojo::shell::mojom::ShellClientRequest request)
132 : root_connection_(std::move(connection)),
133 shell_connection_(new BrowserShellConnection(std::move(request))) {}
134 ~BrowserContextShellConnectionHolder() override {}
135
136 BrowserShellConnection* shell_connection() { return shell_connection_.get(); }
137
138 private:
139 std::unique_ptr<mojo::Connection> root_connection_;
140 std::unique_ptr<BrowserShellConnection> shell_connection_;
141
142 DISALLOW_COPY_AND_ASSIGN(BrowserContextShellConnectionHolder);
143};
144
[email protected]735e20c2012-03-20 01:16:59145} // namespace
146
[email protected]14acc642012-11-17 12:20:10147// static
148void BrowserContext::AsyncObliterateStoragePartition(
149 BrowserContext* browser_context,
[email protected]399583b2012-12-11 09:33:42150 const GURL& site,
151 const base::Closure& on_gc_required) {
152 GetStoragePartitionMap(browser_context)->AsyncObliterate(site,
153 on_gc_required);
154}
155
156// static
157void BrowserContext::GarbageCollectStoragePartitions(
dcheng59716272016-04-09 05:19:08158 BrowserContext* browser_context,
159 std::unique_ptr<base::hash_set<base::FilePath>> active_paths,
160 const base::Closure& done) {
dcheng36b6aec92015-12-26 06:16:36161 GetStoragePartitionMap(browser_context)
162 ->GarbageCollect(std::move(active_paths), done);
[email protected]14acc642012-11-17 12:20:10163}
164
[email protected]b441a8492012-06-06 14:55:57165DownloadManager* BrowserContext::GetDownloadManager(
166 BrowserContext* context) {
mostynbfbcdc27a2015-03-13 17:58:52167 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]b441a8492012-06-06 14:55:57168 if (!context->GetUserData(kDownloadManagerKeyName)) {
[email protected]eba4a4d2013-05-29 02:18:06169 DownloadManager* download_manager =
[email protected]d25fda12012-06-12 17:05:03170 new DownloadManagerImpl(
[email protected]16798692013-04-23 18:08:38171 GetContentClient()->browser()->GetNetLog(), context);
[email protected]d25fda12012-06-12 17:05:03172
ttr31481dc54b2015-08-06 20:11:26173 SetDownloadManager(context, download_manager);
[email protected]b441a8492012-06-06 14:55:57174 download_manager->SetDelegate(context->GetDownloadManagerDelegate());
[email protected]b441a8492012-06-06 14:55:57175 }
176
[email protected]eba4a4d2013-05-29 02:18:06177 return static_cast<DownloadManager*>(
178 context->GetUserData(kDownloadManagerKeyName));
[email protected]b441a8492012-06-06 14:55:57179}
180
[email protected]6ef0c3912013-01-25 22:46:34181// static
[email protected]cd501a72014-08-22 19:58:31182storage::ExternalMountPoints* BrowserContext::GetMountPoints(
[email protected]6ef0c3912013-01-25 22:46:34183 BrowserContext* context) {
184 // Ensure that these methods are called on the UI thread, except for
185 // unittests where a UI thread might not have been created.
186 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) ||
187 !BrowserThread::IsMessageLoopValid(BrowserThread::UI));
188
189#if defined(OS_CHROMEOS)
190 if (!context->GetUserData(kMountPointsKey)) {
[email protected]cd501a72014-08-22 19:58:31191 scoped_refptr<storage::ExternalMountPoints> mount_points =
192 storage::ExternalMountPoints::CreateRefCounted();
[email protected]6ef0c3912013-01-25 22:46:34193 context->SetUserData(
194 kMountPointsKey,
[email protected]cd501a72014-08-22 19:58:31195 new UserDataAdapter<storage::ExternalMountPoints>(mount_points.get()));
[email protected]6ef0c3912013-01-25 22:46:34196 }
197
[email protected]cd501a72014-08-22 19:58:31198 return UserDataAdapter<storage::ExternalMountPoints>::Get(context,
199 kMountPointsKey);
[email protected]6ef0c3912013-01-25 22:46:34200#else
201 return NULL;
202#endif
203}
204
[email protected]4c3a23582012-08-18 08:54:34205StoragePartition* BrowserContext::GetStoragePartition(
206 BrowserContext* browser_context,
207 SiteInstance* site_instance) {
[email protected]1bc28312012-11-08 08:31:53208 std::string partition_domain;
209 std::string partition_name;
210 bool in_memory = false;
[email protected]4c3a23582012-08-18 08:54:34211
212 // TODO(ajwong): After GetDefaultStoragePartition() is removed, get rid of
213 // this conditional and require that |site_instance| is non-NULL.
214 if (site_instance) {
[email protected]1bc28312012-11-08 08:31:53215 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
[email protected]14acc642012-11-17 12:20:10216 browser_context, site_instance->GetSiteURL(), true,
[email protected]1bc28312012-11-08 08:31:53217 &partition_domain, &partition_name, &in_memory);
[email protected]4c3a23582012-08-18 08:54:34218 }
219
[email protected]1bc28312012-11-08 08:31:53220 return GetStoragePartitionFromConfig(
221 browser_context, partition_domain, partition_name, in_memory);
[email protected]4c3a23582012-08-18 08:54:34222}
223
[email protected]e94bbcb2012-09-07 05:33:57224StoragePartition* BrowserContext::GetStoragePartitionForSite(
225 BrowserContext* browser_context,
226 const GURL& site) {
[email protected]1bc28312012-11-08 08:31:53227 std::string partition_domain;
228 std::string partition_name;
229 bool in_memory;
[email protected]e94bbcb2012-09-07 05:33:57230
[email protected]1bc28312012-11-08 08:31:53231 GetContentClient()->browser()->GetStoragePartitionConfigForSite(
[email protected]14acc642012-11-17 12:20:10232 browser_context, site, true, &partition_domain, &partition_name,
233 &in_memory);
[email protected]1bc28312012-11-08 08:31:53234
235 return GetStoragePartitionFromConfig(
236 browser_context, partition_domain, partition_name, in_memory);
[email protected]e94bbcb2012-09-07 05:33:57237}
238
[email protected]4c3a23582012-08-18 08:54:34239void BrowserContext::ForEachStoragePartition(
240 BrowserContext* browser_context,
241 const StoragePartitionCallback& callback) {
242 StoragePartitionImplMap* partition_map =
243 static_cast<StoragePartitionImplMap*>(
a.cavalcantiffab73762015-08-15 02:55:48244 browser_context->GetUserData(kStoragePartitionMapKeyName));
[email protected]4c3a23582012-08-18 08:54:34245 if (!partition_map)
246 return;
247
248 partition_map->ForEach(callback);
249}
250
251StoragePartition* BrowserContext::GetDefaultStoragePartition(
252 BrowserContext* browser_context) {
253 return GetStoragePartition(browser_context, NULL);
[email protected]55eb70e762012-02-20 17:38:39254}
255
tbarzicdb712682015-03-06 06:05:41256// static
[email protected]393b6cb2014-05-15 00:55:12257void BrowserContext::CreateMemoryBackedBlob(BrowserContext* browser_context,
258 const char* data, size_t length,
259 const BlobCallback& callback) {
mostynbfbcdc27a2015-03-13 17:58:52260 DCHECK_CURRENTLY_ON(BrowserThread::UI);
[email protected]393b6cb2014-05-15 00:55:12261
262 ChromeBlobStorageContext* blob_context =
263 ChromeBlobStorageContext::GetFor(browser_context);
264 BrowserThread::PostTaskAndReplyWithResult(
265 BrowserThread::IO, FROM_HERE,
266 base::Bind(&ChromeBlobStorageContext::CreateMemoryBackedBlob,
267 make_scoped_refptr(blob_context), data, length),
268 callback);
269}
270
[email protected]66e53d0282014-08-07 10:04:35271// static
tbarzicdb712682015-03-06 06:05:41272void BrowserContext::CreateFileBackedBlob(
273 BrowserContext* browser_context,
274 const base::FilePath& path,
275 int64_t offset,
276 int64_t size,
277 const base::Time& expected_modification_time,
278 const BlobCallback& callback) {
mostynbfbcdc27a2015-03-13 17:58:52279 DCHECK_CURRENTLY_ON(BrowserThread::UI);
tbarzicdb712682015-03-06 06:05:41280
281 ChromeBlobStorageContext* blob_context =
282 ChromeBlobStorageContext::GetFor(browser_context);
283 BrowserThread::PostTaskAndReplyWithResult(
284 BrowserThread::IO, FROM_HERE,
285 base::Bind(&ChromeBlobStorageContext::CreateFileBackedBlob,
286 make_scoped_refptr(blob_context), path, offset, size,
287 expected_modification_time),
288 callback);
289}
290
291// static
[email protected]66e53d0282014-08-07 10:04:35292void BrowserContext::DeliverPushMessage(
293 BrowserContext* browser_context,
294 const GURL& origin,
avib7348942015-12-25 20:57:10295 int64_t service_worker_registration_id,
harknessdd4d2b22016-01-27 19:26:43296 const PushEventPayload& payload,
johnmea80c2552014-10-17 14:51:40297 const base::Callback<void(PushDeliveryStatus)>& callback) {
mostynbfbcdc27a2015-03-13 17:58:52298 DCHECK_CURRENTLY_ON(BrowserThread::UI);
harknessdd4d2b22016-01-27 19:26:43299 PushMessagingRouter::DeliverMessage(browser_context, origin,
300 service_worker_registration_id, payload,
301 callback);
[email protected]66e53d0282014-08-07 10:04:35302}
303
falken41f4175162014-10-29 07:03:41304// static
305void BrowserContext::NotifyWillBeDestroyed(BrowserContext* browser_context) {
306 // Service Workers must shutdown before the browser context is destroyed,
307 // since they keep render process hosts alive and the codebase assumes that
308 // render process hosts die before their profile (browser context) dies.
309 ForEachStoragePartition(browser_context,
310 base::Bind(ShutdownServiceWorkerContext));
311}
312
[email protected]314c3e22012-02-21 03:57:42313void BrowserContext::EnsureResourceContextInitialized(BrowserContext* context) {
[email protected]7e26ac92012-02-27 20:15:05314 // This will be enough to tickle initialization of BrowserContext if
315 // necessary, which initializes ResourceContext. The reason we don't call
[email protected]4c3a23582012-08-18 08:54:34316 // ResourceContext::InitializeResourceContext() directly here is that
317 // ResourceContext initialization may call back into BrowserContext
318 // and when that call returns it'll end rewriting its UserData map. It will
319 // end up rewriting the same value but this still causes a race condition.
320 //
321 // See http://crbug.com/115678.
322 GetDefaultStoragePartition(context);
[email protected]55eb70e762012-02-20 17:38:39323}
324
[email protected]6e2d3d22012-02-24 18:10:36325void BrowserContext::SaveSessionState(BrowserContext* browser_context) {
[email protected]5c8e67c2012-08-29 00:48:52326 GetDefaultStoragePartition(browser_context)->GetDatabaseTracker()->
327 SetForceKeepSessionState();
[email protected]b1b502e2012-09-16 07:31:43328 StoragePartition* storage_partition =
329 BrowserContext::GetDefaultStoragePartition(browser_context);
[email protected]6e2d3d22012-02-24 18:10:36330
331 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) {
332 BrowserThread::PostTask(
333 BrowserThread::IO, FROM_HERE,
[email protected]6939075a2012-08-28 08:35:53334 base::Bind(
335 &SaveSessionStateOnIOThread,
jamb84299e2016-04-12 16:58:59336 make_scoped_refptr(BrowserContext::GetDefaultStoragePartition(
337 browser_context)->GetURLRequestContext()),
[email protected]98d6d4562014-06-25 20:57:55338 static_cast<AppCacheServiceImpl*>(
[email protected]63ef85512014-06-05 14:21:26339 storage_partition->GetAppCacheService())));
[email protected]6e2d3d22012-02-24 18:10:36340 }
341
[email protected]5f2aa722013-08-07 16:59:41342 DOMStorageContextWrapper* dom_storage_context_proxy =
343 static_cast<DOMStorageContextWrapper*>(
[email protected]b1b502e2012-09-16 07:31:43344 storage_partition->GetDOMStorageContext());
[email protected]5f2aa722013-08-07 16:59:41345 dom_storage_context_proxy->SetForceKeepSessionState();
[email protected]735e20c2012-03-20 01:16:59346
[email protected]89acda82013-06-25 20:52:50347 IndexedDBContextImpl* indexed_db_context_impl =
348 static_cast<IndexedDBContextImpl*>(
[email protected]b1b502e2012-09-16 07:31:43349 storage_partition->GetIndexedDBContext());
[email protected]89acda82013-06-25 20:52:50350 // No task runner in unit tests.
351 if (indexed_db_context_impl->TaskRunner()) {
352 indexed_db_context_impl->TaskRunner()->PostTask(
353 FROM_HERE,
354 base::Bind(&SaveSessionStateOnIndexedDBThread,
355 make_scoped_refptr(indexed_db_context_impl)));
[email protected]6e2d3d22012-02-24 18:10:36356 }
357}
358
ttr31481dc54b2015-08-06 20:11:26359void BrowserContext::SetDownloadManagerForTesting(
360 BrowserContext* browser_context,
361 DownloadManager* download_manager) {
362 SetDownloadManager(browser_context, download_manager);
363}
364
rockot963ad3e82016-04-13 16:33:23365// static
erge69130f52016-03-02 00:13:28366void BrowserContext::Initialize(
367 BrowserContext* browser_context,
368 const base::FilePath& path) {
bencccfe2a2016-03-05 16:54:14369 // Generate a GUID for |browser_context| to use as the Mojo user id.
370 std::string new_id = base::GenerateGUID();
371 while (g_used_user_ids.Get().find(new_id) != g_used_user_ids.Get().end())
372 new_id = base::GenerateGUID();
erge69130f52016-03-02 00:13:28373
374 g_used_user_ids.Get().insert(new_id);
375 g_context_to_user_id.Get().push_back(std::make_pair(browser_context, new_id));
376
ergc1127942016-03-21 18:55:19377 profile::AssociateMojoUserIDWithProfileDir(new_id, path);
erge69130f52016-03-02 00:13:28378 browser_context->SetUserData(kMojoWasInitialized,
379 new base::SupportsUserData::Data);
rockot963ad3e82016-04-13 16:33:23380
381 MojoShellConnection* shell = MojoShellConnection::Get();
382 if (shell) {
383 // NOTE: Many unit tests create a TestBrowserContext without initializing
384 // Mojo or the global Mojo shell connection.
385
386 mojo::shell::mojom::ShellClientPtr shell_client;
387 mojo::shell::mojom::ShellClientRequest shell_client_request =
388 mojo::GetProxy(&shell_client);
389
390 mojo::shell::mojom::PIDReceiverPtr pid_receiver;
391 mojo::Connector::ConnectParams params(
392 mojo::Identity(kBrowserMojoApplicationName, new_id));
393 params.set_client_process_connection(std::move(shell_client),
394 mojo::GetProxy(&pid_receiver));
395 pid_receiver->SetPID(base::GetCurrentProcId());
396
397 BrowserContextShellConnectionHolder* connection_holder =
398 new BrowserContextShellConnectionHolder(
399 shell->GetConnector()->Connect(&params),
400 std::move(shell_client_request));
401 browser_context->SetUserData(kMojoShellConnection, connection_holder);
402
403 BrowserShellConnection* connection = connection_holder->shell_connection();
404
405 // New embedded application factories should be added to |connection| here.
406
407 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
408 switches::kMojoLocalStorage)) {
409 connection->AddEmbeddedApplication(
410 profile::kProfileMojoApplicationName,
411 base::Bind(
412 &profile::CreateProfileApp,
413 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE),
414 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB)),
415 nullptr);
416 }
417 }
erge69130f52016-03-02 00:13:28418}
419
rockot963ad3e82016-04-13 16:33:23420// static
bencccfe2a2016-03-05 16:54:14421const std::string& BrowserContext::GetMojoUserIdFor(
422 BrowserContext* browser_context) {
erge69130f52016-03-02 00:13:28423 CHECK(browser_context->GetUserData(kMojoWasInitialized))
424 << "Attempting to get the mojo user id for a BrowserContext that was "
425 << "never Initialize()ed.";
426
427 auto it = std::find_if(
428 g_context_to_user_id.Get().begin(),
429 g_context_to_user_id.Get().end(),
bencccfe2a2016-03-05 16:54:14430 [&browser_context](const std::pair<BrowserContext*, std::string>& p) {
erge69130f52016-03-02 00:13:28431 return p.first == browser_context; });
432 CHECK(it != g_context_to_user_id.Get().end());
433 return it->second;
434}
435
rockot963ad3e82016-04-13 16:33:23436// static
437mojo::Connector* BrowserContext::GetMojoConnectorFor(
438 BrowserContext* browser_context) {
439 BrowserContextShellConnectionHolder* connection_holder =
440 static_cast<BrowserContextShellConnectionHolder*>(
441 browser_context->GetUserData(kMojoShellConnection));
442 if (!connection_holder)
443 return nullptr;
444 return connection_holder->shell_connection()->GetConnector();
445}
446
[email protected]55eb70e762012-02-20 17:38:39447BrowserContext::~BrowserContext() {
erge69130f52016-03-02 00:13:28448 CHECK(GetUserData(kMojoWasInitialized))
449 << "Attempting to destroy a BrowserContext that never called "
450 << "Initialize()";
451
[email protected]b441a8492012-06-06 14:55:57452 if (GetUserData(kDownloadManagerKeyName))
453 GetDownloadManager(this)->Shutdown();
[email protected]55eb70e762012-02-20 17:38:39454}
455
[email protected]55eb70e762012-02-20 17:38:39456} // namespace content