OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/previews/previews_black_list_item.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "components/previews/previews_opt_out_store.h" |
| 10 |
| 11 namespace previews { |
| 12 |
| 13 PreviewsBlackListItem::PreviewsBlackListItem( |
| 14 size_t stored_history_length, |
| 15 int opt_out_black_list_threshold, |
| 16 base::TimeDelta black_list_duration) |
| 17 : stored_history_length_(stored_history_length), |
| 18 opt_out_black_list_threshold_(opt_out_black_list_threshold), |
| 19 black_list_duration_(black_list_duration), |
| 20 total_opt_out_(0) {} |
| 21 |
| 22 PreviewsBlackListItem::~PreviewsBlackListItem() {} |
| 23 |
| 24 void PreviewsBlackListItem::AddPreviewNavigation(bool opt_out, |
| 25 base::Time entry_time) { |
| 26 DCHECK_LE(opt_out_history_.size(), stored_history_length_); |
| 27 if (!most_recent_opt_out_time_ || |
| 28 entry_time > most_recent_opt_out_time_.value()) { |
| 29 most_recent_opt_out_time_ = entry_time; |
| 30 } |
| 31 total_opt_out_ += opt_out ? 1 : 0; |
| 32 |
| 33 // Find insert postion to keep the list sorted. Typically, this will be at the |
| 34 // end of the list, but for systems with clocks that are not non-decreasing |
| 35 // with time, this may not be the end. Linearly search from end to begin. |
| 36 auto iter = opt_out_history_.rbegin(); |
| 37 while (iter != opt_out_history_.rend() && iter->entry_time > entry_time) { |
| 38 iter++; |
| 39 } |
| 40 TimeOptOut time_opt_out; |
| 41 time_opt_out.entry_time = entry_time; |
| 42 time_opt_out.opt_out = opt_out; |
| 43 opt_out_history_.insert(iter.base(), time_opt_out); |
| 44 |
| 45 // Remove the oldest entry if the size exceeds the history size. |
| 46 if (opt_out_history_.size() > stored_history_length_) { |
| 47 total_opt_out_ -= opt_out_history_.front().opt_out ? 1 : 0; |
| 48 opt_out_history_.pop_front(); |
| 49 } |
| 50 } |
| 51 |
| 52 bool PreviewsBlackListItem::IsBlackListed(base::Time now) const { |
| 53 DCHECK_LE(opt_out_history_.size(), stored_history_length_); |
| 54 return most_recent_opt_out_time_ && |
| 55 now - most_recent_opt_out_time_.value() < black_list_duration_ && |
| 56 total_opt_out_ >= opt_out_black_list_threshold_; |
| 57 } |
| 58 |
| 59 } // namespace previews |
OLD | NEW |