Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 | |
|
fgorski
2016/07/26 16:25:21
nit: #include <map>, <memory>, <string>, <stdint.h
Dmitry Titov
2016/07/26 19:56:48
Done. Don't think I need stdint.h here...
| |
| 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; | |
|
fgorski
2016/07/26 16:25:21
Caught that later yesterday, sorry.
This should al
Dmitry Titov
2016/07/26 19:56:48
Done.
| |
| 45 | |
| 46 protected: | |
| 47 virtual ~Observer() = default; | |
| 48 }; | |
| 49 | |
| 50 explicit DownloadUIAdapter(OfflinePageModel* model); | |
| 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(const std::string& guid) const; | |
| 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 void LoadCache(); | |
| 72 void ClearCache(); | |
| 73 | |
| 74 // Task callbacks. | |
| 75 void OnOfflinePagesLoaded(const MultipleOfflinePageItemResult& pages); | |
| 76 void NotifyItemsLoaded(Observer* observer); | |
| 77 void OnOfflinePagesChanged(const MultipleOfflinePageItemResult& pages); | |
| 78 bool IsVisibleInUI(const OfflinePageItem& page); | |
| 79 | |
| 80 // Always valid, this class is a member of the model. | |
| 81 OfflinePageModel* model_; | |
| 82 | |
| 83 bool is_loaded_; | |
| 84 | |
| 85 // The cache of UI items. | |
| 86 DownloadUIItemsMap items_; | |
| 87 | |
| 88 // The observers. | |
| 89 base::ObserverList<Observer> observers_; | |
| 90 | |
| 91 base::WeakPtrFactory<DownloadUIAdapter> weak_ptr_factory_; | |
| 92 | |
| 93 DISALLOW_COPY_AND_ASSIGN(DownloadUIAdapter); | |
| 94 }; | |
| 95 | |
| 96 } // namespace offline_pages | |
| 97 | |
| 98 #endif // COMPONENTS_OFFLINE_PAGE_DOWNLOADS_DOWNLOAD_UI_ADAPTER_H_ | |
| OLD | NEW |