| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/indexed_db/indexed_db_internals_ui.h" | 5 #include "content/browser/indexed_db/indexed_db_internals_ui.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/threading/platform_thread.h" | 13 #include "base/threading/platform_thread.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "content/browser/indexed_db/indexed_db_context_impl.h" | 15 #include "content/browser/indexed_db/indexed_db_context_impl.h" |
| 16 #include "content/grit/content_resources.h" | 16 #include "content/grit/content_resources.h" |
| 17 #include "content/public/browser/browser_context.h" | 17 #include "content/public/browser/browser_context.h" |
| 18 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/browser/download_manager.h" | 19 #include "content/public/browser/download_manager.h" |
| 20 #include "content/public/browser/download_url_parameters.h" | 20 #include "content/public/browser/download_url_parameters.h" |
| 21 #include "content/public/browser/storage_partition.h" | 21 #include "content/public/browser/storage_partition.h" |
| 22 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
| 23 #include "content/public/browser/web_ui.h" | 23 #include "content/public/browser/web_ui.h" |
| 24 #include "content/public/browser/web_ui_data_source.h" | 24 #include "content/public/browser/web_ui_data_source.h" |
| 25 #include "content/public/common/url_constants.h" | 25 #include "content/public/common/url_constants.h" |
| 26 #include "storage/common/database/database_identifier.h" | 26 #include "storage/common/database/database_identifier.h" |
| 27 #include "third_party/zlib/google/zip.h" | 27 #include "third_party/zlib/google/zip.h" |
| 28 #include "ui/base/text/bytes_formatting.h" | 28 #include "ui/base/text/bytes_formatting.h" |
| 29 #include "url/origin.h" |
| 29 | 30 |
| 30 namespace content { | 31 namespace content { |
| 31 | 32 |
| 32 namespace { | 33 namespace { |
| 33 | 34 |
| 34 bool AllowWhitelistedPaths(const std::vector<base::FilePath>& allowed_paths, | 35 bool AllowWhitelistedPaths(const std::vector<base::FilePath>& allowed_paths, |
| 35 const base::FilePath& candidate_path) { | 36 const base::FilePath& candidate_path) { |
| 36 for (const base::FilePath& allowed_path : allowed_paths) { | 37 for (const base::FilePath& allowed_path : allowed_paths) { |
| 37 if (allowed_path.IsParent(candidate_path)) | 38 if (allowed_path.IsParent(candidate_path)) |
| 38 return true; | 39 return true; |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 if (storage_partition->GetPath() == partition_path) { | 131 if (storage_partition->GetPath() == partition_path) { |
| 131 *result_partition = storage_partition; | 132 *result_partition = storage_partition; |
| 132 *result_context = static_cast<IndexedDBContextImpl*>( | 133 *result_context = static_cast<IndexedDBContextImpl*>( |
| 133 storage_partition->GetIndexedDBContext()); | 134 storage_partition->GetIndexedDBContext()); |
| 134 } | 135 } |
| 135 } | 136 } |
| 136 | 137 |
| 137 bool IndexedDBInternalsUI::GetOriginData( | 138 bool IndexedDBInternalsUI::GetOriginData( |
| 138 const base::ListValue* args, | 139 const base::ListValue* args, |
| 139 base::FilePath* partition_path, | 140 base::FilePath* partition_path, |
| 140 GURL* origin_url, | 141 url::Origin* origin, |
| 141 scoped_refptr<IndexedDBContextImpl>* context) { | 142 scoped_refptr<IndexedDBContextImpl>* context) { |
| 142 base::FilePath::StringType path_string; | 143 base::FilePath::StringType path_string; |
| 143 if (!args->GetString(0, &path_string)) | 144 if (!args->GetString(0, &path_string)) |
| 144 return false; | 145 return false; |
| 145 *partition_path = base::FilePath(path_string); | 146 *partition_path = base::FilePath(path_string); |
| 146 | 147 |
| 147 std::string url_string; | 148 std::string url_string; |
| 148 if (!args->GetString(1, &url_string)) | 149 if (!args->GetString(1, &url_string)) |
| 149 return false; | 150 return false; |
| 150 | 151 |
| 151 *origin_url = GURL(url_string); | 152 *origin = url::Origin(GURL(url_string)); |
| 152 | 153 |
| 153 return GetOriginContext(*partition_path, *origin_url, context); | 154 return GetOriginContext(*partition_path, *origin, context); |
| 154 } | 155 } |
| 155 | 156 |
| 156 bool IndexedDBInternalsUI::GetOriginContext( | 157 bool IndexedDBInternalsUI::GetOriginContext( |
| 157 const base::FilePath& path, | 158 const base::FilePath& path, |
| 158 const GURL& origin_url, | 159 const url::Origin& origin, |
| 159 scoped_refptr<IndexedDBContextImpl>* context) { | 160 scoped_refptr<IndexedDBContextImpl>* context) { |
| 160 // search the origins to find the right context | 161 // search the origins to find the right context |
| 161 BrowserContext* browser_context = | 162 BrowserContext* browser_context = |
| 162 web_ui()->GetWebContents()->GetBrowserContext(); | 163 web_ui()->GetWebContents()->GetBrowserContext(); |
| 163 | 164 |
| 164 StoragePartition* result_partition; | 165 StoragePartition* result_partition; |
| 165 BrowserContext::StoragePartitionCallback cb = | 166 BrowserContext::StoragePartitionCallback cb = |
| 166 base::Bind(&FindContext, path, &result_partition, context); | 167 base::Bind(&FindContext, path, &result_partition, context); |
| 167 BrowserContext::ForEachStoragePartition(browser_context, cb); | 168 BrowserContext::ForEachStoragePartition(browser_context, cb); |
| 168 | 169 |
| 169 if (!result_partition || !(context->get())) | 170 if (!result_partition || !(context->get())) |
| 170 return false; | 171 return false; |
| 171 | 172 |
| 172 return true; | 173 return true; |
| 173 } | 174 } |
| 174 | 175 |
| 175 void IndexedDBInternalsUI::DownloadOriginData(const base::ListValue* args) { | 176 void IndexedDBInternalsUI::DownloadOriginData(const base::ListValue* args) { |
| 176 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 177 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 177 | 178 |
| 178 base::FilePath partition_path; | 179 base::FilePath partition_path; |
| 179 GURL origin_url; | 180 url::Origin origin; |
| 180 scoped_refptr<IndexedDBContextImpl> context; | 181 scoped_refptr<IndexedDBContextImpl> context; |
| 181 if (!GetOriginData(args, &partition_path, &origin_url, &context)) | 182 if (!GetOriginData(args, &partition_path, &origin, &context)) |
| 182 return; | 183 return; |
| 183 | 184 |
| 184 DCHECK(context.get()); | 185 DCHECK(context.get()); |
| 185 context->TaskRunner()->PostTask( | 186 context->TaskRunner()->PostTask( |
| 186 FROM_HERE, | 187 FROM_HERE, |
| 187 base::Bind(&IndexedDBInternalsUI::DownloadOriginDataOnIndexedDBThread, | 188 base::Bind(&IndexedDBInternalsUI::DownloadOriginDataOnIndexedDBThread, |
| 188 base::Unretained(this), | 189 base::Unretained(this), partition_path, context, origin)); |
| 189 partition_path, | |
| 190 context, | |
| 191 origin_url)); | |
| 192 } | 190 } |
| 193 | 191 |
| 194 void IndexedDBInternalsUI::ForceCloseOrigin(const base::ListValue* args) { | 192 void IndexedDBInternalsUI::ForceCloseOrigin(const base::ListValue* args) { |
| 195 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 193 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 196 | 194 |
| 197 base::FilePath partition_path; | 195 base::FilePath partition_path; |
| 198 GURL origin_url; | 196 url::Origin origin; |
| 199 scoped_refptr<IndexedDBContextImpl> context; | 197 scoped_refptr<IndexedDBContextImpl> context; |
| 200 if (!GetOriginData(args, &partition_path, &origin_url, &context)) | 198 if (!GetOriginData(args, &partition_path, &origin, &context)) |
| 201 return; | 199 return; |
| 202 | 200 |
| 203 context->TaskRunner()->PostTask( | 201 context->TaskRunner()->PostTask( |
| 204 FROM_HERE, | 202 FROM_HERE, |
| 205 base::Bind(&IndexedDBInternalsUI::ForceCloseOriginOnIndexedDBThread, | 203 base::Bind(&IndexedDBInternalsUI::ForceCloseOriginOnIndexedDBThread, |
| 206 base::Unretained(this), | 204 base::Unretained(this), partition_path, context, origin)); |
| 207 partition_path, | |
| 208 context, | |
| 209 origin_url)); | |
| 210 } | 205 } |
| 211 | 206 |
| 212 void IndexedDBInternalsUI::DownloadOriginDataOnIndexedDBThread( | 207 void IndexedDBInternalsUI::DownloadOriginDataOnIndexedDBThread( |
| 213 const base::FilePath& partition_path, | 208 const base::FilePath& partition_path, |
| 214 const scoped_refptr<IndexedDBContextImpl> context, | 209 const scoped_refptr<IndexedDBContextImpl> context, |
| 215 const GURL& origin_url) { | 210 const url::Origin& origin) { |
| 216 DCHECK(context->TaskRunner()->RunsTasksOnCurrentThread()); | 211 DCHECK(context->TaskRunner()->RunsTasksOnCurrentThread()); |
| 217 // This runs on the IndexedDB task runner to prevent script from reopening | 212 // This runs on the IndexedDB task runner to prevent script from reopening |
| 218 // the origin while we are zipping. | 213 // the origin while we are zipping. |
| 219 | 214 |
| 220 // Make sure the database hasn't been deleted since the page was loaded. | 215 // Make sure the database hasn't been deleted since the page was loaded. |
| 221 if (!context->HasOrigin(origin_url)) | 216 if (!context->HasOrigin(origin)) |
| 222 return; | 217 return; |
| 223 | 218 |
| 224 context->ForceClose(origin_url, | 219 context->ForceClose(origin, IndexedDBContextImpl::FORCE_CLOSE_INTERNALS_PAGE); |
| 225 IndexedDBContextImpl::FORCE_CLOSE_INTERNALS_PAGE); | 220 size_t connection_count = context->GetConnectionCount(origin); |
| 226 size_t connection_count = context->GetConnectionCount(origin_url); | |
| 227 | 221 |
| 228 base::ScopedTempDir temp_dir; | 222 base::ScopedTempDir temp_dir; |
| 229 if (!temp_dir.CreateUniqueTempDir()) | 223 if (!temp_dir.CreateUniqueTempDir()) |
| 230 return; | 224 return; |
| 231 | 225 |
| 232 // This will get cleaned up on the File thread after the download | 226 // This will get cleaned up on the File thread after the download |
| 233 // has completed. | 227 // has completed. |
| 234 base::FilePath temp_path = temp_dir.Take(); | 228 base::FilePath temp_path = temp_dir.Take(); |
| 235 | 229 |
| 236 std::string origin_id = storage::GetIdentifierFromOrigin(origin_url); | 230 std::string origin_id = |
| 231 storage::GetIdentifierFromOrigin(GURL(origin.Serialize())); |
| 237 base::FilePath zip_path = | 232 base::FilePath zip_path = |
| 238 temp_path.AppendASCII(origin_id).AddExtension(FILE_PATH_LITERAL("zip")); | 233 temp_path.AppendASCII(origin_id).AddExtension(FILE_PATH_LITERAL("zip")); |
| 239 | 234 |
| 240 std::vector<base::FilePath> paths = context->GetStoragePaths(origin_url); | 235 std::vector<base::FilePath> paths = context->GetStoragePaths(origin); |
| 241 zip::ZipWithFilterCallback(context->data_path(), zip_path, | 236 zip::ZipWithFilterCallback(context->data_path(), zip_path, |
| 242 base::Bind(AllowWhitelistedPaths, paths)); | 237 base::Bind(AllowWhitelistedPaths, paths)); |
| 243 | 238 |
| 244 BrowserThread::PostTask(BrowserThread::UI, | 239 BrowserThread::PostTask( |
| 245 FROM_HERE, | 240 BrowserThread::UI, FROM_HERE, |
| 246 base::Bind(&IndexedDBInternalsUI::OnDownloadDataReady, | 241 base::Bind(&IndexedDBInternalsUI::OnDownloadDataReady, |
| 247 base::Unretained(this), | 242 base::Unretained(this), partition_path, origin, temp_path, |
| 248 partition_path, | 243 zip_path, connection_count)); |
| 249 origin_url, | |
| 250 temp_path, | |
| 251 zip_path, | |
| 252 connection_count)); | |
| 253 } | 244 } |
| 254 | 245 |
| 255 void IndexedDBInternalsUI::ForceCloseOriginOnIndexedDBThread( | 246 void IndexedDBInternalsUI::ForceCloseOriginOnIndexedDBThread( |
| 256 const base::FilePath& partition_path, | 247 const base::FilePath& partition_path, |
| 257 const scoped_refptr<IndexedDBContextImpl> context, | 248 const scoped_refptr<IndexedDBContextImpl> context, |
| 258 const GURL& origin_url) { | 249 const url::Origin& origin) { |
| 259 DCHECK(context->TaskRunner()->RunsTasksOnCurrentThread()); | 250 DCHECK(context->TaskRunner()->RunsTasksOnCurrentThread()); |
| 260 | 251 |
| 261 // Make sure the database hasn't been deleted since the page was loaded. | 252 // Make sure the database hasn't been deleted since the page was loaded. |
| 262 if (!context->HasOrigin(origin_url)) | 253 if (!context->HasOrigin(origin)) |
| 263 return; | 254 return; |
| 264 | 255 |
| 265 context->ForceClose(origin_url, | 256 context->ForceClose(origin, IndexedDBContextImpl::FORCE_CLOSE_INTERNALS_PAGE); |
| 266 IndexedDBContextImpl::FORCE_CLOSE_INTERNALS_PAGE); | 257 size_t connection_count = context->GetConnectionCount(origin); |
| 267 size_t connection_count = context->GetConnectionCount(origin_url); | |
| 268 | 258 |
| 269 BrowserThread::PostTask(BrowserThread::UI, | 259 BrowserThread::PostTask( |
| 270 FROM_HERE, | 260 BrowserThread::UI, FROM_HERE, |
| 271 base::Bind(&IndexedDBInternalsUI::OnForcedClose, | 261 base::Bind(&IndexedDBInternalsUI::OnForcedClose, base::Unretained(this), |
| 272 base::Unretained(this), | 262 partition_path, origin, connection_count)); |
| 273 partition_path, | |
| 274 origin_url, | |
| 275 connection_count)); | |
| 276 } | 263 } |
| 277 | 264 |
| 278 void IndexedDBInternalsUI::OnForcedClose(const base::FilePath& partition_path, | 265 void IndexedDBInternalsUI::OnForcedClose(const base::FilePath& partition_path, |
| 279 const GURL& origin_url, | 266 const url::Origin& origin, |
| 280 size_t connection_count) { | 267 size_t connection_count) { |
| 281 web_ui()->CallJavascriptFunction( | 268 web_ui()->CallJavascriptFunction( |
| 282 "indexeddb.onForcedClose", | 269 "indexeddb.onForcedClose", base::StringValue(partition_path.value()), |
| 283 base::StringValue(partition_path.value()), | 270 base::StringValue(origin.Serialize()), |
| 284 base::StringValue(origin_url.spec()), | |
| 285 base::FundamentalValue(static_cast<double>(connection_count))); | 271 base::FundamentalValue(static_cast<double>(connection_count))); |
| 286 } | 272 } |
| 287 | 273 |
| 288 void IndexedDBInternalsUI::OnDownloadDataReady( | 274 void IndexedDBInternalsUI::OnDownloadDataReady( |
| 289 const base::FilePath& partition_path, | 275 const base::FilePath& partition_path, |
| 290 const GURL& origin_url, | 276 const url::Origin& origin, |
| 291 const base::FilePath temp_path, | 277 const base::FilePath temp_path, |
| 292 const base::FilePath zip_path, | 278 const base::FilePath zip_path, |
| 293 size_t connection_count) { | 279 size_t connection_count) { |
| 294 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 280 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 295 const GURL url = GURL(FILE_PATH_LITERAL("file://") + zip_path.value()); | 281 const GURL url = GURL(FILE_PATH_LITERAL("file://") + zip_path.value()); |
| 296 BrowserContext* browser_context = | 282 BrowserContext* browser_context = |
| 297 web_ui()->GetWebContents()->GetBrowserContext(); | 283 web_ui()->GetWebContents()->GetBrowserContext(); |
| 298 scoped_ptr<DownloadUrlParameters> dl_params( | 284 scoped_ptr<DownloadUrlParameters> dl_params( |
| 299 DownloadUrlParameters::FromWebContents(web_ui()->GetWebContents(), url)); | 285 DownloadUrlParameters::FromWebContents(web_ui()->GetWebContents(), url)); |
| 300 DownloadManager* dlm = BrowserContext::GetDownloadManager(browser_context); | 286 DownloadManager* dlm = BrowserContext::GetDownloadManager(browser_context); |
| 301 | 287 |
| 302 const GURL referrer(web_ui()->GetWebContents()->GetLastCommittedURL()); | 288 const GURL referrer(web_ui()->GetWebContents()->GetLastCommittedURL()); |
| 303 dl_params->set_referrer(content::Referrer::SanitizeForRequest( | 289 dl_params->set_referrer(content::Referrer::SanitizeForRequest( |
| 304 url, content::Referrer(referrer, blink::WebReferrerPolicyDefault))); | 290 url, content::Referrer(referrer, blink::WebReferrerPolicyDefault))); |
| 305 | 291 |
| 306 // This is how to watch for the download to finish: first wait for it | 292 // This is how to watch for the download to finish: first wait for it |
| 307 // to start, then attach a DownloadItem::Observer to observe the | 293 // to start, then attach a DownloadItem::Observer to observe the |
| 308 // state change to the finished state. | 294 // state change to the finished state. |
| 309 dl_params->set_callback(base::Bind(&IndexedDBInternalsUI::OnDownloadStarted, | 295 dl_params->set_callback(base::Bind(&IndexedDBInternalsUI::OnDownloadStarted, |
| 310 base::Unretained(this), | 296 base::Unretained(this), partition_path, |
| 311 partition_path, | 297 origin, temp_path, connection_count)); |
| 312 origin_url, | |
| 313 temp_path, | |
| 314 connection_count)); | |
| 315 dlm->DownloadUrl(std::move(dl_params)); | 298 dlm->DownloadUrl(std::move(dl_params)); |
| 316 } | 299 } |
| 317 | 300 |
| 318 // The entire purpose of this class is to delete the temp file after | 301 // The entire purpose of this class is to delete the temp file after |
| 319 // the download is complete. | 302 // the download is complete. |
| 320 class FileDeleter : public DownloadItem::Observer { | 303 class FileDeleter : public DownloadItem::Observer { |
| 321 public: | 304 public: |
| 322 explicit FileDeleter(const base::FilePath& temp_dir) : temp_dir_(temp_dir) {} | 305 explicit FileDeleter(const base::FilePath& temp_dir) : temp_dir_(temp_dir) {} |
| 323 ~FileDeleter() override; | 306 ~FileDeleter() override; |
| 324 | 307 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 350 } | 333 } |
| 351 | 334 |
| 352 FileDeleter::~FileDeleter() { | 335 FileDeleter::~FileDeleter() { |
| 353 base::ScopedTempDir path; | 336 base::ScopedTempDir path; |
| 354 bool will_delete = path.Set(temp_dir_); | 337 bool will_delete = path.Set(temp_dir_); |
| 355 DCHECK(will_delete); | 338 DCHECK(will_delete); |
| 356 } | 339 } |
| 357 | 340 |
| 358 void IndexedDBInternalsUI::OnDownloadStarted( | 341 void IndexedDBInternalsUI::OnDownloadStarted( |
| 359 const base::FilePath& partition_path, | 342 const base::FilePath& partition_path, |
| 360 const GURL& origin_url, | 343 const url::Origin& origin, |
| 361 const base::FilePath& temp_path, | 344 const base::FilePath& temp_path, |
| 362 size_t connection_count, | 345 size_t connection_count, |
| 363 DownloadItem* item, | 346 DownloadItem* item, |
| 364 DownloadInterruptReason interrupt_reason) { | 347 DownloadInterruptReason interrupt_reason) { |
| 365 | |
| 366 if (interrupt_reason != DOWNLOAD_INTERRUPT_REASON_NONE) { | 348 if (interrupt_reason != DOWNLOAD_INTERRUPT_REASON_NONE) { |
| 367 LOG(ERROR) << "Error downloading database dump: " | 349 LOG(ERROR) << "Error downloading database dump: " |
| 368 << DownloadInterruptReasonToString(interrupt_reason); | 350 << DownloadInterruptReasonToString(interrupt_reason); |
| 369 return; | 351 return; |
| 370 } | 352 } |
| 371 | 353 |
| 372 item->AddObserver(new FileDeleter(temp_path)); | 354 item->AddObserver(new FileDeleter(temp_path)); |
| 373 web_ui()->CallJavascriptFunction( | 355 web_ui()->CallJavascriptFunction( |
| 374 "indexeddb.onOriginDownloadReady", | 356 "indexeddb.onOriginDownloadReady", |
| 375 base::StringValue(partition_path.value()), | 357 base::StringValue(partition_path.value()), |
| 376 base::StringValue(origin_url.spec()), | 358 base::StringValue(origin.Serialize()), |
| 377 base::FundamentalValue(static_cast<double>(connection_count))); | 359 base::FundamentalValue(static_cast<double>(connection_count))); |
| 378 } | 360 } |
| 379 | 361 |
| 380 } // namespace content | 362 } // namespace content |
| OLD | NEW |