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())); | |
tbansal1
2016/09/19 21:26:07
Since opt_out_store_ is owned by |this|, is it pos
RyanSturm
2016/09/19 22:37:45
I don't think we should limit the OptOutStore impl
| |
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 if (!opt_out_store_) | |
60 return; | |
61 opt_out_store_->AddPreviewNavigation(opt_out, host_name, type, now); | |
tbansal1
2016/09/19 21:26:08
DCHECK_LE(black_list_item_map_->size(), params::Ma
RyanSturm
2016/09/19 22:37:45
Done.
| |
62 } | |
63 | |
64 bool PreviewsBlackList::IsLoadedAndAllowed(const GURL& url, PreviewsType type) { | |
65 DCHECK(thread_checker_.CalledOnValidThread()); | |
66 DCHECK(url.has_host()); | |
67 std::string host_name = url.host(); | |
68 if (!loaded_) | |
69 return false; | |
70 PreviewsBlackListItem* black_list_item = GetBlackListItem(host_name, false); | |
71 return !black_list_item || !black_list_item->IsBlackListed(clock_->Now()); | |
72 } | |
73 | |
74 void PreviewsBlackList::QueuePendingTask(QueueClosure callback) { | |
75 DCHECK(thread_checker_.CalledOnValidThread()); | |
76 DCHECK(!callback.is_null()); | |
tbansal1
2016/09/19 21:26:07
DCHECK(!loaded_);
RyanSturm
2016/09/19 22:37:45
Done.
| |
77 pending_callbacks_.emplace(callback); | |
78 } | |
79 | |
80 void PreviewsBlackList::LoadBlackListDone( | |
81 std::unique_ptr<BlackListItemMap> black_list_item_map) { | |
82 DCHECK(thread_checker_.CalledOnValidThread()); | |
83 loaded_ = true; | |
84 black_list_item_map_ = std::move(black_list_item_map); | |
85 | |
86 // Run all pending tasks. |loaded_| may change if ClearBlackListIs queued. | |
tbansal1
2016/09/19 21:26:07
s/ClearBlackListIs/ClearBlackList is/
RyanSturm
2016/09/19 22:37:45
Done.
| |
87 while (pending_callbacks_.size() > 0 && loaded_) { | |
88 pending_callbacks_.front().Run(); | |
89 pending_callbacks_.pop(); | |
90 } | |
91 } | |
92 | |
93 PreviewsBlackListItem* PreviewsBlackList::GetBlackListItem( | |
94 const std::string& host_name, | |
95 bool create_if_needed) { | |
96 DCHECK(thread_checker_.CalledOnValidThread()); | |
97 DCHECK(loaded_); | |
98 BlackListItemMap::iterator iter = black_list_item_map_->find(host_name); | |
99 if (iter != black_list_item_map_->end()) { | |
100 return iter->second.get(); | |
101 } | |
102 if (!create_if_needed) | |
103 return nullptr; | |
104 if (black_list_item_map_->size() >= params::MaxInMemoryHostsInBlackList()) { | |
105 EvictOldestOptOut(); | |
106 } | |
tbansal1
2016/09/19 21:26:08
DCHECK_LE(black_list_item_map_->size(), params::Ma
RyanSturm
2016/09/19 22:37:45
Done.
| |
107 // Create the item if it doesn't exist yet. | |
108 PreviewsBlackListItem* black_list_item = new PreviewsBlackListItem( | |
109 params::MaxStoredHistoryLengthForBlackList(), | |
110 params::BlackListOptOutThreshold(), params::BlackListDuration()); | |
111 black_list_item_map_->operator[](host_name) = | |
112 base::WrapUnique(black_list_item); | |
113 return black_list_item; | |
114 } | |
115 | |
116 void PreviewsBlackList::EvictOldestOptOut() { | |
tbansal1
2016/09/19 21:26:07
I think the eviction should happen on the disk als
RyanSturm
2016/09/19 22:37:45
I'm planning to limit the SQL implementation to th
| |
117 DCHECK(thread_checker_.CalledOnValidThread()); | |
118 DCHECK(loaded_); | |
119 // TODO(ryansturm): Add UMA. crbug.com/647717 | |
120 BlackListItemMap::iterator item_to_delete = black_list_item_map_->end(); | |
121 base::Time oldest_opt_out = clock_->Now(); | |
122 for (BlackListItemMap::iterator iter = black_list_item_map_->begin(); | |
123 iter != black_list_item_map_->end(); ++iter) { | |
124 if (!iter->second->most_recent_opt_out_time()) { | |
125 // If there is no opt out time, this is a good choice to evict. | |
126 item_to_delete = iter; | |
127 break; | |
128 } | |
129 if (iter->second->most_recent_opt_out_time().value() < oldest_opt_out) { | |
130 oldest_opt_out = iter->second->most_recent_opt_out_time().value(); | |
131 item_to_delete = iter; | |
132 } | |
133 } | |
134 black_list_item_map_->erase(item_to_delete); | |
135 } | |
136 | |
137 } // namespace previews | |
OLD | NEW |