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

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

Issue 2473553004: Request Picker task (Closed)
Patch Set: merge Created 4 years, 1 month 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 #ifndef COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_COORDINATOR_H_ 5 #ifndef COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_COORDINATOR_H_
6 #define COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_COORDINATOR_H_ 6 #define COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_COORDINATOR_H_
7 7
8 #include <memory> 8 #include <memory>
9 #include <set> 9 #include <set>
10 #include <string> 10 #include <string>
(...skipping 16 matching lines...) Expand all
27 #include "net/nqe/network_quality_estimator.h" 27 #include "net/nqe/network_quality_estimator.h"
28 #include "url/gurl.h" 28 #include "url/gurl.h"
29 29
30 namespace offline_pages { 30 namespace offline_pages {
31 31
32 struct ClientId; 32 struct ClientId;
33 class OfflinerPolicy; 33 class OfflinerPolicy;
34 class OfflinerFactory; 34 class OfflinerFactory;
35 class Offliner; 35 class Offliner;
36 class RequestPicker; 36 class RequestPicker;
37 class RequestQueue;
37 class SavePageRequest; 38 class SavePageRequest;
38 class Scheduler; 39 class Scheduler;
39 class ClientPolicyController; 40 class ClientPolicyController;
40 41
41 // Coordinates queueing and processing save page later requests. 42 // Coordinates queueing and processing save page later requests.
42 class RequestCoordinator : public KeyedService, 43 class RequestCoordinator : public KeyedService,
43 public RequestNotifier, 44 public RequestNotifier,
44 public base::SupportsUserData { 45 public base::SupportsUserData {
45 public: 46 public:
46 // Nested observer class. To make sure that no events are missed, the client 47 // Nested observer class. To make sure that no events are missed, the client
47 // code should first register for notifications, then |GetAllRequests|, and 48 // code should first register for notifications, then |GetAllRequests|, and
48 // ignore all events before the return from |GetAllRequests|, and consume 49 // ignore all events before the return from |GetAllRequests|, and consume
49 // events after the return callback from |GetAllRequests|. 50 // events after the return callback from |GetAllRequests|.
50 class Observer { 51 class Observer {
51 public: 52 public:
52 virtual ~Observer() = default; 53 virtual ~Observer() = default;
53 54
54 virtual void OnAdded(const SavePageRequest& request) = 0; 55 virtual void OnAdded(const SavePageRequest& request) = 0;
55 virtual void OnCompleted( 56 virtual void OnCompleted(
56 const SavePageRequest& request, 57 const SavePageRequest& request,
57 RequestNotifier::BackgroundSavePageResult status) = 0; 58 RequestNotifier::BackgroundSavePageResult status) = 0;
58 virtual void OnChanged(const SavePageRequest& request) = 0; 59 virtual void OnChanged(const SavePageRequest& request) = 0;
59 }; 60 };
60 61
61 enum class RequestAvailability { 62 enum class RequestAvailability {
62 ENABLED_FOR_OFFLINER, 63 ENABLED_FOR_OFFLINER,
63 DISABLED_FOR_OFFLINER, 64 DISABLED_FOR_OFFLINER,
64 }; 65 };
65 66
66 // Callback to report when a request was available.
67 typedef base::Callback<void(const SavePageRequest& request)>
68 RequestPickedCallback;
69
70 // Callback to report when no request was available.
71 typedef base::Callback<void(bool)> RequestNotPickedCallback;
72
73 // Callback specifying which request IDs were actually removed. 67 // Callback specifying which request IDs were actually removed.
74 typedef base::Callback<void(const MultipleItemStatuses&)> 68 typedef base::Callback<void(const MultipleItemStatuses&)>
75 RemoveRequestsCallback; 69 RemoveRequestsCallback;
76 70
77 // Callback that receives the response for GetAllRequests. 71 // Callback that receives the response for GetAllRequests.
78 typedef base::Callback<void(std::vector<std::unique_ptr<SavePageRequest>>)> 72 typedef base::Callback<void(std::vector<std::unique_ptr<SavePageRequest>>)>
79 GetRequestsCallback; 73 GetRequestsCallback;
80 74
81 RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy, 75 RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy,
82 std::unique_ptr<OfflinerFactory> factory, 76 std::unique_ptr<OfflinerFactory> factory,
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 // Returns whether processing is starting (before it is decided to actually 179 // Returns whether processing is starting (before it is decided to actually
186 // process a request (is_busy()) at this time or not. 180 // process a request (is_busy()) at this time or not.
187 bool is_starting() { return is_starting_; } 181 bool is_starting() { return is_starting_; }
188 182
189 // Tracks whether the last offlining attempt got canceled. This is reset by 183 // Tracks whether the last offlining attempt got canceled. This is reset by
190 // the next StartProcessing() call. 184 // the next StartProcessing() call.
191 bool is_canceled() { 185 bool is_canceled() {
192 return processing_state_ == ProcessingWindowState::STOPPED; 186 return processing_state_ == ProcessingWindowState::STOPPED;
193 } 187 }
194 188
195 OfflineEventLogger* GetLogger() { 189 RequestCoordinatorEventLogger* GetLogger() { return &event_logger_; }
196 return &event_logger_;
197 }
198 190
199 private: 191 private:
200 // Immediate start attempt status code for UMA. 192 // Immediate start attempt status code for UMA.
201 // These values are written to logs. New enum values can be added, but 193 // These values are written to logs. New enum values can be added, but
202 // existing enums must never be renumbered or deleted and reused. 194 // existing enums must never be renumbered or deleted and reused.
203 // For any additions, also update corresponding histogram in histograms.xml. 195 // For any additions, also update corresponding histogram in histograms.xml.
204 enum OfflinerImmediateStartStatus { 196 enum OfflinerImmediateStartStatus {
205 // Did start processing request. 197 // Did start processing request.
206 STARTED = 0, 198 STARTED = 0,
207 // Already busy processing a request. 199 // Already busy processing a request.
(...skipping 14 matching lines...) Expand all
222 enum class ProcessingWindowState { 214 enum class ProcessingWindowState {
223 STOPPED, 215 STOPPED,
224 SCHEDULED_WINDOW, 216 SCHEDULED_WINDOW,
225 IMMEDIATE_WINDOW, 217 IMMEDIATE_WINDOW,
226 }; 218 };
227 219
228 // Receives the results of a get from the request queue, and turns that into 220 // Receives the results of a get from the request queue, and turns that into
229 // SavePageRequest objects for the caller of GetQueuedRequests. 221 // SavePageRequest objects for the caller of GetQueuedRequests.
230 void GetQueuedRequestsCallback( 222 void GetQueuedRequestsCallback(
231 const GetRequestsCallback& callback, 223 const GetRequestsCallback& callback,
232 RequestQueue::GetRequestsResult result, 224 QueueResults::GetRequestsResult result,
233 std::vector<std::unique_ptr<SavePageRequest>> requests); 225 std::vector<std::unique_ptr<SavePageRequest>> requests);
234 226
235 // Receives the results of a get from the request queue, and turns that into 227 // Receives the results of a get from the request queue, and turns that into
236 // SavePageRequest objects for the caller of GetQueuedRequests. 228 // SavePageRequest objects for the caller of GetQueuedRequests.
237 void GetRequestsForSchedulingCallback( 229 void GetRequestsForSchedulingCallback(
238 RequestQueue::GetRequestsResult result, 230 QueueResults::GetRequestsResult result,
239 std::vector<std::unique_ptr<SavePageRequest>> requests); 231 std::vector<std::unique_ptr<SavePageRequest>> requests);
240 232
241 // Receives the result of add requests to the request queue. 233 // Receives the result of add requests to the request queue.
242 void AddRequestResultCallback(RequestQueue::AddRequestResult result, 234 void AddRequestResultCallback(QueueResults::AddRequestResult result,
243 const SavePageRequest& request); 235 const SavePageRequest& request);
244 236
245 // Receives the result of mark attempt completed requests. 237 // Receives the result of mark attempt completed requests.
246 void MarkAttemptCompletedDoneCallback( 238 void MarkAttemptCompletedDoneCallback(
247 int64_t request_id, 239 int64_t request_id,
248 const ClientId& client_id, 240 const ClientId& client_id,
249 std::unique_ptr<UpdateRequestsResult> result); 241 std::unique_ptr<QueueResults::UpdateRequestsResult> result);
250 242
251 void UpdateMultipleRequestsCallback( 243 void UpdateMultipleRequestsCallback(
252 std::unique_ptr<UpdateRequestsResult> result); 244 std::unique_ptr<QueueResults::UpdateRequestsResult> result);
253 245
254 void CompletedRequestCallback(const MultipleItemStatuses& status); 246 void CompletedRequestCallback(const MultipleItemStatuses& status);
255 247
256 void HandleRemovedRequestsAndCallback( 248 void HandleRemovedRequestsAndCallback(
257 const RemoveRequestsCallback& callback, 249 const RemoveRequestsCallback& callback,
258 BackgroundSavePageResult status, 250 RequestNotifier::BackgroundSavePageResult status,
259 std::unique_ptr<UpdateRequestsResult> result); 251 std::unique_ptr<QueueResults::UpdateRequestsResult> result);
260 252
261 void HandleRemovedRequests(BackgroundSavePageResult status, 253 void HandleRemovedRequests(
262 std::unique_ptr<UpdateRequestsResult> result); 254 RequestNotifier::BackgroundSavePageResult status,
255 std::unique_ptr<QueueResults::UpdateRequestsResult> result);
263 256
264 bool StartProcessingInternal(const ProcessingWindowState processing_state, 257 bool StartProcessingInternal(const ProcessingWindowState processing_state,
265 const DeviceConditions& device_conditions, 258 const DeviceConditions& device_conditions,
266 const base::Callback<void(bool)>& callback); 259 const base::Callback<void(bool)>& callback);
267 260
268 // Start processing now if connected (but with conservative assumption 261 // Start processing now if connected (but with conservative assumption
269 // as to other device conditions). 262 // as to other device conditions).
270 void StartImmediatelyIfConnected(); 263 void StartImmediatelyIfConnected();
271 264
272 OfflinerImmediateStartStatus TryImmediateStart(); 265 OfflinerImmediateStartStatus TryImmediateStart();
(...skipping 13 matching lines...) Expand all
286 void HandleWatchdogTimeout(); 279 void HandleWatchdogTimeout();
287 280
288 // Cancels an in progress pre-rendering, and updates state appropriately. 281 // Cancels an in progress pre-rendering, and updates state appropriately.
289 void StopPrerendering(Offliner::RequestStatus stop_status); 282 void StopPrerendering(Offliner::RequestStatus stop_status);
290 283
291 // Marks attempt on the request and sends it to offliner in continuation. 284 // Marks attempt on the request and sends it to offliner in continuation.
292 void SendRequestToOffliner(const SavePageRequest& request); 285 void SendRequestToOffliner(const SavePageRequest& request);
293 286
294 // Continuation of |SendRequestToOffliner| after the request is marked as 287 // Continuation of |SendRequestToOffliner| after the request is marked as
295 // started. 288 // started.
296 void StartOffliner(int64_t request_id, 289 void StartOffliner(
297 const std::string& client_namespace, 290 int64_t request_id,
298 std::unique_ptr<UpdateRequestsResult> update_result); 291 const std::string& client_namespace,
292 std::unique_ptr<QueueResults::UpdateRequestsResult> update_result);
299 293
300 // Called by the offliner when an offlining request is completed. (and by 294 // Called by the offliner when an offlining request is completed. (and by
301 // tests). 295 // tests).
302 void OfflinerDoneCallback(const SavePageRequest& request, 296 void OfflinerDoneCallback(const SavePageRequest& request,
303 Offliner::RequestStatus status); 297 Offliner::RequestStatus status);
304 298
305 void TryNextRequest(); 299 void TryNextRequest();
306 300
307 // If there is an active request in the list, cancel that request. 301 // If there is an active request in the list, cancel that request.
308 bool CancelActiveRequestIfItMatches(const std::vector<int64_t>& request_ids); 302 bool CancelActiveRequestIfItMatches(const std::vector<int64_t>& request_ids);
309 303
310 // Records an aborted attempt for the request and update it in the queue 304 // Records an aborted attempt for the request and update it in the queue
311 // (possibly removing it). Returns the updated copy. 305 // (possibly removing it). Returns the updated copy.
312 void AbortRequestAttempt(SavePageRequest* request); 306 void AbortRequestAttempt(SavePageRequest* request);
313 307
314 // Remove the attempted request from the queue with status to pass through to 308 // Remove the attempted request from the queue with status to pass through to
315 // any observers and UMA histogram. 309 // any observers and UMA histogram.
316 void RemoveAttemptedRequest(const SavePageRequest& request, 310 void RemoveAttemptedRequest(const SavePageRequest& request,
317 BackgroundSavePageResult status); 311 BackgroundSavePageResult status);
318 312
319 // Completes aborting the request, reports an error if it fails. 313 // Completes aborting the request, reports an error if it fails.
320 void MarkAttemptAbortedDone(int64_t request_id, 314 void MarkAttemptAbortedDone(
321 const ClientId& client_id, 315 int64_t request_id,
322 std::unique_ptr<UpdateRequestsResult> result); 316 const ClientId& client_id,
317 std::unique_ptr<QueueResults::UpdateRequestsResult> result);
323 318
324 // Returns the appropriate offliner to use, getting a new one from the factory 319 // Returns the appropriate offliner to use, getting a new one from the factory
325 // if needed. 320 // if needed.
326 void GetOffliner(); 321 void GetOffliner();
327 322
328 // Method to wrap calls to getting the connection type so it can be 323 // Method to wrap calls to getting the connection type so it can be
329 // changed for tests. 324 // changed for tests.
330 net::NetworkChangeNotifier::ConnectionType GetConnectionType(); 325 net::NetworkChangeNotifier::ConnectionType GetConnectionType();
331 326
332 void SetNetworkConditionsForTest( 327 void SetNetworkConditionsForTest(
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
377 std::unique_ptr<Scheduler> scheduler_; 372 std::unique_ptr<Scheduler> scheduler_;
378 // Controller of client policies. Owned. 373 // Controller of client policies. Owned.
379 std::unique_ptr<ClientPolicyController> policy_controller_; 374 std::unique_ptr<ClientPolicyController> policy_controller_;
380 // Unowned pointer to the Network Quality Estimator. 375 // Unowned pointer to the Network Quality Estimator.
381 net::NetworkQualityEstimator::NetworkQualityProvider* 376 net::NetworkQualityEstimator::NetworkQualityProvider*
382 network_quality_estimator_; 377 network_quality_estimator_;
383 // Holds copy of the active request, if any. 378 // Holds copy of the active request, if any.
384 std::unique_ptr<SavePageRequest> active_request_; 379 std::unique_ptr<SavePageRequest> active_request_;
385 // Status of the most recent offlining. 380 // Status of the most recent offlining.
386 Offliner::RequestStatus last_offlining_status_; 381 Offliner::RequestStatus last_offlining_status_;
387 // Class to choose which request to schedule next
388 std::unique_ptr<RequestPicker> picker_;
389 // A set of request_ids that we are holding off until the download manager is 382 // A set of request_ids that we are holding off until the download manager is
390 // done with them. 383 // done with them.
391 std::set<int64_t> disabled_requests_; 384 std::set<int64_t> disabled_requests_;
392 // Calling this returns to the scheduler across the JNI bridge. 385 // Calling this returns to the scheduler across the JNI bridge.
393 base::Callback<void(bool)> scheduler_callback_; 386 base::Callback<void(bool)> scheduler_callback_;
394 // Logger to record events. 387 // Logger to record events.
395 RequestCoordinatorEventLogger event_logger_; 388 RequestCoordinatorEventLogger event_logger_;
396 // Timer to watch for pre-render attempts running too long. 389 // Timer to watch for pre-render attempts running too long.
397 base::OneShotTimer watchdog_timer_; 390 base::OneShotTimer watchdog_timer_;
398 // Callback invoked when an immediate request is done (default empty). 391 // Callback invoked when an immediate request is done (default empty).
399 base::Callback<void(bool)> immediate_schedule_callback_; 392 base::Callback<void(bool)> immediate_schedule_callback_;
400 // Allows us to pass a weak pointer to callbacks. 393 // Allows us to pass a weak pointer to callbacks.
401 base::WeakPtrFactory<RequestCoordinator> weak_ptr_factory_; 394 base::WeakPtrFactory<RequestCoordinator> weak_ptr_factory_;
402 395
403 DISALLOW_COPY_AND_ASSIGN(RequestCoordinator); 396 DISALLOW_COPY_AND_ASSIGN(RequestCoordinator);
404 }; 397 };
405 398
406 } // namespace offline_pages 399 } // namespace offline_pages
407 400
408 #endif // COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_COORDINATOR_H_ 401 #endif // COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_COORDINATOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698