OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/public/browser/browser_context.h" | 5 #include "content/public/browser/browser_context.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <algorithm> |
| 10 #include <limits> |
9 #include <utility> | 11 #include <utility> |
10 | 12 |
| 13 #include "base/lazy_instance.h" |
| 14 #include "base/rand_util.h" |
11 #include "build/build_config.h" | 15 #include "build/build_config.h" |
12 | 16 |
13 #if !defined(OS_IOS) | 17 #if !defined(OS_IOS) |
| 18 #include "components/profile_service/profile_app.h" |
14 #include "content/browser/download/download_manager_impl.h" | 19 #include "content/browser/download/download_manager_impl.h" |
15 #include "content/browser/fileapi/chrome_blob_storage_context.h" | 20 #include "content/browser/fileapi/chrome_blob_storage_context.h" |
16 #include "content/browser/indexed_db/indexed_db_context_impl.h" | 21 #include "content/browser/indexed_db/indexed_db_context_impl.h" |
17 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 22 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
18 #include "content/browser/push_messaging/push_messaging_router.h" | 23 #include "content/browser/push_messaging/push_messaging_router.h" |
19 #include "content/browser/storage_partition_impl_map.h" | 24 #include "content/browser/storage_partition_impl_map.h" |
20 #include "content/common/child_process_host_impl.h" | 25 #include "content/common/child_process_host_impl.h" |
21 #include "content/public/browser/blob_handle.h" | 26 #include "content/public/browser/blob_handle.h" |
22 #include "content/public/browser/browser_thread.h" | 27 #include "content/public/browser/browser_thread.h" |
23 #include "content/public/browser/content_browser_client.h" | 28 #include "content/public/browser/content_browser_client.h" |
24 #include "content/public/browser/site_instance.h" | 29 #include "content/public/browser/site_instance.h" |
25 #include "net/cookies/cookie_store.h" | 30 #include "net/cookies/cookie_store.h" |
26 #include "net/ssl/channel_id_service.h" | 31 #include "net/ssl/channel_id_service.h" |
27 #include "net/ssl/channel_id_store.h" | 32 #include "net/ssl/channel_id_store.h" |
28 #include "net/url_request/url_request_context.h" | 33 #include "net/url_request/url_request_context.h" |
29 #include "net/url_request/url_request_context_getter.h" | 34 #include "net/url_request/url_request_context_getter.h" |
30 #include "storage/browser/database/database_tracker.h" | 35 #include "storage/browser/database/database_tracker.h" |
31 #include "storage/browser/fileapi/external_mount_points.h" | 36 #include "storage/browser/fileapi/external_mount_points.h" |
32 #endif // !OS_IOS | 37 #endif // !OS_IOS |
33 | 38 |
34 using base::UserDataAdapter; | 39 using base::UserDataAdapter; |
35 | 40 |
36 namespace content { | 41 namespace content { |
37 | 42 |
38 // Only ~BrowserContext() is needed on iOS. | 43 // Only ~BrowserContext() is needed on iOS. |
39 #if !defined(OS_IOS) | 44 #if !defined(OS_IOS) |
40 namespace { | 45 namespace { |
41 | 46 |
| 47 base::LazyInstance<std::set<uint32_t>> g_used_user_ids = |
| 48 LAZY_INSTANCE_INITIALIZER; |
| 49 base::LazyInstance<std::vector<std::pair<BrowserContext*, uint32_t>>> |
| 50 g_context_to_user_id = LAZY_INSTANCE_INITIALIZER; |
| 51 |
42 // Key names on BrowserContext. | 52 // Key names on BrowserContext. |
43 const char kDownloadManagerKeyName[] = "download_manager"; | 53 const char kDownloadManagerKeyName[] = "download_manager"; |
44 const char kStoragePartitionMapKeyName[] = "content_storage_partition_map"; | 54 const char kStoragePartitionMapKeyName[] = "content_storage_partition_map"; |
45 | 55 |
| 56 const char kMojoWasInitialized[] = "mojo-was-initialized"; |
| 57 |
46 #if defined(OS_CHROMEOS) | 58 #if defined(OS_CHROMEOS) |
47 const char kMountPointsKey[] = "mount_points"; | 59 const char kMountPointsKey[] = "mount_points"; |
48 #endif // defined(OS_CHROMEOS) | 60 #endif // defined(OS_CHROMEOS) |
49 | 61 |
50 StoragePartitionImplMap* GetStoragePartitionMap( | 62 StoragePartitionImplMap* GetStoragePartitionMap( |
51 BrowserContext* browser_context) { | 63 BrowserContext* browser_context) { |
52 StoragePartitionImplMap* partition_map = | 64 StoragePartitionImplMap* partition_map = |
53 static_cast<StoragePartitionImplMap*>( | 65 static_cast<StoragePartitionImplMap*>( |
54 browser_context->GetUserData(kStoragePartitionMapKeyName)); | 66 browser_context->GetUserData(kStoragePartitionMapKeyName)); |
55 if (!partition_map) { | 67 if (!partition_map) { |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 make_scoped_refptr(indexed_db_context_impl))); | 326 make_scoped_refptr(indexed_db_context_impl))); |
315 } | 327 } |
316 } | 328 } |
317 | 329 |
318 void BrowserContext::SetDownloadManagerForTesting( | 330 void BrowserContext::SetDownloadManagerForTesting( |
319 BrowserContext* browser_context, | 331 BrowserContext* browser_context, |
320 DownloadManager* download_manager) { | 332 DownloadManager* download_manager) { |
321 SetDownloadManager(browser_context, download_manager); | 333 SetDownloadManager(browser_context, download_manager); |
322 } | 334 } |
323 | 335 |
| 336 void BrowserContext::Initialize( |
| 337 BrowserContext* browser_context, |
| 338 const base::FilePath& path) { |
| 339 // Associate a random unsigned 32 bit number with |browser_context|. This |
| 340 // becomes the mojo user id for this BrowserContext. |
| 341 uint32_t new_id = static_cast<uint32_t>(base::RandGenerator(UINT32_MAX)); |
| 342 while ((new_id == 0) || |
| 343 (g_used_user_ids.Get().find(new_id) != g_used_user_ids.Get().end())) { |
| 344 new_id = static_cast<uint32_t>(base::RandGenerator(UINT32_MAX)); |
| 345 } |
| 346 |
| 347 g_used_user_ids.Get().insert(new_id); |
| 348 g_context_to_user_id.Get().push_back(std::make_pair(browser_context, new_id)); |
| 349 |
| 350 profile::ProfileApp::AssociateMojoUserIDWithProfileDir(new_id, path); |
| 351 browser_context->SetUserData(kMojoWasInitialized, |
| 352 new base::SupportsUserData::Data); |
| 353 } |
| 354 |
| 355 uint32_t BrowserContext::GetMojoUserIdFor(BrowserContext* browser_context) { |
| 356 CHECK(browser_context->GetUserData(kMojoWasInitialized)) |
| 357 << "Attempting to get the mojo user id for a BrowserContext that was " |
| 358 << "never Initialize()ed."; |
| 359 |
| 360 auto it = std::find_if( |
| 361 g_context_to_user_id.Get().begin(), |
| 362 g_context_to_user_id.Get().end(), |
| 363 [&browser_context](const std::pair<BrowserContext*, uint32_t>& p) { |
| 364 return p.first == browser_context; }); |
| 365 CHECK(it != g_context_to_user_id.Get().end()); |
| 366 return it->second; |
| 367 } |
| 368 |
324 #endif // !OS_IOS | 369 #endif // !OS_IOS |
325 | 370 |
326 BrowserContext::~BrowserContext() { | 371 BrowserContext::~BrowserContext() { |
| 372 CHECK(GetUserData(kMojoWasInitialized)) |
| 373 << "Attempting to destroy a BrowserContext that never called " |
| 374 << "Initialize()"; |
| 375 |
327 #if !defined(OS_IOS) | 376 #if !defined(OS_IOS) |
328 if (GetUserData(kDownloadManagerKeyName)) | 377 if (GetUserData(kDownloadManagerKeyName)) |
329 GetDownloadManager(this)->Shutdown(); | 378 GetDownloadManager(this)->Shutdown(); |
330 #endif | 379 #endif |
331 } | 380 } |
332 | 381 |
333 } // namespace content | 382 } // namespace content |
OLD | NEW |