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