| 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 "base/callback.h" |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 11 #include "components/offline_pages/background/offliner_factory.h" | 12 #include "components/offline_pages/background/offliner_factory.h" |
| 12 #include "components/offline_pages/background/offliner_policy.h" | 13 #include "components/offline_pages/background/offliner_policy.h" |
| 13 #include "components/offline_pages/background/request_picker.h" | 14 #include "components/offline_pages/background/request_picker.h" |
| 14 #include "components/offline_pages/background/save_page_request.h" | 15 #include "components/offline_pages/background/save_page_request.h" |
| 15 #include "components/offline_pages/background/scheduler.h" | 16 #include "components/offline_pages/background/scheduler.h" |
| 16 #include "components/offline_pages/offline_page_item.h" | 17 #include "components/offline_pages/offline_page_item.h" |
| 17 | 18 |
| 18 namespace offline_pages { | 19 namespace offline_pages { |
| 19 | 20 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 // Send the request on to the offliner. | 72 // Send the request on to the offliner. |
| 72 SendRequestToOffliner(request); | 73 SendRequestToOffliner(request); |
| 73 } | 74 } |
| 74 | 75 |
| 75 void RequestCoordinator::RequestQueueEmpty() { | 76 void RequestCoordinator::RequestQueueEmpty() { |
| 76 // TODO(petewil): return to the BackgroundScheduler by calling | 77 // TODO(petewil): return to the BackgroundScheduler by calling |
| 77 // ProcessingDoneCallback | 78 // ProcessingDoneCallback |
| 78 } | 79 } |
| 79 | 80 |
| 80 bool RequestCoordinator::StartProcessing( | 81 bool RequestCoordinator::StartProcessing( |
| 81 const ProcessingDoneCallback& callback) { | 82 const base::Callback<void(bool)>& callback) { |
| 82 // TODO(petewil): Check existing conditions (should be passed down from | 83 // TODO(petewil): Check existing conditions (should be passed down from |
| 83 // BackgroundTask) | 84 // BackgroundTask) |
| 84 | 85 |
| 85 // Choose a request to process that meets the available conditions. | 86 // Choose a request to process that meets the available conditions. |
| 86 // This is an async call, and returns right away. | 87 // This is an async call, and returns right away. |
| 87 picker_->ChooseNextRequest( | 88 picker_->ChooseNextRequest( |
| 88 base::Bind(&RequestCoordinator::RequestPicked, | 89 base::Bind(&RequestCoordinator::RequestPicked, |
| 89 weak_ptr_factory_.GetWeakPtr()), | 90 weak_ptr_factory_.GetWeakPtr()), |
| 90 base::Bind(&RequestCoordinator::RequestQueueEmpty, | 91 base::Bind(&RequestCoordinator::RequestQueueEmpty, |
| 91 weak_ptr_factory_.GetWeakPtr())); | 92 weak_ptr_factory_.GetWeakPtr())); |
| 92 return false; | 93 return false; |
| 93 } | 94 } |
| 94 | 95 |
| 95 void RequestCoordinator::StopProcessing() { | 96 void RequestCoordinator::StopProcessing() { |
| 96 } | 97 } |
| 97 | 98 |
| 98 void RequestCoordinator::SendRequestToOffliner(const SavePageRequest& request) { | 99 void RequestCoordinator::SendRequestToOffliner(const SavePageRequest& request) { |
| 100 // TODO(petewil): Ensure only one offliner at a time is used. |
| 99 // TODO(petewil): When we have multiple offliners, we need to pick one. | 101 // TODO(petewil): When we have multiple offliners, we need to pick one. |
| 100 Offliner* offliner = factory_->GetOffliner(policy_.get()); | 102 Offliner* offliner = factory_->GetOffliner(policy_.get()); |
| 101 if (!offliner) { | 103 if (!offliner) { |
| 102 DVLOG(0) << "Unable to create Offliner. " | 104 DVLOG(0) << "Unable to create Offliner. " |
| 103 << "Cannot background offline page."; | 105 << "Cannot background offline page."; |
| 104 return; | 106 return; |
| 105 } | 107 } |
| 106 | 108 |
| 107 // Start the load and save process in the offliner (Async). | 109 // Start the load and save process in the offliner (Async). |
| 108 offliner->LoadAndSave(request, | 110 offliner->LoadAndSave(request, |
| 109 base::Bind(&RequestCoordinator::OfflinerDoneCallback, | 111 base::Bind(&RequestCoordinator::OfflinerDoneCallback, |
| 110 weak_ptr_factory_.GetWeakPtr())); | 112 weak_ptr_factory_.GetWeakPtr())); |
| 111 } | 113 } |
| 112 | 114 |
| 113 void RequestCoordinator::OfflinerDoneCallback(const SavePageRequest& request, | 115 void RequestCoordinator::OfflinerDoneCallback(const SavePageRequest& request, |
| 114 Offliner::RequestStatus status) { | 116 Offliner::RequestStatus status) { |
| 115 DVLOG(2) << "offliner finished, saved: " | 117 DVLOG(2) << "offliner finished, saved: " |
| 116 << (status == Offliner::RequestStatus::SAVED) << ", " | 118 << (status == Offliner::RequestStatus::SAVED) << ", " |
| 117 << __FUNCTION__; | 119 << __FUNCTION__; |
| 118 last_offlining_status_ = status; | 120 last_offlining_status_ = status; |
| 119 | 121 |
| 120 // TODO(petewil): Check time budget. Start a request if we have time, return | 122 // TODO(petewil): Check time budget. Start a request if we have time, return |
| 121 // to the scheduler if we are out of time. | 123 // to the scheduler if we are out of time. |
| 122 | 124 |
| 123 // TODO(petewil): If the request succeeded, remove it from the Queue. | 125 // TODO(petewil): If the request succeeded, remove it from the Queue. |
| 124 } | 126 } |
| 125 | 127 |
| 126 } // namespace offline_pages | 128 } // namespace offline_pages |
| OLD | NEW |