Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(488)

Side by Side Diff: components/previews/previews_black_list.cc

Issue 2335023002: Adding a previews IO-thread blacklist (Closed)
Patch Set: tbansal comments Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 #include "url/gurl.h"
13
14 namespace previews {
15
16 PreviewsBlackList::PreviewsBlackList(
17 std::unique_ptr<PreviewsOptOutStore> opt_out_store)
18 : loaded_(false),
19 opt_out_store_(std::move(opt_out_store)),
20 weak_factory_(this) {
21 if (opt_out_store_) {
22 opt_out_store_->LoadBlackList(base::Bind(
23 &PreviewsBlackList::LoadBlackListDone, weak_factory_.GetWeakPtr()));
24 } else {
25 LoadBlackListDone(base::MakeUnique<BlackListItemMap>());
26 }
27 }
28
29 PreviewsBlackList::~PreviewsBlackList() {}
30
31 void PreviewsBlackList::AddPreviewNavigation(const GURL& url,
32 bool opt_out,
33 PreviewsType type) {
34 DCHECK(thread_checker_.CalledOnValidThread());
35 DCHECK(url.has_host());
36 // If the |black_list_item_map_| has been loaded from |opt_out_store_|,
37 // synchronous operations will be accurate. Otherwise, queue the task to run
38 // asynchronously.
39 if (loaded_) {
40 AddPreviewNavigationSync(url, opt_out, type);
41 } else {
42 QueuePendingTask(base::Bind(&PreviewsBlackList::AddPreviewNavigationSync,
43 base::Unretained(this), url, opt_out, type));
44 }
45 }
46
47 void PreviewsBlackList::AddPreviewNavigationSync(const GURL& url,
48 bool opt_out,
49 PreviewsType type) {
50 DCHECK(thread_checker_.CalledOnValidThread());
51 DCHECK(url.has_host());
52 std::string host_name = url.host();
53 base::Time now = base::Time::Now();
54 GetBlackListItem(host_name, true)->AddPreviewNavigation(opt_out, now);
55 if (!opt_out_store_)
56 return;
57 opt_out_store_->AddPreviewNavigation(opt_out, host_name, type, now);
58 }
59
60 bool PreviewsBlackList::IsLoadedAndAllowed(const GURL& url, PreviewsType type) {
61 DCHECK(thread_checker_.CalledOnValidThread());
62 DCHECK(url.has_host());
63 std::string host_name = url.host();
64 if (!loaded_ || !previews::params::BlackListParamsAreValid())
65 return false;
66 PreviewsBlackListItem* black_list_item = GetBlackListItem(host_name, false);
67 return !black_list_item || !black_list_item->IsBlackListed(base::Time::Now());
68 }
69
70 void PreviewsBlackList::QueuePendingTask(QueueClosure callback) {
71 DCHECK(thread_checker_.CalledOnValidThread());
72 DCHECK(!callback.is_null());
73 pending_callbacks_.emplace(callback);
74 }
75
76 void PreviewsBlackList::LoadBlackListDone(
77 std::unique_ptr<BlackListItemMap> black_list_item_map) {
78 DCHECK(thread_checker_.CalledOnValidThread());
79 loaded_ = true;
80 black_list_item_map_ = std::move(black_list_item_map);
81
82 // Run all pending tasks. |loaded_| may change if ClearBlackListIs queued.
83 while (pending_callbacks_.size() > 0 && loaded_) {
84 pending_callbacks_.front().Run();
85 pending_callbacks_.pop();
86 }
87 }
88
89 PreviewsBlackListItem* PreviewsBlackList::GetBlackListItem(
90 const std::string& host_name,
91 bool create_if_needed) {
92 DCHECK(thread_checker_.CalledOnValidThread());
93 BlackListItemMap::iterator iter = black_list_item_map_->find(host_name);
94 if (iter != black_list_item_map_->end()) {
95 return iter->second.get();
96 }
97 if (!create_if_needed)
98 return nullptr;
99 // Create the item if it doesn't exist yet.
100 PreviewsBlackListItem* black_list_item = new PreviewsBlackListItem(
101 params::StoredHistoryLengthForBlackList(),
102 params::BlackListOptOutThreshold(), params::BlackListDuration());
103 black_list_item_map_->operator[](host_name) =
104 base::WrapUnique(black_list_item);
105 return black_list_item;
106 }
107
108 } // namespace previews
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698