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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
|
// Copyright (c) 2012 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 "chrome/browser/ui/webui/downloads_dom_handler.h"
#include <algorithm>
#include <functional>
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/i18n/rtl.h"
#include "base/i18n/time_formatting.h"
#include "base/memory/singleton.h"
#include "base/metrics/histogram.h"
#include "base/string_piece.h"
#include "base/threading/thread.h"
#include "base/utf_string_conversions.h"
#include "base/value_conversions.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/download/download_crx_util.h"
#include "chrome/browser/download/download_danger_prompt.h"
#include "chrome/browser/download/download_history.h"
#include "chrome/browser/download/download_item_model.h"
#include "chrome/browser/download/download_prefs.h"
#include "chrome/browser/download/download_query.h"
#include "chrome/browser/download/download_service.h"
#include "chrome/browser/download/download_service_factory.h"
#include "chrome/browser/download/download_util.h"
#include "chrome/browser/platform_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/webui/fileicon_source.h"
#include "chrome/common/time_format.h"
#include "chrome/common/url_constants.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/download_item.h"
#include "content/public/browser/user_metrics.h"
#include "content/public/browser/url_data_source.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui.h"
#include "grit/generated_resources.h"
#include "net/base/net_util.h"
#include "ui/gfx/image/image.h"
#if defined(OS_CHROMEOS)
#include "chrome/browser/chromeos/extensions/file_manager_util.h"
#endif
using content::BrowserContext;
using content::BrowserThread;
using content::UserMetricsAction;
namespace {
// Maximum number of downloads to show. TODO(glen): Remove this and instead
// stuff the downloads down the pipe slowly.
static const size_t kMaxDownloads = 150;
enum DownloadsDOMEvent {
DOWNLOADS_DOM_EVENT_GET_DOWNLOADS = 0,
DOWNLOADS_DOM_EVENT_OPEN_FILE = 1,
DOWNLOADS_DOM_EVENT_DRAG = 2,
DOWNLOADS_DOM_EVENT_SAVE_DANGEROUS = 3,
DOWNLOADS_DOM_EVENT_DISCARD_DANGEROUS = 4,
DOWNLOADS_DOM_EVENT_SHOW = 5,
DOWNLOADS_DOM_EVENT_PAUSE = 6,
DOWNLOADS_DOM_EVENT_REMOVE = 7,
DOWNLOADS_DOM_EVENT_CANCEL = 8,
DOWNLOADS_DOM_EVENT_CLEAR_ALL = 9,
DOWNLOADS_DOM_EVENT_OPEN_FOLDER = 10,
DOWNLOADS_DOM_EVENT_RESUME = 11,
DOWNLOADS_DOM_EVENT_MAX
};
void CountDownloadsDOMEvents(DownloadsDOMEvent event) {
UMA_HISTOGRAM_ENUMERATION("Download.DOMEvent",
event,
DOWNLOADS_DOM_EVENT_MAX);
}
// Returns a string constant to be used as the |danger_type| value in
// CreateDownloadItemValue(). Only return strings for DANGEROUS_FILE,
// DANGEROUS_URL, DANGEROUS_CONTENT, and UNCOMMON_CONTENT because the
// |danger_type| value is only defined if the value of |state| is |DANGEROUS|.
const char* GetDangerTypeString(content::DownloadDangerType danger_type) {
switch (danger_type) {
case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE:
return "DANGEROUS_FILE";
case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL:
return "DANGEROUS_URL";
case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT:
return "DANGEROUS_CONTENT";
case content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT:
return "UNCOMMON_CONTENT";
case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST:
return "DANGEROUS_HOST";
default:
// Don't return a danger type string if it is NOT_DANGEROUS or
// MAYBE_DANGEROUS_CONTENT.
NOTREACHED();
return "";
}
}
// Returns a JSON dictionary containing some of the attributes of |download|.
// The JSON dictionary will also have a field "id" set to |id|, and a field
// "otr" set to |incognito|.
DictionaryValue* CreateDownloadItemValue(
content::DownloadItem* download_item,
bool incognito) {
// TODO(asanka): Move towards using download_model here for getting status and
// progress. The difference currently only matters to Drive downloads and
// those don't show up on the downloads page, but should.
DownloadItemModel download_model(download_item);
DictionaryValue* file_value = new DictionaryValue();
file_value->SetInteger(
"started", static_cast<int>(download_item->GetStartTime().ToTimeT()));
file_value->SetString(
"since_string", TimeFormat::RelativeDate(
download_item->GetStartTime(), NULL));
file_value->SetString(
"date_string", base::TimeFormatShortDate(download_item->GetStartTime()));
file_value->SetInteger("id", download_item->GetId());
base::FilePath download_path(download_item->GetTargetFilePath());
file_value->Set("file_path", base::CreateFilePathValue(download_path));
file_value->SetString("file_url",
net::FilePathToFileURL(download_path).spec());
// Keep file names as LTR.
string16 file_name =
download_item->GetFileNameToReportUser().LossyDisplayName();
file_name = base::i18n::GetDisplayStringInLTRDirectionality(file_name);
file_value->SetString("file_name", file_name);
file_value->SetString("url", download_item->GetURL().spec());
file_value->SetBoolean("otr", incognito);
file_value->SetInteger("total", static_cast<int>(
download_item->GetTotalBytes()));
file_value->SetBoolean("file_externally_removed",
download_item->GetFileExternallyRemoved());
if (download_item->IsInProgress()) {
if (download_item->IsDangerous()) {
file_value->SetString("state", "DANGEROUS");
// These are the only danger states that the UI is equipped to handle.
DCHECK(download_item->GetDangerType() ==
content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE ||
download_item->GetDangerType() ==
content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL ||
download_item->GetDangerType() ==
content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT ||
download_item->GetDangerType() ==
content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT ||
download_item->GetDangerType() ==
content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST);
const char* danger_type_value =
GetDangerTypeString(download_item->GetDangerType());
file_value->SetString("danger_type", danger_type_value);
} else if (download_item->IsPaused()) {
file_value->SetString("state", "PAUSED");
} else {
file_value->SetString("state", "IN_PROGRESS");
}
file_value->SetString("progress_status_text",
download_util::GetProgressStatusText(download_item));
file_value->SetInteger("percent",
static_cast<int>(download_item->PercentComplete()));
file_value->SetInteger("received",
static_cast<int>(download_item->GetReceivedBytes()));
} else if (download_item->IsInterrupted()) {
file_value->SetString("state", "INTERRUPTED");
file_value->SetString("progress_status_text",
download_util::GetProgressStatusText(download_item));
file_value->SetInteger("percent",
static_cast<int>(download_item->PercentComplete()));
file_value->SetInteger("received",
static_cast<int>(download_item->GetReceivedBytes()));
file_value->SetString("last_reason_text",
download_model.GetInterruptReasonText());
} else if (download_item->IsCancelled()) {
file_value->SetString("state", "CANCELLED");
} else if (download_item->IsComplete()) {
DCHECK(!download_item->IsDangerous());
file_value->SetString("state", "COMPLETE");
} else {
NOTREACHED() << "state undefined";
}
return file_value;
}
// Filters out extension downloads and downloads that don't have a filename yet.
bool IsDownloadDisplayable(const content::DownloadItem& item) {
return (!download_crx_util::IsExtensionDownload(item) &&
!item.IsTemporary() &&
!item.GetFileNameToReportUser().empty() &&
!item.GetTargetFilePath().empty());
}
} // namespace
DownloadsDOMHandler::DownloadsDOMHandler(content::DownloadManager* dlm)
: search_text_(),
ALLOW_THIS_IN_INITIALIZER_LIST(main_notifier_(dlm, this)),
update_scheduled_(false),
ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {
// Create our fileicon data source.
Profile* profile = Profile::FromBrowserContext(dlm->GetBrowserContext());
content::URLDataSource::Add(profile, new FileIconSource());
if (profile->IsOffTheRecord()) {
original_notifier_.reset(new AllDownloadItemNotifier(
BrowserContext::GetDownloadManager(profile->GetOriginalProfile()),
this));
}
}
DownloadsDOMHandler::~DownloadsDOMHandler() {
}
// DownloadsDOMHandler, public: -----------------------------------------------
void DownloadsDOMHandler::OnPageLoaded(const base::ListValue* args) {
SendCurrentDownloads();
}
void DownloadsDOMHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback("onPageLoaded",
base::Bind(&DownloadsDOMHandler::OnPageLoaded,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback("getDownloads",
base::Bind(&DownloadsDOMHandler::HandleGetDownloads,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback("openFile",
base::Bind(&DownloadsDOMHandler::HandleOpenFile,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback("drag",
base::Bind(&DownloadsDOMHandler::HandleDrag,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback("saveDangerous",
base::Bind(&DownloadsDOMHandler::HandleSaveDangerous,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback("discardDangerous",
base::Bind(&DownloadsDOMHandler::HandleDiscardDangerous,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback("show",
base::Bind(&DownloadsDOMHandler::HandleShow,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback("pause",
base::Bind(&DownloadsDOMHandler::HandlePause,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback("resume",
base::Bind(&DownloadsDOMHandler::HandleResume,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback("remove",
base::Bind(&DownloadsDOMHandler::HandleRemove,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback("cancel",
base::Bind(&DownloadsDOMHandler::HandleCancel,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback("clearAll",
base::Bind(&DownloadsDOMHandler::HandleClearAll,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback("openDownloadsFolder",
base::Bind(&DownloadsDOMHandler::HandleOpenDownloadsFolder,
weak_ptr_factory_.GetWeakPtr()));
}
void DownloadsDOMHandler::OnDownloadCreated(
content::DownloadManager* manager, content::DownloadItem* download_item) {
if (IsDownloadDisplayable(*download_item))
ScheduleSendCurrentDownloads();
}
void DownloadsDOMHandler::OnDownloadUpdated(
content::DownloadManager* manager,
content::DownloadItem* download_item) {
if (IsDownloadDisplayable(*download_item)) {
if (!search_text_.empty()) {
// Don't CallDownloadUpdated() if download_item doesn't match
// search_text_.
// TODO(benjhayden): Consider splitting MatchesQuery() out to a function.
content::DownloadManager::DownloadVector all_items, filtered_items;
all_items.push_back(download_item);
DownloadQuery query;
scoped_ptr<base::Value> query_text(base::Value::CreateStringValue(
search_text_));
query.AddFilter(DownloadQuery::FILTER_QUERY, *query_text.get());
query.Search(all_items.begin(), all_items.end(), &filtered_items);
if (filtered_items.empty())
return;
}
base::ListValue results_value;
results_value.Append(CreateDownloadItemValue(
download_item,
(original_notifier_.get() &&
(manager == main_notifier_.GetManager()))));
CallDownloadUpdated(results_value);
}
}
void DownloadsDOMHandler::OnDownloadRemoved(
content::DownloadManager* manager,
content::DownloadItem* download_item) {
// This relies on |download_item| being removed from DownloadManager in this
// MessageLoop iteration. |download_item| may not have been removed from
// DownloadManager when OnDownloadRemoved() is fired, so bounce off the
// MessageLoop to give it a chance to be removed. SendCurrentDownloads() looks
// at all downloads, and we do not tell it that |download_item| is being
// removed. If DownloadManager is ever changed to not immediately remove
// |download_item| from its map when OnDownloadRemoved is sent, then
// DownloadsDOMHandler::OnDownloadRemoved() will need to explicitly tell
// SendCurrentDownloads() that |download_item| was removed. A
// SupportsUserData::Data would be the correct way to do this.
ScheduleSendCurrentDownloads();
}
void DownloadsDOMHandler::HandleGetDownloads(const base::ListValue* args) {
CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_GET_DOWNLOADS);
search_text_ = ExtractStringValue(args);
SendCurrentDownloads();
}
void DownloadsDOMHandler::HandleOpenFile(const base::ListValue* args) {
CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_OPEN_FILE);
content::DownloadItem* file = GetDownloadByValue(args);
if (file)
file->OpenDownload();
}
void DownloadsDOMHandler::HandleDrag(const base::ListValue* args) {
CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_DRAG);
content::DownloadItem* file = GetDownloadByValue(args);
content::WebContents* web_contents = GetWebUIWebContents();
// |web_contents| is only NULL in the test.
if (!file || !web_contents || !file->IsComplete())
return;
gfx::Image* icon = g_browser_process->icon_manager()->LookupIcon(
file->GetTargetFilePath(), IconLoader::NORMAL);
gfx::NativeView view = web_contents->GetNativeView();
{
// Enable nested tasks during DnD, while |DragDownload()| blocks.
MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current());
download_util::DragDownload(file, icon, view);
}
}
void DownloadsDOMHandler::HandleSaveDangerous(const base::ListValue* args) {
CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_SAVE_DANGEROUS);
content::DownloadItem* file = GetDownloadByValue(args);
if (file)
ShowDangerPrompt(file);
}
void DownloadsDOMHandler::HandleDiscardDangerous(const base::ListValue* args) {
CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_DISCARD_DANGEROUS);
content::DownloadItem* file = GetDownloadByValue(args);
if (file)
file->Delete(content::DownloadItem::DELETE_DUE_TO_USER_DISCARD);
}
void DownloadsDOMHandler::HandleShow(const base::ListValue* args) {
CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_SHOW);
content::DownloadItem* file = GetDownloadByValue(args);
if (file)
file->ShowDownloadInShell();
}
void DownloadsDOMHandler::HandlePause(const base::ListValue* args) {
CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_PAUSE);
content::DownloadItem* file = GetDownloadByValue(args);
if (file)
file->Pause();
}
void DownloadsDOMHandler::HandleResume(const base::ListValue* args) {
CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_RESUME);
content::DownloadItem* file = GetDownloadByValue(args);
if (file)
file->Resume();
}
void DownloadsDOMHandler::HandleRemove(const base::ListValue* args) {
CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_REMOVE);
content::DownloadItem* file = GetDownloadByValue(args);
if (file)
file->Remove();
}
void DownloadsDOMHandler::HandleCancel(const base::ListValue* args) {
CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_CANCEL);
content::DownloadItem* file = GetDownloadByValue(args);
if (file)
file->Cancel(true);
}
void DownloadsDOMHandler::HandleClearAll(const base::ListValue* args) {
CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_CLEAR_ALL);
if (main_notifier_.GetManager())
main_notifier_.GetManager()->RemoveAllDownloads();
// If this is an incognito downloads page, clear All should clear main
// download manager as well.
if (original_notifier_.get() && original_notifier_->GetManager())
original_notifier_->GetManager()->RemoveAllDownloads();
}
void DownloadsDOMHandler::HandleOpenDownloadsFolder(
const base::ListValue* args) {
CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_OPEN_FOLDER);
if (main_notifier_.GetManager()) {
platform_util::OpenItem(DownloadPrefs::FromDownloadManager(
main_notifier_.GetManager())->DownloadPath());
}
}
// DownloadsDOMHandler, private: ----------------------------------------------
void DownloadsDOMHandler::ScheduleSendCurrentDownloads() {
// Don't call SendCurrentDownloads() every time anything changes. Batch them
// together instead. This may handle hundreds of OnDownloadDestroyed() calls
// in a single UI message loop iteration when the user Clears All downloads.
if (update_scheduled_)
return;
update_scheduled_ = true;
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&DownloadsDOMHandler::SendCurrentDownloads,
weak_ptr_factory_.GetWeakPtr()));
}
void DownloadsDOMHandler::SendCurrentDownloads() {
update_scheduled_ = false;
content::DownloadManager::DownloadVector all_items, filtered_items;
if (main_notifier_.GetManager()) {
main_notifier_.GetManager()->GetAllDownloads(&all_items);
main_notifier_.GetManager()->CheckForHistoryFilesRemoval();
}
if (original_notifier_.get() && original_notifier_->GetManager()) {
original_notifier_->GetManager()->GetAllDownloads(&all_items);
original_notifier_->GetManager()->CheckForHistoryFilesRemoval();
}
DownloadQuery query;
if (!search_text_.empty()) {
scoped_ptr<base::Value> query_text(base::Value::CreateStringValue(
search_text_));
query.AddFilter(DownloadQuery::FILTER_QUERY, *query_text.get());
}
query.AddFilter(base::Bind(&IsDownloadDisplayable));
query.AddSorter(DownloadQuery::SORT_START_TIME, DownloadQuery::DESCENDING);
query.Limit(kMaxDownloads);
query.Search(all_items.begin(), all_items.end(), &filtered_items);
base::ListValue results_value;
for (content::DownloadManager::DownloadVector::const_iterator
iter = filtered_items.begin(); iter != filtered_items.end(); ++iter) {
results_value.Append(CreateDownloadItemValue(
*iter,
(original_notifier_.get() &&
main_notifier_.GetManager() &&
(main_notifier_.GetManager()->GetDownload((*iter)->GetId()) ==
*iter))));
}
CallDownloadsList(results_value);
}
void DownloadsDOMHandler::ShowDangerPrompt(
content::DownloadItem* dangerous_item) {
DownloadDangerPrompt* danger_prompt = DownloadDangerPrompt::Create(
dangerous_item,
GetWebUIWebContents(),
false,
base::Bind(&DownloadsDOMHandler::DangerPromptAccepted,
weak_ptr_factory_.GetWeakPtr(), dangerous_item->GetId()),
base::Closure());
// danger_prompt will delete itself.
DCHECK(danger_prompt);
}
void DownloadsDOMHandler::DangerPromptAccepted(int download_id) {
content::DownloadItem* item = NULL;
if (main_notifier_.GetManager())
item = main_notifier_.GetManager()->GetDownload(download_id);
if (!item && original_notifier_.get() && original_notifier_->GetManager())
item = original_notifier_->GetManager()->GetDownload(download_id);
if (!item || (item->GetState() != content::DownloadItem::IN_PROGRESS))
return;
CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_SAVE_DANGEROUS);
item->DangerousDownloadValidated();
}
content::DownloadItem* DownloadsDOMHandler::GetDownloadByValue(
const base::ListValue* args) {
int download_id = -1;
if (!ExtractIntegerValue(args, &download_id))
return NULL;
content::DownloadItem* item = NULL;
if (main_notifier_.GetManager())
item = main_notifier_.GetManager()->GetDownload(download_id);
if (!item && original_notifier_.get() && original_notifier_->GetManager())
item = original_notifier_->GetManager()->GetDownload(download_id);
return item;
}
content::WebContents* DownloadsDOMHandler::GetWebUIWebContents() {
return web_ui()->GetWebContents();
}
void DownloadsDOMHandler::CallDownloadsList(const base::ListValue& downloads) {
web_ui()->CallJavascriptFunction("downloadsList", downloads);
}
void DownloadsDOMHandler::CallDownloadUpdated(
const base::ListValue& download_item) {
web_ui()->CallJavascriptFunction("downloadUpdated", download_item);
}
|