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

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

Issue 2489443002: Move all components/offline_pages/ files into component/offline_pages/core (Closed)
Patch Set: rebase Created 4 years 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 <map>
9 #include <memory>
10 #include <string>
11 #include <vector>
12
13 #include "base/observer_list.h"
14 #include "base/supports_user_data.h"
15 #include "components/offline_pages/downloads/download_ui_item.h"
16 #include "components/offline_pages/offline_page_model.h"
17 #include "components/offline_pages/offline_page_types.h"
18 #include "url/gurl.h"
19
20 namespace offline_pages {
21 // C++ side of the UI Adapter. Mimics DownloadManager/Item/History (since we
22 // share UI with Downloads).
23 // 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 // initial load the UI components can synchronously pull the whoel list or any
26 // item by its guid.
27 class DownloadUIAdapter : public OfflinePageModel::Observer,
28 public base::SupportsUserData::Data {
29 public:
30 // Observer, normally implemented by UI or a Bridge.
31 class Observer {
32 public:
33 // Invoked when UI items are loaded. GetAllItems/GetItem can now be used.
34 // 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
36 // callback will be posted right away.
37 virtual void ItemsLoaded() = 0;
38
39 // Invoked when the UI Item was added, usually as a request to download.
40 virtual void ItemAdded(const DownloadUIItem& item) = 0;
41
42 // Invoked when the UI Item was updated. Only guid of the item is guaranteed
43 // to survive the update, all other fields can change.
44 virtual void ItemUpdated(const DownloadUIItem& item) = 0;
45
46 // Invoked when the UI Item was deleted. At this point, only guid remains.
47 virtual void ItemDeleted(const std::string& guid) = 0;
48
49 protected:
50 virtual ~Observer() = default;
51 };
52
53 explicit DownloadUIAdapter(OfflinePageModel* model);
54 ~DownloadUIAdapter() override;
55
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
64 // itself as an observer.
65 // When the last observer si removed, cached list of items is destroyed and
66 // next time the initial loading will take longer.
67 void AddObserver(Observer* observer);
68 void RemoveObserver(Observer* observer);
69
70 // Returns all UI items. The list contains references to items in the cache
71 // and has to be used synchronously.
72 std::vector<const DownloadUIItem*> GetAllItems() const;
73 // May return nullptr if item with specified guid does not exist.
74 const DownloadUIItem* GetItem(const std::string& guid) const;
75
76 // Commands from UI. Start async operations, result is observable
77 // via Observer or directly by the user (as in 'open').
78 void DeleteItem(const std::string& guid);
79 int64_t GetOfflineIdByGuid(const std::string& guid) const;
80
81 // OfflinePageModel::Observer
82 void OfflinePageModelLoaded(OfflinePageModel* model) override;
83 void OfflinePageModelChanged(OfflinePageModel* model) override;
84 void OfflinePageDeleted(int64_t offline_id,
85 const ClientId& client_id) override;
86
87 private:
88 enum class State {
89 NOT_LOADED,
90 LOADING,
91 LOADED
92 };
93
94 struct ItemInfo {
95 explicit ItemInfo(const OfflinePageItem& page);
96 ~ItemInfo();
97
98 std::unique_ptr<DownloadUIItem> ui_item;
99 // Additional cached data, not exposed to UI through DownloadUIItem.
100 int64_t offline_id;
101
102 private:
103 DISALLOW_COPY_AND_ASSIGN(ItemInfo);
104 };
105
106 typedef std::map<std::string, std::unique_ptr<ItemInfo>> DownloadUIItems;
107
108 void LoadCache();
109 void ClearCache();
110
111 // Task callbacks.
112 void OnOfflinePagesLoaded(const MultipleOfflinePageItemResult& pages);
113 void NotifyItemsLoaded(Observer* observer);
114 void OnOfflinePagesChanged(const MultipleOfflinePageItemResult& pages);
115 void OnDeletePagesDone(DeletePageResult result);
116
117 // Always valid, this class is a member of the model.
118 OfflinePageModel* model_;
119
120 State state_;
121
122 // The cache of UI items. The key is DownloadUIItem.guid.
123 DownloadUIItems items_;
124
125 // The observers.
126 base::ObserverList<Observer> observers_;
127 int observers_count_;
128
129 base::WeakPtrFactory<DownloadUIAdapter> weak_ptr_factory_;
130
131 DISALLOW_COPY_AND_ASSIGN(DownloadUIAdapter);
132 };
133
134 } // namespace offline_pages
135
136 #endif // COMPONENTS_OFFLINE_PAGE_DOWNLOADS_DOWNLOAD_UI_ADAPTER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698