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