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" |
(...skipping 19 matching lines...) Expand all Loading... | |
30 if (!oldest_opt_out || | 30 if (!oldest_opt_out || |
31 iter->second->most_recent_opt_out_time().value() < | 31 iter->second->most_recent_opt_out_time().value() < |
32 oldest_opt_out.value()) { | 32 oldest_opt_out.value()) { |
33 oldest_opt_out = iter->second->most_recent_opt_out_time().value(); | 33 oldest_opt_out = iter->second->most_recent_opt_out_time().value(); |
34 item_to_delete = iter; | 34 item_to_delete = iter; |
35 } | 35 } |
36 } | 36 } |
37 black_list_item_map->erase(item_to_delete); | 37 black_list_item_map->erase(item_to_delete); |
38 } | 38 } |
39 | 39 |
40 // Returns the PreviewsBlackListItem representing |host_name| in | |
41 // |black_list_item_map|. If there is no item for |host_name|, returns null. | |
42 PreviewsBlackListItem* GetBlackListItemFromMap( | |
43 const BlackListItemMap& black_list_item_map, | |
44 const std::string& host_name) { | |
45 BlackListItemMap::const_iterator iter = black_list_item_map.find(host_name); | |
46 if (iter != black_list_item_map.end()) | |
47 return iter->second.get(); | |
48 return nullptr; | |
49 } | |
50 | |
40 } // namespace | 51 } // namespace |
41 | 52 |
42 PreviewsBlackList::PreviewsBlackList( | 53 PreviewsBlackList::PreviewsBlackList( |
43 std::unique_ptr<PreviewsOptOutStore> opt_out_store, | 54 std::unique_ptr<PreviewsOptOutStore> opt_out_store, |
44 std::unique_ptr<base::Clock> clock) | 55 std::unique_ptr<base::Clock> clock) |
45 : loaded_(false), | 56 : loaded_(false), |
46 opt_out_store_(std::move(opt_out_store)), | 57 opt_out_store_(std::move(opt_out_store)), |
47 clock_(std::move(clock)), | 58 clock_(std::move(clock)), |
48 weak_factory_(this) { | 59 weak_factory_(this) { |
49 if (opt_out_store_) { | 60 if (opt_out_store_) { |
50 opt_out_store_->LoadBlackList(base::Bind( | 61 opt_out_store_->LoadBlackList(base::Bind( |
51 &PreviewsBlackList::LoadBlackListDone, weak_factory_.GetWeakPtr())); | 62 &PreviewsBlackList::LoadBlackListDone, weak_factory_.GetWeakPtr())); |
52 } else { | 63 } else { |
53 LoadBlackListDone(base::MakeUnique<BlackListItemMap>()); | 64 LoadBlackListDone(base::MakeUnique<BlackListItemMap>(), |
65 CreateHostIndifferentBlackListItem()); | |
54 } | 66 } |
55 } | 67 } |
56 | 68 |
57 PreviewsBlackList::~PreviewsBlackList() {} | 69 PreviewsBlackList::~PreviewsBlackList() {} |
58 | 70 |
59 void PreviewsBlackList::AddPreviewNavigation(const GURL& url, | 71 void PreviewsBlackList::AddPreviewNavigation(const GURL& url, |
60 bool opt_out, | 72 bool opt_out, |
61 PreviewsType type) { | 73 PreviewsType type) { |
62 DCHECK(thread_checker_.CalledOnValidThread()); | 74 DCHECK(thread_checker_.CalledOnValidThread()); |
63 DCHECK(url.has_host()); | 75 DCHECK(url.has_host()); |
(...skipping 10 matching lines...) Expand all Loading... | |
74 base::Unretained(this), url, opt_out, type)); | 86 base::Unretained(this), url, opt_out, type)); |
75 } | 87 } |
76 } | 88 } |
77 | 89 |
78 void PreviewsBlackList::AddPreviewNavigationSync(const GURL& url, | 90 void PreviewsBlackList::AddPreviewNavigationSync(const GURL& url, |
79 bool opt_out, | 91 bool opt_out, |
80 PreviewsType type) { | 92 PreviewsType type) { |
81 DCHECK(thread_checker_.CalledOnValidThread()); | 93 DCHECK(thread_checker_.CalledOnValidThread()); |
82 DCHECK(url.has_host()); | 94 DCHECK(url.has_host()); |
83 DCHECK(loaded_); | 95 DCHECK(loaded_); |
96 DCHECK(host_indifferent_black_list_item_); | |
97 DCHECK(black_list_item_map_); | |
84 std::string host_name = url.host(); | 98 std::string host_name = url.host(); |
85 base::Time now = clock_->Now(); | 99 base::Time now = clock_->Now(); |
86 PreviewsBlackListItem* item = | 100 PreviewsBlackListItem* item = |
87 GetOrCreateBlackListItem(black_list_item_map_.get(), host_name); | 101 GetOrCreateBlackListItemForMap(black_list_item_map_.get(), host_name); |
88 item->AddPreviewNavigation(opt_out, now); | 102 item->AddPreviewNavigation(opt_out, now); |
89 DCHECK_LE(black_list_item_map_->size(), | 103 DCHECK_LE(black_list_item_map_->size(), |
90 params::MaxInMemoryHostsInBlackList()); | 104 params::MaxInMemoryHostsInBlackList()); |
105 host_indifferent_black_list_item_->AddPreviewNavigation(opt_out, now); | |
91 if (!opt_out_store_) | 106 if (!opt_out_store_) |
92 return; | 107 return; |
93 opt_out_store_->AddPreviewNavigation(opt_out, host_name, type, now); | 108 opt_out_store_->AddPreviewNavigation(opt_out, host_name, type, now); |
94 } | 109 } |
95 | 110 |
96 bool PreviewsBlackList::IsLoadedAndAllowed(const GURL& url, | 111 bool PreviewsBlackList::IsLoadedAndAllowed(const GURL& url, |
97 PreviewsType type) const { | 112 PreviewsType type) const { |
98 DCHECK(thread_checker_.CalledOnValidThread()); | 113 DCHECK(thread_checker_.CalledOnValidThread()); |
99 DCHECK(url.has_host()); | 114 DCHECK(url.has_host()); |
100 if (!loaded_) | 115 if (!loaded_) |
101 return false; | 116 return false; |
117 DCHECK(black_list_item_map_); | |
102 if (last_opt_out_time_ && | 118 if (last_opt_out_time_ && |
103 clock_->Now() < | 119 clock_->Now() < |
104 last_opt_out_time_.value() + params::SingleOptOutDuration()) { | 120 last_opt_out_time_.value() + params::SingleOptOutDuration()) { |
105 return false; | 121 return false; |
106 } | 122 } |
123 if (host_indifferent_black_list_item_->IsBlackListed(clock_->Now())) | |
124 return false; | |
107 PreviewsBlackListItem* black_list_item = | 125 PreviewsBlackListItem* black_list_item = |
108 GetBlackListItem(*black_list_item_map_, url.host()); | 126 GetBlackListItemFromMap(*black_list_item_map_, url.host()); |
109 return !black_list_item || !black_list_item->IsBlackListed(clock_->Now()); | 127 return !black_list_item || !black_list_item->IsBlackListed(clock_->Now()); |
110 } | 128 } |
111 | 129 |
112 void PreviewsBlackList::ClearBlackList(base::Time begin_time, | 130 void PreviewsBlackList::ClearBlackList(base::Time begin_time, |
113 base::Time end_time) { | 131 base::Time end_time) { |
114 DCHECK(thread_checker_.CalledOnValidThread()); | 132 DCHECK(thread_checker_.CalledOnValidThread()); |
115 DCHECK_LE(begin_time, end_time); | 133 DCHECK_LE(begin_time, end_time); |
116 // If the |black_list_item_map_| has been loaded from |opt_out_store_|, | 134 // If the |black_list_item_map_| has been loaded from |opt_out_store_|, |
117 // synchronous operations will be accurate. Otherwise, queue the task to run | 135 // synchronous operations will be accurate. Otherwise, queue the task to run |
118 // asynchronously. | 136 // asynchronously. |
119 if (loaded_) { | 137 if (loaded_) { |
120 ClearBlackListSync(begin_time, end_time); | 138 ClearBlackListSync(begin_time, end_time); |
121 } else { | 139 } else { |
122 QueuePendingTask(base::Bind(&PreviewsBlackList::ClearBlackListSync, | 140 QueuePendingTask(base::Bind(&PreviewsBlackList::ClearBlackListSync, |
123 base::Unretained(this), begin_time, end_time)); | 141 base::Unretained(this), begin_time, end_time)); |
124 } | 142 } |
125 } | 143 } |
126 | 144 |
127 void PreviewsBlackList::ClearBlackListSync(base::Time begin_time, | 145 void PreviewsBlackList::ClearBlackListSync(base::Time begin_time, |
128 base::Time end_time) { | 146 base::Time end_time) { |
129 DCHECK(thread_checker_.CalledOnValidThread()); | 147 DCHECK(thread_checker_.CalledOnValidThread()); |
130 DCHECK(loaded_); | 148 DCHECK(loaded_); |
131 DCHECK_LE(begin_time, end_time); | 149 DCHECK_LE(begin_time, end_time); |
132 black_list_item_map_.reset(nullptr); | 150 black_list_item_map_.reset(); |
151 host_indifferent_black_list_item_.reset(); | |
133 loaded_ = false; | 152 loaded_ = false; |
134 // Delete relevant entries and reload the blacklist into memory. | 153 // Delete relevant entries and reload the blacklist into memory. |
135 if (opt_out_store_) { | 154 if (opt_out_store_) { |
136 opt_out_store_->ClearBlackList(begin_time, end_time); | 155 opt_out_store_->ClearBlackList(begin_time, end_time); |
137 opt_out_store_->LoadBlackList(base::Bind( | 156 opt_out_store_->LoadBlackList(base::Bind( |
138 &PreviewsBlackList::LoadBlackListDone, weak_factory_.GetWeakPtr())); | 157 &PreviewsBlackList::LoadBlackListDone, weak_factory_.GetWeakPtr())); |
139 } else { | 158 } else { |
140 LoadBlackListDone(base::MakeUnique<BlackListItemMap>()); | 159 LoadBlackListDone(base::MakeUnique<BlackListItemMap>(), |
160 CreateHostIndifferentBlackListItem()); | |
141 } | 161 } |
142 } | 162 } |
143 | 163 |
144 void PreviewsBlackList::QueuePendingTask(base::Closure callback) { | 164 void PreviewsBlackList::QueuePendingTask(base::Closure callback) { |
145 DCHECK(thread_checker_.CalledOnValidThread()); | 165 DCHECK(thread_checker_.CalledOnValidThread()); |
146 DCHECK(!loaded_); | 166 DCHECK(!loaded_); |
147 DCHECK(!callback.is_null()); | 167 DCHECK(!callback.is_null()); |
148 pending_callbacks_.emplace(callback); | 168 pending_callbacks_.emplace(callback); |
149 } | 169 } |
150 | 170 |
151 void PreviewsBlackList::LoadBlackListDone( | 171 void PreviewsBlackList::LoadBlackListDone( |
152 std::unique_ptr<BlackListItemMap> black_list_item_map) { | 172 std::unique_ptr<BlackListItemMap> black_list_item_map, |
173 std::unique_ptr<PreviewsBlackListItem> host_indifferent_black_list_item) { | |
153 DCHECK(thread_checker_.CalledOnValidThread()); | 174 DCHECK(thread_checker_.CalledOnValidThread()); |
175 DCHECK(host_indifferent_black_list_item); | |
tbansal1
2016/10/28 21:59:33
super nit:put DCHECK in the same order as function
RyanSturm
2016/11/02 19:52:30
Done.
| |
176 DCHECK(black_list_item_map); | |
154 DCHECK_LE(black_list_item_map->size(), params::MaxInMemoryHostsInBlackList()); | 177 DCHECK_LE(black_list_item_map->size(), params::MaxInMemoryHostsInBlackList()); |
155 loaded_ = true; | 178 loaded_ = true; |
156 black_list_item_map_ = std::move(black_list_item_map); | 179 black_list_item_map_ = std::move(black_list_item_map); |
180 host_indifferent_black_list_item_ = | |
181 std::move(host_indifferent_black_list_item); | |
157 | 182 |
158 // Run all pending tasks. |loaded_| may change if ClearBlackList is queued. | 183 // Run all pending tasks. |loaded_| may change if ClearBlackList is queued. |
159 while (pending_callbacks_.size() > 0 && loaded_) { | 184 while (pending_callbacks_.size() > 0 && loaded_) { |
160 pending_callbacks_.front().Run(); | 185 pending_callbacks_.front().Run(); |
161 pending_callbacks_.pop(); | 186 pending_callbacks_.pop(); |
162 } | 187 } |
163 } | 188 } |
164 | 189 |
165 // static | 190 // static |
166 PreviewsBlackListItem* PreviewsBlackList::GetBlackListItem( | 191 PreviewsBlackListItem* PreviewsBlackList::GetOrCreateBlackListItemForMap( |
167 const BlackListItemMap& black_list_item_map, | |
168 const std::string& host_name) { | |
169 BlackListItemMap::const_iterator iter = black_list_item_map.find(host_name); | |
170 if (iter != black_list_item_map.end()) | |
171 return iter->second.get(); | |
172 return nullptr; | |
173 } | |
174 | |
175 // static | |
176 PreviewsBlackListItem* PreviewsBlackList::GetOrCreateBlackListItem( | |
177 BlackListItemMap* black_list_item_map, | 192 BlackListItemMap* black_list_item_map, |
178 const std::string& host_name) { | 193 const std::string& host_name) { |
179 PreviewsBlackListItem* black_list_item = | 194 PreviewsBlackListItem* black_list_item = |
180 GetBlackListItem(*black_list_item_map, host_name); | 195 GetBlackListItemFromMap(*black_list_item_map, host_name); |
181 if (black_list_item) | 196 if (black_list_item) |
182 return black_list_item; | 197 return black_list_item; |
183 if (black_list_item_map->size() >= params::MaxInMemoryHostsInBlackList()) | 198 if (black_list_item_map->size() >= params::MaxInMemoryHostsInBlackList()) |
184 EvictOldestOptOut(black_list_item_map); | 199 EvictOldestOptOut(black_list_item_map); |
185 DCHECK_LT(black_list_item_map->size(), params::MaxInMemoryHostsInBlackList()); | 200 DCHECK_LT(black_list_item_map->size(), params::MaxInMemoryHostsInBlackList()); |
186 black_list_item = new PreviewsBlackListItem( | 201 black_list_item = new PreviewsBlackListItem( |
187 params::MaxStoredHistoryLengthForBlackList(), | 202 params::MaxStoredHistoryLengthForPerHostBlackList(), |
188 params::BlackListOptOutThreshold(), params::BlackListDuration()); | 203 params::PerHostBlackListOptOutThreshold(), |
204 params::PerHostBlackListDuration()); | |
189 black_list_item_map->operator[](host_name) = | 205 black_list_item_map->operator[](host_name) = |
190 base::WrapUnique(black_list_item); | 206 base::WrapUnique(black_list_item); |
191 return black_list_item; | 207 return black_list_item; |
192 } | 208 } |
193 | 209 |
210 // static | |
211 std::unique_ptr<PreviewsBlackListItem> | |
212 PreviewsBlackList::CreateHostIndifferentBlackListItem() { | |
213 return base::MakeUnique<PreviewsBlackListItem>( | |
214 params::MaxStoredHistoryLengthForHostIndifferentBlackList(), | |
215 params::HostIndifferentBlackListOptOutThreshold(), | |
216 params::HostIndifferentBlackListPerHostDuration()); | |
217 } | |
218 | |
194 } // namespace previews | 219 } // namespace previews |
OLD | NEW |