OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/offline_pages/core/background/cleanup_task.h" | 5 #include "components/offline_pages/core/background/cleanup_task.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "components/offline_pages/core/background/offliner_policy.h" | 9 #include "components/offline_pages/core/background/offliner_policy.h" |
10 #include "components/offline_pages/core/background/offliner_policy_utils.h" | 10 #include "components/offline_pages/core/background/offliner_policy_utils.h" |
11 #include "components/offline_pages/core/background/request_coordinator_event_log ger.h" | 11 #include "components/offline_pages/core/background/request_coordinator_event_log ger.h" |
12 #include "components/offline_pages/core/background/request_notifier.h" | 12 #include "components/offline_pages/core/background/request_notifier.h" |
13 #include "components/offline_pages/core/background/request_queue_store.h" | 13 #include "components/offline_pages/core/background/request_queue_store.h" |
14 #include "components/offline_pages/core/background/save_page_request.h" | 14 #include "components/offline_pages/core/background/save_page_request.h" |
15 | 15 |
16 namespace offline_pages { | 16 namespace offline_pages { |
17 namespace { | |
18 RequestNotifier::BackgroundSavePageResult ToBackgroundSavePageResult( | |
19 OfflinerPolicyUtils::RequestExpirationStatus expiration_status) { | |
20 switch (expiration_status) { | |
21 case OfflinerPolicyUtils::RequestExpirationStatus::EXPIRED: | |
22 return RequestNotifier::BackgroundSavePageResult::EXPIRED; | |
23 case OfflinerPolicyUtils::RequestExpirationStatus::START_COUNT_EXCEEDED: | |
24 return RequestNotifier::BackgroundSavePageResult::START_COUNT_EXCEEDED; | |
25 case OfflinerPolicyUtils::RequestExpirationStatus:: | |
26 COMPLETION_COUNT_EXCEEDED: | |
27 return RequestNotifier::BackgroundSavePageResult::RETRY_COUNT_EXCEEDED; | |
28 case OfflinerPolicyUtils::RequestExpirationStatus::VALID: | |
29 default: | |
30 NOTREACHED(); | |
31 return RequestNotifier::BackgroundSavePageResult::EXPIRED; | |
32 } | |
33 } | |
34 } // namespace | |
17 | 35 |
18 CleanupTask::CleanupTask(RequestQueueStore* store, | 36 CleanupTask::CleanupTask(RequestQueueStore* store, |
19 OfflinerPolicy* policy, | 37 OfflinerPolicy* policy, |
20 RequestNotifier* notifier, | 38 RequestNotifier* notifier, |
21 RequestCoordinatorEventLogger* event_logger) | 39 RequestCoordinatorEventLogger* event_logger) |
22 : store_(store), | 40 : store_(store), |
23 policy_(policy), | 41 policy_(policy), |
24 notifier_(notifier), | 42 notifier_(notifier), |
25 event_logger_(event_logger), | 43 event_logger_(event_logger), |
26 weak_ptr_factory_(this) {} | 44 weak_ptr_factory_(this) {} |
(...skipping 12 matching lines...) Expand all Loading... | |
39 | 57 |
40 void CleanupTask::Prune( | 58 void CleanupTask::Prune( |
41 bool success, | 59 bool success, |
42 std::vector<std::unique_ptr<SavePageRequest>> requests) { | 60 std::vector<std::unique_ptr<SavePageRequest>> requests) { |
43 // If there is nothing to do, return right away. | 61 // If there is nothing to do, return right away. |
44 if (requests.empty()) { | 62 if (requests.empty()) { |
45 TaskComplete(); | 63 TaskComplete(); |
46 return; | 64 return; |
47 } | 65 } |
48 | 66 |
49 // Get the expired requests to be removed from the queue. | 67 PopulateExpiredRequestIdsAndReasons(std::move(requests)); |
50 std::vector<int64_t> expired_request_ids; | |
51 GetExpiredRequestIds(std::move(requests), &expired_request_ids); | |
52 | 68 |
53 // Continue processing by handling expired requests, if any. | 69 // If there are not expired requests processing is done. |
Pete Williamson
2017/05/15 23:44:19
nit not-> not any
fgorski
2017/05/16 17:41:51
Done.
| |
54 if (expired_request_ids.size() == 0) { | 70 if (expired_request_ids_and_reasons_.size() == 0) { |
55 TaskComplete(); | 71 TaskComplete(); |
56 return; | 72 return; |
57 } | 73 } |
58 | 74 |
59 // TODO(petewil): Add UMA saying why we remove them. Round trip the reason | 75 std::vector<int64_t> expired_request_ids; |
60 // for deleting through the RQ callbacks. crbug.com/705115. | 76 for (auto const& id_reason_pair : expired_request_ids_and_reasons_) |
77 expired_request_ids.push_back(id_reason_pair.first); | |
78 | |
61 store_->RemoveRequests(expired_request_ids, | 79 store_->RemoveRequests(expired_request_ids, |
62 base::Bind(&CleanupTask::OnRequestsExpired, | 80 base::Bind(&CleanupTask::OnRequestsExpired, |
63 weak_ptr_factory_.GetWeakPtr())); | 81 weak_ptr_factory_.GetWeakPtr())); |
64 } | 82 } |
65 | 83 |
66 void CleanupTask::OnRequestsExpired( | 84 void CleanupTask::OnRequestsExpired( |
67 std::unique_ptr<UpdateRequestsResult> result) { | 85 std::unique_ptr<UpdateRequestsResult> result) { |
68 RequestNotifier::BackgroundSavePageResult save_page_result( | |
69 RequestNotifier::BackgroundSavePageResult::EXPIRED); | |
70 for (const auto& request : result->updated_items) { | 86 for (const auto& request : result->updated_items) { |
87 // Ensure we have an expiration reason for this request. | |
88 auto iter = expired_request_ids_and_reasons_.find(request.request_id()); | |
Pete Williamson
2017/05/15 23:44:19
Could we avoid n**2 behavior here by having a map
fgorski
2017/05/16 17:41:51
expired_request_ids_and_reasons_ is a map, making
Pete Williamson
2017/05/16 18:13:54
I think I was looking at expired_request_ids_, thi
| |
89 if (iter == expired_request_ids_and_reasons_.end()) { | |
90 NOTREACHED() << "Expired request not found in deleted results."; | |
91 continue; | |
92 } | |
93 | |
94 // Establish save page result based on the expiration reason. | |
95 RequestNotifier::BackgroundSavePageResult save_page_result( | |
96 ToBackgroundSavePageResult(iter->second)); | |
71 event_logger_->RecordDroppedSavePageRequest( | 97 event_logger_->RecordDroppedSavePageRequest( |
72 request.client_id().name_space, save_page_result, request.request_id()); | 98 request.client_id().name_space, save_page_result, request.request_id()); |
73 notifier_->NotifyCompleted(request, save_page_result); | 99 notifier_->NotifyCompleted(request, save_page_result); |
74 } | 100 } |
75 | 101 |
76 // The task is now done, return control to the task queue. | 102 // The task is now done, return control to the task queue. |
77 TaskComplete(); | 103 TaskComplete(); |
78 } | 104 } |
79 | 105 |
80 void CleanupTask::GetExpiredRequestIds( | 106 void CleanupTask::PopulateExpiredRequestIdsAndReasons( |
81 std::vector<std::unique_ptr<SavePageRequest>> requests, | 107 std::vector<std::unique_ptr<SavePageRequest>> requests) { |
82 std::vector<int64_t>* expired_request_ids) { | |
83 for (auto& request : requests) { | 108 for (auto& request : requests) { |
84 // Check for requests past their expiration time or with too many tries. If | 109 // 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 | 110 // it is not still valid, push the request and the reason onto the deletion |
86 // list. | 111 // list. |
87 OfflinerPolicyUtils::RequestExpirationStatus status = | 112 OfflinerPolicyUtils::RequestExpirationStatus status = |
88 OfflinerPolicyUtils::CheckRequestExpirationStatus(request.get(), | 113 OfflinerPolicyUtils::CheckRequestExpirationStatus(request.get(), |
89 policy_); | 114 policy_); |
90 | 115 |
91 // If we are not working on this request in an offliner, and it is not | 116 // If we are not working on this request in an offliner, and it is not |
92 // valid, put it on a list for removal. We make the exception for current | 117 // valid, put it on a list for removal. We make the exception for current |
93 // requests because the request might expire after being chosen and before | 118 // requests because the request might expire after being chosen and before |
94 // we call cleanup, and we shouldn't delete the request while offlining it. | 119 // we call cleanup, and we shouldn't delete the request while offlining it. |
95 if (status != OfflinerPolicyUtils::RequestExpirationStatus::VALID && | 120 if (status != OfflinerPolicyUtils::RequestExpirationStatus::VALID && |
96 request->request_state() != SavePageRequest::RequestState::OFFLINING) { | 121 request->request_state() != SavePageRequest::RequestState::OFFLINING) { |
97 // TODO(petewil): Push both request and reason, will need to change type | 122 expired_request_ids_and_reasons_.emplace(request->request_id(), status); |
98 // of list to pairs. | |
99 expired_request_ids->push_back(request->request_id()); | |
100 } | 123 } |
101 } | 124 } |
102 } | 125 } |
103 | 126 |
104 } // namespace offline_pages | 127 } // namespace offline_pages |
OLD | NEW |