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