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