| 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 #include <vector> |
| 11 | 12 |
| 12 #include "base/observer_list.h" | 13 #include "base/observer_list.h" |
| 13 #include "base/supports_user_data.h" | 14 #include "base/supports_user_data.h" |
| 14 #include "components/offline_pages/downloads/download_ui_item.h" | 15 #include "components/offline_pages/downloads/download_ui_item.h" |
| 15 #include "components/offline_pages/offline_page_model.h" | 16 #include "components/offline_pages/offline_page_model.h" |
| 16 #include "components/offline_pages/offline_page_types.h" | 17 #include "components/offline_pages/offline_page_types.h" |
| 18 #include "url/gurl.h" |
| 17 | 19 |
| 18 namespace offline_pages { | 20 namespace offline_pages { |
| 19 | |
| 20 // Key is DownloadUIItem.guid. | |
| 21 typedef | |
| 22 std::map<std::string, std::unique_ptr<DownloadUIItem>> DownloadUIItemsMap; | |
| 23 | |
| 24 // C++ side of the UI Adapter. Mimics DownloadManager/Item/History (since we | 21 // C++ side of the UI Adapter. Mimics DownloadManager/Item/History (since we |
| 25 // share UI with Downloads). | 22 // share UI with Downloads). |
| 26 // An instance of this class is owned by OfflinePageModel and is shared between | 23 // An instance of this class is owned by OfflinePageModel and is shared between |
| 27 // UI components if needed. It manages the cache of DownloadUIItems, so after | 24 // UI components if needed. It manages the cache of DownloadUIItems, so after |
| 28 // initial load the UI components can synchronously pull the whoel list or any | 25 // initial load the UI components can synchronously pull the whoel list or any |
| 29 // item by its guid. | 26 // item by its guid. |
| 30 class DownloadUIAdapter : public OfflinePageModel::Observer, | 27 class DownloadUIAdapter : public OfflinePageModel::Observer, |
| 31 public base::SupportsUserData::Data { | 28 public base::SupportsUserData::Data { |
| 32 public: | 29 public: |
| 33 // Observer, normally implemented by UI or a Bridge. | 30 // Observer, normally implemented by UI or a Bridge. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 59 static DownloadUIAdapter* FromOfflinePageModel( | 56 static DownloadUIAdapter* FromOfflinePageModel( |
| 60 OfflinePageModel* offline_page_model); | 57 OfflinePageModel* offline_page_model); |
| 61 | 58 |
| 62 // This adapter is potentially shared by UI elements, each of which adds | 59 // This adapter is potentially shared by UI elements, each of which adds |
| 63 // itself as an observer. | 60 // itself as an observer. |
| 64 // When the last observer si removed, cached list of items is destroyed and | 61 // When the last observer si removed, cached list of items is destroyed and |
| 65 // next time the initial loading will take longer. | 62 // next time the initial loading will take longer. |
| 66 void AddObserver(Observer* observer); | 63 void AddObserver(Observer* observer); |
| 67 void RemoveObserver(Observer* observer); | 64 void RemoveObserver(Observer* observer); |
| 68 | 65 |
| 69 const DownloadUIItemsMap& GetAllItems() const; | 66 // Returns all UI items. The list contains references to items in the cache |
| 70 // May return nullptr. | 67 // and has to be used synchronously. |
| 68 std::vector<const DownloadUIItem*> GetAllItems() const; |
| 69 // May return nullptr if item with specified guid does not exist. |
| 71 const DownloadUIItem* GetItem(const std::string& guid) const; | 70 const DownloadUIItem* GetItem(const std::string& guid) const; |
| 72 | 71 |
| 72 // Commands from UI. Start async operations, result is observable |
| 73 // via Observer or directly by the user (as in 'open'). |
| 74 void DeleteItem(const std::string& guid); |
| 75 GURL GetOfflineUrlByGuid(const std::string& guid) const; |
| 76 |
| 73 // OfflinePageModel::Observer | 77 // OfflinePageModel::Observer |
| 74 void OfflinePageModelLoaded(OfflinePageModel* model) override; | 78 void OfflinePageModelLoaded(OfflinePageModel* model) override; |
| 75 void OfflinePageModelChanged(OfflinePageModel* model) override; | 79 void OfflinePageModelChanged(OfflinePageModel* model) override; |
| 76 void OfflinePageDeleted(int64_t offline_id, | 80 void OfflinePageDeleted(int64_t offline_id, |
| 77 const ClientId& client_id) override; | 81 const ClientId& client_id) override; |
| 78 | 82 |
| 79 private: | 83 private: |
| 84 struct ItemInfo { |
| 85 explicit ItemInfo(const OfflinePageItem& page); |
| 86 ~ItemInfo(); |
| 87 |
| 88 std::unique_ptr<DownloadUIItem> ui_item; |
| 89 // Additional cached data, not exposed to UI through DownloadUIItem. |
| 90 int64_t offline_id; |
| 91 GURL offline_url; |
| 92 |
| 93 private: |
| 94 DISALLOW_COPY_AND_ASSIGN(ItemInfo); |
| 95 }; |
| 96 |
| 97 typedef std::map<std::string, std::unique_ptr<ItemInfo>> DownloadUIItems; |
| 98 |
| 80 void LoadCache(); | 99 void LoadCache(); |
| 81 void ClearCache(); | 100 void ClearCache(); |
| 82 | 101 |
| 83 // Task callbacks. | 102 // Task callbacks. |
| 84 void OnOfflinePagesLoaded(const MultipleOfflinePageItemResult& pages); | 103 void OnOfflinePagesLoaded(const MultipleOfflinePageItemResult& pages); |
| 85 void NotifyItemsLoaded(Observer* observer); | 104 void NotifyItemsLoaded(Observer* observer); |
| 86 void OnOfflinePagesChanged(const MultipleOfflinePageItemResult& pages); | 105 void OnOfflinePagesChanged(const MultipleOfflinePageItemResult& pages); |
| 87 bool IsVisibleInUI(const OfflinePageItem& page); | 106 void OnDeletePagesDone(DeletePageResult result); |
| 107 |
| 108 bool IsVisibleInUI(const ClientId& page); |
| 88 | 109 |
| 89 // Always valid, this class is a member of the model. | 110 // Always valid, this class is a member of the model. |
| 90 OfflinePageModel* model_; | 111 OfflinePageModel* model_; |
| 91 | 112 |
| 92 bool is_loaded_; | 113 bool is_loaded_; |
| 93 | 114 |
| 94 // The cache of UI items. | 115 // The cache of UI items. The key is DownloadUIItem.guid. |
| 95 DownloadUIItemsMap items_; | 116 DownloadUIItems items_; |
| 96 | 117 |
| 97 // The observers. | 118 // The observers. |
| 98 base::ObserverList<Observer> observers_; | 119 base::ObserverList<Observer> observers_; |
| 99 | 120 |
| 100 base::WeakPtrFactory<DownloadUIAdapter> weak_ptr_factory_; | 121 base::WeakPtrFactory<DownloadUIAdapter> weak_ptr_factory_; |
| 101 | 122 |
| 102 DISALLOW_COPY_AND_ASSIGN(DownloadUIAdapter); | 123 DISALLOW_COPY_AND_ASSIGN(DownloadUIAdapter); |
| 103 }; | 124 }; |
| 104 | 125 |
| 105 } // namespace offline_pages | 126 } // namespace offline_pages |
| 106 | 127 |
| 107 #endif // COMPONENTS_OFFLINE_PAGE_DOWNLOADS_DOWNLOAD_UI_ADAPTER_H_ | 128 #endif // COMPONENTS_OFFLINE_PAGE_DOWNLOADS_DOWNLOAD_UI_ADAPTER_H_ |
| OLD | NEW |