[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" |
Chris Cunningham | a5b3801 | 2017-10-28 07:27:18 | [diff] [blame] | 18 | #include "base/files/file_path.h" |
ben | cccfe2a | 2016-03-05 16:54:14 | [diff] [blame] | 19 | #include "base/guid.h" |
erg | e69130f5 | 2016-03-02 00:13:28 | [diff] [blame] | 20 | #include "base/lazy_instance.h" |
mmenke | c0b2b8b1 | 2017-04-21 16:27:52 | [diff] [blame] | 21 | #include "base/logging.h" |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 22 | #include "base/macros.h" |
avi | 1ed4a437 | 2017-04-25 05:39:41 | [diff] [blame] | 23 | #include "base/memory/ptr_util.h" |
erg | e69130f5 | 2016-03-02 00:13:28 | [diff] [blame] | 24 | #include "base/rand_util.h" |
mek | 17399e67 | 2017-05-09 03:51:20 | [diff] [blame] | 25 | #include "base/task_scheduler/post_task.h" |
Ken Rockot | 84f58fd | 2018-06-22 21:54:14 | [diff] [blame^] | 26 | #include "base/threading/sequenced_task_runner_handle.h" |
ben | 21a34c25 | 2016-06-29 22:24:37 | [diff] [blame] | 27 | #include "base/threading/thread_task_runner_handle.h" |
Andrey Lushnikov | d39d906 | 2018-04-24 17:33:35 | [diff] [blame] | 28 | #include "base/unguessable_token.h" |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 29 | #include "build/build_config.h" |
dmurph | 7ac019a | 2016-05-13 00:13:17 | [diff] [blame] | 30 | #include "content/browser/blob_storage/chrome_blob_storage_context.h" |
msramek | e169ccb | 2017-04-26 05:21:41 | [diff] [blame] | 31 | #include "content/browser/browsing_data/browsing_data_remover_impl.h" |
Ken Rockot | 84f58fd | 2018-06-22 21:54:14 | [diff] [blame^] | 32 | #include "content/browser/content_service_delegate_impl.h" |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 33 | #include "content/browser/download/download_manager_impl.h" |
[email protected] | c4d28166 | 2013-03-31 00:35:08 | [diff] [blame] | 34 | #include "content/browser/indexed_db/indexed_db_context_impl.h" |
[email protected] | 678c036 | 2012-12-05 08:02:44 | [diff] [blame] | 35 | #include "content/browser/loader/resource_dispatcher_host_impl.h" |
mvanouwerkerk | 17205ea | 2014-11-07 17:30:15 | [diff] [blame] | 36 | #include "content/browser/push_messaging/push_messaging_router.h" |
Ken Rockot | c7a279c | 2017-05-04 23:51:45 | [diff] [blame] | 37 | #include "content/browser/service_manager/common_browser_interfaces.h" |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 38 | #include "content/browser/storage_partition_impl_map.h" |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 39 | #include "content/common/child_process_host_impl.h" |
[email protected] | 393b6cb | 2014-05-15 00:55:12 | [diff] [blame] | 40 | #include "content/public/browser/blob_handle.h" |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 41 | #include "content/public/browser/browser_thread.h" |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 42 | #include "content/public/browser/content_browser_client.h" |
falken | 04a6912a | 2016-09-23 21:06:29 | [diff] [blame] | 43 | #include "content/public/browser/render_process_host.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 44 | #include "content/public/browser/site_instance.h" |
Elad Alon | 12ecdf0 | 2018-03-14 12:07:31 | [diff] [blame] | 45 | #include "content/public/browser/webrtc_event_logger.h" |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 46 | #include "content/public/common/content_switches.h" |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 47 | #include "content/public/common/service_manager_connection.h" |
ben | 51bb6c6 | 2016-11-17 20:15:57 | [diff] [blame] | 48 | #include "content/public/common/service_names.mojom.h" |
Chris Cunningham | 9e66947 | 2017-11-15 21:03:11 | [diff] [blame] | 49 | #include "media/capabilities/video_decode_stats_db_impl.h" |
Chris Cunningham | a5b3801 | 2017-10-28 07:27:18 | [diff] [blame] | 50 | #include "media/mojo/services/video_decode_perf_history.h" |
[email protected] | 4d7c4ef | 2012-03-16 01:47:12 | [diff] [blame] | 51 | #include "net/cookies/cookie_store.h" |
[email protected] | 6b8a3c74 | 2014-07-25 00:25:35 | [diff] [blame] | 52 | #include "net/ssl/channel_id_service.h" |
| 53 | #include "net/ssl/channel_id_store.h" |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 54 | #include "net/url_request/url_request_context.h" |
[email protected] | 6939075a | 2012-08-28 08:35:53 | [diff] [blame] | 55 | #include "net/url_request/url_request_context_getter.h" |
Ken Rockot | 84f58fd | 2018-06-22 21:54:14 | [diff] [blame^] | 56 | #include "services/content/public/mojom/constants.mojom.h" |
| 57 | #include "services/content/service.h" |
ben | 768c8dc | 2016-08-12 00:26:50 | [diff] [blame] | 58 | #include "services/file/file_service.h" |
Ken Rockot | 21142de | 2018-02-10 01:45:30 | [diff] [blame] | 59 | #include "services/file/public/mojom/constants.mojom.h" |
ben | 768c8dc | 2016-08-12 00:26:50 | [diff] [blame] | 60 | #include "services/file/user_id_map.h" |
rockot | 734fb66 | 2016-10-15 16:41:30 | [diff] [blame] | 61 | #include "services/service_manager/public/cpp/connector.h" |
Ken Rockot | 543f5e3 | 2018-02-04 02:13:50 | [diff] [blame] | 62 | #include "services/service_manager/public/mojom/service.mojom.h" |
Xing Liu | 8986047 | 2018-02-09 20:07:02 | [diff] [blame] | 63 | #include "storage/browser/blob/blob_storage_context.h" |
pilgrim | e92c5fcd | 2014-09-10 23:31:23 | [diff] [blame] | 64 | #include "storage/browser/database/database_tracker.h" |
| 65 | #include "storage/browser/fileapi/external_mount_points.h" |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 66 | |
Elad Alon | 03ad23b | 2018-02-07 16:54:31 | [diff] [blame] | 67 | |
[email protected] | 314c3e2 | 2012-02-21 03:57:42 | [diff] [blame] | 68 | using base::UserDataAdapter; |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 69 | |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 70 | namespace content { |
| 71 | |
[email protected] | 735e20c | 2012-03-20 01:16:59 | [diff] [blame] | 72 | namespace { |
| 73 | |
scottmg | 5e65e3a | 2017-03-08 08:48:46 | [diff] [blame] | 74 | base::LazyInstance<std::map<std::string, BrowserContext*>>::DestructorAtExit |
ben | 6c85c449 | 2016-06-16 20:40:51 | [diff] [blame] | 75 | g_user_id_to_context = LAZY_INSTANCE_INITIALIZER; |
| 76 | |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 77 | class ServiceUserIdHolder : public base::SupportsUserData::Data { |
ben | 6c85c449 | 2016-06-16 20:40:51 | [diff] [blame] | 78 | public: |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 79 | explicit ServiceUserIdHolder(const std::string& user_id) |
| 80 | : user_id_(user_id) {} |
| 81 | ~ServiceUserIdHolder() override {} |
ben | 6c85c449 | 2016-06-16 20:40:51 | [diff] [blame] | 82 | |
| 83 | const std::string& user_id() const { return user_id_; } |
| 84 | |
| 85 | private: |
| 86 | std::string user_id_; |
| 87 | |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 88 | DISALLOW_COPY_AND_ASSIGN(ServiceUserIdHolder); |
ben | 6c85c449 | 2016-06-16 20:40:51 | [diff] [blame] | 89 | }; |
erg | e69130f5 | 2016-03-02 00:13:28 | [diff] [blame] | 90 | |
Ken Rockot | 84f58fd | 2018-06-22 21:54:14 | [diff] [blame^] | 91 | class ContentServiceDelegateHolder : public base::SupportsUserData::Data { |
| 92 | public: |
| 93 | explicit ContentServiceDelegateHolder(BrowserContext* browser_context) |
| 94 | : delegate_(browser_context) {} |
| 95 | ~ContentServiceDelegateHolder() override = default; |
| 96 | |
| 97 | ContentServiceDelegateImpl* delegate() { return &delegate_; } |
| 98 | |
| 99 | private: |
| 100 | ContentServiceDelegateImpl delegate_; |
| 101 | |
| 102 | DISALLOW_COPY_AND_ASSIGN(ContentServiceDelegateHolder); |
| 103 | }; |
| 104 | |
[email protected] | e0ce8a1e | 2012-09-18 10:26:36 | [diff] [blame] | 105 | // Key names on BrowserContext. |
msramek | e169ccb | 2017-04-26 05:21:41 | [diff] [blame] | 106 | const char kBrowsingDataRemoverKey[] = "browsing-data-remover"; |
Ken Rockot | 84f58fd | 2018-06-22 21:54:14 | [diff] [blame^] | 107 | const char kContentServiceDelegateKey[] = "content-service-delegate"; |
[email protected] | 6ef0c391 | 2013-01-25 22:46:34 | [diff] [blame] | 108 | const char kDownloadManagerKeyName[] = "download_manager"; |
erg | e69130f5 | 2016-03-02 00:13:28 | [diff] [blame] | 109 | const char kMojoWasInitialized[] = "mojo-was-initialized"; |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 110 | const char kServiceManagerConnection[] = "service-manager-connection"; |
| 111 | const char kServiceUserId[] = "service-user-id"; |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 112 | const char kStoragePartitionMapKeyName[] = "content_storage_partition_map"; |
Chris Cunningham | a5b3801 | 2017-10-28 07:27:18 | [diff] [blame] | 113 | const char kVideoDecodePerfHistoryId[] = "video-decode-perf-history"; |
erg | e69130f5 | 2016-03-02 00:13:28 | [diff] [blame] | 114 | |
[email protected] | 9afc14e2 | 2013-09-25 22:34:14 | [diff] [blame] | 115 | #if defined(OS_CHROMEOS) |
| 116 | const char kMountPointsKey[] = "mount_points"; |
| 117 | #endif // defined(OS_CHROMEOS) |
| 118 | |
ben | 6c85c449 | 2016-06-16 20:40:51 | [diff] [blame] | 119 | void RemoveBrowserContextFromUserIdMap(BrowserContext* browser_context) { |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 120 | ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>( |
| 121 | browser_context->GetUserData(kServiceUserId)); |
ben | 6c85c449 | 2016-06-16 20:40:51 | [diff] [blame] | 122 | if (holder) { |
| 123 | auto it = g_user_id_to_context.Get().find(holder->user_id()); |
| 124 | if (it != g_user_id_to_context.Get().end()) |
| 125 | g_user_id_to_context.Get().erase(it); |
| 126 | } |
| 127 | } |
| 128 | |
[email protected] | 14acc64 | 2012-11-17 12:20:10 | [diff] [blame] | 129 | StoragePartitionImplMap* GetStoragePartitionMap( |
| 130 | BrowserContext* browser_context) { |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 131 | StoragePartitionImplMap* partition_map = |
| 132 | static_cast<StoragePartitionImplMap*>( |
a.cavalcanti | ffab7376 | 2015-08-15 02:55:48 | [diff] [blame] | 133 | browser_context->GetUserData(kStoragePartitionMapKeyName)); |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 134 | if (!partition_map) { |
avi | 1ed4a437 | 2017-04-25 05:39:41 | [diff] [blame] | 135 | auto partition_map_owned = |
Jeremy Roman | 04f27c37 | 2017-10-27 15:20:55 | [diff] [blame] | 136 | std::make_unique<StoragePartitionImplMap>(browser_context); |
avi | 1ed4a437 | 2017-04-25 05:39:41 | [diff] [blame] | 137 | partition_map = partition_map_owned.get(); |
| 138 | browser_context->SetUserData(kStoragePartitionMapKeyName, |
| 139 | std::move(partition_map_owned)); |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 140 | } |
[email protected] | 14acc64 | 2012-11-17 12:20:10 | [diff] [blame] | 141 | return partition_map; |
| 142 | } |
| 143 | |
| 144 | StoragePartition* GetStoragePartitionFromConfig( |
| 145 | BrowserContext* browser_context, |
| 146 | const std::string& partition_domain, |
| 147 | const std::string& partition_name, |
Dan Elphick | 6c0d849 | 2017-09-12 09:35:38 | [diff] [blame] | 148 | bool in_memory, |
| 149 | bool can_create) { |
[email protected] | 14acc64 | 2012-11-17 12:20:10 | [diff] [blame] | 150 | StoragePartitionImplMap* partition_map = |
| 151 | GetStoragePartitionMap(browser_context); |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 152 | |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 153 | if (browser_context->IsOffTheRecord()) |
| 154 | in_memory = true; |
| 155 | |
Dan Elphick | 6c0d849 | 2017-09-12 09:35:38 | [diff] [blame] | 156 | return partition_map->Get(partition_domain, partition_name, in_memory, |
| 157 | can_create); |
[email protected] | d1198fd | 2012-08-13 22:50:19 | [diff] [blame] | 158 | } |
| 159 | |
[email protected] | 6939075a | 2012-08-28 08:35:53 | [diff] [blame] | 160 | void SaveSessionStateOnIOThread( |
| 161 | const scoped_refptr<net::URLRequestContextGetter>& context_getter, |
[email protected] | 98d6d456 | 2014-06-25 20:57:55 | [diff] [blame] | 162 | AppCacheServiceImpl* appcache_service) { |
[email protected] | 6939075a | 2012-08-28 08:35:53 | [diff] [blame] | 163 | net::URLRequestContext* context = context_getter->GetURLRequestContext(); |
mmenke | ded79da | 2016-02-06 08:28:51 | [diff] [blame] | 164 | context->cookie_store()->SetForceKeepSessionState(); |
Reid Kleckner | c114888 | 2018-05-11 00:04:07 | [diff] [blame] | 165 | context->channel_id_service()->GetChannelIDStore()-> |
| 166 | SetForceKeepSessionState(); |
[email protected] | 6939075a | 2012-08-28 08:35:53 | [diff] [blame] | 167 | appcache_service->set_force_keep_session_state(); |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 168 | } |
| 169 | |
[email protected] | 89acda8 | 2013-06-25 20:52:50 | [diff] [blame] | 170 | void SaveSessionStateOnIndexedDBThread( |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 171 | scoped_refptr<IndexedDBContextImpl> indexed_db_context) { |
[email protected] | bf510ed | 2012-06-05 08:31:43 | [diff] [blame] | 172 | indexed_db_context->SetForceKeepSessionState(); |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 173 | } |
| 174 | |
falken | 41f417516 | 2014-10-29 07:03:41 | [diff] [blame] | 175 | void ShutdownServiceWorkerContext(StoragePartition* partition) { |
| 176 | ServiceWorkerContextWrapper* wrapper = |
| 177 | static_cast<ServiceWorkerContextWrapper*>( |
| 178 | partition->GetServiceWorkerContext()); |
| 179 | wrapper->process_manager()->Shutdown(); |
| 180 | } |
| 181 | |
avi | 1ed4a437 | 2017-04-25 05:39:41 | [diff] [blame] | 182 | void SetDownloadManager( |
| 183 | BrowserContext* context, |
| 184 | std::unique_ptr<content::DownloadManager> download_manager) { |
ttr314 | 81dc54b | 2015-08-06 20:11:26 | [diff] [blame] | 185 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 186 | DCHECK(download_manager); |
avi | 1ed4a437 | 2017-04-25 05:39:41 | [diff] [blame] | 187 | context->SetUserData(kDownloadManagerKeyName, std::move(download_manager)); |
ttr314 | 81dc54b | 2015-08-06 20:11:26 | [diff] [blame] | 188 | } |
| 189 | |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 190 | class BrowserContextServiceManagerConnectionHolder |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 191 | : public base::SupportsUserData::Data { |
| 192 | public: |
ben | aad37ecd | 2017-04-12 22:08:20 | [diff] [blame] | 193 | explicit BrowserContextServiceManagerConnectionHolder( |
rockot | 400ea35b | 2016-10-15 19:15:32 | [diff] [blame] | 194 | service_manager::mojom::ServiceRequest request) |
ben | aad37ecd | 2017-04-12 22:08:20 | [diff] [blame] | 195 | : service_manager_connection_(ServiceManagerConnection::Create( |
rockot | cef3827 | 2016-07-15 22:47:47 | [diff] [blame] | 196 | std::move(request), |
| 197 | BrowserThread::GetTaskRunnerForThread(BrowserThread::IO))) {} |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 198 | ~BrowserContextServiceManagerConnectionHolder() override {} |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 199 | |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 200 | ServiceManagerConnection* service_manager_connection() { |
| 201 | return service_manager_connection_.get(); |
| 202 | } |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 203 | |
| 204 | private: |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 205 | std::unique_ptr<ServiceManagerConnection> service_manager_connection_; |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 206 | |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 207 | DISALLOW_COPY_AND_ASSIGN(BrowserContextServiceManagerConnectionHolder); |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 208 | }; |
| 209 | |
Jens Widell | 7ca8fb4 | 2018-02-21 14:11:54 | [diff] [blame] | 210 | base::WeakPtr<storage::BlobStorageContext> BlobStorageContextGetterForBrowser( |
Xing Liu | 8986047 | 2018-02-09 20:07:02 | [diff] [blame] | 211 | scoped_refptr<ChromeBlobStorageContext> blob_context) { |
| 212 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 213 | return blob_context->context()->AsWeakPtr(); |
| 214 | } |
| 215 | |
[email protected] | 735e20c | 2012-03-20 01:16:59 | [diff] [blame] | 216 | } // namespace |
| 217 | |
[email protected] | 14acc64 | 2012-11-17 12:20:10 | [diff] [blame] | 218 | // static |
| 219 | void BrowserContext::AsyncObliterateStoragePartition( |
| 220 | BrowserContext* browser_context, |
[email protected] | 399583b | 2012-12-11 09:33:42 | [diff] [blame] | 221 | const GURL& site, |
| 222 | const base::Closure& on_gc_required) { |
| 223 | GetStoragePartitionMap(browser_context)->AsyncObliterate(site, |
| 224 | on_gc_required); |
| 225 | } |
| 226 | |
| 227 | // static |
| 228 | void BrowserContext::GarbageCollectStoragePartitions( |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 229 | BrowserContext* browser_context, |
| 230 | std::unique_ptr<base::hash_set<base::FilePath>> active_paths, |
| 231 | const base::Closure& done) { |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 232 | GetStoragePartitionMap(browser_context) |
| 233 | ->GarbageCollect(std::move(active_paths), done); |
[email protected] | 14acc64 | 2012-11-17 12:20:10 | [diff] [blame] | 234 | } |
| 235 | |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 236 | DownloadManager* BrowserContext::GetDownloadManager( |
| 237 | BrowserContext* context) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 238 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 239 | if (!context->GetUserData(kDownloadManagerKeyName)) { |
Min Qin | ddb40ce | 2017-10-28 03:38:26 | [diff] [blame] | 240 | DownloadManager* download_manager = new DownloadManagerImpl(context); |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 241 | |
avi | 1ed4a437 | 2017-04-25 05:39:41 | [diff] [blame] | 242 | SetDownloadManager(context, base::WrapUnique(download_manager)); |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 243 | download_manager->SetDelegate(context->GetDownloadManagerDelegate()); |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 244 | } |
| 245 | |
[email protected] | eba4a4d | 2013-05-29 02:18:06 | [diff] [blame] | 246 | return static_cast<DownloadManager*>( |
| 247 | context->GetUserData(kDownloadManagerKeyName)); |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 248 | } |
| 249 | |
[email protected] | 6ef0c391 | 2013-01-25 22:46:34 | [diff] [blame] | 250 | // static |
[email protected] | cd501a7 | 2014-08-22 19:58:31 | [diff] [blame] | 251 | storage::ExternalMountPoints* BrowserContext::GetMountPoints( |
[email protected] | 6ef0c391 | 2013-01-25 22:46:34 | [diff] [blame] | 252 | BrowserContext* context) { |
| 253 | // Ensure that these methods are called on the UI thread, except for |
| 254 | // unittests where a UI thread might not have been created. |
| 255 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
Gabriel Charette | 2983181c | 2018-03-28 17:01:09 | [diff] [blame] | 256 | !BrowserThread::IsThreadInitialized(BrowserThread::UI)); |
[email protected] | 6ef0c391 | 2013-01-25 22:46:34 | [diff] [blame] | 257 | |
| 258 | #if defined(OS_CHROMEOS) |
| 259 | if (!context->GetUserData(kMountPointsKey)) { |
[email protected] | cd501a7 | 2014-08-22 19:58:31 | [diff] [blame] | 260 | scoped_refptr<storage::ExternalMountPoints> mount_points = |
| 261 | storage::ExternalMountPoints::CreateRefCounted(); |
[email protected] | 6ef0c391 | 2013-01-25 22:46:34 | [diff] [blame] | 262 | context->SetUserData( |
| 263 | kMountPointsKey, |
Jeremy Roman | 04f27c37 | 2017-10-27 15:20:55 | [diff] [blame] | 264 | std::make_unique<UserDataAdapter<storage::ExternalMountPoints>>( |
avi | cb129c0 | 2017-05-03 06:49:29 | [diff] [blame] | 265 | mount_points.get())); |
[email protected] | 6ef0c391 | 2013-01-25 22:46:34 | [diff] [blame] | 266 | } |
| 267 | |
[email protected] | cd501a7 | 2014-08-22 19:58:31 | [diff] [blame] | 268 | return UserDataAdapter<storage::ExternalMountPoints>::Get(context, |
| 269 | kMountPointsKey); |
[email protected] | 6ef0c391 | 2013-01-25 22:46:34 | [diff] [blame] | 270 | #else |
Ivan Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 271 | return nullptr; |
[email protected] | 6ef0c391 | 2013-01-25 22:46:34 | [diff] [blame] | 272 | #endif |
| 273 | } |
| 274 | |
msramek | e169ccb | 2017-04-26 05:21:41 | [diff] [blame] | 275 | // static |
| 276 | content::BrowsingDataRemover* content::BrowserContext::GetBrowsingDataRemover( |
| 277 | BrowserContext* context) { |
| 278 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 279 | |
| 280 | if (!context->GetUserData(kBrowsingDataRemoverKey)) { |
| 281 | std::unique_ptr<BrowsingDataRemoverImpl> remover = |
Jeremy Roman | 04f27c37 | 2017-10-27 15:20:55 | [diff] [blame] | 282 | std::make_unique<BrowsingDataRemoverImpl>(context); |
msramek | e169ccb | 2017-04-26 05:21:41 | [diff] [blame] | 283 | remover->SetEmbedderDelegate(context->GetBrowsingDataRemoverDelegate()); |
| 284 | context->SetUserData(kBrowsingDataRemoverKey, std::move(remover)); |
| 285 | } |
| 286 | |
| 287 | return static_cast<BrowsingDataRemoverImpl*>( |
| 288 | context->GetUserData(kBrowsingDataRemoverKey)); |
| 289 | } |
| 290 | |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 291 | StoragePartition* BrowserContext::GetStoragePartition( |
| 292 | BrowserContext* browser_context, |
Dan Elphick | 6c0d849 | 2017-09-12 09:35:38 | [diff] [blame] | 293 | SiteInstance* site_instance, |
| 294 | bool can_create) { |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 295 | std::string partition_domain; |
| 296 | std::string partition_name; |
| 297 | bool in_memory = false; |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 298 | |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 299 | if (site_instance) { |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 300 | GetContentClient()->browser()->GetStoragePartitionConfigForSite( |
[email protected] | 14acc64 | 2012-11-17 12:20:10 | [diff] [blame] | 301 | browser_context, site_instance->GetSiteURL(), true, |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 302 | &partition_domain, &partition_name, &in_memory); |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 303 | } |
| 304 | |
Dan Elphick | 6c0d849 | 2017-09-12 09:35:38 | [diff] [blame] | 305 | return GetStoragePartitionFromConfig(browser_context, partition_domain, |
| 306 | partition_name, in_memory, can_create); |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 307 | } |
| 308 | |
[email protected] | e94bbcb | 2012-09-07 05:33:57 | [diff] [blame] | 309 | StoragePartition* BrowserContext::GetStoragePartitionForSite( |
| 310 | BrowserContext* browser_context, |
Dan Elphick | 6c0d849 | 2017-09-12 09:35:38 | [diff] [blame] | 311 | const GURL& site, |
| 312 | bool can_create) { |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 313 | std::string partition_domain; |
| 314 | std::string partition_name; |
| 315 | bool in_memory; |
[email protected] | e94bbcb | 2012-09-07 05:33:57 | [diff] [blame] | 316 | |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 317 | GetContentClient()->browser()->GetStoragePartitionConfigForSite( |
[email protected] | 14acc64 | 2012-11-17 12:20:10 | [diff] [blame] | 318 | browser_context, site, true, &partition_domain, &partition_name, |
| 319 | &in_memory); |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 320 | |
Dan Elphick | 6c0d849 | 2017-09-12 09:35:38 | [diff] [blame] | 321 | return GetStoragePartitionFromConfig(browser_context, partition_domain, |
| 322 | partition_name, in_memory, can_create); |
[email protected] | e94bbcb | 2012-09-07 05:33:57 | [diff] [blame] | 323 | } |
| 324 | |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 325 | void BrowserContext::ForEachStoragePartition( |
| 326 | BrowserContext* browser_context, |
| 327 | const StoragePartitionCallback& callback) { |
| 328 | StoragePartitionImplMap* partition_map = |
| 329 | static_cast<StoragePartitionImplMap*>( |
a.cavalcanti | ffab7376 | 2015-08-15 02:55:48 | [diff] [blame] | 330 | browser_context->GetUserData(kStoragePartitionMapKeyName)); |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 331 | if (!partition_map) |
| 332 | return; |
| 333 | |
| 334 | partition_map->ForEach(callback); |
| 335 | } |
| 336 | |
| 337 | StoragePartition* BrowserContext::GetDefaultStoragePartition( |
| 338 | BrowserContext* browser_context) { |
Ivan Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 339 | return GetStoragePartition(browser_context, nullptr); |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 340 | } |
| 341 | |
tbarzic | db71268 | 2015-03-06 06:05:41 | [diff] [blame] | 342 | // static |
[email protected] | 393b6cb | 2014-05-15 00:55:12 | [diff] [blame] | 343 | void BrowserContext::CreateMemoryBackedBlob(BrowserContext* browser_context, |
Marijn Kruisselbrink | 604fd7e7 | 2017-10-26 16:31:05 | [diff] [blame] | 344 | const char* data, |
| 345 | size_t length, |
| 346 | const std::string& content_type, |
| 347 | BlobCallback callback) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 348 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 393b6cb | 2014-05-15 00:55:12 | [diff] [blame] | 349 | |
| 350 | ChromeBlobStorageContext* blob_context = |
| 351 | ChromeBlobStorageContext::GetFor(browser_context); |
| 352 | BrowserThread::PostTaskAndReplyWithResult( |
| 353 | BrowserThread::IO, FROM_HERE, |
Marijn Kruisselbrink | 604fd7e7 | 2017-10-26 16:31:05 | [diff] [blame] | 354 | base::BindOnce(&ChromeBlobStorageContext::CreateMemoryBackedBlob, |
| 355 | base::WrapRefCounted(blob_context), data, length, |
| 356 | content_type), |
| 357 | std::move(callback)); |
[email protected] | 393b6cb | 2014-05-15 00:55:12 | [diff] [blame] | 358 | } |
| 359 | |
[email protected] | 66e53d028 | 2014-08-07 10:04:35 | [diff] [blame] | 360 | // static |
Xing Liu | 8986047 | 2018-02-09 20:07:02 | [diff] [blame] | 361 | BrowserContext::BlobContextGetter BrowserContext::GetBlobStorageContext( |
| 362 | BrowserContext* browser_context) { |
| 363 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 364 | scoped_refptr<ChromeBlobStorageContext> chrome_blob_context = |
| 365 | ChromeBlobStorageContext::GetFor(browser_context); |
Jens Widell | 7ca8fb4 | 2018-02-21 14:11:54 | [diff] [blame] | 366 | return base::BindRepeating(&BlobStorageContextGetterForBrowser, |
| 367 | chrome_blob_context); |
Xing Liu | 8986047 | 2018-02-09 20:07:02 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | // static |
Marijn Kruisselbrink | 9e073a8 | 2018-06-18 17:48:58 | [diff] [blame] | 371 | blink::mojom::BlobPtr BrowserContext::GetBlobPtr( |
| 372 | BrowserContext* browser_context, |
| 373 | const std::string& uuid) { |
| 374 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 375 | return ChromeBlobStorageContext::GetBlobPtr(browser_context, uuid); |
| 376 | } |
| 377 | |
| 378 | // static |
[email protected] | 66e53d028 | 2014-08-07 10:04:35 | [diff] [blame] | 379 | void BrowserContext::DeliverPushMessage( |
| 380 | BrowserContext* browser_context, |
| 381 | const GURL& origin, |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 382 | int64_t service_worker_registration_id, |
harkness | dd4d2b2 | 2016-01-27 19:26:43 | [diff] [blame] | 383 | const PushEventPayload& payload, |
Peter Beverloo | 7815db1e0 | 2017-07-12 19:03:21 | [diff] [blame] | 384 | const base::Callback<void(mojom::PushDeliveryStatus)>& callback) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 385 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
harkness | dd4d2b2 | 2016-01-27 19:26:43 | [diff] [blame] | 386 | PushMessagingRouter::DeliverMessage(browser_context, origin, |
| 387 | service_worker_registration_id, payload, |
| 388 | callback); |
[email protected] | 66e53d028 | 2014-08-07 10:04:35 | [diff] [blame] | 389 | } |
| 390 | |
falken | 41f417516 | 2014-10-29 07:03:41 | [diff] [blame] | 391 | // static |
| 392 | void BrowserContext::NotifyWillBeDestroyed(BrowserContext* browser_context) { |
Lukasz Anforowicz | 58d0dac | 2018-03-23 15:48:10 | [diff] [blame] | 393 | // Make sure NotifyWillBeDestroyed is idempotent. This helps facilitate the |
| 394 | // pattern where NotifyWillBeDestroyed is called from *both* |
| 395 | // ShellBrowserContext and its derived classes (e.g. |
| 396 | // LayoutTestBrowserContext). |
| 397 | if (browser_context->was_notify_will_be_destroyed_called_) |
| 398 | return; |
| 399 | browser_context->was_notify_will_be_destroyed_called_ = true; |
| 400 | |
falken | 41f417516 | 2014-10-29 07:03:41 | [diff] [blame] | 401 | // Service Workers must shutdown before the browser context is destroyed, |
| 402 | // since they keep render process hosts alive and the codebase assumes that |
| 403 | // render process hosts die before their profile (browser context) dies. |
| 404 | ForEachStoragePartition(browser_context, |
| 405 | base::Bind(ShutdownServiceWorkerContext)); |
falken | 04a6912a | 2016-09-23 21:06:29 | [diff] [blame] | 406 | |
| 407 | // Shared workers also keep render process hosts alive, and are expected to |
Darin Fisher | d3768b2f6 | 2017-10-07 01:00:16 | [diff] [blame] | 408 | // return ref counts to 0 after documents close. However, to ensure that |
| 409 | // hosts are destructed now, forcibly release their ref counts here. |
falken | 04a6912a | 2016-09-23 21:06:29 | [diff] [blame] | 410 | for (RenderProcessHost::iterator host_iterator = |
| 411 | RenderProcessHost::AllHostsIterator(); |
| 412 | !host_iterator.IsAtEnd(); host_iterator.Advance()) { |
| 413 | RenderProcessHost* host = host_iterator.GetCurrentValue(); |
Benoit Lize | ab08362c | 2017-07-19 14:50:54 | [diff] [blame] | 414 | if (host->GetBrowserContext() == browser_context) { |
| 415 | // This will also clean up spare RPH references. |
Yutaka Hirano | 09a9afb9 | 2017-08-16 09:11:45 | [diff] [blame] | 416 | host->DisableKeepAliveRefCount(); |
Benoit Lize | ab08362c | 2017-07-19 14:50:54 | [diff] [blame] | 417 | } |
falken | 04a6912a | 2016-09-23 21:06:29 | [diff] [blame] | 418 | } |
falken | 41f417516 | 2014-10-29 07:03:41 | [diff] [blame] | 419 | } |
| 420 | |
[email protected] | 314c3e2 | 2012-02-21 03:57:42 | [diff] [blame] | 421 | void BrowserContext::EnsureResourceContextInitialized(BrowserContext* context) { |
[email protected] | 7e26ac9 | 2012-02-27 20:15:05 | [diff] [blame] | 422 | // This will be enough to tickle initialization of BrowserContext if |
| 423 | // necessary, which initializes ResourceContext. The reason we don't call |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 424 | // ResourceContext::InitializeResourceContext() directly here is that |
| 425 | // ResourceContext initialization may call back into BrowserContext |
| 426 | // and when that call returns it'll end rewriting its UserData map. It will |
| 427 | // end up rewriting the same value but this still causes a race condition. |
| 428 | // |
| 429 | // See http://crbug.com/115678. |
| 430 | GetDefaultStoragePartition(context); |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 431 | } |
| 432 | |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 433 | void BrowserContext::SaveSessionState(BrowserContext* browser_context) { |
[email protected] | b1b502e | 2012-09-16 07:31:43 | [diff] [blame] | 434 | StoragePartition* storage_partition = |
| 435 | BrowserContext::GetDefaultStoragePartition(browser_context); |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 436 | |
Joshua Bell | 607cb14 | 2017-07-24 19:17:16 | [diff] [blame] | 437 | storage::DatabaseTracker* database_tracker = |
| 438 | storage_partition->GetDatabaseTracker(); |
| 439 | database_tracker->task_runner()->PostTask( |
| 440 | FROM_HERE, |
| 441 | base::BindOnce(&storage::DatabaseTracker::SetForceKeepSessionState, |
kylechar | da69d88 | 2017-10-04 05:49:52 | [diff] [blame] | 442 | base::WrapRefCounted(database_tracker))); |
Joshua Bell | 607cb14 | 2017-07-24 19:17:16 | [diff] [blame] | 443 | |
Gabriel Charette | 2983181c | 2018-03-28 17:01:09 | [diff] [blame] | 444 | if (BrowserThread::IsThreadInitialized(BrowserThread::IO)) { |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 445 | BrowserThread::PostTask( |
| 446 | BrowserThread::IO, FROM_HERE, |
tzik | 4fea24af | 2017-08-23 11:41:47 | [diff] [blame] | 447 | base::BindOnce( |
[email protected] | 6939075a | 2012-08-28 08:35:53 | [diff] [blame] | 448 | &SaveSessionStateOnIOThread, |
Clark DuVall | 385b5a5 | 2018-06-14 21:33:32 | [diff] [blame] | 449 | base::WrapRefCounted(storage_partition->GetURLRequestContext()), |
[email protected] | 98d6d456 | 2014-06-25 20:57:55 | [diff] [blame] | 450 | static_cast<AppCacheServiceImpl*>( |
[email protected] | 63ef8551 | 2014-06-05 14:21:26 | [diff] [blame] | 451 | storage_partition->GetAppCacheService()))); |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 452 | } |
| 453 | |
Clark DuVall | 385b5a5 | 2018-06-14 21:33:32 | [diff] [blame] | 454 | storage_partition->GetCookieManagerForBrowserProcess() |
| 455 | ->SetForceKeepSessionState(); |
| 456 | |
[email protected] | 5f2aa72 | 2013-08-07 16:59:41 | [diff] [blame] | 457 | DOMStorageContextWrapper* dom_storage_context_proxy = |
| 458 | static_cast<DOMStorageContextWrapper*>( |
[email protected] | b1b502e | 2012-09-16 07:31:43 | [diff] [blame] | 459 | storage_partition->GetDOMStorageContext()); |
[email protected] | 5f2aa72 | 2013-08-07 16:59:41 | [diff] [blame] | 460 | dom_storage_context_proxy->SetForceKeepSessionState(); |
[email protected] | 735e20c | 2012-03-20 01:16:59 | [diff] [blame] | 461 | |
[email protected] | 89acda8 | 2013-06-25 20:52:50 | [diff] [blame] | 462 | IndexedDBContextImpl* indexed_db_context_impl = |
| 463 | static_cast<IndexedDBContextImpl*>( |
[email protected] | b1b502e | 2012-09-16 07:31:43 | [diff] [blame] | 464 | storage_partition->GetIndexedDBContext()); |
[email protected] | 89acda8 | 2013-06-25 20:52:50 | [diff] [blame] | 465 | // No task runner in unit tests. |
| 466 | if (indexed_db_context_impl->TaskRunner()) { |
| 467 | indexed_db_context_impl->TaskRunner()->PostTask( |
kylechar | da69d88 | 2017-10-04 05:49:52 | [diff] [blame] | 468 | FROM_HERE, |
| 469 | base::BindOnce(&SaveSessionStateOnIndexedDBThread, |
| 470 | base::WrapRefCounted(indexed_db_context_impl))); |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | |
ttr314 | 81dc54b | 2015-08-06 20:11:26 | [diff] [blame] | 474 | void BrowserContext::SetDownloadManagerForTesting( |
| 475 | BrowserContext* browser_context, |
avi | 1ed4a437 | 2017-04-25 05:39:41 | [diff] [blame] | 476 | std::unique_ptr<content::DownloadManager> download_manager) { |
| 477 | SetDownloadManager(browser_context, std::move(download_manager)); |
ttr314 | 81dc54b | 2015-08-06 20:11:26 | [diff] [blame] | 478 | } |
| 479 | |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 480 | // static |
erg | e69130f5 | 2016-03-02 00:13:28 | [diff] [blame] | 481 | void BrowserContext::Initialize( |
| 482 | BrowserContext* browser_context, |
| 483 | const base::FilePath& path) { |
ben | 6c85c449 | 2016-06-16 20:40:51 | [diff] [blame] | 484 | std::string new_id; |
| 485 | if (GetContentClient() && GetContentClient()->browser()) { |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 486 | new_id = GetContentClient()->browser()->GetServiceUserIdForBrowserContext( |
ben | 6c85c449 | 2016-06-16 20:40:51 | [diff] [blame] | 487 | browser_context); |
| 488 | } else { |
| 489 | // Some test scenarios initialize a BrowserContext without a content client. |
ben | cccfe2a | 2016-03-05 16:54:14 | [diff] [blame] | 490 | new_id = base::GenerateGUID(); |
ben | 6c85c449 | 2016-06-16 20:40:51 | [diff] [blame] | 491 | } |
erg | e69130f5 | 2016-03-02 00:13:28 | [diff] [blame] | 492 | |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 493 | ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>( |
| 494 | browser_context->GetUserData(kServiceUserId)); |
ben | 6c85c449 | 2016-06-16 20:40:51 | [diff] [blame] | 495 | if (holder) |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 496 | file::ForgetServiceUserIdUserDirAssociation(holder->user_id()); |
| 497 | file::AssociateServiceUserIdWithUserDir(new_id, path); |
ben | 6c85c449 | 2016-06-16 20:40:51 | [diff] [blame] | 498 | RemoveBrowserContextFromUserIdMap(browser_context); |
| 499 | g_user_id_to_context.Get()[new_id] = browser_context; |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 500 | browser_context->SetUserData(kServiceUserId, |
Jeremy Roman | 04f27c37 | 2017-10-27 15:20:55 | [diff] [blame] | 501 | std::make_unique<ServiceUserIdHolder>(new_id)); |
erg | e69130f5 | 2016-03-02 00:13:28 | [diff] [blame] | 502 | |
avi | 1ed4a437 | 2017-04-25 05:39:41 | [diff] [blame] | 503 | browser_context->SetUserData( |
Jeremy Roman | 04f27c37 | 2017-10-27 15:20:55 | [diff] [blame] | 504 | kMojoWasInitialized, std::make_unique<base::SupportsUserData::Data>()); |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 505 | |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 506 | ServiceManagerConnection* service_manager_connection = |
| 507 | ServiceManagerConnection::GetForProcess(); |
fdoray | 4f155f0 | 2016-10-12 11:28:50 | [diff] [blame] | 508 | if (service_manager_connection && base::ThreadTaskRunnerHandle::IsSet()) { |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 509 | // NOTE: Many unit tests create a TestBrowserContext without initializing |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 510 | // Mojo or the global service manager connection. |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 511 | |
rockot | 400ea35b | 2016-10-15 19:15:32 | [diff] [blame] | 512 | service_manager::mojom::ServicePtr service; |
Ken Rockot | f4d8a94 | 2017-05-13 00:10:37 | [diff] [blame] | 513 | auto service_request = mojo::MakeRequest(&service); |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 514 | |
rockot | 400ea35b | 2016-10-15 19:15:32 | [diff] [blame] | 515 | service_manager::mojom::PIDReceiverPtr pid_receiver; |
ben | e6a9f01 | 2017-01-07 00:43:43 | [diff] [blame] | 516 | service_manager::Identity identity(mojom::kBrowserServiceName, new_id); |
ben | bd3c248 | 2017-01-07 05:48:21 | [diff] [blame] | 517 | service_manager_connection->GetConnector()->StartService( |
ben | e6a9f01 | 2017-01-07 00:43:43 | [diff] [blame] | 518 | identity, std::move(service), mojo::MakeRequest(&pid_receiver)); |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 519 | pid_receiver->SetPID(base::GetCurrentProcId()); |
| 520 | |
ben | aad37ecd | 2017-04-12 22:08:20 | [diff] [blame] | 521 | service_manager_connection->GetConnector()->StartService(identity); |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 522 | BrowserContextServiceManagerConnectionHolder* connection_holder = |
| 523 | new BrowserContextServiceManagerConnectionHolder( |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 524 | std::move(service_request)); |
avi | 1ed4a437 | 2017-04-25 05:39:41 | [diff] [blame] | 525 | browser_context->SetUserData(kServiceManagerConnection, |
| 526 | base::WrapUnique(connection_holder)); |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 527 | |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 528 | ServiceManagerConnection* connection = |
| 529 | connection_holder->service_manager_connection(); |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 530 | |
ben | 146248de | 2016-06-14 15:24:59 | [diff] [blame] | 531 | // New embedded service factories should be added to |connection| here. |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 532 | |
Ken Rockot | 84f58fd | 2018-06-22 21:54:14 | [diff] [blame^] | 533 | { |
| 534 | service_manager::EmbeddedServiceInfo info; |
| 535 | info.factory = base::BindRepeating(&file::CreateFileService); |
| 536 | connection->AddEmbeddedService(file::mojom::kServiceName, info); |
| 537 | } |
| 538 | |
| 539 | browser_context->SetUserData( |
| 540 | kContentServiceDelegateKey, |
| 541 | std::make_unique<ContentServiceDelegateHolder>(browser_context)); |
| 542 | |
| 543 | { |
| 544 | service_manager::EmbeddedServiceInfo info; |
| 545 | info.task_runner = base::SequencedTaskRunnerHandle::Get(); |
| 546 | info.factory = base::BindRepeating( |
| 547 | [](BrowserContext* context) |
| 548 | -> std::unique_ptr<service_manager::Service> { |
| 549 | auto* holder = static_cast<ContentServiceDelegateHolder*>( |
| 550 | context->GetUserData(kContentServiceDelegateKey)); |
| 551 | auto* delegate = holder->delegate(); |
| 552 | auto service = std::make_unique<content::Service>(delegate); |
| 553 | delegate->AddService(service.get()); |
| 554 | return service; |
| 555 | }, |
| 556 | browser_context); |
| 557 | connection->AddEmbeddedService(content::mojom::kServiceName, info); |
| 558 | } |
tibell | ab3d36b | 2017-03-10 02:57:25 | [diff] [blame] | 559 | |
| 560 | ContentBrowserClient::StaticServiceMap services; |
| 561 | browser_context->RegisterInProcessServices(&services); |
| 562 | for (const auto& entry : services) { |
| 563 | connection->AddEmbeddedService(entry.first, entry.second); |
| 564 | } |
Ken Rockot | c7a279c | 2017-05-04 23:51:45 | [diff] [blame] | 565 | |
| 566 | RegisterCommonBrowserInterfaces(connection); |
sammc | 030f501 | 2017-03-13 02:28:45 | [diff] [blame] | 567 | connection->Start(); |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 568 | } |
Elad Alon | 03ad23b | 2018-02-07 16:54:31 | [diff] [blame] | 569 | |
Elad Alon | 03ad23b | 2018-02-07 16:54:31 | [diff] [blame] | 570 | if (!browser_context->IsOffTheRecord()) { |
Elad Alon | 12ecdf0 | 2018-03-14 12:07:31 | [diff] [blame] | 571 | WebRtcEventLogger* const logger = WebRtcEventLogger::Get(); |
| 572 | if (logger) { |
| 573 | logger->EnableForBrowserContext(browser_context); |
Elad Alon | 03ad23b | 2018-02-07 16:54:31 | [diff] [blame] | 574 | } |
| 575 | } |
erg | e69130f5 | 2016-03-02 00:13:28 | [diff] [blame] | 576 | } |
| 577 | |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 578 | // static |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 579 | const std::string& BrowserContext::GetServiceUserIdFor( |
ben | cccfe2a | 2016-03-05 16:54:14 | [diff] [blame] | 580 | BrowserContext* browser_context) { |
erg | e69130f5 | 2016-03-02 00:13:28 | [diff] [blame] | 581 | CHECK(browser_context->GetUserData(kMojoWasInitialized)) |
| 582 | << "Attempting to get the mojo user id for a BrowserContext that was " |
| 583 | << "never Initialize()ed."; |
| 584 | |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 585 | ServiceUserIdHolder* holder = static_cast<ServiceUserIdHolder*>( |
| 586 | browser_context->GetUserData(kServiceUserId)); |
ben | 6c85c449 | 2016-06-16 20:40:51 | [diff] [blame] | 587 | return holder->user_id(); |
erg | e69130f5 | 2016-03-02 00:13:28 | [diff] [blame] | 588 | } |
| 589 | |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 590 | // static |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 591 | BrowserContext* BrowserContext::GetBrowserContextForServiceUserId( |
ben | 6c85c449 | 2016-06-16 20:40:51 | [diff] [blame] | 592 | const std::string& user_id) { |
| 593 | auto it = g_user_id_to_context.Get().find(user_id); |
| 594 | return it != g_user_id_to_context.Get().end() ? it->second : nullptr; |
| 595 | } |
| 596 | |
| 597 | // static |
rockot | 400ea35b | 2016-10-15 19:15:32 | [diff] [blame] | 598 | service_manager::Connector* BrowserContext::GetConnectorFor( |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 599 | BrowserContext* browser_context) { |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 600 | ServiceManagerConnection* connection = |
| 601 | GetServiceManagerConnectionFor(browser_context); |
ben | 5be0b913 | 2016-08-03 00:17:18 | [diff] [blame] | 602 | return connection ? connection->GetConnector() : nullptr; |
| 603 | } |
| 604 | |
| 605 | // static |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 606 | ServiceManagerConnection* BrowserContext::GetServiceManagerConnectionFor( |
ben | 5be0b913 | 2016-08-03 00:17:18 | [diff] [blame] | 607 | BrowserContext* browser_context) { |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 608 | BrowserContextServiceManagerConnectionHolder* connection_holder = |
| 609 | static_cast<BrowserContextServiceManagerConnectionHolder*>( |
| 610 | browser_context->GetUserData(kServiceManagerConnection)); |
| 611 | return connection_holder ? connection_holder->service_manager_connection() |
| 612 | : nullptr; |
rockot | 963ad3e8 | 2016-04-13 16:33:23 | [diff] [blame] | 613 | } |
| 614 | |
mmenke | c0b2b8b1 | 2017-04-21 16:27:52 | [diff] [blame] | 615 | BrowserContext::BrowserContext() |
Andrey Lushnikov | d39d906 | 2018-04-24 17:33:35 | [diff] [blame] | 616 | : unique_id_(base::UnguessableToken::Create().ToString()) {} |
mmenke | c0b2b8b1 | 2017-04-21 16:27:52 | [diff] [blame] | 617 | |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 618 | BrowserContext::~BrowserContext() { |
erg | e69130f5 | 2016-03-02 00:13:28 | [diff] [blame] | 619 | CHECK(GetUserData(kMojoWasInitialized)) |
| 620 | << "Attempting to destroy a BrowserContext that never called " |
| 621 | << "Initialize()"; |
| 622 | |
kinuko | f6ed359c | 2016-07-26 13:27:21 | [diff] [blame] | 623 | DCHECK(!GetUserData(kStoragePartitionMapKeyName)) |
| 624 | << "StoragePartitionMap is not shut down properly"; |
| 625 | |
Lukasz Anforowicz | 58d0dac | 2018-03-23 15:48:10 | [diff] [blame] | 626 | DCHECK(was_notify_will_be_destroyed_called_); |
| 627 | |
Elad Alon | 12ecdf0 | 2018-03-14 12:07:31 | [diff] [blame] | 628 | WebRtcEventLogger* const logger = WebRtcEventLogger::Get(); |
| 629 | if (logger) { |
| 630 | logger->DisableForBrowserContext(this); |
Elad Alon | 03ad23b | 2018-02-07 16:54:31 | [diff] [blame] | 631 | } |
Elad Alon | 03ad23b | 2018-02-07 16:54:31 | [diff] [blame] | 632 | |
ben | 6c85c449 | 2016-06-16 20:40:51 | [diff] [blame] | 633 | RemoveBrowserContextFromUserIdMap(this); |
| 634 | |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 635 | if (GetUserData(kDownloadManagerKeyName)) |
| 636 | GetDownloadManager(this)->Shutdown(); |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 637 | } |
| 638 | |
kinuko | f6ed359c | 2016-07-26 13:27:21 | [diff] [blame] | 639 | void BrowserContext::ShutdownStoragePartitions() { |
| 640 | if (GetUserData(kStoragePartitionMapKeyName)) |
| 641 | RemoveUserData(kStoragePartitionMapKeyName); |
| 642 | } |
| 643 | |
mmenke | c0b2b8b1 | 2017-04-21 16:27:52 | [diff] [blame] | 644 | std::string BrowserContext::GetMediaDeviceIDSalt() { |
Andrey Lushnikov | d39d906 | 2018-04-24 17:33:35 | [diff] [blame] | 645 | return unique_id_; |
mmenke | c0b2b8b1 | 2017-04-21 16:27:52 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | // static |
| 649 | std::string BrowserContext::CreateRandomMediaDeviceIDSalt() { |
Andrey Lushnikov | d39d906 | 2018-04-24 17:33:35 | [diff] [blame] | 650 | return base::UnguessableToken::Create().ToString(); |
| 651 | } |
| 652 | |
| 653 | const std::string& BrowserContext::UniqueId() const { |
| 654 | return unique_id_; |
mmenke | c0b2b8b1 | 2017-04-21 16:27:52 | [diff] [blame] | 655 | } |
| 656 | |
Chris Cunningham | a5b3801 | 2017-10-28 07:27:18 | [diff] [blame] | 657 | media::VideoDecodePerfHistory* BrowserContext::GetVideoDecodePerfHistory() { |
| 658 | media::VideoDecodePerfHistory* decode_history = |
| 659 | static_cast<media::VideoDecodePerfHistory*>( |
| 660 | GetUserData(kVideoDecodePerfHistoryId)); |
| 661 | |
| 662 | // Lazily created. Note, this does not trigger loading the DB from disk. That |
| 663 | // occurs later upon first VideoDecodePerfHistory API request that requires DB |
| 664 | // access. DB operations will not block the UI thread. |
| 665 | if (!decode_history) { |
| 666 | auto db_factory = std::make_unique<media::VideoDecodeStatsDBImplFactory>( |
| 667 | GetPath().Append(FILE_PATH_LITERAL("VideoDecodeStats"))); |
| 668 | decode_history = new media::VideoDecodePerfHistory(std::move(db_factory)); |
| 669 | SetUserData(kVideoDecodePerfHistoryId, base::WrapUnique(decode_history)); |
| 670 | } |
| 671 | |
| 672 | return decode_history; |
| 673 | } |
| 674 | |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 675 | } // namespace content |