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); |
| 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 // Inserts an item into |sorted_visible_items_| and calls "insertItems" if |
| 88 // |sending_updates_|. |
| 89 void InsertItem(const SortedSet::iterator& insert); |
| 90 |
| 91 // Inserts from |start| to |end| into |sorted_visible_items_| and notifies the |
| 92 // page if |sending_updates_|. |
| 93 void InsertItems(const SortedSet::iterator& start, |
| 94 const SortedSet::iterator& end); |
| 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_|. May modify |current_offset_|. |
| 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 send |
| 114 // via JavaScript. |
| 115 bool sending_updates_ = false; |
| 116 |
| 117 SortedSet sorted_visible_items_; |
| 118 |
| 119 // Current search terms. |
| 120 base::ListValue 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 |