Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(163)

Side by Side Diff: components/offline_pages/background/request_coordinator.cc

Issue 2091473004: Add the ability to cancel prerender reqeusts and tests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comment changes per DougArnett Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 13 matching lines...) Expand all
24 false /* require_power_connected */, 24 false /* require_power_connected */,
25 50 /* minimum_battery_percentage */, 25 50 /* minimum_battery_percentage */,
26 false /* require_unmetered_network */); 26 false /* require_unmetered_network */);
27 } // namespace 27 } // namespace
28 28
29 RequestCoordinator::RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy, 29 RequestCoordinator::RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy,
30 std::unique_ptr<OfflinerFactory> factory, 30 std::unique_ptr<OfflinerFactory> factory,
31 std::unique_ptr<RequestQueue> queue, 31 std::unique_ptr<RequestQueue> queue,
32 std::unique_ptr<Scheduler> scheduler) 32 std::unique_ptr<Scheduler> scheduler)
33 : is_busy_(false), 33 : is_busy_(false),
34 is_canceled_(false),
35 offliner_(nullptr),
34 policy_(std::move(policy)), 36 policy_(std::move(policy)),
35 factory_(std::move(factory)), 37 factory_(std::move(factory)),
36 queue_(std::move(queue)), 38 queue_(std::move(queue)),
37 scheduler_(std::move(scheduler)), 39 scheduler_(std::move(scheduler)),
38 last_offlining_status_(Offliner::RequestStatus::UNKNOWN), 40 last_offlining_status_(Offliner::RequestStatus::UNKNOWN),
39 weak_ptr_factory_(this) { 41 weak_ptr_factory_(this) {
40 DCHECK(policy_ != nullptr); 42 DCHECK(policy_ != nullptr);
41 picker_.reset(new RequestPicker(queue_.get())); 43 picker_.reset(new RequestPicker(queue_.get()));
42 } 44 }
43 45
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 scheduler_callback_.Run(true); 90 scheduler_callback_.Run(true);
89 } 91 }
90 92
91 // Returns true if the caller should expect a callback, false otherwise. For 93 // Returns true if the caller should expect a callback, false otherwise. For
92 // instance, this would return false if a request is already in progress. 94 // instance, this would return false if a request is already in progress.
93 bool RequestCoordinator::StartProcessing( 95 bool RequestCoordinator::StartProcessing(
94 const DeviceConditions& device_conditions, 96 const DeviceConditions& device_conditions,
95 const base::Callback<void(bool)>& callback) { 97 const base::Callback<void(bool)>& callback) {
96 if (is_busy_) return false; 98 if (is_busy_) return false;
97 99
100 is_canceled_ = false;
98 scheduler_callback_ = callback; 101 scheduler_callback_ = callback;
99 // TODO(petewil): Check existing conditions (should be passed down from 102 // TODO(petewil): Check existing conditions (should be passed down from
100 // BackgroundTask) 103 // BackgroundTask)
101 104
102 TryNextRequest(); 105 TryNextRequest();
103 106
104 return true; 107 return true;
105 } 108 }
106 109
107 void RequestCoordinator::TryNextRequest() { 110 void RequestCoordinator::TryNextRequest() {
108 // Choose a request to process that meets the available conditions. 111 // Choose a request to process that meets the available conditions.
109 // This is an async call, and returns right away. 112 // This is an async call, and returns right away.
110 picker_->ChooseNextRequest( 113 picker_->ChooseNextRequest(
111 base::Bind(&RequestCoordinator::RequestPicked, 114 base::Bind(&RequestCoordinator::RequestPicked,
112 weak_ptr_factory_.GetWeakPtr()), 115 weak_ptr_factory_.GetWeakPtr()),
113 base::Bind(&RequestCoordinator::RequestQueueEmpty, 116 base::Bind(&RequestCoordinator::RequestQueueEmpty,
114 weak_ptr_factory_.GetWeakPtr())); 117 weak_ptr_factory_.GetWeakPtr()));
115 } 118 }
116 119
117 void RequestCoordinator::StopProcessing() { 120 void RequestCoordinator::StopProcessing() {
121 is_canceled_ = true;
122 if (offliner_ && is_busy_)
123 offliner_->Cancel();
118 } 124 }
119 125
120 Scheduler::TriggerConditions const& 126 Scheduler::TriggerConditions const&
121 RequestCoordinator::GetTriggerConditionsForUserRequest() { 127 RequestCoordinator::GetTriggerConditionsForUserRequest() {
122 return kUserRequestTriggerConditions; 128 return kUserRequestTriggerConditions;
123 } 129 }
124 130
125 void RequestCoordinator::SendRequestToOffliner(const SavePageRequest& request) { 131 void RequestCoordinator::SendRequestToOffliner(const SavePageRequest& request) {
126 // TODO(petewil): When we have multiple offliners, we need to pick one. 132 // Check that offlining didn't get cancelled while performing some async
127 Offliner* offliner = factory_->GetOffliner(policy_.get()); 133 // steps.
128 if (!offliner) { 134 if (is_canceled_)
135 return;
136
137 GetOffliner();
138 if (!offliner_) {
129 DVLOG(0) << "Unable to create Offliner. " 139 DVLOG(0) << "Unable to create Offliner. "
130 << "Cannot background offline page."; 140 << "Cannot background offline page.";
131 return; 141 return;
132 } 142 }
133 143
134 DCHECK(!is_busy_); 144 DCHECK(!is_busy_);
135 is_busy_ = true; 145 is_busy_ = true;
136 146
137 // Start the load and save process in the offliner (Async). 147 // Start the load and save process in the offliner (Async).
138 offliner->LoadAndSave(request, 148 offliner_->LoadAndSave(request,
139 base::Bind(&RequestCoordinator::OfflinerDoneCallback, 149 base::Bind(&RequestCoordinator::OfflinerDoneCallback,
140 weak_ptr_factory_.GetWeakPtr())); 150 weak_ptr_factory_.GetWeakPtr()));
141 } 151 }
142 152
143 void RequestCoordinator::OfflinerDoneCallback(const SavePageRequest& request, 153 void RequestCoordinator::OfflinerDoneCallback(const SavePageRequest& request,
144 Offliner::RequestStatus status) { 154 Offliner::RequestStatus status) {
145 DVLOG(2) << "offliner finished, saved: " 155 DVLOG(2) << "offliner finished, saved: "
146 << (status == Offliner::RequestStatus::SAVED) << ", " 156 << (status == Offliner::RequestStatus::SAVED) << ", status: "
147 << __FUNCTION__; 157 << (int) status << ", " << __FUNCTION__;
148 last_offlining_status_ = status; 158 last_offlining_status_ = status;
149 159
150 is_busy_ = false; 160 is_busy_ = false;
151 161
152 // If the request succeeded, remove it from the Queue and maybe schedule 162 // If the request succeeded, remove it from the Queue and maybe schedule
153 // another one. 163 // another one.
154 if (status == Offliner::RequestStatus::SAVED) { 164 if (status == Offliner::RequestStatus::SAVED) {
155 queue_->RemoveRequest(request.request_id(), 165 queue_->RemoveRequest(request.request_id(),
156 base::Bind(&RequestCoordinator::UpdateRequestCallback, 166 base::Bind(&RequestCoordinator::UpdateRequestCallback,
157 weak_ptr_factory_.GetWeakPtr())); 167 weak_ptr_factory_.GetWeakPtr()));
158 168
159 // TODO(petewil): Check time budget. Return to the scheduler if we are out. 169 // TODO(petewil): Check time budget. Return to the scheduler if we are out.
160 170
161 // Start another request if we have time. 171 // Start another request if we have time.
162 TryNextRequest(); 172 TryNextRequest();
163 } 173 }
164 } 174 }
165 175
176 void RequestCoordinator::GetOffliner() {
177 if (!offliner_) {
178 offliner_ = factory_->GetOffliner(policy_.get());
179 }
180 }
181
166 } // namespace offline_pages 182 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698