| 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/background/request_coordinator.h" | 5 #include "components/offline_pages/background/request_coordinator.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "components/offline_pages/background/offliner_factory.h" | 10 #include "components/offline_pages/background/offliner_factory.h" |
| 11 #include "components/offline_pages/background/offliner_policy.h" | 11 #include "components/offline_pages/background/offliner_policy.h" |
| 12 #include "components/offline_pages/background/save_page_request.h" | 12 #include "components/offline_pages/background/save_page_request.h" |
| 13 #include "components/offline_pages/background/scheduler.h" | 13 #include "components/offline_pages/background/scheduler.h" |
| 14 #include "components/offline_pages/offline_page_item.h" | 14 #include "components/offline_pages/offline_page_item.h" |
| 15 | 15 |
| 16 namespace offline_pages { | 16 namespace offline_pages { |
| 17 | 17 |
| 18 RequestCoordinator::RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy, | 18 RequestCoordinator::RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy, |
| 19 std::unique_ptr<OfflinerFactory> factory, | 19 std::unique_ptr<OfflinerFactory> factory, |
| 20 std::unique_ptr<RequestQueue> queue, | 20 std::unique_ptr<RequestQueue> queue, |
| 21 std::unique_ptr<Scheduler> scheduler) | 21 std::unique_ptr<Scheduler> scheduler) |
| 22 : policy_(std::move(policy)), | 22 : policy_(std::move(policy)), |
| 23 factory_(std::move(factory)), | 23 factory_(std::move(factory)), |
| 24 queue_(std::move(queue)), | 24 queue_(std::move(queue)), |
| 25 scheduler_(std::move(scheduler)), | 25 scheduler_(std::move(scheduler)), |
| 26 last_offlining_status_(Offliner::UNKNOWN) { | 26 last_offlining_status_(Offliner::RequestStatus::UNKNOWN) { |
| 27 DCHECK(policy_ != nullptr); | 27 DCHECK(policy_ != nullptr); |
| 28 } | 28 } |
| 29 | 29 |
| 30 RequestCoordinator::~RequestCoordinator() {} | 30 RequestCoordinator::~RequestCoordinator() {} |
| 31 | 31 |
| 32 bool RequestCoordinator::SavePageLater( | 32 bool RequestCoordinator::SavePageLater( |
| 33 const GURL& url, const ClientId& client_id) { | 33 const GURL& url, const ClientId& client_id) { |
| 34 DVLOG(2) << "URL is " << url << " " << __FUNCTION__; | 34 DVLOG(2) << "URL is " << url << " " << __FUNCTION__; |
| 35 | 35 |
| 36 // TODO(petewil): We need a robust scheme for allocating new IDs. | 36 // TODO(petewil): We need a robust scheme for allocating new IDs. |
| 37 static int64_t id = 0; | 37 static int64_t id = 0; |
| 38 | 38 |
| 39 // Build a SavePageRequest. | 39 // Build a SavePageRequest. |
| 40 // TODO(petewil): Use something like base::Clock to help in testing. | 40 // TODO(petewil): Use something like base::Clock to help in testing. |
| 41 offline_pages::SavePageRequest request( | 41 offline_pages::SavePageRequest request( |
| 42 id++, url, client_id, base::Time::Now()); | 42 id++, url, client_id, base::Time::Now()); |
| 43 | 43 |
| 44 // Put the request on the request queue. | 44 // Put the request on the request queue. |
| 45 queue_->AddRequest(request, | 45 queue_->AddRequest(request, |
| 46 base::Bind(&RequestCoordinator::AddRequestResultCallback, | 46 base::Bind(&RequestCoordinator::AddRequestResultCallback, |
| 47 AsWeakPtr())); | 47 AsWeakPtr())); |
| 48 // TODO: Do I need to persist the request in case the add fails? | 48 // TODO(petewil): Do I need to persist the request in case the add fails? |
| 49 | 49 |
| 50 // TODO(petewil): Eventually we will wait for the StartProcessing callback, | 50 // TODO(petewil): Eventually we will wait for the StartProcessing callback, |
| 51 // but for now just kick start the request so we can test the wiring. | 51 // but for now just kick start the request so we can test the wiring. |
| 52 SendRequestToOffliner(request); | 52 SendRequestToOffliner(request); |
| 53 | 53 |
| 54 return true; | 54 return true; |
| 55 } | 55 } |
| 56 | 56 |
| 57 void RequestCoordinator::AddRequestResultCallback( | 57 void RequestCoordinator::AddRequestResultCallback( |
| 58 RequestQueue::AddRequestResult result, | 58 RequestQueue::AddRequestResult result, |
| 59 const SavePageRequest& request) { | 59 const SavePageRequest& request) { |
| 60 DVLOG(2) << __FUNCTION__; | 60 DVLOG(2) << __FUNCTION__; |
| 61 | 61 |
| 62 // Inform the scheduler that we have an outstanding task. | 62 // Inform the scheduler that we have an outstanding task. |
| 63 // TODO(petewil): Define proper TriggerConditions and set them. | 63 // TODO(petewil): Define proper TriggerConditions and set them. |
| 64 Scheduler::TriggerCondition conditions; | 64 Scheduler::TriggerCondition conditions; |
| 65 scheduler_->Schedule(conditions); | 65 scheduler_->Schedule(conditions); |
| 66 } | 66 } |
| 67 | 67 |
| 68 bool RequestCoordinator::StartProcessing( | 68 bool RequestCoordinator::StartProcessing( |
| 69 const ProcessingDoneCallback& callback) { | 69 const ProcessingDoneCallback& callback) { |
| 70 return false; | 70 return false; |
| 71 } | 71 } |
| 72 | 72 |
| 73 void RequestCoordinator::StopProcessing() { | 73 void RequestCoordinator::StopProcessing() { |
| 74 } | 74 } |
| 75 | 75 |
| 76 void RequestCoordinator::SendRequestToOffliner(SavePageRequest& request) { | 76 void RequestCoordinator::SendRequestToOffliner(const SavePageRequest& request) { |
| 77 // TODO(petewil): When we have multiple offliners, we need to pick one. | 77 // TODO(petewil): When we have multiple offliners, we need to pick one. |
| 78 Offliner* offliner = factory_->GetOffliner(policy_.get()); | 78 Offliner* offliner = factory_->GetOffliner(policy_.get()); |
| 79 if (!offliner) { | 79 if (!offliner) { |
| 80 LOG(ERROR) << "Unable to create Prerendering Offliner. " | 80 DVLOG(0) << "Unable to create Offliner. " |
| 81 << "Cannot background offline page."; | 81 << "Cannot background offline page."; |
| 82 return; | 82 return; |
| 83 } | 83 } |
| 84 | 84 |
| 85 // Start the load and save process in the prerenderer (Async). | 85 // Start the load and save process in the offliner (Async). |
| 86 offliner->LoadAndSave( | 86 offliner->LoadAndSave( |
| 87 request, | 87 request, |
| 88 base::Bind(&RequestCoordinator::OfflinerDoneCallback, AsWeakPtr())); | 88 base::Bind(&RequestCoordinator::OfflinerDoneCallback, AsWeakPtr())); |
| 89 } | 89 } |
| 90 | 90 |
| 91 void RequestCoordinator::OfflinerDoneCallback( | 91 void RequestCoordinator::OfflinerDoneCallback(const SavePageRequest& request, |
| 92 const SavePageRequest& request, | 92 Offliner::RequestStatus status) { |
| 93 Offliner::CompletionStatus status) { | 93 DVLOG(2) << "offliner finished, saved: " |
| 94 DVLOG(2) << "prerenderer finished, status " << status << ", " << __FUNCTION__; | 94 << (status == Offliner::RequestStatus::SAVED) << ", " |
| 95 << __FUNCTION__; |
| 95 last_offlining_status_ = status; | 96 last_offlining_status_ = status; |
| 96 } | 97 } |
| 97 | 98 |
| 98 } // namespace offline_pages | 99 } // namespace offline_pages |
| OLD | NEW |