[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 1 | // Copyright 2012 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 "chrome/browser/extensions/blacklist.h" |
| 6 | |
[email protected] | 695b571 | 2012-12-06 23:55:28 | [diff] [blame] | 7 | #include <algorithm> |
[email protected] | c014f2b3 | 2013-09-03 23:29:12 | [diff] [blame] | 8 | #include <iterator> |
[email protected] | 695b571 | 2012-12-06 23:55:28 | [diff] [blame] | 9 | |
| 10 | #include "base/bind.h" |
Avi Drissman | e222a56 | 2018-03-27 03:25:48 | [diff] [blame] | 11 | #include "base/callback_list.h" |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 12 | #include "base/lazy_instance.h" |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 13 | #include "base/macros.h" |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
pranay.kumar | 0730078 | 2015-05-04 14:03:58 | [diff] [blame] | 15 | #include "base/single_thread_task_runner.h" |
[email protected] | 16bf7ba7 | 2013-08-23 11:52:54 | [diff] [blame] | 16 | #include "base/stl_util.h" |
gab | b15e1907 | 2016-05-11 20:45:41 | [diff] [blame] | 17 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 18 | #include "chrome/browser/browser_process.h" |
reillyg | 121e889 | 2014-11-03 22:12:59 | [diff] [blame] | 19 | #include "chrome/browser/extensions/blacklist_factory.h" |
[email protected] | 8e289f0b | 2013-12-17 17:49:07 | [diff] [blame] | 20 | #include "chrome/browser/extensions/blacklist_state_fetcher.h" |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 21 | #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
brettw | b1fc1b8 | 2016-02-02 00:19:08 | [diff] [blame] | 22 | #include "components/prefs/pref_service.h" |
Tim Volodine | e4593847 | 2017-09-21 10:08:22 | [diff] [blame] | 23 | #include "components/safe_browsing/db/util.h" |
Gabriel Charette | 790754c | 2018-03-16 21:32:59 | [diff] [blame] | 24 | #include "content/public/browser/browser_thread.h" |
[email protected] | 489db084 | 2014-01-22 18:20:03 | [diff] [blame] | 25 | #include "extensions/browser/extension_prefs.h" |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 26 | |
| 27 | using content::BrowserThread; |
vakh | 9a474d83 | 2015-11-13 01:43:09 | [diff] [blame] | 28 | using safe_browsing::SafeBrowsingDatabaseManager; |
[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 29 | |
| 30 | namespace extensions { |
| 31 | |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 32 | namespace { |
| 33 | |
| 34 | // The safe browsing database manager to use. Make this a global/static variable |
| 35 | // rather than a member of Blacklist because Blacklist accesses the real |
| 36 | // database manager before it has a chance to get a fake one. |
| 37 | class LazySafeBrowsingDatabaseManager { |
| 38 | public: |
| 39 | LazySafeBrowsingDatabaseManager() { |
nparker | 333f169b | 2015-04-18 13:33:07 | [diff] [blame] | 40 | #if defined(SAFE_BROWSING_DB_LOCAL) |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 41 | if (g_browser_process && g_browser_process->safe_browsing_service()) { |
| 42 | instance_ = |
| 43 | g_browser_process->safe_browsing_service()->database_manager(); |
| 44 | } |
[email protected] | 04f7d829 | 2013-02-18 09:31:10 | [diff] [blame] | 45 | #endif |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | scoped_refptr<SafeBrowsingDatabaseManager> get() { |
| 49 | return instance_; |
| 50 | } |
| 51 | |
| 52 | void set(scoped_refptr<SafeBrowsingDatabaseManager> instance) { |
| 53 | instance_ = instance; |
Avi Drissman | e222a56 | 2018-03-27 03:25:48 | [diff] [blame] | 54 | database_changed_callback_list_.Notify(); |
| 55 | } |
| 56 | |
| 57 | std::unique_ptr<base::CallbackList<void()>::Subscription> |
| 58 | RegisterDatabaseChangedCallback(const base::RepeatingClosure& cb) { |
| 59 | return database_changed_callback_list_.Add(cb); |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | private: |
| 63 | scoped_refptr<SafeBrowsingDatabaseManager> instance_; |
Avi Drissman | e222a56 | 2018-03-27 03:25:48 | [diff] [blame] | 64 | base::CallbackList<void()> database_changed_callback_list_; |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 65 | }; |
| 66 | |
scottmg | 5e65e3a | 2017-03-08 08:48:46 | [diff] [blame] | 67 | static base::LazyInstance<LazySafeBrowsingDatabaseManager>::DestructorAtExit |
| 68 | g_database_manager = LAZY_INSTANCE_INITIALIZER; |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 69 | |
| 70 | // Implementation of SafeBrowsingDatabaseManager::Client, the class which is |
| 71 | // called back from safebrowsing queries. |
| 72 | // |
| 73 | // Constructed on any thread but lives on the IO from then on. |
| 74 | class SafeBrowsingClientImpl |
| 75 | : public SafeBrowsingDatabaseManager::Client, |
| 76 | public base::RefCountedThreadSafe<SafeBrowsingClientImpl> { |
| 77 | public: |
Avi Drissman | 6ee0692f | 2018-03-26 17:08:01 | [diff] [blame] | 78 | using OnResultCallback = base::Callback<void(const std::set<std::string>&)>; |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 79 | |
| 80 | // Constructs a client to query the database manager for |extension_ids| and |
| 81 | // run |callback| with the IDs of those which have been blacklisted. |
| 82 | SafeBrowsingClientImpl( |
| 83 | const std::set<std::string>& extension_ids, |
| 84 | const OnResultCallback& callback) |
pranay.kumar | 0730078 | 2015-05-04 14:03:58 | [diff] [blame] | 85 | : callback_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 86 | callback_(callback) { |
| 87 | BrowserThread::PostTask( |
tzik | 8d880ee | 2017-04-20 19:46:24 | [diff] [blame] | 88 | BrowserThread::IO, FROM_HERE, |
| 89 | base::BindOnce(&SafeBrowsingClientImpl::StartCheck, this, |
| 90 | g_database_manager.Get().get(), extension_ids)); |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | private: |
| 94 | friend class base::RefCountedThreadSafe<SafeBrowsingClientImpl>; |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 95 | ~SafeBrowsingClientImpl() override {} |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 96 | |
| 97 | // Pass |database_manager| as a parameter to avoid touching |
| 98 | // SafeBrowsingService on the IO thread. |
| 99 | void StartCheck(scoped_refptr<SafeBrowsingDatabaseManager> database_manager, |
| 100 | const std::set<std::string>& extension_ids) { |
[email protected] | 54ee819 | 2014-03-29 17:37:24 | [diff] [blame] | 101 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 102 | if (database_manager->CheckExtensionIDs(extension_ids, this)) { |
| 103 | // Definitely not blacklisted. Callback immediately. |
pranay.kumar | 0730078 | 2015-05-04 14:03:58 | [diff] [blame] | 104 | callback_task_runner_->PostTask( |
tzik | 8d880ee | 2017-04-20 19:46:24 | [diff] [blame] | 105 | FROM_HERE, base::BindOnce(callback_, std::set<std::string>())); |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 106 | return; |
| 107 | } |
| 108 | // Something might be blacklisted, response will come in |
| 109 | // OnCheckExtensionsResult. |
| 110 | AddRef(); // Balanced in OnCheckExtensionsResult |
| 111 | } |
| 112 | |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 113 | void OnCheckExtensionsResult(const std::set<std::string>& hits) override { |
[email protected] | 54ee819 | 2014-03-29 17:37:24 | [diff] [blame] | 114 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
tzik | 8d880ee | 2017-04-20 19:46:24 | [diff] [blame] | 115 | callback_task_runner_->PostTask(FROM_HERE, base::BindOnce(callback_, hits)); |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 116 | Release(); // Balanced in StartCheck. |
| 117 | } |
| 118 | |
pranay.kumar | 0730078 | 2015-05-04 14:03:58 | [diff] [blame] | 119 | scoped_refptr<base::SingleThreadTaskRunner> callback_task_runner_; |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 120 | OnResultCallback callback_; |
| 121 | |
| 122 | DISALLOW_COPY_AND_ASSIGN(SafeBrowsingClientImpl); |
| 123 | }; |
| 124 | |
[email protected] | 48a35934 | 2013-10-30 00:22:00 | [diff] [blame] | 125 | void CheckOneExtensionState( |
| 126 | const Blacklist::IsBlacklistedCallback& callback, |
| 127 | const Blacklist::BlacklistStateMap& state_map) { |
[email protected] | 8e289f0b | 2013-12-17 17:49:07 | [diff] [blame] | 128 | callback.Run(state_map.empty() ? NOT_BLACKLISTED : state_map.begin()->second); |
[email protected] | 48a35934 | 2013-10-30 00:22:00 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void GetMalwareFromBlacklistStateMap( |
| 132 | const Blacklist::GetMalwareIDsCallback& callback, |
| 133 | const Blacklist::BlacklistStateMap& state_map) { |
| 134 | std::set<std::string> malware; |
Avi Drissman | 6ee0692f | 2018-03-26 17:08:01 | [diff] [blame] | 135 | for (const auto& state_pair : state_map) { |
[email protected] | 8e289f0b | 2013-12-17 17:49:07 | [diff] [blame] | 136 | // TODO(oleg): UNKNOWN is treated as MALWARE for backwards compatibility. |
| 137 | // In future GetMalwareIDs will be removed and the caller will have to |
| 138 | // deal with BLACKLISTED_UNKNOWN state returned from GetBlacklistedIDs. |
Avi Drissman | 6ee0692f | 2018-03-26 17:08:01 | [diff] [blame] | 139 | if (state_pair.second == BLACKLISTED_MALWARE || |
| 140 | state_pair.second == BLACKLISTED_UNKNOWN) { |
| 141 | malware.insert(state_pair.first); |
| 142 | } |
[email protected] | 48a35934 | 2013-10-30 00:22:00 | [diff] [blame] | 143 | } |
| 144 | callback.Run(malware); |
[email protected] | bc151cf9 | 2013-02-12 04:57:26 | [diff] [blame] | 145 | } |
| 146 | |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 147 | } // namespace |
| 148 | |
[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 149 | Blacklist::Observer::Observer(Blacklist* blacklist) : blacklist_(blacklist) { |
| 150 | blacklist_->AddObserver(this); |
| 151 | } |
| 152 | |
| 153 | Blacklist::Observer::~Observer() { |
| 154 | blacklist_->RemoveObserver(this); |
| 155 | } |
| 156 | |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 157 | Blacklist::ScopedDatabaseManagerForTest::ScopedDatabaseManagerForTest( |
| 158 | scoped_refptr<SafeBrowsingDatabaseManager> database_manager) |
| 159 | : original_(GetDatabaseManager()) { |
| 160 | SetDatabaseManager(database_manager); |
| 161 | } |
| 162 | |
| 163 | Blacklist::ScopedDatabaseManagerForTest::~ScopedDatabaseManagerForTest() { |
| 164 | SetDatabaseManager(original_); |
| 165 | } |
| 166 | |
[email protected] | 3f2a2fa | 2013-09-24 02:55:25 | [diff] [blame] | 167 | Blacklist::Blacklist(ExtensionPrefs* prefs) { |
Avi Drissman | e222a56 | 2018-03-27 03:25:48 | [diff] [blame] | 168 | auto& lazy_database_manager = g_database_manager.Get(); |
| 169 | // Using base::Unretained is safe because when this object goes away, the |
| 170 | // subscription will automatically be destroyed. |
| 171 | database_changed_subscription_ = |
| 172 | lazy_database_manager.RegisterDatabaseChangedCallback(base::BindRepeating( |
| 173 | &Blacklist::ObserveNewDatabase, base::Unretained(this))); |
| 174 | |
| 175 | ObserveNewDatabase(); |
[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | Blacklist::~Blacklist() { |
| 179 | } |
| 180 | |
reillyg | 121e889 | 2014-11-03 22:12:59 | [diff] [blame] | 181 | // static |
| 182 | Blacklist* Blacklist::Get(content::BrowserContext* context) { |
| 183 | return BlacklistFactory::GetForBrowserContext(context); |
| 184 | } |
| 185 | |
[email protected] | 695b571 | 2012-12-06 23:55:28 | [diff] [blame] | 186 | void Blacklist::GetBlacklistedIDs(const std::set<std::string>& ids, |
| 187 | const GetBlacklistedIDsCallback& callback) { |
[email protected] | 54ee819 | 2014-03-29 17:37:24 | [diff] [blame] | 188 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 189 | |
Avi Drissman | 6ee0692f | 2018-03-26 17:08:01 | [diff] [blame] | 190 | if (ids.empty() || !GetDatabaseManager().get()) { |
pranay.kumar | 0730078 | 2015-05-04 14:03:58 | [diff] [blame] | 191 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
tzik | 8d880ee | 2017-04-20 19:46:24 | [diff] [blame] | 192 | FROM_HERE, base::BindOnce(callback, BlacklistStateMap())); |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 193 | return; |
| 194 | } |
| 195 | |
| 196 | // Constructing the SafeBrowsingClientImpl begins the process of asking |
[email protected] | 48a35934 | 2013-10-30 00:22:00 | [diff] [blame] | 197 | // safebrowsing for the blacklisted extensions. The set of blacklisted |
| 198 | // extensions returned by SafeBrowsing will then be passed to |
| 199 | // GetBlacklistStateIDs to get the particular BlacklistState for each id. |
| 200 | new SafeBrowsingClientImpl( |
| 201 | ids, base::Bind(&Blacklist::GetBlacklistStateForIDs, AsWeakPtr(), |
| 202 | callback)); |
[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 203 | } |
| 204 | |
[email protected] | 48a35934 | 2013-10-30 00:22:00 | [diff] [blame] | 205 | void Blacklist::GetMalwareIDs(const std::set<std::string>& ids, |
| 206 | const GetMalwareIDsCallback& callback) { |
| 207 | GetBlacklistedIDs(ids, base::Bind(&GetMalwareFromBlacklistStateMap, |
| 208 | callback)); |
| 209 | } |
| 210 | |
| 211 | |
[email protected] | bc151cf9 | 2013-02-12 04:57:26 | [diff] [blame] | 212 | void Blacklist::IsBlacklisted(const std::string& extension_id, |
| 213 | const IsBlacklistedCallback& callback) { |
| 214 | std::set<std::string> check; |
| 215 | check.insert(extension_id); |
[email protected] | 48a35934 | 2013-10-30 00:22:00 | [diff] [blame] | 216 | GetBlacklistedIDs(check, base::Bind(&CheckOneExtensionState, callback)); |
| 217 | } |
| 218 | |
| 219 | void Blacklist::GetBlacklistStateForIDs( |
| 220 | const GetBlacklistedIDsCallback& callback, |
| 221 | const std::set<std::string>& blacklisted_ids) { |
[email protected] | 54ee819 | 2014-03-29 17:37:24 | [diff] [blame] | 222 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 48a35934 | 2013-10-30 00:22:00 | [diff] [blame] | 223 | |
| 224 | std::set<std::string> ids_unknown_state; |
| 225 | BlacklistStateMap extensions_state; |
Avi Drissman | 6ee0692f | 2018-03-26 17:08:01 | [diff] [blame] | 226 | for (const auto& blacklisted_id : blacklisted_ids) { |
| 227 | auto cache_it = blacklist_state_cache_.find(blacklisted_id); |
[email protected] | 8e289f0b | 2013-12-17 17:49:07 | [diff] [blame] | 228 | if (cache_it == blacklist_state_cache_.end() || |
Avi Drissman | 6ee0692f | 2018-03-26 17:08:01 | [diff] [blame] | 229 | cache_it->second == |
| 230 | BLACKLISTED_UNKNOWN) { // Do not return UNKNOWN |
| 231 | // from cache, retry request. |
| 232 | ids_unknown_state.insert(blacklisted_id); |
| 233 | } else { |
| 234 | extensions_state[blacklisted_id] = cache_it->second; |
| 235 | } |
[email protected] | 48a35934 | 2013-10-30 00:22:00 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | if (ids_unknown_state.empty()) { |
| 239 | callback.Run(extensions_state); |
| 240 | } else { |
| 241 | // After the extension blacklist states have been downloaded, call this |
| 242 | // functions again, but prevent infinite cycle in case server is offline |
| 243 | // or some other reason prevents us from receiving the blacklist state for |
| 244 | // these extensions. |
| 245 | RequestExtensionsBlacklistState( |
| 246 | ids_unknown_state, |
Avi Drissman | 6ee0692f | 2018-03-26 17:08:01 | [diff] [blame] | 247 | base::BindOnce(&Blacklist::ReturnBlacklistStateMap, AsWeakPtr(), |
| 248 | callback, blacklisted_ids)); |
[email protected] | 48a35934 | 2013-10-30 00:22:00 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
| 252 | void Blacklist::ReturnBlacklistStateMap( |
| 253 | const GetBlacklistedIDsCallback& callback, |
| 254 | const std::set<std::string>& blacklisted_ids) { |
| 255 | BlacklistStateMap extensions_state; |
Avi Drissman | 6ee0692f | 2018-03-26 17:08:01 | [diff] [blame] | 256 | for (const auto& blacklisted_id : blacklisted_ids) { |
| 257 | auto cache_it = blacklist_state_cache_.find(blacklisted_id); |
[email protected] | 48a35934 | 2013-10-30 00:22:00 | [diff] [blame] | 258 | if (cache_it != blacklist_state_cache_.end()) |
Avi Drissman | 6ee0692f | 2018-03-26 17:08:01 | [diff] [blame] | 259 | extensions_state[blacklisted_id] = cache_it->second; |
[email protected] | 48a35934 | 2013-10-30 00:22:00 | [diff] [blame] | 260 | // If for some reason we still haven't cached the state of this extension, |
| 261 | // we silently skip it. |
| 262 | } |
| 263 | |
| 264 | callback.Run(extensions_state); |
| 265 | } |
| 266 | |
| 267 | void Blacklist::RequestExtensionsBlacklistState( |
Avi Drissman | 6ee0692f | 2018-03-26 17:08:01 | [diff] [blame] | 268 | const std::set<std::string>& ids, |
| 269 | base::OnceClosure callback) { |
[email protected] | 54ee819 | 2014-03-29 17:37:24 | [diff] [blame] | 270 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 8e289f0b | 2013-12-17 17:49:07 | [diff] [blame] | 271 | if (!state_fetcher_) |
| 272 | state_fetcher_.reset(new BlacklistStateFetcher()); |
| 273 | |
Avi Drissman | 6ee0692f | 2018-03-26 17:08:01 | [diff] [blame] | 274 | state_requests_.emplace_back(std::vector<std::string>(ids.begin(), ids.end()), |
| 275 | std::move(callback)); |
| 276 | for (const auto& id : ids) { |
[email protected] | 8e289f0b | 2013-12-17 17:49:07 | [diff] [blame] | 277 | state_fetcher_->Request( |
Avi Drissman | 6ee0692f | 2018-03-26 17:08:01 | [diff] [blame] | 278 | id, base::Bind(&Blacklist::OnBlacklistStateReceived, AsWeakPtr(), id)); |
[email protected] | 48a35934 | 2013-10-30 00:22:00 | [diff] [blame] | 279 | } |
[email protected] | 8e289f0b | 2013-12-17 17:49:07 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | void Blacklist::OnBlacklistStateReceived(const std::string& id, |
| 283 | BlacklistState state) { |
[email protected] | 54ee819 | 2014-03-29 17:37:24 | [diff] [blame] | 284 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 8e289f0b | 2013-12-17 17:49:07 | [diff] [blame] | 285 | blacklist_state_cache_[id] = state; |
| 286 | |
| 287 | // Go through the opened requests and call the callbacks for those requests |
| 288 | // for which we already got all the required blacklist states. |
Avi Drissman | 6ee0692f | 2018-03-26 17:08:01 | [diff] [blame] | 289 | auto requests_it = state_requests_.begin(); |
[email protected] | 8e289f0b | 2013-12-17 17:49:07 | [diff] [blame] | 290 | while (requests_it != state_requests_.end()) { |
| 291 | const std::vector<std::string>& ids = requests_it->first; |
| 292 | |
| 293 | bool have_all_in_cache = true; |
Avi Drissman | 6ee0692f | 2018-03-26 17:08:01 | [diff] [blame] | 294 | for (const auto& id : ids) { |
| 295 | if (!base::ContainsKey(blacklist_state_cache_, id)) { |
[email protected] | 8e289f0b | 2013-12-17 17:49:07 | [diff] [blame] | 296 | have_all_in_cache = false; |
| 297 | break; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | if (have_all_in_cache) { |
Avi Drissman | 6ee0692f | 2018-03-26 17:08:01 | [diff] [blame] | 302 | std::move(requests_it->second).Run(); |
[email protected] | 8e289f0b | 2013-12-17 17:49:07 | [diff] [blame] | 303 | requests_it = state_requests_.erase(requests_it); // returns next element |
| 304 | } else { |
| 305 | ++requests_it; |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | void Blacklist::SetBlacklistStateFetcherForTest( |
| 311 | BlacklistStateFetcher* fetcher) { |
| 312 | state_fetcher_.reset(fetcher); |
[email protected] | bc151cf9 | 2013-02-12 04:57:26 | [diff] [blame] | 313 | } |
| 314 | |
[email protected] | f71b582c | 2014-01-10 17:03:15 | [diff] [blame] | 315 | BlacklistStateFetcher* Blacklist::ResetBlacklistStateFetcherForTest() { |
| 316 | return state_fetcher_.release(); |
| 317 | } |
| 318 | |
Avi Drissman | e222a56 | 2018-03-27 03:25:48 | [diff] [blame] | 319 | void Blacklist::ResetDatabaseUpdatedListenerForTest() { |
| 320 | database_updated_subscription_.reset(); |
| 321 | } |
| 322 | |
[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 323 | void Blacklist::AddObserver(Observer* observer) { |
[email protected] | 54ee819 | 2014-03-29 17:37:24 | [diff] [blame] | 324 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 325 | observers_.AddObserver(observer); |
| 326 | } |
| 327 | |
| 328 | void Blacklist::RemoveObserver(Observer* observer) { |
[email protected] | 54ee819 | 2014-03-29 17:37:24 | [diff] [blame] | 329 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 330 | observers_.RemoveObserver(observer); |
| 331 | } |
| 332 | |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 333 | // static |
| 334 | void Blacklist::SetDatabaseManager( |
| 335 | scoped_refptr<SafeBrowsingDatabaseManager> database_manager) { |
| 336 | g_database_manager.Get().set(database_manager); |
| 337 | } |
| 338 | |
| 339 | // static |
| 340 | scoped_refptr<SafeBrowsingDatabaseManager> Blacklist::GetDatabaseManager() { |
| 341 | return g_database_manager.Get().get(); |
| 342 | } |
| 343 | |
Avi Drissman | e222a56 | 2018-03-27 03:25:48 | [diff] [blame] | 344 | void Blacklist::ObserveNewDatabase() { |
| 345 | auto database_manager = GetDatabaseManager(); |
| 346 | if (database_manager.get()) { |
| 347 | // Using base::Unretained is safe because when this object goes away, the |
| 348 | // subscription to the callback list will automatically be destroyed. |
| 349 | database_updated_subscription_ = |
| 350 | database_manager.get()->RegisterDatabaseUpdatedCallback( |
| 351 | base::BindRepeating(&Blacklist::NotifyObservers, |
| 352 | base::Unretained(this))); |
| 353 | } else { |
| 354 | database_updated_subscription_.reset(); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | void Blacklist::NotifyObservers() { |
ericwilligers | b5f79de | 2016-10-19 04:15:10 | [diff] [blame] | 359 | for (auto& observer : observers_) |
| 360 | observer.OnBlacklistUpdated(); |
[email protected] | 3e72ed75 | 2013-02-02 00:47:47 | [diff] [blame] | 361 | } |
| 362 | |
[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 363 | } // namespace extensions |