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