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 | 8 |
9 #include "components/previews/core/previews_opt_out_store.h" | 9 #include "components/previews/core/previews_opt_out_store.h" |
10 | 10 |
11 namespace previews { | 11 namespace previews { |
12 | 12 |
13 PreviewsBlackListItem::OptOutRecord::OptOutRecord(base::Time entry_time, | 13 PreviewsBlackListItem::OptOutRecord::OptOutRecord(base::Time entry_time, |
14 bool opt_out) | 14 bool opt_out) |
15 : entry_time(entry_time), opt_out(opt_out) {} | 15 : entry_time_(entry_time), opt_out_(opt_out) {} |
16 | |
17 PreviewsBlackListItem::OptOutRecord::~OptOutRecord() {} | |
18 | |
19 PreviewsBlackListItem::OptOutRecord::OptOutRecord(OptOutRecord&&) = default; | |
20 | |
21 PreviewsBlackListItem::OptOutRecord& PreviewsBlackListItem::OptOutRecord:: | |
22 operator=(OptOutRecord&&) = default; | |
23 | |
24 bool PreviewsBlackListItem::OptOutRecord::operator<( | |
25 const OptOutRecord& other) const { | |
26 // Fresher entries are lower priority to evict, as are non-opt-outs. | |
27 return (entry_time_ == other.entry_time_) ? opt_out_ > other.opt_out_ | |
tbansal1
2016/10/25 16:15:47
Why not use std::tie?
That's slightly more readabl
RyanSturm
2016/10/25 18:15:44
couldn't remember what tie was called. Thanks.
| |
28 : entry_time_ > other.entry_time_; | |
29 } | |
16 | 30 |
17 PreviewsBlackListItem::PreviewsBlackListItem( | 31 PreviewsBlackListItem::PreviewsBlackListItem( |
18 size_t stored_history_length, | 32 size_t stored_history_length, |
19 int opt_out_black_list_threshold, | 33 int opt_out_black_list_threshold, |
20 base::TimeDelta black_list_duration) | 34 base::TimeDelta black_list_duration) |
21 : max_stored_history_length_(stored_history_length), | 35 : max_stored_history_length_(stored_history_length), |
22 opt_out_black_list_threshold_(opt_out_black_list_threshold), | 36 opt_out_black_list_threshold_(opt_out_black_list_threshold), |
23 max_black_list_duration_(black_list_duration), | 37 max_black_list_duration_(black_list_duration), |
24 total_opt_out_(0) {} | 38 total_opt_out_(0) {} |
25 | 39 |
26 PreviewsBlackListItem::~PreviewsBlackListItem() {} | 40 PreviewsBlackListItem::~PreviewsBlackListItem() {} |
27 | 41 |
28 void PreviewsBlackListItem::AddPreviewNavigation(bool opt_out, | 42 void PreviewsBlackListItem::AddPreviewNavigation(bool opt_out, |
29 base::Time entry_time) { | 43 base::Time entry_time) { |
30 DCHECK_LE(opt_out_records_.size(), max_stored_history_length_); | 44 DCHECK_LE(opt_out_records_.size(), max_stored_history_length_); |
45 | |
46 opt_out_records_.emplace(entry_time, opt_out); | |
47 | |
31 if (opt_out && (!most_recent_opt_out_time_ || | 48 if (opt_out && (!most_recent_opt_out_time_ || |
32 entry_time > most_recent_opt_out_time_.value())) { | 49 entry_time > most_recent_opt_out_time_.value())) { |
33 most_recent_opt_out_time_ = entry_time; | 50 most_recent_opt_out_time_ = entry_time; |
34 } | 51 } |
35 total_opt_out_ += opt_out ? 1 : 0; | 52 total_opt_out_ += opt_out ? 1 : 0; |
36 | 53 |
37 // Find insert postion to keep the list sorted. Typically, this will be at the | 54 // 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_) { | 55 if (opt_out_records_.size() > max_stored_history_length_) { |
47 total_opt_out_ -= opt_out_records_.front().opt_out ? 1 : 0; | 56 DCHECK(opt_out_records_.top().entry_time() <= entry_time); |
tbansal1
2016/10/25 16:15:47
DCHECK_LE
tbansal1
2016/10/25 16:15:47
Add DCHECK_EQ(opt_out_records_.size(), max_stored_
RyanSturm
2016/10/25 18:15:44
Done.
RyanSturm
2016/10/25 18:15:44
Done.
| |
48 opt_out_records_.pop_front(); | 57 total_opt_out_ -= opt_out_records_.top().opt_out() ? 1 : 0; |
58 opt_out_records_.pop(); | |
49 } | 59 } |
50 DCHECK_LE(opt_out_records_.size(), max_stored_history_length_); | 60 DCHECK_LE(opt_out_records_.size(), max_stored_history_length_); |
51 } | 61 } |
52 | 62 |
53 bool PreviewsBlackListItem::IsBlackListed(base::Time now) const { | 63 bool PreviewsBlackListItem::IsBlackListed(base::Time now) const { |
54 DCHECK_LE(opt_out_records_.size(), max_stored_history_length_); | 64 DCHECK_LE(opt_out_records_.size(), max_stored_history_length_); |
55 return most_recent_opt_out_time_ && | 65 return most_recent_opt_out_time_ && |
56 now - most_recent_opt_out_time_.value() < max_black_list_duration_ && | 66 now - most_recent_opt_out_time_.value() < max_black_list_duration_ && |
57 total_opt_out_ >= opt_out_black_list_threshold_; | 67 total_opt_out_ >= opt_out_black_list_threshold_; |
58 } | 68 } |
59 | 69 |
60 } // namespace previews | 70 } // namespace previews |
OLD | NEW |