Chromium Code Reviews| Index: components/offline_pages/background/cleanup_task.cc |
| diff --git a/components/offline_pages/background/cleanup_task.cc b/components/offline_pages/background/cleanup_task.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f6506f4d29d1c0a76e35d0fc4c6d6ca88f24ad94 |
| --- /dev/null |
| +++ b/components/offline_pages/background/cleanup_task.cc |
| @@ -0,0 +1,96 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/offline_pages/background/cleanup_task.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/logging.h" |
| +#include "components/offline_pages/background/offliner_policy.h" |
| +#include "components/offline_pages/background/request_coordinator_event_logger.h" |
| +#include "components/offline_pages/background/request_notifier.h" |
| +#include "components/offline_pages/background/request_queue_store.h" |
| +#include "components/offline_pages/background/save_page_request.h" |
| + |
| +namespace offline_pages { |
| + |
| +CleanupTask::CleanupTask(RequestQueueStore* store, |
| + OfflinerPolicy* policy, |
| + RequestNotifier* notifier, |
| + RequestCoordinatorEventLogger* event_logger) |
| + : store_(store), |
| + policy_(policy), |
| + notifier_(notifier), |
| + event_logger_(event_logger), |
| + weak_ptr_factory_(this) {} |
| + |
| +CleanupTask::~CleanupTask() {} |
| + |
| +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
|
| + std::vector<std::unique_ptr<SavePageRequest>> requests, |
| + std::vector<int64_t>* expired_request_ids) { |
| + for (auto& request : requests) { |
| + // Check for requests past their expiration time or with too many tries. |
| + if (base::Time::Now() - request->creation_time() >= |
| + base::TimeDelta::FromSeconds( |
| + policy_->GetRequestExpirationTimeInSeconds()) || |
| + request->started_attempt_count() >= policy_->GetMaxStartedTries() || |
| + request->completed_attempt_count() >= policy_->GetMaxCompletedTries()) { |
| + expired_request_ids->push_back(request->request_id()); |
| + } |
| + } |
| +} |
| + |
| +void CleanupTask::Run() { |
| + GetRequests(); |
| +} |
| + |
| +void CleanupTask::GetRequests() { |
| + // Get all the requests from the queue, we will classify them in the callback. |
| + store_->GetRequests( |
| + base::Bind(&CleanupTask::Prune, weak_ptr_factory_.GetWeakPtr())); |
| +} |
| + |
| +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
|
| + bool success, |
| + std::vector<std::unique_ptr<SavePageRequest>> requests) { |
| + // If there is nothing to do, return right away. |
| + if (requests.empty()) { |
| + TaskComplete(); |
| + return; |
| + } |
| + |
| + // Get the expired requests to be removed from the queue. |
| + std::vector<int64_t> expired_request_ids; |
| + GetExpiredRequestIds(std::move(requests), &expired_request_ids); |
| + |
| + // Continue processing by handling expired requests, if any. |
| + if (expired_request_ids.size() == 0) { |
| + TaskComplete(); |
| + return; |
| + } |
| + |
| + RemoveStaleRequests(std::move(expired_request_ids)); |
| +} |
| + |
| +void CleanupTask::RemoveStaleRequests(std::vector<int64_t> stale_request_ids) { |
| + // 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.
|
| + store_->RemoveRequests(stale_request_ids, |
| + base::Bind(&CleanupTask::OnRequestsExpired, |
| + weak_ptr_factory_.GetWeakPtr())); |
| +} |
| + |
| +void CleanupTask::OnRequestsExpired( |
| + std::unique_ptr<UpdateRequestsResult> result) { |
| + RequestNotifier::BackgroundSavePageResult save_page_result( |
| + 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.
|
| + for (const auto& request : result->updated_items) { |
| + event_logger_->RecordDroppedSavePageRequest( |
| + request.client_id().name_space, save_page_result, request.request_id()); |
| + notifier_->NotifyCompleted(request, save_page_result); |
| + } |
| + |
| + // The task is now done, return control to the task queue. |
| + TaskComplete(); |
| +} |
| +}; |