Chromium Code Reviews| 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 08b9fa2910c4abbb05b4232100bd2bb6038144e1..d199ff4d1bc9b076a94dbd7ea0062dc2142d02c9 100644 |
| --- a/components/previews/core/previews_black_list.cc |
| +++ b/components/previews/core/previews_black_list.cc |
| @@ -30,34 +30,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>(), |
| + CreateHostIndifferentBlackListItem()); |
| } |
| } |
| PreviewsBlackList::~PreviewsBlackList() {} |
| void PreviewsBlackList::AddPreviewNavigation(const GURL& url, |
| bool opt_out, |
| PreviewsType type) { |
| DCHECK(thread_checker_.CalledOnValidThread()); |
| DCHECK(url.has_host()); |
| @@ -74,45 +86,51 @@ 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(host_indifferent_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()); |
| + host_indifferent_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(black_list_item_map_); |
| if (last_opt_out_time_ && |
| clock_->Now() < |
| last_opt_out_time_.value() + params::SingleOptOutDuration()) { |
| return false; |
| } |
| + if (host_indifferent_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. |
| @@ -122,73 +140,80 @@ 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(); |
| + host_indifferent_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>(), |
| + CreateHostIndifferentBlackListItem()); |
| } |
| } |
| 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> host_indifferent_black_list_item) { |
| DCHECK(thread_checker_.CalledOnValidThread()); |
| + DCHECK(host_indifferent_black_list_item); |
|
tbansal1
2016/10/28 21:59:33
super nit:put DCHECK in the same order as function
RyanSturm
2016/11/02 19:52:30
Done.
|
| + 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); |
| + host_indifferent_black_list_item_ = |
| + std::move(host_indifferent_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::CreateHostIndifferentBlackListItem() { |
| + return base::MakeUnique<PreviewsBlackListItem>( |
| + params::MaxStoredHistoryLengthForHostIndifferentBlackList(), |
| + params::HostIndifferentBlackListOptOutThreshold(), |
| + params::HostIndifferentBlackListPerHostDuration()); |
| +} |
| + |
| } // namespace previews |