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 // 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
| |
51 if (request.request_state() == SavePageRequest::RequestState::PAUSED) | |
52 notifier_->NotifyDownloadPaused(DownloadUIItem(request)); | |
53 else | |
54 notifier_->NotifyDownloadProgress(DownloadUIItem(request)); | |
55 } | |
56 | |
57 void DownloadNotifyingObserver::OnCompleted( | |
58 const SavePageRequest& request, | |
59 RequestCoordinator::SavePageStatus status) { | |
60 DCHECK(notifier_.get()); | |
61 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.
| |
62 notifier_->NotifyDownloadSuccessful(DownloadUIItem(request)); | |
63 else | |
64 notifier_->NotifyDownloadFailed(DownloadUIItem(request)); | |
65 } | |
66 } // namespace offline_pages | |
OLD | NEW |