1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/indexed_db/indexed_db_internals_ui.h"
#include <string>
#include "base/bind.h"
#include "base/files/scoped_temp_dir.h"
#include "base/threading/platform_thread.h"
#include "base/values.h"
#include "content/browser/indexed_db/indexed_db_context_impl.h"
#include "content/grit/content_resources.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/download_manager.h"
#include "content/public/browser/download_url_parameters.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui.h"
#include "content/public/browser/web_ui_data_source.h"
#include "content/public/common/url_constants.h"
#include "storage/common/database/database_identifier.h"
#include "third_party/zlib/google/zip.h"
#include "ui/base/text/bytes_formatting.h"
namespace content {
namespace {
bool AllowWhitelistedPaths(const std::vector<base::FilePath>& allowed_paths,
const base::FilePath& candidate_path) {
for (const base::FilePath& allowed_path : allowed_paths) {
if (allowed_path.IsParent(candidate_path))
return true;
}
return false;
}
} // namespace
IndexedDBInternalsUI::IndexedDBInternalsUI(WebUI* web_ui)
: WebUIController(web_ui) {
web_ui->RegisterMessageCallback(
"getAllOrigins",
base::Bind(&IndexedDBInternalsUI::GetAllOrigins, base::Unretained(this)));
web_ui->RegisterMessageCallback(
"downloadOriginData",
base::Bind(&IndexedDBInternalsUI::DownloadOriginData,
base::Unretained(this)));
web_ui->RegisterMessageCallback(
"forceClose",
base::Bind(&IndexedDBInternalsUI::ForceCloseOrigin,
base::Unretained(this)));
WebUIDataSource* source =
WebUIDataSource::Create(kChromeUIIndexedDBInternalsHost);
source->SetJsonPath("strings.js");
source->AddResourcePath("indexeddb_internals.js",
IDR_INDEXED_DB_INTERNALS_JS);
source->AddResourcePath("indexeddb_internals.css",
IDR_INDEXED_DB_INTERNALS_CSS);
source->SetDefaultResource(IDR_INDEXED_DB_INTERNALS_HTML);
BrowserContext* browser_context =
web_ui->GetWebContents()->GetBrowserContext();
WebUIDataSource::Add(browser_context, source);
}
IndexedDBInternalsUI::~IndexedDBInternalsUI() {}
void IndexedDBInternalsUI::AddContextFromStoragePartition(
StoragePartition* partition) {
scoped_refptr<IndexedDBContext> context = partition->GetIndexedDBContext();
context->TaskRunner()->PostTask(
FROM_HERE,
base::Bind(&IndexedDBInternalsUI::GetAllOriginsOnIndexedDBThread,
base::Unretained(this),
context,
partition->GetPath()));
}
void IndexedDBInternalsUI::GetAllOrigins(const base::ListValue* args) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
BrowserContext* browser_context =
web_ui()->GetWebContents()->GetBrowserContext();
BrowserContext::StoragePartitionCallback cb =
base::Bind(&IndexedDBInternalsUI::AddContextFromStoragePartition,
base::Unretained(this));
BrowserContext::ForEachStoragePartition(browser_context, cb);
}
void IndexedDBInternalsUI::GetAllOriginsOnIndexedDBThread(
scoped_refptr<IndexedDBContext> context,
const base::FilePath& context_path) {
DCHECK(context->TaskRunner()->RunsTasksOnCurrentThread());
IndexedDBContextImpl* context_impl =
static_cast<IndexedDBContextImpl*>(context.get());
scoped_ptr<base::ListValue> info_list(context_impl->GetAllOriginsDetails());
bool is_incognito = context_impl->is_incognito();
BrowserThread::PostTask(
BrowserThread::UI,
FROM_HERE,
base::Bind(&IndexedDBInternalsUI::OnOriginsReady,
base::Unretained(this),
base::Passed(&info_list),
is_incognito ? base::FilePath() : context_path));
}
void IndexedDBInternalsUI::OnOriginsReady(scoped_ptr<base::ListValue> origins,
const base::FilePath& path) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
web_ui()->CallJavascriptFunction(
"indexeddb.onOriginsReady", *origins, base::StringValue(path.value()));
}
static void FindContext(const base::FilePath& partition_path,
StoragePartition** result_partition,
scoped_refptr<IndexedDBContextImpl>* result_context,
StoragePartition* storage_partition) {
if (storage_partition->GetPath() == partition_path) {
*result_partition = storage_partition;
*result_context = static_cast<IndexedDBContextImpl*>(
storage_partition->GetIndexedDBContext());
}
}
bool IndexedDBInternalsUI::GetOriginData(
const base::ListValue* args,
base::FilePath* partition_path,
GURL* origin_url,
scoped_refptr<IndexedDBContextImpl>* context) {
base::FilePath::StringType path_string;
if (!args->GetString(0, &path_string))
return false;
*partition_path = base::FilePath(path_string);
std::string url_string;
if (!args->GetString(1, &url_string))
return false;
*origin_url = GURL(url_string);
return GetOriginContext(*partition_path, *origin_url, context);
}
bool IndexedDBInternalsUI::GetOriginContext(
const base::FilePath& path,
const GURL& origin_url,
scoped_refptr<IndexedDBContextImpl>* context) {
// search the origins to find the right context
BrowserContext* browser_context =
web_ui()->GetWebContents()->GetBrowserContext();
StoragePartition* result_partition;
BrowserContext::StoragePartitionCallback cb =
base::Bind(&FindContext, path, &result_partition, context);
BrowserContext::ForEachStoragePartition(browser_context, cb);
if (!result_partition || !(context->get()))
return false;
return true;
}
void IndexedDBInternalsUI::DownloadOriginData(const base::ListValue* args) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
base::FilePath partition_path;
GURL origin_url;
scoped_refptr<IndexedDBContextImpl> context;
if (!GetOriginData(args, &partition_path, &origin_url, &context))
return;
DCHECK(context.get());
context->TaskRunner()->PostTask(
FROM_HERE,
base::Bind(&IndexedDBInternalsUI::DownloadOriginDataOnIndexedDBThread,
base::Unretained(this),
partition_path,
context,
origin_url));
}
void IndexedDBInternalsUI::ForceCloseOrigin(const base::ListValue* args) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
base::FilePath partition_path;
GURL origin_url;
scoped_refptr<IndexedDBContextImpl> context;
if (!GetOriginData(args, &partition_path, &origin_url, &context))
return;
context->TaskRunner()->PostTask(
FROM_HERE,
base::Bind(&IndexedDBInternalsUI::ForceCloseOriginOnIndexedDBThread,
base::Unretained(this),
partition_path,
context,
origin_url));
}
void IndexedDBInternalsUI::DownloadOriginDataOnIndexedDBThread(
const base::FilePath& partition_path,
const scoped_refptr<IndexedDBContextImpl> context,
const GURL& origin_url) {
DCHECK(context->TaskRunner()->RunsTasksOnCurrentThread());
// Make sure the database hasn't been deleted since the page was loaded.
if (!context->IsInOriginSet(origin_url))
return;
context->ForceClose(origin_url,
IndexedDBContextImpl::FORCE_CLOSE_INTERNALS_PAGE);
size_t connection_count = context->GetConnectionCount(origin_url);
base::ScopedTempDir temp_dir;
if (!temp_dir.CreateUniqueTempDir())
return;
// This will get cleaned up on the File thread after the download
// has completed.
base::FilePath temp_path = temp_dir.Take();
std::string origin_id = storage::GetIdentifierFromOrigin(origin_url);
base::FilePath zip_path =
temp_path.AppendASCII(origin_id).AddExtension(FILE_PATH_LITERAL("zip"));
// This happens on the "webkit" thread (which is really just the IndexedDB
// thread) as a simple way to avoid another script reopening the origin
// while we are zipping.
std::vector<base::FilePath> paths = context->GetStoragePaths(origin_url);
zip::ZipWithFilterCallback(context->data_path(), zip_path,
base::Bind(AllowWhitelistedPaths, paths));
BrowserThread::PostTask(BrowserThread::UI,
FROM_HERE,
base::Bind(&IndexedDBInternalsUI::OnDownloadDataReady,
base::Unretained(this),
partition_path,
origin_url,
temp_path,
zip_path,
connection_count));
}
void IndexedDBInternalsUI::ForceCloseOriginOnIndexedDBThread(
const base::FilePath& partition_path,
const scoped_refptr<IndexedDBContextImpl> context,
const GURL& origin_url) {
DCHECK(context->TaskRunner()->RunsTasksOnCurrentThread());
// Make sure the database hasn't been deleted since the page was loaded.
if (!context->IsInOriginSet(origin_url))
return;
context->ForceClose(origin_url,
IndexedDBContextImpl::FORCE_CLOSE_INTERNALS_PAGE);
size_t connection_count = context->GetConnectionCount(origin_url);
BrowserThread::PostTask(BrowserThread::UI,
FROM_HERE,
base::Bind(&IndexedDBInternalsUI::OnForcedClose,
base::Unretained(this),
partition_path,
origin_url,
connection_count));
}
void IndexedDBInternalsUI::OnForcedClose(const base::FilePath& partition_path,
const GURL& origin_url,
size_t connection_count) {
web_ui()->CallJavascriptFunction(
"indexeddb.onForcedClose",
base::StringValue(partition_path.value()),
base::StringValue(origin_url.spec()),
base::FundamentalValue(static_cast<double>(connection_count)));
}
void IndexedDBInternalsUI::OnDownloadDataReady(
const base::FilePath& partition_path,
const GURL& origin_url,
const base::FilePath temp_path,
const base::FilePath zip_path,
size_t connection_count) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
const GURL url = GURL(FILE_PATH_LITERAL("file://") + zip_path.value());
BrowserContext* browser_context =
web_ui()->GetWebContents()->GetBrowserContext();
scoped_ptr<DownloadUrlParameters> dl_params(
DownloadUrlParameters::FromWebContents(web_ui()->GetWebContents(), url));
DownloadManager* dlm = BrowserContext::GetDownloadManager(browser_context);
const GURL referrer(web_ui()->GetWebContents()->GetLastCommittedURL());
dl_params->set_referrer(
content::Referrer(referrer, blink::WebReferrerPolicyDefault));
// This is how to watch for the download to finish: first wait for it
// to start, then attach a DownloadItem::Observer to observe the
// state change to the finished state.
dl_params->set_callback(base::Bind(&IndexedDBInternalsUI::OnDownloadStarted,
base::Unretained(this),
partition_path,
origin_url,
temp_path,
connection_count));
dlm->DownloadUrl(dl_params.Pass());
}
// The entire purpose of this class is to delete the temp file after
// the download is complete.
class FileDeleter : public DownloadItem::Observer {
public:
explicit FileDeleter(const base::FilePath& temp_dir) : temp_dir_(temp_dir) {}
~FileDeleter() override;
void OnDownloadUpdated(DownloadItem* download) override;
void OnDownloadOpened(DownloadItem* item) override {}
void OnDownloadRemoved(DownloadItem* item) override {}
void OnDownloadDestroyed(DownloadItem* item) override {}
private:
const base::FilePath temp_dir_;
DISALLOW_COPY_AND_ASSIGN(FileDeleter);
};
void FileDeleter::OnDownloadUpdated(DownloadItem* item) {
switch (item->GetState()) {
case DownloadItem::IN_PROGRESS:
break;
case DownloadItem::COMPLETE:
case DownloadItem::CANCELLED:
case DownloadItem::INTERRUPTED: {
item->RemoveObserver(this);
BrowserThread::DeleteOnFileThread::Destruct(this);
break;
}
default:
NOTREACHED();
}
}
FileDeleter::~FileDeleter() {
base::ScopedTempDir path;
bool will_delete = path.Set(temp_dir_);
DCHECK(will_delete);
}
void IndexedDBInternalsUI::OnDownloadStarted(
const base::FilePath& partition_path,
const GURL& origin_url,
const base::FilePath& temp_path,
size_t connection_count,
DownloadItem* item,
DownloadInterruptReason interrupt_reason) {
if (interrupt_reason != DOWNLOAD_INTERRUPT_REASON_NONE) {
LOG(ERROR) << "Error downloading database dump: "
<< DownloadInterruptReasonToString(interrupt_reason);
return;
}
item->AddObserver(new FileDeleter(temp_path));
web_ui()->CallJavascriptFunction(
"indexeddb.onOriginDownloadReady",
base::StringValue(partition_path.value()),
base::StringValue(origin_url.spec()),
base::FundamentalValue(static_cast<double>(connection_count)));
}
} // namespace content
|