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