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

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

Issue 2815913005: Switch to using scoped_ptr with UserData (Closed)
Patch Set: fixes Created 3 years, 8 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/command_line.h" 16 #include "base/command_line.h"
16 #include "base/guid.h" 17 #include "base/guid.h"
17 #include "base/lazy_instance.h" 18 #include "base/lazy_instance.h"
18 #include "base/macros.h" 19 #include "base/macros.h"
20 #include "base/memory/ptr_util.h"
19 #include "base/rand_util.h" 21 #include "base/rand_util.h"
20 #include "base/threading/thread_task_runner_handle.h" 22 #include "base/threading/thread_task_runner_handle.h"
21 #include "build/build_config.h" 23 #include "build/build_config.h"
22 #include "content/browser/blob_storage/chrome_blob_storage_context.h" 24 #include "content/browser/blob_storage/chrome_blob_storage_context.h"
23 #include "content/browser/download/download_manager_impl.h" 25 #include "content/browser/download/download_manager_impl.h"
24 #include "content/browser/indexed_db/indexed_db_context_impl.h" 26 #include "content/browser/indexed_db/indexed_db_context_impl.h"
25 #include "content/browser/loader/resource_dispatcher_host_impl.h" 27 #include "content/browser/loader/resource_dispatcher_host_impl.h"
26 #include "content/browser/push_messaging/push_messaging_router.h" 28 #include "content/browser/push_messaging/push_messaging_router.h"
27 #include "content/browser/storage_partition_impl_map.h" 29 #include "content/browser/storage_partition_impl_map.h"
28 #include "content/common/child_process_host_impl.h" 30 #include "content/common/child_process_host_impl.h"
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 g_user_id_to_context.Get().erase(it); 92 g_user_id_to_context.Get().erase(it);
91 } 93 }
92 } 94 }
93 95
94 StoragePartitionImplMap* GetStoragePartitionMap( 96 StoragePartitionImplMap* GetStoragePartitionMap(
95 BrowserContext* browser_context) { 97 BrowserContext* browser_context) {
96 StoragePartitionImplMap* partition_map = 98 StoragePartitionImplMap* partition_map =
97 static_cast<StoragePartitionImplMap*>( 99 static_cast<StoragePartitionImplMap*>(
98 browser_context->GetUserData(kStoragePartitionMapKeyName)); 100 browser_context->GetUserData(kStoragePartitionMapKeyName));
99 if (!partition_map) { 101 if (!partition_map) {
100 partition_map = new StoragePartitionImplMap(browser_context); 102 auto partition_map_owned =
101 browser_context->SetUserData(kStoragePartitionMapKeyName, partition_map); 103 base::MakeUnique<StoragePartitionImplMap>(browser_context);
104 partition_map = partition_map_owned.get();
105 browser_context->SetUserData(kStoragePartitionMapKeyName,
106 std::move(partition_map_owned));
102 } 107 }
103 return partition_map; 108 return partition_map;
104 } 109 }
105 110
106 StoragePartition* GetStoragePartitionFromConfig( 111 StoragePartition* GetStoragePartitionFromConfig(
107 BrowserContext* browser_context, 112 BrowserContext* browser_context,
108 const std::string& partition_domain, 113 const std::string& partition_domain,
109 const std::string& partition_name, 114 const std::string& partition_name,
110 bool in_memory) { 115 bool in_memory) {
111 StoragePartitionImplMap* partition_map = 116 StoragePartitionImplMap* partition_map =
(...skipping 20 matching lines...) Expand all
132 indexed_db_context->SetForceKeepSessionState(); 137 indexed_db_context->SetForceKeepSessionState();
133 } 138 }
134 139
135 void ShutdownServiceWorkerContext(StoragePartition* partition) { 140 void ShutdownServiceWorkerContext(StoragePartition* partition) {
136 ServiceWorkerContextWrapper* wrapper = 141 ServiceWorkerContextWrapper* wrapper =
137 static_cast<ServiceWorkerContextWrapper*>( 142 static_cast<ServiceWorkerContextWrapper*>(
138 partition->GetServiceWorkerContext()); 143 partition->GetServiceWorkerContext());
139 wrapper->process_manager()->Shutdown(); 144 wrapper->process_manager()->Shutdown();
140 } 145 }
141 146
142 void SetDownloadManager(BrowserContext* context, 147 void SetDownloadManager(
143 content::DownloadManager* download_manager) { 148 BrowserContext* context,
149 std::unique_ptr<content::DownloadManager> download_manager) {
144 DCHECK_CURRENTLY_ON(BrowserThread::UI); 150 DCHECK_CURRENTLY_ON(BrowserThread::UI);
145 DCHECK(download_manager); 151 DCHECK(download_manager);
146 context->SetUserData(kDownloadManagerKeyName, download_manager); 152 context->SetUserData(kDownloadManagerKeyName, std::move(download_manager));
147 } 153 }
148 154
149 class BrowserContextServiceManagerConnectionHolder 155 class BrowserContextServiceManagerConnectionHolder
150 : public base::SupportsUserData::Data { 156 : public base::SupportsUserData::Data {
151 public: 157 public:
152 explicit BrowserContextServiceManagerConnectionHolder( 158 explicit BrowserContextServiceManagerConnectionHolder(
153 service_manager::mojom::ServiceRequest request) 159 service_manager::mojom::ServiceRequest request)
154 : service_manager_connection_(ServiceManagerConnection::Create( 160 : service_manager_connection_(ServiceManagerConnection::Create(
155 std::move(request), 161 std::move(request),
156 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO))) {} 162 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO))) {}
(...skipping 30 matching lines...) Expand all
187 } 193 }
188 194
189 DownloadManager* BrowserContext::GetDownloadManager( 195 DownloadManager* BrowserContext::GetDownloadManager(
190 BrowserContext* context) { 196 BrowserContext* context) {
191 DCHECK_CURRENTLY_ON(BrowserThread::UI); 197 DCHECK_CURRENTLY_ON(BrowserThread::UI);
192 if (!context->GetUserData(kDownloadManagerKeyName)) { 198 if (!context->GetUserData(kDownloadManagerKeyName)) {
193 DownloadManager* download_manager = 199 DownloadManager* download_manager =
194 new DownloadManagerImpl( 200 new DownloadManagerImpl(
195 GetContentClient()->browser()->GetNetLog(), context); 201 GetContentClient()->browser()->GetNetLog(), context);
196 202
197 SetDownloadManager(context, download_manager); 203 SetDownloadManager(context, base::WrapUnique(download_manager));
198 download_manager->SetDelegate(context->GetDownloadManagerDelegate()); 204 download_manager->SetDelegate(context->GetDownloadManagerDelegate());
199 } 205 }
200 206
201 return static_cast<DownloadManager*>( 207 return static_cast<DownloadManager*>(
202 context->GetUserData(kDownloadManagerKeyName)); 208 context->GetUserData(kDownloadManagerKeyName));
203 } 209 }
204 210
205 // static 211 // static
206 storage::ExternalMountPoints* BrowserContext::GetMountPoints( 212 storage::ExternalMountPoints* BrowserContext::GetMountPoints(
207 BrowserContext* context) { 213 BrowserContext* context) {
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
385 if (indexed_db_context_impl->TaskRunner()) { 391 if (indexed_db_context_impl->TaskRunner()) {
386 indexed_db_context_impl->TaskRunner()->PostTask( 392 indexed_db_context_impl->TaskRunner()->PostTask(
387 FROM_HERE, 393 FROM_HERE,
388 base::Bind(&SaveSessionStateOnIndexedDBThread, 394 base::Bind(&SaveSessionStateOnIndexedDBThread,
389 make_scoped_refptr(indexed_db_context_impl))); 395 make_scoped_refptr(indexed_db_context_impl)));
390 } 396 }
391 } 397 }
392 398
393 void BrowserContext::SetDownloadManagerForTesting( 399 void BrowserContext::SetDownloadManagerForTesting(
394 BrowserContext* browser_context, 400 BrowserContext* browser_context,
395 DownloadManager* download_manager) { 401 std::unique_ptr<content::DownloadManager> download_manager) {
396 SetDownloadManager(browser_context, download_manager); 402 SetDownloadManager(browser_context, std::move(download_manager));
397 } 403 }
398 404
399 // static 405 // static
400 void BrowserContext::Initialize( 406 void BrowserContext::Initialize(
401 BrowserContext* browser_context, 407 BrowserContext* browser_context,
402 const base::FilePath& path) { 408 const base::FilePath& path) {
403 409
404 std::string new_id; 410 std::string new_id;
405 if (GetContentClient() && GetContentClient()->browser()) { 411 if (GetContentClient() && GetContentClient()->browser()) {
406 new_id = GetContentClient()->browser()->GetServiceUserIdForBrowserContext( 412 new_id = GetContentClient()->browser()->GetServiceUserIdForBrowserContext(
407 browser_context); 413 browser_context);
408 } else { 414 } else {
409 // Some test scenarios initialize a BrowserContext without a content client. 415 // Some test scenarios initialize a BrowserContext without a content client.
410 new_id = base::GenerateGUID(); 416 new_id = base::GenerateGUID();
411 } 417 }
412 418
413 ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>( 419 ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>(
414 browser_context->GetUserData(kServiceUserId)); 420 browser_context->GetUserData(kServiceUserId));
415 if (holder) 421 if (holder)
416 file::ForgetServiceUserIdUserDirAssociation(holder->user_id()); 422 file::ForgetServiceUserIdUserDirAssociation(holder->user_id());
417 file::AssociateServiceUserIdWithUserDir(new_id, path); 423 file::AssociateServiceUserIdWithUserDir(new_id, path);
418 RemoveBrowserContextFromUserIdMap(browser_context); 424 RemoveBrowserContextFromUserIdMap(browser_context);
419 g_user_id_to_context.Get()[new_id] = browser_context; 425 g_user_id_to_context.Get()[new_id] = browser_context;
420 browser_context->SetUserData(kServiceUserId, 426 browser_context->SetUserData(kServiceUserId,
421 new ServiceUserIdHolder(new_id)); 427 base::MakeUnique<ServiceUserIdHolder>(new_id));
422 428
423 browser_context->SetUserData(kMojoWasInitialized, 429 browser_context->SetUserData(
424 new base::SupportsUserData::Data); 430 kMojoWasInitialized, base::MakeUnique<base::SupportsUserData::Data>());
425 431
426 ServiceManagerConnection* service_manager_connection = 432 ServiceManagerConnection* service_manager_connection =
427 ServiceManagerConnection::GetForProcess(); 433 ServiceManagerConnection::GetForProcess();
428 if (service_manager_connection && base::ThreadTaskRunnerHandle::IsSet()) { 434 if (service_manager_connection && base::ThreadTaskRunnerHandle::IsSet()) {
429 // NOTE: Many unit tests create a TestBrowserContext without initializing 435 // NOTE: Many unit tests create a TestBrowserContext without initializing
430 // Mojo or the global service manager connection. 436 // Mojo or the global service manager connection.
431 437
432 service_manager::mojom::ServicePtr service; 438 service_manager::mojom::ServicePtr service;
433 service_manager::mojom::ServiceRequest service_request(&service); 439 service_manager::mojom::ServiceRequest service_request(&service);
434 440
435 service_manager::mojom::PIDReceiverPtr pid_receiver; 441 service_manager::mojom::PIDReceiverPtr pid_receiver;
436 service_manager::Identity identity(mojom::kBrowserServiceName, new_id); 442 service_manager::Identity identity(mojom::kBrowserServiceName, new_id);
437 service_manager_connection->GetConnector()->StartService( 443 service_manager_connection->GetConnector()->StartService(
438 identity, std::move(service), mojo::MakeRequest(&pid_receiver)); 444 identity, std::move(service), mojo::MakeRequest(&pid_receiver));
439 pid_receiver->SetPID(base::GetCurrentProcId()); 445 pid_receiver->SetPID(base::GetCurrentProcId());
440 446
441 service_manager_connection->GetConnector()->StartService(identity); 447 service_manager_connection->GetConnector()->StartService(identity);
442 BrowserContextServiceManagerConnectionHolder* connection_holder = 448 BrowserContextServiceManagerConnectionHolder* connection_holder =
443 new BrowserContextServiceManagerConnectionHolder( 449 new BrowserContextServiceManagerConnectionHolder(
444 std::move(service_request)); 450 std::move(service_request));
445 browser_context->SetUserData(kServiceManagerConnection, connection_holder); 451 browser_context->SetUserData(kServiceManagerConnection,
452 base::WrapUnique(connection_holder));
446 453
447 ServiceManagerConnection* connection = 454 ServiceManagerConnection* connection =
448 connection_holder->service_manager_connection(); 455 connection_holder->service_manager_connection();
449 456
450 // New embedded service factories should be added to |connection| here. 457 // New embedded service factories should be added to |connection| here.
451 458
452 if (base::CommandLine::ForCurrentProcess()->HasSwitch( 459 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
453 switches::kMojoLocalStorage)) { 460 switches::kMojoLocalStorage)) {
454 ServiceInfo info; 461 ServiceInfo info;
455 info.factory = 462 info.factory =
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
518 if (GetUserData(kDownloadManagerKeyName)) 525 if (GetUserData(kDownloadManagerKeyName))
519 GetDownloadManager(this)->Shutdown(); 526 GetDownloadManager(this)->Shutdown();
520 } 527 }
521 528
522 void BrowserContext::ShutdownStoragePartitions() { 529 void BrowserContext::ShutdownStoragePartitions() {
523 if (GetUserData(kStoragePartitionMapKeyName)) 530 if (GetUserData(kStoragePartitionMapKeyName))
524 RemoveUserData(kStoragePartitionMapKeyName); 531 RemoveUserData(kStoragePartitionMapKeyName);
525 } 532 }
526 533
527 } // namespace content 534 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698