Chromium Code Reviews| 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" |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 70 DOWNLOADS_DOM_EVENT_SHOW = 5, | 70 DOWNLOADS_DOM_EVENT_SHOW = 5, |
| 71 DOWNLOADS_DOM_EVENT_PAUSE = 6, | 71 DOWNLOADS_DOM_EVENT_PAUSE = 6, |
| 72 DOWNLOADS_DOM_EVENT_REMOVE = 7, | 72 DOWNLOADS_DOM_EVENT_REMOVE = 7, |
| 73 DOWNLOADS_DOM_EVENT_CANCEL = 8, | 73 DOWNLOADS_DOM_EVENT_CANCEL = 8, |
| 74 DOWNLOADS_DOM_EVENT_CLEAR_ALL = 9, | 74 DOWNLOADS_DOM_EVENT_CLEAR_ALL = 9, |
| 75 DOWNLOADS_DOM_EVENT_OPEN_FOLDER = 10, | 75 DOWNLOADS_DOM_EVENT_OPEN_FOLDER = 10, |
| 76 DOWNLOADS_DOM_EVENT_RESUME = 11, | 76 DOWNLOADS_DOM_EVENT_RESUME = 11, |
| 77 DOWNLOADS_DOM_EVENT_MAX | 77 DOWNLOADS_DOM_EVENT_MAX |
| 78 }; | 78 }; |
| 79 | 79 |
| 80 static const char kKey[] = "DownloadsDOMHandlerData"; | |
| 81 | |
| 82 class DownloadsDOMHandlerData : public base::SupportsUserData::Data { | |
| 83 public: | |
| 84 static DownloadsDOMHandlerData* Get(content::DownloadItem* item) { | |
| 85 return static_cast<DownloadsDOMHandlerData*>(item->GetUserData(kKey)); | |
| 86 } | |
| 87 | |
| 88 static const DownloadsDOMHandlerData* Get(const content::DownloadItem* item) { | |
| 89 return static_cast<DownloadsDOMHandlerData*>(item->GetUserData(kKey)); | |
| 90 } | |
| 91 | |
| 92 static void Set(content::DownloadItem* item, DownloadsDOMHandlerData* data) { | |
| 93 item->SetUserData(kKey, data); | |
| 94 } | |
| 95 | |
| 96 static DownloadsDOMHandlerData* Create(content::DownloadItem* item) { | |
| 97 DownloadsDOMHandlerData* data = new DownloadsDOMHandlerData; | |
| 98 item->SetUserData(kKey, data); | |
| 99 return data; | |
| 100 } | |
| 101 | |
| 102 void set_is_removed(bool is_removed) { is_removed_ = is_removed; } | |
| 103 bool is_removed() const { return is_removed_; } | |
| 104 | |
| 105 private: | |
| 106 bool is_removed_; | |
| 107 }; | |
| 108 | |
| 109 void CountDownloadsDOMEvents(DownloadsDOMEvent event) { | 80 void CountDownloadsDOMEvents(DownloadsDOMEvent event) { |
| 110 UMA_HISTOGRAM_ENUMERATION("Download.DOMEvent", | 81 UMA_HISTOGRAM_ENUMERATION("Download.DOMEvent", |
| 111 event, | 82 event, |
| 112 DOWNLOADS_DOM_EVENT_MAX); | 83 DOWNLOADS_DOM_EVENT_MAX); |
| 113 } | 84 } |
| 114 | 85 |
| 115 // Returns a string constant to be used as the |danger_type| value in | 86 // Returns a string constant to be used as the |danger_type| value in |
| 116 // CreateDownloadItemValue(). Only return strings for DANGEROUS_FILE, | 87 // CreateDownloadItemValue(). Only return strings for DANGEROUS_FILE, |
| 117 // DANGEROUS_URL, DANGEROUS_CONTENT, and UNCOMMON_CONTENT because the | 88 // DANGEROUS_URL, DANGEROUS_CONTENT, and UNCOMMON_CONTENT because the |
| 118 // |danger_type| value is only defined if the value of |state| is |DANGEROUS|. | 89 // |danger_type| value is only defined if the value of |state| is |DANGEROUS|. |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 260 file_value->SetString("state", "COMPLETE"); | 231 file_value->SetString("state", "COMPLETE"); |
| 261 break; | 232 break; |
| 262 | 233 |
| 263 case content::DownloadItem::MAX_DOWNLOAD_STATE: | 234 case content::DownloadItem::MAX_DOWNLOAD_STATE: |
| 264 NOTREACHED(); | 235 NOTREACHED(); |
| 265 } | 236 } |
| 266 | 237 |
| 267 return file_value; | 238 return file_value; |
| 268 } | 239 } |
| 269 | 240 |
| 270 bool IsRemoved(const content::DownloadItem& item) { | |
| 271 const DownloadsDOMHandlerData* data = DownloadsDOMHandlerData::Get(&item); | |
| 272 return data && data->is_removed(); | |
| 273 } | |
| 274 | |
| 275 // Filters out extension downloads and downloads that don't have a filename yet. | 241 // Filters out extension downloads and downloads that don't have a filename yet. |
| 276 bool IsDownloadDisplayable(const content::DownloadItem& item) { | 242 bool IsDownloadDisplayable(const content::DownloadItem& item) { |
| 277 return !download_crx_util::IsExtensionDownload(item) && | 243 return !download_crx_util::IsExtensionDownload(item) && |
| 278 !item.IsTemporary() && | 244 !item.IsTemporary() && |
| 279 !item.GetFileNameToReportUser().empty() && | 245 !item.GetFileNameToReportUser().empty() && |
| 280 !item.GetTargetFilePath().empty() && | 246 !item.GetTargetFilePath().empty() && |
| 281 !IsRemoved(item); | 247 DownloadItemModel( |
| 248 const_cast<content::DownloadItem*>(&item)).ShouldShowInShelf(); | |
|
Dan Beam
2015/03/02 18:50:09
open to suggestions if you don't like this
| |
| 282 } | 249 } |
| 283 | 250 |
| 284 } // namespace | 251 } // namespace |
| 285 | 252 |
| 286 DownloadsDOMHandler::DownloadsDOMHandler(content::DownloadManager* dlm) | 253 DownloadsDOMHandler::DownloadsDOMHandler(content::DownloadManager* dlm) |
| 287 : main_notifier_(dlm, this), | 254 : main_notifier_(dlm, this), |
| 288 update_scheduled_(false), | 255 update_scheduled_(false), |
| 289 weak_ptr_factory_(this) { | 256 weak_ptr_factory_(this) { |
| 290 // Create our fileicon data source. | 257 // Create our fileicon data source. |
| 291 Profile* profile = Profile::FromBrowserContext(dlm->GetBrowserContext()); | 258 Profile* profile = Profile::FromBrowserContext(dlm->GetBrowserContext()); |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 391 (manager == main_notifier_.GetManager())))); | 358 (manager == main_notifier_.GetManager())))); |
| 392 CallDownloadUpdated(results_value); | 359 CallDownloadUpdated(results_value); |
| 393 } else { | 360 } else { |
| 394 ScheduleSendCurrentDownloads(); | 361 ScheduleSendCurrentDownloads(); |
| 395 } | 362 } |
| 396 } | 363 } |
| 397 | 364 |
| 398 void DownloadsDOMHandler::OnDownloadRemoved( | 365 void DownloadsDOMHandler::OnDownloadRemoved( |
| 399 content::DownloadManager* manager, | 366 content::DownloadManager* manager, |
| 400 content::DownloadItem* download_item) { | 367 content::DownloadItem* download_item) { |
| 401 if (IsRemoved(*download_item)) | 368 if (!DownloadItemModel(download_item).ShouldShowInShelf()) |
| 402 return; | 369 return; |
| 403 | 370 |
| 404 // This relies on |download_item| being removed from DownloadManager in this | 371 // This relies on |download_item| being removed from DownloadManager in this |
| 405 // MessageLoop iteration. |download_item| may not have been removed from | 372 // MessageLoop iteration. |download_item| may not have been removed from |
| 406 // DownloadManager when OnDownloadRemoved() is fired, so bounce off the | 373 // DownloadManager when OnDownloadRemoved() is fired, so bounce off the |
| 407 // MessageLoop to give it a chance to be removed. SendCurrentDownloads() looks | 374 // MessageLoop to give it a chance to be removed. SendCurrentDownloads() looks |
| 408 // at all downloads, and we do not tell it that |download_item| is being | 375 // at all downloads, and we do not tell it that |download_item| is being |
| 409 // removed. If DownloadManager is ever changed to not immediately remove | 376 // removed. If DownloadManager is ever changed to not immediately remove |
| 410 // |download_item| from its map when OnDownloadRemoved is sent, then | 377 // |download_item| from its map when OnDownloadRemoved is sent, then |
| 411 // DownloadsDOMHandler::OnDownloadRemoved() will need to explicitly tell | 378 // DownloadsDOMHandler::OnDownloadRemoved() will need to explicitly tell |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 506 if (removes_.empty()) | 473 if (removes_.empty()) |
| 507 return; | 474 return; |
| 508 | 475 |
| 509 const std::set<uint32> last_removed_ids = removes_.back(); | 476 const std::set<uint32> last_removed_ids = removes_.back(); |
| 510 removes_.pop_back(); | 477 removes_.pop_back(); |
| 511 | 478 |
| 512 for (auto id : last_removed_ids) { | 479 for (auto id : last_removed_ids) { |
| 513 content::DownloadItem* download = GetDownloadById(id); | 480 content::DownloadItem* download = GetDownloadById(id); |
| 514 if (!download) | 481 if (!download) |
| 515 continue; | 482 continue; |
| 516 DownloadsDOMHandlerData::Set(download, nullptr); | 483 DownloadItemModel(download).SetShouldShowInShelf(true); |
| 517 download->UpdateObservers(); | 484 download->UpdateObservers(); |
| 518 } | 485 } |
| 519 } | 486 } |
| 520 | 487 |
| 521 void DownloadsDOMHandler::HandleCancel(const base::ListValue* args) { | 488 void DownloadsDOMHandler::HandleCancel(const base::ListValue* args) { |
| 522 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_CANCEL); | 489 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_CANCEL); |
| 523 content::DownloadItem* file = GetDownloadByValue(args); | 490 content::DownloadItem* file = GetDownloadByValue(args); |
| 524 if (file) | 491 if (file) |
| 525 file->Cancel(true); | 492 file->Cancel(true); |
| 526 } | 493 } |
| 527 | 494 |
| 528 void DownloadsDOMHandler::HandleClearAll(const base::ListValue* args) { | 495 void DownloadsDOMHandler::HandleClearAll(const base::ListValue* args) { |
| 529 if (!IsDeletingHistoryAllowed()) | 496 if (!IsDeletingHistoryAllowed()) |
| 530 return; | 497 return; |
| 531 | 498 |
| 532 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_CLEAR_ALL); | 499 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_CLEAR_ALL); |
| 533 | 500 |
| 534 std::vector<content::DownloadItem*> downloads; | 501 std::vector<content::DownloadItem*> downloads; |
| 535 if (GetMainNotifierManager()) | 502 if (GetMainNotifierManager()) |
| 536 GetMainNotifierManager()->GetAllDownloads(&downloads); | 503 GetMainNotifierManager()->GetAllDownloads(&downloads); |
| 537 if (original_notifier_ && original_notifier_->GetManager()) | 504 if (original_notifier_ && original_notifier_->GetManager()) |
| 538 original_notifier_->GetManager()->GetAllDownloads(&downloads); | 505 original_notifier_->GetManager()->GetAllDownloads(&downloads); |
| 539 RemoveDownloads(downloads); | 506 RemoveDownloads(downloads); |
| 540 } | 507 } |
| 541 | 508 |
| 542 void DownloadsDOMHandler::RemoveDownloads( | 509 void DownloadsDOMHandler::RemoveDownloads( |
| 543 const std::vector<content::DownloadItem*>& to_remove) { | 510 const std::vector<content::DownloadItem*>& to_remove) { |
| 544 std::set<uint32> ids; | 511 std::set<uint32> ids; |
| 545 for (auto* download : to_remove) { | 512 for (auto* download : to_remove) { |
| 546 if (IsRemoved(*download) || | 513 DownloadItemModel item_model(download); |
| 514 if (!item_model.ShouldShowInShelf() || | |
| 547 download->GetState() == content::DownloadItem::IN_PROGRESS) { | 515 download->GetState() == content::DownloadItem::IN_PROGRESS) { |
| 548 continue; | 516 continue; |
| 549 } | 517 } |
| 550 | 518 |
| 551 DownloadsDOMHandlerData::Create(download)->set_is_removed(true); | 519 item_model.SetShouldShowInShelf(false); |
| 552 ids.insert(download->GetId()); | 520 ids.insert(download->GetId()); |
| 553 download->UpdateObservers(); | 521 download->UpdateObservers(); |
| 554 } | 522 } |
| 555 removes_.push_back(ids); | 523 removes_.push_back(ids); |
| 556 } | 524 } |
| 557 | 525 |
| 558 void DownloadsDOMHandler::HandleOpenDownloadsFolder( | 526 void DownloadsDOMHandler::HandleOpenDownloadsFolder( |
| 559 const base::ListValue* args) { | 527 const base::ListValue* args) { |
| 560 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_OPEN_FOLDER); | 528 CountDownloadsDOMEvents(DOWNLOADS_DOM_EVENT_OPEN_FOLDER); |
| 561 content::DownloadManager* manager = main_notifier_.GetManager(); | 529 content::DownloadManager* manager = main_notifier_.GetManager(); |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 682 } | 650 } |
| 683 | 651 |
| 684 void DownloadsDOMHandler::CallDownloadsList(const base::ListValue& downloads) { | 652 void DownloadsDOMHandler::CallDownloadsList(const base::ListValue& downloads) { |
| 685 web_ui()->CallJavascriptFunction("downloadsList", downloads); | 653 web_ui()->CallJavascriptFunction("downloadsList", downloads); |
| 686 } | 654 } |
| 687 | 655 |
| 688 void DownloadsDOMHandler::CallDownloadUpdated( | 656 void DownloadsDOMHandler::CallDownloadUpdated( |
| 689 const base::ListValue& download_item) { | 657 const base::ListValue& download_item) { |
| 690 web_ui()->CallJavascriptFunction("downloadUpdated", download_item); | 658 web_ui()->CallJavascriptFunction("downloadUpdated", download_item); |
| 691 } | 659 } |
| OLD | NEW |