| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/offline_pages/downloads/download_notifying_observer.h" | |
| 6 | |
| 7 #include "components/offline_pages/background/request_coordinator.h" | |
| 8 #include "components/offline_pages/background/save_page_request.h" | |
| 9 #include "components/offline_pages/client_policy_controller.h" | |
| 10 #include "components/offline_pages/downloads/download_ui_adapter.h" | |
| 11 #include "components/offline_pages/downloads/offline_page_download_notifier.h" | |
| 12 | |
| 13 namespace offline_pages { | |
| 14 namespace { | |
| 15 int kUserDataKey; // Only address is used. | |
| 16 } // namespace | |
| 17 | |
| 18 DownloadNotifyingObserver::DownloadNotifyingObserver( | |
| 19 std::unique_ptr<OfflinePageDownloadNotifier> notifier, | |
| 20 ClientPolicyController* policy_controller) | |
| 21 : notifier_(std::move(notifier)), policy_controller_(policy_controller) {} | |
| 22 | |
| 23 DownloadNotifyingObserver::~DownloadNotifyingObserver() {} | |
| 24 | |
| 25 // static | |
| 26 DownloadNotifyingObserver* DownloadNotifyingObserver::GetFromRequestCoordinator( | |
| 27 RequestCoordinator* request_coordinator) { | |
| 28 DCHECK(request_coordinator); | |
| 29 return static_cast<DownloadNotifyingObserver*>( | |
| 30 request_coordinator->GetUserData(&kUserDataKey)); | |
| 31 } | |
| 32 | |
| 33 // static | |
| 34 void DownloadNotifyingObserver::CreateAndStartObserving( | |
| 35 RequestCoordinator* request_coordinator, | |
| 36 std::unique_ptr<OfflinePageDownloadNotifier> notifier) { | |
| 37 DCHECK(request_coordinator); | |
| 38 DCHECK(notifier.get()); | |
| 39 DownloadNotifyingObserver* observer = new DownloadNotifyingObserver( | |
| 40 std::move(notifier), request_coordinator->GetPolicyController()); | |
| 41 request_coordinator->AddObserver(observer); | |
| 42 // |request_coordinator| takes ownership of observer here. | |
| 43 request_coordinator->SetUserData(&kUserDataKey, observer); | |
| 44 } | |
| 45 | |
| 46 void DownloadNotifyingObserver::OnAdded(const SavePageRequest& request) { | |
| 47 DCHECK(notifier_.get()); | |
| 48 if (!IsVisibleInUI(request.client_id())) | |
| 49 return; | |
| 50 notifier_->NotifyDownloadProgress(DownloadUIItem(request)); | |
| 51 } | |
| 52 | |
| 53 void DownloadNotifyingObserver::OnChanged(const SavePageRequest& request) { | |
| 54 DCHECK(notifier_.get()); | |
| 55 if (!IsVisibleInUI(request.client_id())) | |
| 56 return; | |
| 57 if (request.request_state() == SavePageRequest::RequestState::PAUSED) | |
| 58 notifier_->NotifyDownloadPaused(DownloadUIItem(request)); | |
| 59 else | |
| 60 notifier_->NotifyDownloadProgress(DownloadUIItem(request)); | |
| 61 } | |
| 62 | |
| 63 void DownloadNotifyingObserver::OnCompleted( | |
| 64 const SavePageRequest& request, | |
| 65 RequestCoordinator::BackgroundSavePageResult status) { | |
| 66 DCHECK(notifier_.get()); | |
| 67 if (!IsVisibleInUI(request.client_id())) | |
| 68 return; | |
| 69 if (status == RequestCoordinator::BackgroundSavePageResult::SUCCESS) | |
| 70 notifier_->NotifyDownloadSuccessful(DownloadUIItem(request)); | |
| 71 else if (status == RequestCoordinator::BackgroundSavePageResult::REMOVED) | |
| 72 notifier_->NotifyDownloadCanceled(DownloadUIItem(request)); | |
| 73 else | |
| 74 notifier_->NotifyDownloadFailed(DownloadUIItem(request)); | |
| 75 } | |
| 76 | |
| 77 bool DownloadNotifyingObserver::IsVisibleInUI(const ClientId& page) { | |
| 78 return policy_controller_->IsSupportedByDownload(page.name_space) && | |
| 79 base::IsValidGUID(page.id); | |
| 80 } | |
| 81 | |
| 82 } // namespace offline_pages | |
| OLD | NEW |