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