Chromium Code Reviews| OLD | NEW |
|---|---|
| (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, | |
| 44 type)); | |
| 45 } | |
| 46 } | |
| 47 | |
| 48 void PreviewsBlackList::AddPreviewNavigationSync(const GURL& url, | |
| 49 bool opt_out, | |
| 50 PreviewsType type) { | |
| 51 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 52 DCHECK(url.has_host()); | |
| 53 std::string host_name = url.host(); | |
| 54 base::Time now = base::Time::Now(); | |
| 55 GetBlackListItem(host_name, true)->AddPreviewNavigation(opt_out, now); | |
| 56 if (!opt_out_store_) | |
| 57 return; | |
| 58 opt_out_store_->AddPreviewNavigation(opt_out, host_name, type, now); | |
| 59 } | |
| 60 | |
| 61 bool PreviewsBlackList::IsLoadedAndAllowed(const GURL& url, PreviewsType type) { | |
| 62 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 63 DCHECK(url.has_host()); | |
| 64 std::string host_name = url.host(); | |
| 65 if (!loaded_ || !previews::params::BlackListParamsAreValid()) { | |
|
tbansal1
2016/09/14 20:17:50
nit: no parenthesis required.
RyanSturm
2016/09/14 21:01:04
Done.
| |
| 66 return false; | |
| 67 } | |
| 68 PreviewsBlackListItem* black_list_item = GetBlackListItem(host_name, false); | |
| 69 return !black_list_item || !black_list_item->IsBlackListed(base::Time::Now()); | |
| 70 } | |
| 71 | |
| 72 void PreviewsBlackList::QueuePendingTask(QueueClosure callback) { | |
| 73 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 74 DCHECK(!callback.is_null()); | |
| 75 pending_callbacks_.emplace(callback); | |
| 76 } | |
| 77 | |
| 78 void PreviewsBlackList::LoadBlackListDone( | |
| 79 std::unique_ptr<BlackListItemMap> black_list_item_map) { | |
| 80 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 81 loaded_ = true; | |
| 82 black_list_item_map_ = std::move(black_list_item_map); | |
| 83 | |
| 84 // Run all pending tasks. |loaded_| may change if ClearBlackListIs queued. | |
| 85 while (pending_callbacks_.size() > 0 && loaded_) { | |
| 86 pending_callbacks_.front().Run(); | |
| 87 pending_callbacks_.pop(); | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 PreviewsBlackListItem* PreviewsBlackList::GetBlackListItem( | |
| 92 const std::string& host_name, | |
| 93 bool create_if_needed) { | |
| 94 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 95 BlackListItemMap::iterator iter = black_list_item_map_->find(host_name); | |
| 96 if (iter != black_list_item_map_->end()) { | |
| 97 return iter->second.get(); | |
| 98 } | |
| 99 if (!create_if_needed) | |
| 100 return nullptr; | |
| 101 // Create the item if it doesn't exist yet. | |
| 102 PreviewsBlackListItem* black_list_item = new PreviewsBlackListItem( | |
| 103 params::StoredHistoryLengthForBlackList(), | |
| 104 params::BlackListOptOutThreshold(), params::BlackListDuration()); | |
| 105 black_list_item_map_->operator[](host_name) = | |
| 106 base::WrapUnique(black_list_item); | |
| 107 return black_list_item; | |
| 108 } | |
| 109 | |
| 110 } // namespace previews | |
| OLD | NEW |