Chromium Code Reviews| 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 // If we are not working on this request in an offliner, and it is not | |
| 91 // valid, put it on a list for removal. We make the exception for current | |
| 92 // requests because the request might expire after being chosen and before | |
| 93 // we call cleanup, and we shouldn't delete the request while offlining it. | |
| 94 if (status != OfflinerPolicyUtils::RequestExpirationStatus::VALID && | |
| 95 request->request_state() != SavePageRequest::RequestState::OFFLINING) { | |
|
dougarnett
2016/12/06 00:59:41
This check relies on the reconciliation task to ha
Pete Williamson
2016/12/06 02:06:28
Yes, but we are planning on making a reconciliatio
dougarnett
2016/12/06 02:11:13
Please document the dependency
| |
| 96 // TODO(petewil): Push both request and reason, will need to change type | |
| 97 // of list to pairs. | |
| 98 expired_request_ids->push_back(request->request_id()); | |
| 99 } | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 } // namespace offline_pages | |
| OLD | NEW |