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