| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/ui/webui/downloads_dom_handler.h" | 5 #include "chrome/browser/ui/webui/downloads_dom_handler.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <functional> | 8 #include <functional> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
| 13 #include "base/i18n/rtl.h" | 13 #include "base/i18n/rtl.h" |
| 14 #include "base/i18n/time_formatting.h" | 14 #include "base/i18n/time_formatting.h" |
| 15 #include "base/memory/singleton.h" | 15 #include "base/memory/singleton.h" |
| 16 #include "base/metrics/histogram.h" | 16 #include "base/metrics/histogram.h" |
| 17 #include "base/string_piece.h" | 17 #include "base/string_piece.h" |
| 18 #include "base/threading/thread.h" | 18 #include "base/threading/thread.h" |
| 19 #include "base/utf_string_conversions.h" | 19 #include "base/utf_string_conversions.h" |
| 20 #include "base/value_conversions.h" | 20 #include "base/value_conversions.h" |
| 21 #include "base/values.h" | 21 #include "base/values.h" |
| 22 #include "chrome/browser/browser_process.h" | 22 #include "chrome/browser/browser_process.h" |
| 23 #include "chrome/browser/download/download_crx_util.h" | 23 #include "chrome/browser/download/download_crx_util.h" |
| 24 #include "chrome/browser/download/download_danger_prompt.h" | 24 #include "chrome/browser/download/download_danger_prompt.h" |
| 25 #include "chrome/browser/download/download_history.h" | 25 #include "chrome/browser/download/download_history.h" |
| 26 #include "chrome/browser/download/download_item_model.h" | 26 #include "chrome/browser/download/download_item_model.h" |
| 27 #include "chrome/browser/download/download_prefs.h" | 27 #include "chrome/browser/download/download_prefs.h" |
| 28 #include "chrome/browser/download/download_query.h" |
| 28 #include "chrome/browser/download/download_service.h" | 29 #include "chrome/browser/download/download_service.h" |
| 29 #include "chrome/browser/download/download_service_factory.h" | 30 #include "chrome/browser/download/download_service_factory.h" |
| 30 #include "chrome/browser/download/download_util.h" | 31 #include "chrome/browser/download/download_util.h" |
| 31 #include "chrome/browser/platform_util.h" | 32 #include "chrome/browser/platform_util.h" |
| 32 #include "chrome/browser/profiles/profile.h" | 33 #include "chrome/browser/profiles/profile.h" |
| 33 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 34 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 34 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" | 35 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" |
| 35 #include "chrome/browser/ui/webui/fileicon_source.h" | 36 #include "chrome/browser/ui/webui/fileicon_source.h" |
| 36 #include "chrome/common/time_format.h" | 37 #include "chrome/common/time_format.h" |
| 37 #include "chrome/common/url_constants.h" | 38 #include "chrome/common/url_constants.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 54 using content::BrowserContext; | 55 using content::BrowserContext; |
| 55 using content::BrowserThread; | 56 using content::BrowserThread; |
| 56 using content::UserMetricsAction; | 57 using content::UserMetricsAction; |
| 57 | 58 |
| 58 namespace { | 59 namespace { |
| 59 | 60 |
| 60 // Maximum number of downloads to show. TODO(glen): Remove this and instead | 61 // Maximum number of downloads to show. TODO(glen): Remove this and instead |
| 61 // stuff the downloads down the pipe slowly. | 62 // stuff the downloads down the pipe slowly. |
| 62 static const size_t kMaxDownloads = 150; | 63 static const size_t kMaxDownloads = 150; |
| 63 | 64 |
| 64 // Sorts DownloadItems into descending order by their start time. | |
| 65 class DownloadItemSorter : public std::binary_function<content::DownloadItem*, | |
| 66 content::DownloadItem*, | |
| 67 bool> { | |
| 68 public: | |
| 69 bool operator()(const content::DownloadItem* lhs, | |
| 70 const content::DownloadItem* rhs) { | |
| 71 return lhs->GetStartTime() > rhs->GetStartTime(); | |
| 72 } | |
| 73 }; | |
| 74 | |
| 75 enum DownloadsDOMEvent { | 65 enum DownloadsDOMEvent { |
| 76 DOWNLOADS_DOM_EVENT_GET_DOWNLOADS = 0, | 66 DOWNLOADS_DOM_EVENT_GET_DOWNLOADS = 0, |
| 77 DOWNLOADS_DOM_EVENT_OPEN_FILE = 1, | 67 DOWNLOADS_DOM_EVENT_OPEN_FILE = 1, |
| 78 DOWNLOADS_DOM_EVENT_DRAG = 2, | 68 DOWNLOADS_DOM_EVENT_DRAG = 2, |
| 79 DOWNLOADS_DOM_EVENT_SAVE_DANGEROUS = 3, | 69 DOWNLOADS_DOM_EVENT_SAVE_DANGEROUS = 3, |
| 80 DOWNLOADS_DOM_EVENT_DISCARD_DANGEROUS = 4, | 70 DOWNLOADS_DOM_EVENT_DISCARD_DANGEROUS = 4, |
| 81 DOWNLOADS_DOM_EVENT_SHOW = 5, | 71 DOWNLOADS_DOM_EVENT_SHOW = 5, |
| 82 DOWNLOADS_DOM_EVENT_PAUSE = 6, | 72 DOWNLOADS_DOM_EVENT_PAUSE = 6, |
| 83 DOWNLOADS_DOM_EVENT_REMOVE = 7, | 73 DOWNLOADS_DOM_EVENT_REMOVE = 7, |
| 84 DOWNLOADS_DOM_EVENT_CANCEL = 8, | 74 DOWNLOADS_DOM_EVENT_CANCEL = 8, |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 void DownloadsDOMHandler::OnDownloadCreated( | 274 void DownloadsDOMHandler::OnDownloadCreated( |
| 285 content::DownloadManager* manager, content::DownloadItem* download_item) { | 275 content::DownloadManager* manager, content::DownloadItem* download_item) { |
| 286 if (IsDownloadDisplayable(*download_item)) | 276 if (IsDownloadDisplayable(*download_item)) |
| 287 ScheduleSendCurrentDownloads(); | 277 ScheduleSendCurrentDownloads(); |
| 288 } | 278 } |
| 289 | 279 |
| 290 void DownloadsDOMHandler::OnDownloadUpdated( | 280 void DownloadsDOMHandler::OnDownloadUpdated( |
| 291 content::DownloadManager* manager, | 281 content::DownloadManager* manager, |
| 292 content::DownloadItem* download_item) { | 282 content::DownloadItem* download_item) { |
| 293 if (IsDownloadDisplayable(*download_item)) { | 283 if (IsDownloadDisplayable(*download_item)) { |
| 284 if (!search_text_.empty()) { |
| 285 // Don't CallDownloadUpdated() if download_item doesn't match |
| 286 // search_text_. |
| 287 // TODO(benjhayden): Consider splitting MatchesQuery() out to a function. |
| 288 content::DownloadManager::DownloadVector all_items, filtered_items; |
| 289 all_items.push_back(download_item); |
| 290 DownloadQuery query; |
| 291 scoped_ptr<base::Value> query_text(base::Value::CreateStringValue( |
| 292 search_text_)); |
| 293 query.AddFilter(DownloadQuery::FILTER_QUERY, *query_text.get()); |
| 294 query.Search(all_items.begin(), all_items.end(), &filtered_items); |
| 295 if (filtered_items.empty()) |
| 296 return; |
| 297 } |
| 294 base::ListValue results_value; | 298 base::ListValue results_value; |
| 295 results_value.Append(CreateDownloadItemValue( | 299 results_value.Append(CreateDownloadItemValue( |
| 296 download_item, | 300 download_item, |
| 297 (original_notifier_.get() && | 301 (original_notifier_.get() && |
| 298 (manager == main_notifier_.GetManager())))); | 302 (manager == main_notifier_.GetManager())))); |
| 299 CallDownloadUpdated(results_value); | 303 CallDownloadUpdated(results_value); |
| 300 } | 304 } |
| 301 } | 305 } |
| 302 | 306 |
| 303 void DownloadsDOMHandler::OnDownloadRemoved( | 307 void DownloadsDOMHandler::OnDownloadRemoved( |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 372 void DownloadsDOMHandler::HandlePause(const base::ListValue* args) { | 376 void DownloadsDOMHandler::HandlePause(const base::ListValue* args) { |
| 373 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_PAUSE); | 377 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_PAUSE); |
| 374 content::DownloadItem* file = GetDownloadByValue(args); | 378 content::DownloadItem* file = GetDownloadByValue(args); |
| 375 if (file) | 379 if (file) |
| 376 file->TogglePause(); | 380 file->TogglePause(); |
| 377 } | 381 } |
| 378 | 382 |
| 379 void DownloadsDOMHandler::HandleRemove(const base::ListValue* args) { | 383 void DownloadsDOMHandler::HandleRemove(const base::ListValue* args) { |
| 380 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_REMOVE); | 384 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_REMOVE); |
| 381 content::DownloadItem* file = GetDownloadByValue(args); | 385 content::DownloadItem* file = GetDownloadByValue(args); |
| 382 if (file) { | 386 if (file) |
| 383 DCHECK(file->IsPersisted()); | |
| 384 file->Remove(); | 387 file->Remove(); |
| 385 } | |
| 386 } | 388 } |
| 387 | 389 |
| 388 void DownloadsDOMHandler::HandleCancel(const base::ListValue* args) { | 390 void DownloadsDOMHandler::HandleCancel(const base::ListValue* args) { |
| 389 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_CANCEL); | 391 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_CANCEL); |
| 390 content::DownloadItem* file = GetDownloadByValue(args); | 392 content::DownloadItem* file = GetDownloadByValue(args); |
| 391 if (file) | 393 if (file) |
| 392 file->Cancel(true); | 394 file->Cancel(true); |
| 393 } | 395 } |
| 394 | 396 |
| 395 void DownloadsDOMHandler::HandleClearAll(const base::ListValue* args) { | 397 void DownloadsDOMHandler::HandleClearAll(const base::ListValue* args) { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 422 return; | 424 return; |
| 423 update_scheduled_ = true; | 425 update_scheduled_ = true; |
| 424 BrowserThread::PostTask( | 426 BrowserThread::PostTask( |
| 425 BrowserThread::UI, FROM_HERE, | 427 BrowserThread::UI, FROM_HERE, |
| 426 base::Bind(&DownloadsDOMHandler::SendCurrentDownloads, | 428 base::Bind(&DownloadsDOMHandler::SendCurrentDownloads, |
| 427 weak_ptr_factory_.GetWeakPtr())); | 429 weak_ptr_factory_.GetWeakPtr())); |
| 428 } | 430 } |
| 429 | 431 |
| 430 void DownloadsDOMHandler::SendCurrentDownloads() { | 432 void DownloadsDOMHandler::SendCurrentDownloads() { |
| 431 update_scheduled_ = false; | 433 update_scheduled_ = false; |
| 432 content::DownloadManager::DownloadVector downloads; | 434 content::DownloadManager::DownloadVector all_items, filtered_items; |
| 433 SearchDownloads(&downloads); | 435 if (main_notifier_.GetManager()) |
| 434 sort(downloads.begin(), downloads.end(), DownloadItemSorter()); | 436 main_notifier_.GetManager()->GetAllDownloads(&all_items); |
| 437 if (original_notifier_.get() && original_notifier_->GetManager()) |
| 438 original_notifier_->GetManager()->GetAllDownloads(&all_items); |
| 439 DownloadQuery query; |
| 440 if (!search_text_.empty()) { |
| 441 scoped_ptr<base::Value> query_text(base::Value::CreateStringValue( |
| 442 search_text_)); |
| 443 query.AddFilter(DownloadQuery::FILTER_QUERY, *query_text.get()); |
| 444 } |
| 445 query.AddFilter(base::Bind(&IsDownloadDisplayable)); |
| 446 query.AddSorter(DownloadQuery::SORT_START_TIME, DownloadQuery::DESCENDING); |
| 447 query.Limit(kMaxDownloads); |
| 448 query.Search(all_items.begin(), all_items.end(), &filtered_items); |
| 435 base::ListValue results_value; | 449 base::ListValue results_value; |
| 436 for (content::DownloadManager::DownloadVector::const_iterator | 450 for (content::DownloadManager::DownloadVector::const_iterator |
| 437 iter = downloads.begin(); | 451 iter = filtered_items.begin(); iter != filtered_items.end(); ++iter) { |
| 438 iter != downloads.end(); ++iter) { | 452 results_value.Append(CreateDownloadItemValue( |
| 439 if (IsDownloadDisplayable(**iter)) { | 453 *iter, |
| 440 results_value.Append(CreateDownloadItemValue( | 454 (original_notifier_.get() && |
| 441 *iter, | 455 main_notifier_.GetManager() && |
| 442 (original_notifier_.get() && | 456 (main_notifier_.GetManager()->GetDownload((*iter)->GetId()) == |
| 443 main_notifier_.GetManager() && | 457 *iter)))); |
| 444 (main_notifier_.GetManager()->GetDownload((*iter)->GetId()) == | |
| 445 *iter)))); | |
| 446 } | |
| 447 if (results_value.GetSize() == kMaxDownloads) | |
| 448 break; | |
| 449 } | 458 } |
| 450 CallDownloadsList(results_value); | 459 CallDownloadsList(results_value); |
| 451 } | 460 } |
| 452 | 461 |
| 453 void DownloadsDOMHandler::SearchDownloads( | |
| 454 content::DownloadManager::DownloadVector* downloads) { | |
| 455 if (main_notifier_.GetManager()) | |
| 456 main_notifier_.GetManager()->SearchDownloads(search_text_, downloads); | |
| 457 if (original_notifier_.get() && original_notifier_->GetManager()) | |
| 458 original_notifier_->GetManager()->SearchDownloads(search_text_, downloads); | |
| 459 } | |
| 460 | |
| 461 void DownloadsDOMHandler::ShowDangerPrompt( | 462 void DownloadsDOMHandler::ShowDangerPrompt( |
| 462 content::DownloadItem* dangerous_item) { | 463 content::DownloadItem* dangerous_item) { |
| 463 DownloadDangerPrompt* danger_prompt = DownloadDangerPrompt::Create( | 464 DownloadDangerPrompt* danger_prompt = DownloadDangerPrompt::Create( |
| 464 dangerous_item, | 465 dangerous_item, |
| 465 TabContents::FromWebContents(GetWebUIWebContents()), | 466 TabContents::FromWebContents(GetWebUIWebContents()), |
| 466 base::Bind(&DownloadsDOMHandler::DangerPromptAccepted, | 467 base::Bind(&DownloadsDOMHandler::DangerPromptAccepted, |
| 467 weak_ptr_factory_.GetWeakPtr(), dangerous_item->GetId()), | 468 weak_ptr_factory_.GetWeakPtr(), dangerous_item->GetId()), |
| 468 base::Closure()); | 469 base::Closure()); |
| 469 // danger_prompt will delete itself. | 470 // danger_prompt will delete itself. |
| 470 DCHECK(danger_prompt); | 471 DCHECK(danger_prompt); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 500 } | 501 } |
| 501 | 502 |
| 502 void DownloadsDOMHandler::CallDownloadsList(const base::ListValue& downloads) { | 503 void DownloadsDOMHandler::CallDownloadsList(const base::ListValue& downloads) { |
| 503 web_ui()->CallJavascriptFunction("downloadsList", downloads); | 504 web_ui()->CallJavascriptFunction("downloadsList", downloads); |
| 504 } | 505 } |
| 505 | 506 |
| 506 void DownloadsDOMHandler::CallDownloadUpdated( | 507 void DownloadsDOMHandler::CallDownloadUpdated( |
| 507 const base::ListValue& download_item) { | 508 const base::ListValue& download_item) { |
| 508 web_ui()->CallJavascriptFunction("downloadUpdated", download_item); | 509 web_ui()->CallJavascriptFunction("downloadUpdated", download_item); |
| 509 } | 510 } |
| OLD | NEW |