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

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

Issue 2078113002: Don't start a second pre-render if one is in progress. (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 : request_in_progress_(false),
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() {}
36 37
37 bool RequestCoordinator::SavePageLater( 38 bool RequestCoordinator::SavePageLater(
38 const GURL& url, const ClientId& client_id) { 39 const GURL& url, const ClientId& client_id) {
39 DVLOG(2) << "URL is " << url << " " << __FUNCTION__; 40 DVLOG(2) << "URL is " << url << " " << __FUNCTION__;
40 41
41 // TODO(petewil): We need a robust scheme for allocating new IDs. 42 // TODO(petewil): We need a robust scheme for allocating new IDs.
42 static int64_t id = 0; 43 static int64_t id = 0;
43 44
44 // Build a SavePageRequest. 45 // Build a SavePageRequest.
45 // TODO(petewil): Use something like base::Clock to help in testing. 46 // TODO(petewil): Use something like base::Clock to help in testing.
46 offline_pages::SavePageRequest request( 47 offline_pages::SavePageRequest request(
47 id++, url, client_id, base::Time::Now()); 48 id++, url, client_id, base::Time::Now());
48 49
49 // Put the request on the request queue. 50 // Put the request on the request queue.
50 queue_->AddRequest(request, 51 queue_->AddRequest(request,
51 base::Bind(&RequestCoordinator::AddRequestResultCallback, 52 base::Bind(&RequestCoordinator::AddRequestResultCallback,
52 weak_ptr_factory_.GetWeakPtr())); 53 weak_ptr_factory_.GetWeakPtr()));
53 // TODO(petewil): Do I need to persist the request in case the add fails?
54
55 // TODO(petewil): Make a new chromium command line switch to send the request
56 // immediately for testing. It should call SendRequestToOffliner()
57
58 return true; 54 return true;
59 } 55 }
60 56
61 void RequestCoordinator::AddRequestResultCallback( 57 void RequestCoordinator::AddRequestResultCallback(
62 RequestQueue::AddRequestResult result, 58 RequestQueue::AddRequestResult result,
63 const SavePageRequest& request) { 59 const SavePageRequest& request) {
64 60
65 // Inform the scheduler that we have an outstanding task. 61 // Inform the scheduler that we have an outstanding task.
66 // TODO(petewil): Define proper TriggerConditions and set them. 62 // TODO(petewil): Define proper TriggerConditions and set them.
67 Scheduler::TriggerCondition conditions; 63 Scheduler::TriggerCondition conditions;
(...skipping 10 matching lines...) Expand all
78 SendRequestToOffliner(request); 74 SendRequestToOffliner(request);
79 } 75 }
80 76
81 void RequestCoordinator::RequestQueueEmpty() { 77 void RequestCoordinator::RequestQueueEmpty() {
82 // Clear the outstanding "safety" task in the scheduler. 78 // Clear the outstanding "safety" task in the scheduler.
83 scheduler_->Unschedule(); 79 scheduler_->Unschedule();
84 // Return control to the scheduler when there is no more to do. 80 // Return control to the scheduler when there is no more to do.
85 scheduler_callback_.Run(true); 81 scheduler_callback_.Run(true);
86 } 82 }
87 83
84 // Returns true if the caller should expect a callback, false otherwise. For
85 // instance, this would return false if a request is already in progress.
88 bool RequestCoordinator::StartProcessing( 86 bool RequestCoordinator::StartProcessing(
89 const DeviceConditions& device_conditions, 87 const DeviceConditions& device_conditions,
90 const base::Callback<void(bool)>& callback) { 88 const base::Callback<void(bool)>& callback) {
89 if (request_in_progress_) {
fgorski 2016/06/20 20:24:07 nit: {} are not necessary here. (not required by t
Pete Williamson 2016/06/21 17:55:25 Done.
90 return false;
91 }
92
91 scheduler_callback_ = callback; 93 scheduler_callback_ = callback;
92 // TODO(petewil): Check existing conditions (should be passed down from 94 // TODO(petewil): Check existing conditions (should be passed down from
93 // BackgroundTask) 95 // BackgroundTask)
94 96
95 TryNextRequest(); 97 TryNextRequest();
96 98
97 // TODO(petewil): Should return true if the caller should expect a
98 // callback. Return false if there is already a request running.
99 // Probably best to do this when I prevent multiple instances from
100 // running at the same time.
101 return true; 99 return true;
102 } 100 }
103 101
104 void RequestCoordinator::TryNextRequest() { 102 void RequestCoordinator::TryNextRequest() {
105 // Choose a request to process that meets the available conditions. 103 // Choose a request to process that meets the available conditions.
106 // This is an async call, and returns right away. 104 // This is an async call, and returns right away.
107 picker_->ChooseNextRequest( 105 picker_->ChooseNextRequest(
108 base::Bind(&RequestCoordinator::RequestPicked, 106 base::Bind(&RequestCoordinator::RequestPicked,
109 weak_ptr_factory_.GetWeakPtr()), 107 weak_ptr_factory_.GetWeakPtr()),
110 base::Bind(&RequestCoordinator::RequestQueueEmpty, 108 base::Bind(&RequestCoordinator::RequestQueueEmpty,
111 weak_ptr_factory_.GetWeakPtr())); 109 weak_ptr_factory_.GetWeakPtr()));
112 } 110 }
113 111
114 void RequestCoordinator::StopProcessing() { 112 void RequestCoordinator::StopProcessing() {
115 } 113 }
116 114
117 void RequestCoordinator::SendRequestToOffliner(const SavePageRequest& request) { 115 void RequestCoordinator::SendRequestToOffliner(const SavePageRequest& request) {
118 // TODO(petewil): Ensure only one offliner at a time is used.
119 // TODO(petewil): When we have multiple offliners, we need to pick one. 116 // TODO(petewil): When we have multiple offliners, we need to pick one.
120 Offliner* offliner = factory_->GetOffliner(policy_.get()); 117 Offliner* offliner = factory_->GetOffliner(policy_.get());
121 if (!offliner) { 118 if (!offliner) {
122 DVLOG(0) << "Unable to create Offliner. " 119 DVLOG(0) << "Unable to create Offliner. "
123 << "Cannot background offline page."; 120 << "Cannot background offline page.";
124 return; 121 return;
125 } 122 }
126 123
124 DCHECK(request_in_progress_ == false);
fgorski 2016/06/20 20:24:07 nit: DCHECK(!request_in_progress_);
Pete Williamson 2016/06/21 17:55:25 Done.
125 request_in_progress_ = true;
126
127 // Start the load and save process in the offliner (Async). 127 // Start the load and save process in the offliner (Async).
128 offliner->LoadAndSave(request, 128 offliner->LoadAndSave(request,
129 base::Bind(&RequestCoordinator::OfflinerDoneCallback, 129 base::Bind(&RequestCoordinator::OfflinerDoneCallback,
130 weak_ptr_factory_.GetWeakPtr())); 130 weak_ptr_factory_.GetWeakPtr()));
131 } 131 }
132 132
133 void RequestCoordinator::OfflinerDoneCallback(const SavePageRequest& request, 133 void RequestCoordinator::OfflinerDoneCallback(const SavePageRequest& request,
134 Offliner::RequestStatus status) { 134 Offliner::RequestStatus status) {
135 DVLOG(2) << "offliner finished, saved: " 135 DVLOG(2) << "offliner finished, saved: "
136 << (status == Offliner::RequestStatus::SAVED) << ", " 136 << (status == Offliner::RequestStatus::SAVED) << ", "
137 << __FUNCTION__; 137 << __FUNCTION__;
138 last_offlining_status_ = status; 138 last_offlining_status_ = status;
139 139
140 request_in_progress_ = false;
fgorski 2016/06/20 20:24:07 do you think it makes sense to DCHECK(request_in_p
Pete Williamson 2016/06/21 17:55:25 Done.
Pete Williamson 2016/06/21 17:57:45 Oops, I did it and then undid it. I undid it sinc
141
140 // If the request succeeded, remove it from the Queue and maybe schedule 142 // If the request succeeded, remove it from the Queue and maybe schedule
141 // another one. 143 // another one.
142 if (status == Offliner::RequestStatus::SAVED) { 144 if (status == Offliner::RequestStatus::SAVED) {
143 queue_->RemoveRequest(request.request_id(), 145 queue_->RemoveRequest(request.request_id(),
144 base::Bind(&RequestCoordinator::UpdateRequestCallback, 146 base::Bind(&RequestCoordinator::UpdateRequestCallback,
145 weak_ptr_factory_.GetWeakPtr())); 147 weak_ptr_factory_.GetWeakPtr()));
146 148
147 // TODO(petewil): Check time budget. Return to the scheduler if we are out. 149 // TODO(petewil): Check time budget. Return to the scheduler if we are out.
148 150
149 // Start another request if we have time. 151 // Start another request if we have time.
150 TryNextRequest(); 152 TryNextRequest();
151 } 153 }
152 } 154 }
153 155
154 } // namespace offline_pages 156 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698