Chromium Code Reviews| Index: components/offline_pages/downloads/download_notifying_observer.cc |
| diff --git a/components/offline_pages/downloads/download_notifying_observer.cc b/components/offline_pages/downloads/download_notifying_observer.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d02c6c33e6e5ab24d418984092c5386f46589073 |
| --- /dev/null |
| +++ b/components/offline_pages/downloads/download_notifying_observer.cc |
| @@ -0,0 +1,66 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/offline_pages/downloads/download_notifying_observer.h" |
| + |
| +#include "components/offline_pages/background/request_coordinator.h" |
| +#include "components/offline_pages/background/save_page_request.h" |
| +#include "components/offline_pages/downloads/offline_page_download_notifier.h" |
| + |
| +namespace offline_pages { |
| +namespace { |
| +int kUserDataKey; // Only address is used. |
| +} // namespace |
| + |
| +DownloadNotifyingObserver::DownloadNotifyingObserver( |
| + std::unique_ptr<OfflinePageDownloadNotifier> notifier) |
| + : notifier_(std::move(notifier)) {} |
| + |
| +DownloadNotifyingObserver::~DownloadNotifyingObserver() {} |
| + |
| +// static |
| +DownloadNotifyingObserver* DownloadNotifyingObserver::GetFromRequestCoordinator( |
| + RequestCoordinator* request_coordinator) { |
| + DCHECK(request_coordinator); |
| + return static_cast<DownloadNotifyingObserver*>( |
| + request_coordinator->GetUserData(&kUserDataKey)); |
| +} |
| + |
| +// static |
| +void DownloadNotifyingObserver::CreateAndStartObserving( |
| + RequestCoordinator* request_coordinator, |
| + std::unique_ptr<OfflinePageDownloadNotifier> notifier) { |
| + DCHECK(request_coordinator); |
| + DCHECK(notifier.get()); |
| + DownloadNotifyingObserver* observer = |
| + new DownloadNotifyingObserver(std::move(notifier)); |
| + request_coordinator->AddObserver(observer); |
| + // |request_coordinator| takes ownership of observer here. |
| + request_coordinator->SetUserData(&kUserDataKey, observer); |
| +} |
| + |
| +void DownloadNotifyingObserver::OnAdded(const SavePageRequest& request) { |
| + DCHECK(notifier_.get()); |
| + notifier_->NotifyDownloadProgress(DownloadUIItem(request)); |
| +} |
| + |
| +void DownloadNotifyingObserver::OnChanged(const SavePageRequest& request) { |
| + DCHECK(notifier_.get()); |
| + // TODO(fgorski): Discuss change related logic with the team. |
|
Pete Williamson
2016/08/24 17:02:41
What discussion do we need to have? (OK to answer
fgorski
2016/08/24 18:25:23
For now I think this is OK. I believe we actually
|
| + if (request.request_state() == SavePageRequest::RequestState::PAUSED) |
| + notifier_->NotifyDownloadPaused(DownloadUIItem(request)); |
| + else |
| + notifier_->NotifyDownloadProgress(DownloadUIItem(request)); |
| +} |
| + |
| +void DownloadNotifyingObserver::OnCompleted( |
| + const SavePageRequest& request, |
| + RequestCoordinator::SavePageStatus status) { |
| + DCHECK(notifier_.get()); |
| + if (status == RequestCoordinator::SavePageStatus::SUCCESS) |
|
Pete Williamson
2016/08/24 17:02:41
Is it OK at this level to treat removed requests a
fgorski
2016/08/24 18:25:23
Done. We can cancel as an alternative.
|
| + notifier_->NotifyDownloadSuccessful(DownloadUIItem(request)); |
| + else |
| + notifier_->NotifyDownloadFailed(DownloadUIItem(request)); |
| +} |
| +} // namespace offline_pages |