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

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

Issue 2279913002: Start processing immediately if there is network (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 3 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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 base::HistogramBase::kUmaTargetedHistogramFlag); 44 base::HistogramBase::kUmaTargetedHistogramFlag);
45 histogram->Add(static_cast<int>(request_status)); 45 histogram->Add(static_cast<int>(request_status));
46 } 46 }
47 47
48 // This should use the same algorithm as we use for OfflinePageItem, so the IDs 48 // This should use the same algorithm as we use for OfflinePageItem, so the IDs
49 // are similar. 49 // are similar.
50 int64_t GenerateOfflineId() { 50 int64_t GenerateOfflineId() {
51 return base::RandGenerator(std::numeric_limits<int64_t>::max()) + 1; 51 return base::RandGenerator(std::numeric_limits<int64_t>::max()) + 1;
52 } 52 }
53 53
54 // In case we start processing from SavePageLater, we need a callback, but there
55 // is nothing for it to do.
56 void EmptySchedulerCallback(bool started) {}
57
54 } // namespace 58 } // namespace
55 59
56 RequestCoordinator::RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy, 60 RequestCoordinator::RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy,
57 std::unique_ptr<OfflinerFactory> factory, 61 std::unique_ptr<OfflinerFactory> factory,
58 std::unique_ptr<RequestQueue> queue, 62 std::unique_ptr<RequestQueue> queue,
59 std::unique_ptr<Scheduler> scheduler) 63 std::unique_ptr<Scheduler> scheduler)
60 : is_busy_(false), 64 : is_busy_(false),
61 is_stopped_(false), 65 is_stopped_(false),
66 use_test_connection_type_(false),
67 test_connection_type_(),
62 offliner_(nullptr), 68 offliner_(nullptr),
63 policy_(std::move(policy)), 69 policy_(std::move(policy)),
64 factory_(std::move(factory)), 70 factory_(std::move(factory)),
65 queue_(std::move(queue)), 71 queue_(std::move(queue)),
66 scheduler_(std::move(scheduler)), 72 scheduler_(std::move(scheduler)),
67 active_request_(nullptr), 73 active_request_(nullptr),
68 last_offlining_status_(Offliner::RequestStatus::UNKNOWN), 74 last_offlining_status_(Offliner::RequestStatus::UNKNOWN),
69 offliner_timeout_(base::TimeDelta::FromSeconds( 75 offliner_timeout_(base::TimeDelta::FromSeconds(
70 policy_->GetSinglePageTimeLimitInSeconds())), 76 policy_->GetSinglePageTimeLimitInSeconds())),
71 weak_ptr_factory_(this) { 77 weak_ptr_factory_(this) {
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 182
177 void RequestCoordinator::ResumeRequests( 183 void RequestCoordinator::ResumeRequests(
178 const std::vector<int64_t>& request_ids) { 184 const std::vector<int64_t>& request_ids) {
179 queue_->ChangeRequestsState( 185 queue_->ChangeRequestsState(
180 request_ids, SavePageRequest::RequestState::AVAILABLE, 186 request_ids, SavePageRequest::RequestState::AVAILABLE,
181 base::Bind(&RequestCoordinator::UpdateMultipleRequestsCallback, 187 base::Bind(&RequestCoordinator::UpdateMultipleRequestsCallback,
182 weak_ptr_factory_.GetWeakPtr())); 188 weak_ptr_factory_.GetWeakPtr()));
183 // TODO: Should we also schedule a task, in case there is not one scheduled? 189 // TODO: Should we also schedule a task, in case there is not one scheduled?
184 } 190 }
185 191
192 net::NetworkChangeNotifier::ConnectionType
193 RequestCoordinator::GetConnectionType() {
dougarnett 2016/08/25 21:03:11 indentation?
194 // If we have a connection type set for test, use that.
195 if (use_test_connection_type_)
196 return test_connection_type_;
197
198 return net::NetworkChangeNotifier::GetConnectionType();
199 }
200
186 void RequestCoordinator::AddRequestResultCallback( 201 void RequestCoordinator::AddRequestResultCallback(
187 RequestQueue::AddRequestResult result, 202 RequestQueue::AddRequestResult result,
188 const SavePageRequest& request) { 203 const SavePageRequest& request) {
189 NotifyAdded(request); 204 NotifyAdded(request);
190 // Inform the scheduler that we have an outstanding task.. 205 // Inform the scheduler that we have an outstanding task..
191 scheduler_->Schedule(GetTriggerConditionsForUserRequest()); 206 scheduler_->Schedule(GetTriggerConditionsForUserRequest());
207
208 // If it makes sense, start processing now.
209 if (is_busy_) return;
210
211 // Check for network
212 net::NetworkChangeNotifier::ConnectionType connection = GetConnectionType();
213
214 if ((connection !=
215 net::NetworkChangeNotifier::ConnectionType::CONNECTION_NONE) &&
216 request.user_requested()) {
217 // Create device conditions.
218 DeviceConditions device_conditions(false, 0, connection);
219
220 // Start processing if it makes sense. (net, user requested)
221 StartProcessing(device_conditions, base::Bind(&EmptySchedulerCallback));
222 }
192 } 223 }
193 224
194 // Called in response to updating a request in the request queue. 225 // Called in response to updating a request in the request queue.
195 void RequestCoordinator::UpdateRequestCallback( 226 void RequestCoordinator::UpdateRequestCallback(
196 const ClientId& client_id, 227 const ClientId& client_id,
197 RequestQueue::UpdateRequestResult result) { 228 RequestQueue::UpdateRequestResult result) {
198 // If the request succeeded, nothing to do. If it failed, we can't really do 229 // If the request succeeded, nothing to do. If it failed, we can't really do
199 // much, so just log it. 230 // much, so just log it.
200 if (result != RequestQueue::UpdateRequestResult::SUCCESS) { 231 if (result != RequestQueue::UpdateRequestResult::SUCCESS) {
201 DVLOG(1) << "Failed to update request attempt details. " 232 DVLOG(1) << "Failed to update request attempt details. "
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
448 FOR_EACH_OBSERVER(Observer, observers_, OnChanged(request)); 479 FOR_EACH_OBSERVER(Observer, observers_, OnChanged(request));
449 } 480 }
450 481
451 void RequestCoordinator::GetOffliner() { 482 void RequestCoordinator::GetOffliner() {
452 if (!offliner_) { 483 if (!offliner_) {
453 offliner_ = factory_->GetOffliner(policy_.get()); 484 offliner_ = factory_->GetOffliner(policy_.get());
454 } 485 }
455 } 486 }
456 487
457 } // namespace offline_pages 488 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698