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 // Clears all downloads on the page if currently sending updates and resets |
38 void CallClearAll(); | 38 // chunk tracking data. |
| 39 void Reset(); |
39 | 40 |
40 // Shows only downloads that match |search_terms|. An empty list shows all | 41 // This class only cares about downloads that match |search_terms|. |
41 // downloads. Returns whether |search_terms.Equals(&search_terms_)|. | 42 // An empty list shows all downloads (the default). Returns true if |
| 43 // |search_terms| differ from the current ones. |
42 bool SetSearchTerms(const base::ListValue& search_terms); | 44 bool SetSearchTerms(const base::ListValue& search_terms); |
43 | 45 |
44 // Sends all downloads and enables updates. | 46 // Starts sending updates and sends a capped amount of downloads. Tracks which |
45 void Start(); | 47 // downloads have been sent. Re-call this to send more. |
| 48 void StartAndSendChunk(); |
46 | 49 |
47 // Stops sending updates to the page. | 50 // Stops sending updates to the page. |
48 void Stop(); | 51 void Stop(); |
49 | 52 |
50 content::DownloadManager* GetMainNotifierManager() const; | 53 content::DownloadManager* GetMainNotifierManager() const; |
51 content::DownloadManager* GetOriginalNotifierManager() const; | 54 content::DownloadManager* GetOriginalNotifierManager() const; |
52 | 55 |
53 // AllDownloadItemNotifier::Observer: | 56 // AllDownloadItemNotifier::Observer: |
54 void OnDownloadCreated(content::DownloadManager* manager, | 57 void OnDownloadCreated(content::DownloadManager* manager, |
55 content::DownloadItem* download_item) override; | 58 content::DownloadItem* download_item) override; |
(...skipping 10 matching lines...) Expand all Loading... |
66 | 69 |
67 // Creates a dictionary value that's sent to the page as JSON. | 70 // Creates a dictionary value that's sent to the page as JSON. |
68 virtual scoped_ptr<base::DictionaryValue> CreateDownloadItemValue( | 71 virtual scoped_ptr<base::DictionaryValue> CreateDownloadItemValue( |
69 content::DownloadItem* item) const; | 72 content::DownloadItem* item) const; |
70 | 73 |
71 // Exposed for testing. | 74 // Exposed for testing. |
72 bool IsIncognito(const content::DownloadItem& item) const; | 75 bool IsIncognito(const content::DownloadItem& item) const; |
73 | 76 |
74 const content::DownloadItem* GetItemForTesting(size_t index) const; | 77 const content::DownloadItem* GetItemForTesting(size_t index) const; |
75 | 78 |
| 79 void SetChunkSizeForTesting(size_t chunk_size); |
| 80 |
76 private: | 81 private: |
77 struct StartTimeComparator { | 82 struct StartTimeComparator { |
78 bool operator() (const content::DownloadItem* a, | 83 bool operator() (const content::DownloadItem* a, |
79 const content::DownloadItem* b) const; | 84 const content::DownloadItem* b) const; |
80 }; | 85 }; |
81 using SortedSet = std::set<content::DownloadItem*, StartTimeComparator>; | 86 using SortedSet = std::set<content::DownloadItem*, StartTimeComparator>; |
82 | 87 |
83 // Called by both constructors to initialize common state. | 88 // Called by both constructors to initialize common state. |
84 void Init(); | 89 void Init(); |
85 | 90 |
86 // Clears and re-inserts all visible items in a sorted order into | 91 // Clears and re-inserts all downloads items into |sorted_items_|. |
87 // |sorted_visible_items_|. | 92 void RebuildSortedItems(); |
88 void RebuildSortedSet(); | |
89 | 93 |
90 // Whether |item| should show on the current page. | 94 // Whether |item| should show on the current page. |
91 bool ShouldShow(const content::DownloadItem& item) const; | 95 bool ShouldShow(const content::DownloadItem& item) const; |
92 | 96 |
93 // Gets a page index for |position| from |sorted_visible_items_|. | 97 // Returns the index of |item| in |sorted_items_|. |
94 int GetIndex(const SortedSet::iterator& position) const; | 98 size_t GetIndex(const SortedSet::iterator& item) const; |
95 | 99 |
96 // Calls "insertItems" if |sending_updates_|. | 100 // Calls "insertItems" if sending updates and the page knows about |insert|. |
97 void CallInsertItem(const SortedSet::iterator& insert); | 101 void InsertItem(const SortedSet::iterator& insert); |
98 | 102 |
99 // Calls "updateItem" if |sending_updates_|. | 103 // Calls "updateItem" if sending updates and the page knows about |update|. |
100 void CallUpdateItem(const SortedSet::iterator& update); | 104 void UpdateItem(const SortedSet::iterator& update); |
101 | 105 |
102 // Removes the item that corresponds to |remove| and sends a "removeItems" | 106 // Removes the item that corresponds to |remove| and sends "removeItems" |
103 // message to the page if |sending_updates_|. | 107 // if sending updates. |
104 void RemoveItem(const SortedSet::iterator& remove); | 108 void RemoveItem(const SortedSet::iterator& remove); |
105 | 109 |
106 AllDownloadItemNotifier main_notifier_; | 110 AllDownloadItemNotifier main_notifier_; |
107 scoped_ptr<AllDownloadItemNotifier> original_notifier_; | 111 scoped_ptr<AllDownloadItemNotifier> original_notifier_; |
108 | 112 |
109 // The WebUI object corresponding to the page we care about. | 113 // The WebUI object corresponding to the page we care about. |
110 content::WebUI* const web_ui_; | 114 content::WebUI* const web_ui_; |
111 | 115 |
112 // Callback used to determine if an item should show on the page. Set to | 116 // Callback used to determine if an item should show on the page. Set to |
113 // |ShouldShow()| in default constructor, passed in while testing. | 117 // |ShouldShow()| in default constructor, passed in while testing. |
114 base::Callback<bool(const content::DownloadItem&)> should_show_; | 118 base::Callback<bool(const content::DownloadItem&)> should_show_; |
115 | 119 |
116 // When this is true, all changes to downloads that affect the page are sent | 120 // When this is true, all changes to downloads that affect the page are sent |
117 // via JavaScript. | 121 // via JavaScript. |
118 bool sending_updates_ = false; | 122 bool sending_updates_ = false; |
119 | 123 |
120 SortedSet sorted_visible_items_; | 124 SortedSet sorted_items_; |
| 125 |
| 126 // The number of items sent to the page so far. |
| 127 size_t sent_to_page_ = 0u; |
| 128 |
| 129 // The maximum number of items sent to the page at a time. |
| 130 size_t chunk_size_ = 20u; |
121 | 131 |
122 // Current search terms. | 132 // Current search terms. |
123 std::vector<base::string16> search_terms_; | 133 std::vector<base::string16> search_terms_; |
124 | 134 |
125 DISALLOW_COPY_AND_ASSIGN(DownloadsListTracker); | 135 DISALLOW_COPY_AND_ASSIGN(DownloadsListTracker); |
126 }; | 136 }; |
127 | 137 |
128 #endif // CHROME_BROWSER_UI_WEBUI_MD_DOWNLOADS_DOWNLOADS_LIST_TRACKER_H_ | 138 #endif // CHROME_BROWSER_UI_WEBUI_MD_DOWNLOADS_DOWNLOADS_LIST_TRACKER_H_ |
OLD | NEW |