| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef CHROME_BROWSER_UI_WEBUI_MD_DOWNLOADS_DOWNLOADS_LIST_TRACKER_H_ | 5 #ifndef CHROME_BROWSER_UI_WEBUI_MD_DOWNLOADS_DOWNLOADS_LIST_TRACKER_H_ |
| 6 #define CHROME_BROWSER_UI_WEBUI_MD_DOWNLOADS_DOWNLOADS_LIST_TRACKER_H_ | 6 #define CHROME_BROWSER_UI_WEBUI_MD_DOWNLOADS_DOWNLOADS_LIST_TRACKER_H_ |
| 7 | 7 |
| 8 #include <set> | 8 #include <set> |
| 9 | 9 |
| 10 #include "base/callback_forward.h" | 10 #include "base/callback_forward.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 } | 27 } |
| 28 | 28 |
| 29 // A class that tracks all downloads activity and keeps a sorted representation | 29 // A class that tracks all downloads activity and keeps a sorted representation |
| 30 // of the downloads as chrome://downloads wants to display them. | 30 // of the downloads as chrome://downloads wants to display them. |
| 31 class DownloadsListTracker : public AllDownloadItemNotifier::Observer { | 31 class DownloadsListTracker : public AllDownloadItemNotifier::Observer { |
| 32 public: | 32 public: |
| 33 DownloadsListTracker(content::DownloadManager* download_manager, | 33 DownloadsListTracker(content::DownloadManager* download_manager, |
| 34 content::WebUI* web_ui); | 34 content::WebUI* web_ui); |
| 35 ~DownloadsListTracker() override; | 35 ~DownloadsListTracker() override; |
| 36 | 36 |
| 37 // Clears all downloads on the page (if it's ready). | 37 // This class only cares about downloads that match |search_terms|. |
| 38 void CallClearAll(); | 38 // An empty list shows all downloads (the default). Returns true if |
| 39 | 39 // |search_terms| differ from the current ones. |
| 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); | 40 bool SetSearchTerms(const base::ListValue& search_terms); |
| 43 | 41 |
| 44 // Sends all downloads and enables updates. | 42 // Starts sending updates and sends a capped amount of downloads. Tracks which |
| 45 void Start(); | 43 // downloads have been sent. Re-call this to send more. |
| 44 void StartAndSendChunk(); |
| 46 | 45 |
| 47 // Stops sending updates to the page. | 46 // Clears all downloads on the page if currently sending updates, resets chunk |
| 48 void Stop(); | 47 // tracking data, and stops sending updates. |
| 48 void Reset(); |
| 49 | 49 |
| 50 content::DownloadManager* GetMainNotifierManager() const; | 50 content::DownloadManager* GetMainNotifierManager() const; |
| 51 content::DownloadManager* GetOriginalNotifierManager() const; | 51 content::DownloadManager* GetOriginalNotifierManager() const; |
| 52 | 52 |
| 53 // AllDownloadItemNotifier::Observer: | 53 // AllDownloadItemNotifier::Observer: |
| 54 void OnDownloadCreated(content::DownloadManager* manager, | 54 void OnDownloadCreated(content::DownloadManager* manager, |
| 55 content::DownloadItem* download_item) override; | 55 content::DownloadItem* download_item) override; |
| 56 void OnDownloadUpdated(content::DownloadManager* manager, | 56 void OnDownloadUpdated(content::DownloadManager* manager, |
| 57 content::DownloadItem* download_item) override; | 57 content::DownloadItem* download_item) override; |
| 58 void OnDownloadRemoved(content::DownloadManager* manager, | 58 void OnDownloadRemoved(content::DownloadManager* manager, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 73 private: | 73 private: |
| 74 struct StartTimeComparator { | 74 struct StartTimeComparator { |
| 75 bool operator() (const content::DownloadItem* a, | 75 bool operator() (const content::DownloadItem* a, |
| 76 const content::DownloadItem* b) const; | 76 const content::DownloadItem* b) const; |
| 77 }; | 77 }; |
| 78 using SortedSet = std::set<content::DownloadItem*, StartTimeComparator>; | 78 using SortedSet = std::set<content::DownloadItem*, StartTimeComparator>; |
| 79 | 79 |
| 80 // Called by both constructors to initialize common state. | 80 // Called by both constructors to initialize common state. |
| 81 void Init(); | 81 void Init(); |
| 82 | 82 |
| 83 // Clears and re-inserts all visible items in a sorted order into | 83 // Clears and re-inserts all downloads items into |sorted_items_|. |
| 84 // |sorted_visible_items_|. | 84 void RebuildSortedItems(); |
| 85 void RebuildSortedSet(); | |
| 86 | 85 |
| 87 // Whether |item| should show on the current page. | 86 // Whether |item| should show on the current page. |
| 88 bool ShouldShow(const content::DownloadItem& item) const; | 87 bool ShouldShow(const content::DownloadItem& item) const; |
| 89 | 88 |
| 90 // Gets a page index for |position| from |sorted_visible_items_|. | 89 // Returns the index of |item| in |sorted_items_|. |
| 91 int GetIndex(const SortedSet::iterator& position) const; | 90 size_t GetIndex(const SortedSet::iterator& item) const; |
| 92 | 91 |
| 93 // Calls "insertItems" if |sending_updates_|. | 92 // Calls "insertItems" if sending updates and the page knows about |insert|. |
| 94 void CallInsertItem(const SortedSet::iterator& insert); | 93 void InsertItem(const SortedSet::iterator& insert); |
| 95 | 94 |
| 96 // Calls "updateItem" if |sending_updates_|. | 95 // Calls "updateItem" if sending updates and the page knows about |update|. |
| 97 void CallUpdateItem(const SortedSet::iterator& update); | 96 void UpdateItem(const SortedSet::iterator& update); |
| 98 | 97 |
| 99 // Removes the item that corresponds to |remove| and sends a "removeItems" | 98 // Removes the item that corresponds to |remove| and sends "removeItems" |
| 100 // message to the page if |sending_updates_|. | 99 // if sending updates. |
| 101 void RemoveItem(const SortedSet::iterator& remove); | 100 void RemoveItem(const SortedSet::iterator& remove); |
| 102 | 101 |
| 103 AllDownloadItemNotifier main_notifier_; | 102 AllDownloadItemNotifier main_notifier_; |
| 104 scoped_ptr<AllDownloadItemNotifier> original_notifier_; | 103 scoped_ptr<AllDownloadItemNotifier> original_notifier_; |
| 105 | 104 |
| 106 // The WebUI object corresponding to the page we care about. | 105 // The WebUI object corresponding to the page we care about. |
| 107 content::WebUI* const web_ui_; | 106 content::WebUI* const web_ui_; |
| 108 | 107 |
| 109 // Callback used to determine if an item should show on the page. Set to | 108 // Callback used to determine if an item should show on the page. Set to |
| 110 // |ShouldShow()| in default constructor, passed in while testing. | 109 // |ShouldShow()| in default constructor, passed in while testing. |
| 111 base::Callback<bool(const content::DownloadItem&)> should_show_; | 110 base::Callback<bool(const content::DownloadItem&)> should_show_; |
| 112 | 111 |
| 113 // When this is true, all changes to downloads that affect the page are sent | 112 // When this is true, all changes to downloads that affect the page are sent |
| 114 // via JavaScript. | 113 // via JavaScript. |
| 115 bool sending_updates_ = false; | 114 bool sending_updates_ = false; |
| 116 | 115 |
| 117 SortedSet sorted_visible_items_; | 116 SortedSet sorted_items_; |
| 117 |
| 118 // The number of items sent to the page so far. |
| 119 size_t sent_to_page_ = 0u; |
| 118 | 120 |
| 119 // Current search terms. | 121 // Current search terms. |
| 120 std::vector<base::string16> search_terms_; | 122 std::vector<base::string16> search_terms_; |
| 121 | 123 |
| 122 DISALLOW_COPY_AND_ASSIGN(DownloadsListTracker); | 124 DISALLOW_COPY_AND_ASSIGN(DownloadsListTracker); |
| 123 }; | 125 }; |
| 124 | 126 |
| 125 #endif // CHROME_BROWSER_UI_WEBUI_MD_DOWNLOADS_DOWNLOADS_LIST_TRACKER_H_ | 127 #endif // CHROME_BROWSER_UI_WEBUI_MD_DOWNLOADS_DOWNLOADS_LIST_TRACKER_H_ |
| OLD | NEW |