Chromium Code Reviews| Index: components/previews/previews_black_list.cc |
| diff --git a/components/previews/previews_black_list.cc b/components/previews/previews_black_list.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..25160fae0e50d4481fddbdd7526e756248007c83 |
| --- /dev/null |
| +++ b/components/previews/previews_black_list.cc |
| @@ -0,0 +1,137 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/previews/previews_black_list.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "base/time/time.h" |
| +#include "components/previews/previews_black_list_item.h" |
| +#include "components/previews/previews_experiments.h" |
| + |
| +namespace previews { |
| + |
| +PreviewsBlackList::PreviewsBlackList( |
| + std::unique_ptr<PreviewsOptOutStore> opt_out_store) |
| + : loaded_(false), |
| + opt_out_store_(std::move(opt_out_store)), |
| + weak_factory_(this) { |
| + if (opt_out_store_) { |
| + opt_out_store_->LoadBlackList(base::Bind( |
| + &PreviewsBlackList::LoadBlackListDone, weak_factory_.GetWeakPtr())); |
| + } else { |
| + LoadBlackListDone(base::MakeUnique<BlackListItemMap>()); |
| + } |
| +} |
| + |
| +PreviewsBlackList::~PreviewsBlackList() {} |
| + |
| +void PreviewsBlackList::AddPreviewNavigation(const std::string& host_name, |
| + bool opt_out, |
| + PreviewsType type) { |
| + DCHECK(!host_name.empty()); |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + // If the |black_list_item_map_| has been loaded from |opt_out_store_|, |
| + // synchronous operations will be accurate. Otherwise, queue the task to run |
| + // asynchronously. |
| + if (loaded_) { |
| + AddPreviewNavigationSync(host_name, opt_out, type); |
| + } else { |
| + QueuePendingTask(base::Bind(&PreviewsBlackList::AddPreviewNavigationSync, |
| + weak_factory_.GetWeakPtr(), host_name, opt_out, |
| + type)); |
| + } |
| +} |
| + |
| +void PreviewsBlackList::AddPreviewNavigationSync(const std::string& host_name, |
| + bool opt_out, |
| + PreviewsType type) { |
| + DCHECK(!host_name.empty()); |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + base::Time now = base::Time::Now(); |
| + GetBlackListItem(host_name, true)->AddPreviewNavigation(opt_out, now); |
| + if (!opt_out_store_) |
| + return; |
| + opt_out_store_->AddPreviewNavigation(opt_out, host_name, type, now); |
| +} |
| + |
| +bool PreviewsBlackList::IsLoadedAndAllowed(const std::string& host_name) { |
| + DCHECK(!host_name.empty()); |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + if (!loaded_ || !previews::params::BlackListParamsAreValid()) { |
| + return false; |
| + } |
| + PreviewsBlackListItem* black_list_item = GetBlackListItem(host_name, false); |
| + return !black_list_item || !black_list_item->IsBlackListed(base::Time::Now()); |
| +} |
| + |
| +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.
|
| + const base::Time& end_time) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + // If the |black_list_item_map_| has been loaded from |opt_out_store_|, |
| + // synchronous operations will be accurate. Otherwise, queue the task to run |
| + // asynchronously. |
| + if (loaded_) { |
| + ClearBlackListSync(begin_time, end_time); |
| + } else { |
| + QueuePendingTask(base::Bind(&PreviewsBlackList::ClearBlackListSync, |
| + weak_factory_.GetWeakPtr(), begin_time, |
| + end_time)); |
| + } |
| +} |
| + |
| +void PreviewsBlackList::ClearBlackListSync(const base::Time& begin_time, |
| + const base::Time& end_time) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + black_list_item_map_.reset(nullptr); |
| + loaded_ = false; |
| + // Delete relevant entries and reload the blacklist into memory. |
| + if (opt_out_store_) { |
| + opt_out_store_->ClearBlackList( |
| + begin_time, end_time, base::Bind(&PreviewsBlackList::LoadBlackListDone, |
| + weak_factory_.GetWeakPtr())); |
| + } else { |
| + LoadBlackListDone(base::MakeUnique<BlackListItemMap>()); |
| + } |
| +} |
| + |
| +void PreviewsBlackList::QueuePendingTask(QueueClosure callback) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + DCHECK(!callback.is_null()); |
| + pending_callbacks_.emplace(callback); |
| +} |
| + |
| +void PreviewsBlackList::LoadBlackListDone( |
| + std::unique_ptr<BlackListItemMap> black_list_item_map) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + loaded_ = true; |
| + black_list_item_map_ = std::move(black_list_item_map); |
| + |
| + // Run all pending tasks. |loaded_| may change if ClearBlackListIs queued. |
| + while (pending_callbacks_.size() > 0 && loaded_) { |
| + pending_callbacks_.front().Run(); |
| + pending_callbacks_.pop(); |
| + } |
| +} |
| + |
| +PreviewsBlackListItem* PreviewsBlackList::GetBlackListItem( |
| + std::string host_name, |
| + bool create_if_needed) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + BlackListItemMap::iterator iter = black_list_item_map_->find(host_name); |
| + if (iter != black_list_item_map_->end()) { |
| + return iter->second.get(); |
| + } |
| + if (!create_if_needed) |
| + return nullptr; |
| + // Create the item if it doesn't exist yet. |
| + PreviewsBlackListItem* black_list_item = new PreviewsBlackListItem( |
| + params::StoredHistoryLengthForBlackList(), |
| + params::BlackListOptOutThreshold(), params::BlackListDuration()); |
| + black_list_item_map_->operator[](host_name) = |
| + base::WrapUnique(black_list_item); |
| + return black_list_item; |
| +} |
| + |
| +} // namespace previews |