| Index: components/previews/core/previews_black_list.cc
|
| diff --git a/components/previews/core/previews_black_list.cc b/components/previews/core/previews_black_list.cc
|
| index 896507deee1e3391ed33a620df2b9e2777e7eba6..772b084836e5ad538b80a45f6422726ccaaa19b2 100644
|
| --- a/components/previews/core/previews_black_list.cc
|
| +++ b/components/previews/core/previews_black_list.cc
|
| @@ -31,34 +31,46 @@ void EvictOldestOptOut(BlackListItemMap* black_list_item_map) {
|
| if (!oldest_opt_out ||
|
| iter->second->most_recent_opt_out_time().value() <
|
| oldest_opt_out.value()) {
|
| oldest_opt_out = iter->second->most_recent_opt_out_time().value();
|
| item_to_delete = iter;
|
| }
|
| }
|
| black_list_item_map->erase(item_to_delete);
|
| }
|
|
|
| +// Returns the PreviewsBlackListItem representing |host_name| in
|
| +// |black_list_item_map|. If there is no item for |host_name|, returns null.
|
| +PreviewsBlackListItem* GetBlackListItemFromMap(
|
| + const BlackListItemMap& black_list_item_map,
|
| + const std::string& host_name) {
|
| + BlackListItemMap::const_iterator iter = black_list_item_map.find(host_name);
|
| + if (iter != black_list_item_map.end())
|
| + return iter->second.get();
|
| + return nullptr;
|
| +}
|
| +
|
| } // namespace
|
|
|
| PreviewsBlackList::PreviewsBlackList(
|
| std::unique_ptr<PreviewsOptOutStore> opt_out_store,
|
| std::unique_ptr<base::Clock> clock)
|
| : loaded_(false),
|
| opt_out_store_(std::move(opt_out_store)),
|
| clock_(std::move(clock)),
|
| weak_factory_(this) {
|
| if (opt_out_store_) {
|
| opt_out_store_->LoadBlackList(base::Bind(
|
| &PreviewsBlackList::LoadBlackListDone, weak_factory_.GetWeakPtr()));
|
| } else {
|
| - LoadBlackListDone(base::MakeUnique<BlackListItemMap>());
|
| + LoadBlackListDone(base::MakeUnique<BlackListItemMap>(),
|
| + CreateGeneralBlackListItem());
|
| }
|
| }
|
|
|
| PreviewsBlackList::~PreviewsBlackList() {}
|
|
|
| void PreviewsBlackList::AddPreviewNavigation(const GURL& url,
|
| bool opt_out,
|
| PreviewsType type) {
|
| DCHECK(thread_checker_.CalledOnValidThread());
|
| DCHECK(url.has_host());
|
| @@ -72,40 +84,47 @@ void PreviewsBlackList::AddPreviewNavigation(const GURL& url,
|
| base::Unretained(this), url, opt_out, type));
|
| }
|
| }
|
|
|
| void PreviewsBlackList::AddPreviewNavigationSync(const GURL& url,
|
| bool opt_out,
|
| PreviewsType type) {
|
| DCHECK(thread_checker_.CalledOnValidThread());
|
| DCHECK(url.has_host());
|
| DCHECK(loaded_);
|
| + DCHECK(general_black_list_item_);
|
| + DCHECK(black_list_item_map_);
|
| std::string host_name = url.host();
|
| base::Time now = clock_->Now();
|
| PreviewsBlackListItem* item =
|
| - GetOrCreateBlackListItem(black_list_item_map_.get(), host_name);
|
| + GetOrCreateBlackListItemForMap(black_list_item_map_.get(), host_name);
|
| item->AddPreviewNavigation(opt_out, now);
|
| DCHECK_LE(black_list_item_map_->size(),
|
| params::MaxInMemoryHostsInBlackList());
|
| + general_black_list_item_->AddPreviewNavigation(opt_out, now);
|
| if (!opt_out_store_)
|
| return;
|
| opt_out_store_->AddPreviewNavigation(opt_out, host_name, type, now);
|
| }
|
|
|
| bool PreviewsBlackList::IsLoadedAndAllowed(const GURL& url,
|
| PreviewsType type) const {
|
| DCHECK(thread_checker_.CalledOnValidThread());
|
| DCHECK(url.has_host());
|
| if (!loaded_)
|
| return false;
|
| + DCHECK(general_black_list_item_);
|
| + DCHECK(black_list_item_map_);
|
| + if (general_black_list_item_->IsBlackListed(clock_->Now()))
|
| + return false;
|
| PreviewsBlackListItem* black_list_item =
|
| - GetBlackListItem(*black_list_item_map_, url.host());
|
| + GetBlackListItemFromMap(*black_list_item_map_, url.host());
|
| return !black_list_item || !black_list_item->IsBlackListed(clock_->Now());
|
| }
|
|
|
| void PreviewsBlackList::ClearBlackList(base::Time begin_time,
|
| base::Time end_time) {
|
| DCHECK(thread_checker_.CalledOnValidThread());
|
| DCHECK_LE(begin_time, end_time);
|
| // 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.
|
| @@ -115,73 +134,79 @@ void PreviewsBlackList::ClearBlackList(base::Time begin_time,
|
| QueuePendingTask(base::Bind(&PreviewsBlackList::ClearBlackListSync,
|
| base::Unretained(this), begin_time, end_time));
|
| }
|
| }
|
|
|
| void PreviewsBlackList::ClearBlackListSync(base::Time begin_time,
|
| base::Time end_time) {
|
| DCHECK(thread_checker_.CalledOnValidThread());
|
| DCHECK(loaded_);
|
| DCHECK_LE(begin_time, end_time);
|
| - black_list_item_map_.reset(nullptr);
|
| + black_list_item_map_.reset();
|
| + general_black_list_item_.reset();
|
| loaded_ = false;
|
| // Delete relevant entries and reload the blacklist into memory.
|
| if (opt_out_store_) {
|
| opt_out_store_->ClearBlackList(begin_time, end_time);
|
| opt_out_store_->LoadBlackList(base::Bind(
|
| &PreviewsBlackList::LoadBlackListDone, weak_factory_.GetWeakPtr()));
|
| } else {
|
| - LoadBlackListDone(base::MakeUnique<BlackListItemMap>());
|
| + LoadBlackListDone(base::MakeUnique<BlackListItemMap>(),
|
| + CreateGeneralBlackListItem());
|
| }
|
| }
|
|
|
| void PreviewsBlackList::QueuePendingTask(base::Closure callback) {
|
| DCHECK(thread_checker_.CalledOnValidThread());
|
| DCHECK(!loaded_);
|
| DCHECK(!callback.is_null());
|
| pending_callbacks_.emplace(callback);
|
| }
|
|
|
| void PreviewsBlackList::LoadBlackListDone(
|
| - std::unique_ptr<BlackListItemMap> black_list_item_map) {
|
| + std::unique_ptr<BlackListItemMap> black_list_item_map,
|
| + std::unique_ptr<PreviewsBlackListItem> general_black_list_item) {
|
| DCHECK(thread_checker_.CalledOnValidThread());
|
| + DCHECK(general_black_list_item);
|
| + DCHECK(black_list_item_map);
|
| DCHECK_LE(black_list_item_map->size(), params::MaxInMemoryHostsInBlackList());
|
| loaded_ = true;
|
| black_list_item_map_ = std::move(black_list_item_map);
|
| + general_black_list_item_ = std::move(general_black_list_item);
|
|
|
| // Run all pending tasks. |loaded_| may change if ClearBlackList is queued.
|
| while (pending_callbacks_.size() > 0 && loaded_) {
|
| pending_callbacks_.front().Run();
|
| pending_callbacks_.pop();
|
| }
|
| }
|
|
|
| // static
|
| -PreviewsBlackListItem* PreviewsBlackList::GetBlackListItem(
|
| - const BlackListItemMap& black_list_item_map,
|
| - const std::string& host_name) {
|
| - BlackListItemMap::const_iterator iter = black_list_item_map.find(host_name);
|
| - if (iter != black_list_item_map.end())
|
| - return iter->second.get();
|
| - return nullptr;
|
| -}
|
| -
|
| -// static
|
| -PreviewsBlackListItem* PreviewsBlackList::GetOrCreateBlackListItem(
|
| +PreviewsBlackListItem* PreviewsBlackList::GetOrCreateBlackListItemForMap(
|
| BlackListItemMap* black_list_item_map,
|
| const std::string& host_name) {
|
| PreviewsBlackListItem* black_list_item =
|
| - GetBlackListItem(*black_list_item_map, host_name);
|
| + GetBlackListItemFromMap(*black_list_item_map, host_name);
|
| if (black_list_item)
|
| return black_list_item;
|
| if (black_list_item_map->size() >= params::MaxInMemoryHostsInBlackList())
|
| EvictOldestOptOut(black_list_item_map);
|
| DCHECK_LT(black_list_item_map->size(), params::MaxInMemoryHostsInBlackList());
|
| black_list_item = new PreviewsBlackListItem(
|
| - params::MaxStoredHistoryLengthForBlackList(),
|
| - params::BlackListOptOutThreshold(), params::BlackListDuration());
|
| + params::MaxStoredHistoryLengthForPerHostBlackList(),
|
| + params::PerHostBlackListOptOutThreshold(),
|
| + params::PerHostBlackListDuration());
|
| black_list_item_map->operator[](host_name) =
|
| base::WrapUnique(black_list_item);
|
| return black_list_item;
|
| }
|
|
|
| +// static
|
| +std::unique_ptr<PreviewsBlackListItem>
|
| +PreviewsBlackList::CreateGeneralBlackListItem() {
|
| + return base::MakeUnique<PreviewsBlackListItem>(
|
| + params::MaxStoredHistoryLengthForGeneralBlackList(),
|
| + params::GeneralBlackListOptOutThreshold(),
|
| + params::GeneralBlackListPerHostDuration());
|
| +}
|
| +
|
| } // namespace previews
|
|
|