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

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

Issue 2020833002: Add the request picker and a unit 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/logging.h"
10 #include "components/offline_pages/background/offliner_factory.h" 11 #include "components/offline_pages/background/offliner_factory.h"
11 #include "components/offline_pages/background/offliner_policy.h" 12 #include "components/offline_pages/background/offliner_policy.h"
13 #include "components/offline_pages/background/request_picker.h"
12 #include "components/offline_pages/background/save_page_request.h" 14 #include "components/offline_pages/background/save_page_request.h"
13 #include "components/offline_pages/background/scheduler.h" 15 #include "components/offline_pages/background/scheduler.h"
14 #include "components/offline_pages/offline_page_item.h" 16 #include "components/offline_pages/offline_page_item.h"
15 17
16 namespace offline_pages { 18 namespace offline_pages {
17 19
18 RequestCoordinator::RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy, 20 RequestCoordinator::RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy,
19 std::unique_ptr<OfflinerFactory> factory, 21 std::unique_ptr<OfflinerFactory> factory,
20 std::unique_ptr<RequestQueue> queue, 22 std::unique_ptr<RequestQueue> queue,
21 std::unique_ptr<Scheduler> scheduler) 23 std::unique_ptr<Scheduler> scheduler)
22 : policy_(std::move(policy)), 24 : policy_(std::move(policy)),
23 factory_(std::move(factory)), 25 factory_(std::move(factory)),
24 queue_(std::move(queue)), 26 queue_(std::move(queue)),
25 scheduler_(std::move(scheduler)), 27 scheduler_(std::move(scheduler)),
26 last_offlining_status_(Offliner::RequestStatus::UNKNOWN) { 28 last_offlining_status_(Offliner::RequestStatus::UNKNOWN),
29 weak_ptr_factory_(this) {
27 DCHECK(policy_ != nullptr); 30 DCHECK(policy_ != nullptr);
31 picker_.reset(new RequestPicker(
32 queue_.get(),
33 base::Bind(&RequestCoordinator::RequestPicked,
34 weak_ptr_factory_.GetWeakPtr()),
35 base::Bind(&RequestCoordinator::RequestQueueEmpty,
36 weak_ptr_factory_.GetWeakPtr())));
28 } 37 }
29 38
30 RequestCoordinator::~RequestCoordinator() {} 39 RequestCoordinator::~RequestCoordinator() {}
31 40
32 bool RequestCoordinator::SavePageLater( 41 bool RequestCoordinator::SavePageLater(
33 const GURL& url, const ClientId& client_id) { 42 const GURL& url, const ClientId& client_id) {
34 DVLOG(2) << "URL is " << url << " " << __FUNCTION__; 43 DVLOG(2) << "URL is " << url << " " << __FUNCTION__;
35 44
36 // TODO(petewil): We need a robust scheme for allocating new IDs. 45 // TODO(petewil): We need a robust scheme for allocating new IDs.
37 static int64_t id = 0; 46 static int64_t id = 0;
38 47
39 // Build a SavePageRequest. 48 // Build a SavePageRequest.
40 // TODO(petewil): Use something like base::Clock to help in testing. 49 // TODO(petewil): Use something like base::Clock to help in testing.
41 offline_pages::SavePageRequest request( 50 offline_pages::SavePageRequest request(
42 id++, url, client_id, base::Time::Now()); 51 id++, url, client_id, base::Time::Now());
43 52
44 // Put the request on the request queue. 53 // Put the request on the request queue.
45 queue_->AddRequest(request, 54 queue_->AddRequest(request,
46 base::Bind(&RequestCoordinator::AddRequestResultCallback, 55 base::Bind(&RequestCoordinator::AddRequestResultCallback,
47 AsWeakPtr())); 56 weak_ptr_factory_.GetWeakPtr()));
48 // TODO(petewil): Do I need to persist the request in case the add fails? 57 // TODO(petewil): Do I need to persist the request in case the add fails?
49 58
50 // TODO(petewil): Eventually we will wait for the StartProcessing callback, 59 // TODO(petewil): Make a new chromium command line switch to send the request
51 // but for now just kick start the request so we can test the wiring. 60 // immediately for testing. It should call SendRequestToOffliner()
52 SendRequestToOffliner(request);
53 61
54 return true; 62 return true;
55 } 63 }
56 64
57 void RequestCoordinator::AddRequestResultCallback( 65 void RequestCoordinator::AddRequestResultCallback(
58 RequestQueue::AddRequestResult result, 66 RequestQueue::AddRequestResult result,
59 const SavePageRequest& request) { 67 const SavePageRequest& request) {
60 DVLOG(2) << __FUNCTION__;
61 68
62 // Inform the scheduler that we have an outstanding task. 69 // Inform the scheduler that we have an outstanding task.
63 // TODO(petewil): Define proper TriggerConditions and set them. 70 // TODO(petewil): Define proper TriggerConditions and set them.
64 Scheduler::TriggerCondition conditions; 71 Scheduler::TriggerCondition conditions;
65 scheduler_->Schedule(conditions); 72 scheduler_->Schedule(conditions);
66 } 73 }
67 74
75 void RequestCoordinator::RequestPicked(const SavePageRequest& request) {
76 // Send the request on to the offliner.
77 SendRequestToOffliner(request);
78 }
79
80 void RequestCoordinator::RequestQueueEmpty() {
81 // Send the request on to the offliner.
chili 2016/05/27 23:54:13 I don't quite understand this comment. Which requ
Pete Williamson 2016/05/28 00:08:52 Good catch, cut and paste bug, comment meant for a
82 // TODO(petewil): return to the BackgroundScheduler by calling
83 // ProcessingDoneCallback
84 }
85
68 bool RequestCoordinator::StartProcessing( 86 bool RequestCoordinator::StartProcessing(
69 const ProcessingDoneCallback& callback) { 87 const ProcessingDoneCallback& callback) {
chili 2016/05/27 23:54:13 nit: does this fit on previous line?
Pete Williamson 2016/05/28 00:08:52 Nope, just a bit over 80.
88 // TODO(petewil): Check existing conditions (should be passed down from
89 // BackgroundTask)
90
91 // Choose a request to process that meets the available conditions.
92 // This is an async call, and returns right away.
93 picker_->ChooseNextRequest();
70 return false; 94 return false;
71 } 95 }
72 96
73 void RequestCoordinator::StopProcessing() { 97 void RequestCoordinator::StopProcessing() {
74 } 98 }
75 99
76 void RequestCoordinator::SendRequestToOffliner(const SavePageRequest& request) { 100 void RequestCoordinator::SendRequestToOffliner(const SavePageRequest& request) {
77 // TODO(petewil): When we have multiple offliners, we need to pick one. 101 // TODO(petewil): When we have multiple offliners, we need to pick one.
78 Offliner* offliner = factory_->GetOffliner(policy_.get()); 102 Offliner* offliner = factory_->GetOffliner(policy_.get());
79 if (!offliner) { 103 if (!offliner) {
80 DVLOG(0) << "Unable to create Offliner. " 104 DVLOG(0) << "Unable to create Offliner. "
81 << "Cannot background offline page."; 105 << "Cannot background offline page.";
82 return; 106 return;
83 } 107 }
84 108
85 // Start the load and save process in the offliner (Async). 109 // Start the load and save process in the offliner (Async).
86 offliner->LoadAndSave( 110 offliner->LoadAndSave(request,
87 request, 111 base::Bind(&RequestCoordinator::OfflinerDoneCallback,
88 base::Bind(&RequestCoordinator::OfflinerDoneCallback, AsWeakPtr())); 112 weak_ptr_factory_.GetWeakPtr()));
89 } 113 }
90 114
91 void RequestCoordinator::OfflinerDoneCallback(const SavePageRequest& request, 115 void RequestCoordinator::OfflinerDoneCallback(const SavePageRequest& request,
92 Offliner::RequestStatus status) { 116 Offliner::RequestStatus status) {
93 DVLOG(2) << "offliner finished, saved: " 117 DVLOG(2) << "offliner finished, saved: "
94 << (status == Offliner::RequestStatus::SAVED) << ", " 118 << (status == Offliner::RequestStatus::SAVED) << ", "
95 << __FUNCTION__; 119 << __FUNCTION__;
96 last_offlining_status_ = status; 120 last_offlining_status_ = status;
121
122 // TODO(petewil): Check time budget. Start a request if we have time, return
123 // to the scheduler if we are out of time.
124
125 // TODO(petewil): If the request succeeded, remove it from the Queue.
97 } 126 }
98 127
99 } // namespace offline_pages 128 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698