OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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. |
| 4 |
| 5 #ifndef CHROME_BROWSER_UI_WEBUI_MD_DOWNLOADS_DOWNLOADS_LIST_TRACKER_H_ |
| 6 #define CHROME_BROWSER_UI_WEBUI_MD_DOWNLOADS_DOWNLOADS_LIST_TRACKER_H_ |
| 7 |
| 8 #include <set> |
| 9 |
| 10 #include "base/callback_forward.h" |
| 11 #include "base/macros.h" |
| 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/strings/string16.h" |
| 14 #include "base/time/time.h" |
| 15 #include "base/values.h" |
| 16 #include "chrome/browser/download/all_download_item_notifier.h" |
| 17 #include "content/public/browser/download_item.h" |
| 18 |
| 19 namespace base { |
| 20 class DictionaryValue; |
| 21 class ListValue; |
| 22 } |
| 23 |
| 24 namespace content { |
| 25 class DownloadManager; |
| 26 class WebUI; |
| 27 } |
| 28 |
| 29 // A class that tracks all downloads activity and keeps a sorted representation |
| 30 // of the downloads as chrome://downloads wants to display them. |
| 31 class DownloadsListTracker : public AllDownloadItemNotifier::Observer { |
| 32 public: |
| 33 DownloadsListTracker(content::DownloadManager* download_manager, |
| 34 content::WebUI* web_ui); |
| 35 ~DownloadsListTracker() override; |
| 36 |
| 37 // Clears all downloads on the page (if it's ready). |
| 38 void CallClearAll(); |
| 39 |
| 40 // Shows only downloads that match |search_terms|. An empty list shows all |
| 41 // downloads. Returns whether |search_terms.Equals(&search_terms_)|. |
| 42 bool SetSearchTerms(const base::ListValue& search_terms); |
| 43 |
| 44 // Sends all downloads and enables updates. |
| 45 void Start(); |
| 46 |
| 47 // Stops sending updates to the page. |
| 48 void Stop(); |
| 49 |
| 50 content::DownloadManager* GetMainNotifierManager() const; |
| 51 content::DownloadManager* GetOriginalNotifierManager() const; |
| 52 |
| 53 // AllDownloadItemNotifier::Observer: |
| 54 void OnDownloadCreated(content::DownloadManager* manager, |
| 55 content::DownloadItem* download_item) override; |
| 56 void OnDownloadUpdated(content::DownloadManager* manager, |
| 57 content::DownloadItem* download_item) override; |
| 58 void OnDownloadRemoved(content::DownloadManager* manager, |
| 59 content::DownloadItem* download_item) override; |
| 60 |
| 61 protected: |
| 62 // Testing constructor. |
| 63 DownloadsListTracker(content::DownloadManager* download_manager, |
| 64 content::WebUI* web_ui, |
| 65 base::Callback<bool(const content::DownloadItem&)>); |
| 66 |
| 67 // Creates a dictionary value that's sent to the page as JSON. |
| 68 virtual scoped_ptr<base::DictionaryValue> CreateDownloadItemValue( |
| 69 content::DownloadItem* item) const; |
| 70 |
| 71 const content::DownloadItem* GetItemForTesting(size_t index) const; |
| 72 |
| 73 private: |
| 74 struct StartTimeComparator { |
| 75 bool operator() (const content::DownloadItem* a, |
| 76 const content::DownloadItem* b) const; |
| 77 }; |
| 78 using SortedSet = std::set<content::DownloadItem*, StartTimeComparator>; |
| 79 |
| 80 // Called by both constructors to initialize common state. |
| 81 void Init(); |
| 82 |
| 83 // Clears and re-inserts all visible items in a sorted order into |
| 84 // |sorted_visible_items_|. |
| 85 void RebuildSortedSet(); |
| 86 |
| 87 // Whether |item| should show on the current page. |
| 88 bool ShouldShow(const content::DownloadItem& item) const; |
| 89 |
| 90 // Gets a page index for |position| from |sorted_visible_items_|. |
| 91 int GetIndex(const SortedSet::iterator& position) const; |
| 92 |
| 93 // Calls "insertItems" if |sending_updates_|. |
| 94 void CallInsertItem(const SortedSet::iterator& insert); |
| 95 |
| 96 // Calls "updateItem" if |sending_updates_|. |
| 97 void CallUpdateItem(const SortedSet::iterator& update); |
| 98 |
| 99 // Removes the item that corresponds to |remove| and sends a "removeItems" |
| 100 // message to the page if |sending_updates_|. |
| 101 void RemoveItem(const SortedSet::iterator& remove); |
| 102 |
| 103 AllDownloadItemNotifier main_notifier_; |
| 104 scoped_ptr<AllDownloadItemNotifier> original_notifier_; |
| 105 |
| 106 // The WebUI object corresponding to the page we care about. |
| 107 content::WebUI* const web_ui_; |
| 108 |
| 109 // Callback used to determine if an item should show on the page. Set to |
| 110 // |ShouldShow()| in default constructor, passed in while testing. |
| 111 base::Callback<bool(const content::DownloadItem&)> should_show_; |
| 112 |
| 113 // When this is true, all changes to downloads that affect the page are sent |
| 114 // via JavaScript. |
| 115 bool sending_updates_ = false; |
| 116 |
| 117 SortedSet sorted_visible_items_; |
| 118 |
| 119 // Current search terms. |
| 120 std::vector<base::string16> search_terms_; |
| 121 |
| 122 DISALLOW_COPY_AND_ASSIGN(DownloadsListTracker); |
| 123 }; |
| 124 |
| 125 #endif // CHROME_BROWSER_UI_WEBUI_MD_DOWNLOADS_DOWNLOADS_LIST_TRACKER_H_ |
OLD | NEW |