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