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(queue_.get())); |
28 } | 32 } |
29 | 33 |
30 RequestCoordinator::~RequestCoordinator() {} | 34 RequestCoordinator::~RequestCoordinator() {} |
31 | 35 |
32 bool RequestCoordinator::SavePageLater( | 36 bool RequestCoordinator::SavePageLater( |
33 const GURL& url, const ClientId& client_id) { | 37 const GURL& url, const ClientId& client_id) { |
34 DVLOG(2) << "URL is " << url << " " << __FUNCTION__; | 38 DVLOG(2) << "URL is " << url << " " << __FUNCTION__; |
35 | 39 |
36 // TODO(petewil): We need a robust scheme for allocating new IDs. | 40 // TODO(petewil): We need a robust scheme for allocating new IDs. |
37 static int64_t id = 0; | 41 static int64_t id = 0; |
38 | 42 |
39 // Build a SavePageRequest. | 43 // Build a SavePageRequest. |
40 // TODO(petewil): Use something like base::Clock to help in testing. | 44 // TODO(petewil): Use something like base::Clock to help in testing. |
41 offline_pages::SavePageRequest request( | 45 offline_pages::SavePageRequest request( |
42 id++, url, client_id, base::Time::Now()); | 46 id++, url, client_id, base::Time::Now()); |
43 | 47 |
44 // Put the request on the request queue. | 48 // Put the request on the request queue. |
45 queue_->AddRequest(request, | 49 queue_->AddRequest(request, |
46 base::Bind(&RequestCoordinator::AddRequestResultCallback, | 50 base::Bind(&RequestCoordinator::AddRequestResultCallback, |
47 AsWeakPtr())); | 51 weak_ptr_factory_.GetWeakPtr())); |
48 // TODO(petewil): Do I need to persist the request in case the add fails? | 52 // TODO(petewil): Do I need to persist the request in case the add fails? |
49 | 53 |
50 // TODO(petewil): Eventually we will wait for the StartProcessing callback, | 54 // 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. | 55 // immediately for testing. It should call SendRequestToOffliner() |
52 SendRequestToOffliner(request); | |
53 | 56 |
54 return true; | 57 return true; |
55 } | 58 } |
56 | 59 |
57 void RequestCoordinator::AddRequestResultCallback( | 60 void RequestCoordinator::AddRequestResultCallback( |
58 RequestQueue::AddRequestResult result, | 61 RequestQueue::AddRequestResult result, |
59 const SavePageRequest& request) { | 62 const SavePageRequest& request) { |
60 DVLOG(2) << __FUNCTION__; | |
61 | 63 |
62 // Inform the scheduler that we have an outstanding task. | 64 // Inform the scheduler that we have an outstanding task. |
63 // TODO(petewil): Define proper TriggerConditions and set them. | 65 // TODO(petewil): Define proper TriggerConditions and set them. |
64 Scheduler::TriggerCondition conditions; | 66 Scheduler::TriggerCondition conditions; |
65 scheduler_->Schedule(conditions); | 67 scheduler_->Schedule(conditions); |
66 } | 68 } |
67 | 69 |
| 70 void RequestCoordinator::RequestPicked(const SavePageRequest& request) { |
| 71 // Send the request on to the offliner. |
| 72 SendRequestToOffliner(request); |
| 73 } |
| 74 |
| 75 void RequestCoordinator::RequestQueueEmpty() { |
| 76 // TODO(petewil): return to the BackgroundScheduler by calling |
| 77 // ProcessingDoneCallback |
| 78 } |
| 79 |
68 bool RequestCoordinator::StartProcessing( | 80 bool RequestCoordinator::StartProcessing( |
69 const ProcessingDoneCallback& callback) { | 81 const ProcessingDoneCallback& callback) { |
| 82 // TODO(petewil): Check existing conditions (should be passed down from |
| 83 // BackgroundTask) |
| 84 |
| 85 // Choose a request to process that meets the available conditions. |
| 86 // This is an async call, and returns right away. |
| 87 picker_->ChooseNextRequest( |
| 88 base::Bind(&RequestCoordinator::RequestPicked, |
| 89 weak_ptr_factory_.GetWeakPtr()), |
| 90 base::Bind(&RequestCoordinator::RequestQueueEmpty, |
| 91 weak_ptr_factory_.GetWeakPtr())); |
70 return false; | 92 return false; |
71 } | 93 } |
72 | 94 |
73 void RequestCoordinator::StopProcessing() { | 95 void RequestCoordinator::StopProcessing() { |
74 } | 96 } |
75 | 97 |
76 void RequestCoordinator::SendRequestToOffliner(const SavePageRequest& request) { | 98 void RequestCoordinator::SendRequestToOffliner(const SavePageRequest& request) { |
77 // TODO(petewil): When we have multiple offliners, we need to pick one. | 99 // TODO(petewil): When we have multiple offliners, we need to pick one. |
78 Offliner* offliner = factory_->GetOffliner(policy_.get()); | 100 Offliner* offliner = factory_->GetOffliner(policy_.get()); |
79 if (!offliner) { | 101 if (!offliner) { |
80 DVLOG(0) << "Unable to create Offliner. " | 102 DVLOG(0) << "Unable to create Offliner. " |
81 << "Cannot background offline page."; | 103 << "Cannot background offline page."; |
82 return; | 104 return; |
83 } | 105 } |
84 | 106 |
85 // Start the load and save process in the offliner (Async). | 107 // Start the load and save process in the offliner (Async). |
86 offliner->LoadAndSave( | 108 offliner->LoadAndSave(request, |
87 request, | 109 base::Bind(&RequestCoordinator::OfflinerDoneCallback, |
88 base::Bind(&RequestCoordinator::OfflinerDoneCallback, AsWeakPtr())); | 110 weak_ptr_factory_.GetWeakPtr())); |
89 } | 111 } |
90 | 112 |
91 void RequestCoordinator::OfflinerDoneCallback(const SavePageRequest& request, | 113 void RequestCoordinator::OfflinerDoneCallback(const SavePageRequest& request, |
92 Offliner::RequestStatus status) { | 114 Offliner::RequestStatus status) { |
93 DVLOG(2) << "offliner finished, saved: " | 115 DVLOG(2) << "offliner finished, saved: " |
94 << (status == Offliner::RequestStatus::SAVED) << ", " | 116 << (status == Offliner::RequestStatus::SAVED) << ", " |
95 << __FUNCTION__; | 117 << __FUNCTION__; |
96 last_offlining_status_ = status; | 118 last_offlining_status_ = status; |
| 119 |
| 120 // TODO(petewil): Check time budget. Start a request if we have time, return |
| 121 // to the scheduler if we are out of time. |
| 122 |
| 123 // TODO(petewil): If the request succeeded, remove it from the Queue. |
97 } | 124 } |
98 | 125 |
99 } // namespace offline_pages | 126 } // namespace offline_pages |
OLD | NEW |