| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/core/downloads/download_ui_adapter.h" | 5 #include "components/offline_pages/core/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/core/background/request_coordinator.h" | 12 #include "components/offline_pages/core/background/request_coordinator.h" |
| 13 #include "components/offline_pages/core/background/save_page_request.h" | 13 #include "components/offline_pages/core/background/save_page_request.h" |
| 14 #include "components/offline_pages/core/client_namespace_constants.h" | 14 #include "components/offline_pages/core/client_namespace_constants.h" |
| 15 #include "components/offline_pages/core/client_policy_controller.h" | 15 #include "components/offline_pages/core/client_policy_controller.h" |
| 16 #include "components/offline_pages/core/downloads/download_ui_item.h" | 16 #include "components/offline_pages/core/downloads/download_ui_item.h" |
| 17 #include "components/offline_pages/core/offline_page_model.h" | 17 #include "components/offline_pages/core/offline_page_model.h" |
| 18 | 18 |
| 19 namespace offline_pages { | 19 namespace offline_pages { |
| 20 | 20 |
| 21 DownloadUIAdapter::ItemInfo::ItemInfo(const OfflinePageItem& page) | 21 DownloadUIAdapter::ItemInfo::ItemInfo(const OfflinePageItem& page, |
| 22 bool temporarily_hidden) |
| 22 : ui_item(base::MakeUnique<DownloadUIItem>(page)), | 23 : ui_item(base::MakeUnique<DownloadUIItem>(page)), |
| 23 is_request(false), | 24 is_request(false), |
| 24 offline_id(page.offline_id), | 25 offline_id(page.offline_id), |
| 25 client_id(page.client_id), | 26 client_id(page.client_id), |
| 26 temporarily_hidden(false) {} | 27 temporarily_hidden(temporarily_hidden) {} |
| 27 | 28 |
| 28 DownloadUIAdapter::ItemInfo::ItemInfo(const SavePageRequest& request) | 29 DownloadUIAdapter::ItemInfo::ItemInfo(const SavePageRequest& request, |
| 30 bool temporarily_hidden) |
| 29 : ui_item(base::MakeUnique<DownloadUIItem>(request)), | 31 : ui_item(base::MakeUnique<DownloadUIItem>(request)), |
| 30 is_request(true), | 32 is_request(true), |
| 31 offline_id(request.request_id()), | 33 offline_id(request.request_id()), |
| 32 client_id(request.client_id()), | 34 client_id(request.client_id()), |
| 33 temporarily_hidden(false) {} | 35 temporarily_hidden() {} |
| 34 | 36 |
| 35 DownloadUIAdapter::ItemInfo::~ItemInfo() {} | 37 DownloadUIAdapter::ItemInfo::~ItemInfo() {} |
| 36 | 38 |
| 37 DownloadUIAdapter::DownloadUIAdapter(OfflinePageModel* model, | 39 DownloadUIAdapter::DownloadUIAdapter(OfflinePageModel* model, |
| 38 RequestCoordinator* request_coordinator, | 40 RequestCoordinator* request_coordinator, |
| 39 std::unique_ptr<Delegate> delegate) | 41 std::unique_ptr<Delegate> delegate) |
| 40 : model_(model), | 42 : model_(model), |
| 41 request_coordinator_(request_coordinator), | 43 request_coordinator_(request_coordinator), |
| 42 delegate_(std::move(delegate)), | 44 delegate_(std::move(delegate)), |
| 43 state_(State::NOT_LOADED), | 45 state_(State::NOT_LOADED), |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 void DownloadUIAdapter::OfflinePageModelLoaded(OfflinePageModel* model) { | 80 void DownloadUIAdapter::OfflinePageModelLoaded(OfflinePageModel* model) { |
| 79 // This signal is not used here. | 81 // This signal is not used here. |
| 80 } | 82 } |
| 81 | 83 |
| 82 void DownloadUIAdapter::OfflinePageAdded(OfflinePageModel* model, | 84 void DownloadUIAdapter::OfflinePageAdded(OfflinePageModel* model, |
| 83 const OfflinePageItem& added_page) { | 85 const OfflinePageItem& added_page) { |
| 84 DCHECK(model == model_); | 86 DCHECK(model == model_); |
| 85 if (!delegate_->IsVisibleInUI(added_page.client_id)) | 87 if (!delegate_->IsVisibleInUI(added_page.client_id)) |
| 86 return; | 88 return; |
| 87 | 89 |
| 88 AddItemHelper(base::MakeUnique<ItemInfo>(added_page)); | 90 bool temporarily_hidden = |
| 91 delegate_->IsTemporarilyHiddenInUI(added_page.client_id); |
| 92 AddItemHelper(base::MakeUnique<ItemInfo>(added_page, temporarily_hidden)); |
| 89 } | 93 } |
| 90 | 94 |
| 91 void DownloadUIAdapter::OfflinePageDeleted(int64_t offline_id, | 95 void DownloadUIAdapter::OfflinePageDeleted(int64_t offline_id, |
| 92 const ClientId& client_id) { | 96 const ClientId& client_id) { |
| 93 if (!delegate_->IsVisibleInUI(client_id)) | 97 if (!delegate_->IsVisibleInUI(client_id)) |
| 94 return; | 98 return; |
| 95 DeleteItemHelper(client_id.id); | 99 DeleteItemHelper(client_id.id); |
| 96 } | 100 } |
| 97 | 101 |
| 98 // RequestCoordinator::Observer | 102 // RequestCoordinator::Observer |
| 99 void DownloadUIAdapter::OnAdded(const SavePageRequest& added_request) { | 103 void DownloadUIAdapter::OnAdded(const SavePageRequest& added_request) { |
| 100 if (!delegate_->IsVisibleInUI(added_request.client_id())) | 104 if (!delegate_->IsVisibleInUI(added_request.client_id())) |
| 101 return; | 105 return; |
| 102 | 106 |
| 103 AddItemHelper(base::MakeUnique<ItemInfo>(added_request)); | 107 bool temporarily_hidden = |
| 108 delegate_->IsTemporarilyHiddenInUI(added_request.client_id()); |
| 109 AddItemHelper(base::MakeUnique<ItemInfo>(added_request, temporarily_hidden)); |
| 104 } | 110 } |
| 105 | 111 |
| 106 // RequestCoordinator::Observer | 112 // RequestCoordinator::Observer |
| 107 void DownloadUIAdapter::OnCompleted( | 113 void DownloadUIAdapter::OnCompleted( |
| 108 const SavePageRequest& request, | 114 const SavePageRequest& request, |
| 109 RequestNotifier::BackgroundSavePageResult status) { | 115 RequestNotifier::BackgroundSavePageResult status) { |
| 110 if (!delegate_->IsVisibleInUI(request.client_id())) | 116 if (!delegate_->IsVisibleInUI(request.client_id())) |
| 111 return; | 117 return; |
| 112 | 118 |
| 113 // If request completed successfully, report ItemUpdated when a page is added | 119 // If request completed successfully, report ItemUpdated when a page is added |
| 114 // to the model. If the request failed, tell UI that the item is gone. | 120 // to the model. If the request failed, tell UI that the item is gone. |
| 115 if (status == RequestNotifier::BackgroundSavePageResult::SUCCESS) | 121 if (status == RequestNotifier::BackgroundSavePageResult::SUCCESS) |
| 116 return; | 122 return; |
| 117 DeleteItemHelper(request.client_id().id); | 123 DeleteItemHelper(request.client_id().id); |
| 118 } | 124 } |
| 119 | 125 |
| 120 // RequestCoordinator::Observer | 126 // RequestCoordinator::Observer |
| 121 void DownloadUIAdapter::OnChanged(const SavePageRequest& request) { | 127 void DownloadUIAdapter::OnChanged(const SavePageRequest& request) { |
| 122 if (!delegate_->IsVisibleInUI(request.client_id())) | 128 if (!delegate_->IsVisibleInUI(request.client_id())) |
| 123 return; | 129 return; |
| 124 | 130 |
| 125 std::string guid = request.client_id().id; | 131 std::string guid = request.client_id().id; |
| 126 items_[guid] = base::MakeUnique<ItemInfo>(request); | 132 bool temporarily_hidden = |
| 133 delegate_->IsTemporarilyHiddenInUI(request.client_id()); |
| 134 items_[guid] = base::MakeUnique<ItemInfo>(request, temporarily_hidden); |
| 127 | 135 |
| 128 if (state_ != State::LOADED) | 136 if (state_ != State::LOADED) |
| 129 return; | 137 return; |
| 130 | 138 |
| 131 const DownloadUIItem& download_ui_item = *(items_[guid]->ui_item); | 139 const DownloadUIItem& download_ui_item = *(items_[guid]->ui_item); |
| 132 for (Observer& observer : observers_) | 140 for (Observer& observer : observers_) |
| 133 observer.ItemUpdated(download_ui_item); | 141 observer.ItemUpdated(download_ui_item); |
| 134 } | 142 } |
| 135 | 143 |
| 136 void DownloadUIAdapter::TemporaryHiddenStatusChanged( | 144 void DownloadUIAdapter::TemporaryHiddenStatusChanged( |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 return; | 187 return; |
| 180 | 188 |
| 181 std::vector<int64_t> page_ids; | 189 std::vector<int64_t> page_ids; |
| 182 page_ids.push_back(it->second->offline_id); | 190 page_ids.push_back(it->second->offline_id); |
| 183 model_->DeletePagesByOfflineId( | 191 model_->DeletePagesByOfflineId( |
| 184 page_ids, base::Bind(&DownloadUIAdapter::OnDeletePagesDone, | 192 page_ids, base::Bind(&DownloadUIAdapter::OnDeletePagesDone, |
| 185 weak_ptr_factory_.GetWeakPtr())); | 193 weak_ptr_factory_.GetWeakPtr())); |
| 186 } | 194 } |
| 187 | 195 |
| 188 int64_t DownloadUIAdapter::GetOfflineIdByGuid(const std::string& guid) const { | 196 int64_t DownloadUIAdapter::GetOfflineIdByGuid(const std::string& guid) const { |
| 197 if (deleting_item_ && deleting_item_->ui_item->guid == guid) |
| 198 return deleting_item_->offline_id; |
| 199 |
| 189 DownloadUIItems::const_iterator it = items_.find(guid); | 200 DownloadUIItems::const_iterator it = items_.find(guid); |
| 190 if (it != items_.end()) | 201 if (it != items_.end()) |
| 191 return it->second->offline_id; | 202 return it->second->offline_id; |
| 192 return 0; | 203 return 0; |
| 193 } | 204 } |
| 194 | 205 |
| 195 // Note that several LoadCache calls may be issued before the async GetAllPages | 206 // Note that several LoadCache calls may be issued before the async GetAllPages |
| 196 // comes back. | 207 // comes back. |
| 197 void DownloadUIAdapter::LoadCache() { | 208 void DownloadUIAdapter::LoadCache() { |
| 198 state_ = State::LOADING_PAGES; | 209 state_ = State::LOADING_PAGES; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 213 const MultipleOfflinePageItemResult& pages) { | 224 const MultipleOfflinePageItemResult& pages) { |
| 214 // If multiple observers register quickly, the cache might be already loaded | 225 // If multiple observers register quickly, the cache might be already loaded |
| 215 // by the previous LoadCache call. At the same time, if all observers already | 226 // by the previous LoadCache call. At the same time, if all observers already |
| 216 // left, there is no reason to populate the cache. | 227 // left, there is no reason to populate the cache. |
| 217 if (state_ != State::LOADING_PAGES) | 228 if (state_ != State::LOADING_PAGES) |
| 218 return; | 229 return; |
| 219 for (const auto& page : pages) { | 230 for (const auto& page : pages) { |
| 220 if (delegate_->IsVisibleInUI(page.client_id)) { | 231 if (delegate_->IsVisibleInUI(page.client_id)) { |
| 221 std::string guid = page.client_id.id; | 232 std::string guid = page.client_id.id; |
| 222 DCHECK(items_.find(guid) == items_.end()); | 233 DCHECK(items_.find(guid) == items_.end()); |
| 223 std::unique_ptr<ItemInfo> item = base::MakeUnique<ItemInfo>(page); | 234 bool temporarily_hidden = |
| 224 item->temporarily_hidden = | |
| 225 delegate_->IsTemporarilyHiddenInUI(page.client_id); | 235 delegate_->IsTemporarilyHiddenInUI(page.client_id); |
| 236 std::unique_ptr<ItemInfo> item = |
| 237 base::MakeUnique<ItemInfo>(page, temporarily_hidden); |
| 226 items_[guid] = std::move(item); | 238 items_[guid] = std::move(item); |
| 227 } | 239 } |
| 228 } | 240 } |
| 229 model_->AddObserver(this); | 241 model_->AddObserver(this); |
| 230 | 242 |
| 231 state_ = State::LOADING_REQUESTS; | 243 state_ = State::LOADING_REQUESTS; |
| 232 request_coordinator_->GetAllRequests(base::Bind( | 244 request_coordinator_->GetAllRequests(base::Bind( |
| 233 &DownloadUIAdapter::OnRequestsLoaded, weak_ptr_factory_.GetWeakPtr())); | 245 &DownloadUIAdapter::OnRequestsLoaded, weak_ptr_factory_.GetWeakPtr())); |
| 234 } | 246 } |
| 235 | 247 |
| 236 void DownloadUIAdapter::OnRequestsLoaded( | 248 void DownloadUIAdapter::OnRequestsLoaded( |
| 237 std::vector<std::unique_ptr<SavePageRequest>> requests) { | 249 std::vector<std::unique_ptr<SavePageRequest>> requests) { |
| 238 // If multiple observers register quickly, the cache might be already loaded | 250 // If multiple observers register quickly, the cache might be already loaded |
| 239 // by the previous LoadCache call. At the same time, if all observers already | 251 // by the previous LoadCache call. At the same time, if all observers already |
| 240 // left, there is no reason to populate the cache. | 252 // left, there is no reason to populate the cache. |
| 241 if (state_ != State::LOADING_REQUESTS) | 253 if (state_ != State::LOADING_REQUESTS) |
| 242 return; | 254 return; |
| 243 | 255 |
| 244 for (const auto& request : requests) { | 256 for (const auto& request : requests) { |
| 245 if (delegate_->IsVisibleInUI(request->client_id())) { | 257 if (delegate_->IsVisibleInUI(request->client_id())) { |
| 246 std::string guid = request->client_id().id; | 258 std::string guid = request->client_id().id; |
| 247 DCHECK(items_.find(guid) == items_.end()); | 259 DCHECK(items_.find(guid) == items_.end()); |
| 260 bool temporarily_hidden = |
| 261 delegate_->IsTemporarilyHiddenInUI(request->client_id()); |
| 248 std::unique_ptr<ItemInfo> item = | 262 std::unique_ptr<ItemInfo> item = |
| 249 base::MakeUnique<ItemInfo>(*request.get()); | 263 base::MakeUnique<ItemInfo>(*request.get(), temporarily_hidden); |
| 250 item->temporarily_hidden = | |
| 251 delegate_->IsTemporarilyHiddenInUI(request->client_id()); | |
| 252 items_[guid] = std::move(item); | 264 items_[guid] = std::move(item); |
| 253 } | 265 } |
| 254 } | 266 } |
| 255 request_coordinator_->AddObserver(this); | 267 request_coordinator_->AddObserver(this); |
| 256 | 268 |
| 257 state_ = State::LOADED; | 269 state_ = State::LOADED; |
| 258 for (Observer& observer : observers_) | 270 for (Observer& observer : observers_) |
| 259 observer.ItemsLoaded(); | 271 observer.ItemsLoaded(); |
| 260 } | 272 } |
| 261 | 273 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 278 // notification and when page is added, fire 'updated' instead of 'added'. | 290 // notification and when page is added, fire 'updated' instead of 'added'. |
| 279 bool request_to_page_transition = | 291 bool request_to_page_transition = |
| 280 (it != items_.end() && it->second->is_request && !item_info->is_request); | 292 (it != items_.end() && it->second->is_request && !item_info->is_request); |
| 281 | 293 |
| 282 items_[guid] = std::move(item_info); | 294 items_[guid] = std::move(item_info); |
| 283 | 295 |
| 284 if (state_ != State::LOADED) | 296 if (state_ != State::LOADED) |
| 285 return; | 297 return; |
| 286 | 298 |
| 287 DownloadUIItem* download_ui_item = items_[guid]->ui_item.get(); | 299 DownloadUIItem* download_ui_item = items_[guid]->ui_item.get(); |
| 300 |
| 288 if (request_to_page_transition) { | 301 if (request_to_page_transition) { |
| 289 download_ui_item->download_state = DownloadUIItem::DownloadState::COMPLETE; | 302 download_ui_item->download_state = DownloadUIItem::DownloadState::COMPLETE; |
| 290 for (Observer& observer : observers_) | 303 if (!items_[guid]->temporarily_hidden) { |
| 291 observer.ItemUpdated(*download_ui_item); | 304 for (Observer& observer : observers_) |
| 305 observer.ItemUpdated(*download_ui_item); |
| 306 } |
| 292 } else { | 307 } else { |
| 293 for (Observer& observer : observers_) | 308 if (!items_[guid]->temporarily_hidden) { |
| 294 observer.ItemAdded(*download_ui_item); | 309 for (Observer& observer : observers_) |
| 310 observer.ItemAdded(*download_ui_item); |
| 311 } |
| 295 } | 312 } |
| 296 } | 313 } |
| 297 | 314 |
| 298 void DownloadUIAdapter::DeleteItemHelper(const std::string& guid) { | 315 void DownloadUIAdapter::DeleteItemHelper(const std::string& guid) { |
| 299 DownloadUIItems::const_iterator it = items_.find(guid); | 316 DownloadUIItems::iterator it = items_.find(guid); |
| 300 if (it == items_.end()) | 317 if (it == items_.end()) |
| 301 return; | 318 return; |
| 319 DCHECK(deleting_item_ == nullptr); |
| 320 deleting_item_ = std::move(it->second); |
| 302 items_.erase(it); | 321 items_.erase(it); |
| 303 | 322 |
| 304 if (state_ != State::LOADED) | 323 if (!deleting_item_->temporarily_hidden && state_ == State::LOADED) { |
| 305 return; | 324 for (Observer& observer : observers_) |
| 325 observer.ItemDeleted(guid); |
| 326 } |
| 306 | 327 |
| 307 for (Observer& observer : observers_) | 328 deleting_item_.reset(); |
| 308 observer.ItemDeleted(guid); | |
| 309 } | 329 } |
| 310 | 330 |
| 311 } // namespace offline_pages | 331 } // namespace offline_pages |
| OLD | NEW |