summaryrefslogtreecommitdiffstats
path: root/content/browser/indexed_db/indexed_db_internals_ui.cc
blob: 720376875cc07784abd8695d77006f8f58f3971f (plain)
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
// 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 <algorithm>
#include <string>

#include "base/bind.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/scoped_vector.h"
#include "base/threading/platform_thread.h"
#include "base/values.h"
#include "content/browser/indexed_db/indexed_db_context_impl.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 "grit/content_resources.h"
#include "third_party/WebKit/public/platform/WebString.h"
#include "third_party/zlib/google/zip.h"
#include "webkit/base/file_path_string_conversions.h"
#include "webkit/base/origin_url_conversions.h"

namespace content {

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)));

  WebUIDataSource* source =
      WebUIDataSource::Create(kChromeUIIndexedDBInternalsHost);
  source->SetUseJsonJSFormatV2();
  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(
    ContextList* contexts,
    std::vector<base::FilePath>* paths,
    StoragePartition* partition) {
  scoped_refptr<IndexedDBContext> context = partition->GetIndexedDBContext();
  contexts->push_back(context);
  paths->push_back(partition->GetPath());
}

void IndexedDBInternalsUI::GetAllOrigins(const base::ListValue* args) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  BrowserContext* browser_context =
      web_ui()->GetWebContents()->GetBrowserContext();

  scoped_ptr<std::vector<base::FilePath> > paths(
      new std::vector<base::FilePath>);
  scoped_ptr<ContextList> contexts(new ContextList);
  BrowserContext::StoragePartitionCallback cb =
      base::Bind(&AddContextFromStoragePartition, contexts.get(), paths.get());
  BrowserContext::ForEachStoragePartition(browser_context, cb);

  BrowserThread::PostTask(
      BrowserThread::WEBKIT_DEPRECATED,
      FROM_HERE,
      base::Bind(&IndexedDBInternalsUI::GetAllOriginsOnWebkitThread,
                 base::Unretained(this),
                 base::Passed(&contexts),
                 base::Passed(&paths)));
}

bool HostNameComparator(const IndexedDBInfo& i, const IndexedDBInfo& j) {
  return i.origin_.host() < j.origin_.host();
}

void IndexedDBInternalsUI::GetAllOriginsOnWebkitThread(
    const scoped_ptr<ContextList> contexts,
    const scoped_ptr<std::vector<base::FilePath> > context_paths) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED));
  DCHECK_EQ(contexts->size(), context_paths->size());

  std::vector<base::FilePath>::const_iterator path_iter =
      context_paths->begin();
  for (ContextList::const_iterator iter = contexts->begin();
       iter != contexts->end();
       ++iter, ++path_iter) {
    IndexedDBContext* context = *iter;
    const base::FilePath& context_path = *path_iter;

    scoped_ptr<std::vector<IndexedDBInfo> > info_list(
        new std::vector<IndexedDBInfo>(context->GetAllOriginsInfo()));
    std::sort(info_list->begin(), info_list->end(), HostNameComparator);
    BrowserThread::PostTask(BrowserThread::UI,
                            FROM_HERE,
                            base::Bind(&IndexedDBInternalsUI::OnOriginsReady,
                                       base::Unretained(this),
                                       base::Passed(&info_list),
                                       context_path));
  }
}

void IndexedDBInternalsUI::OnOriginsReady(
    scoped_ptr<std::vector<IndexedDBInfo> > origins,
    const base::FilePath& path) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  base::ListValue urls;
  for (std::vector<IndexedDBInfo>::const_iterator iter = origins->begin();
       iter != origins->end();
       ++iter) {
    base::DictionaryValue* info = new DictionaryValue;
    info->SetString("url", iter->origin_.spec());
    info->SetDouble("size", iter->size_);
    info->SetDouble("last_modified", iter->last_modified_.ToJsTime());
    info->SetString("path", iter->path_.value());
    urls.Append(info);
  }
  web_ui()->CallJavascriptFunction(
      "indexeddb.onOriginsReady", urls, 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());
  }
}

void IndexedDBInternalsUI::DownloadOriginData(const base::ListValue* args) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  base::FilePath::StringType path_string;
  if (!args->GetString(0, &path_string))
    return;
  const base::FilePath partition_path(path_string);

  std::string url_string;
  if (!args->GetString(1, &url_string))
    return;
  const GURL origin_url(url_string);

  // search the origins to find the right context

  BrowserContext* browser_context =
      web_ui()->GetWebContents()->GetBrowserContext();

  scoped_refptr<IndexedDBContextImpl> result_context;
  StoragePartition* result_partition;
  scoped_ptr<ContextList> contexts(new ContextList);
  BrowserContext::StoragePartitionCallback cb = base::Bind(
      &FindContext, partition_path, &result_partition, &result_context);
  BrowserContext::ForEachStoragePartition(browser_context, cb);
  DCHECK(result_partition);
  DCHECK(result_context);

  BrowserThread::PostTask(
      BrowserThread::WEBKIT_DEPRECATED,
      FROM_HERE,
      base::Bind(&IndexedDBInternalsUI::DownloadOriginDataOnWebkitThread,
                 base::Unretained(this),
                 result_partition->GetPath(),
                 result_context,
                 origin_url));
}

void IndexedDBInternalsUI::DownloadOriginDataOnWebkitThread(
    const base::FilePath& partition_path,
    const scoped_refptr<IndexedDBContextImpl> context,
    const GURL& origin_url) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED));

  if (!context->IsInOriginSet(origin_url))
    return;

  context->ForceClose(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();

  base::string16 origin_id =
      webkit_base::GetOriginIdentifierFromURL(origin_url);
  base::FilePath::StringType zip_name =
      webkit_base::WebStringToFilePathString(origin_id);
  base::FilePath zip_path =
      temp_path.Append(zip_name).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.
  zip::Zip(context->GetFilePath(origin_url), zip_path, true);

  BrowserThread::PostTask(BrowserThread::UI,
                          FROM_HERE,
                          base::Bind(&IndexedDBInternalsUI::OnDownloadDataReady,
                                     base::Unretained(this),
                                     partition_path,
                                     origin_url,
                                     temp_path,
                                     zip_path));
}

void IndexedDBInternalsUI::OnDownloadDataReady(
    const base::FilePath& partition_path,
    const GURL& origin_url,
    const base::FilePath temp_path,
    const base::FilePath zip_path) {
  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()->GetURL());
  dl_params->set_referrer(
      content::Referrer(referrer, WebKit::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));
  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) {}
  virtual ~FileDeleter();

  virtual void OnDownloadUpdated(DownloadItem* download) OVERRIDE;
  virtual void OnDownloadOpened(DownloadItem* item) OVERRIDE {}
  virtual void OnDownloadRemoved(DownloadItem* item) OVERRIDE {}
  virtual void OnDownloadDestroyed(DownloadItem* item) OVERRIDE {}

 private:
  const base::FilePath temp_dir_;
};

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 ALLOW_UNUSED = path.Set(temp_dir_);
  DCHECK(will_delete);
}

void IndexedDBInternalsUI::OnDownloadStarted(
    const base::FilePath& partition_path,
    const GURL& origin_url,
    const base::FilePath& temp_path,
    DownloadItem* item,
    net::Error error) {

  if (error != net::OK) {
    LOG(ERROR) << "Error downloading database dump: "
               << net::ErrorToString(error);
    return;
  }

  item->AddObserver(new FileDeleter(temp_path));
  web_ui()->CallJavascriptFunction("indexeddb.onOriginDownloadReady",
                                   base::StringValue(partition_path.value()),
                                   base::StringValue(origin_url.spec()));
}

}  // namespace content