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