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