| 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 |
| 8 #include <map> |
| 9 #include <memory> |
| 10 #include <string> |
| 11 |
| 12 #include "base/observer_list.h" |
| 13 #include "components/offline_pages/downloads/download_ui_item.h" |
| 14 #include "components/offline_pages/offline_page_model.h" |
| 15 #include "components/offline_pages/offline_page_types.h" |
| 16 |
| 17 namespace offline_pages { |
| 18 |
| 19 // Key is DownloadUIItem.guid. |
| 20 typedef |
| 21 std::map<std::string, std::unique_ptr<DownloadUIItem>> DownloadUIItemsMap; |
| 22 |
| 23 // C++ side of the UI Adapter. Mimics DownloadManager/Item/History (since we |
| 24 // share UI with Downloads). |
| 25 // An instance of this class is owned by OfflinePageModel and is shared between |
| 26 // UI components if needed. It manages the cache of DownloadUIItems, so after |
| 27 // initial load the UI components can synchronously pull the whoel list or any |
| 28 // item by its guid. |
| 29 class DownloadUIAdapter : public OfflinePageModel::Observer { |
| 30 public: |
| 31 // Observer, normally implemented by UI or a Bridge. |
| 32 class Observer { |
| 33 public: |
| 34 // Invoked when UI items are loaded. GetAllItems/GetItem can now be used. |
| 35 // Must be listened for in order to start getting the items. |
| 36 // If the items are already loaded by the time observer is added, this |
| 37 // callback will be posted right away. |
| 38 virtual void ItemsLoaded() = 0; |
| 39 |
| 40 // Invoked when the UI Item was added, usually as a request to download. |
| 41 virtual void ItemAdded(const DownloadUIItem& item) = 0; |
| 42 |
| 43 // Invoked when the UI Item was updated. Only guid of the item is guaranteed |
| 44 // to survive the update, all other fields can change. |
| 45 virtual void ItemUpdated(const DownloadUIItem& item) = 0; |
| 46 |
| 47 // Invoked when the UI Item was deleted. At this point, only guid remains. |
| 48 virtual void ItemDeleted(const std::string& guid) = 0; |
| 49 |
| 50 protected: |
| 51 virtual ~Observer() = default; |
| 52 }; |
| 53 |
| 54 explicit DownloadUIAdapter(OfflinePageModel* model); |
| 55 ~DownloadUIAdapter() override; |
| 56 |
| 57 // This adapter is potentially shared by UI elements, each of which adds |
| 58 // itself as an observer. |
| 59 // When the last observer si removed, cached list of items is destroyed and |
| 60 // next time the initial loading will take longer. |
| 61 void AddObserver(Observer* observer); |
| 62 void RemoveObserver(Observer* observer); |
| 63 |
| 64 const DownloadUIItemsMap& GetAllItems() const; |
| 65 // May return nullptr. |
| 66 const DownloadUIItem* GetItem(const std::string& guid) const; |
| 67 |
| 68 // OfflinePageModel::Observer |
| 69 void OfflinePageModelLoaded(OfflinePageModel* model) override; |
| 70 void OfflinePageModelChanged(OfflinePageModel* model) override; |
| 71 void OfflinePageDeleted(int64_t offline_id, |
| 72 const ClientId& client_id) override; |
| 73 |
| 74 private: |
| 75 void LoadCache(); |
| 76 void ClearCache(); |
| 77 |
| 78 // Task callbacks. |
| 79 void OnOfflinePagesLoaded(const MultipleOfflinePageItemResult& pages); |
| 80 void NotifyItemsLoaded(Observer* observer); |
| 81 void OnOfflinePagesChanged(const MultipleOfflinePageItemResult& pages); |
| 82 bool IsVisibleInUI(const OfflinePageItem& page); |
| 83 |
| 84 // Always valid, this class is a member of the model. |
| 85 OfflinePageModel* model_; |
| 86 |
| 87 bool is_loaded_; |
| 88 |
| 89 // The cache of UI items. |
| 90 DownloadUIItemsMap items_; |
| 91 |
| 92 // The observers. |
| 93 base::ObserverList<Observer> observers_; |
| 94 |
| 95 base::WeakPtrFactory<DownloadUIAdapter> weak_ptr_factory_; |
| 96 |
| 97 DISALLOW_COPY_AND_ASSIGN(DownloadUIAdapter); |
| 98 }; |
| 99 |
| 100 } // namespace offline_pages |
| 101 |
| 102 #endif // COMPONENTS_OFFLINE_PAGE_DOWNLOADS_DOWNLOAD_UI_ADAPTER_H_ |
| OLD | NEW |