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

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: 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"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "components/offline_pages/background/offliner_factory.h" 12 #include "components/offline_pages/background/offliner_factory.h"
13 #include "components/offline_pages/background/offliner_policy.h" 13 #include "components/offline_pages/background/offliner_policy.h"
14 #include "components/offline_pages/background/request_picker.h" 14 #include "components/offline_pages/background/request_picker.h"
15 #include "components/offline_pages/background/save_page_request.h" 15 #include "components/offline_pages/background/save_page_request.h"
16 #include "components/offline_pages/background/scheduler.h" 16 #include "components/offline_pages/background/scheduler.h"
17 #include "components/offline_pages/offline_page_item.h" 17 #include "components/offline_pages/offline_page_item.h"
18 18
19 namespace offline_pages { 19 namespace offline_pages {
20 20
21 RequestCoordinator::RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy, 21 RequestCoordinator::RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy,
22 std::unique_ptr<OfflinerFactory> factory, 22 std::unique_ptr<OfflinerFactory> factory,
23 std::unique_ptr<RequestQueue> queue, 23 std::unique_ptr<RequestQueue> queue,
24 std::unique_ptr<Scheduler> scheduler) 24 std::unique_ptr<Scheduler> scheduler)
25 : policy_(std::move(policy)), 25 : offliner_(nullptr),
26 policy_(std::move(policy)),
26 factory_(std::move(factory)), 27 factory_(std::move(factory)),
27 queue_(std::move(queue)), 28 queue_(std::move(queue)),
28 scheduler_(std::move(scheduler)), 29 scheduler_(std::move(scheduler)),
29 last_offlining_status_(Offliner::RequestStatus::UNKNOWN), 30 last_offlining_status_(Offliner::RequestStatus::UNKNOWN),
30 weak_ptr_factory_(this) { 31 weak_ptr_factory_(this) {
31 DCHECK(policy_ != nullptr); 32 DCHECK(policy_ != nullptr);
32 picker_.reset(new RequestPicker(queue_.get())); 33 picker_.reset(new RequestPicker(queue_.get()));
33 } 34 }
34 35
35 RequestCoordinator::~RequestCoordinator() {} 36 RequestCoordinator::~RequestCoordinator() {}
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 void RequestCoordinator::RequestQueueEmpty() { 82 void RequestCoordinator::RequestQueueEmpty() {
82 // Clear the outstanding "safety" task in the scheduler. 83 // Clear the outstanding "safety" task in the scheduler.
83 scheduler_->Unschedule(); 84 scheduler_->Unschedule();
84 // Return control to the scheduler when there is no more to do. 85 // Return control to the scheduler when there is no more to do.
85 scheduler_callback_.Run(true); 86 scheduler_callback_.Run(true);
86 } 87 }
87 88
88 bool RequestCoordinator::StartProcessing( 89 bool RequestCoordinator::StartProcessing(
89 const DeviceConditions& device_conditions, 90 const DeviceConditions& device_conditions,
90 const base::Callback<void(bool)>& callback) { 91 const base::Callback<void(bool)>& callback) {
92 started_ = false;
93 cancelled_ = false;
Pete Williamson 2016/06/22 22:53:22 FYI - there may be some overlap with the is_busy_
chili 2016/06/23 05:42:06 nit - we seem to have different spellings of cance
Pete Williamson 2016/06/23 16:46:13 When I checked with a dictionary, both were legit,
91 scheduler_callback_ = callback; 94 scheduler_callback_ = callback;
92 // TODO(petewil): Check existing conditions (should be passed down from 95 // TODO(petewil): Check existing conditions (should be passed down from
93 // BackgroundTask) 96 // BackgroundTask)
94 97
98 GetOffliner();
99 if (!offliner_) {
100 DVLOG(0) << "Unable to create Offliner. "
101 << "Cannot background offline page.";
102 return false;
103 }
104
95 TryNextRequest(); 105 TryNextRequest();
96 106
97 // TODO(petewil): Should return true if the caller should expect a 107 // TODO(petewil): Should return true if the caller should expect a
98 // callback. Return false if there is already a request running. 108 // callback. Return false if there is already a request running.
99 // Probably best to do this when I prevent multiple instances from 109 // Probably best to do this when I prevent multiple instances from
100 // running at the same time. 110 // running at the same time.
101 return true; 111 return true;
102 } 112 }
103 113
104 void RequestCoordinator::TryNextRequest() { 114 void RequestCoordinator::TryNextRequest() {
105 // Choose a request to process that meets the available conditions. 115 // Choose a request to process that meets the available conditions.
106 // This is an async call, and returns right away. 116 // This is an async call, and returns right away.
107 picker_->ChooseNextRequest( 117 picker_->ChooseNextRequest(
108 base::Bind(&RequestCoordinator::RequestPicked, 118 base::Bind(&RequestCoordinator::RequestPicked,
109 weak_ptr_factory_.GetWeakPtr()), 119 weak_ptr_factory_.GetWeakPtr()),
110 base::Bind(&RequestCoordinator::RequestQueueEmpty, 120 base::Bind(&RequestCoordinator::RequestQueueEmpty,
111 weak_ptr_factory_.GetWeakPtr())); 121 weak_ptr_factory_.GetWeakPtr()));
112 } 122 }
113 123
114 void RequestCoordinator::StopProcessing() { 124 void RequestCoordinator::StopProcessing() {
125 cancelled_ = true;
126 if (offliner_ && started_)
127 offliner_->Cancel();
115 } 128 }
116 129
117 void RequestCoordinator::SendRequestToOffliner(const SavePageRequest& request) { 130 void RequestCoordinator::SendRequestToOffliner(const SavePageRequest& request) {
118 // TODO(petewil): Ensure only one offliner at a time is used. 131 if (cancelled_)
119 // TODO(petewil): When we have multiple offliners, we need to pick one. 132 return;
120 Offliner* offliner = factory_->GetOffliner(policy_.get()); 133 started_ = true;
121 if (!offliner) { 134
135 GetOffliner();
136 if (!offliner_) {
122 DVLOG(0) << "Unable to create Offliner. " 137 DVLOG(0) << "Unable to create Offliner. "
123 << "Cannot background offline page."; 138 << "Cannot background offline page.";
124 return; 139 return;
125 } 140 }
126 141
127 // Start the load and save process in the offliner (Async). 142 // Start the load and save process in the offliner (Async).
128 offliner->LoadAndSave(request, 143 offliner_->LoadAndSave(request,
129 base::Bind(&RequestCoordinator::OfflinerDoneCallback, 144 base::Bind(&RequestCoordinator::OfflinerDoneCallback,
130 weak_ptr_factory_.GetWeakPtr())); 145 weak_ptr_factory_.GetWeakPtr()));
131 } 146 }
132 147
133 void RequestCoordinator::OfflinerDoneCallback(const SavePageRequest& request, 148 void RequestCoordinator::OfflinerDoneCallback(const SavePageRequest& request,
134 Offliner::RequestStatus status) { 149 Offliner::RequestStatus status) {
135 DVLOG(2) << "offliner finished, saved: " 150 DVLOG(2) << "offliner finished, saved: "
136 << (status == Offliner::RequestStatus::SAVED) << ", " 151 << (status == Offliner::RequestStatus::SAVED) << ", status: "
137 << __FUNCTION__; 152 << (int) status << ", " << __FUNCTION__;
138 last_offlining_status_ = status; 153 last_offlining_status_ = status;
154 started_ = false;
139 155
140 // If the request succeeded, remove it from the Queue and maybe schedule 156 // If the request succeeded, remove it from the Queue and maybe schedule
141 // another one. 157 // another one.
142 if (status == Offliner::RequestStatus::SAVED) { 158 if (status == Offliner::RequestStatus::SAVED) {
143 queue_->RemoveRequest(request.request_id(), 159 queue_->RemoveRequest(request.request_id(),
144 base::Bind(&RequestCoordinator::UpdateRequestCallback, 160 base::Bind(&RequestCoordinator::UpdateRequestCallback,
145 weak_ptr_factory_.GetWeakPtr())); 161 weak_ptr_factory_.GetWeakPtr()));
146 162
147 // TODO(petewil): Check time budget. Return to the scheduler if we are out. 163 // TODO(petewil): Check time budget. Return to the scheduler if we are out.
148 164
149 // Start another request if we have time. 165 // Start another request if we have time.
150 TryNextRequest(); 166 TryNextRequest();
151 } 167 }
152 } 168 }
153 169
170 void RequestCoordinator::GetOffliner() {
171 if (!offliner_) {
172 offliner_ = factory_->GetOffliner(policy_.get());
173 }
174 }
175
154 } // namespace offline_pages 176 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698