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

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

Issue 2631933002: Adding status info to DownloadUIItem and piping it through. (Closed)
Patch Set: fix comment Created 3 years, 10 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 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 COMPONENTS_OFFLINE_PAGES_CORE_DOWNLOADS_DOWNLOAD_UI_ADAPTER_H_ 5 #ifndef COMPONENTS_OFFLINE_PAGES_CORE_DOWNLOADS_DOWNLOAD_UI_ADAPTER_H_
6 #define COMPONENTS_OFFLINE_PAGES_CORE_DOWNLOADS_DOWNLOAD_UI_ADAPTER_H_ 6 #define COMPONENTS_OFFLINE_PAGES_CORE_DOWNLOADS_DOWNLOAD_UI_ADAPTER_H_
7 7
8 #include <map> 8 #include <map>
9 #include <memory> 9 #include <memory>
10 #include <string> 10 #include <string>
11 #include <vector> 11 #include <vector>
12 12
13 #include "base/observer_list.h" 13 #include "base/observer_list.h"
14 #include "base/supports_user_data.h" 14 #include "base/supports_user_data.h"
15 #include "components/offline_pages/core/background/request_coordinator.h"
15 #include "components/offline_pages/core/downloads/download_ui_item.h" 16 #include "components/offline_pages/core/downloads/download_ui_item.h"
16 #include "components/offline_pages/core/offline_page_model.h" 17 #include "components/offline_pages/core/offline_page_model.h"
17 #include "components/offline_pages/core/offline_page_types.h" 18 #include "components/offline_pages/core/offline_page_types.h"
18 #include "url/gurl.h" 19 #include "url/gurl.h"
19 20
20 namespace offline_pages { 21 namespace offline_pages {
21 // C++ side of the UI Adapter. Mimics DownloadManager/Item/History (since we 22 // C++ side of the UI Adapter. Mimics DownloadManager/Item/History (since we
22 // share UI with Downloads). 23 // share UI with Downloads).
23 // An instance of this class is owned by OfflinePageModel and is shared between 24 // An instance of this class is owned by OfflinePageModel and is shared between
24 // UI components if needed. It manages the cache of DownloadUIItems, so after 25 // UI components if needed. It manages the cache of DownloadUIItems, so after
25 // initial load the UI components can synchronously pull the whoel list or any 26 // initial load the UI components can synchronously pull the whole list or any
26 // item by its guid. 27 // item by its guid.
28 // The items are exposed to UI layer (consumer of this class) as an observable
29 // collection of DownloadUIItems. The consumer is supposed to implement
30 // the DownloadUIAdapter::Observer interface. The creator of the adapter
31 // also passes in the Delegate that determines which items in the underlying
32 // OfflinePage backend are to be included (visible) in the collection.
27 class DownloadUIAdapter : public OfflinePageModel::Observer, 33 class DownloadUIAdapter : public OfflinePageModel::Observer,
34 public RequestCoordinator::Observer,
28 public base::SupportsUserData::Data { 35 public base::SupportsUserData::Data {
29 public: 36 public:
37 // Delegate, used to customize behavior of this Adapter.
38 class Delegate {
39 public:
40 virtual ~Delegate() = default;
41 // Returns true if the page or request with the specified Client Id should
42 // be visible in the collection of items exposed by this Adapter. This also
43 // indicates if Observers will be notified about changes for the given page.
44 virtual bool IsVisibleInUI(const ClientId& client_id) = 0;
45 // Sometimes the item should be in the collection but not visible in the UI,
46 // temporarily. This is a relatively special case, for example for Last_N
47 // snapshots that are only valid while their tab is alive. When the status
48 // of temporary visibility changes, the Delegate is supposed to call
49 // DownloadUIAdapter::TemporarilyHiddenStatusChanged().
50 virtual bool IsTemporarilyHiddenInUI(const ClientId& client_id) = 0;
51 };
52
30 // Observer, normally implemented by UI or a Bridge. 53 // Observer, normally implemented by UI or a Bridge.
31 class Observer { 54 class Observer {
32 public: 55 public:
33 // Invoked when UI items are loaded. GetAllItems/GetItem can now be used. 56 // Invoked when UI items are loaded. GetAllItems/GetItem can now be used.
34 // Must be listened for in order to start getting the items. 57 // Must be listened for in order to start getting the items.
35 // If the items are already loaded by the time observer is added, this 58 // If the items are already loaded by the time observer is added, this
36 // callback will be posted right away. 59 // callback will be posted right away.
37 virtual void ItemsLoaded() = 0; 60 virtual void ItemsLoaded() = 0;
38 61
39 // Invoked when the UI Item was added, usually as a request to download. 62 // Invoked when the UI Item was added, usually as a request to download.
40 virtual void ItemAdded(const DownloadUIItem& item) = 0; 63 virtual void ItemAdded(const DownloadUIItem& item) = 0;
41 64
42 // Invoked when the UI Item was updated. Only guid of the item is guaranteed 65 // Invoked when the UI Item was updated. Only guid of the item is guaranteed
43 // to survive the update, all other fields can change. 66 // to survive the update, all other fields can change.
44 virtual void ItemUpdated(const DownloadUIItem& item) = 0; 67 virtual void ItemUpdated(const DownloadUIItem& item) = 0;
45 68
46 // Invoked when the UI Item was deleted. At this point, only guid remains. 69 // Invoked when the UI Item was deleted. At this point, only guid remains.
47 virtual void ItemDeleted(const std::string& guid) = 0; 70 virtual void ItemDeleted(const std::string& guid) = 0;
48 71
49 protected: 72 protected:
50 virtual ~Observer() = default; 73 virtual ~Observer() = default;
51 }; 74 };
52 75
53 explicit DownloadUIAdapter(OfflinePageModel* model); 76 DownloadUIAdapter(OfflinePageModel* model,
77 RequestCoordinator* coordinator,
78 std::unique_ptr<Delegate> delegate);
54 ~DownloadUIAdapter() override; 79 ~DownloadUIAdapter() override;
55 80
56 static DownloadUIAdapter* FromOfflinePageModel(
57 OfflinePageModel* offline_page_model);
58
59 // Checks a client ID for proper namespace and ID format to be shown in the
60 // Downloads Home UI.
61 bool IsVisibleInUI(const ClientId& page);
62
63 // This adapter is potentially shared by UI elements, each of which adds 81 // This adapter is potentially shared by UI elements, each of which adds
64 // itself as an observer. 82 // itself as an observer.
65 // When the last observer si removed, cached list of items is destroyed and 83 // When the last observer is removed, cached list of items is destroyed and
66 // next time the initial loading will take longer. 84 // next time the initial loading will take longer.
67 void AddObserver(Observer* observer); 85 void AddObserver(Observer* observer);
68 void RemoveObserver(Observer* observer); 86 void RemoveObserver(Observer* observer);
69 87
70 // Returns all UI items. The list contains references to items in the cache 88 // Returns all UI items. The list contains references to items in the cache
71 // and has to be used synchronously. 89 // and has to be used synchronously.
72 std::vector<const DownloadUIItem*> GetAllItems() const; 90 std::vector<const DownloadUIItem*> GetAllItems() const;
73 // May return nullptr if item with specified guid does not exist. 91 // May return nullptr if item with specified guid does not exist.
74 const DownloadUIItem* GetItem(const std::string& guid) const; 92 const DownloadUIItem* GetItem(const std::string& guid) const;
75 93
76 // Commands from UI. Start async operations, result is observable 94 // Commands from UI. Start async operations, result is observable
77 // via Observer or directly by the user (as in 'open'). 95 // via Observer or directly by the user (as in 'open').
78 void DeleteItem(const std::string& guid); 96 void DeleteItem(const std::string& guid);
79 int64_t GetOfflineIdByGuid(const std::string& guid) const; 97 int64_t GetOfflineIdByGuid(const std::string& guid) const;
80 98
81 // OfflinePageModel::Observer 99 // OfflinePageModel::Observer
82 void OfflinePageModelLoaded(OfflinePageModel* model) override; 100 void OfflinePageModelLoaded(OfflinePageModel* model) override;
83 void OfflinePageAdded(OfflinePageModel* model, 101 void OfflinePageAdded(OfflinePageModel* model,
84 const OfflinePageItem& added_page) override; 102 const OfflinePageItem& added_page) override;
85 void OfflinePageDeleted(int64_t offline_id, 103 void OfflinePageDeleted(int64_t offline_id,
86 const ClientId& client_id) override; 104 const ClientId& client_id) override;
87 105
106 // RequestCoordinator::Observer
107 void OnAdded(const SavePageRequest& request) override;
108 void OnCompleted(const SavePageRequest& request,
109 RequestNotifier::BackgroundSavePageResult status) override;
110 void OnChanged(const SavePageRequest& request) override;
111
112 // For the DownloadUIAdapter::Delegate, to report the temporary hidden status
113 // change.
114 void TemporaryHiddenStatusChanged(const ClientId& client_id);
115
88 private: 116 private:
89 enum class State { NOT_LOADED, LOADING, LOADED }; 117 enum class State { NOT_LOADED, LOADING_PAGES, LOADING_REQUESTS, LOADED };
90 118
91 struct ItemInfo { 119 struct ItemInfo {
92 explicit ItemInfo(const OfflinePageItem& page); 120 explicit ItemInfo(const OfflinePageItem& page);
121 explicit ItemInfo(const SavePageRequest& request);
93 ~ItemInfo(); 122 ~ItemInfo();
94 123
95 std::unique_ptr<DownloadUIItem> ui_item; 124 std::unique_ptr<DownloadUIItem> ui_item;
125
96 // Additional cached data, not exposed to UI through DownloadUIItem. 126 // Additional cached data, not exposed to UI through DownloadUIItem.
127 // Indicates if this item wraps the completed page or in-progress request.
128 bool is_request;
129
130 // These are shared between pages and requests.
97 int64_t offline_id; 131 int64_t offline_id;
98 132
133 // ClientId is here to support the Delegate that can toggle temporary
134 // visibility of the items in the collection.
135 ClientId client_id;
136
137 // This item is present in the collection but temporarily hidden from UI.
138 // This is useful when unrelated reasons cause the UI item to be excluded
139 // (filtered out) from UI. When item becomes temporarily hidden the adapter
140 // issues ItemDeleted notification to observers, and ItemAdded when it
141 // becomes visible again.
142 bool temporarily_hidden;
143
99 private: 144 private:
100 DISALLOW_COPY_AND_ASSIGN(ItemInfo); 145 DISALLOW_COPY_AND_ASSIGN(ItemInfo);
101 }; 146 };
102 147
103 typedef std::map<std::string, std::unique_ptr<ItemInfo>> DownloadUIItems; 148 typedef std::map<std::string, std::unique_ptr<ItemInfo>> DownloadUIItems;
104 149
105 void LoadCache(); 150 void LoadCache();
106 void ClearCache(); 151 void ClearCache();
107 152
108 // Task callbacks. 153 // Task callbacks.
109 void OnOfflinePagesLoaded(const MultipleOfflinePageItemResult& pages); 154 void OnOfflinePagesLoaded(const MultipleOfflinePageItemResult& pages);
155 void OnRequestsLoaded(std::vector<std::unique_ptr<SavePageRequest>> requests);
156
110 void NotifyItemsLoaded(Observer* observer); 157 void NotifyItemsLoaded(Observer* observer);
111 void OnDeletePagesDone(DeletePageResult result); 158 void OnDeletePagesDone(DeletePageResult result);
112 159
160 void AddItemHelper(std::unique_ptr<ItemInfo> item_info);
161 void DeleteItemHelper(const std::string& guid);
162
113 // Always valid, this class is a member of the model. 163 // Always valid, this class is a member of the model.
114 OfflinePageModel* model_; 164 OfflinePageModel* model_;
115 165
166 // Always valid, a service.
167 RequestCoordinator* request_coordinator_;
168
169 // A delegate, supplied at construction.
170 std::unique_ptr<Delegate> delegate_;
171
116 State state_; 172 State state_;
117 173
118 // The cache of UI items. The key is DownloadUIItem.guid. 174 // The cache of UI items. The key is DownloadUIItem.guid.
119 DownloadUIItems items_; 175 DownloadUIItems items_;
120 176
121 // The observers. 177 // The observers.
122 base::ObserverList<Observer> observers_; 178 base::ObserverList<Observer> observers_;
123 int observers_count_; 179 int observers_count_;
124 180
125 base::WeakPtrFactory<DownloadUIAdapter> weak_ptr_factory_; 181 base::WeakPtrFactory<DownloadUIAdapter> weak_ptr_factory_;
126 182
127 DISALLOW_COPY_AND_ASSIGN(DownloadUIAdapter); 183 DISALLOW_COPY_AND_ASSIGN(DownloadUIAdapter);
128 }; 184 };
129 185
130 } // namespace offline_pages 186 } // namespace offline_pages
131 187
132 #endif // COMPONENTS_OFFLINE_PAGE_DOWNLOADS_DOWNLOAD_UI_ADAPTER_H_ 188 #endif // COMPONENTS_OFFLINE_PAGE_DOWNLOADS_DOWNLOAD_UI_ADAPTER_H_
OLDNEW
« no previous file with comments | « components/offline_pages/core/downloads/BUILD.gn ('k') | components/offline_pages/core/downloads/download_ui_adapter.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698