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

Side by Side Diff: components/offline_pages/downloads/download_ui_adapter.h

Issue 2176013002: Add support for OfflinePagesDownloadBridge. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 5 months 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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 COMPONENTS_OFFLINE_PAGE_DOWNLOADS_DOWNLOAD_UI_ADAPTER_H_
6 #define COMPONENTS_OFFLINE_PAGE_DOWNLOADS_DOWNLOAD_UI_ADAPTER_H_
7
8 #include "base/observer_list.h"
9 #include "components/offline_pages/downloads/download_ui_item.h"
10 #include "components/offline_pages/offline_page_model.h"
11 #include "components/offline_pages/offline_page_types.h"
12
13 namespace offline_pages {
14
15 // Key is DownloadUIItem.guid.
16 typedef
17 std::map<std::string, std::unique_ptr<DownloadUIItem>> DownloadUIItemsMap;
18
19 // C++ side of the UI Adapter. Mimics DownloadManager/Item/History (since we
20 // share UI with Downloads).
21 // An instance of this class is owned by OfflinePageModel and is shared between
22 // UI components if needed. It manages the cache of DownloadUIItems, so after
23 // initial load the UI components can synchronously pull the whoel list or any
24 // item by its guid.
25 class DownloadUIAdapter : public OfflinePageModel::Observer {
26 public:
27 // Observer, normally implemented by UI or a Bridge.
28 class Observer {
29 public:
30 // Invoked when UI items are loaded. GetAllItems/GetItem can now be used.
31 // Must be listened for in order to start getting the items.
32 // If the items are already loaded by the time observer is added, this
33 // callback will be posted right away.
34 virtual void ItemsLoaded() = 0;
35
36 // Invoked when the UI Item was added, usually as a request to download.
37 virtual void ItemAdded(const DownloadUIItem& item) = 0;
38
39 // Invoked when the UI Item was updated. Only guid of the item is guaranteed
40 // to survive the update, all other fields can change.
41 virtual void ItemUpdated(const DownloadUIItem& item) = 0;
42
43 // Invoked when the UI Item was deleted. At this point, only guid remains.
44 virtual void ItemDeleted(std::string guid) = 0;
45
46 protected:
47 virtual ~Observer() = default;
48 };
49
50 DownloadUIAdapter(OfflinePageModel* model);
fgorski 2016/07/25 17:09:57 Make this constructor explicit, as it accepts only
Dmitry Titov 2016/07/26 00:41:25 Done.
51 ~DownloadUIAdapter() override;
52
53 // This adapter is potentially shared by UI elements, each of which adds
54 // itself as an observer.
55 // When the last observer si removed, cached list of items is destroyed and
56 // next time the initial loading will take longer.
57 void AddObserver(Observer* observer);
58 void RemoveObserver(Observer* observer);
59
60 const DownloadUIItemsMap& GetAllItems() const;
61 // May return nullptr.
62 const DownloadUIItem* GetItem(std::string guid) const;
fgorski 2016/07/25 17:09:56 const std::string& guid
Dmitry Titov 2016/07/26 00:41:25 Done.
63
64 // OfflinePageModel::Observer
65 void OfflinePageModelLoaded(OfflinePageModel* model) override;
66 void OfflinePageModelChanged(OfflinePageModel* model) override;
67 void OfflinePageDeleted(int64_t offline_id,
68 const ClientId& client_id) override;
69
70 private:
71
fgorski 2016/07/25 17:09:56 nit: empty line not needed.
Dmitry Titov 2016/07/26 00:41:25 Done.
72 void LoadCache();
73 void ClearCache();
74
75 // Task callbacks.
76 void OnOfflinePagesLoaded(const MultipleOfflinePageItemResult& pages);
77 void NotifyOnLoad(Observer* observer);
fgorski 2016/07/25 17:09:57 perhaps rename: NotifyItemsLoaded?
Dmitry Titov 2016/07/26 00:41:25 Done.
78 void OnOfflinePagesChanged(const MultipleOfflinePageItemResult& pages);
79 bool IsVisibleInUI(const OfflinePageItem& page);
80
81 // Always valid, this class is a member of the model.
82 OfflinePageModel* model_;
83
84 bool is_loaded_;
85
86 // The cache of UI items.
87 DownloadUIItemsMap items_;
88
89 // The observers.
90 base::ObserverList<Observer> observers_;
91
92 base::WeakPtrFactory<DownloadUIAdapter> weak_ptr_factory_;
93
94 DISALLOW_COPY_AND_ASSIGN(DownloadUIAdapter);
95 };
96
97 } // namespace offline_pages
98
99 #endif // COMPONENTS_OFFLINE_PAGE_DOWNLOADS_DOWNLOAD_UI_ADAPTER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698