Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(75)

Side by Side Diff: content/browser/browser_context.cc

Issue 2815913005: Switch to using scoped_ptr with UserData (Closed)
Patch Set: rebase Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
9 #include <algorithm> 10 #include <algorithm>
10 #include <limits> 11 #include <limits>
11 #include <memory> 12 #include <memory>
12 #include <utility> 13 #include <utility>
13 #include <vector> 14 #include <vector>
14 15
15 #include "base/base64.h" 16 #include "base/base64.h"
16 #include "base/command_line.h" 17 #include "base/command_line.h"
17 #include "base/guid.h" 18 #include "base/guid.h"
18 #include "base/lazy_instance.h" 19 #include "base/lazy_instance.h"
19 #include "base/logging.h" 20 #include "base/logging.h"
20 #include "base/macros.h" 21 #include "base/macros.h"
22 #include "base/memory/ptr_util.h"
21 #include "base/rand_util.h" 23 #include "base/rand_util.h"
22 #include "base/threading/thread_task_runner_handle.h" 24 #include "base/threading/thread_task_runner_handle.h"
23 #include "build/build_config.h" 25 #include "build/build_config.h"
24 #include "content/browser/blob_storage/chrome_blob_storage_context.h" 26 #include "content/browser/blob_storage/chrome_blob_storage_context.h"
25 #include "content/browser/download/download_manager_impl.h" 27 #include "content/browser/download/download_manager_impl.h"
26 #include "content/browser/indexed_db/indexed_db_context_impl.h" 28 #include "content/browser/indexed_db/indexed_db_context_impl.h"
27 #include "content/browser/loader/resource_dispatcher_host_impl.h" 29 #include "content/browser/loader/resource_dispatcher_host_impl.h"
28 #include "content/browser/push_messaging/push_messaging_router.h" 30 #include "content/browser/push_messaging/push_messaging_router.h"
29 #include "content/browser/storage_partition_impl_map.h" 31 #include "content/browser/storage_partition_impl_map.h"
30 #include "content/common/child_process_host_impl.h" 32 #include "content/common/child_process_host_impl.h"
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 g_user_id_to_context.Get().erase(it); 94 g_user_id_to_context.Get().erase(it);
93 } 95 }
94 } 96 }
95 97
96 StoragePartitionImplMap* GetStoragePartitionMap( 98 StoragePartitionImplMap* GetStoragePartitionMap(
97 BrowserContext* browser_context) { 99 BrowserContext* browser_context) {
98 StoragePartitionImplMap* partition_map = 100 StoragePartitionImplMap* partition_map =
99 static_cast<StoragePartitionImplMap*>( 101 static_cast<StoragePartitionImplMap*>(
100 browser_context->GetUserData(kStoragePartitionMapKeyName)); 102 browser_context->GetUserData(kStoragePartitionMapKeyName));
101 if (!partition_map) { 103 if (!partition_map) {
102 partition_map = new StoragePartitionImplMap(browser_context); 104 auto partition_map_owned =
103 browser_context->SetUserData(kStoragePartitionMapKeyName, partition_map); 105 base::MakeUnique<StoragePartitionImplMap>(browser_context);
106 partition_map = partition_map_owned.get();
107 browser_context->SetUserData(kStoragePartitionMapKeyName,
108 std::move(partition_map_owned));
104 } 109 }
105 return partition_map; 110 return partition_map;
106 } 111 }
107 112
108 StoragePartition* GetStoragePartitionFromConfig( 113 StoragePartition* GetStoragePartitionFromConfig(
109 BrowserContext* browser_context, 114 BrowserContext* browser_context,
110 const std::string& partition_domain, 115 const std::string& partition_domain,
111 const std::string& partition_name, 116 const std::string& partition_name,
112 bool in_memory) { 117 bool in_memory) {
113 StoragePartitionImplMap* partition_map = 118 StoragePartitionImplMap* partition_map =
(...skipping 20 matching lines...) Expand all
134 indexed_db_context->SetForceKeepSessionState(); 139 indexed_db_context->SetForceKeepSessionState();
135 } 140 }
136 141
137 void ShutdownServiceWorkerContext(StoragePartition* partition) { 142 void ShutdownServiceWorkerContext(StoragePartition* partition) {
138 ServiceWorkerContextWrapper* wrapper = 143 ServiceWorkerContextWrapper* wrapper =
139 static_cast<ServiceWorkerContextWrapper*>( 144 static_cast<ServiceWorkerContextWrapper*>(
140 partition->GetServiceWorkerContext()); 145 partition->GetServiceWorkerContext());
141 wrapper->process_manager()->Shutdown(); 146 wrapper->process_manager()->Shutdown();
142 } 147 }
143 148
144 void SetDownloadManager(BrowserContext* context, 149 void SetDownloadManager(
145 content::DownloadManager* download_manager) { 150 BrowserContext* context,
151 std::unique_ptr<content::DownloadManager> download_manager) {
146 DCHECK_CURRENTLY_ON(BrowserThread::UI); 152 DCHECK_CURRENTLY_ON(BrowserThread::UI);
147 DCHECK(download_manager); 153 DCHECK(download_manager);
148 context->SetUserData(kDownloadManagerKeyName, download_manager); 154 context->SetUserData(kDownloadManagerKeyName, std::move(download_manager));
149 } 155 }
150 156
151 class BrowserContextServiceManagerConnectionHolder 157 class BrowserContextServiceManagerConnectionHolder
152 : public base::SupportsUserData::Data { 158 : public base::SupportsUserData::Data {
153 public: 159 public:
154 explicit BrowserContextServiceManagerConnectionHolder( 160 explicit BrowserContextServiceManagerConnectionHolder(
155 service_manager::mojom::ServiceRequest request) 161 service_manager::mojom::ServiceRequest request)
156 : service_manager_connection_(ServiceManagerConnection::Create( 162 : service_manager_connection_(ServiceManagerConnection::Create(
157 std::move(request), 163 std::move(request),
158 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO))) {} 164 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO))) {}
(...skipping 30 matching lines...) Expand all
189 } 195 }
190 196
191 DownloadManager* BrowserContext::GetDownloadManager( 197 DownloadManager* BrowserContext::GetDownloadManager(
192 BrowserContext* context) { 198 BrowserContext* context) {
193 DCHECK_CURRENTLY_ON(BrowserThread::UI); 199 DCHECK_CURRENTLY_ON(BrowserThread::UI);
194 if (!context->GetUserData(kDownloadManagerKeyName)) { 200 if (!context->GetUserData(kDownloadManagerKeyName)) {
195 DownloadManager* download_manager = 201 DownloadManager* download_manager =
196 new DownloadManagerImpl( 202 new DownloadManagerImpl(
197 GetContentClient()->browser()->GetNetLog(), context); 203 GetContentClient()->browser()->GetNetLog(), context);
198 204
199 SetDownloadManager(context, download_manager); 205 SetDownloadManager(context, base::WrapUnique(download_manager));
200 download_manager->SetDelegate(context->GetDownloadManagerDelegate()); 206 download_manager->SetDelegate(context->GetDownloadManagerDelegate());
201 } 207 }
202 208
203 return static_cast<DownloadManager*>( 209 return static_cast<DownloadManager*>(
204 context->GetUserData(kDownloadManagerKeyName)); 210 context->GetUserData(kDownloadManagerKeyName));
205 } 211 }
206 212
207 // static 213 // static
208 storage::ExternalMountPoints* BrowserContext::GetMountPoints( 214 storage::ExternalMountPoints* BrowserContext::GetMountPoints(
209 BrowserContext* context) { 215 BrowserContext* context) {
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
387 if (indexed_db_context_impl->TaskRunner()) { 393 if (indexed_db_context_impl->TaskRunner()) {
388 indexed_db_context_impl->TaskRunner()->PostTask( 394 indexed_db_context_impl->TaskRunner()->PostTask(
389 FROM_HERE, 395 FROM_HERE,
390 base::Bind(&SaveSessionStateOnIndexedDBThread, 396 base::Bind(&SaveSessionStateOnIndexedDBThread,
391 make_scoped_refptr(indexed_db_context_impl))); 397 make_scoped_refptr(indexed_db_context_impl)));
392 } 398 }
393 } 399 }
394 400
395 void BrowserContext::SetDownloadManagerForTesting( 401 void BrowserContext::SetDownloadManagerForTesting(
396 BrowserContext* browser_context, 402 BrowserContext* browser_context,
397 DownloadManager* download_manager) { 403 std::unique_ptr<content::DownloadManager> download_manager) {
398 SetDownloadManager(browser_context, download_manager); 404 SetDownloadManager(browser_context, std::move(download_manager));
399 } 405 }
400 406
401 // static 407 // static
402 void BrowserContext::Initialize( 408 void BrowserContext::Initialize(
403 BrowserContext* browser_context, 409 BrowserContext* browser_context,
404 const base::FilePath& path) { 410 const base::FilePath& path) {
405 411
406 std::string new_id; 412 std::string new_id;
407 if (GetContentClient() && GetContentClient()->browser()) { 413 if (GetContentClient() && GetContentClient()->browser()) {
408 new_id = GetContentClient()->browser()->GetServiceUserIdForBrowserContext( 414 new_id = GetContentClient()->browser()->GetServiceUserIdForBrowserContext(
409 browser_context); 415 browser_context);
410 } else { 416 } else {
411 // Some test scenarios initialize a BrowserContext without a content client. 417 // Some test scenarios initialize a BrowserContext without a content client.
412 new_id = base::GenerateGUID(); 418 new_id = base::GenerateGUID();
413 } 419 }
414 420
415 ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>( 421 ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>(
416 browser_context->GetUserData(kServiceUserId)); 422 browser_context->GetUserData(kServiceUserId));
417 if (holder) 423 if (holder)
418 file::ForgetServiceUserIdUserDirAssociation(holder->user_id()); 424 file::ForgetServiceUserIdUserDirAssociation(holder->user_id());
419 file::AssociateServiceUserIdWithUserDir(new_id, path); 425 file::AssociateServiceUserIdWithUserDir(new_id, path);
420 RemoveBrowserContextFromUserIdMap(browser_context); 426 RemoveBrowserContextFromUserIdMap(browser_context);
421 g_user_id_to_context.Get()[new_id] = browser_context; 427 g_user_id_to_context.Get()[new_id] = browser_context;
422 browser_context->SetUserData(kServiceUserId, 428 browser_context->SetUserData(kServiceUserId,
423 new ServiceUserIdHolder(new_id)); 429 base::MakeUnique<ServiceUserIdHolder>(new_id));
424 430
425 browser_context->SetUserData(kMojoWasInitialized, 431 browser_context->SetUserData(
426 new base::SupportsUserData::Data); 432 kMojoWasInitialized, base::MakeUnique<base::SupportsUserData::Data>());
427 433
428 ServiceManagerConnection* service_manager_connection = 434 ServiceManagerConnection* service_manager_connection =
429 ServiceManagerConnection::GetForProcess(); 435 ServiceManagerConnection::GetForProcess();
430 if (service_manager_connection && base::ThreadTaskRunnerHandle::IsSet()) { 436 if (service_manager_connection && base::ThreadTaskRunnerHandle::IsSet()) {
431 // NOTE: Many unit tests create a TestBrowserContext without initializing 437 // NOTE: Many unit tests create a TestBrowserContext without initializing
432 // Mojo or the global service manager connection. 438 // Mojo or the global service manager connection.
433 439
434 service_manager::mojom::ServicePtr service; 440 service_manager::mojom::ServicePtr service;
435 service_manager::mojom::ServiceRequest service_request(&service); 441 service_manager::mojom::ServiceRequest service_request(&service);
436 442
437 service_manager::mojom::PIDReceiverPtr pid_receiver; 443 service_manager::mojom::PIDReceiverPtr pid_receiver;
438 service_manager::Identity identity(mojom::kBrowserServiceName, new_id); 444 service_manager::Identity identity(mojom::kBrowserServiceName, new_id);
439 service_manager_connection->GetConnector()->StartService( 445 service_manager_connection->GetConnector()->StartService(
440 identity, std::move(service), mojo::MakeRequest(&pid_receiver)); 446 identity, std::move(service), mojo::MakeRequest(&pid_receiver));
441 pid_receiver->SetPID(base::GetCurrentProcId()); 447 pid_receiver->SetPID(base::GetCurrentProcId());
442 448
443 service_manager_connection->GetConnector()->StartService(identity); 449 service_manager_connection->GetConnector()->StartService(identity);
444 BrowserContextServiceManagerConnectionHolder* connection_holder = 450 BrowserContextServiceManagerConnectionHolder* connection_holder =
445 new BrowserContextServiceManagerConnectionHolder( 451 new BrowserContextServiceManagerConnectionHolder(
446 std::move(service_request)); 452 std::move(service_request));
447 browser_context->SetUserData(kServiceManagerConnection, connection_holder); 453 browser_context->SetUserData(kServiceManagerConnection,
454 base::WrapUnique(connection_holder));
448 455
449 ServiceManagerConnection* connection = 456 ServiceManagerConnection* connection =
450 connection_holder->service_manager_connection(); 457 connection_holder->service_manager_connection();
451 458
452 // New embedded service factories should be added to |connection| here. 459 // New embedded service factories should be added to |connection| here.
453 460
454 if (base::CommandLine::ForCurrentProcess()->HasSwitch( 461 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
455 switches::kMojoLocalStorage)) { 462 switches::kMojoLocalStorage)) {
456 ServiceInfo info; 463 ServiceInfo info;
457 info.factory = 464 info.factory =
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
535 542
536 // static 543 // static
537 std::string BrowserContext::CreateRandomMediaDeviceIDSalt() { 544 std::string BrowserContext::CreateRandomMediaDeviceIDSalt() {
538 std::string salt; 545 std::string salt;
539 base::Base64Encode(base::RandBytesAsString(16), &salt); 546 base::Base64Encode(base::RandBytesAsString(16), &salt);
540 DCHECK(!salt.empty()); 547 DCHECK(!salt.empty());
541 return salt; 548 return salt;
542 } 549 }
543 550
544 } // namespace content 551 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/blob_storage/chrome_blob_storage_context.cc ('k') | content/browser/download/download_request_core.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698