| 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) | 22 DownloadUIAdapter::ItemInfo::ItemInfo(const OfflinePageItem& page) |
| 23 : ui_item(base::MakeUnique<DownloadUIItem>(page)), | 23 : ui_item(base::MakeUnique<DownloadUIItem>(page)), |
| 24 offline_id(page.offline_id), | 24 offline_id(page.offline_id) {} |
| 25 offline_url(page.GetOfflineURL()) {} | |
| 26 | 25 |
| 27 DownloadUIAdapter::ItemInfo::~ItemInfo() {} | 26 DownloadUIAdapter::ItemInfo::~ItemInfo() {} |
| 28 | 27 |
| 29 DownloadUIAdapter::DownloadUIAdapter(OfflinePageModel* model) | 28 DownloadUIAdapter::DownloadUIAdapter(OfflinePageModel* model) |
| 30 : model_(model), | 29 : model_(model), |
| 31 state_(State::NOT_LOADED), | 30 state_(State::NOT_LOADED), |
| 32 observers_count_(0), | 31 observers_count_(0), |
| 33 weak_ptr_factory_(this) { | 32 weak_ptr_factory_(this) { |
| 34 } | 33 } |
| 35 | 34 |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 std::vector<int64_t> page_ids; | 122 std::vector<int64_t> page_ids; |
| 124 page_ids.push_back(it->second->offline_id); | 123 page_ids.push_back(it->second->offline_id); |
| 125 // TODO(dimich): This should be ExpirePages(...Now()..) when Expire is | 124 // TODO(dimich): This should be ExpirePages(...Now()..) when Expire is |
| 126 // firing Observer method. The resulting Observer notification will update | 125 // firing Observer method. The resulting Observer notification will update |
| 127 // local cache. | 126 // local cache. |
| 128 model_->DeletePagesByOfflineId( | 127 model_->DeletePagesByOfflineId( |
| 129 page_ids, base::Bind(&DownloadUIAdapter::OnDeletePagesDone, | 128 page_ids, base::Bind(&DownloadUIAdapter::OnDeletePagesDone, |
| 130 weak_ptr_factory_.GetWeakPtr())); | 129 weak_ptr_factory_.GetWeakPtr())); |
| 131 } | 130 } |
| 132 | 131 |
| 133 GURL DownloadUIAdapter::GetOfflineUrlByGuid( | |
| 134 const std::string& guid) const { | |
| 135 // TODO(dimich): when requests are also in the cache, filter them out. | |
| 136 // Requests do not yet have offline URL. | |
| 137 DownloadUIItems::const_iterator it = items_.find(guid); | |
| 138 if (it != items_.end()) | |
| 139 return it->second->offline_url; | |
| 140 return GURL(); | |
| 141 } | |
| 142 | |
| 143 // Note that several LoadCache calls may be issued before the async GetAllPages | 132 // Note that several LoadCache calls may be issued before the async GetAllPages |
| 144 // comes back. | 133 // comes back. |
| 145 void DownloadUIAdapter::LoadCache() { | 134 void DownloadUIAdapter::LoadCache() { |
| 146 // TODO(dimich): Add fetching from RequestQueue as well. | 135 // TODO(dimich): Add fetching from RequestQueue as well. |
| 147 state_ = State::LOADING; | 136 state_ = State::LOADING; |
| 148 model_->GetAllPages( | 137 model_->GetAllPages( |
| 149 base::Bind(&DownloadUIAdapter::OnOfflinePagesLoaded, | 138 base::Bind(&DownloadUIAdapter::OnOfflinePagesLoaded, |
| 150 weak_ptr_factory_.GetWeakPtr())); | 139 weak_ptr_factory_.GetWeakPtr())); |
| 151 } | 140 } |
| 152 | 141 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 // TODO(dimich): Consider adding UMA to record user actions. | 199 // TODO(dimich): Consider adding UMA to record user actions. |
| 211 } | 200 } |
| 212 | 201 |
| 213 bool DownloadUIAdapter::IsVisibleInUI(const ClientId& client_id) { | 202 bool DownloadUIAdapter::IsVisibleInUI(const ClientId& client_id) { |
| 214 const std::string& name_space = client_id.name_space; | 203 const std::string& name_space = client_id.name_space; |
| 215 return (name_space == kAsyncNamespace || name_space == kDownloadNamespace) && | 204 return (name_space == kAsyncNamespace || name_space == kDownloadNamespace) && |
| 216 base::IsValidGUID(client_id.id); | 205 base::IsValidGUID(client_id.id); |
| 217 } | 206 } |
| 218 | 207 |
| 219 } // namespace offline_pages | 208 } // namespace offline_pages |
| OLD | NEW |