Chromium Code Reviews| 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 | 16 |
| 17 PreviewsBlackListItem::OptOutRecord& PreviewsBlackListItem::OptOutRecord:: | |
| 18 operator=(const OptOutRecord&) = default; | |
| 19 | |
| 20 PreviewsBlackListItem::OptOutRecord::~OptOutRecord() {} | |
| 21 | |
| 17 PreviewsBlackListItem::PreviewsBlackListItem( | 22 PreviewsBlackListItem::PreviewsBlackListItem( |
| 18 size_t stored_history_length, | 23 size_t stored_history_length, |
| 19 int opt_out_black_list_threshold, | 24 int opt_out_black_list_threshold, |
| 20 base::TimeDelta black_list_duration) | 25 base::TimeDelta black_list_duration) |
| 21 : max_stored_history_length_(stored_history_length), | 26 : max_stored_history_length_(stored_history_length), |
| 22 opt_out_black_list_threshold_(opt_out_black_list_threshold), | 27 opt_out_black_list_threshold_(opt_out_black_list_threshold), |
| 23 max_black_list_duration_(black_list_duration), | 28 max_black_list_duration_(black_list_duration), |
| 24 total_opt_out_(0) {} | 29 total_opt_out_(0) {} |
| 25 | 30 |
| 26 PreviewsBlackListItem::~PreviewsBlackListItem() {} | 31 PreviewsBlackListItem::~PreviewsBlackListItem() {} |
| 27 | 32 |
| 28 void PreviewsBlackListItem::AddPreviewNavigation(bool opt_out, | 33 void PreviewsBlackListItem::AddPreviewNavigation(bool opt_out, |
| 29 base::Time entry_time) { | 34 base::Time entry_time) { |
| 30 DCHECK_LE(opt_out_records_.size(), max_stored_history_length_); | 35 DCHECK_LE(opt_out_records_.size(), max_stored_history_length_); |
| 36 // It is typically expected that entries will come through with entry time | |
| 37 // later or earlier than the existing entries. Therefore, compare front | |
| 38 // and back first. | |
| 39 if (opt_out_records_.size() == 0 || | |
|
tbansal1
2016/10/21 22:56:26
s/opt_out_records_.size() == 0/opt_out_records_.em
RyanSturm
2016/10/24 22:24:40
Done.
| |
| 40 entry_time < opt_out_records_.front().entry_time) { | |
| 41 if (opt_out_records_.size() == max_stored_history_length_) { | |
| 42 // Adding here would be pointless, as it will be removed afterward. | |
|
tbansal1
2016/10/21 22:56:26
This is a bit confusing. May be it is simpler to j
RyanSturm
2016/10/24 22:24:40
Done.
| |
| 43 return; | |
| 44 } | |
| 45 opt_out_records_.emplace_front(entry_time, opt_out); | |
| 46 } else if (entry_time >= opt_out_records_.back().entry_time) { | |
| 47 opt_out_records_.emplace_back(entry_time, opt_out); | |
| 48 } else { | |
| 49 // If the new time falls somewhere in between, binary search and linearly | |
| 50 // insert the new entry. | |
| 51 auto iter = std::lower_bound( | |
| 52 opt_out_records_.begin(), opt_out_records_.end(), entry_time, | |
| 53 [](const OptOutRecord& item, base::Time entry_time) { | |
| 54 // Prefer the new entry over the existing entry if this is a tie. | |
| 55 return item.entry_time <= entry_time; | |
| 56 }); | |
|
tbansal1
2016/10/21 22:56:26
Add a DCHECK that previous entry is smaller than t
RyanSturm
2016/10/24 22:24:40
Done.
| |
| 57 OptOutRecord opt_out_record(entry_time, opt_out); | |
| 58 opt_out_records_.insert(iter, opt_out_record); | |
| 59 } | |
| 60 | |
| 31 if (opt_out && (!most_recent_opt_out_time_ || | 61 if (opt_out && (!most_recent_opt_out_time_ || |
| 32 entry_time > most_recent_opt_out_time_.value())) { | 62 entry_time > most_recent_opt_out_time_.value())) { |
| 33 most_recent_opt_out_time_ = entry_time; | 63 most_recent_opt_out_time_ = entry_time; |
| 34 } | 64 } |
| 35 total_opt_out_ += opt_out ? 1 : 0; | 65 total_opt_out_ += opt_out ? 1 : 0; |
| 36 | 66 |
| 37 // Find insert postion to keep the list sorted. Typically, this will be at the | |
| 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. | 67 // Remove the oldest entry if the size exceeds the history size. |
| 46 if (opt_out_records_.size() > max_stored_history_length_) { | 68 if (opt_out_records_.size() > max_stored_history_length_) { |
| 47 total_opt_out_ -= opt_out_records_.front().opt_out ? 1 : 0; | 69 total_opt_out_ -= opt_out_records_.front().opt_out ? 1 : 0; |
| 48 opt_out_records_.pop_front(); | 70 opt_out_records_.pop_front(); |
| 49 } | 71 } |
| 50 DCHECK_LE(opt_out_records_.size(), max_stored_history_length_); | 72 DCHECK_LE(opt_out_records_.size(), max_stored_history_length_); |
| 51 } | 73 } |
| 52 | 74 |
| 53 bool PreviewsBlackListItem::IsBlackListed(base::Time now) const { | 75 bool PreviewsBlackListItem::IsBlackListed(base::Time now) const { |
| 54 DCHECK_LE(opt_out_records_.size(), max_stored_history_length_); | 76 DCHECK_LE(opt_out_records_.size(), max_stored_history_length_); |
| 55 return most_recent_opt_out_time_ && | 77 return most_recent_opt_out_time_ && |
| 56 now - most_recent_opt_out_time_.value() < max_black_list_duration_ && | 78 now - most_recent_opt_out_time_.value() < max_black_list_duration_ && |
| 57 total_opt_out_ >= opt_out_black_list_threshold_; | 79 total_opt_out_ >= opt_out_black_list_threshold_; |
| 58 } | 80 } |
| 59 | 81 |
| 60 } // namespace previews | 82 } // namespace previews |
| OLD | NEW |