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/core/previews_black_list.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/optional.h" |
| 10 #include "base/time/clock.h" |
| 11 #include "base/time/time.h" |
| 12 #include "components/previews/core/previews_black_list_item.h" |
| 13 #include "components/previews/core/previews_experiments.h" |
| 14 #include "url/gurl.h" |
| 15 |
| 16 namespace previews { |
| 17 |
| 18 PreviewsBlackList::PreviewsBlackList( |
| 19 std::unique_ptr<PreviewsOptOutStore> opt_out_store, |
| 20 std::unique_ptr<base::Clock> clock) |
| 21 : loaded_(false), |
| 22 opt_out_store_(std::move(opt_out_store)), |
| 23 clock_(std::move(clock)), |
| 24 weak_factory_(this) { |
| 25 if (opt_out_store_) { |
| 26 opt_out_store_->LoadBlackList(base::Bind( |
| 27 &PreviewsBlackList::LoadBlackListDone, weak_factory_.GetWeakPtr())); |
| 28 } else { |
| 29 LoadBlackListDone(base::MakeUnique<BlackListItemMap>()); |
| 30 } |
| 31 } |
| 32 |
| 33 PreviewsBlackList::~PreviewsBlackList() {} |
| 34 |
| 35 void PreviewsBlackList::AddPreviewNavigation(const GURL& url, |
| 36 bool opt_out, |
| 37 PreviewsType type) { |
| 38 DCHECK(thread_checker_.CalledOnValidThread()); |
| 39 DCHECK(url.has_host()); |
| 40 // If the |black_list_item_map_| has been loaded from |opt_out_store_|, |
| 41 // synchronous operations will be accurate. Otherwise, queue the task to run |
| 42 // asynchronously. |
| 43 if (loaded_) { |
| 44 AddPreviewNavigationSync(url, opt_out, type); |
| 45 } else { |
| 46 QueuePendingTask(base::Bind(&PreviewsBlackList::AddPreviewNavigationSync, |
| 47 base::Unretained(this), url, opt_out, type)); |
| 48 } |
| 49 } |
| 50 |
| 51 void PreviewsBlackList::AddPreviewNavigationSync(const GURL& url, |
| 52 bool opt_out, |
| 53 PreviewsType type) { |
| 54 DCHECK(thread_checker_.CalledOnValidThread()); |
| 55 DCHECK(url.has_host()); |
| 56 DCHECK(loaded_); |
| 57 std::string host_name = url.host(); |
| 58 base::Time now = clock_->Now(); |
| 59 PreviewsBlackListItem* item = GetBlackListItem(host_name); |
| 60 if (!item) { |
| 61 item = CreateBlackListItem(host_name); |
| 62 } |
| 63 item->AddPreviewNavigation(opt_out, now); |
| 64 DCHECK_LE(black_list_item_map_->size(), |
| 65 params::MaxInMemoryHostsInBlackList()); |
| 66 if (!opt_out_store_) |
| 67 return; |
| 68 opt_out_store_->AddPreviewNavigation(opt_out, host_name, type, now); |
| 69 } |
| 70 |
| 71 bool PreviewsBlackList::IsLoadedAndAllowed(const GURL& url, |
| 72 PreviewsType type) const { |
| 73 DCHECK(thread_checker_.CalledOnValidThread()); |
| 74 DCHECK(url.has_host()); |
| 75 if (!loaded_) |
| 76 return false; |
| 77 PreviewsBlackListItem* black_list_item = GetBlackListItem(url.host()); |
| 78 return !black_list_item || !black_list_item->IsBlackListed(clock_->Now()); |
| 79 } |
| 80 |
| 81 void PreviewsBlackList::QueuePendingTask(base::Closure callback) { |
| 82 DCHECK(thread_checker_.CalledOnValidThread()); |
| 83 DCHECK(!loaded_); |
| 84 DCHECK(!callback.is_null()); |
| 85 pending_callbacks_.emplace(callback); |
| 86 } |
| 87 |
| 88 void PreviewsBlackList::LoadBlackListDone( |
| 89 std::unique_ptr<BlackListItemMap> black_list_item_map) { |
| 90 DCHECK(thread_checker_.CalledOnValidThread()); |
| 91 DCHECK_LE(black_list_item_map->size(), params::MaxInMemoryHostsInBlackList()); |
| 92 loaded_ = true; |
| 93 black_list_item_map_ = std::move(black_list_item_map); |
| 94 |
| 95 // Run all pending tasks. |loaded_| may change if ClearBlackList is queued. |
| 96 while (pending_callbacks_.size() > 0 && loaded_) { |
| 97 pending_callbacks_.front().Run(); |
| 98 pending_callbacks_.pop(); |
| 99 } |
| 100 } |
| 101 |
| 102 PreviewsBlackListItem* PreviewsBlackList::GetBlackListItem( |
| 103 const std::string& host_name) const { |
| 104 DCHECK(thread_checker_.CalledOnValidThread()); |
| 105 DCHECK(loaded_); |
| 106 BlackListItemMap::iterator iter = black_list_item_map_->find(host_name); |
| 107 if (iter != black_list_item_map_->end()) |
| 108 return iter->second.get(); |
| 109 return nullptr; |
| 110 } |
| 111 |
| 112 PreviewsBlackListItem* PreviewsBlackList::CreateBlackListItem( |
| 113 const std::string& host_name) { |
| 114 DCHECK(thread_checker_.CalledOnValidThread()); |
| 115 DCHECK(loaded_); |
| 116 DCHECK(!GetBlackListItem(host_name)); |
| 117 if (black_list_item_map_->size() >= params::MaxInMemoryHostsInBlackList()) |
| 118 EvictOldestOptOut(); |
| 119 DCHECK_LT(black_list_item_map_->size(), |
| 120 params::MaxInMemoryHostsInBlackList()); |
| 121 PreviewsBlackListItem* black_list_item = new PreviewsBlackListItem( |
| 122 params::MaxStoredHistoryLengthForBlackList(), |
| 123 params::BlackListOptOutThreshold(), params::BlackListDuration()); |
| 124 black_list_item_map_->operator[](host_name) = |
| 125 base::WrapUnique(black_list_item); |
| 126 return black_list_item; |
| 127 } |
| 128 |
| 129 void PreviewsBlackList::EvictOldestOptOut() { |
| 130 DCHECK(thread_checker_.CalledOnValidThread()); |
| 131 DCHECK(loaded_); |
| 132 // TODO(ryansturm): Add UMA. crbug.com/647717 |
| 133 BlackListItemMap::iterator item_to_delete = black_list_item_map_->end(); |
| 134 base::Time oldest_opt_out = clock_->Now(); |
| 135 for (BlackListItemMap::iterator iter = black_list_item_map_->begin(); |
| 136 iter != black_list_item_map_->end(); ++iter) { |
| 137 if (!iter->second->most_recent_opt_out_time()) { |
| 138 // If there is no opt out time, this is a good choice to evict. |
| 139 item_to_delete = iter; |
| 140 break; |
| 141 } |
| 142 if (iter->second->most_recent_opt_out_time().value() < oldest_opt_out) { |
| 143 oldest_opt_out = iter->second->most_recent_opt_out_time().value(); |
| 144 item_to_delete = iter; |
| 145 } |
| 146 } |
| 147 black_list_item_map_->erase(item_to_delete); |
| 148 } |
| 149 |
| 150 } // namespace previews |
OLD | NEW |