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

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

Issue 2064793003: Remove old requests from queue, start new requests if any, add test. (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"
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 void RequestCoordinator::AddRequestResultCallback( 61 void RequestCoordinator::AddRequestResultCallback(
62 RequestQueue::AddRequestResult result, 62 RequestQueue::AddRequestResult result,
63 const SavePageRequest& request) { 63 const SavePageRequest& request) {
64 64
65 // Inform the scheduler that we have an outstanding task. 65 // Inform the scheduler that we have an outstanding task.
66 // TODO(petewil): Define proper TriggerConditions and set them. 66 // TODO(petewil): Define proper TriggerConditions and set them.
67 Scheduler::TriggerCondition conditions; 67 Scheduler::TriggerCondition conditions;
68 scheduler_->Schedule(conditions); 68 scheduler_->Schedule(conditions);
69 } 69 }
70 70
71 void RequestCoordinator::UpdateRequestCallback(
72 RequestQueue::UpdateRequestResult result) {}
dougarnett 2016/06/14 18:12:52 Where do we re-schedule and/or unschedule dependin
Pete Williamson 2016/06/14 21:03:14 OfflinerDoneCallback calls TryNextRequest() to sch
73
71 void RequestCoordinator::RequestPicked(const SavePageRequest& request) { 74 void RequestCoordinator::RequestPicked(const SavePageRequest& request) {
72 Scheduler::TriggerCondition conditions; 75 Scheduler::TriggerCondition conditions;
73 76
74 // Ensure that the scheduler has actively scheduled a task. 77 // Ensure that the scheduler has actively scheduled a task.
75 scheduler_->Schedule(conditions); 78 scheduler_->Schedule(conditions);
76 79
77 // Send the request on to the offliner. 80 // Send the request on to the offliner.
78 SendRequestToOffliner(request); 81 SendRequestToOffliner(request);
79 } 82 }
80 83
81 void RequestCoordinator::RequestQueueEmpty() { 84 void RequestCoordinator::RequestQueueEmpty() {
82 // TODO(petewil): return to the BackgroundScheduler by calling 85 // Return control to the scheduler when there is no more to do.
83 // ProcessingDoneCallback 86 scheduler_callback_.Run(true);
dougarnett 2016/06/14 18:12:52 unschedule() here too?
Pete Williamson 2016/06/14 21:03:14 Actually, this is the main place to add unschedule
84 } 87 }
85 88
86 bool RequestCoordinator::StartProcessing( 89 bool RequestCoordinator::StartProcessing(
87 const base::Callback<void(bool)>& callback) { 90 const base::Callback<void(bool)> callback) {
dougarnett 2016/06/14 18:12:52 couldn't this still be a reference here if copying
Pete Williamson 2016/06/14 21:03:13 Done.
91 scheduler_callback_ = callback;
88 // TODO(petewil): Check existing conditions (should be passed down from 92 // TODO(petewil): Check existing conditions (should be passed down from
89 // BackgroundTask) 93 // BackgroundTask)
90 94
95 TryNextRequest();
96
97 return false;
dougarnett 2016/06/14 18:12:52 should return true if caller should expect a callb
Pete Williamson 2016/06/14 21:03:13 Done.
98 }
99
100 void RequestCoordinator::TryNextRequest() {
91 // Choose a request to process that meets the available conditions. 101 // Choose a request to process that meets the available conditions.
92 // This is an async call, and returns right away. 102 // This is an async call, and returns right away.
93 picker_->ChooseNextRequest( 103 picker_->ChooseNextRequest(
94 base::Bind(&RequestCoordinator::RequestPicked, 104 base::Bind(&RequestCoordinator::RequestPicked,
95 weak_ptr_factory_.GetWeakPtr()), 105 weak_ptr_factory_.GetWeakPtr()),
96 base::Bind(&RequestCoordinator::RequestQueueEmpty, 106 base::Bind(&RequestCoordinator::RequestQueueEmpty,
97 weak_ptr_factory_.GetWeakPtr())); 107 weak_ptr_factory_.GetWeakPtr()));
98 return false;
99 } 108 }
100 109
101 void RequestCoordinator::StopProcessing() { 110 void RequestCoordinator::StopProcessing() {
102 } 111 }
103 112
104 void RequestCoordinator::SendRequestToOffliner(const SavePageRequest& request) { 113 void RequestCoordinator::SendRequestToOffliner(const SavePageRequest& request) {
105 // TODO(petewil): Ensure only one offliner at a time is used. 114 // TODO(petewil): Ensure only one offliner at a time is used.
106 // TODO(petewil): When we have multiple offliners, we need to pick one. 115 // TODO(petewil): When we have multiple offliners, we need to pick one.
107 Offliner* offliner = factory_->GetOffliner(policy_.get()); 116 Offliner* offliner = factory_->GetOffliner(policy_.get());
108 if (!offliner) { 117 if (!offliner) {
109 DVLOG(0) << "Unable to create Offliner. " 118 DVLOG(0) << "Unable to create Offliner. "
110 << "Cannot background offline page."; 119 << "Cannot background offline page.";
111 return; 120 return;
112 } 121 }
113 122
114 // Start the load and save process in the offliner (Async). 123 // Start the load and save process in the offliner (Async).
115 offliner->LoadAndSave(request, 124 offliner->LoadAndSave(request,
116 base::Bind(&RequestCoordinator::OfflinerDoneCallback, 125 base::Bind(&RequestCoordinator::OfflinerDoneCallback,
117 weak_ptr_factory_.GetWeakPtr())); 126 weak_ptr_factory_.GetWeakPtr()));
118 } 127 }
119 128
120 void RequestCoordinator::OfflinerDoneCallback(const SavePageRequest& request, 129 void RequestCoordinator::OfflinerDoneCallback(const SavePageRequest& request,
121 Offliner::RequestStatus status) { 130 Offliner::RequestStatus status) {
122 DVLOG(2) << "offliner finished, saved: " 131 DVLOG(2) << "offliner finished, saved: "
123 << (status == Offliner::RequestStatus::SAVED) << ", " 132 << (status == Offliner::RequestStatus::SAVED) << ", "
124 << __FUNCTION__; 133 << __FUNCTION__;
125 last_offlining_status_ = status; 134 last_offlining_status_ = status;
126 135
127 // TODO(petewil): Check time budget. Start a request if we have time, return 136 // TODO(petewil): If the request succeeded, remove it from the Queue.
128 // to the scheduler if we are out of time. 137 if (status == Offliner::RequestStatus::SAVED) {
138 queue_->RemoveRequest(request.request_id(),
139 base::Bind(&RequestCoordinator::UpdateRequestCallback,
140 weak_ptr_factory_.GetWeakPtr()));
141 }
129 142
130 // TODO(petewil): If the request succeeded, remove it from the Queue. 143 // TODO(petewil): Check time budget. Return to the scheduler if we are out.
144
145
146 // Start a request if we have time.
147 TryNextRequest();
148
131 } 149 }
132 150
133 } // namespace offline_pages 151 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698