| 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> | 9 #include <algorithm> |
| 10 #include <limits> | 10 #include <limits> |
| 11 #include <utility> | 11 #include <utility> |
| 12 | 12 |
| 13 #include "base/guid.h" |
| 13 #include "base/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
| 14 #include "base/rand_util.h" | 15 #include "base/rand_util.h" |
| 15 #include "build/build_config.h" | 16 #include "build/build_config.h" |
| 16 | 17 |
| 17 #if !defined(OS_IOS) | 18 #if !defined(OS_IOS) |
| 18 #include "components/profile_service/profile_app.h" | 19 #include "components/profile_service/profile_app.h" |
| 19 #include "content/browser/download/download_manager_impl.h" | 20 #include "content/browser/download/download_manager_impl.h" |
| 20 #include "content/browser/fileapi/chrome_blob_storage_context.h" | 21 #include "content/browser/fileapi/chrome_blob_storage_context.h" |
| 21 #include "content/browser/indexed_db/indexed_db_context_impl.h" | 22 #include "content/browser/indexed_db/indexed_db_context_impl.h" |
| 22 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 23 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 37 #endif // !OS_IOS | 38 #endif // !OS_IOS |
| 38 | 39 |
| 39 using base::UserDataAdapter; | 40 using base::UserDataAdapter; |
| 40 | 41 |
| 41 namespace content { | 42 namespace content { |
| 42 | 43 |
| 43 // Only ~BrowserContext() is needed on iOS. | 44 // Only ~BrowserContext() is needed on iOS. |
| 44 #if !defined(OS_IOS) | 45 #if !defined(OS_IOS) |
| 45 namespace { | 46 namespace { |
| 46 | 47 |
| 47 base::LazyInstance<std::set<uint32_t>> g_used_user_ids = | 48 base::LazyInstance<std::set<std::string>> g_used_user_ids = |
| 48 LAZY_INSTANCE_INITIALIZER; | 49 LAZY_INSTANCE_INITIALIZER; |
| 49 base::LazyInstance<std::vector<std::pair<BrowserContext*, uint32_t>>> | 50 base::LazyInstance<std::vector<std::pair<BrowserContext*, std::string>>> |
| 50 g_context_to_user_id = LAZY_INSTANCE_INITIALIZER; | 51 g_context_to_user_id = LAZY_INSTANCE_INITIALIZER; |
| 51 | 52 |
| 52 // Key names on BrowserContext. | 53 // Key names on BrowserContext. |
| 53 const char kDownloadManagerKeyName[] = "download_manager"; | 54 const char kDownloadManagerKeyName[] = "download_manager"; |
| 54 const char kStoragePartitionMapKeyName[] = "content_storage_partition_map"; | 55 const char kStoragePartitionMapKeyName[] = "content_storage_partition_map"; |
| 55 | 56 |
| 56 const char kMojoWasInitialized[] = "mojo-was-initialized"; | 57 const char kMojoWasInitialized[] = "mojo-was-initialized"; |
| 57 | 58 |
| 58 #if defined(OS_CHROMEOS) | 59 #if defined(OS_CHROMEOS) |
| 59 const char kMountPointsKey[] = "mount_points"; | 60 const char kMountPointsKey[] = "mount_points"; |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 | 330 |
| 330 void BrowserContext::SetDownloadManagerForTesting( | 331 void BrowserContext::SetDownloadManagerForTesting( |
| 331 BrowserContext* browser_context, | 332 BrowserContext* browser_context, |
| 332 DownloadManager* download_manager) { | 333 DownloadManager* download_manager) { |
| 333 SetDownloadManager(browser_context, download_manager); | 334 SetDownloadManager(browser_context, download_manager); |
| 334 } | 335 } |
| 335 | 336 |
| 336 void BrowserContext::Initialize( | 337 void BrowserContext::Initialize( |
| 337 BrowserContext* browser_context, | 338 BrowserContext* browser_context, |
| 338 const base::FilePath& path) { | 339 const base::FilePath& path) { |
| 339 // Associate a random unsigned 32 bit number with |browser_context|. This | 340 // Generate a GUID for |browser_context| to use as the Mojo user id. |
| 340 // becomes the mojo user id for this BrowserContext. | 341 std::string new_id = base::GenerateGUID(); |
| 341 uint32_t new_id = static_cast<uint32_t>(base::RandGenerator(UINT32_MAX)); | 342 while (g_used_user_ids.Get().find(new_id) != g_used_user_ids.Get().end()) |
| 342 while ((new_id == 0) || | 343 new_id = base::GenerateGUID(); |
| 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 | 344 |
| 347 g_used_user_ids.Get().insert(new_id); | 345 g_used_user_ids.Get().insert(new_id); |
| 348 g_context_to_user_id.Get().push_back(std::make_pair(browser_context, new_id)); | 346 g_context_to_user_id.Get().push_back(std::make_pair(browser_context, new_id)); |
| 349 | 347 |
| 350 profile::ProfileApp::AssociateMojoUserIDWithProfileDir(new_id, path); | 348 profile::ProfileApp::AssociateMojoUserIDWithProfileDir(new_id, path); |
| 351 browser_context->SetUserData(kMojoWasInitialized, | 349 browser_context->SetUserData(kMojoWasInitialized, |
| 352 new base::SupportsUserData::Data); | 350 new base::SupportsUserData::Data); |
| 353 } | 351 } |
| 354 | 352 |
| 355 uint32_t BrowserContext::GetMojoUserIdFor(BrowserContext* browser_context) { | 353 const std::string& BrowserContext::GetMojoUserIdFor( |
| 354 BrowserContext* browser_context) { |
| 356 CHECK(browser_context->GetUserData(kMojoWasInitialized)) | 355 CHECK(browser_context->GetUserData(kMojoWasInitialized)) |
| 357 << "Attempting to get the mojo user id for a BrowserContext that was " | 356 << "Attempting to get the mojo user id for a BrowserContext that was " |
| 358 << "never Initialize()ed."; | 357 << "never Initialize()ed."; |
| 359 | 358 |
| 360 auto it = std::find_if( | 359 auto it = std::find_if( |
| 361 g_context_to_user_id.Get().begin(), | 360 g_context_to_user_id.Get().begin(), |
| 362 g_context_to_user_id.Get().end(), | 361 g_context_to_user_id.Get().end(), |
| 363 [&browser_context](const std::pair<BrowserContext*, uint32_t>& p) { | 362 [&browser_context](const std::pair<BrowserContext*, std::string>& p) { |
| 364 return p.first == browser_context; }); | 363 return p.first == browser_context; }); |
| 365 CHECK(it != g_context_to_user_id.Get().end()); | 364 CHECK(it != g_context_to_user_id.Get().end()); |
| 366 return it->second; | 365 return it->second; |
| 367 } | 366 } |
| 368 | 367 |
| 369 #endif // !OS_IOS | 368 #endif // !OS_IOS |
| 370 | 369 |
| 371 BrowserContext::~BrowserContext() { | 370 BrowserContext::~BrowserContext() { |
| 372 CHECK(GetUserData(kMojoWasInitialized)) | 371 CHECK(GetUserData(kMojoWasInitialized)) |
| 373 << "Attempting to destroy a BrowserContext that never called " | 372 << "Attempting to destroy a BrowserContext that never called " |
| 374 << "Initialize()"; | 373 << "Initialize()"; |
| 375 | 374 |
| 376 #if !defined(OS_IOS) | 375 #if !defined(OS_IOS) |
| 377 if (GetUserData(kDownloadManagerKeyName)) | 376 if (GetUserData(kDownloadManagerKeyName)) |
| 378 GetDownloadManager(this)->Shutdown(); | 377 GetDownloadManager(this)->Shutdown(); |
| 379 #endif | 378 #endif |
| 380 } | 379 } |
| 381 | 380 |
| 382 } // namespace content | 381 } // namespace content |
| OLD | NEW |