OLD | NEW |
---|---|
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 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::OnTemporaryHidenStatusChanged(). | |
dewittj
2017/01/18 19:24:07
s/Temporary/Temporarily/
s/Hiden/Hidden
Dmitry Titov
2017/01/27 04:26:24
Done.
| |
50 virtual bool IsTemporaryHiddenInUI(const ClientId& client_id) = 0; | |
dewittj
2017/01/18 19:24:07
s/Temporary/Temporarily/
Dmitry Titov
2017/01/27 04:26:24
Done.
| |
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 explicit DownloadUIAdapter(OfflinePageModel* model, |
fgorski
2017/01/20 17:03:01
nit: remove explicit
Dmitry Titov
2017/01/27 04:26:24
Done.
| |
77 RequestCoordinator* coordinator, | |
78 std::unique_ptr<Delegate> delegate); | |
54 ~DownloadUIAdapter() override; | 79 ~DownloadUIAdapter() override; |
55 | 80 |
56 static DownloadUIAdapter* FromOfflinePageModel( | 81 // This adapter is potentially shared by UI elements, each of which adds |
fgorski
2017/01/20 17:03:01
nit: git cl format. something is off with spaces.
Dmitry Titov
2017/01/27 04:26:24
Done.
| |
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 | |
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 si 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 OnTemporaryHidenStatusChanged(const ClientId& client_id, bool hidden); | |
dewittj
2017/01/18 19:24:07
s/Hiden/Hidden
Dmitry Titov
2017/01/27 04:26:24
Done.
| |
115 | |
88 private: | 116 private: |
89 enum class State { NOT_LOADED, LOADING, LOADED }; | 117 enum class State { |
118 NOT_LOADED, | |
119 LOADING_PAGES, | |
120 LOADING_REQUESTS, | |
121 LOADED | |
122 }; | |
90 | 123 |
91 struct ItemInfo { | 124 struct ItemInfo { |
92 explicit ItemInfo(const OfflinePageItem& page); | 125 explicit ItemInfo(const OfflinePageItem& page); |
126 explicit ItemInfo(const SavePageRequest& request); | |
93 ~ItemInfo(); | 127 ~ItemInfo(); |
94 | 128 |
95 std::unique_ptr<DownloadUIItem> ui_item; | 129 std::unique_ptr<DownloadUIItem> ui_item; |
130 | |
96 // Additional cached data, not exposed to UI through DownloadUIItem. | 131 // Additional cached data, not exposed to UI through DownloadUIItem. |
132 // Indicates if this item wraps the completed page or in-progress request. | |
133 bool is_request; | |
134 | |
135 // These are shared between pages and requests. | |
97 int64_t offline_id; | 136 int64_t offline_id; |
98 | 137 |
138 // ClientId is here to support the Delegate that can toggle temporary | |
139 // visibility of the items in the collection. | |
140 ClientId client_id; | |
141 | |
142 // This item is present in the collection but temporarily hidden from UI. | |
143 // This is useful when unrelated reasons cause the UI item to be excluded | |
144 // (filtered out) from UI. When item becomes temporarily hidden the adapter | |
145 // issues ItemDeleted notification to observers, and ItemAdded when it | |
146 // becomes visible again. | |
147 bool temporarily_hidden; | |
148 | |
99 private: | 149 private: |
100 DISALLOW_COPY_AND_ASSIGN(ItemInfo); | 150 DISALLOW_COPY_AND_ASSIGN(ItemInfo); |
101 }; | 151 }; |
102 | 152 |
103 typedef std::map<std::string, std::unique_ptr<ItemInfo>> DownloadUIItems; | 153 typedef std::map<std::string, std::unique_ptr<ItemInfo>> DownloadUIItems; |
104 | 154 |
105 void LoadCache(); | 155 void LoadCache(); |
106 void ClearCache(); | 156 void ClearCache(); |
107 | 157 |
108 // Task callbacks. | 158 // Task callbacks. |
109 void OnOfflinePagesLoaded(const MultipleOfflinePageItemResult& pages); | 159 void OnOfflinePagesLoaded(const MultipleOfflinePageItemResult& pages); |
160 void OnRequestsLoaded(std::vector<std::unique_ptr<SavePageRequest>> requests); | |
161 | |
110 void NotifyItemsLoaded(Observer* observer); | 162 void NotifyItemsLoaded(Observer* observer); |
111 void OnDeletePagesDone(DeletePageResult result); | 163 void OnDeletePagesDone(DeletePageResult result); |
112 | 164 |
165 void addItemHelper(std::unique_ptr<ItemInfo> item_info); | |
fgorski
2017/01/20 17:03:02
AddItemHelper
Dmitry Titov
2017/01/27 04:26:24
Done.
| |
166 void deleteItemHelper(const std::string& guid); | |
fgorski
2017/01/20 17:03:02
DeleteItemHelper
Dmitry Titov
2017/01/27 04:26:24
Done.
| |
167 | |
113 // Always valid, this class is a member of the model. | 168 // Always valid, this class is a member of the model. |
114 OfflinePageModel* model_; | 169 OfflinePageModel* model_; |
115 | 170 |
171 // Always valid, a service. | |
172 RequestCoordinator* request_coordinator_; | |
173 | |
174 // A delegate, supplied at construction. | |
175 std::unique_ptr<Delegate> delegate_; | |
176 | |
116 State state_; | 177 State state_; |
117 | 178 |
118 // The cache of UI items. The key is DownloadUIItem.guid. | 179 // The cache of UI items. The key is DownloadUIItem.guid. |
119 DownloadUIItems items_; | 180 DownloadUIItems items_; |
120 | 181 |
121 // The observers. | 182 // The observers. |
122 base::ObserverList<Observer> observers_; | 183 base::ObserverList<Observer> observers_; |
123 int observers_count_; | 184 int observers_count_; |
124 | 185 |
125 base::WeakPtrFactory<DownloadUIAdapter> weak_ptr_factory_; | 186 base::WeakPtrFactory<DownloadUIAdapter> weak_ptr_factory_; |
126 | 187 |
127 DISALLOW_COPY_AND_ASSIGN(DownloadUIAdapter); | 188 DISALLOW_COPY_AND_ASSIGN(DownloadUIAdapter); |
128 }; | 189 }; |
129 | 190 |
130 } // namespace offline_pages | 191 } // namespace offline_pages |
131 | 192 |
132 #endif // COMPONENTS_OFFLINE_PAGE_DOWNLOADS_DOWNLOAD_UI_ADAPTER_H_ | 193 #endif // COMPONENTS_OFFLINE_PAGE_DOWNLOADS_DOWNLOAD_UI_ADAPTER_H_ |
OLD | NEW |