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

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: Created 3 years, 11 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 whoel list or any
26 // item by its guid. 27 // item by its guid.
27 class DownloadUIAdapter : public OfflinePageModel::Observer, 28 class DownloadUIAdapter : public OfflinePageModel::Observer,
29 public RequestCoordinator::Observer,
28 public base::SupportsUserData::Data { 30 public base::SupportsUserData::Data {
29 public: 31 public:
32 // Delegate, used to customize behavior of this Adapter.
33 class Delegate {
34 public:
35 virtual ~Delegate() = default;
36 // Returns true if the page or request with the specified Client Id should
37 // be visible in the collection of items exposed by this Adapter. This also
38 // indicates if Observers will be notified about changes for the given page.
39 virtual bool IsVisibleInUI(const ClientId& client_id) = 0;
dewittj 2017/01/18 18:49:48 Eventually I will need a way to ask the DownloadUI
Dmitry Titov 2017/01/27 04:30:22 Done.
40 };
41
30 // Observer, normally implemented by UI or a Bridge. 42 // Observer, normally implemented by UI or a Bridge.
31 class Observer { 43 class Observer {
32 public: 44 public:
33 // Invoked when UI items are loaded. GetAllItems/GetItem can now be used. 45 // Invoked when UI items are loaded. GetAllItems/GetItem can now be used.
34 // Must be listened for in order to start getting the items. 46 // 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 47 // If the items are already loaded by the time observer is added, this
36 // callback will be posted right away. 48 // callback will be posted right away.
37 virtual void ItemsLoaded() = 0; 49 virtual void ItemsLoaded() = 0;
38 50
39 // Invoked when the UI Item was added, usually as a request to download. 51 // Invoked when the UI Item was added, usually as a request to download.
40 virtual void ItemAdded(const DownloadUIItem& item) = 0; 52 virtual void ItemAdded(const DownloadUIItem& item) = 0;
41 53
42 // Invoked when the UI Item was updated. Only guid of the item is guaranteed 54 // Invoked when the UI Item was updated. Only guid of the item is guaranteed
43 // to survive the update, all other fields can change. 55 // to survive the update, all other fields can change.
44 virtual void ItemUpdated(const DownloadUIItem& item) = 0; 56 virtual void ItemUpdated(const DownloadUIItem& item) = 0;
45 57
46 // Invoked when the UI Item was deleted. At this point, only guid remains. 58 // Invoked when the UI Item was deleted. At this point, only guid remains.
47 virtual void ItemDeleted(const std::string& guid) = 0; 59 virtual void ItemDeleted(const std::string& guid) = 0;
48 60
49 protected: 61 protected:
50 virtual ~Observer() = default; 62 virtual ~Observer() = default;
51 }; 63 };
52 64
53 explicit DownloadUIAdapter(OfflinePageModel* model); 65 explicit DownloadUIAdapter(OfflinePageModel* model,
66 RequestCoordinator* coordinator,
67 std::unique_ptr<Delegate> delegate);
54 ~DownloadUIAdapter() override; 68 ~DownloadUIAdapter() override;
55 69
56 static DownloadUIAdapter* FromOfflinePageModel( 70 // This adapter is potentially shared by UI elements, each of which adds
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. 71 // itself as an observer.
65 // When the last observer si removed, cached list of items is destroyed and 72 // When the last observer si removed, cached list of items is destroyed and
66 // next time the initial loading will take longer. 73 // next time the initial loading will take longer.
67 void AddObserver(Observer* observer); 74 void AddObserver(Observer* observer);
68 void RemoveObserver(Observer* observer); 75 void RemoveObserver(Observer* observer);
69 76
70 // Returns all UI items. The list contains references to items in the cache 77 // Returns all UI items. The list contains references to items in the cache
71 // and has to be used synchronously. 78 // and has to be used synchronously.
72 std::vector<const DownloadUIItem*> GetAllItems() const; 79 std::vector<const DownloadUIItem*> GetAllItems() const;
73 // May return nullptr if item with specified guid does not exist. 80 // May return nullptr if item with specified guid does not exist.
74 const DownloadUIItem* GetItem(const std::string& guid) const; 81 const DownloadUIItem* GetItem(const std::string& guid) const;
75 82
76 // Commands from UI. Start async operations, result is observable 83 // Commands from UI. Start async operations, result is observable
77 // via Observer or directly by the user (as in 'open'). 84 // via Observer or directly by the user (as in 'open').
78 void DeleteItem(const std::string& guid); 85 void DeleteItem(const std::string& guid);
79 int64_t GetOfflineIdByGuid(const std::string& guid) const; 86 int64_t GetOfflineIdByGuid(const std::string& guid) const;
80 87
81 // OfflinePageModel::Observer 88 // OfflinePageModel::Observer
82 void OfflinePageModelLoaded(OfflinePageModel* model) override; 89 void OfflinePageModelLoaded(OfflinePageModel* model) override;
83 void OfflinePageAdded(OfflinePageModel* model, 90 void OfflinePageAdded(OfflinePageModel* model,
84 const OfflinePageItem& added_page) override; 91 const OfflinePageItem& added_page) override;
85 void OfflinePageDeleted(int64_t offline_id, 92 void OfflinePageDeleted(int64_t offline_id,
86 const ClientId& client_id) override; 93 const ClientId& client_id) override;
87 94
95 // RequestCoordinator::Observer
96 void OnAdded(const SavePageRequest& request) override;
97 void OnCompleted(const SavePageRequest& request,
98 RequestNotifier::BackgroundSavePageResult status) override;
99 void OnChanged(const SavePageRequest& request) override;
100
88 private: 101 private:
89 enum class State { NOT_LOADED, LOADING, LOADED }; 102 enum class State {
103 NOT_LOADED,
104 LOADING_PAGES,
105 LOADING_REQUESTS,
106 LOADED
107 };
90 108
91 struct ItemInfo { 109 struct ItemInfo {
92 explicit ItemInfo(const OfflinePageItem& page); 110 explicit ItemInfo(const OfflinePageItem& page);
111 explicit ItemInfo(const SavePageRequest& request);
93 ~ItemInfo(); 112 ~ItemInfo();
94 113
95 std::unique_ptr<DownloadUIItem> ui_item; 114 std::unique_ptr<DownloadUIItem> ui_item;
115
96 // Additional cached data, not exposed to UI through DownloadUIItem. 116 // Additional cached data, not exposed to UI through DownloadUIItem.
117 // Indicates if this item wraps the completed page or in-progress request.
118 bool is_request;
119
120 // These are shared between pages and requests.
97 int64_t offline_id; 121 int64_t offline_id;
98 122
99 private: 123 private:
100 DISALLOW_COPY_AND_ASSIGN(ItemInfo); 124 DISALLOW_COPY_AND_ASSIGN(ItemInfo);
101 }; 125 };
102 126
103 typedef std::map<std::string, std::unique_ptr<ItemInfo>> DownloadUIItems; 127 typedef std::map<std::string, std::unique_ptr<ItemInfo>> DownloadUIItems;
104 128
105 void LoadCache(); 129 void LoadCache();
106 void ClearCache(); 130 void ClearCache();
107 131
108 // Task callbacks. 132 // Task callbacks.
109 void OnOfflinePagesLoaded(const MultipleOfflinePageItemResult& pages); 133 void OnOfflinePagesLoaded(const MultipleOfflinePageItemResult& pages);
134 void OnRequestsLoaded(std::vector<std::unique_ptr<SavePageRequest>> requests);
135
110 void NotifyItemsLoaded(Observer* observer); 136 void NotifyItemsLoaded(Observer* observer);
111 void OnDeletePagesDone(DeletePageResult result); 137 void OnDeletePagesDone(DeletePageResult result);
112 138
139 void addItemHelper(std::unique_ptr<ItemInfo> item_info);
dewittj 2017/01/18 18:49:48 nit: capital A, also next line.
Dmitry Titov 2017/01/27 04:30:22 Done.
140 void deleteItemHelper(const std::string& guid);
141
113 // Always valid, this class is a member of the model. 142 // Always valid, this class is a member of the model.
114 OfflinePageModel* model_; 143 OfflinePageModel* model_;
115 144
145 // Always valid, a service.
Pete Williamson 2017/01/17 18:27:34 Should we note that this pointer is unowned?
Dmitry Titov 2017/01/27 04:30:23 It is a raw pointer, so unowned... The comment say
146 RequestCoordinator* request_coordinator_;
147
148 // A delegate, supplied at construction.
149 std::unique_ptr<Delegate> delegate_;
150
116 State state_; 151 State state_;
117 152
118 // The cache of UI items. The key is DownloadUIItem.guid. 153 // The cache of UI items. The key is DownloadUIItem.guid.
119 DownloadUIItems items_; 154 DownloadUIItems items_;
120 155
121 // The observers. 156 // The observers.
122 base::ObserverList<Observer> observers_; 157 base::ObserverList<Observer> observers_;
123 int observers_count_; 158 int observers_count_;
124 159
125 base::WeakPtrFactory<DownloadUIAdapter> weak_ptr_factory_; 160 base::WeakPtrFactory<DownloadUIAdapter> weak_ptr_factory_;
126 161
127 DISALLOW_COPY_AND_ASSIGN(DownloadUIAdapter); 162 DISALLOW_COPY_AND_ASSIGN(DownloadUIAdapter);
128 }; 163 };
129 164
130 } // namespace offline_pages 165 } // namespace offline_pages
131 166
132 #endif // COMPONENTS_OFFLINE_PAGE_DOWNLOADS_DOWNLOAD_UI_ADAPTER_H_ 167 #endif // COMPONENTS_OFFLINE_PAGE_DOWNLOADS_DOWNLOAD_UI_ADAPTER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698