OLD | NEW |
| (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 : store_(store), | |
23 policy_(policy), | |
24 notifier_(notifier), | |
25 event_logger_(event_logger), | |
26 weak_ptr_factory_(this) {} | |
27 | |
28 CleanupTask::~CleanupTask() {} | |
29 | |
30 void CleanupTask::Run() { | |
31 GetRequests(); | |
32 } | |
33 | |
34 void CleanupTask::GetRequests() { | |
35 // Get all the requests from the queue, we will classify them in the callback. | |
36 store_->GetRequests( | |
37 base::Bind(&CleanupTask::Prune, weak_ptr_factory_.GetWeakPtr())); | |
38 } | |
39 | |
40 void CleanupTask::Prune( | |
41 bool success, | |
42 std::vector<std::unique_ptr<SavePageRequest>> requests) { | |
43 // If there is nothing to do, return right away. | |
44 if (requests.empty()) { | |
45 TaskComplete(); | |
46 return; | |
47 } | |
48 | |
49 // Get the expired requests to be removed from the queue. | |
50 std::vector<int64_t> expired_request_ids; | |
51 GetExpiredRequestIds(std::move(requests), &expired_request_ids); | |
52 | |
53 // Continue processing by handling expired requests, if any. | |
54 if (expired_request_ids.size() == 0) { | |
55 TaskComplete(); | |
56 return; | |
57 } | |
58 | |
59 // TODO(petewil): Add UMA saying why we remove them | |
60 // TODO(petewil): Round trip the reason for deleting through the RQ | |
61 store_->RemoveRequests(expired_request_ids, | |
62 base::Bind(&CleanupTask::OnRequestsExpired, | |
63 weak_ptr_factory_.GetWeakPtr())); | |
64 } | |
65 | |
66 void CleanupTask::OnRequestsExpired( | |
67 std::unique_ptr<UpdateRequestsResult> result) { | |
68 RequestNotifier::BackgroundSavePageResult save_page_result( | |
69 RequestNotifier::BackgroundSavePageResult::EXPIRED); | |
70 for (const auto& request : result->updated_items) { | |
71 event_logger_->RecordDroppedSavePageRequest( | |
72 request.client_id().name_space, save_page_result, request.request_id()); | |
73 notifier_->NotifyCompleted(request, save_page_result); | |
74 } | |
75 | |
76 // The task is now done, return control to the task queue. | |
77 TaskComplete(); | |
78 } | |
79 | |
80 void CleanupTask::GetExpiredRequestIds( | |
81 std::vector<std::unique_ptr<SavePageRequest>> requests, | |
82 std::vector<int64_t>* expired_request_ids) { | |
83 for (auto& request : requests) { | |
84 // Check for requests past their expiration time or with too many tries. If | |
85 // it is not still valid, push the request and the reason onto the deletion | |
86 // list. | |
87 OfflinerPolicyUtils::RequestExpirationStatus status = | |
88 OfflinerPolicyUtils::CheckRequestExpirationStatus(request.get(), | |
89 policy_); | |
90 | |
91 // TODO(petewil): The strategy of checking the state below relies on a | |
92 // reconciliation task, so at startup we convert any OFFLINING requests to | |
93 // AVAILABLE. Otherwise, requests in the OFFLINING state when chromium dies | |
94 // will never be cleaned up. | |
95 | |
96 // If we are not working on this request in an offliner, and it is not | |
97 // valid, put it on a list for removal. We make the exception for current | |
98 // requests because the request might expire after being chosen and before | |
99 // we call cleanup, and we shouldn't delete the request while offlining it. | |
100 if (status != OfflinerPolicyUtils::RequestExpirationStatus::VALID && | |
101 request->request_state() != SavePageRequest::RequestState::OFFLINING) { | |
102 // TODO(petewil): Push both request and reason, will need to change type | |
103 // of list to pairs. | |
104 expired_request_ids->push_back(request->request_id()); | |
105 } | |
106 } | |
107 } | |
108 | |
109 } // namespace offline_pages | |
OLD | NEW |