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

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

Issue 1971923004: Add calls to the offliner and unit tests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: CR feedback per DougArnett Created 4 years, 7 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 "components/offline_pages/background/offliner_factory.h" 10 #include "components/offline_pages/background/offliner_factory.h"
11 #include "components/offline_pages/background/offliner_policy.h" 11 #include "components/offline_pages/background/offliner_policy.h"
12 #include "components/offline_pages/background/save_page_request.h" 12 #include "components/offline_pages/background/save_page_request.h"
13 #include "components/offline_pages/background/scheduler.h" 13 #include "components/offline_pages/background/scheduler.h"
14 #include "components/offline_pages/offline_page_item.h" 14 #include "components/offline_pages/offline_page_item.h"
15 15
16 namespace offline_pages { 16 namespace offline_pages {
17 17
18 RequestCoordinator::RequestCoordinator( 18 RequestCoordinator::RequestCoordinator(std::unique_ptr<OfflinerPolicy> policy,
19 std::unique_ptr<OfflinerPolicy> policy, 19 std::unique_ptr<OfflinerFactory> factory,
20 std::unique_ptr<OfflinerFactory> factory, 20 std::unique_ptr<RequestQueue> queue,
21 std::unique_ptr<RequestQueue> queue, 21 std::unique_ptr<Scheduler> scheduler)
22 std::unique_ptr<Scheduler> scheduler) 22 : policy_(std::move(policy)),
23 : policy_(std::move(policy)), factory_(std::move(factory)), 23 factory_(std::move(factory)),
24 queue_(std::move(queue)), scheduler_(std::move(scheduler)) { 24 queue_(std::move(queue)),
25 scheduler_(std::move(scheduler)),
26 last_offlining_status_(Offliner::UNKNOWN) {
25 DCHECK(policy_ != nullptr); 27 DCHECK(policy_ != nullptr);
26 } 28 }
27 29
28 RequestCoordinator::~RequestCoordinator() {} 30 RequestCoordinator::~RequestCoordinator() {}
29 31
30 bool RequestCoordinator::SavePageLater( 32 bool RequestCoordinator::SavePageLater(
31 const GURL& url, const ClientId& client_id) { 33 const GURL& url, const ClientId& client_id) {
32 DVLOG(2) << "URL is " << url << " " << __FUNCTION__; 34 DVLOG(2) << "URL is " << url << " " << __FUNCTION__;
33 35
34 // TODO(petewil): We need a robust scheme for allocating new IDs. 36 // TODO(petewil): We need a robust scheme for allocating new IDs.
35 static int64_t id = 0; 37 static int64_t id = 0;
36 38
37 // Build a SavePageRequest. 39 // Build a SavePageRequest.
38 // TODO(petewil): Use something like base::Clock to help in testing. 40 // TODO(petewil): Use something like base::Clock to help in testing.
39 offline_pages::SavePageRequest request( 41 offline_pages::SavePageRequest request(
40 id++, url, client_id, base::Time::Now()); 42 id++, url, client_id, base::Time::Now());
41 43
42 // Put the request on the request queue. 44 // Put the request on the request queue.
43 queue_->AddRequest(request, 45 queue_->AddRequest(request,
44 base::Bind(&RequestCoordinator::AddRequestResultCallback, 46 base::Bind(&RequestCoordinator::AddRequestResultCallback,
45 AsWeakPtr())); 47 AsWeakPtr()));
46 // TODO: Do I need to persist the request in case the add fails? 48 // TODO: Do I need to persist the request in case the add fails?
47 49
50 // TODO(petewil): Eventually we will wait for the StartProcessing callback,
51 // but for now just kick start the request so we can test the wiring.
52 SendRequestToOffliner(request);
53
48 return true; 54 return true;
49 } 55 }
50 56
51 void RequestCoordinator::AddRequestResultCallback( 57 void RequestCoordinator::AddRequestResultCallback(
52 RequestQueue::AddRequestResult result, 58 RequestQueue::AddRequestResult result,
53 const SavePageRequest& request) { 59 const SavePageRequest& request) {
54 DVLOG(2) << __FUNCTION__; 60 DVLOG(2) << __FUNCTION__;
55 61
56 // Inform the scheduler that we have an outstanding task. 62 // Inform the scheduler that we have an outstanding task.
57 // TODO(petewil): Define proper TriggerConditions and set them. 63 // TODO(petewil): Define proper TriggerConditions and set them.
58 Scheduler::TriggerCondition conditions; 64 Scheduler::TriggerCondition conditions;
59 scheduler_->Schedule(conditions); 65 scheduler_->Schedule(conditions);
60 } 66 }
61 67
62 bool RequestCoordinator::StartProcessing( 68 bool RequestCoordinator::StartProcessing(
63 const ProcessingDoneCallback& callback) { 69 const ProcessingDoneCallback& callback) {
64 return false; 70 return false;
65 } 71 }
66 72
67 void RequestCoordinator::StopProcessing() { 73 void RequestCoordinator::StopProcessing() {
68 } 74 }
69 75
76 void RequestCoordinator::SendRequestToOffliner(SavePageRequest& request) {
77 // TODO(petewil): When we have multiple offliners, we need to pick one.
78 Offliner* offliner = factory_->GetOffliner(policy_.get());
79 if (!offliner) {
80 LOG(ERROR) << "Unable to create Prerendering Offliner. "
81 << "Cannot background offline page.";
82 return;
83 }
84
85 // Start the load and save process in the prerenderer (Async).
86 offliner->LoadAndSave(
87 request,
88 base::Bind(&RequestCoordinator::OfflinerDoneCallback, AsWeakPtr()));
89 }
90
91 void RequestCoordinator::OfflinerDoneCallback(
92 const SavePageRequest& request,
93 Offliner::CompletionStatus status) {
94 DVLOG(2) << "prerenderer finished, status " << status << ", " << __FUNCTION__;
95 last_offlining_status_ = status;
96 }
97
70 } // namespace offline_pages 98 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698