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