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/downloads/offline_page_download_notifier.h" |
| 10 |
| 11 namespace offline_pages { |
| 12 namespace { |
| 13 int kUserDataKey; // Only address is used. |
| 14 } // namespace |
| 15 |
| 16 DownloadNotifyingObserver::DownloadNotifyingObserver( |
| 17 std::unique_ptr<OfflinePageDownloadNotifier> notifier) |
| 18 : notifier_(std::move(notifier)) {} |
| 19 |
| 20 DownloadNotifyingObserver::~DownloadNotifyingObserver() {} |
| 21 |
| 22 // static |
| 23 DownloadNotifyingObserver* DownloadNotifyingObserver::GetFromRequestCoordinator( |
| 24 RequestCoordinator* request_coordinator) { |
| 25 DCHECK(request_coordinator); |
| 26 return static_cast<DownloadNotifyingObserver*>( |
| 27 request_coordinator->GetUserData(&kUserDataKey)); |
| 28 } |
| 29 |
| 30 // static |
| 31 void DownloadNotifyingObserver::CreateAndStartObserving( |
| 32 RequestCoordinator* request_coordinator, |
| 33 std::unique_ptr<OfflinePageDownloadNotifier> notifier) { |
| 34 DCHECK(request_coordinator); |
| 35 DCHECK(notifier.get()); |
| 36 DownloadNotifyingObserver* observer = |
| 37 new DownloadNotifyingObserver(std::move(notifier)); |
| 38 request_coordinator->AddObserver(observer); |
| 39 // |request_coordinator| takes ownership of observer here. |
| 40 request_coordinator->SetUserData(&kUserDataKey, observer); |
| 41 } |
| 42 |
| 43 void DownloadNotifyingObserver::OnAdded(const SavePageRequest& request) { |
| 44 DCHECK(notifier_.get()); |
| 45 notifier_->NotifyDownloadProgress(DownloadUIItem(request)); |
| 46 } |
| 47 |
| 48 void DownloadNotifyingObserver::OnChanged(const SavePageRequest& request) { |
| 49 DCHECK(notifier_.get()); |
| 50 if (request.request_state() == SavePageRequest::RequestState::PAUSED) |
| 51 notifier_->NotifyDownloadPaused(DownloadUIItem(request)); |
| 52 else |
| 53 notifier_->NotifyDownloadProgress(DownloadUIItem(request)); |
| 54 } |
| 55 |
| 56 void DownloadNotifyingObserver::OnCompleted( |
| 57 const SavePageRequest& request, |
| 58 RequestCoordinator::SavePageStatus status) { |
| 59 DCHECK(notifier_.get()); |
| 60 if (status == RequestCoordinator::SavePageStatus::SUCCESS) |
| 61 notifier_->NotifyDownloadSuccessful(DownloadUIItem(request)); |
| 62 else if (status == RequestCoordinator::SavePageStatus::REMOVED) |
| 63 notifier_->NotifyDownloadCanceled(DownloadUIItem(request)); |
| 64 else |
| 65 notifier_->NotifyDownloadFailed(DownloadUIItem(request)); |
| 66 } |
| 67 } // namespace offline_pages |
OLD | NEW |