[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> |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 11 | #include <utility> |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 12 | |
ben | cccfe2a | 2016-03-05 16:54:14 | [diff] [blame^] | 13 | #include "base/guid.h" |
erg | e69130f5 | 2016-03-02 00:13:28 | [diff] [blame] | 14 | #include "base/lazy_instance.h" |
| 15 | #include "base/rand_util.h" |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 16 | #include "build/build_config.h" |
| 17 | |
[email protected] | e0ce8a1e | 2012-09-18 10:26:36 | [diff] [blame] | 18 | #if !defined(OS_IOS) |
erg | e69130f5 | 2016-03-02 00:13:28 | [diff] [blame] | 19 | #include "components/profile_service/profile_app.h" |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 20 | #include "content/browser/download/download_manager_impl.h" |
[email protected] | 393b6cb | 2014-05-15 00:55:12 | [diff] [blame] | 21 | #include "content/browser/fileapi/chrome_blob_storage_context.h" |
[email protected] | c4d28166 | 2013-03-31 00:35:08 | [diff] [blame] | 22 | #include "content/browser/indexed_db/indexed_db_context_impl.h" |
[email protected] | 678c036 | 2012-12-05 08:02:44 | [diff] [blame] | 23 | #include "content/browser/loader/resource_dispatcher_host_impl.h" |
mvanouwerkerk | 17205ea | 2014-11-07 17:30:15 | [diff] [blame] | 24 | #include "content/browser/push_messaging/push_messaging_router.h" |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 25 | #include "content/browser/storage_partition_impl_map.h" |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 26 | #include "content/common/child_process_host_impl.h" |
[email protected] | 393b6cb | 2014-05-15 00:55:12 | [diff] [blame] | 27 | #include "content/public/browser/blob_handle.h" |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 28 | #include "content/public/browser/browser_thread.h" |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 29 | #include "content/public/browser/content_browser_client.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 30 | #include "content/public/browser/site_instance.h" |
[email protected] | 4d7c4ef | 2012-03-16 01:47:12 | [diff] [blame] | 31 | #include "net/cookies/cookie_store.h" |
[email protected] | 6b8a3c74 | 2014-07-25 00:25:35 | [diff] [blame] | 32 | #include "net/ssl/channel_id_service.h" |
| 33 | #include "net/ssl/channel_id_store.h" |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 34 | #include "net/url_request/url_request_context.h" |
[email protected] | 6939075a | 2012-08-28 08:35:53 | [diff] [blame] | 35 | #include "net/url_request/url_request_context_getter.h" |
pilgrim | e92c5fcd | 2014-09-10 23:31:23 | [diff] [blame] | 36 | #include "storage/browser/database/database_tracker.h" |
| 37 | #include "storage/browser/fileapi/external_mount_points.h" |
[email protected] | e0ce8a1e | 2012-09-18 10:26:36 | [diff] [blame] | 38 | #endif // !OS_IOS |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 39 | |
[email protected] | 314c3e2 | 2012-02-21 03:57:42 | [diff] [blame] | 40 | using base::UserDataAdapter; |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 41 | |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 42 | namespace content { |
| 43 | |
[email protected] | e0ce8a1e | 2012-09-18 10:26:36 | [diff] [blame] | 44 | // Only ~BrowserContext() is needed on iOS. |
| 45 | #if !defined(OS_IOS) |
[email protected] | 735e20c | 2012-03-20 01:16:59 | [diff] [blame] | 46 | namespace { |
| 47 | |
ben | cccfe2a | 2016-03-05 16:54:14 | [diff] [blame^] | 48 | base::LazyInstance<std::set<std::string>> g_used_user_ids = |
erg | e69130f5 | 2016-03-02 00:13:28 | [diff] [blame] | 49 | LAZY_INSTANCE_INITIALIZER; |
ben | cccfe2a | 2016-03-05 16:54:14 | [diff] [blame^] | 50 | base::LazyInstance<std::vector<std::pair<BrowserContext*, std::string>>> |
erg | e69130f5 | 2016-03-02 00:13:28 | [diff] [blame] | 51 | g_context_to_user_id = LAZY_INSTANCE_INITIALIZER; |
| 52 | |
[email protected] | e0ce8a1e | 2012-09-18 10:26:36 | [diff] [blame] | 53 | // Key names on BrowserContext. |
[email protected] | 6ef0c391 | 2013-01-25 22:46:34 | [diff] [blame] | 54 | const char kDownloadManagerKeyName[] = "download_manager"; |
a.cavalcanti | ffab7376 | 2015-08-15 02:55:48 | [diff] [blame] | 55 | const char kStoragePartitionMapKeyName[] = "content_storage_partition_map"; |
[email protected] | e0ce8a1e | 2012-09-18 10:26:36 | [diff] [blame] | 56 | |
erg | e69130f5 | 2016-03-02 00:13:28 | [diff] [blame] | 57 | const char kMojoWasInitialized[] = "mojo-was-initialized"; |
| 58 | |
[email protected] | 9afc14e2 | 2013-09-25 22:34:14 | [diff] [blame] | 59 | #if defined(OS_CHROMEOS) |
| 60 | const char kMountPointsKey[] = "mount_points"; |
| 61 | #endif // defined(OS_CHROMEOS) |
| 62 | |
[email protected] | 14acc64 | 2012-11-17 12:20:10 | [diff] [blame] | 63 | StoragePartitionImplMap* GetStoragePartitionMap( |
| 64 | BrowserContext* browser_context) { |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 65 | StoragePartitionImplMap* partition_map = |
| 66 | static_cast<StoragePartitionImplMap*>( |
a.cavalcanti | ffab7376 | 2015-08-15 02:55:48 | [diff] [blame] | 67 | browser_context->GetUserData(kStoragePartitionMapKeyName)); |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 68 | if (!partition_map) { |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 69 | partition_map = new StoragePartitionImplMap(browser_context); |
a.cavalcanti | ffab7376 | 2015-08-15 02:55:48 | [diff] [blame] | 70 | browser_context->SetUserData(kStoragePartitionMapKeyName, partition_map); |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 71 | } |
[email protected] | 14acc64 | 2012-11-17 12:20:10 | [diff] [blame] | 72 | return partition_map; |
| 73 | } |
| 74 | |
| 75 | StoragePartition* GetStoragePartitionFromConfig( |
| 76 | BrowserContext* browser_context, |
| 77 | const std::string& partition_domain, |
| 78 | const std::string& partition_name, |
| 79 | bool in_memory) { |
| 80 | StoragePartitionImplMap* partition_map = |
| 81 | GetStoragePartitionMap(browser_context); |
[email protected] | d7c7c98a | 2012-07-12 21:27:44 | [diff] [blame] | 82 | |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 83 | if (browser_context->IsOffTheRecord()) |
| 84 | in_memory = true; |
| 85 | |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 86 | return partition_map->Get(partition_domain, partition_name, in_memory); |
[email protected] | d1198fd | 2012-08-13 22:50:19 | [diff] [blame] | 87 | } |
| 88 | |
[email protected] | 6939075a | 2012-08-28 08:35:53 | [diff] [blame] | 89 | void SaveSessionStateOnIOThread( |
| 90 | const scoped_refptr<net::URLRequestContextGetter>& context_getter, |
[email protected] | 98d6d456 | 2014-06-25 20:57:55 | [diff] [blame] | 91 | AppCacheServiceImpl* appcache_service) { |
[email protected] | 6939075a | 2012-08-28 08:35:53 | [diff] [blame] | 92 | net::URLRequestContext* context = context_getter->GetURLRequestContext(); |
mmenke | ded79da | 2016-02-06 08:28:51 | [diff] [blame] | 93 | context->cookie_store()->SetForceKeepSessionState(); |
[email protected] | 6b8a3c74 | 2014-07-25 00:25:35 | [diff] [blame] | 94 | context->channel_id_service()->GetChannelIDStore()-> |
[email protected] | 6939075a | 2012-08-28 08:35:53 | [diff] [blame] | 95 | SetForceKeepSessionState(); |
| 96 | appcache_service->set_force_keep_session_state(); |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 97 | } |
| 98 | |
[email protected] | 89acda8 | 2013-06-25 20:52:50 | [diff] [blame] | 99 | void SaveSessionStateOnIndexedDBThread( |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 100 | scoped_refptr<IndexedDBContextImpl> indexed_db_context) { |
[email protected] | bf510ed | 2012-06-05 08:31:43 | [diff] [blame] | 101 | indexed_db_context->SetForceKeepSessionState(); |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 102 | } |
| 103 | |
falken | 41f417516 | 2014-10-29 07:03:41 | [diff] [blame] | 104 | void ShutdownServiceWorkerContext(StoragePartition* partition) { |
| 105 | ServiceWorkerContextWrapper* wrapper = |
| 106 | static_cast<ServiceWorkerContextWrapper*>( |
| 107 | partition->GetServiceWorkerContext()); |
| 108 | wrapper->process_manager()->Shutdown(); |
| 109 | } |
| 110 | |
ttr314 | 81dc54b | 2015-08-06 20:11:26 | [diff] [blame] | 111 | void SetDownloadManager(BrowserContext* context, |
| 112 | content::DownloadManager* download_manager) { |
| 113 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 114 | DCHECK(download_manager); |
| 115 | context->SetUserData(kDownloadManagerKeyName, download_manager); |
| 116 | } |
| 117 | |
[email protected] | 735e20c | 2012-03-20 01:16:59 | [diff] [blame] | 118 | } // namespace |
| 119 | |
[email protected] | 14acc64 | 2012-11-17 12:20:10 | [diff] [blame] | 120 | // static |
| 121 | void BrowserContext::AsyncObliterateStoragePartition( |
| 122 | BrowserContext* browser_context, |
[email protected] | 399583b | 2012-12-11 09:33:42 | [diff] [blame] | 123 | const GURL& site, |
| 124 | const base::Closure& on_gc_required) { |
| 125 | GetStoragePartitionMap(browser_context)->AsyncObliterate(site, |
| 126 | on_gc_required); |
| 127 | } |
| 128 | |
| 129 | // static |
| 130 | void BrowserContext::GarbageCollectStoragePartitions( |
| 131 | BrowserContext* browser_context, |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 132 | scoped_ptr<base::hash_set<base::FilePath> > active_paths, |
[email protected] | 399583b | 2012-12-11 09:33:42 | [diff] [blame] | 133 | const base::Closure& done) { |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 134 | GetStoragePartitionMap(browser_context) |
| 135 | ->GarbageCollect(std::move(active_paths), done); |
[email protected] | 14acc64 | 2012-11-17 12:20:10 | [diff] [blame] | 136 | } |
| 137 | |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 138 | DownloadManager* BrowserContext::GetDownloadManager( |
| 139 | BrowserContext* context) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 140 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 141 | if (!context->GetUserData(kDownloadManagerKeyName)) { |
[email protected] | eba4a4d | 2013-05-29 02:18:06 | [diff] [blame] | 142 | DownloadManager* download_manager = |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 143 | new DownloadManagerImpl( |
[email protected] | 1679869 | 2013-04-23 18:08:38 | [diff] [blame] | 144 | GetContentClient()->browser()->GetNetLog(), context); |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 145 | |
ttr314 | 81dc54b | 2015-08-06 20:11:26 | [diff] [blame] | 146 | SetDownloadManager(context, download_manager); |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 147 | download_manager->SetDelegate(context->GetDownloadManagerDelegate()); |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 148 | } |
| 149 | |
[email protected] | eba4a4d | 2013-05-29 02:18:06 | [diff] [blame] | 150 | return static_cast<DownloadManager*>( |
| 151 | context->GetUserData(kDownloadManagerKeyName)); |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 152 | } |
| 153 | |
[email protected] | 6ef0c391 | 2013-01-25 22:46:34 | [diff] [blame] | 154 | // static |
[email protected] | cd501a7 | 2014-08-22 19:58:31 | [diff] [blame] | 155 | storage::ExternalMountPoints* BrowserContext::GetMountPoints( |
[email protected] | 6ef0c391 | 2013-01-25 22:46:34 | [diff] [blame] | 156 | BrowserContext* context) { |
| 157 | // Ensure that these methods are called on the UI thread, except for |
| 158 | // unittests where a UI thread might not have been created. |
| 159 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || |
| 160 | !BrowserThread::IsMessageLoopValid(BrowserThread::UI)); |
| 161 | |
| 162 | #if defined(OS_CHROMEOS) |
| 163 | if (!context->GetUserData(kMountPointsKey)) { |
[email protected] | cd501a7 | 2014-08-22 19:58:31 | [diff] [blame] | 164 | scoped_refptr<storage::ExternalMountPoints> mount_points = |
| 165 | storage::ExternalMountPoints::CreateRefCounted(); |
[email protected] | 6ef0c391 | 2013-01-25 22:46:34 | [diff] [blame] | 166 | context->SetUserData( |
| 167 | kMountPointsKey, |
[email protected] | cd501a7 | 2014-08-22 19:58:31 | [diff] [blame] | 168 | new UserDataAdapter<storage::ExternalMountPoints>(mount_points.get())); |
[email protected] | 6ef0c391 | 2013-01-25 22:46:34 | [diff] [blame] | 169 | } |
| 170 | |
[email protected] | cd501a7 | 2014-08-22 19:58:31 | [diff] [blame] | 171 | return UserDataAdapter<storage::ExternalMountPoints>::Get(context, |
| 172 | kMountPointsKey); |
[email protected] | 6ef0c391 | 2013-01-25 22:46:34 | [diff] [blame] | 173 | #else |
| 174 | return NULL; |
| 175 | #endif |
| 176 | } |
| 177 | |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 178 | StoragePartition* BrowserContext::GetStoragePartition( |
| 179 | BrowserContext* browser_context, |
| 180 | SiteInstance* site_instance) { |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 181 | std::string partition_domain; |
| 182 | std::string partition_name; |
| 183 | bool in_memory = false; |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 184 | |
| 185 | // TODO(ajwong): After GetDefaultStoragePartition() is removed, get rid of |
| 186 | // this conditional and require that |site_instance| is non-NULL. |
| 187 | if (site_instance) { |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 188 | GetContentClient()->browser()->GetStoragePartitionConfigForSite( |
[email protected] | 14acc64 | 2012-11-17 12:20:10 | [diff] [blame] | 189 | browser_context, site_instance->GetSiteURL(), true, |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 190 | &partition_domain, &partition_name, &in_memory); |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 191 | } |
| 192 | |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 193 | return GetStoragePartitionFromConfig( |
| 194 | browser_context, partition_domain, partition_name, in_memory); |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 195 | } |
| 196 | |
[email protected] | e94bbcb | 2012-09-07 05:33:57 | [diff] [blame] | 197 | StoragePartition* BrowserContext::GetStoragePartitionForSite( |
| 198 | BrowserContext* browser_context, |
| 199 | const GURL& site) { |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 200 | std::string partition_domain; |
| 201 | std::string partition_name; |
| 202 | bool in_memory; |
[email protected] | e94bbcb | 2012-09-07 05:33:57 | [diff] [blame] | 203 | |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 204 | GetContentClient()->browser()->GetStoragePartitionConfigForSite( |
[email protected] | 14acc64 | 2012-11-17 12:20:10 | [diff] [blame] | 205 | browser_context, site, true, &partition_domain, &partition_name, |
| 206 | &in_memory); |
[email protected] | 1bc2831 | 2012-11-08 08:31:53 | [diff] [blame] | 207 | |
| 208 | return GetStoragePartitionFromConfig( |
| 209 | browser_context, partition_domain, partition_name, in_memory); |
[email protected] | e94bbcb | 2012-09-07 05:33:57 | [diff] [blame] | 210 | } |
| 211 | |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 212 | void BrowserContext::ForEachStoragePartition( |
| 213 | BrowserContext* browser_context, |
| 214 | const StoragePartitionCallback& callback) { |
| 215 | StoragePartitionImplMap* partition_map = |
| 216 | static_cast<StoragePartitionImplMap*>( |
a.cavalcanti | ffab7376 | 2015-08-15 02:55:48 | [diff] [blame] | 217 | browser_context->GetUserData(kStoragePartitionMapKeyName)); |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 218 | if (!partition_map) |
| 219 | return; |
| 220 | |
| 221 | partition_map->ForEach(callback); |
| 222 | } |
| 223 | |
| 224 | StoragePartition* BrowserContext::GetDefaultStoragePartition( |
| 225 | BrowserContext* browser_context) { |
| 226 | return GetStoragePartition(browser_context, NULL); |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 227 | } |
| 228 | |
tbarzic | db71268 | 2015-03-06 06:05:41 | [diff] [blame] | 229 | // static |
[email protected] | 393b6cb | 2014-05-15 00:55:12 | [diff] [blame] | 230 | void BrowserContext::CreateMemoryBackedBlob(BrowserContext* browser_context, |
| 231 | const char* data, size_t length, |
| 232 | const BlobCallback& callback) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 233 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 393b6cb | 2014-05-15 00:55:12 | [diff] [blame] | 234 | |
| 235 | ChromeBlobStorageContext* blob_context = |
| 236 | ChromeBlobStorageContext::GetFor(browser_context); |
| 237 | BrowserThread::PostTaskAndReplyWithResult( |
| 238 | BrowserThread::IO, FROM_HERE, |
| 239 | base::Bind(&ChromeBlobStorageContext::CreateMemoryBackedBlob, |
| 240 | make_scoped_refptr(blob_context), data, length), |
| 241 | callback); |
| 242 | } |
| 243 | |
[email protected] | 66e53d028 | 2014-08-07 10:04:35 | [diff] [blame] | 244 | // static |
tbarzic | db71268 | 2015-03-06 06:05:41 | [diff] [blame] | 245 | void BrowserContext::CreateFileBackedBlob( |
| 246 | BrowserContext* browser_context, |
| 247 | const base::FilePath& path, |
| 248 | int64_t offset, |
| 249 | int64_t size, |
| 250 | const base::Time& expected_modification_time, |
| 251 | const BlobCallback& callback) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 252 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
tbarzic | db71268 | 2015-03-06 06:05:41 | [diff] [blame] | 253 | |
| 254 | ChromeBlobStorageContext* blob_context = |
| 255 | ChromeBlobStorageContext::GetFor(browser_context); |
| 256 | BrowserThread::PostTaskAndReplyWithResult( |
| 257 | BrowserThread::IO, FROM_HERE, |
| 258 | base::Bind(&ChromeBlobStorageContext::CreateFileBackedBlob, |
| 259 | make_scoped_refptr(blob_context), path, offset, size, |
| 260 | expected_modification_time), |
| 261 | callback); |
| 262 | } |
| 263 | |
| 264 | // static |
[email protected] | 66e53d028 | 2014-08-07 10:04:35 | [diff] [blame] | 265 | void BrowserContext::DeliverPushMessage( |
| 266 | BrowserContext* browser_context, |
| 267 | const GURL& origin, |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 268 | int64_t service_worker_registration_id, |
harkness | dd4d2b2 | 2016-01-27 19:26:43 | [diff] [blame] | 269 | const PushEventPayload& payload, |
johnme | a80c255 | 2014-10-17 14:51:40 | [diff] [blame] | 270 | const base::Callback<void(PushDeliveryStatus)>& callback) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 271 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
harkness | dd4d2b2 | 2016-01-27 19:26:43 | [diff] [blame] | 272 | PushMessagingRouter::DeliverMessage(browser_context, origin, |
| 273 | service_worker_registration_id, payload, |
| 274 | callback); |
[email protected] | 66e53d028 | 2014-08-07 10:04:35 | [diff] [blame] | 275 | } |
| 276 | |
falken | 41f417516 | 2014-10-29 07:03:41 | [diff] [blame] | 277 | // static |
| 278 | void BrowserContext::NotifyWillBeDestroyed(BrowserContext* browser_context) { |
| 279 | // Service Workers must shutdown before the browser context is destroyed, |
| 280 | // since they keep render process hosts alive and the codebase assumes that |
| 281 | // render process hosts die before their profile (browser context) dies. |
| 282 | ForEachStoragePartition(browser_context, |
| 283 | base::Bind(ShutdownServiceWorkerContext)); |
| 284 | } |
| 285 | |
[email protected] | 314c3e2 | 2012-02-21 03:57:42 | [diff] [blame] | 286 | void BrowserContext::EnsureResourceContextInitialized(BrowserContext* context) { |
[email protected] | 7e26ac9 | 2012-02-27 20:15:05 | [diff] [blame] | 287 | // This will be enough to tickle initialization of BrowserContext if |
| 288 | // necessary, which initializes ResourceContext. The reason we don't call |
[email protected] | 4c3a2358 | 2012-08-18 08:54:34 | [diff] [blame] | 289 | // ResourceContext::InitializeResourceContext() directly here is that |
| 290 | // ResourceContext initialization may call back into BrowserContext |
| 291 | // and when that call returns it'll end rewriting its UserData map. It will |
| 292 | // end up rewriting the same value but this still causes a race condition. |
| 293 | // |
| 294 | // See http://crbug.com/115678. |
| 295 | GetDefaultStoragePartition(context); |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 296 | } |
| 297 | |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 298 | void BrowserContext::SaveSessionState(BrowserContext* browser_context) { |
[email protected] | 5c8e67c | 2012-08-29 00:48:52 | [diff] [blame] | 299 | GetDefaultStoragePartition(browser_context)->GetDatabaseTracker()-> |
| 300 | SetForceKeepSessionState(); |
[email protected] | b1b502e | 2012-09-16 07:31:43 | [diff] [blame] | 301 | StoragePartition* storage_partition = |
| 302 | BrowserContext::GetDefaultStoragePartition(browser_context); |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 303 | |
| 304 | if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) { |
| 305 | BrowserThread::PostTask( |
| 306 | BrowserThread::IO, FROM_HERE, |
[email protected] | 6939075a | 2012-08-28 08:35:53 | [diff] [blame] | 307 | base::Bind( |
| 308 | &SaveSessionStateOnIOThread, |
| 309 | make_scoped_refptr(browser_context->GetRequestContext()), |
[email protected] | 98d6d456 | 2014-06-25 20:57:55 | [diff] [blame] | 310 | static_cast<AppCacheServiceImpl*>( |
[email protected] | 63ef8551 | 2014-06-05 14:21:26 | [diff] [blame] | 311 | storage_partition->GetAppCacheService()))); |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 312 | } |
| 313 | |
[email protected] | 5f2aa72 | 2013-08-07 16:59:41 | [diff] [blame] | 314 | DOMStorageContextWrapper* dom_storage_context_proxy = |
| 315 | static_cast<DOMStorageContextWrapper*>( |
[email protected] | b1b502e | 2012-09-16 07:31:43 | [diff] [blame] | 316 | storage_partition->GetDOMStorageContext()); |
[email protected] | 5f2aa72 | 2013-08-07 16:59:41 | [diff] [blame] | 317 | dom_storage_context_proxy->SetForceKeepSessionState(); |
[email protected] | 735e20c | 2012-03-20 01:16:59 | [diff] [blame] | 318 | |
[email protected] | 89acda8 | 2013-06-25 20:52:50 | [diff] [blame] | 319 | IndexedDBContextImpl* indexed_db_context_impl = |
| 320 | static_cast<IndexedDBContextImpl*>( |
[email protected] | b1b502e | 2012-09-16 07:31:43 | [diff] [blame] | 321 | storage_partition->GetIndexedDBContext()); |
[email protected] | 89acda8 | 2013-06-25 20:52:50 | [diff] [blame] | 322 | // No task runner in unit tests. |
| 323 | if (indexed_db_context_impl->TaskRunner()) { |
| 324 | indexed_db_context_impl->TaskRunner()->PostTask( |
| 325 | FROM_HERE, |
| 326 | base::Bind(&SaveSessionStateOnIndexedDBThread, |
| 327 | make_scoped_refptr(indexed_db_context_impl))); |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | |
ttr314 | 81dc54b | 2015-08-06 20:11:26 | [diff] [blame] | 331 | void BrowserContext::SetDownloadManagerForTesting( |
| 332 | BrowserContext* browser_context, |
| 333 | DownloadManager* download_manager) { |
| 334 | SetDownloadManager(browser_context, download_manager); |
| 335 | } |
| 336 | |
erg | e69130f5 | 2016-03-02 00:13:28 | [diff] [blame] | 337 | void BrowserContext::Initialize( |
| 338 | BrowserContext* browser_context, |
| 339 | const base::FilePath& path) { |
ben | cccfe2a | 2016-03-05 16:54:14 | [diff] [blame^] | 340 | // Generate a GUID for |browser_context| to use as the Mojo user id. |
| 341 | std::string new_id = base::GenerateGUID(); |
| 342 | while (g_used_user_ids.Get().find(new_id) != g_used_user_ids.Get().end()) |
| 343 | new_id = base::GenerateGUID(); |
erg | e69130f5 | 2016-03-02 00:13:28 | [diff] [blame] | 344 | |
| 345 | g_used_user_ids.Get().insert(new_id); |
| 346 | g_context_to_user_id.Get().push_back(std::make_pair(browser_context, new_id)); |
| 347 | |
| 348 | profile::ProfileApp::AssociateMojoUserIDWithProfileDir(new_id, path); |
| 349 | browser_context->SetUserData(kMojoWasInitialized, |
| 350 | new base::SupportsUserData::Data); |
| 351 | } |
| 352 | |
ben | cccfe2a | 2016-03-05 16:54:14 | [diff] [blame^] | 353 | const std::string& BrowserContext::GetMojoUserIdFor( |
| 354 | BrowserContext* browser_context) { |
erg | e69130f5 | 2016-03-02 00:13:28 | [diff] [blame] | 355 | CHECK(browser_context->GetUserData(kMojoWasInitialized)) |
| 356 | << "Attempting to get the mojo user id for a BrowserContext that was " |
| 357 | << "never Initialize()ed."; |
| 358 | |
| 359 | auto it = std::find_if( |
| 360 | g_context_to_user_id.Get().begin(), |
| 361 | g_context_to_user_id.Get().end(), |
ben | cccfe2a | 2016-03-05 16:54:14 | [diff] [blame^] | 362 | [&browser_context](const std::pair<BrowserContext*, std::string>& p) { |
erg | e69130f5 | 2016-03-02 00:13:28 | [diff] [blame] | 363 | return p.first == browser_context; }); |
| 364 | CHECK(it != g_context_to_user_id.Get().end()); |
| 365 | return it->second; |
| 366 | } |
| 367 | |
[email protected] | e0ce8a1e | 2012-09-18 10:26:36 | [diff] [blame] | 368 | #endif // !OS_IOS |
[email protected] | 6e2d3d2 | 2012-02-24 18:10:36 | [diff] [blame] | 369 | |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 370 | BrowserContext::~BrowserContext() { |
erg | e69130f5 | 2016-03-02 00:13:28 | [diff] [blame] | 371 | CHECK(GetUserData(kMojoWasInitialized)) |
| 372 | << "Attempting to destroy a BrowserContext that never called " |
| 373 | << "Initialize()"; |
| 374 | |
[email protected] | e0ce8a1e | 2012-09-18 10:26:36 | [diff] [blame] | 375 | #if !defined(OS_IOS) |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 376 | if (GetUserData(kDownloadManagerKeyName)) |
| 377 | GetDownloadManager(this)->Shutdown(); |
[email protected] | e0ce8a1e | 2012-09-18 10:26:36 | [diff] [blame] | 378 | #endif |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 379 | } |
| 380 | |
[email protected] | 55eb70e76 | 2012-02-20 17:38:39 | [diff] [blame] | 381 | } // namespace content |