| 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/callback.h" |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 | 54 |
| 55 bool RequestCoordinator::SavePageLater( | 55 bool RequestCoordinator::SavePageLater( |
| 56 const GURL& url, const ClientId& client_id, bool was_user_requested) { | 56 const GURL& url, const ClientId& client_id, bool was_user_requested) { |
| 57 DVLOG(2) << "URL is " << url << " " << __func__; | 57 DVLOG(2) << "URL is " << url << " " << __func__; |
| 58 | 58 |
| 59 // TODO(petewil): We need a robust scheme for allocating new IDs. We may need | 59 // TODO(petewil): We need a robust scheme for allocating new IDs. We may need |
| 60 // GUIDS for the downloads home design. | 60 // GUIDS for the downloads home design. |
| 61 static int64_t id = 0; | 61 static int64_t id = 0; |
| 62 | 62 |
| 63 // Build a SavePageRequest. | 63 // Build a SavePageRequest. |
| 64 // TODO(petewil): Use something like base::Clock to help in testing. | |
| 65 offline_pages::SavePageRequest request( | 64 offline_pages::SavePageRequest request( |
| 66 id++, url, client_id, base::Time::Now(), was_user_requested); | 65 id++, url, client_id, base::Time::Now(), was_user_requested); |
| 67 | 66 |
| 68 // Put the request on the request queue. | 67 // Put the request on the request queue. |
| 69 queue_->AddRequest(request, | 68 queue_->AddRequest(request, |
| 70 base::Bind(&RequestCoordinator::AddRequestResultCallback, | 69 base::Bind(&RequestCoordinator::AddRequestResultCallback, |
| 71 weak_ptr_factory_.GetWeakPtr())); | 70 weak_ptr_factory_.GetWeakPtr())); |
| 72 return true; | 71 return true; |
| 73 } | 72 } |
| 74 | 73 |
| 75 void RequestCoordinator::AddRequestResultCallback( | 74 void RequestCoordinator::AddRequestResultCallback( |
| 76 RequestQueue::AddRequestResult result, | 75 RequestQueue::AddRequestResult result, |
| 77 const SavePageRequest& request) { | 76 const SavePageRequest& request) { |
| 78 | 77 |
| 79 // Inform the scheduler that we have an outstanding task. | 78 // Inform the scheduler that we have an outstanding task.. |
| 80 // TODO(petewil): Determine trigger conditions from policy. | |
| 81 scheduler_->Schedule(GetTriggerConditionsForUserRequest()); | 79 scheduler_->Schedule(GetTriggerConditionsForUserRequest()); |
| 82 } | 80 } |
| 83 | 81 |
| 84 // Called in response to updating a request in the request queue. | 82 // Called in response to updating a request in the request queue. |
| 85 void RequestCoordinator::UpdateRequestCallback( | 83 void RequestCoordinator::UpdateRequestCallback( |
| 86 RequestQueue::UpdateRequestResult result) { | 84 RequestQueue::UpdateRequestResult result) { |
| 87 // If the request succeeded, nothing to do. If it failed, we can't really do | 85 // If the request succeeded, nothing to do. If it failed, we can't really do |
| 88 // much, so just log it. | 86 // much, so just log it. |
| 89 if (result != RequestQueue::UpdateRequestResult::SUCCESS) { | 87 if (result != RequestQueue::UpdateRequestResult::SUCCESS) { |
| 90 // TODO(petewil): Consider adding UMA or showing on offline-internals page. | 88 // TODO(petewil): Consider adding UMA or showing on offline-internals page. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 114 const base::Callback<void(bool)>& callback) { | 112 const base::Callback<void(bool)>& callback) { |
| 115 current_conditions_.reset(new DeviceConditions(device_conditions)); | 113 current_conditions_.reset(new DeviceConditions(device_conditions)); |
| 116 if (is_busy_) return false; | 114 if (is_busy_) return false; |
| 117 | 115 |
| 118 // Mark the time at which we started processing so we can check our time | 116 // Mark the time at which we started processing so we can check our time |
| 119 // budget. | 117 // budget. |
| 120 operation_start_time_ = base::Time::Now(); | 118 operation_start_time_ = base::Time::Now(); |
| 121 | 119 |
| 122 is_canceled_ = false; | 120 is_canceled_ = false; |
| 123 scheduler_callback_ = callback; | 121 scheduler_callback_ = callback; |
| 124 // TODO(petewil): Check existing conditions (should be passed down from | |
| 125 // BackgroundTask) | |
| 126 | 122 |
| 127 TryNextRequest(); | 123 TryNextRequest(); |
| 128 | 124 |
| 129 return true; | 125 return true; |
| 130 } | 126 } |
| 131 | 127 |
| 132 void RequestCoordinator::TryNextRequest() { | 128 void RequestCoordinator::TryNextRequest() { |
| 133 // If there is no time left in the budget, return to the scheduler. | 129 // If there is no time left in the budget, return to the scheduler. |
| 134 // We do not remove the pending task that was set up earlier in case | 130 // We do not remove the pending task that was set up earlier in case |
| 135 // we run out of time, so the background scheduler will return to us | 131 // we run out of time, so the background scheduler will return to us |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 return trigger_conditions; | 241 return trigger_conditions; |
| 246 } | 242 } |
| 247 | 243 |
| 248 void RequestCoordinator::GetOffliner() { | 244 void RequestCoordinator::GetOffliner() { |
| 249 if (!offliner_) { | 245 if (!offliner_) { |
| 250 offliner_ = factory_->GetOffliner(policy_.get()); | 246 offliner_ = factory_->GetOffliner(policy_.get()); |
| 251 } | 247 } |
| 252 } | 248 } |
| 253 | 249 |
| 254 } // namespace offline_pages | 250 } // namespace offline_pages |
| OLD | NEW |