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/request_coordinator_event_logger.h " | |
| 11 #include "components/offline_pages/background/request_notifier.h" | |
| 12 #include "components/offline_pages/background/request_queue_store.h" | |
| 13 #include "components/offline_pages/background/save_page_request.h" | |
| 14 | |
| 15 namespace offline_pages { | |
| 16 | |
| 17 CleanupTask::CleanupTask(RequestQueueStore* store, | |
| 18 OfflinerPolicy* policy, | |
| 19 RequestNotifier* notifier, | |
| 20 RequestCoordinatorEventLogger* event_logger) | |
| 21 : store_(store), | |
| 22 policy_(policy), | |
| 23 notifier_(notifier), | |
| 24 event_logger_(event_logger), | |
| 25 weak_ptr_factory_(this) {} | |
| 26 | |
| 27 CleanupTask::~CleanupTask() {} | |
| 28 | |
| 29 void CleanupTask::GetExpiredRequestIds( | |
|
dougarnett
2016/12/02 17:20:26
should this be positioned later in file per orderi
fgorski
2016/12/02 21:44:04
Actually this can be defined as a function in anon
Pete Williamson
2016/12/05 20:39:45
Moved.
Pete Williamson
2016/12/05 20:39:45
Created a new class, OfflinerPolicyUtils for the b
| |
| 30 std::vector<std::unique_ptr<SavePageRequest>> requests, | |
| 31 std::vector<int64_t>* expired_request_ids) { | |
| 32 for (auto& request : requests) { | |
| 33 // Check for requests past their expiration time or with too many tries. | |
| 34 if (base::Time::Now() - request->creation_time() >= | |
| 35 base::TimeDelta::FromSeconds( | |
| 36 policy_->GetRequestExpirationTimeInSeconds()) || | |
| 37 request->started_attempt_count() >= policy_->GetMaxStartedTries() || | |
| 38 request->completed_attempt_count() >= policy_->GetMaxCompletedTries()) { | |
| 39 expired_request_ids->push_back(request->request_id()); | |
| 40 } | |
| 41 } | |
| 42 } | |
| 43 | |
| 44 void CleanupTask::Run() { | |
| 45 GetRequests(); | |
| 46 } | |
| 47 | |
| 48 void CleanupTask::GetRequests() { | |
| 49 // Get all the requests from the queue, we will classify them in the callback. | |
| 50 store_->GetRequests( | |
| 51 base::Bind(&CleanupTask::Prune, weak_ptr_factory_.GetWeakPtr())); | |
| 52 } | |
| 53 | |
| 54 void CleanupTask::Prune( | |
|
fgorski
2016/12/02 21:44:04
what is the meaning of prune?
Why does it deserve
Pete Williamson
2016/12/05 20:39:45
No reason to keep RemoveStaleRequests, so I got ri
| |
| 55 bool success, | |
| 56 std::vector<std::unique_ptr<SavePageRequest>> requests) { | |
| 57 // If there is nothing to do, return right away. | |
| 58 if (requests.empty()) { | |
| 59 TaskComplete(); | |
| 60 return; | |
| 61 } | |
| 62 | |
| 63 // Get the expired requests to be removed from the queue. | |
| 64 std::vector<int64_t> expired_request_ids; | |
| 65 GetExpiredRequestIds(std::move(requests), &expired_request_ids); | |
| 66 | |
| 67 // Continue processing by handling expired requests, if any. | |
| 68 if (expired_request_ids.size() == 0) { | |
| 69 TaskComplete(); | |
| 70 return; | |
| 71 } | |
| 72 | |
| 73 RemoveStaleRequests(std::move(expired_request_ids)); | |
| 74 } | |
| 75 | |
| 76 void CleanupTask::RemoveStaleRequests(std::vector<int64_t> stale_request_ids) { | |
| 77 // TODO: Add UMA when we remove them, saying why | |
|
dougarnett
2016/12/02 17:20:26
TODO(petewil):
Pete Williamson
2016/12/05 20:39:45
Done.
| |
| 78 store_->RemoveRequests(stale_request_ids, | |
| 79 base::Bind(&CleanupTask::OnRequestsExpired, | |
| 80 weak_ptr_factory_.GetWeakPtr())); | |
| 81 } | |
| 82 | |
| 83 void CleanupTask::OnRequestsExpired( | |
| 84 std::unique_ptr<UpdateRequestsResult> result) { | |
| 85 RequestNotifier::BackgroundSavePageResult save_page_result( | |
| 86 RequestNotifier::BackgroundSavePageResult::EXPIRED); | |
|
dougarnett
2016/12/02 17:20:26
I think we want to figure out which of the 3 Backg
Pete Williamson
2016/12/05 20:39:45
Can we do that in a later changelist? It involves
dougarnett
2016/12/05 21:27:45
Sure, we can TODO and follow-up. This is new behav
Pete Williamson
2016/12/06 00:25:38
Acknowledged.
| |
| 87 for (const auto& request : result->updated_items) { | |
| 88 event_logger_->RecordDroppedSavePageRequest( | |
| 89 request.client_id().name_space, save_page_result, request.request_id()); | |
| 90 notifier_->NotifyCompleted(request, save_page_result); | |
| 91 } | |
| 92 | |
| 93 // The task is now done, return control to the task queue. | |
| 94 TaskComplete(); | |
| 95 } | |
| 96 }; | |
| OLD | NEW |