| 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_PAGE_DOWNLOADS_DOWNLOAD_UI_ADAPTER_H_ | 5 #ifndef COMPONENTS_OFFLINE_PAGE_DOWNLOADS_DOWNLOAD_UI_ADAPTER_H_ |
| 6 #define COMPONENTS_OFFLINE_PAGE_DOWNLOADS_DOWNLOAD_UI_ADAPTER_H_ | 6 #define COMPONENTS_OFFLINE_PAGE_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 | 11 |
| 12 #include "base/observer_list.h" | 12 #include "base/observer_list.h" |
| 13 #include "base/supports_user_data.h" |
| 13 #include "components/offline_pages/downloads/download_ui_item.h" | 14 #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_model.h" |
| 15 #include "components/offline_pages/offline_page_types.h" | 16 #include "components/offline_pages/offline_page_types.h" |
| 16 | 17 |
| 17 namespace offline_pages { | 18 namespace offline_pages { |
| 18 | 19 |
| 19 // Key is DownloadUIItem.guid. | 20 // Key is DownloadUIItem.guid. |
| 20 typedef | 21 typedef |
| 21 std::map<std::string, std::unique_ptr<DownloadUIItem>> DownloadUIItemsMap; | 22 std::map<std::string, std::unique_ptr<DownloadUIItem>> DownloadUIItemsMap; |
| 22 | 23 |
| 23 // C++ side of the UI Adapter. Mimics DownloadManager/Item/History (since we | 24 // C++ side of the UI Adapter. Mimics DownloadManager/Item/History (since we |
| 24 // share UI with Downloads). | 25 // share UI with Downloads). |
| 25 // An instance of this class is owned by OfflinePageModel and is shared between | 26 // 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 // 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 // initial load the UI components can synchronously pull the whoel list or any |
| 28 // item by its guid. | 29 // item by its guid. |
| 29 class DownloadUIAdapter : public OfflinePageModel::Observer { | 30 class DownloadUIAdapter : public OfflinePageModel::Observer, |
| 31 public base::SupportsUserData::Data { |
| 30 public: | 32 public: |
| 31 // Observer, normally implemented by UI or a Bridge. | 33 // Observer, normally implemented by UI or a Bridge. |
| 32 class Observer { | 34 class Observer { |
| 33 public: | 35 public: |
| 34 // Invoked when UI items are loaded. GetAllItems/GetItem can now be used. | 36 // Invoked when UI items are loaded. GetAllItems/GetItem can now be used. |
| 35 // Must be listened for in order to start getting the items. | 37 // 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 | 38 // If the items are already loaded by the time observer is added, this |
| 37 // callback will be posted right away. | 39 // callback will be posted right away. |
| 38 virtual void ItemsLoaded() = 0; | 40 virtual void ItemsLoaded() = 0; |
| 39 | 41 |
| 40 // Invoked when the UI Item was added, usually as a request to download. | 42 // Invoked when the UI Item was added, usually as a request to download. |
| 41 virtual void ItemAdded(const DownloadUIItem& item) = 0; | 43 virtual void ItemAdded(const DownloadUIItem& item) = 0; |
| 42 | 44 |
| 43 // Invoked when the UI Item was updated. Only guid of the item is guaranteed | 45 // Invoked when the UI Item was updated. Only guid of the item is guaranteed |
| 44 // to survive the update, all other fields can change. | 46 // to survive the update, all other fields can change. |
| 45 virtual void ItemUpdated(const DownloadUIItem& item) = 0; | 47 virtual void ItemUpdated(const DownloadUIItem& item) = 0; |
| 46 | 48 |
| 47 // Invoked when the UI Item was deleted. At this point, only guid remains. | 49 // Invoked when the UI Item was deleted. At this point, only guid remains. |
| 48 virtual void ItemDeleted(const std::string& guid) = 0; | 50 virtual void ItemDeleted(const std::string& guid) = 0; |
| 49 | 51 |
| 50 protected: | 52 protected: |
| 51 virtual ~Observer() = default; | 53 virtual ~Observer() = default; |
| 52 }; | 54 }; |
| 53 | 55 |
| 54 explicit DownloadUIAdapter(OfflinePageModel* model); | 56 explicit DownloadUIAdapter(OfflinePageModel* model); |
| 55 ~DownloadUIAdapter() override; | 57 ~DownloadUIAdapter() override; |
| 56 | 58 |
| 59 static DownloadUIAdapter* FromOfflinePageModel( |
| 60 OfflinePageModel* offline_page_model); |
| 61 |
| 57 // This adapter is potentially shared by UI elements, each of which adds | 62 // This adapter is potentially shared by UI elements, each of which adds |
| 58 // itself as an observer. | 63 // itself as an observer. |
| 59 // When the last observer si removed, cached list of items is destroyed and | 64 // When the last observer si removed, cached list of items is destroyed and |
| 60 // next time the initial loading will take longer. | 65 // next time the initial loading will take longer. |
| 61 void AddObserver(Observer* observer); | 66 void AddObserver(Observer* observer); |
| 62 void RemoveObserver(Observer* observer); | 67 void RemoveObserver(Observer* observer); |
| 63 | 68 |
| 64 const DownloadUIItemsMap& GetAllItems() const; | 69 const DownloadUIItemsMap& GetAllItems() const; |
| 65 // May return nullptr. | 70 // May return nullptr. |
| 66 const DownloadUIItem* GetItem(const std::string& guid) const; | 71 const DownloadUIItem* GetItem(const std::string& guid) const; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 92 // The observers. | 97 // The observers. |
| 93 base::ObserverList<Observer> observers_; | 98 base::ObserverList<Observer> observers_; |
| 94 | 99 |
| 95 base::WeakPtrFactory<DownloadUIAdapter> weak_ptr_factory_; | 100 base::WeakPtrFactory<DownloadUIAdapter> weak_ptr_factory_; |
| 96 | 101 |
| 97 DISALLOW_COPY_AND_ASSIGN(DownloadUIAdapter); | 102 DISALLOW_COPY_AND_ASSIGN(DownloadUIAdapter); |
| 98 }; | 103 }; |
| 99 | 104 |
| 100 } // namespace offline_pages | 105 } // namespace offline_pages |
| 101 | 106 |
| 102 #endif // COMPONENTS_OFFLINE_PAGE_DOWNLOADS_DOWNLOAD_UI_ADAPTER_H_ | 107 #endif // COMPONENTS_OFFLINE_PAGE_DOWNLOADS_DOWNLOAD_UI_ADAPTER_H_ |
| OLD | NEW |