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