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