| 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" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 32 } | 32 } |
| 33 | 33 |
| 34 // static | 34 // static |
| 35 void DownloadUIAdapter::AttachToOfflinePageModel(DownloadUIAdapter* adapter, | 35 void DownloadUIAdapter::AttachToOfflinePageModel(DownloadUIAdapter* adapter, |
| 36 OfflinePageModel* model) { | 36 OfflinePageModel* model) { |
| 37 DCHECK(adapter); | 37 DCHECK(adapter); |
| 38 DCHECK(model); | 38 DCHECK(model); |
| 39 model->SetUserData(kDownloadUIAdapterKey, adapter); | 39 model->SetUserData(kDownloadUIAdapterKey, adapter); |
| 40 } | 40 } |
| 41 | 41 |
| 42 DownloadUIAdapter::ItemInfo::ItemInfo(const OfflinePageItem& page) | 42 DownloadUIAdapter::ItemInfo::ItemInfo(const OfflinePageItem& page, |
| 43 bool temporarily_hidden) |
| 43 : ui_item(base::MakeUnique<DownloadUIItem>(page)), | 44 : ui_item(base::MakeUnique<DownloadUIItem>(page)), |
| 44 is_request(false), | 45 is_request(false), |
| 45 offline_id(page.offline_id), | 46 offline_id(page.offline_id), |
| 46 client_id(page.client_id), | 47 client_id(page.client_id), |
| 47 temporarily_hidden(false) {} | 48 temporarily_hidden(temporarily_hidden) {} |
| 48 | 49 |
| 49 DownloadUIAdapter::ItemInfo::ItemInfo(const SavePageRequest& request) | 50 DownloadUIAdapter::ItemInfo::ItemInfo(const SavePageRequest& request, |
| 51 bool temporarily_hidden) |
| 50 : ui_item(base::MakeUnique<DownloadUIItem>(request)), | 52 : ui_item(base::MakeUnique<DownloadUIItem>(request)), |
| 51 is_request(true), | 53 is_request(true), |
| 52 offline_id(request.request_id()), | 54 offline_id(request.request_id()), |
| 53 client_id(request.client_id()), | 55 client_id(request.client_id()), |
| 54 temporarily_hidden(false) {} | 56 temporarily_hidden() {} |
| 55 | 57 |
| 56 DownloadUIAdapter::ItemInfo::~ItemInfo() {} | 58 DownloadUIAdapter::ItemInfo::~ItemInfo() {} |
| 57 | 59 |
| 58 DownloadUIAdapter::DownloadUIAdapter(OfflinePageModel* model, | 60 DownloadUIAdapter::DownloadUIAdapter(OfflinePageModel* model, |
| 59 RequestCoordinator* request_coordinator, | 61 RequestCoordinator* request_coordinator, |
| 60 std::unique_ptr<Delegate> delegate) | 62 std::unique_ptr<Delegate> delegate) |
| 61 : model_(model), | 63 : model_(model), |
| 62 request_coordinator_(request_coordinator), | 64 request_coordinator_(request_coordinator), |
| 63 delegate_(std::move(delegate)), | 65 delegate_(std::move(delegate)), |
| 64 state_(State::NOT_LOADED), | 66 state_(State::NOT_LOADED), |
| 65 observers_count_(0), | 67 observers_count_(0), |
| 66 weak_ptr_factory_(this) {} | 68 weak_ptr_factory_(this) { |
| 69 delegate_->SetUIAdapter(this); |
| 70 } |
| 67 | 71 |
| 68 DownloadUIAdapter::~DownloadUIAdapter() {} | 72 DownloadUIAdapter::~DownloadUIAdapter() {} |
| 69 | 73 |
| 70 void DownloadUIAdapter::AddObserver(Observer* observer) { | 74 void DownloadUIAdapter::AddObserver(Observer* observer) { |
| 71 DCHECK(observer); | 75 DCHECK(observer); |
| 72 if (observers_.HasObserver(observer)) | 76 if (observers_.HasObserver(observer)) |
| 73 return; | 77 return; |
| 74 if (observers_count_ == 0) | 78 if (observers_count_ == 0) |
| 75 LoadCache(); | 79 LoadCache(); |
| 76 observers_.AddObserver(observer); | 80 observers_.AddObserver(observer); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 99 void DownloadUIAdapter::OfflinePageModelLoaded(OfflinePageModel* model) { | 103 void DownloadUIAdapter::OfflinePageModelLoaded(OfflinePageModel* model) { |
| 100 // This signal is not used here. | 104 // This signal is not used here. |
| 101 } | 105 } |
| 102 | 106 |
| 103 void DownloadUIAdapter::OfflinePageAdded(OfflinePageModel* model, | 107 void DownloadUIAdapter::OfflinePageAdded(OfflinePageModel* model, |
| 104 const OfflinePageItem& added_page) { | 108 const OfflinePageItem& added_page) { |
| 105 DCHECK(model == model_); | 109 DCHECK(model == model_); |
| 106 if (!delegate_->IsVisibleInUI(added_page.client_id)) | 110 if (!delegate_->IsVisibleInUI(added_page.client_id)) |
| 107 return; | 111 return; |
| 108 | 112 |
| 109 AddItemHelper(base::MakeUnique<ItemInfo>(added_page)); | 113 bool temporarily_hidden = |
| 114 delegate_->IsTemporarilyHiddenInUI(added_page.client_id); |
| 115 AddItemHelper(base::MakeUnique<ItemInfo>(added_page, temporarily_hidden)); |
| 110 } | 116 } |
| 111 | 117 |
| 112 void DownloadUIAdapter::OfflinePageDeleted(int64_t offline_id, | 118 void DownloadUIAdapter::OfflinePageDeleted(int64_t offline_id, |
| 113 const ClientId& client_id) { | 119 const ClientId& client_id) { |
| 114 if (!delegate_->IsVisibleInUI(client_id)) | 120 if (!delegate_->IsVisibleInUI(client_id)) |
| 115 return; | 121 return; |
| 116 DeleteItemHelper(client_id.id); | 122 DeleteItemHelper(client_id.id); |
| 117 } | 123 } |
| 118 | 124 |
| 119 // RequestCoordinator::Observer | 125 // RequestCoordinator::Observer |
| 120 void DownloadUIAdapter::OnAdded(const SavePageRequest& added_request) { | 126 void DownloadUIAdapter::OnAdded(const SavePageRequest& added_request) { |
| 121 if (!delegate_->IsVisibleInUI(added_request.client_id())) | 127 if (!delegate_->IsVisibleInUI(added_request.client_id())) |
| 122 return; | 128 return; |
| 123 | 129 |
| 124 AddItemHelper(base::MakeUnique<ItemInfo>(added_request)); | 130 bool temporarily_hidden = |
| 131 delegate_->IsTemporarilyHiddenInUI(added_request.client_id()); |
| 132 AddItemHelper(base::MakeUnique<ItemInfo>(added_request, temporarily_hidden)); |
| 125 } | 133 } |
| 126 | 134 |
| 127 // RequestCoordinator::Observer | 135 // RequestCoordinator::Observer |
| 128 void DownloadUIAdapter::OnCompleted( | 136 void DownloadUIAdapter::OnCompleted( |
| 129 const SavePageRequest& request, | 137 const SavePageRequest& request, |
| 130 RequestNotifier::BackgroundSavePageResult status) { | 138 RequestNotifier::BackgroundSavePageResult status) { |
| 131 if (!delegate_->IsVisibleInUI(request.client_id())) | 139 if (!delegate_->IsVisibleInUI(request.client_id())) |
| 132 return; | 140 return; |
| 133 | 141 |
| 134 // If request completed successfully, report ItemUpdated when a page is added | 142 // If request completed successfully, report ItemUpdated when a page is added |
| 135 // to the model. If the request failed, tell UI that the item is gone. | 143 // to the model. If the request failed, tell UI that the item is gone. |
| 136 if (status == RequestNotifier::BackgroundSavePageResult::SUCCESS) | 144 if (status == RequestNotifier::BackgroundSavePageResult::SUCCESS) |
| 137 return; | 145 return; |
| 138 DeleteItemHelper(request.client_id().id); | 146 DeleteItemHelper(request.client_id().id); |
| 139 } | 147 } |
| 140 | 148 |
| 141 // RequestCoordinator::Observer | 149 // RequestCoordinator::Observer |
| 142 void DownloadUIAdapter::OnChanged(const SavePageRequest& request) { | 150 void DownloadUIAdapter::OnChanged(const SavePageRequest& request) { |
| 143 if (!delegate_->IsVisibleInUI(request.client_id())) | 151 if (!delegate_->IsVisibleInUI(request.client_id())) |
| 144 return; | 152 return; |
| 145 | 153 |
| 146 std::string guid = request.client_id().id; | 154 std::string guid = request.client_id().id; |
| 147 items_[guid] = base::MakeUnique<ItemInfo>(request); | 155 bool temporarily_hidden = |
| 156 delegate_->IsTemporarilyHiddenInUI(request.client_id()); |
| 157 items_[guid] = base::MakeUnique<ItemInfo>(request, temporarily_hidden); |
| 148 | 158 |
| 149 if (state_ != State::LOADED) | 159 if (state_ != State::LOADED) |
| 150 return; | 160 return; |
| 151 | 161 |
| 152 const DownloadUIItem& download_ui_item = *(items_[guid]->ui_item); | 162 const DownloadUIItem& download_ui_item = *(items_[guid]->ui_item); |
| 153 for (Observer& observer : observers_) | 163 for (Observer& observer : observers_) |
| 154 observer.ItemUpdated(download_ui_item); | 164 observer.ItemUpdated(download_ui_item); |
| 155 } | 165 } |
| 156 | 166 |
| 157 void DownloadUIAdapter::TemporaryHiddenStatusChanged( | 167 void DownloadUIAdapter::TemporaryHiddenStatusChanged( |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 return; | 210 return; |
| 201 | 211 |
| 202 std::vector<int64_t> page_ids; | 212 std::vector<int64_t> page_ids; |
| 203 page_ids.push_back(it->second->offline_id); | 213 page_ids.push_back(it->second->offline_id); |
| 204 model_->DeletePagesByOfflineId( | 214 model_->DeletePagesByOfflineId( |
| 205 page_ids, base::Bind(&DownloadUIAdapter::OnDeletePagesDone, | 215 page_ids, base::Bind(&DownloadUIAdapter::OnDeletePagesDone, |
| 206 weak_ptr_factory_.GetWeakPtr())); | 216 weak_ptr_factory_.GetWeakPtr())); |
| 207 } | 217 } |
| 208 | 218 |
| 209 int64_t DownloadUIAdapter::GetOfflineIdByGuid(const std::string& guid) const { | 219 int64_t DownloadUIAdapter::GetOfflineIdByGuid(const std::string& guid) const { |
| 220 if (deleting_item_ && deleting_item_->ui_item->guid == guid) |
| 221 return deleting_item_->offline_id; |
| 222 |
| 210 DownloadUIItems::const_iterator it = items_.find(guid); | 223 DownloadUIItems::const_iterator it = items_.find(guid); |
| 211 if (it != items_.end()) | 224 if (it != items_.end()) |
| 212 return it->second->offline_id; | 225 return it->second->offline_id; |
| 213 return 0; | 226 return 0; |
| 214 } | 227 } |
| 215 | 228 |
| 216 void DownloadUIAdapter::UpdateProgress(int64_t offline_id, int64_t bytes) { | 229 void DownloadUIAdapter::UpdateProgress(int64_t offline_id, int64_t bytes) { |
| 217 for (auto& item : items_) { | 230 for (auto& item : items_) { |
| 218 if (item.second->is_request && item.second->offline_id == offline_id) { | 231 if (item.second->is_request && item.second->offline_id == offline_id) { |
| 219 if (bytes == item.second->ui_item->download_progress_bytes) | 232 if (bytes == item.second->ui_item->download_progress_bytes) |
| (...skipping 29 matching lines...) Expand all Loading... |
| 249 const MultipleOfflinePageItemResult& pages) { | 262 const MultipleOfflinePageItemResult& pages) { |
| 250 // If multiple observers register quickly, the cache might be already loaded | 263 // If multiple observers register quickly, the cache might be already loaded |
| 251 // by the previous LoadCache call. At the same time, if all observers already | 264 // by the previous LoadCache call. At the same time, if all observers already |
| 252 // left, there is no reason to populate the cache. | 265 // left, there is no reason to populate the cache. |
| 253 if (state_ != State::LOADING_PAGES) | 266 if (state_ != State::LOADING_PAGES) |
| 254 return; | 267 return; |
| 255 for (const auto& page : pages) { | 268 for (const auto& page : pages) { |
| 256 if (delegate_->IsVisibleInUI(page.client_id)) { | 269 if (delegate_->IsVisibleInUI(page.client_id)) { |
| 257 std::string guid = page.client_id.id; | 270 std::string guid = page.client_id.id; |
| 258 DCHECK(items_.find(guid) == items_.end()); | 271 DCHECK(items_.find(guid) == items_.end()); |
| 259 std::unique_ptr<ItemInfo> item = base::MakeUnique<ItemInfo>(page); | 272 bool temporarily_hidden = |
| 260 item->temporarily_hidden = | |
| 261 delegate_->IsTemporarilyHiddenInUI(page.client_id); | 273 delegate_->IsTemporarilyHiddenInUI(page.client_id); |
| 274 std::unique_ptr<ItemInfo> item = |
| 275 base::MakeUnique<ItemInfo>(page, temporarily_hidden); |
| 262 items_[guid] = std::move(item); | 276 items_[guid] = std::move(item); |
| 263 } | 277 } |
| 264 } | 278 } |
| 265 model_->AddObserver(this); | 279 model_->AddObserver(this); |
| 266 | 280 |
| 267 state_ = State::LOADING_REQUESTS; | 281 state_ = State::LOADING_REQUESTS; |
| 268 request_coordinator_->GetAllRequests(base::Bind( | 282 request_coordinator_->GetAllRequests(base::Bind( |
| 269 &DownloadUIAdapter::OnRequestsLoaded, weak_ptr_factory_.GetWeakPtr())); | 283 &DownloadUIAdapter::OnRequestsLoaded, weak_ptr_factory_.GetWeakPtr())); |
| 270 } | 284 } |
| 271 | 285 |
| 272 void DownloadUIAdapter::OnRequestsLoaded( | 286 void DownloadUIAdapter::OnRequestsLoaded( |
| 273 std::vector<std::unique_ptr<SavePageRequest>> requests) { | 287 std::vector<std::unique_ptr<SavePageRequest>> requests) { |
| 274 // If multiple observers register quickly, the cache might be already loaded | 288 // If multiple observers register quickly, the cache might be already loaded |
| 275 // by the previous LoadCache call. At the same time, if all observers already | 289 // by the previous LoadCache call. At the same time, if all observers already |
| 276 // left, there is no reason to populate the cache. | 290 // left, there is no reason to populate the cache. |
| 277 if (state_ != State::LOADING_REQUESTS) | 291 if (state_ != State::LOADING_REQUESTS) |
| 278 return; | 292 return; |
| 279 | 293 |
| 280 for (const auto& request : requests) { | 294 for (const auto& request : requests) { |
| 281 if (delegate_->IsVisibleInUI(request->client_id())) { | 295 if (delegate_->IsVisibleInUI(request->client_id())) { |
| 282 std::string guid = request->client_id().id; | 296 std::string guid = request->client_id().id; |
| 283 DCHECK(items_.find(guid) == items_.end()); | 297 DCHECK(items_.find(guid) == items_.end()); |
| 298 bool temporarily_hidden = |
| 299 delegate_->IsTemporarilyHiddenInUI(request->client_id()); |
| 284 std::unique_ptr<ItemInfo> item = | 300 std::unique_ptr<ItemInfo> item = |
| 285 base::MakeUnique<ItemInfo>(*request.get()); | 301 base::MakeUnique<ItemInfo>(*request.get(), temporarily_hidden); |
| 286 item->temporarily_hidden = | |
| 287 delegate_->IsTemporarilyHiddenInUI(request->client_id()); | |
| 288 items_[guid] = std::move(item); | 302 items_[guid] = std::move(item); |
| 289 } | 303 } |
| 290 } | 304 } |
| 291 request_coordinator_->AddObserver(this); | 305 request_coordinator_->AddObserver(this); |
| 292 | 306 |
| 293 state_ = State::LOADED; | 307 state_ = State::LOADED; |
| 294 for (Observer& observer : observers_) | 308 for (Observer& observer : observers_) |
| 295 observer.ItemsLoaded(); | 309 observer.ItemsLoaded(); |
| 296 } | 310 } |
| 297 | 311 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 314 // notification and when page is added, fire 'updated' instead of 'added'. | 328 // notification and when page is added, fire 'updated' instead of 'added'. |
| 315 bool request_to_page_transition = | 329 bool request_to_page_transition = |
| 316 (it != items_.end() && it->second->is_request && !item_info->is_request); | 330 (it != items_.end() && it->second->is_request && !item_info->is_request); |
| 317 | 331 |
| 318 items_[guid] = std::move(item_info); | 332 items_[guid] = std::move(item_info); |
| 319 | 333 |
| 320 if (state_ != State::LOADED) | 334 if (state_ != State::LOADED) |
| 321 return; | 335 return; |
| 322 | 336 |
| 323 DownloadUIItem* download_ui_item = items_[guid]->ui_item.get(); | 337 DownloadUIItem* download_ui_item = items_[guid]->ui_item.get(); |
| 338 |
| 324 if (request_to_page_transition) { | 339 if (request_to_page_transition) { |
| 325 download_ui_item->download_state = DownloadUIItem::DownloadState::COMPLETE; | 340 download_ui_item->download_state = DownloadUIItem::DownloadState::COMPLETE; |
| 326 for (Observer& observer : observers_) | 341 if (!items_[guid]->temporarily_hidden) { |
| 327 observer.ItemUpdated(*download_ui_item); | 342 for (Observer& observer : observers_) |
| 343 observer.ItemUpdated(*download_ui_item); |
| 344 } |
| 328 } else { | 345 } else { |
| 329 for (Observer& observer : observers_) | 346 if (!items_[guid]->temporarily_hidden) { |
| 330 observer.ItemAdded(*download_ui_item); | 347 for (Observer& observer : observers_) |
| 348 observer.ItemAdded(*download_ui_item); |
| 349 } |
| 331 } | 350 } |
| 332 } | 351 } |
| 333 | 352 |
| 334 void DownloadUIAdapter::DeleteItemHelper(const std::string& guid) { | 353 void DownloadUIAdapter::DeleteItemHelper(const std::string& guid) { |
| 335 DownloadUIItems::const_iterator it = items_.find(guid); | 354 DownloadUIItems::iterator it = items_.find(guid); |
| 336 if (it == items_.end()) | 355 if (it == items_.end()) |
| 337 return; | 356 return; |
| 357 DCHECK(deleting_item_ == nullptr); |
| 358 deleting_item_ = std::move(it->second); |
| 338 items_.erase(it); | 359 items_.erase(it); |
| 339 | 360 |
| 340 if (state_ != State::LOADED) | 361 if (!deleting_item_->temporarily_hidden && state_ == State::LOADED) { |
| 341 return; | 362 for (Observer& observer : observers_) |
| 363 observer.ItemDeleted(guid); |
| 364 } |
| 342 | 365 |
| 343 for (Observer& observer : observers_) | 366 deleting_item_.reset(); |
| 344 observer.ItemDeleted(guid); | |
| 345 } | 367 } |
| 346 | 368 |
| 347 } // namespace offline_pages | 369 } // namespace offline_pages |
| OLD | NEW |