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