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) { | |
Scott Hess - ex-Googler
2016/10/08 00:04:22
This is a bit involved, but I can't, offhand, thin
RyanSturm
2016/10/10 18:50:35
That is the same conclusion I came to. I don't thi
| |
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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
125 loaded_ = true; | 149 loaded_ = true; |
126 black_list_item_map_ = std::move(black_list_item_map); | 150 black_list_item_map_ = std::move(black_list_item_map); |
127 | 151 |
128 // Run all pending tasks. |loaded_| may change if ClearBlackList is queued. | 152 // Run all pending tasks. |loaded_| may change if ClearBlackList is queued. |
129 while (pending_callbacks_.size() > 0 && loaded_) { | 153 while (pending_callbacks_.size() > 0 && loaded_) { |
130 pending_callbacks_.front().Run(); | 154 pending_callbacks_.front().Run(); |
131 pending_callbacks_.pop(); | 155 pending_callbacks_.pop(); |
132 } | 156 } |
133 } | 157 } |
134 | 158 |
159 // static | |
135 PreviewsBlackListItem* PreviewsBlackList::GetBlackListItem( | 160 PreviewsBlackListItem* PreviewsBlackList::GetBlackListItem( |
136 const std::string& host_name) const { | 161 const BlackListItemMap& black_list_item_map, |
137 DCHECK(thread_checker_.CalledOnValidThread()); | 162 const std::string& host_name) { |
138 DCHECK(loaded_); | 163 BlackListItemMap::const_iterator iter = black_list_item_map.find(host_name); |
139 BlackListItemMap::iterator iter = black_list_item_map_->find(host_name); | 164 if (iter != black_list_item_map.end()) |
140 if (iter != black_list_item_map_->end()) | |
141 return iter->second.get(); | 165 return iter->second.get(); |
142 return nullptr; | 166 return nullptr; |
143 } | 167 } |
144 | 168 |
145 PreviewsBlackListItem* PreviewsBlackList::CreateBlackListItem( | 169 // static |
170 PreviewsBlackListItem* PreviewsBlackList::GetOrCreateBlackListItem( | |
171 BlackListItemMap* black_list_item_map, | |
146 const std::string& host_name) { | 172 const std::string& host_name) { |
147 DCHECK(thread_checker_.CalledOnValidThread()); | 173 PreviewsBlackListItem* black_list_item = |
148 DCHECK(loaded_); | 174 GetBlackListItem(*black_list_item_map, host_name); |
149 DCHECK(!GetBlackListItem(host_name)); | 175 if (black_list_item) |
150 if (black_list_item_map_->size() >= params::MaxInMemoryHostsInBlackList()) | 176 return black_list_item; |
151 EvictOldestOptOut(); | 177 if (black_list_item_map->size() >= params::MaxInMemoryHostsInBlackList()) |
152 DCHECK_LT(black_list_item_map_->size(), | 178 EvictOldestOptOut(black_list_item_map); |
153 params::MaxInMemoryHostsInBlackList()); | 179 DCHECK_LT(black_list_item_map->size(), params::MaxInMemoryHostsInBlackList()); |
154 PreviewsBlackListItem* black_list_item = new PreviewsBlackListItem( | 180 black_list_item = new PreviewsBlackListItem( |
155 params::MaxStoredHistoryLengthForBlackList(), | 181 params::MaxStoredHistoryLengthForBlackList(), |
156 params::BlackListOptOutThreshold(), params::BlackListDuration()); | 182 params::BlackListOptOutThreshold(), params::BlackListDuration()); |
157 black_list_item_map_->operator[](host_name) = | 183 black_list_item_map->operator[](host_name) = |
158 base::WrapUnique(black_list_item); | 184 base::WrapUnique(black_list_item); |
159 return black_list_item; | 185 return black_list_item; |
160 } | 186 } |
161 | 187 |
162 void PreviewsBlackList::EvictOldestOptOut() { | |
163 DCHECK(thread_checker_.CalledOnValidThread()); | |
164 DCHECK(loaded_); | |
165 // TODO(ryansturm): Add UMA. crbug.com/647717 | |
166 BlackListItemMap::iterator item_to_delete = black_list_item_map_->end(); | |
167 base::Time oldest_opt_out = clock_->Now(); | |
168 for (BlackListItemMap::iterator iter = black_list_item_map_->begin(); | |
169 iter != black_list_item_map_->end(); ++iter) { | |
170 if (!iter->second->most_recent_opt_out_time()) { | |
171 // If there is no opt out time, this is a good choice to evict. | |
172 item_to_delete = iter; | |
173 break; | |
174 } | |
175 if (iter->second->most_recent_opt_out_time().value() < oldest_opt_out) { | |
176 oldest_opt_out = iter->second->most_recent_opt_out_time().value(); | |
177 item_to_delete = iter; | |
178 } | |
179 } | |
180 black_list_item_map_->erase(item_to_delete); | |
181 } | |
182 | |
183 } // namespace previews | 188 } // namespace previews |
OLD | NEW |