| 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 #include "components/offline_pages/downloads/download_ui_adapter.h" | 5 #include "components/offline_pages/downloads/download_ui_adapter.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/guid.h" | 8 #include "base/guid.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/threading/thread_task_runner_handle.h" | 11 #include "base/threading/thread_task_runner_handle.h" |
| 12 #include "components/offline_pages/client_namespace_constants.h" | 12 #include "components/offline_pages/client_namespace_constants.h" |
| 13 #include "components/offline_pages/downloads/download_ui_item.h" | 13 #include "components/offline_pages/downloads/download_ui_item.h" |
| 14 #include "components/offline_pages/offline_page_model.h" | 14 #include "components/offline_pages/offline_page_model.h" |
| 15 | 15 |
| 16 namespace offline_pages { | 16 namespace offline_pages { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 const char kDownloadUIAdapterKey[] = "download-ui-adapter"; | 19 const char kDownloadUIAdapterKey[] = "download-ui-adapter"; |
| 20 } | 20 } |
| 21 | 21 |
| 22 DownloadUIAdapter::ItemInfo::ItemInfo(const OfflinePageItem& page) |
| 23 : ui_item(base::MakeUnique<DownloadUIItem>(page)), |
| 24 offline_id(page.offline_id), |
| 25 offline_url(page.GetOfflineURL()) {} |
| 26 |
| 27 DownloadUIAdapter::ItemInfo::~ItemInfo() {} |
| 28 |
| 22 DownloadUIAdapter::DownloadUIAdapter(OfflinePageModel* model) | 29 DownloadUIAdapter::DownloadUIAdapter(OfflinePageModel* model) |
| 23 : model_(model), | 30 : model_(model), |
| 24 is_loaded_(false), | 31 is_loaded_(false), |
| 25 weak_ptr_factory_(this) { | 32 weak_ptr_factory_(this) { |
| 26 } | 33 } |
| 27 | 34 |
| 28 DownloadUIAdapter::~DownloadUIAdapter() { } | 35 DownloadUIAdapter::~DownloadUIAdapter() { } |
| 29 | 36 |
| 30 // static | 37 // static |
| 31 DownloadUIAdapter* DownloadUIAdapter::FromOfflinePageModel( | 38 DownloadUIAdapter* DownloadUIAdapter::FromOfflinePageModel( |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 | 74 |
| 68 void DownloadUIAdapter::OfflinePageModelChanged(OfflinePageModel* model) { | 75 void DownloadUIAdapter::OfflinePageModelChanged(OfflinePageModel* model) { |
| 69 DCHECK(model == model_); | 76 DCHECK(model == model_); |
| 70 model_->GetAllPages( | 77 model_->GetAllPages( |
| 71 base::Bind(&DownloadUIAdapter::OnOfflinePagesChanged, | 78 base::Bind(&DownloadUIAdapter::OnOfflinePagesChanged, |
| 72 weak_ptr_factory_.GetWeakPtr())); | 79 weak_ptr_factory_.GetWeakPtr())); |
| 73 } | 80 } |
| 74 | 81 |
| 75 void DownloadUIAdapter::OfflinePageDeleted( | 82 void DownloadUIAdapter::OfflinePageDeleted( |
| 76 int64_t offline_id, const ClientId& client_id) { | 83 int64_t offline_id, const ClientId& client_id) { |
| 77 for(const auto& item : items_) { | 84 if (!IsVisibleInUI(client_id)) |
| 78 if (item.second->guid == client_id.id) { | 85 return; |
| 79 items_.erase(item.first); | 86 std::string guid = client_id.id; |
| 80 FOR_EACH_OBSERVER(Observer, observers_, ItemDeleted(client_id.id)); | 87 DownloadUIItems::const_iterator it = items_.find(guid); |
| 81 return; | 88 if (it == items_.end()) |
| 82 } | 89 return; |
| 83 } | 90 items_.erase(it); |
| 91 FOR_EACH_OBSERVER(Observer, observers_, ItemDeleted(guid)); |
| 84 } | 92 } |
| 85 | 93 |
| 86 const DownloadUIItemsMap& DownloadUIAdapter::GetAllItems() const { | 94 std::vector<const DownloadUIItem*> DownloadUIAdapter::GetAllItems() const { |
| 87 return items_; | 95 std::vector<const DownloadUIItem*> result; |
| 96 for (const auto& item : items_) |
| 97 result.push_back(item.second->ui_item.get()); |
| 98 return result; |
| 88 } | 99 } |
| 89 | 100 |
| 90 const DownloadUIItem* | 101 const DownloadUIItem* |
| 91 DownloadUIAdapter::GetItem(const std::string& guid) const { | 102 DownloadUIAdapter::GetItem(const std::string& guid) const { |
| 92 DownloadUIItemsMap::const_iterator it = items_.find(guid); | 103 DownloadUIItems::const_iterator it = items_.find(guid); |
| 93 if (it == items_.end()) | 104 if (it == items_.end()) |
| 94 return nullptr; | 105 return nullptr; |
| 95 return (*it).second.get(); | 106 return it->second->ui_item.get(); |
| 107 } |
| 108 |
| 109 void DownloadUIAdapter::DeleteItem(const std::string& guid) { |
| 110 // TODO(dimich): Also remove pending request from RequestQueue. |
| 111 DownloadUIItems::const_iterator it = items_.find(guid); |
| 112 if (it == items_.end()) |
| 113 return; |
| 114 |
| 115 std::vector<int64_t> page_ids; |
| 116 page_ids.push_back(it->second->offline_id); |
| 117 // TODO(dimich): This should be ExpirePages(...Now()..) when Expire is |
| 118 // firing Observer method. The resulting Observer notification will update |
| 119 // local cache. |
| 120 model_->DeletePagesByOfflineId( |
| 121 page_ids, base::Bind(&DownloadUIAdapter::OnDeletePagesDone, |
| 122 weak_ptr_factory_.GetWeakPtr())); |
| 123 } |
| 124 |
| 125 GURL DownloadUIAdapter::GetOfflineUrlByGuid( |
| 126 const std::string& guid) const { |
| 127 // TODO(dimich): when requests are also in the cache, filter them out. |
| 128 // Requests do not yet have offline URL. |
| 129 DownloadUIItems::const_iterator it = items_.find(guid); |
| 130 if (it != items_.end()) |
| 131 return it->second->offline_url; |
| 132 return GURL(); |
| 96 } | 133 } |
| 97 | 134 |
| 98 void DownloadUIAdapter::LoadCache() { | 135 void DownloadUIAdapter::LoadCache() { |
| 99 DCHECK(!is_loaded_); | 136 DCHECK(!is_loaded_); |
| 100 // TODO(dimich): Add fetching from RequestQueue as well. | 137 // TODO(dimich): Add fetching from RequestQueue as well. |
| 101 model_->AddObserver(this); | 138 model_->AddObserver(this); |
| 102 model_->GetAllPages( | 139 model_->GetAllPages( |
| 103 base::Bind(&DownloadUIAdapter::OnOfflinePagesLoaded, | 140 base::Bind(&DownloadUIAdapter::OnOfflinePagesLoaded, |
| 104 weak_ptr_factory_.GetWeakPtr())); | 141 weak_ptr_factory_.GetWeakPtr())); |
| 105 } | 142 } |
| 106 | 143 |
| 107 void DownloadUIAdapter::ClearCache() { | 144 void DownloadUIAdapter::ClearCache() { |
| 108 model_->RemoveObserver(this); | 145 model_->RemoveObserver(this); |
| 109 items_.clear(); | 146 items_.clear(); |
| 110 is_loaded_ = false; | 147 is_loaded_ = false; |
| 111 } | 148 } |
| 112 | 149 |
| 113 void DownloadUIAdapter::OnOfflinePagesLoaded( | 150 void DownloadUIAdapter::OnOfflinePagesLoaded( |
| 114 const MultipleOfflinePageItemResult& pages) { | 151 const MultipleOfflinePageItemResult& pages) { |
| 115 for (const auto& page : pages) { | 152 for (const auto& page : pages) { |
| 116 if (IsVisibleInUI(page)) { | 153 if (IsVisibleInUI(page.client_id)) { |
| 117 std::unique_ptr<DownloadUIItem> new_item(new DownloadUIItem(page)); | 154 std::string guid = page.client_id.id; |
| 118 const std::string& guid = new_item->guid; | |
| 119 DCHECK(items_.find(guid) == items_.end()); | 155 DCHECK(items_.find(guid) == items_.end()); |
| 120 items_[guid] = std::move(new_item); | 156 items_[guid] = base::MakeUnique<ItemInfo>(page); |
| 121 } | 157 } |
| 122 } | 158 } |
| 123 is_loaded_ = true; | 159 is_loaded_ = true; |
| 124 FOR_EACH_OBSERVER(Observer, observers_, ItemsLoaded()); | 160 FOR_EACH_OBSERVER(Observer, observers_, ItemsLoaded()); |
| 125 } | 161 } |
| 126 | 162 |
| 127 void DownloadUIAdapter::NotifyItemsLoaded(Observer* observer) { | 163 void DownloadUIAdapter::NotifyItemsLoaded(Observer* observer) { |
| 128 if (observer && observers_.HasObserver(observer)) | 164 if (observer && observers_.HasObserver(observer)) |
| 129 observer->ItemsLoaded(); | 165 observer->ItemsLoaded(); |
| 130 } | 166 } |
| 131 | 167 |
| 132 // This method is only called by OPM when a single item added. | 168 // This method is only called by OPM when a single item added. |
| 133 // TODO(dimich): change OPM to have real OnPageAdded/OnPageUpdated and | 169 // TODO(dimich): change OPM to have real OnPageAdded/OnPageUpdated and |
| 134 // simplify this code. | 170 // simplify this code. |
| 135 void DownloadUIAdapter::OnOfflinePagesChanged( | 171 void DownloadUIAdapter::OnOfflinePagesChanged( |
| 136 const MultipleOfflinePageItemResult& pages) { | 172 const MultipleOfflinePageItemResult& pages) { |
| 137 std::vector<std::string> added_guids; | 173 std::vector<std::string> added_guids; |
| 138 for (const auto& page : pages) { | 174 for (const auto& page : pages) { |
| 139 if (!IsVisibleInUI(page)) // Item should be filtered out. | 175 if (!IsVisibleInUI(page.client_id)) // Item should be filtered out. |
| 140 continue; | 176 continue; |
| 141 const std::string& guid = page.client_id.id; | 177 const std::string& guid = page.client_id.id; |
| 142 if (items_.find(guid) != items_.end()) // Item already exists. | 178 if (items_.find(guid) != items_.end()) // Item already exists. |
| 143 continue; | 179 continue; |
| 144 std::unique_ptr<DownloadUIItem> item(new DownloadUIItem(page)); | 180 items_[guid] = base::MakeUnique<ItemInfo>(page); |
| 145 items_[guid] = std::move(item); | |
| 146 added_guids.push_back(guid); | 181 added_guids.push_back(guid); |
| 147 } | 182 } |
| 148 // Only one added page | 183 // Only one added page |
| 149 CHECK(added_guids.size() <= 1); | 184 CHECK(added_guids.size() <= 1); |
| 150 for (auto& guid : added_guids) { | 185 for (auto& guid : added_guids) { |
| 151 const DownloadUIItem& item = *(items_.find(guid)->second.get()); | 186 const DownloadUIItem& item = *(items_.find(guid)->second->ui_item.get()); |
| 152 FOR_EACH_OBSERVER(Observer, observers_, ItemAdded(item)); | 187 FOR_EACH_OBSERVER(Observer, observers_, ItemAdded(item)); |
| 153 } | 188 } |
| 154 } | 189 } |
| 155 | 190 |
| 156 bool DownloadUIAdapter::IsVisibleInUI(const OfflinePageItem& page) { | 191 void DownloadUIAdapter::OnDeletePagesDone(DeletePageResult result) { |
| 157 // TODO(dimich): set up the right filter here. | 192 // TODO(dimich): Consider adding UMA to record user actions. |
| 158 return page.client_id.name_space == kAsyncNamespace && | 193 } |
| 159 base::IsValidGUID(page.client_id.id); | 194 |
| 195 bool DownloadUIAdapter::IsVisibleInUI(const ClientId& client_id) { |
| 196 const std::string& name_space = client_id.name_space; |
| 197 return (name_space == kAsyncNamespace || name_space == kDownloadNamespace) && |
| 198 base::IsValidGUID(client_id.id); |
| 160 } | 199 } |
| 161 | 200 |
| 162 } // namespace offline_pages | 201 } // namespace offline_pages |
| OLD | NEW |