Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(721)

Unified Diff: chrome/browser/ui/webui/md_downloads/downloads_list_tracker.h

Issue 1428833005: MD Downloads: track downloads in C++, dispatch discrete JS updates (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: moar testz Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/webui/md_downloads/downloads_list_tracker.h
diff --git a/chrome/browser/ui/webui/md_downloads/downloads_list_tracker.h b/chrome/browser/ui/webui/md_downloads/downloads_list_tracker.h
new file mode 100644
index 0000000000000000000000000000000000000000..203d658922e11342628676aae395cb033e0bfa33
--- /dev/null
+++ b/chrome/browser/ui/webui/md_downloads/downloads_list_tracker.h
@@ -0,0 +1,125 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_UI_WEBUI_MD_DOWNLOADS_DOWNLOADS_LIST_TRACKER_H_
+#define CHROME_BROWSER_UI_WEBUI_MD_DOWNLOADS_DOWNLOADS_LIST_TRACKER_H_
+
+#include <set>
+
+#include "base/callback_forward.h"
+#include "base/macros.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/time/time.h"
+#include "base/values.h"
+#include "chrome/browser/download/all_download_item_notifier.h"
+#include "content/public/browser/download_item.h"
+
+namespace content {
+class DownloadManager;
+class WebUI;
+}
+
+// A class that tracks all downloads activity and keeps a sorted representation
+// of the downloads as chrome://downloads wants to display them.
+class DownloadsListTracker : public AllDownloadItemNotifier::Observer {
+ public:
+ DownloadsListTracker(content::DownloadManager* download_manager,
+ content::WebUI* web_ui);
+ ~DownloadsListTracker() override;
+
+ // Clears all downloads on the page (if it's ready).
+ void CallClearAll();
+
+ // Shows only downloads that match |search_terms|. nullptr or an empty list
+ // shows all downloads. Returns whether this class' search terms changed.
+ bool SetSearchTerms(const base::ListValue& search_terms);
+
+ // Sends a chunk of downloads and enables updates.
+ void Start();
+
+ // Stops sending updates to the page.
+ void Stop();
+
+ content::DownloadManager* GetMainNotifierManager() const;
+ content::DownloadManager* GetOriginalNotifierManager() const;
+
+ // AllDownloadItemNotifier::Observer:
+ void OnDownloadCreated(content::DownloadManager* manager,
+ content::DownloadItem* download_item) override;
+ void OnDownloadUpdated(content::DownloadManager* manager,
+ content::DownloadItem* download_item) override;
+ void OnDownloadRemoved(content::DownloadManager* manager,
+ content::DownloadItem* download_item) override;
+
+ protected:
+ // Testing constructor.
+ DownloadsListTracker(content::DownloadManager* download_manager,
+ content::WebUI* web_ui,
+ base::Callback<bool(const content::DownloadItem&)>);
+
+ // Creates a dictionary value that's sent to the page as JSON.
+ virtual scoped_ptr<base::DictionaryValue> CreateDownloadItemValue(
+ content::DownloadItem* item) const;
+
+ content::DownloadItem* GetItemForTesting(size_t index);
+
+ private:
+ struct StartTimeComparator {
+ bool operator() (const content::DownloadItem* a,
+ const content::DownloadItem* b) const;
+ };
+ using SortedSet = std::set<content::DownloadItem*, StartTimeComparator>;
+
+ // Called by both constructors to initialize common state.
+ void Init();
+
+ // Clears and re-inserts all visible items in a sorted order into
+ // |sorted_visible_items_|.
+ void RebuildSortedSet();
+
+ // Whether |item| should show on the current page.
+ bool ShouldShow(const content::DownloadItem& item) const;
+
+ // Gets a page index for |position| from |sorted_visible_items_|.
+ int GetIndex(const SortedSet::iterator& position) const;
+
+ // Inserts an item into |sorted_visible_items_| and calls "insertItems" if
+ // |sending_updates_|.
+ void InsertItem(const SortedSet::iterator& insert);
+
+ // Inserts from |start| to |end| into |sorted_visible_items_| and notifies the
+ // page if |sending_updates_|.
+ void InsertItems(const SortedSet::iterator& start,
+ const SortedSet::iterator& end);
+
+ // Calls "updateItem" if |sending_updates_|.
+ void CallUpdateItem(const SortedSet::iterator& update);
+
+ // Removes the item that corresponds to |remove| and sends a "removeItems"
+ // message to the page if |sending_updates_|. May modify |current_offset_|.
+ void RemoveItem(const SortedSet::iterator& remove);
+
+ AllDownloadItemNotifier main_notifier_;
+ scoped_ptr<AllDownloadItemNotifier> original_notifier_;
+
+ // The WebUI object corresponding to the page we care about.
+ content::WebUI* const web_ui_;
+
+ // Callback used to determine if an item should show on the page. Set to
+ // |ShouldShow()| in default constructor, passed in while testing.
+ base::Callback<bool(const content::DownloadItem&)> should_show_;
+
+ // When this is true, all changes to downloads that affect the page are send
+ // via JavaScript.
+ bool sending_updates_ = false;
+
+ SortedSet sorted_visible_items_;
+
+ // Current search terms.
+ base::ListValue search_terms_;
+
+ DISALLOW_COPY_AND_ASSIGN(DownloadsListTracker);
+};
+
+#endif // CHROME_BROWSER_UI_WEBUI_MD_DOWNLOADS_DOWNLOADS_LIST_TRACKER_H_

Powered by Google App Engine
This is Rietveld 408576698