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

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

Issue 2543093002: Split the RequestPicker task into two separate tasks. (Closed)
Patch Set: CR feedback per FGorski and DougArnett Created 4 years 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "components/offline_pages/background/cleanup_task.h"
6
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "components/offline_pages/background/offliner_policy.h"
10 #include "components/offline_pages/background/offliner_policy_utils.h"
11 #include "components/offline_pages/background/request_coordinator_event_logger.h "
12 #include "components/offline_pages/background/request_notifier.h"
13 #include "components/offline_pages/background/request_queue_store.h"
14 #include "components/offline_pages/background/save_page_request.h"
15
16 namespace offline_pages {
17
18 CleanupTask::CleanupTask(RequestQueueStore* store,
19 OfflinerPolicy* policy,
20 RequestNotifier* notifier,
21 RequestCoordinatorEventLogger* event_logger,
22 int64_t current_request_id)
23 : store_(store),
24 policy_(policy),
25 notifier_(notifier),
26 event_logger_(event_logger),
27 current_request_id_(current_request_id),
28 weak_ptr_factory_(this) {}
29
30 CleanupTask::~CleanupTask() {}
31
32 void CleanupTask::Run() {
33 GetRequests();
34 }
35
36 void CleanupTask::GetRequests() {
37 // Get all the requests from the queue, we will classify them in the callback.
38 store_->GetRequests(
39 base::Bind(&CleanupTask::Prune, weak_ptr_factory_.GetWeakPtr()));
40 }
41
42 void CleanupTask::Prune(
43 bool success,
44 std::vector<std::unique_ptr<SavePageRequest>> requests) {
45 // If there is nothing to do, return right away.
46 if (requests.empty()) {
47 TaskComplete();
48 return;
49 }
50
51 // Get the expired requests to be removed from the queue.
52 std::vector<int64_t> expired_request_ids;
53 GetExpiredRequestIds(std::move(requests), &expired_request_ids);
54
55 // Continue processing by handling expired requests, if any.
56 if (expired_request_ids.size() == 0) {
57 TaskComplete();
58 return;
59 }
60
61 // TODO(petewil): Add UMA saying why we remove them
62 // TODO(petewil): Round trip the reason for deleting through the RQ
63 store_->RemoveRequests(expired_request_ids,
64 base::Bind(&CleanupTask::OnRequestsExpired,
65 weak_ptr_factory_.GetWeakPtr()));
66 }
67
68 void CleanupTask::OnRequestsExpired(
69 std::unique_ptr<UpdateRequestsResult> result) {
70 RequestNotifier::BackgroundSavePageResult save_page_result(
71 RequestNotifier::BackgroundSavePageResult::EXPIRED);
72 for (const auto& request : result->updated_items) {
73 event_logger_->RecordDroppedSavePageRequest(
74 request.client_id().name_space, save_page_result, request.request_id());
75 notifier_->NotifyCompleted(request, save_page_result);
76 }
77
78 // The task is now done, return control to the task queue.
79 TaskComplete();
80 }
81
82 void CleanupTask::GetExpiredRequestIds(
83 std::vector<std::unique_ptr<SavePageRequest>> requests,
84 std::vector<int64_t>* expired_request_ids) {
85 for (auto& request : requests) {
86 // Check for requests past their expiration time or with too many tries. If
87 // it is not still valid, push the request and the reason onto the deletion
88 // list.
89 OfflinerPolicyUtils::RequestExpirationStatus status =
90 OfflinerPolicyUtils::CheckRequestExpirationStatus(request.get(),
91 policy_);
92 // If we are not working on this request in an offliner, and it is not
93 // valid, put it on a list for removal. We make the exception for current
94 // requests because the request might expire after being chosen and before
95 // we call cleanup, and we shouldn't delete the request while offlining it.
96 if (status != OfflinerPolicyUtils::RequestExpirationStatus::VALID &&
97 request->request_id() != current_request_id_) {
fgorski 2016/12/05 21:05:37 This does not scale to multiple offliners. can you
Pete Williamson 2016/12/06 00:25:38 Done.
98 // TODO(petewil): Push both request and reason, will need to change type
99 // of list to pairs.
100 expired_request_ids->push_back(request->request_id());
101 }
102 }
103 }
104
105 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698