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/previews_black_list.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/memory/ptr_util.h" | |
9 #include "base/time/time.h" | |
10 #include "components/previews/previews_black_list_item.h" | |
11 #include "components/previews/previews_experiments.h" | |
12 | |
13 namespace previews { | |
14 | |
15 PreviewsBlackList::PreviewsBlackList( | |
16 std::unique_ptr<PreviewsOptOutStore> opt_out_store) | |
17 : loaded_(false), | |
18 opt_out_store_(std::move(opt_out_store)), | |
19 weak_factory_(this) { | |
20 if (opt_out_store_) { | |
21 opt_out_store_->LoadBlackList(base::Bind( | |
22 &PreviewsBlackList::LoadBlackListDone, weak_factory_.GetWeakPtr())); | |
23 } else { | |
24 LoadBlackListDone(base::MakeUnique<BlackListItemMap>()); | |
25 } | |
26 } | |
27 | |
28 PreviewsBlackList::~PreviewsBlackList() {} | |
29 | |
30 void PreviewsBlackList::AddPreviewNavigation(const std::string& host_name, | |
31 bool opt_out, | |
32 PreviewsType type) { | |
33 DCHECK(!host_name.empty()); | |
34 DCHECK(thread_checker_.CalledOnValidThread()); | |
35 // If the |black_list_item_map_| has been loaded from |opt_out_store_|, | |
36 // synchronous operations will be accurate. Otherwise, queue the task to run | |
37 // asynchronously. | |
38 if (loaded_) { | |
39 AddPreviewNavigationSync(host_name, opt_out, type); | |
40 } else { | |
41 QueuePendingTask(base::Bind(&PreviewsBlackList::AddPreviewNavigationSync, | |
42 weak_factory_.GetWeakPtr(), host_name, opt_out, | |
43 type)); | |
44 } | |
45 } | |
46 | |
47 void PreviewsBlackList::AddPreviewNavigationSync(const std::string& host_name, | |
48 bool opt_out, | |
49 PreviewsType type) { | |
50 DCHECK(!host_name.empty()); | |
51 DCHECK(thread_checker_.CalledOnValidThread()); | |
52 base::Time now = base::Time::Now(); | |
53 GetBlackListItem(host_name, true)->AddPreviewNavigation(opt_out, now); | |
54 if (!opt_out_store_) | |
55 return; | |
56 opt_out_store_->AddPreviewNavigation(opt_out, host_name, type, now); | |
57 } | |
58 | |
59 bool PreviewsBlackList::IsLoadedAndAllowed(const std::string& host_name) { | |
60 DCHECK(!host_name.empty()); | |
61 DCHECK(thread_checker_.CalledOnValidThread()); | |
62 if (!loaded_ || !previews::params::BlackListParamsAreValid()) { | |
63 return false; | |
64 } | |
65 PreviewsBlackListItem* black_list_item = GetBlackListItem(host_name, false); | |
66 return !black_list_item || !black_list_item->IsBlackListed(base::Time::Now()); | |
67 } | |
68 | |
69 void PreviewsBlackList::ClearBlackList(const base::Time& begin_time, | |
tbansal1
2016/09/13 23:52:45
Can this be done in a separate CL?
RyanSturm
2016/09/14 16:44:29
Done.
| |
70 const base::Time& end_time) { | |
71 DCHECK(thread_checker_.CalledOnValidThread()); | |
72 // If the |black_list_item_map_| has been loaded from |opt_out_store_|, | |
73 // synchronous operations will be accurate. Otherwise, queue the task to run | |
74 // asynchronously. | |
75 if (loaded_) { | |
76 ClearBlackListSync(begin_time, end_time); | |
77 } else { | |
78 QueuePendingTask(base::Bind(&PreviewsBlackList::ClearBlackListSync, | |
79 weak_factory_.GetWeakPtr(), begin_time, | |
80 end_time)); | |
81 } | |
82 } | |
83 | |
84 void PreviewsBlackList::ClearBlackListSync(const base::Time& begin_time, | |
85 const base::Time& end_time) { | |
86 DCHECK(thread_checker_.CalledOnValidThread()); | |
87 black_list_item_map_.reset(nullptr); | |
88 loaded_ = false; | |
89 // Delete relevant entries and reload the blacklist into memory. | |
90 if (opt_out_store_) { | |
91 opt_out_store_->ClearBlackList( | |
92 begin_time, end_time, base::Bind(&PreviewsBlackList::LoadBlackListDone, | |
93 weak_factory_.GetWeakPtr())); | |
94 } else { | |
95 LoadBlackListDone(base::MakeUnique<BlackListItemMap>()); | |
96 } | |
97 } | |
98 | |
99 void PreviewsBlackList::QueuePendingTask(QueueClosure callback) { | |
100 DCHECK(thread_checker_.CalledOnValidThread()); | |
101 DCHECK(!callback.is_null()); | |
102 pending_callbacks_.emplace(callback); | |
103 } | |
104 | |
105 void PreviewsBlackList::LoadBlackListDone( | |
106 std::unique_ptr<BlackListItemMap> black_list_item_map) { | |
107 DCHECK(thread_checker_.CalledOnValidThread()); | |
108 loaded_ = true; | |
109 black_list_item_map_ = std::move(black_list_item_map); | |
110 | |
111 // Run all pending tasks. |loaded_| may change if ClearBlackListIs queued. | |
112 while (pending_callbacks_.size() > 0 && loaded_) { | |
113 pending_callbacks_.front().Run(); | |
114 pending_callbacks_.pop(); | |
115 } | |
116 } | |
117 | |
118 PreviewsBlackListItem* PreviewsBlackList::GetBlackListItem( | |
119 std::string host_name, | |
120 bool create_if_needed) { | |
121 DCHECK(thread_checker_.CalledOnValidThread()); | |
122 BlackListItemMap::iterator iter = black_list_item_map_->find(host_name); | |
123 if (iter != black_list_item_map_->end()) { | |
124 return iter->second.get(); | |
125 } | |
126 if (!create_if_needed) | |
127 return nullptr; | |
128 // Create the item if it doesn't exist yet. | |
129 PreviewsBlackListItem* black_list_item = new PreviewsBlackListItem( | |
130 params::StoredHistoryLengthForBlackList(), | |
131 params::BlackListOptOutThreshold(), params::BlackListDuration()); | |
132 black_list_item_map_->operator[](host_name) = | |
133 base::WrapUnique(black_list_item); | |
134 return black_list_item; | |
135 } | |
136 | |
137 } // namespace previews | |
OLD | NEW |