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()); | |
tbansal1
2016/09/14 17:00:03
Thread checker is typically the first check.
RyanSturm
2016/09/14 18:36:42
Done.
| |
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, | |
tbansal1
2016/09/14 17:00:03
Since the pending task run on the same thread, you
RyanSturm
2016/09/14 18:36:42
Done.
| |
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::QueuePendingTask(QueueClosure callback) { | |
70 DCHECK(thread_checker_.CalledOnValidThread()); | |
71 DCHECK(!callback.is_null()); | |
72 pending_callbacks_.emplace(callback); | |
73 } | |
74 | |
75 void PreviewsBlackList::LoadBlackListDone( | |
76 std::unique_ptr<BlackListItemMap> black_list_item_map) { | |
77 DCHECK(thread_checker_.CalledOnValidThread()); | |
78 loaded_ = true; | |
79 black_list_item_map_ = std::move(black_list_item_map); | |
80 | |
81 // Run all pending tasks. |loaded_| may change if ClearBlackListIs queued. | |
82 while (pending_callbacks_.size() > 0 && loaded_) { | |
83 pending_callbacks_.front().Run(); | |
84 pending_callbacks_.pop(); | |
85 } | |
86 } | |
87 | |
88 PreviewsBlackListItem* PreviewsBlackList::GetBlackListItem( | |
89 const std::string& host_name, | |
90 bool create_if_needed) { | |
91 DCHECK(thread_checker_.CalledOnValidThread()); | |
92 BlackListItemMap::iterator iter = black_list_item_map_->find(host_name); | |
93 if (iter != black_list_item_map_->end()) { | |
94 return iter->second.get(); | |
95 } | |
96 if (!create_if_needed) | |
97 return nullptr; | |
98 // Create the item if it doesn't exist yet. | |
99 PreviewsBlackListItem* black_list_item = new PreviewsBlackListItem( | |
100 params::StoredHistoryLengthForBlackList(), | |
101 params::BlackListOptOutThreshold(), params::BlackListDuration()); | |
102 black_list_item_map_->operator[](host_name) = | |
103 base::WrapUnique(black_list_item); | |
104 return black_list_item; | |
105 } | |
106 | |
107 } // namespace previews | |
OLD | NEW |