[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "content/public/browser/browser_context.h" |
| 6 | |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
avi | 1ed4a437 | 2017-04-25 05:39:41 | [diff] [blame] | 9 | |
erg | e69130f5 | 2016-03-02 00:13:28 | [diff] [blame] | 10 | #include <algorithm> |
| 11 | #include <limits> |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 12 | #include <memory> |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 13 | #include <utility> |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 14 | #include <vector> |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 15 | |
mmenke | c0b2b8b1 | 2017-04-21 16:27:52 | [diff] [blame] | 16 | #include "base/base64.h" |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 17 | #include "base/command_line.h" |
ben | cccfe2a | 2016-03-05 16:54:14 | [diff] [blame] | 18 | #include "base/guid.h" |
erg | e69130f5 | 2016-03-02 00:13:28 | [diff] [blame] | 19 | #include "base/lazy_instance.h" |
mmenke | c0b2b8b1 | 2017-04-21 16:27:52 | [diff] [blame] | 20 | #include "base/logging.h" |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 21 | #include "base/macros.h" |
avi | 1ed4a437 | 2017-04-25 05:39:41 | [diff] [blame] | 22 | #include "base/memory/ptr_util.h" |
erg | e69130f5 | 2016-03-02 00:13:28 | [diff] [blame] | 23 | #include "base/rand_util.h" |
mek | 17399e67 | 2017-05-09 03:51:20 | [diff] [blame] | 24 | #include "base/task_scheduler/post_task.h" |
ben | 21a34c25 | 2016-06-29 22:24:37 | [diff] [blame] | 25 | #include "base/threading/thread_task_runner_handle.h" |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 26 | #include "build/build_config.h" |
dmurph | 7ac019a | 2016-05-13 00:13:17 | [diff] [blame] | 27 | #include "content/browser/blob_storage/chrome_blob_storage_context.h" |
msramek | e169ccb | 2017-04-26 05:21:41 | [diff] [blame] | 28 | #include "content/browser/browsing_data/browsing_data_remover_impl.h" |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 29 | #include "content/browser/download/download_manager_impl.h" |
[email protected] | c4d28166 | 2013-03-31 00:35:08 | [diff] [blame] | 30 | #include "content/browser/indexed_db/indexed_db_context_impl.h" |
[email protected] | 678c036 | 2012-12-05 08:02:44 | [diff] [blame] | 31 | #include "content/browser/loader/resource_dispatcher_host_impl.h" |
mvanouwerkerk | 17205ea | 2014-11-07 17:30:15 | [diff] [blame] | 32 | #include "content/browser/push_messaging/push_messaging_router.h" |
Ken Rockot | c7a279c | 2017-05-04 23:51:45 | [diff] [blame] | 33 | #include "content/browser/service_manager/common_browser_interfaces.h" |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 34 | #include "content/browser/storage_partition_impl_map.h" |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 35 | #include "content/common/child_process_host_impl.h" |
[email protected] | 393b6cb | 2014-05-15 00:55:12 | [diff] [blame] | 36 | #include "content/public/browser/blob_handle.h" |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 37 | #include "content/public/browser/browser_thread.h" |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 38 | #include "content/public/browser/content_browser_client.h" |
falken | 04a6912a | 2016-09-23 21:06:29 | [diff] [blame] | 39 | #include "content/public/browser/render_process_host.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 40 | #include "content/public/browser/site_instance.h" |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 41 | #include "content/public/common/content_switches.h" |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 42 | #include "content/public/common/service_manager_connection.h" |
ben | 51bb6c6 | 2016-11-17 20:15:57 | [diff] [blame] | 43 | #include "content/public/common/service_names.mojom.h" |
[email protected] | 4d7c4ef | 2012-03-16 01:47:12 | [diff] [blame] | 44 | #include "net/cookies/cookie_store.h" |
[email protected] | 6b8a3c74 | 2014-07-25 00:25:35 | [diff] [blame] | 45 | #include "net/ssl/channel_id_service.h" |
| 46 | #include "net/ssl/channel_id_store.h" |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 47 | #include "net/url_request/url_request_context.h" |
[email protected] | 6939075a | 2012-08-28 08:35:53 | [diff] [blame] | 48 | #include "net/url_request/url_request_context_getter.h" |
ben | 768c8dc | 2016-08-12 00:26:50 | [diff] [blame] | 49 | #include "services/file/file_service.h" |
ben | aabad0b | 2016-11-16 23:54:17 | [diff] [blame] | 50 | #include "services/file/public/interfaces/constants.mojom.h" |
ben | 768c8dc | 2016-08-12 00:26:50 | [diff] [blame] | 51 | #include "services/file/user_id_map.h" |
rockot | 734fb66 | 2016-10-15 16:41:30 | [diff] [blame] | 52 | #include "services/service_manager/public/cpp/connector.h" |
| 53 | #include "services/service_manager/public/interfaces/service.mojom.h" |
pilgrim | e92c5fcd | 2014-09-10 23:31:23 | [diff] [blame] | 54 | #include "storage/browser/database/database_tracker.h" |
| 55 | #include "storage/browser/fileapi/external_mount_points.h" |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 56 | |
[email protected] | 314c3e2 | 2012-02-21 03:57:42 | [diff] [blame] | 57 | using base::UserDataAdapter; |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 58 | |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 59 | namespace content { |
| 60 | |
[email protected] | 735e20c | 2012-03-20 01:16:59 | [diff] [blame] | 61 | namespace { |
| 62 | |
scottmg | 5e65e3a | 2017-03-08 08:48:46 | [diff] [blame] | 63 | base::LazyInstance<std::map<std::string, BrowserContext*>>::DestructorAtExit |
ben | 6c85c449 | 2016-06-16 20:40:51 | [diff] [blame] | 64 | g_user_id_to_context = LAZY_INSTANCE_INITIALIZER; |
| 65 | |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 66 | class ServiceUserIdHolder : public base::SupportsUserData::Data { |
ben | 6c85c449 | 2016-06-16 20:40:51 | [diff] [blame] | 67 | public: |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 68 | explicit ServiceUserIdHolder(const std::string& user_id) |
| 69 | : user_id_(user_id) {} |
| 70 | ~ServiceUserIdHolder() override {} |
ben | 6c85c449 | 2016-06-16 20:40:51 | [diff] [blame] | 71 | |
| 72 | const std::string& user_id() const { return user_id_; } |
| 73 | |
| 74 | private: |
| 75 | std::string user_id_; |
| 76 | |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 77 | DISALLOW_COPY_AND_ASSIGN(ServiceUserIdHolder); |
ben | 6c85c449 | 2016-06-16 20:40:51 | [diff] [blame] | 78 | }; |
erg | e69130f5 | 2016-03-02 00:13:28 | [diff] [blame] | 79 | |
[email protected] | e0ce8a1e | 2012-09-18 10:26:36 | [diff] [blame] | 80 | // Key names on BrowserContext. |
msramek | e169ccb | 2017-04-26 05:21:41 | [diff] [blame] | 81 | const char kBrowsingDataRemoverKey[] = "browsing-data-remover"; |
[email protected] | 6ef0c391 | 2013-01-25 22:46:34 | [diff] [blame] | 82 | const char kDownloadManagerKeyName[] = "download_manager"; |
erg | e69130f5 | 2016-03-02 00:13:28 | [diff] [blame] | 83 | const char kMojoWasInitialized[] = "mojo-was-initialized"; |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 84 | const char kServiceManagerConnection[] = "service-manager-connection"; |
| 85 | const char kServiceUserId[] = "service-user-id"; |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 86 | const char kStoragePartitionMapKeyName[] = "content_storage_partition_map"; |
erg | e69130f5 | 2016-03-02 00:13:28 | [diff] [blame] | 87 | |
[email protected] | 9afc14e2 | 2013-09-25 22:34:14 | [diff] [blame] | 88 | #if defined(OS_CHROMEOS) |
| 89 | const char kMountPointsKey[] = "mount_points"; |
| 90 | #endif // defined(OS_CHROMEOS) |
| 91 | |
ben | 6c85c449 | 2016-06-16 20:40:51 | [diff] [blame] | 92 | void RemoveBrowserContextFromUserIdMap(BrowserContext* browser_context) { |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 93 | ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>( |
| 94 | browser_context->GetUserData(kServiceUserId)); |
ben | 6c85c449 | 2016-06-16 20:40:51 | [diff] [blame] | 95 | if (holder) { |
| 96 | auto it = g_user_id_to_context.Get().find(holder->user_id()); |
| 97 | if (it != g_user_id_to_context.Get().end()) |
| 98 | g_user_id_to_context.Get().erase(it); |
| 99 | } |
| 100 | } |
| 101 | |
[email protected] | 14acc64 | 2012-11-17 12:20:10 | [diff] [blame] | 102 | StoragePartitionImplMap* GetStoragePartitionMap( |
| 103 | BrowserContext* browser_context) { |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 104 | StoragePartitionImplMap* partition_map = |
| 105 | static_cast<StoragePartitionImplMap*>( |
a.cavalcanti | ffab7376 | 2015-08-15 02:55:48 | [diff] [blame] | 106 | browser_context->GetUserData(kStoragePartitionMapKeyName)); |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 107 | if (!partition_map) { |
avi | 1ed4a437 | 2017-04-25 05:39:41 | [diff] [blame] | 108 | auto partition_map_owned = |
| 109 | base::MakeUnique<StoragePartitionImplMap>(browser_context); |
| 110 | partition_map = partition_map_owned.get(); |
| 111 | browser_context->SetUserData(kStoragePartitionMapKeyName, |
| 112 | std::move(partition_map_owned)); |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 113 | } |
[email protected] | 14acc64 | 2012-11-17 12:20:10 | [diff] [blame] | 114 | return partition_map; |
| 115 | } |
| 116 | |
| 117 | StoragePartition* GetStoragePartitionFromConfig( |
| 118 | BrowserContext* browser_context, |
| 119 | const std::string& partition_domain, |
| 120 | const std::string& partition_name, |
| 121 | bool in_memory) { |
| 122 | StoragePartitionImplMap* partition_map = |
| 123 | GetStoragePartitionMap(browser_context); |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 124 | |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 125 | if (browser_context->IsOffTheRecord()) |
| 126 | in_memory = true; |
| 127 | |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 128 | return partition_map->Get(partition_domain, partition_name, in_memory); |
[email protected] | d1198fd | 2012-08-13 22:50:19 | [diff] [blame] | 129 | } |
| 130 | |
[email protected] | 6939075a | 2012-08-28 08:35:53 | [diff] [blame] | 131 | void SaveSessionStateOnIOThread( |
| 132 | const scoped_refptr<net::URLRequestContextGetter>& context_getter, |
[email protected] | 98d6d456 | 2014-06-25 20:57:55 | [diff] [blame] | 133 | AppCacheServiceImpl* appcache_service) { |
[email protected] | 6939075a | 2012-08-28 08:35:53 | [diff] [blame] | 134 | net::URLRequestContext* context = context_getter->GetURLRequestContext(); |
mmenke | ded79da | 2016-02-06 08:28:51 | [diff] [blame] | 135 | context->cookie_store()->SetForceKeepSessionState(); |
[email protected] | 6b8a3c74 | 2014-07-25 00:25:35 | [diff] [blame] | 136 | context->channel_id_service()->GetChannelIDStore()-> |
[email protected] | 6939075a | 2012-08-28 08:35:53 | [diff] [blame] | 137 | SetForceKeepSessionState(); |
| 138 | appcache_service->set_force_keep_session_state(); |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 139 | } |
| 140 | |
[email protected] | 89acda8 | 2013-06-25 20:52:50 | [diff] [blame] | 141 | void SaveSessionStateOnIndexedDBThread( |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 142 | scoped_refptr<IndexedDBContextImpl> indexed_db_context) { |
[email protected] | bf510ed | 2012-06-05 08:31:43 | [diff] [blame] | 143 | indexed_db_context->SetForceKeepSessionState(); |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 144 | } |
| 145 | |
falken | 41f417516 | 2014-10-29 07:03:41 | [diff] [blame] | 146 | void ShutdownServiceWorkerContext(StoragePartition* partition) { |
| 147 | ServiceWorkerContextWrapper* wrapper = |
| 148 | static_cast<ServiceWorkerContextWrapper*>( |
| 149 | partition->GetServiceWorkerContext()); |
| 150 | wrapper->process_manager()->Shutdown(); |
| 151 | } |
| 152 | |
avi | 1ed4a437 | 2017-04-25 05:39:41 | [diff] [blame] | 153 | void SetDownloadManager( |
| 154 | BrowserContext* context, |
| 155 | std::unique_ptr<content::DownloadManager> download_manager) { |
ttr314 | 81dc54b | 2015-08-06 20:11:26 | [diff] [blame] | 156 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 157 | DCHECK(download_manager); |
avi | 1ed4a437 | 2017-04-25 05:39:41 | [diff] [blame] | 158 | context->SetUserData(kDownloadManagerKeyName, std::move(download_manager)); |
ttr314 | 81dc54b | 2015-08-06 20:11:26 | [diff] [blame] | 159 | } |
| 160 | |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 161 | class BrowserContextServiceManagerConnectionHolder |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 162 | : public base::SupportsUserData::Data { |
| 163 | public: |
ben | aad37ecd | 2017-04-12 22:08:20 | [diff] [blame] | 164 | explicit BrowserContextServiceManagerConnectionHolder( |
rockot | 400ea35b | 2016-10-15 19:15:32 | [diff] [blame] | 165 | service_manager::mojom::ServiceRequest request) |
ben | aad37ecd | 2017-04-12 22:08:20 | [diff] [blame] | 166 | : service_manager_connection_(ServiceManagerConnection::Create( |
rockot | cef3827 | 2016-07-15 22:47:47 | [diff] [blame] | 167 | std::move(request), |
| 168 | BrowserThread::GetTaskRunnerForThread(BrowserThread::IO))) {} |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 169 | ~BrowserContextServiceManagerConnectionHolder() override {} |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 170 | |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 171 | ServiceManagerConnection* service_manager_connection() { |
| 172 | return service_manager_connection_.get(); |
| 173 | } |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 174 | |
| 175 | private: |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 176 | std::unique_ptr<ServiceManagerConnection> service_manager_connection_; |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 177 | |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 178 | DISALLOW_COPY_AND_ASSIGN(BrowserContextServiceManagerConnectionHolder); |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 179 | }; |
| 180 | |
[email protected] | 735e20c | 2012-03-20 01:16:59 | [diff] [blame] | 181 | } // namespace |
| 182 | |
[email protected] | 14acc64 | 2012-11-17 12:20:10 | [diff] [blame] | 183 | // static |
| 184 | void BrowserContext::AsyncObliterateStoragePartition( |
| 185 | BrowserContext* browser_context, |
[email protected] | 399583b | 2012-12-11 09:33:42 | [diff] [blame] | 186 | const GURL& site, |
| 187 | const base::Closure& on_gc_required) { |
| 188 | GetStoragePartitionMap(browser_context)->AsyncObliterate(site, |
| 189 | on_gc_required); |
| 190 | } |
| 191 | |
| 192 | // static |
| 193 | void BrowserContext::GarbageCollectStoragePartitions( |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 194 | BrowserContext* browser_context, |
| 195 | std::unique_ptr<base::hash_set<base::FilePath>> active_paths, |
| 196 | const base::Closure& done) { |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 197 | GetStoragePartitionMap(browser_context) |
| 198 | ->GarbageCollect(std::move(active_paths), done); |
[email protected] | 14acc64 | 2012-11-17 12:20:10 | [diff] [blame] | 199 | } |
| 200 | |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 201 | DownloadManager* BrowserContext::GetDownloadManager( |
| 202 | BrowserContext* context) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 203 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 204 | if (!context->GetUserData(kDownloadManagerKeyName)) { |
[email protected] | eba4a4d | 2013-05-29 02:18:06 | [diff] [blame] | 205 | DownloadManager* download_manager = |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 206 | new DownloadManagerImpl( |
[email protected] | 1679869 | 2013-04-23 18:08:38 | [diff] [blame] | 207 | GetContentClient()->browser()->GetNetLog(), context); |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 208 | |
avi | 1ed4a437 | 2017-04-25 05:39:41 | [diff] [blame] | 209 | SetDownloadManager(context, base::WrapUnique(download_manager)); |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 210 | download_manager->SetDelegate(context->GetDownloadManagerDelegate()); |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 211 | } |
| 212 | |
[email protected] | eba4a4d | 2013-05-29 02:18:06 | [diff] [blame] | 213 | return static_cast<DownloadManager*>( |
| 214 | context->GetUserData(kDownloadManagerKeyName)); |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 215 | } |
| 216 | |
[email protected] | 6ef0c391 | 2013-01-25 22:46:34 | [diff] [blame] | 217 | // static |
[email protected] | cd501a7 | 2014-08-22 19:58:31 | [diff] [blame] | 218 | storage::ExternalMountPoints* BrowserContext::GetMountPoints( |
[email protected] | 6ef0c391 | 2013-01-25 22:46:34 | [diff] [blame] | 219 | BrowserContext* context) { |
| 220 | // Ensure that these methods are called on the UI thread, except for |
| 221 | // unittests where a UI thread might not have been created. |
| 222 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
| 223 | !BrowserThread::IsMessageLoopValid(BrowserThread::UI)); |
| 224 | |
| 225 | #if defined(OS_CHROMEOS) |
| 226 | if (!context->GetUserData(kMountPointsKey)) { |
[email protected] | cd501a7 | 2014-08-22 19:58:31 | [diff] [blame] | 227 | scoped_refptr<storage::ExternalMountPoints> mount_points = |
| 228 | storage::ExternalMountPoints::CreateRefCounted(); |
[email protected] | 6ef0c391 | 2013-01-25 22:46:34 | [diff] [blame] | 229 | context->SetUserData( |
| 230 | kMountPointsKey, |
avi | cb129c0 | 2017-05-03 06:49:29 | [diff] [blame] | 231 | base::MakeUnique<UserDataAdapter<storage::ExternalMountPoints>>( |
| 232 | mount_points.get())); |
[email protected] | 6ef0c391 | 2013-01-25 22:46:34 | [diff] [blame] | 233 | } |
| 234 | |
[email protected] | cd501a7 | 2014-08-22 19:58:31 | [diff] [blame] | 235 | return UserDataAdapter<storage::ExternalMountPoints>::Get(context, |
| 236 | kMountPointsKey); |
[email protected] | 6ef0c391 | 2013-01-25 22:46:34 | [diff] [blame] | 237 | #else |
| 238 | return NULL; |
| 239 | #endif |
| 240 | } |
| 241 | |
msramek | e169ccb | 2017-04-26 05:21:41 | [diff] [blame] | 242 | // static |
| 243 | content::BrowsingDataRemover* content::BrowserContext::GetBrowsingDataRemover( |
| 244 | BrowserContext* context) { |
| 245 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 246 | |
| 247 | if (!context->GetUserData(kBrowsingDataRemoverKey)) { |
| 248 | std::unique_ptr<BrowsingDataRemoverImpl> remover = |
| 249 | base::MakeUnique<BrowsingDataRemoverImpl>(context); |
| 250 | remover->SetEmbedderDelegate(context->GetBrowsingDataRemoverDelegate()); |
| 251 | context->SetUserData(kBrowsingDataRemoverKey, std::move(remover)); |
| 252 | } |
| 253 | |
| 254 | return static_cast<BrowsingDataRemoverImpl*>( |
| 255 | context->GetUserData(kBrowsingDataRemoverKey)); |
| 256 | } |
| 257 | |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 258 | StoragePartition* BrowserContext::GetStoragePartition( |
| 259 | BrowserContext* browser_context, |
| 260 | SiteInstance* site_instance) { |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 261 | std::string partition_domain; |
| 262 | std::string partition_name; |
| 263 | bool in_memory = false; |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 264 | |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 265 | if (site_instance) { |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 266 | GetContentClient()->browser()->GetStoragePartitionConfigForSite( |
[email protected] | 14acc64 | 2012-11-17 12:20:10 | [diff] [blame] | 267 | browser_context, site_instance->GetSiteURL(), true, |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 268 | &partition_domain, &partition_name, &in_memory); |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 269 | } |
| 270 | |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 271 | return GetStoragePartitionFromConfig( |
| 272 | browser_context, partition_domain, partition_name, in_memory); |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 273 | } |
| 274 | |
[email protected] | e94bbcb | 2012-09-07 05:33:57 | [diff] [blame] | 275 | StoragePartition* BrowserContext::GetStoragePartitionForSite( |
| 276 | BrowserContext* browser_context, |
| 277 | const GURL& site) { |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 278 | std::string partition_domain; |
| 279 | std::string partition_name; |
| 280 | bool in_memory; |
[email protected] | e94bbcb | 2012-09-07 05:33:57 | [diff] [blame] | 281 | |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 282 | GetContentClient()->browser()->GetStoragePartitionConfigForSite( |
[email protected] | 14acc64 | 2012-11-17 12:20:10 | [diff] [blame] | 283 | browser_context, site, true, &partition_domain, &partition_name, |
| 284 | &in_memory); |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 285 | |
| 286 | return GetStoragePartitionFromConfig( |
| 287 | browser_context, partition_domain, partition_name, in_memory); |
[email protected] | e94bbcb | 2012-09-07 05:33:57 | [diff] [blame] | 288 | } |
| 289 | |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 290 | void BrowserContext::ForEachStoragePartition( |
| 291 | BrowserContext* browser_context, |
| 292 | const StoragePartitionCallback& callback) { |
| 293 | StoragePartitionImplMap* partition_map = |
| 294 | static_cast<StoragePartitionImplMap*>( |
a.cavalcanti | ffab7376 | 2015-08-15 02:55:48 | [diff] [blame] | 295 | browser_context->GetUserData(kStoragePartitionMapKeyName)); |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 296 | if (!partition_map) |
| 297 | return; |
| 298 | |
| 299 | partition_map->ForEach(callback); |
| 300 | } |
| 301 | |
| 302 | StoragePartition* BrowserContext::GetDefaultStoragePartition( |
| 303 | BrowserContext* browser_context) { |
| 304 | return GetStoragePartition(browser_context, NULL); |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 305 | } |
| 306 | |
tbarzic | db71268 | 2015-03-06 06:05:41 | [diff] [blame] | 307 | // static |
[email protected] | 393b6cb | 2014-05-15 00:55:12 | [diff] [blame] | 308 | void BrowserContext::CreateMemoryBackedBlob(BrowserContext* browser_context, |
| 309 | const char* data, size_t length, |
| 310 | const BlobCallback& callback) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 311 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 393b6cb | 2014-05-15 00:55:12 | [diff] [blame] | 312 | |
| 313 | ChromeBlobStorageContext* blob_context = |
| 314 | ChromeBlobStorageContext::GetFor(browser_context); |
| 315 | BrowserThread::PostTaskAndReplyWithResult( |
| 316 | BrowserThread::IO, FROM_HERE, |
| 317 | base::Bind(&ChromeBlobStorageContext::CreateMemoryBackedBlob, |
| 318 | make_scoped_refptr(blob_context), data, length), |
| 319 | callback); |
| 320 | } |
| 321 | |
[email protected] | 66e53d028 | 2014-08-07 10:04:35 | [diff] [blame] | 322 | // static |
tbarzic | db71268 | 2015-03-06 06:05:41 | [diff] [blame] | 323 | void BrowserContext::CreateFileBackedBlob( |
| 324 | BrowserContext* browser_context, |
| 325 | const base::FilePath& path, |
| 326 | int64_t offset, |
| 327 | int64_t size, |
| 328 | const base::Time& expected_modification_time, |
| 329 | const BlobCallback& callback) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 330 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
tbarzic | db71268 | 2015-03-06 06:05:41 | [diff] [blame] | 331 | |
| 332 | ChromeBlobStorageContext* blob_context = |
| 333 | ChromeBlobStorageContext::GetFor(browser_context); |
| 334 | BrowserThread::PostTaskAndReplyWithResult( |
| 335 | BrowserThread::IO, FROM_HERE, |
| 336 | base::Bind(&ChromeBlobStorageContext::CreateFileBackedBlob, |
| 337 | make_scoped_refptr(blob_context), path, offset, size, |
| 338 | expected_modification_time), |
| 339 | callback); |
| 340 | } |
| 341 | |
| 342 | // static |
[email protected] | 66e53d028 | 2014-08-07 10:04:35 | [diff] [blame] | 343 | void BrowserContext::DeliverPushMessage( |
| 344 | BrowserContext* browser_context, |
| 345 | const GURL& origin, |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 346 | int64_t service_worker_registration_id, |
harkness | dd4d2b2 | 2016-01-27 19:26:43 | [diff] [blame] | 347 | const PushEventPayload& payload, |
johnme | a80c255 | 2014-10-17 14:51:40 | [diff] [blame] | 348 | const base::Callback<void(PushDeliveryStatus)>& callback) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 349 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
harkness | dd4d2b2 | 2016-01-27 19:26:43 | [diff] [blame] | 350 | PushMessagingRouter::DeliverMessage(browser_context, origin, |
| 351 | service_worker_registration_id, payload, |
| 352 | callback); |
[email protected] | 66e53d028 | 2014-08-07 10:04:35 | [diff] [blame] | 353 | } |
| 354 | |
falken | 41f417516 | 2014-10-29 07:03:41 | [diff] [blame] | 355 | // static |
| 356 | void BrowserContext::NotifyWillBeDestroyed(BrowserContext* browser_context) { |
| 357 | // Service Workers must shutdown before the browser context is destroyed, |
| 358 | // since they keep render process hosts alive and the codebase assumes that |
| 359 | // render process hosts die before their profile (browser context) dies. |
| 360 | ForEachStoragePartition(browser_context, |
| 361 | base::Bind(ShutdownServiceWorkerContext)); |
falken | 04a6912a | 2016-09-23 21:06:29 | [diff] [blame] | 362 | |
| 363 | // Shared workers also keep render process hosts alive, and are expected to |
| 364 | // return ref counts to 0 after documents close. However, shared worker |
| 365 | // bookkeeping is done on the IO thread and we want to ensure the hosts are |
| 366 | // destructed now, so forcibly release their ref counts here. |
| 367 | for (RenderProcessHost::iterator host_iterator = |
| 368 | RenderProcessHost::AllHostsIterator(); |
| 369 | !host_iterator.IsAtEnd(); host_iterator.Advance()) { |
| 370 | RenderProcessHost* host = host_iterator.GetCurrentValue(); |
| 371 | if (host->GetBrowserContext() == browser_context) |
| 372 | host->ForceReleaseWorkerRefCounts(); |
| 373 | } |
falken | 41f417516 | 2014-10-29 07:03:41 | [diff] [blame] | 374 | } |
| 375 | |
[email protected] | 314c3e2 | 2012-02-21 03:57:42 | [diff] [blame] | 376 | void BrowserContext::EnsureResourceContextInitialized(BrowserContext* context) { |
[email protected] | 7e26ac9 | 2012-02-27 20:15:05 | [diff] [blame] | 377 | // This will be enough to tickle initialization of BrowserContext if |
| 378 | // necessary, which initializes ResourceContext. The reason we don't call |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 379 | // ResourceContext::InitializeResourceContext() directly here is that |
| 380 | // ResourceContext initialization may call back into BrowserContext |
| 381 | // and when that call returns it'll end rewriting its UserData map. It will |
| 382 | // end up rewriting the same value but this still causes a race condition. |
| 383 | // |
| 384 | // See http://crbug.com/115678. |
| 385 | GetDefaultStoragePartition(context); |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 386 | } |
| 387 | |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 388 | void BrowserContext::SaveSessionState(BrowserContext* browser_context) { |
[email protected] | 5c8e67c | 2012-08-29 00:48:52 | [diff] [blame] | 389 | GetDefaultStoragePartition(browser_context)->GetDatabaseTracker()-> |
| 390 | SetForceKeepSessionState(); |
[email protected] | b1b502e | 2012-09-16 07:31:43 | [diff] [blame] | 391 | StoragePartition* storage_partition = |
| 392 | BrowserContext::GetDefaultStoragePartition(browser_context); |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 393 | |
| 394 | if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) { |
| 395 | BrowserThread::PostTask( |
| 396 | BrowserThread::IO, FROM_HERE, |
[email protected] | 6939075a | 2012-08-28 08:35:53 | [diff] [blame] | 397 | base::Bind( |
| 398 | &SaveSessionStateOnIOThread, |
jam | b84299e | 2016-04-12 16:58:59 | [diff] [blame] | 399 | make_scoped_refptr(BrowserContext::GetDefaultStoragePartition( |
| 400 | browser_context)->GetURLRequestContext()), |
[email protected] | 98d6d456 | 2014-06-25 20:57:55 | [diff] [blame] | 401 | static_cast<AppCacheServiceImpl*>( |
[email protected] | 63ef8551 | 2014-06-05 14:21:26 | [diff] [blame] | 402 | storage_partition->GetAppCacheService()))); |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 403 | } |
| 404 | |
[email protected] | 5f2aa72 | 2013-08-07 16:59:41 | [diff] [blame] | 405 | DOMStorageContextWrapper* dom_storage_context_proxy = |
| 406 | static_cast<DOMStorageContextWrapper*>( |
[email protected] | b1b502e | 2012-09-16 07:31:43 | [diff] [blame] | 407 | storage_partition->GetDOMStorageContext()); |
[email protected] | 5f2aa72 | 2013-08-07 16:59:41 | [diff] [blame] | 408 | dom_storage_context_proxy->SetForceKeepSessionState(); |
[email protected] | 735e20c | 2012-03-20 01:16:59 | [diff] [blame] | 409 | |
[email protected] | 89acda8 | 2013-06-25 20:52:50 | [diff] [blame] | 410 | IndexedDBContextImpl* indexed_db_context_impl = |
| 411 | static_cast<IndexedDBContextImpl*>( |
[email protected] | b1b502e | 2012-09-16 07:31:43 | [diff] [blame] | 412 | storage_partition->GetIndexedDBContext()); |
[email protected] | 89acda8 | 2013-06-25 20:52:50 | [diff] [blame] | 413 | // No task runner in unit tests. |
| 414 | if (indexed_db_context_impl->TaskRunner()) { |
| 415 | indexed_db_context_impl->TaskRunner()->PostTask( |
| 416 | FROM_HERE, |
| 417 | base::Bind(&SaveSessionStateOnIndexedDBThread, |
| 418 | make_scoped_refptr(indexed_db_context_impl))); |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | |
ttr314 | 81dc54b | 2015-08-06 20:11:26 | [diff] [blame] | 422 | void BrowserContext::SetDownloadManagerForTesting( |
| 423 | BrowserContext* browser_context, |
avi | 1ed4a437 | 2017-04-25 05:39:41 | [diff] [blame] | 424 | std::unique_ptr<content::DownloadManager> download_manager) { |
| 425 | SetDownloadManager(browser_context, std::move(download_manager)); |
ttr314 | 81dc54b | 2015-08-06 20:11:26 | [diff] [blame] | 426 | } |
| 427 | |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 428 | // static |
erg | e69130f5 | 2016-03-02 00:13:28 | [diff] [blame] | 429 | void BrowserContext::Initialize( |
| 430 | BrowserContext* browser_context, |
| 431 | const base::FilePath& path) { |
ben | 6c85c449 | 2016-06-16 20:40:51 | [diff] [blame] | 432 | |
| 433 | std::string new_id; |
| 434 | if (GetContentClient() && GetContentClient()->browser()) { |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 435 | new_id = GetContentClient()->browser()->GetServiceUserIdForBrowserContext( |
ben | 6c85c449 | 2016-06-16 20:40:51 | [diff] [blame] | 436 | browser_context); |
| 437 | } else { |
| 438 | // Some test scenarios initialize a BrowserContext without a content client. |
ben | cccfe2a | 2016-03-05 16:54:14 | [diff] [blame] | 439 | new_id = base::GenerateGUID(); |
ben | 6c85c449 | 2016-06-16 20:40:51 | [diff] [blame] | 440 | } |
erg | e69130f5 | 2016-03-02 00:13:28 | [diff] [blame] | 441 | |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 442 | ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>( |
| 443 | browser_context->GetUserData(kServiceUserId)); |
ben | 6c85c449 | 2016-06-16 20:40:51 | [diff] [blame] | 444 | if (holder) |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 445 | file::ForgetServiceUserIdUserDirAssociation(holder->user_id()); |
| 446 | file::AssociateServiceUserIdWithUserDir(new_id, path); |
ben | 6c85c449 | 2016-06-16 20:40:51 | [diff] [blame] | 447 | RemoveBrowserContextFromUserIdMap(browser_context); |
| 448 | g_user_id_to_context.Get()[new_id] = browser_context; |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 449 | browser_context->SetUserData(kServiceUserId, |
avi | 1ed4a437 | 2017-04-25 05:39:41 | [diff] [blame] | 450 | base::MakeUnique<ServiceUserIdHolder>(new_id)); |
erg | e69130f5 | 2016-03-02 00:13:28 | [diff] [blame] | 451 | |
avi | 1ed4a437 | 2017-04-25 05:39:41 | [diff] [blame] | 452 | browser_context->SetUserData( |
| 453 | kMojoWasInitialized, base::MakeUnique<base::SupportsUserData::Data>()); |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 454 | |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 455 | ServiceManagerConnection* service_manager_connection = |
| 456 | ServiceManagerConnection::GetForProcess(); |
fdoray | 4f155f0 | 2016-10-12 11:28:50 | [diff] [blame] | 457 | if (service_manager_connection && base::ThreadTaskRunnerHandle::IsSet()) { |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 458 | // NOTE: Many unit tests create a TestBrowserContext without initializing |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 459 | // Mojo or the global service manager connection. |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 460 | |
rockot | 400ea35b | 2016-10-15 19:15:32 | [diff] [blame] | 461 | service_manager::mojom::ServicePtr service; |
Ken Rockot | f4d8a94 | 2017-05-13 00:10:37 | [diff] [blame] | 462 | auto service_request = mojo::MakeRequest(&service); |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 463 | |
rockot | 400ea35b | 2016-10-15 19:15:32 | [diff] [blame] | 464 | service_manager::mojom::PIDReceiverPtr pid_receiver; |
ben | e6a9f01 | 2017-01-07 00:43:43 | [diff] [blame] | 465 | service_manager::Identity identity(mojom::kBrowserServiceName, new_id); |
ben | bd3c248 | 2017-01-07 05:48:21 | [diff] [blame] | 466 | service_manager_connection->GetConnector()->StartService( |
ben | e6a9f01 | 2017-01-07 00:43:43 | [diff] [blame] | 467 | identity, std::move(service), mojo::MakeRequest(&pid_receiver)); |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 468 | pid_receiver->SetPID(base::GetCurrentProcId()); |
| 469 | |
ben | aad37ecd | 2017-04-12 22:08:20 | [diff] [blame] | 470 | service_manager_connection->GetConnector()->StartService(identity); |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 471 | BrowserContextServiceManagerConnectionHolder* connection_holder = |
| 472 | new BrowserContextServiceManagerConnectionHolder( |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 473 | std::move(service_request)); |
avi | 1ed4a437 | 2017-04-25 05:39:41 | [diff] [blame] | 474 | browser_context->SetUserData(kServiceManagerConnection, |
| 475 | base::WrapUnique(connection_holder)); |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 476 | |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 477 | ServiceManagerConnection* connection = |
| 478 | connection_holder->service_manager_connection(); |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 479 | |
ben | 146248de | 2016-06-14 15:24:59 | [diff] [blame] | 480 | // New embedded service factories should be added to |connection| here. |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 481 | |
mek | c8e2b27 | 2017-06-06 23:18:28 | [diff] [blame] | 482 | if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 483 | switches::kDisableMojoLocalStorage)) { |
Colin Blundell | 812ec680 | 2017-06-27 15:50:49 | [diff] [blame^] | 484 | service_manager::EmbeddedServiceInfo info; |
mek | 17399e67 | 2017-05-09 03:51:20 | [diff] [blame] | 485 | // TODO(mek): Use sequenced task runner rather than single thread task |
| 486 | // runner when mojo supports that (http://crbug.com/678155). |
| 487 | info.factory = base::Bind( |
| 488 | &file::CreateFileService, |
| 489 | base::CreateSingleThreadTaskRunnerWithTraits( |
| 490 | {base::MayBlock(), base::TaskShutdownBehavior::BLOCK_SHUTDOWN}), |
| 491 | BrowserThread::GetTaskRunnerForThread(BrowserThread::DB)); |
ben | aabad0b | 2016-11-16 23:54:17 | [diff] [blame] | 492 | connection->AddEmbeddedService(file::mojom::kServiceName, info); |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 493 | } |
tibell | ab3d36b | 2017-03-10 02:57:25 | [diff] [blame] | 494 | |
| 495 | ContentBrowserClient::StaticServiceMap services; |
| 496 | browser_context->RegisterInProcessServices(&services); |
| 497 | for (const auto& entry : services) { |
| 498 | connection->AddEmbeddedService(entry.first, entry.second); |
| 499 | } |
Ken Rockot | c7a279c | 2017-05-04 23:51:45 | [diff] [blame] | 500 | |
| 501 | RegisterCommonBrowserInterfaces(connection); |
sammc | 030f501 | 2017-03-13 02:28:45 | [diff] [blame] | 502 | connection->Start(); |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 503 | } |
erg | e69130f5 | 2016-03-02 00:13:28 | [diff] [blame] | 504 | } |
| 505 | |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 506 | // static |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 507 | const std::string& BrowserContext::GetServiceUserIdFor( |
ben | cccfe2a | 2016-03-05 16:54:14 | [diff] [blame] | 508 | BrowserContext* browser_context) { |
erg | e69130f5 | 2016-03-02 00:13:28 | [diff] [blame] | 509 | CHECK(browser_context->GetUserData(kMojoWasInitialized)) |
| 510 | << "Attempting to get the mojo user id for a BrowserContext that was " |
| 511 | << "never Initialize()ed."; |
| 512 | |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 513 | ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>( |
| 514 | browser_context->GetUserData(kServiceUserId)); |
ben | 6c85c449 | 2016-06-16 20:40:51 | [diff] [blame] | 515 | return holder->user_id(); |
erg | e69130f5 | 2016-03-02 00:13:28 | [diff] [blame] | 516 | } |
| 517 | |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 518 | // static |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 519 | BrowserContext* BrowserContext::GetBrowserContextForServiceUserId( |
ben | 6c85c449 | 2016-06-16 20:40:51 | [diff] [blame] | 520 | const std::string& user_id) { |
| 521 | auto it = g_user_id_to_context.Get().find(user_id); |
| 522 | return it != g_user_id_to_context.Get().end() ? it->second : nullptr; |
| 523 | } |
| 524 | |
| 525 | // static |
rockot | 400ea35b | 2016-10-15 19:15:32 | [diff] [blame] | 526 | service_manager::Connector* BrowserContext::GetConnectorFor( |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 527 | BrowserContext* browser_context) { |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 528 | ServiceManagerConnection* connection = |
| 529 | GetServiceManagerConnectionFor(browser_context); |
ben | 5be0b913 | 2016-08-03 00:17:18 | [diff] [blame] | 530 | return connection ? connection->GetConnector() : nullptr; |
| 531 | } |
| 532 | |
| 533 | // static |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 534 | ServiceManagerConnection* BrowserContext::GetServiceManagerConnectionFor( |
ben | 5be0b913 | 2016-08-03 00:17:18 | [diff] [blame] | 535 | BrowserContext* browser_context) { |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 536 | BrowserContextServiceManagerConnectionHolder* connection_holder = |
| 537 | static_cast<BrowserContextServiceManagerConnectionHolder*>( |
| 538 | browser_context->GetUserData(kServiceManagerConnection)); |
| 539 | return connection_holder ? connection_holder->service_manager_connection() |
| 540 | : nullptr; |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 541 | } |
| 542 | |
mmenke | c0b2b8b1 | 2017-04-21 16:27:52 | [diff] [blame] | 543 | BrowserContext::BrowserContext() |
| 544 | : media_device_id_salt_(CreateRandomMediaDeviceIDSalt()) {} |
| 545 | |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 546 | BrowserContext::~BrowserContext() { |
erg | e69130f5 | 2016-03-02 00:13:28 | [diff] [blame] | 547 | CHECK(GetUserData(kMojoWasInitialized)) |
| 548 | << "Attempting to destroy a BrowserContext that never called " |
| 549 | << "Initialize()"; |
| 550 | |
kinuko | f6ed359c | 2016-07-26 13:27:21 | [diff] [blame] | 551 | DCHECK(!GetUserData(kStoragePartitionMapKeyName)) |
| 552 | << "StoragePartitionMap is not shut down properly"; |
| 553 | |
ben | 6c85c449 | 2016-06-16 20:40:51 | [diff] [blame] | 554 | RemoveBrowserContextFromUserIdMap(this); |
| 555 | |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 556 | if (GetUserData(kDownloadManagerKeyName)) |
| 557 | GetDownloadManager(this)->Shutdown(); |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 558 | } |
| 559 | |
kinuko | f6ed359c | 2016-07-26 13:27:21 | [diff] [blame] | 560 | void BrowserContext::ShutdownStoragePartitions() { |
| 561 | if (GetUserData(kStoragePartitionMapKeyName)) |
| 562 | RemoveUserData(kStoragePartitionMapKeyName); |
| 563 | } |
| 564 | |
mmenke | c0b2b8b1 | 2017-04-21 16:27:52 | [diff] [blame] | 565 | std::string BrowserContext::GetMediaDeviceIDSalt() { |
| 566 | return media_device_id_salt_; |
| 567 | } |
| 568 | |
| 569 | // static |
| 570 | std::string BrowserContext::CreateRandomMediaDeviceIDSalt() { |
| 571 | std::string salt; |
| 572 | base::Base64Encode(base::RandBytesAsString(16), &salt); |
| 573 | DCHECK(!salt.empty()); |
| 574 | return salt; |
| 575 | } |
| 576 | |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 577 | } // namespace content |