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/previews/core/previews_black_list_item.h" | 5 #include "components/previews/core/previews_black_list_item.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <tuple> |
8 | 9 |
9 #include "components/previews/core/previews_opt_out_store.h" | 10 #include "components/previews/core/previews_opt_out_store.h" |
10 | 11 |
11 namespace previews { | 12 namespace previews { |
12 | 13 |
13 PreviewsBlackListItem::OptOutRecord::OptOutRecord(base::Time entry_time, | 14 PreviewsBlackListItem::OptOutRecord::OptOutRecord(base::Time entry_time, |
14 bool opt_out) | 15 bool opt_out) |
15 : entry_time(entry_time), opt_out(opt_out) {} | 16 : entry_time_(entry_time), opt_out_(opt_out) {} |
| 17 |
| 18 PreviewsBlackListItem::OptOutRecord::~OptOutRecord() {} |
| 19 |
| 20 PreviewsBlackListItem::OptOutRecord::OptOutRecord(OptOutRecord&&) = default; |
| 21 |
| 22 PreviewsBlackListItem::OptOutRecord& PreviewsBlackListItem::OptOutRecord:: |
| 23 operator=(OptOutRecord&&) = default; |
| 24 |
| 25 bool PreviewsBlackListItem::OptOutRecord::operator<( |
| 26 const OptOutRecord& other) const { |
| 27 // Fresher entries are lower priority to evict, as are non-opt-outs. |
| 28 return std::tie(entry_time_, opt_out_) > |
| 29 std::tie(other.entry_time_, other.opt_out_); |
| 30 } |
16 | 31 |
17 PreviewsBlackListItem::PreviewsBlackListItem( | 32 PreviewsBlackListItem::PreviewsBlackListItem( |
18 size_t stored_history_length, | 33 size_t stored_history_length, |
19 int opt_out_black_list_threshold, | 34 int opt_out_black_list_threshold, |
20 base::TimeDelta black_list_duration) | 35 base::TimeDelta black_list_duration) |
21 : max_stored_history_length_(stored_history_length), | 36 : max_stored_history_length_(stored_history_length), |
22 opt_out_black_list_threshold_(opt_out_black_list_threshold), | 37 opt_out_black_list_threshold_(opt_out_black_list_threshold), |
23 max_black_list_duration_(black_list_duration), | 38 max_black_list_duration_(black_list_duration), |
24 total_opt_out_(0) {} | 39 total_opt_out_(0) {} |
25 | 40 |
26 PreviewsBlackListItem::~PreviewsBlackListItem() {} | 41 PreviewsBlackListItem::~PreviewsBlackListItem() {} |
27 | 42 |
28 void PreviewsBlackListItem::AddPreviewNavigation(bool opt_out, | 43 void PreviewsBlackListItem::AddPreviewNavigation(bool opt_out, |
29 base::Time entry_time) { | 44 base::Time entry_time) { |
30 DCHECK_LE(opt_out_records_.size(), max_stored_history_length_); | 45 DCHECK_LE(opt_out_records_.size(), max_stored_history_length_); |
| 46 |
| 47 opt_out_records_.emplace(entry_time, opt_out); |
| 48 |
31 if (opt_out && (!most_recent_opt_out_time_ || | 49 if (opt_out && (!most_recent_opt_out_time_ || |
32 entry_time > most_recent_opt_out_time_.value())) { | 50 entry_time > most_recent_opt_out_time_.value())) { |
33 most_recent_opt_out_time_ = entry_time; | 51 most_recent_opt_out_time_ = entry_time; |
34 } | 52 } |
35 total_opt_out_ += opt_out ? 1 : 0; | 53 total_opt_out_ += opt_out ? 1 : 0; |
36 | 54 |
37 // Find insert postion to keep the list sorted. Typically, this will be at the | 55 // Remove the oldest entry if the size exceeds the max history size. |
38 // end of the list, but for systems with clocks that are not non-decreasing | |
39 // with time, this may not be the end. Linearly search from end to begin. | |
40 auto iter = opt_out_records_.rbegin(); | |
41 while (iter != opt_out_records_.rend() && iter->entry_time > entry_time) | |
42 iter++; | |
43 opt_out_records_.emplace(iter.base(), entry_time, opt_out); | |
44 | |
45 // Remove the oldest entry if the size exceeds the history size. | |
46 if (opt_out_records_.size() > max_stored_history_length_) { | 56 if (opt_out_records_.size() > max_stored_history_length_) { |
47 total_opt_out_ -= opt_out_records_.front().opt_out ? 1 : 0; | 57 DCHECK_EQ(opt_out_records_.size(), max_stored_history_length_ + 1); |
48 opt_out_records_.pop_front(); | 58 DCHECK_LE(opt_out_records_.top().entry_time(), entry_time); |
| 59 total_opt_out_ -= opt_out_records_.top().opt_out() ? 1 : 0; |
| 60 opt_out_records_.pop(); |
49 } | 61 } |
50 DCHECK_LE(opt_out_records_.size(), max_stored_history_length_); | 62 DCHECK_LE(opt_out_records_.size(), max_stored_history_length_); |
51 } | 63 } |
52 | 64 |
53 bool PreviewsBlackListItem::IsBlackListed(base::Time now) const { | 65 bool PreviewsBlackListItem::IsBlackListed(base::Time now) const { |
54 DCHECK_LE(opt_out_records_.size(), max_stored_history_length_); | 66 DCHECK_LE(opt_out_records_.size(), max_stored_history_length_); |
55 return most_recent_opt_out_time_ && | 67 return most_recent_opt_out_time_ && |
56 now - most_recent_opt_out_time_.value() < max_black_list_duration_ && | 68 now - most_recent_opt_out_time_.value() < max_black_list_duration_ && |
57 total_opt_out_ >= opt_out_black_list_threshold_; | 69 total_opt_out_ >= opt_out_black_list_threshold_; |
58 } | 70 } |
59 | 71 |
60 size_t PreviewsBlackListItem::OptOutRecordsSizeForTesting() const { | 72 size_t PreviewsBlackListItem::OptOutRecordsSizeForTesting() const { |
61 return opt_out_records_.size(); | 73 return opt_out_records_.size(); |
62 } | 74 } |
63 | 75 |
64 } // namespace previews | 76 } // namespace previews |
OLD | NEW |