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

Unified Diff: components/previews/core/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 side-by-side diff with in-line comments
Download patch
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
new file mode 100644
index 0000000000000000000000000000000000000000..d04586d4a5703fc720b275f0416716b4fdfc17c3
--- /dev/null
+++ b/components/previews/core/previews_black_list.cc
@@ -0,0 +1,143 @@
+// 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/core/previews_black_list.h"
+
+#include "base/bind.h"
+#include "base/memory/ptr_util.h"
+#include "base/time/clock.h"
+#include "base/time/time.h"
+#include "components/previews/core/previews_black_list_item.h"
+#include "components/previews/core/previews_experiments.h"
+#include "url/gurl.h"
+
+namespace previews {
+
+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>());
+ }
+}
+
+PreviewsBlackList::~PreviewsBlackList() {}
+
+void PreviewsBlackList::AddPreviewNavigation(const GURL& url,
+ bool opt_out,
+ PreviewsType type) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ DCHECK(url.has_host());
+ // 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(url, opt_out, type);
+ } else {
+ QueuePendingTask(base::Bind(&PreviewsBlackList::AddPreviewNavigationSync,
+ 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_);
+ std::string host_name = url.host();
+ base::Time now = clock_->Now();
+ GetBlackListItem(host_name, true)->AddPreviewNavigation(opt_out, now);
+ DCHECK_LE(black_list_item_map_->size(),
+ params::MaxInMemoryHostsInBlackList());
+ if (!opt_out_store_)
+ return;
+ opt_out_store_->AddPreviewNavigation(opt_out, host_name, type, now);
+}
+
+bool PreviewsBlackList::IsLoadedAndAllowed(const GURL& url, PreviewsType type) {
tbansal1 2016/09/20 17:48:50 I wish this was a const method so callers can call
RyanSturm 2016/09/20 18:38:16 I can make it const, i'll add another method for C
+ DCHECK(thread_checker_.CalledOnValidThread());
+ DCHECK(url.has_host());
+ std::string host_name = url.host();
+ if (!loaded_)
+ return false;
+ PreviewsBlackListItem* black_list_item = GetBlackListItem(host_name, false);
+ return !black_list_item || !black_list_item->IsBlackListed(clock_->Now());
+}
+
+void PreviewsBlackList::QueuePendingTask(QueueClosure 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) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ DCHECK_LE(black_list_item_map->size(), params::MaxInMemoryHostsInBlackList());
+ loaded_ = true;
+ black_list_item_map_ = std::move(black_list_item_map);
+
+ // Run all pending tasks. |loaded_| may change if ClearBlackList is queued.
+ while (pending_callbacks_.size() > 0 && loaded_) {
+ pending_callbacks_.front().Run();
+ pending_callbacks_.pop();
+ }
+}
+
+PreviewsBlackListItem* PreviewsBlackList::GetBlackListItem(
+ const std::string& host_name,
+ bool create_if_needed) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ DCHECK(loaded_);
+ BlackListItemMap::iterator iter = black_list_item_map_->find(host_name);
+ if (iter != black_list_item_map_->end()) {
tbansal1 2016/09/20 17:48:50 nit: braces not needed.
RyanSturm 2016/09/20 18:38:16 Done.
+ return iter->second.get();
+ }
+ if (!create_if_needed)
+ return nullptr;
+ if (black_list_item_map_->size() >= params::MaxInMemoryHostsInBlackList()) {
tbansal1 2016/09/20 17:48:50 nit: braces not needed.
RyanSturm 2016/09/20 18:38:16 Done.
+ EvictOldestOptOut();
+ }
+ DCHECK_LE(black_list_item_map_->size(),
tbansal1 2016/09/20 17:48:50 s/DCHECK_LE/DCHECK_LT/
RyanSturm 2016/09/20 18:38:16 Done.
+ params::MaxInMemoryHostsInBlackList());
+ // Create the item if it doesn't exist yet.
tbansal1 2016/09/20 17:48:50 s/Create the item if it doesn't exist yet./Create
RyanSturm 2016/09/20 18:38:16 I removed the comment based on refactor.
+ PreviewsBlackListItem* black_list_item = new PreviewsBlackListItem(
+ params::MaxStoredHistoryLengthForBlackList(),
+ params::BlackListOptOutThreshold(), params::BlackListDuration());
+ black_list_item_map_->operator[](host_name) =
+ base::WrapUnique(black_list_item);
+ return black_list_item;
+}
+
+void PreviewsBlackList::EvictOldestOptOut() {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ DCHECK(loaded_);
+ // TODO(ryansturm): Add UMA. crbug.com/647717
+ BlackListItemMap::iterator item_to_delete = black_list_item_map_->end();
+ base::Time oldest_opt_out = clock_->Now();
+ for (BlackListItemMap::iterator iter = black_list_item_map_->begin();
+ iter != black_list_item_map_->end(); ++iter) {
+ if (!iter->second->most_recent_opt_out_time()) {
tbansal1 2016/09/20 17:48:50 #include "base/optional.h"
RyanSturm 2016/09/20 18:38:16 Done.
+ // If there is no opt out time, this is a good choice to evict.
+ item_to_delete = iter;
+ break;
+ }
+ if (iter->second->most_recent_opt_out_time().value() < oldest_opt_out) {
+ oldest_opt_out = iter->second->most_recent_opt_out_time().value();
+ item_to_delete = iter;
+ }
+ }
+ black_list_item_map_->erase(item_to_delete);
+}
+
+} // namespace previews

Powered by Google App Engine
This is Rietveld 408576698