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
3 // found in the LICENSE file.
5 #include "content/browser/indexed_db/indexed_db_internals_ui.h"
10 #include "base/files/scoped_temp_dir.h"
11 #include "base/threading/platform_thread.h"
12 #include "base/values.h"
13 #include "content/browser/indexed_db/indexed_db_context_impl.h"
14 #include "content/public/browser/browser_context.h"
15 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/download_manager.h"
17 #include "content/public/browser/download_url_parameters.h"
18 #include "content/public/browser/storage_partition.h"
19 #include "content/public/browser/web_contents.h"
20 #include "content/public/browser/web_ui.h"
21 #include "content/public/browser/web_ui_data_source.h"
22 #include "content/public/common/url_constants.h"
23 #include "grit/content_resources.h"
24 #include "third_party/zlib/google/zip.h"
25 #include "ui/base/text/bytes_formatting.h"
26 #include "webkit/common/database/database_identifier.h"
30 IndexedDBInternalsUI::IndexedDBInternalsUI(WebUI* web_ui)
31 : WebUIController(web_ui) {
32 web_ui->RegisterMessageCallback(
34 base::Bind(&IndexedDBInternalsUI::GetAllOrigins, base::Unretained(this)));
36 web_ui->RegisterMessageCallback(
38 base::Bind(&IndexedDBInternalsUI::DownloadOriginData,
39 base::Unretained(this)));
40 web_ui->RegisterMessageCallback(
42 base::Bind(&IndexedDBInternalsUI::ForceCloseOrigin,
43 base::Unretained(this)));
45 WebUIDataSource* source =
46 WebUIDataSource::Create(kChromeUIIndexedDBInternalsHost);
47 source->SetUseJsonJSFormatV2();
48 source->SetJsonPath("strings.js");
49 source->AddResourcePath("indexeddb_internals.js",
50 IDR_INDEXED_DB_INTERNALS_JS);
51 source->AddResourcePath("indexeddb_internals.css",
52 IDR_INDEXED_DB_INTERNALS_CSS);
53 source->SetDefaultResource(IDR_INDEXED_DB_INTERNALS_HTML);
55 BrowserContext* browser_context =
56 web_ui->GetWebContents()->GetBrowserContext();
57 WebUIDataSource::Add(browser_context, source);
60 IndexedDBInternalsUI::~IndexedDBInternalsUI() {}
62 void IndexedDBInternalsUI::AddContextFromStoragePartition(
63 StoragePartition* partition) {
64 scoped_refptr<IndexedDBContext> context = partition->GetIndexedDBContext();
65 context->TaskRunner()->PostTask(
67 base::Bind(&IndexedDBInternalsUI::GetAllOriginsOnIndexedDBThread,
68 base::Unretained(this),
70 partition->GetPath()));
73 void IndexedDBInternalsUI::GetAllOrigins(const base::ListValue* args) {
74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
76 BrowserContext* browser_context =
77 web_ui()->GetWebContents()->GetBrowserContext();
79 BrowserContext::StoragePartitionCallback cb =
80 base::Bind(&IndexedDBInternalsUI::AddContextFromStoragePartition,
81 base::Unretained(this));
82 BrowserContext::ForEachStoragePartition(browser_context, cb);
85 void IndexedDBInternalsUI::GetAllOriginsOnIndexedDBThread(
86 scoped_refptr<IndexedDBContext> context,
87 const base::FilePath& context_path) {
88 DCHECK(context->TaskRunner()->RunsTasksOnCurrentThread());
90 scoped_ptr<ListValue> info_list(static_cast<IndexedDBContextImpl*>(
91 context.get())->GetAllOriginsDetails());
93 BrowserThread::PostTask(BrowserThread::UI,
95 base::Bind(&IndexedDBInternalsUI::OnOriginsReady,
96 base::Unretained(this),
97 base::Passed(&info_list),
101 void IndexedDBInternalsUI::OnOriginsReady(scoped_ptr<ListValue> origins,
102 const base::FilePath& path) {
103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
104 web_ui()->CallJavascriptFunction(
105 "indexeddb.onOriginsReady", *origins, base::StringValue(path.value()));
108 static void FindContext(const base::FilePath& partition_path,
109 StoragePartition** result_partition,
110 scoped_refptr<IndexedDBContextImpl>* result_context,
111 StoragePartition* storage_partition) {
112 if (storage_partition->GetPath() == partition_path) {
113 *result_partition = storage_partition;
114 *result_context = static_cast<IndexedDBContextImpl*>(
115 storage_partition->GetIndexedDBContext());
119 bool IndexedDBInternalsUI::GetOriginData(
120 const base::ListValue* args,
121 base::FilePath* partition_path,
123 scoped_refptr<IndexedDBContextImpl>* context) {
124 base::FilePath::StringType path_string;
125 if (!args->GetString(0, &path_string))
127 *partition_path = base::FilePath(path_string);
129 std::string url_string;
130 if (!args->GetString(1, &url_string))
133 *origin_url = GURL(url_string);
135 return GetOriginContext(*partition_path, *origin_url, context);
138 bool IndexedDBInternalsUI::GetOriginContext(
139 const base::FilePath& path,
140 const GURL& origin_url,
141 scoped_refptr<IndexedDBContextImpl>* context) {
142 // search the origins to find the right context
143 BrowserContext* browser_context =
144 web_ui()->GetWebContents()->GetBrowserContext();
146 StoragePartition* result_partition;
147 BrowserContext::StoragePartitionCallback cb =
148 base::Bind(&FindContext, path, &result_partition, context);
149 BrowserContext::ForEachStoragePartition(browser_context, cb);
151 if (!result_partition || !(*context))
157 void IndexedDBInternalsUI::DownloadOriginData(const base::ListValue* args) {
158 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
160 base::FilePath partition_path;
162 scoped_refptr<IndexedDBContextImpl> context;
163 if (!GetOriginData(args, &partition_path, &origin_url, &context))
167 context->TaskRunner()->PostTask(
169 base::Bind(&IndexedDBInternalsUI::DownloadOriginDataOnIndexedDBThread,
170 base::Unretained(this),
176 void IndexedDBInternalsUI::ForceCloseOrigin(const base::ListValue* args) {
177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
179 base::FilePath partition_path;
181 scoped_refptr<IndexedDBContextImpl> context;
182 if (!GetOriginData(args, &partition_path, &origin_url, &context))
185 context->TaskRunner()->PostTask(
187 base::Bind(&IndexedDBInternalsUI::ForceCloseOriginOnIndexedDBThread,
188 base::Unretained(this),
194 void IndexedDBInternalsUI::DownloadOriginDataOnIndexedDBThread(
195 const base::FilePath& partition_path,
196 const scoped_refptr<IndexedDBContextImpl> context,
197 const GURL& origin_url) {
198 DCHECK(context->TaskRunner()->RunsTasksOnCurrentThread());
200 // Make sure the database hasn't been deleted since the page was loaded.
201 if (!context->IsInOriginSet(origin_url))
204 context->ForceClose(origin_url);
205 size_t connection_count = context->GetConnectionCount(origin_url);
207 base::ScopedTempDir temp_dir;
208 if (!temp_dir.CreateUniqueTempDir())
211 // This will get cleaned up on the File thread after the download
213 base::FilePath temp_path = temp_dir.Take();
215 std::string origin_id = webkit_database::GetIdentifierFromOrigin(origin_url);
216 base::FilePath zip_path =
217 temp_path.AppendASCII(origin_id).AddExtension(FILE_PATH_LITERAL("zip"));
219 // This happens on the "webkit" thread (which is really just the IndexedDB
220 // thread) as a simple way to avoid another script reopening the origin
221 // while we are zipping.
222 zip::Zip(context->GetFilePath(origin_url), zip_path, true);
224 BrowserThread::PostTask(BrowserThread::UI,
226 base::Bind(&IndexedDBInternalsUI::OnDownloadDataReady,
227 base::Unretained(this),
235 void IndexedDBInternalsUI::ForceCloseOriginOnIndexedDBThread(
236 const base::FilePath& partition_path,
237 const scoped_refptr<IndexedDBContextImpl> context,
238 const GURL& origin_url) {
239 DCHECK(context->TaskRunner()->RunsTasksOnCurrentThread());
241 // Make sure the database hasn't been deleted since the page was loaded.
242 if (!context->IsInOriginSet(origin_url))
245 context->ForceClose(origin_url);
246 size_t connection_count = context->GetConnectionCount(origin_url);
248 BrowserThread::PostTask(BrowserThread::UI,
250 base::Bind(&IndexedDBInternalsUI::OnForcedClose,
251 base::Unretained(this),
257 void IndexedDBInternalsUI::OnForcedClose(const base::FilePath& partition_path,
258 const GURL& origin_url,
259 size_t connection_count) {
260 web_ui()->CallJavascriptFunction(
261 "indexeddb.onForcedClose",
262 base::StringValue(partition_path.value()),
263 base::StringValue(origin_url.spec()),
264 base::FundamentalValue(double(connection_count)));
267 void IndexedDBInternalsUI::OnDownloadDataReady(
268 const base::FilePath& partition_path,
269 const GURL& origin_url,
270 const base::FilePath temp_path,
271 const base::FilePath zip_path,
272 size_t connection_count) {
273 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
274 const GURL url = GURL(FILE_PATH_LITERAL("file://") + zip_path.value());
275 BrowserContext* browser_context =
276 web_ui()->GetWebContents()->GetBrowserContext();
277 scoped_ptr<DownloadUrlParameters> dl_params(
278 DownloadUrlParameters::FromWebContents(web_ui()->GetWebContents(), url));
279 DownloadManager* dlm = BrowserContext::GetDownloadManager(browser_context);
281 const GURL referrer(web_ui()->GetWebContents()->GetLastCommittedURL());
282 dl_params->set_referrer(
283 content::Referrer(referrer, WebKit::WebReferrerPolicyDefault));
285 // This is how to watch for the download to finish: first wait for it
286 // to start, then attach a DownloadItem::Observer to observe the
287 // state change to the finished state.
288 dl_params->set_callback(base::Bind(&IndexedDBInternalsUI::OnDownloadStarted,
289 base::Unretained(this),
294 dlm->DownloadUrl(dl_params.Pass());
297 // The entire purpose of this class is to delete the temp file after
298 // the download is complete.
299 class FileDeleter : public DownloadItem::Observer {
301 explicit FileDeleter(const base::FilePath& temp_dir) : temp_dir_(temp_dir) {}
302 virtual ~FileDeleter();
304 virtual void OnDownloadUpdated(DownloadItem* download) OVERRIDE;
305 virtual void OnDownloadOpened(DownloadItem* item) OVERRIDE {}
306 virtual void OnDownloadRemoved(DownloadItem* item) OVERRIDE {}
307 virtual void OnDownloadDestroyed(DownloadItem* item) OVERRIDE {}
310 const base::FilePath temp_dir_;
313 void FileDeleter::OnDownloadUpdated(DownloadItem* item) {
314 switch (item->GetState()) {
315 case DownloadItem::IN_PROGRESS:
317 case DownloadItem::COMPLETE:
318 case DownloadItem::CANCELLED:
319 case DownloadItem::INTERRUPTED: {
320 item->RemoveObserver(this);
321 BrowserThread::DeleteOnFileThread::Destruct(this);
329 FileDeleter::~FileDeleter() {
330 base::ScopedTempDir path;
331 bool will_delete ALLOW_UNUSED = path.Set(temp_dir_);
335 void IndexedDBInternalsUI::OnDownloadStarted(
336 const base::FilePath& partition_path,
337 const GURL& origin_url,
338 const base::FilePath& temp_path,
339 size_t connection_count,
343 if (error != net::OK) {
345 << "Error downloading database dump: " << net::ErrorToString(error);
349 item->AddObserver(new FileDeleter(temp_path));
350 web_ui()->CallJavascriptFunction(
351 "indexeddb.onOriginDownloadReady",
352 base::StringValue(partition_path.value()),
353 base::StringValue(origin_url.spec()),
354 base::FundamentalValue(double(connection_count)));
357 } // namespace content