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