OLD | NEW |
---|---|
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( | |
dougarnett
2016/05/31 17:45:37
Doesn't look like we are accessing as KeyedService
dougarnett
2016/05/31 18:03:04
never mind this too
| |
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 // TODO(petewil): return to the BackgroundScheduler by calling | |
82 // ProcessingDoneCallback | |
83 } | |
84 | |
68 bool RequestCoordinator::StartProcessing( | 85 bool RequestCoordinator::StartProcessing( |
69 const ProcessingDoneCallback& callback) { | 86 const ProcessingDoneCallback& callback) { |
87 // TODO(petewil): Check existing conditions (should be passed down from | |
88 // BackgroundTask) | |
89 | |
90 // Choose a request to process that meets the available conditions. | |
91 // This is an async call, and returns right away. | |
92 picker_->ChooseNextRequest(); | |
dougarnett
2016/05/31 18:03:04
We might want to keep track that we have a request
| |
70 return false; | 93 return false; |
71 } | 94 } |
72 | 95 |
73 void RequestCoordinator::StopProcessing() { | 96 void RequestCoordinator::StopProcessing() { |
74 } | 97 } |
75 | 98 |
76 void RequestCoordinator::SendRequestToOffliner(const SavePageRequest& request) { | 99 void RequestCoordinator::SendRequestToOffliner(const SavePageRequest& request) { |
77 // TODO(petewil): When we have multiple offliners, we need to pick one. | 100 // TODO(petewil): When we have multiple offliners, we need to pick one. |
78 Offliner* offliner = factory_->GetOffliner(policy_.get()); | 101 Offliner* offliner = factory_->GetOffliner(policy_.get()); |
79 if (!offliner) { | 102 if (!offliner) { |
80 DVLOG(0) << "Unable to create Offliner. " | 103 DVLOG(0) << "Unable to create Offliner. " |
81 << "Cannot background offline page."; | 104 << "Cannot background offline page."; |
82 return; | 105 return; |
83 } | 106 } |
84 | 107 |
85 // Start the load and save process in the offliner (Async). | 108 // Start the load and save process in the offliner (Async). |
86 offliner->LoadAndSave( | 109 offliner->LoadAndSave(request, |
87 request, | 110 base::Bind(&RequestCoordinator::OfflinerDoneCallback, |
88 base::Bind(&RequestCoordinator::OfflinerDoneCallback, AsWeakPtr())); | 111 weak_ptr_factory_.GetWeakPtr())); |
89 } | 112 } |
90 | 113 |
91 void RequestCoordinator::OfflinerDoneCallback(const SavePageRequest& request, | 114 void RequestCoordinator::OfflinerDoneCallback(const SavePageRequest& request, |
92 Offliner::RequestStatus status) { | 115 Offliner::RequestStatus status) { |
93 DVLOG(2) << "offliner finished, saved: " | 116 DVLOG(2) << "offliner finished, saved: " |
94 << (status == Offliner::RequestStatus::SAVED) << ", " | 117 << (status == Offliner::RequestStatus::SAVED) << ", " |
95 << __FUNCTION__; | 118 << __FUNCTION__; |
96 last_offlining_status_ = status; | 119 last_offlining_status_ = status; |
120 | |
121 // TODO(petewil): Check time budget. Start a request if we have time, return | |
122 // to the scheduler if we are out of time. | |
123 | |
124 // TODO(petewil): If the request succeeded, remove it from the Queue. | |
97 } | 125 } |
98 | 126 |
99 } // namespace offline_pages | 127 } // namespace offline_pages |
OLD | NEW |