| 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 <memory> |
| 11 #include <utility> | 12 #include <utility> |
| 13 #include <vector> |
| 12 | 14 |
| 15 #include "base/command_line.h" |
| 13 #include "base/guid.h" | 16 #include "base/guid.h" |
| 14 #include "base/lazy_instance.h" | 17 #include "base/lazy_instance.h" |
| 18 #include "base/macros.h" |
| 15 #include "base/rand_util.h" | 19 #include "base/rand_util.h" |
| 16 #include "build/build_config.h" | 20 #include "build/build_config.h" |
| 21 #include "components/profile_service/profile_app.h" |
| 22 #include "components/profile_service/public/cpp/constants.h" |
| 17 #include "components/profile_service/user_id_map.h" | 23 #include "components/profile_service/user_id_map.h" |
| 18 #include "content/browser/download/download_manager_impl.h" | 24 #include "content/browser/download/download_manager_impl.h" |
| 19 #include "content/browser/fileapi/chrome_blob_storage_context.h" | 25 #include "content/browser/fileapi/chrome_blob_storage_context.h" |
| 20 #include "content/browser/indexed_db/indexed_db_context_impl.h" | 26 #include "content/browser/indexed_db/indexed_db_context_impl.h" |
| 21 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 27 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| 28 #include "content/browser/mojo/browser_shell_connection.h" |
| 29 #include "content/browser/mojo/constants.h" |
| 22 #include "content/browser/push_messaging/push_messaging_router.h" | 30 #include "content/browser/push_messaging/push_messaging_router.h" |
| 23 #include "content/browser/storage_partition_impl_map.h" | 31 #include "content/browser/storage_partition_impl_map.h" |
| 24 #include "content/common/child_process_host_impl.h" | 32 #include "content/common/child_process_host_impl.h" |
| 25 #include "content/public/browser/blob_handle.h" | 33 #include "content/public/browser/blob_handle.h" |
| 26 #include "content/public/browser/browser_thread.h" | 34 #include "content/public/browser/browser_thread.h" |
| 27 #include "content/public/browser/content_browser_client.h" | 35 #include "content/public/browser/content_browser_client.h" |
| 28 #include "content/public/browser/site_instance.h" | 36 #include "content/public/browser/site_instance.h" |
| 37 #include "content/public/common/content_switches.h" |
| 38 #include "content/public/common/mojo_shell_connection.h" |
| 29 #include "net/cookies/cookie_store.h" | 39 #include "net/cookies/cookie_store.h" |
| 30 #include "net/ssl/channel_id_service.h" | 40 #include "net/ssl/channel_id_service.h" |
| 31 #include "net/ssl/channel_id_store.h" | 41 #include "net/ssl/channel_id_store.h" |
| 32 #include "net/url_request/url_request_context.h" | 42 #include "net/url_request/url_request_context.h" |
| 33 #include "net/url_request/url_request_context_getter.h" | 43 #include "net/url_request/url_request_context_getter.h" |
| 44 #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" |
| 34 #include "storage/browser/database/database_tracker.h" | 47 #include "storage/browser/database/database_tracker.h" |
| 35 #include "storage/browser/fileapi/external_mount_points.h" | 48 #include "storage/browser/fileapi/external_mount_points.h" |
| 36 | 49 |
| 37 using base::UserDataAdapter; | 50 using base::UserDataAdapter; |
| 38 | 51 |
| 39 namespace content { | 52 namespace content { |
| 40 | 53 |
| 41 namespace { | 54 namespace { |
| 42 | 55 |
| 43 base::LazyInstance<std::set<std::string>> g_used_user_ids = | 56 base::LazyInstance<std::set<std::string>> g_used_user_ids = |
| 44 LAZY_INSTANCE_INITIALIZER; | 57 LAZY_INSTANCE_INITIALIZER; |
| 45 base::LazyInstance<std::vector<std::pair<BrowserContext*, std::string>>> | 58 base::LazyInstance<std::vector<std::pair<BrowserContext*, std::string>>> |
| 46 g_context_to_user_id = LAZY_INSTANCE_INITIALIZER; | 59 g_context_to_user_id = LAZY_INSTANCE_INITIALIZER; |
| 47 | 60 |
| 48 // Key names on BrowserContext. | 61 // Key names on BrowserContext. |
| 49 const char kDownloadManagerKeyName[] = "download_manager"; | 62 const char kDownloadManagerKeyName[] = "download_manager"; |
| 63 const char kMojoShellConnection[] = "mojo-shell-connection"; |
| 64 const char kMojoWasInitialized[] = "mojo-was-initialized"; |
| 50 const char kStoragePartitionMapKeyName[] = "content_storage_partition_map"; | 65 const char kStoragePartitionMapKeyName[] = "content_storage_partition_map"; |
| 51 | 66 |
| 52 const char kMojoWasInitialized[] = "mojo-was-initialized"; | |
| 53 | |
| 54 #if defined(OS_CHROMEOS) | 67 #if defined(OS_CHROMEOS) |
| 55 const char kMountPointsKey[] = "mount_points"; | 68 const char kMountPointsKey[] = "mount_points"; |
| 56 #endif // defined(OS_CHROMEOS) | 69 #endif // defined(OS_CHROMEOS) |
| 57 | 70 |
| 58 StoragePartitionImplMap* GetStoragePartitionMap( | 71 StoragePartitionImplMap* GetStoragePartitionMap( |
| 59 BrowserContext* browser_context) { | 72 BrowserContext* browser_context) { |
| 60 StoragePartitionImplMap* partition_map = | 73 StoragePartitionImplMap* partition_map = |
| 61 static_cast<StoragePartitionImplMap*>( | 74 static_cast<StoragePartitionImplMap*>( |
| 62 browser_context->GetUserData(kStoragePartitionMapKeyName)); | 75 browser_context->GetUserData(kStoragePartitionMapKeyName)); |
| 63 if (!partition_map) { | 76 if (!partition_map) { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 wrapper->process_manager()->Shutdown(); | 116 wrapper->process_manager()->Shutdown(); |
| 104 } | 117 } |
| 105 | 118 |
| 106 void SetDownloadManager(BrowserContext* context, | 119 void SetDownloadManager(BrowserContext* context, |
| 107 content::DownloadManager* download_manager) { | 120 content::DownloadManager* download_manager) { |
| 108 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 121 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 109 DCHECK(download_manager); | 122 DCHECK(download_manager); |
| 110 context->SetUserData(kDownloadManagerKeyName, download_manager); | 123 context->SetUserData(kDownloadManagerKeyName, download_manager); |
| 111 } | 124 } |
| 112 | 125 |
| 126 class 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 |
| 113 } // namespace | 145 } // namespace |
| 114 | 146 |
| 115 // static | 147 // static |
| 116 void BrowserContext::AsyncObliterateStoragePartition( | 148 void BrowserContext::AsyncObliterateStoragePartition( |
| 117 BrowserContext* browser_context, | 149 BrowserContext* browser_context, |
| 118 const GURL& site, | 150 const GURL& site, |
| 119 const base::Closure& on_gc_required) { | 151 const base::Closure& on_gc_required) { |
| 120 GetStoragePartitionMap(browser_context)->AsyncObliterate(site, | 152 GetStoragePartitionMap(browser_context)->AsyncObliterate(site, |
| 121 on_gc_required); | 153 on_gc_required); |
| 122 } | 154 } |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 make_scoped_refptr(indexed_db_context_impl))); | 355 make_scoped_refptr(indexed_db_context_impl))); |
| 324 } | 356 } |
| 325 } | 357 } |
| 326 | 358 |
| 327 void BrowserContext::SetDownloadManagerForTesting( | 359 void BrowserContext::SetDownloadManagerForTesting( |
| 328 BrowserContext* browser_context, | 360 BrowserContext* browser_context, |
| 329 DownloadManager* download_manager) { | 361 DownloadManager* download_manager) { |
| 330 SetDownloadManager(browser_context, download_manager); | 362 SetDownloadManager(browser_context, download_manager); |
| 331 } | 363 } |
| 332 | 364 |
| 365 // static |
| 333 void BrowserContext::Initialize( | 366 void BrowserContext::Initialize( |
| 334 BrowserContext* browser_context, | 367 BrowserContext* browser_context, |
| 335 const base::FilePath& path) { | 368 const base::FilePath& path) { |
| 336 // Generate a GUID for |browser_context| to use as the Mojo user id. | 369 // Generate a GUID for |browser_context| to use as the Mojo user id. |
| 337 std::string new_id = base::GenerateGUID(); | 370 std::string new_id = base::GenerateGUID(); |
| 338 while (g_used_user_ids.Get().find(new_id) != g_used_user_ids.Get().end()) | 371 while (g_used_user_ids.Get().find(new_id) != g_used_user_ids.Get().end()) |
| 339 new_id = base::GenerateGUID(); | 372 new_id = base::GenerateGUID(); |
| 340 | 373 |
| 341 g_used_user_ids.Get().insert(new_id); | 374 g_used_user_ids.Get().insert(new_id); |
| 342 g_context_to_user_id.Get().push_back(std::make_pair(browser_context, new_id)); | 375 g_context_to_user_id.Get().push_back(std::make_pair(browser_context, new_id)); |
| 343 | 376 |
| 344 profile::AssociateMojoUserIDWithProfileDir(new_id, path); | 377 profile::AssociateMojoUserIDWithProfileDir(new_id, path); |
| 345 browser_context->SetUserData(kMojoWasInitialized, | 378 browser_context->SetUserData(kMojoWasInitialized, |
| 346 new base::SupportsUserData::Data); | 379 new base::SupportsUserData::Data); |
| 380 |
| 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(¶ms), |
| 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 } |
| 347 } | 418 } |
| 348 | 419 |
| 420 // static |
| 349 const std::string& BrowserContext::GetMojoUserIdFor( | 421 const std::string& BrowserContext::GetMojoUserIdFor( |
| 350 BrowserContext* browser_context) { | 422 BrowserContext* browser_context) { |
| 351 CHECK(browser_context->GetUserData(kMojoWasInitialized)) | 423 CHECK(browser_context->GetUserData(kMojoWasInitialized)) |
| 352 << "Attempting to get the mojo user id for a BrowserContext that was " | 424 << "Attempting to get the mojo user id for a BrowserContext that was " |
| 353 << "never Initialize()ed."; | 425 << "never Initialize()ed."; |
| 354 | 426 |
| 355 auto it = std::find_if( | 427 auto it = std::find_if( |
| 356 g_context_to_user_id.Get().begin(), | 428 g_context_to_user_id.Get().begin(), |
| 357 g_context_to_user_id.Get().end(), | 429 g_context_to_user_id.Get().end(), |
| 358 [&browser_context](const std::pair<BrowserContext*, std::string>& p) { | 430 [&browser_context](const std::pair<BrowserContext*, std::string>& p) { |
| 359 return p.first == browser_context; }); | 431 return p.first == browser_context; }); |
| 360 CHECK(it != g_context_to_user_id.Get().end()); | 432 CHECK(it != g_context_to_user_id.Get().end()); |
| 361 return it->second; | 433 return it->second; |
| 362 } | 434 } |
| 363 | 435 |
| 436 // static |
| 437 mojo::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 |
| 364 BrowserContext::~BrowserContext() { | 447 BrowserContext::~BrowserContext() { |
| 365 CHECK(GetUserData(kMojoWasInitialized)) | 448 CHECK(GetUserData(kMojoWasInitialized)) |
| 366 << "Attempting to destroy a BrowserContext that never called " | 449 << "Attempting to destroy a BrowserContext that never called " |
| 367 << "Initialize()"; | 450 << "Initialize()"; |
| 368 | 451 |
| 369 if (GetUserData(kDownloadManagerKeyName)) | 452 if (GetUserData(kDownloadManagerKeyName)) |
| 370 GetDownloadManager(this)->Shutdown(); | 453 GetDownloadManager(this)->Shutdown(); |
| 371 } | 454 } |
| 372 | 455 |
| 373 } // namespace content | 456 } // namespace content |
| OLD | NEW |