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