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> |
(...skipping 14 matching lines...) Expand all Loading... |
25 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 25 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
26 #include "content/browser/push_messaging/push_messaging_router.h" | 26 #include "content/browser/push_messaging/push_messaging_router.h" |
27 #include "content/browser/storage_partition_impl_map.h" | 27 #include "content/browser/storage_partition_impl_map.h" |
28 #include "content/common/child_process_host_impl.h" | 28 #include "content/common/child_process_host_impl.h" |
29 #include "content/public/browser/blob_handle.h" | 29 #include "content/public/browser/blob_handle.h" |
30 #include "content/public/browser/browser_thread.h" | 30 #include "content/public/browser/browser_thread.h" |
31 #include "content/public/browser/content_browser_client.h" | 31 #include "content/public/browser/content_browser_client.h" |
32 #include "content/public/browser/render_process_host.h" | 32 #include "content/public/browser/render_process_host.h" |
33 #include "content/public/browser/site_instance.h" | 33 #include "content/public/browser/site_instance.h" |
34 #include "content/public/common/content_switches.h" | 34 #include "content/public/common/content_switches.h" |
35 #include "content/public/common/mojo_shell_connection.h" | 35 #include "content/public/common/service_manager_connection.h" |
36 #include "content/public/common/service_names.h" | 36 #include "content/public/common/service_names.h" |
37 #include "net/cookies/cookie_store.h" | 37 #include "net/cookies/cookie_store.h" |
38 #include "net/ssl/channel_id_service.h" | 38 #include "net/ssl/channel_id_service.h" |
39 #include "net/ssl/channel_id_store.h" | 39 #include "net/ssl/channel_id_store.h" |
40 #include "net/url_request/url_request_context.h" | 40 #include "net/url_request/url_request_context.h" |
41 #include "net/url_request/url_request_context_getter.h" | 41 #include "net/url_request/url_request_context_getter.h" |
42 #include "services/file/file_service.h" | 42 #include "services/file/file_service.h" |
43 #include "services/file/public/cpp/constants.h" | 43 #include "services/file/public/cpp/constants.h" |
44 #include "services/file/user_id_map.h" | 44 #include "services/file/user_id_map.h" |
45 #include "services/shell/public/cpp/connection.h" | 45 #include "services/shell/public/cpp/connection.h" |
46 #include "services/shell/public/cpp/connector.h" | 46 #include "services/shell/public/cpp/connector.h" |
47 #include "services/shell/public/interfaces/service.mojom.h" | 47 #include "services/shell/public/interfaces/service.mojom.h" |
48 #include "storage/browser/database/database_tracker.h" | 48 #include "storage/browser/database/database_tracker.h" |
49 #include "storage/browser/fileapi/external_mount_points.h" | 49 #include "storage/browser/fileapi/external_mount_points.h" |
50 | 50 |
51 using base::UserDataAdapter; | 51 using base::UserDataAdapter; |
52 | 52 |
53 namespace content { | 53 namespace content { |
54 | 54 |
55 namespace { | 55 namespace { |
56 | 56 |
57 base::LazyInstance<std::map<std::string, BrowserContext*>> | 57 base::LazyInstance<std::map<std::string, BrowserContext*>> |
58 g_user_id_to_context = LAZY_INSTANCE_INITIALIZER; | 58 g_user_id_to_context = LAZY_INSTANCE_INITIALIZER; |
59 | 59 |
60 class ShellUserIdHolder : public base::SupportsUserData::Data { | 60 class ServiceUserIdHolder : public base::SupportsUserData::Data { |
61 public: | 61 public: |
62 explicit ShellUserIdHolder(const std::string& user_id) : user_id_(user_id) {} | 62 explicit ServiceUserIdHolder(const std::string& user_id) |
63 ~ShellUserIdHolder() override {} | 63 : user_id_(user_id) {} |
| 64 ~ServiceUserIdHolder() override {} |
64 | 65 |
65 const std::string& user_id() const { return user_id_; } | 66 const std::string& user_id() const { return user_id_; } |
66 | 67 |
67 private: | 68 private: |
68 std::string user_id_; | 69 std::string user_id_; |
69 | 70 |
70 DISALLOW_COPY_AND_ASSIGN(ShellUserIdHolder); | 71 DISALLOW_COPY_AND_ASSIGN(ServiceUserIdHolder); |
71 }; | 72 }; |
72 | 73 |
73 // Key names on BrowserContext. | 74 // Key names on BrowserContext. |
74 const char kDownloadManagerKeyName[] = "download_manager"; | 75 const char kDownloadManagerKeyName[] = "download_manager"; |
75 const char kMojoShellConnection[] = "mojo-shell-connection"; | |
76 const char kMojoWasInitialized[] = "mojo-was-initialized"; | 76 const char kMojoWasInitialized[] = "mojo-was-initialized"; |
77 const char kMojoShellUserId[] = "mojo-shell-user-id"; | 77 const char kServiceManagerConnection[] = "service-manager-connection"; |
| 78 const char kServiceUserId[] = "service-user-id"; |
78 const char kStoragePartitionMapKeyName[] = "content_storage_partition_map"; | 79 const char kStoragePartitionMapKeyName[] = "content_storage_partition_map"; |
79 | 80 |
80 #if defined(OS_CHROMEOS) | 81 #if defined(OS_CHROMEOS) |
81 const char kMountPointsKey[] = "mount_points"; | 82 const char kMountPointsKey[] = "mount_points"; |
82 #endif // defined(OS_CHROMEOS) | 83 #endif // defined(OS_CHROMEOS) |
83 | 84 |
84 void RemoveBrowserContextFromUserIdMap(BrowserContext* browser_context) { | 85 void RemoveBrowserContextFromUserIdMap(BrowserContext* browser_context) { |
85 ShellUserIdHolder* holder = static_cast<ShellUserIdHolder*>( | 86 ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>( |
86 browser_context->GetUserData(kMojoShellUserId)); | 87 browser_context->GetUserData(kServiceUserId)); |
87 if (holder) { | 88 if (holder) { |
88 auto it = g_user_id_to_context.Get().find(holder->user_id()); | 89 auto it = g_user_id_to_context.Get().find(holder->user_id()); |
89 if (it != g_user_id_to_context.Get().end()) | 90 if (it != g_user_id_to_context.Get().end()) |
90 g_user_id_to_context.Get().erase(it); | 91 g_user_id_to_context.Get().erase(it); |
91 } | 92 } |
92 } | 93 } |
93 | 94 |
94 StoragePartitionImplMap* GetStoragePartitionMap( | 95 StoragePartitionImplMap* GetStoragePartitionMap( |
95 BrowserContext* browser_context) { | 96 BrowserContext* browser_context) { |
96 StoragePartitionImplMap* partition_map = | 97 StoragePartitionImplMap* partition_map = |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 wrapper->process_manager()->Shutdown(); | 140 wrapper->process_manager()->Shutdown(); |
140 } | 141 } |
141 | 142 |
142 void SetDownloadManager(BrowserContext* context, | 143 void SetDownloadManager(BrowserContext* context, |
143 content::DownloadManager* download_manager) { | 144 content::DownloadManager* download_manager) { |
144 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 145 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
145 DCHECK(download_manager); | 146 DCHECK(download_manager); |
146 context->SetUserData(kDownloadManagerKeyName, download_manager); | 147 context->SetUserData(kDownloadManagerKeyName, download_manager); |
147 } | 148 } |
148 | 149 |
149 class BrowserContextShellConnectionHolder | 150 class BrowserContextServiceManagerConnectionHolder |
150 : public base::SupportsUserData::Data { | 151 : public base::SupportsUserData::Data { |
151 public: | 152 public: |
152 BrowserContextShellConnectionHolder( | 153 BrowserContextServiceManagerConnectionHolder( |
153 std::unique_ptr<shell::Connection> connection, | 154 std::unique_ptr<shell::Connection> connection, |
154 shell::mojom::ServiceRequest request) | 155 shell::mojom::ServiceRequest request) |
155 : root_connection_(std::move(connection)), | 156 : root_connection_(std::move(connection)), |
156 shell_connection_(MojoShellConnection::Create( | 157 service_manager_connection_(ServiceManagerConnection::Create( |
157 std::move(request), | 158 std::move(request), |
158 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO))) {} | 159 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO))) {} |
159 ~BrowserContextShellConnectionHolder() override {} | 160 ~BrowserContextServiceManagerConnectionHolder() override {} |
160 | 161 |
161 MojoShellConnection* shell_connection() { return shell_connection_.get(); } | 162 ServiceManagerConnection* service_manager_connection() { |
| 163 return service_manager_connection_.get(); |
| 164 } |
162 | 165 |
163 private: | 166 private: |
164 std::unique_ptr<shell::Connection> root_connection_; | 167 std::unique_ptr<shell::Connection> root_connection_; |
165 std::unique_ptr<MojoShellConnection> shell_connection_; | 168 std::unique_ptr<ServiceManagerConnection> service_manager_connection_; |
166 | 169 |
167 DISALLOW_COPY_AND_ASSIGN(BrowserContextShellConnectionHolder); | 170 DISALLOW_COPY_AND_ASSIGN(BrowserContextServiceManagerConnectionHolder); |
168 }; | 171 }; |
169 | 172 |
170 } // namespace | 173 } // namespace |
171 | 174 |
172 // static | 175 // static |
173 void BrowserContext::AsyncObliterateStoragePartition( | 176 void BrowserContext::AsyncObliterateStoragePartition( |
174 BrowserContext* browser_context, | 177 BrowserContext* browser_context, |
175 const GURL& site, | 178 const GURL& site, |
176 const base::Closure& on_gc_required) { | 179 const base::Closure& on_gc_required) { |
177 GetStoragePartitionMap(browser_context)->AsyncObliterate(site, | 180 GetStoragePartitionMap(browser_context)->AsyncObliterate(site, |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
399 SetDownloadManager(browser_context, download_manager); | 402 SetDownloadManager(browser_context, download_manager); |
400 } | 403 } |
401 | 404 |
402 // static | 405 // static |
403 void BrowserContext::Initialize( | 406 void BrowserContext::Initialize( |
404 BrowserContext* browser_context, | 407 BrowserContext* browser_context, |
405 const base::FilePath& path) { | 408 const base::FilePath& path) { |
406 | 409 |
407 std::string new_id; | 410 std::string new_id; |
408 if (GetContentClient() && GetContentClient()->browser()) { | 411 if (GetContentClient() && GetContentClient()->browser()) { |
409 new_id = GetContentClient()->browser()->GetShellUserIdForBrowserContext( | 412 new_id = GetContentClient()->browser()->GetServiceUserIdForBrowserContext( |
410 browser_context); | 413 browser_context); |
411 } else { | 414 } else { |
412 // Some test scenarios initialize a BrowserContext without a content client. | 415 // Some test scenarios initialize a BrowserContext without a content client. |
413 new_id = base::GenerateGUID(); | 416 new_id = base::GenerateGUID(); |
414 } | 417 } |
415 | 418 |
416 ShellUserIdHolder* holder = static_cast<ShellUserIdHolder*>( | 419 ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>( |
417 browser_context->GetUserData(kMojoShellUserId)); | 420 browser_context->GetUserData(kServiceUserId)); |
418 if (holder) | 421 if (holder) |
419 file::ForgetShellUserIdUserDirAssociation(holder->user_id()); | 422 file::ForgetServiceUserIdUserDirAssociation(holder->user_id()); |
420 file::AssociateShellUserIdWithUserDir(new_id, path); | 423 file::AssociateServiceUserIdWithUserDir(new_id, path); |
421 RemoveBrowserContextFromUserIdMap(browser_context); | 424 RemoveBrowserContextFromUserIdMap(browser_context); |
422 g_user_id_to_context.Get()[new_id] = browser_context; | 425 g_user_id_to_context.Get()[new_id] = browser_context; |
423 browser_context->SetUserData(kMojoShellUserId, | 426 browser_context->SetUserData(kServiceUserId, |
424 new ShellUserIdHolder(new_id)); | 427 new ServiceUserIdHolder(new_id)); |
425 | 428 |
426 browser_context->SetUserData(kMojoWasInitialized, | 429 browser_context->SetUserData(kMojoWasInitialized, |
427 new base::SupportsUserData::Data); | 430 new base::SupportsUserData::Data); |
428 | 431 |
429 MojoShellConnection* shell = MojoShellConnection::GetForProcess(); | 432 ServiceManagerConnection* service_manager_connection = |
430 if (shell && base::MessageLoop::current()) { | 433 ServiceManagerConnection::GetForProcess(); |
| 434 if (service_manager_connection && base::MessageLoop::current()) { |
431 // NOTE: Many unit tests create a TestBrowserContext without initializing | 435 // NOTE: Many unit tests create a TestBrowserContext without initializing |
432 // Mojo or the global Mojo shell connection. | 436 // Mojo or the global service manager connection. |
433 | 437 |
434 shell::mojom::ServicePtr service; | 438 shell::mojom::ServicePtr service; |
435 shell::mojom::ServiceRequest service_request = mojo::GetProxy(&service); | 439 shell::mojom::ServiceRequest service_request = mojo::GetProxy(&service); |
436 | 440 |
437 shell::mojom::PIDReceiverPtr pid_receiver; | 441 shell::mojom::PIDReceiverPtr pid_receiver; |
438 shell::Connector::ConnectParams params( | 442 shell::Connector::ConnectParams params( |
439 shell::Identity(kBrowserMojoApplicationName, new_id)); | 443 shell::Identity(kBrowserServiceName, new_id)); |
440 params.set_client_process_connection(std::move(service), | 444 params.set_client_process_connection(std::move(service), |
441 mojo::GetProxy(&pid_receiver)); | 445 mojo::GetProxy(&pid_receiver)); |
442 pid_receiver->SetPID(base::GetCurrentProcId()); | 446 pid_receiver->SetPID(base::GetCurrentProcId()); |
443 | 447 |
444 BrowserContextShellConnectionHolder* connection_holder = | 448 BrowserContextServiceManagerConnectionHolder* connection_holder = |
445 new BrowserContextShellConnectionHolder( | 449 new BrowserContextServiceManagerConnectionHolder( |
446 shell->GetConnector()->Connect(¶ms), | 450 service_manager_connection->GetConnector()->Connect(¶ms), |
447 std::move(service_request)); | 451 std::move(service_request)); |
448 browser_context->SetUserData(kMojoShellConnection, connection_holder); | 452 browser_context->SetUserData(kServiceManagerConnection, connection_holder); |
449 | 453 |
450 MojoShellConnection* connection = connection_holder->shell_connection(); | 454 ServiceManagerConnection* connection = |
| 455 connection_holder->service_manager_connection(); |
451 connection->Start(); | 456 connection->Start(); |
452 | 457 |
453 // New embedded service factories should be added to |connection| here. | 458 // New embedded service factories should be added to |connection| here. |
454 | 459 |
455 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 460 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
456 switches::kMojoLocalStorage)) { | 461 switches::kMojoLocalStorage)) { |
457 MojoApplicationInfo info; | 462 ServiceInfo info; |
458 info.application_factory = | 463 info.factory = |
459 base::Bind(&file::CreateFileService, | 464 base::Bind(&file::CreateFileService, |
460 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE), | 465 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE), |
461 BrowserThread::GetTaskRunnerForThread(BrowserThread::DB)); | 466 BrowserThread::GetTaskRunnerForThread(BrowserThread::DB)); |
462 connection->AddEmbeddedService(file::kFileServiceName, info); | 467 connection->AddEmbeddedService(file::kFileServiceName, info); |
463 } | 468 } |
464 } | 469 } |
465 } | 470 } |
466 | 471 |
467 // static | 472 // static |
468 const std::string& BrowserContext::GetShellUserIdFor( | 473 const std::string& BrowserContext::GetServiceUserIdFor( |
469 BrowserContext* browser_context) { | 474 BrowserContext* browser_context) { |
470 CHECK(browser_context->GetUserData(kMojoWasInitialized)) | 475 CHECK(browser_context->GetUserData(kMojoWasInitialized)) |
471 << "Attempting to get the mojo user id for a BrowserContext that was " | 476 << "Attempting to get the mojo user id for a BrowserContext that was " |
472 << "never Initialize()ed."; | 477 << "never Initialize()ed."; |
473 | 478 |
474 ShellUserIdHolder* holder = static_cast<ShellUserIdHolder*>( | 479 ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>( |
475 browser_context->GetUserData(kMojoShellUserId)); | 480 browser_context->GetUserData(kServiceUserId)); |
476 return holder->user_id(); | 481 return holder->user_id(); |
477 } | 482 } |
478 | 483 |
479 // static | 484 // static |
480 BrowserContext* BrowserContext::GetBrowserContextForShellUserId( | 485 BrowserContext* BrowserContext::GetBrowserContextForServiceUserId( |
481 const std::string& user_id) { | 486 const std::string& user_id) { |
482 auto it = g_user_id_to_context.Get().find(user_id); | 487 auto it = g_user_id_to_context.Get().find(user_id); |
483 return it != g_user_id_to_context.Get().end() ? it->second : nullptr; | 488 return it != g_user_id_to_context.Get().end() ? it->second : nullptr; |
484 } | 489 } |
485 | 490 |
486 // static | 491 // static |
487 shell::Connector* BrowserContext::GetShellConnectorFor( | 492 shell::Connector* BrowserContext::GetConnectorFor( |
488 BrowserContext* browser_context) { | 493 BrowserContext* browser_context) { |
489 MojoShellConnection* connection = GetMojoShellConnectionFor(browser_context); | 494 ServiceManagerConnection* connection = |
| 495 GetServiceManagerConnectionFor(browser_context); |
490 return connection ? connection->GetConnector() : nullptr; | 496 return connection ? connection->GetConnector() : nullptr; |
491 } | 497 } |
492 | 498 |
493 // static | 499 // static |
494 MojoShellConnection* BrowserContext::GetMojoShellConnectionFor( | 500 ServiceManagerConnection* BrowserContext::GetServiceManagerConnectionFor( |
495 BrowserContext* browser_context) { | 501 BrowserContext* browser_context) { |
496 BrowserContextShellConnectionHolder* connection_holder = | 502 BrowserContextServiceManagerConnectionHolder* connection_holder = |
497 static_cast<BrowserContextShellConnectionHolder*>( | 503 static_cast<BrowserContextServiceManagerConnectionHolder*>( |
498 browser_context->GetUserData(kMojoShellConnection)); | 504 browser_context->GetUserData(kServiceManagerConnection)); |
499 return connection_holder ? connection_holder->shell_connection() : nullptr; | 505 return connection_holder ? connection_holder->service_manager_connection() |
| 506 : nullptr; |
500 } | 507 } |
501 | 508 |
502 BrowserContext::~BrowserContext() { | 509 BrowserContext::~BrowserContext() { |
503 CHECK(GetUserData(kMojoWasInitialized)) | 510 CHECK(GetUserData(kMojoWasInitialized)) |
504 << "Attempting to destroy a BrowserContext that never called " | 511 << "Attempting to destroy a BrowserContext that never called " |
505 << "Initialize()"; | 512 << "Initialize()"; |
506 | 513 |
507 DCHECK(!GetUserData(kStoragePartitionMapKeyName)) | 514 DCHECK(!GetUserData(kStoragePartitionMapKeyName)) |
508 << "StoragePartitionMap is not shut down properly"; | 515 << "StoragePartitionMap is not shut down properly"; |
509 | 516 |
510 RemoveBrowserContextFromUserIdMap(this); | 517 RemoveBrowserContextFromUserIdMap(this); |
511 | 518 |
512 if (GetUserData(kDownloadManagerKeyName)) | 519 if (GetUserData(kDownloadManagerKeyName)) |
513 GetDownloadManager(this)->Shutdown(); | 520 GetDownloadManager(this)->Shutdown(); |
514 } | 521 } |
515 | 522 |
516 void BrowserContext::ShutdownStoragePartitions() { | 523 void BrowserContext::ShutdownStoragePartitions() { |
517 if (GetUserData(kStoragePartitionMapKeyName)) | 524 if (GetUserData(kStoragePartitionMapKeyName)) |
518 RemoveUserData(kStoragePartitionMapKeyName); | 525 RemoveUserData(kStoragePartitionMapKeyName); |
519 } | 526 } |
520 | 527 |
521 } // namespace content | 528 } // namespace content |
OLD | NEW |