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