OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 #ifndef CHROME_BROWSER_SAFE_SEARCH_API_SAFE_SEARCH_URL_CHECKER_H_ |
| 6 #define CHROME_BROWSER_SAFE_SEARCH_API_SAFE_SEARCH_URL_CHECKER_H_ |
| 7 |
| 8 #include <stddef.h> |
| 9 |
| 10 #include "base/callback_forward.h" |
| 11 #include "base/containers/mru_cache.h" |
| 12 #include "base/macros.h" |
| 13 #include "base/memory/scoped_vector.h" |
| 14 #include "base/time/time.h" |
| 15 #include "net/url_request/url_fetcher_delegate.h" |
| 16 #include "url/gurl.h" |
| 17 |
| 18 namespace net { |
| 19 class URLFetcher; |
| 20 class URLRequestContextGetter; |
| 21 } |
| 22 |
| 23 // This class uses the SafeSearch API to check the SafeSearch classification |
| 24 // of the content on a given URL and returns the result asynchronously |
| 25 // via a callback. |
| 26 class SafeSearchURLChecker : net::URLFetcherDelegate { |
| 27 public: |
| 28 enum class Classification { SAFE, UNSAFE }; |
| 29 |
| 30 // Returns whether |url| should be blocked. Called from CheckURL. |
| 31 using CheckCallback = base::Callback< |
| 32 void(const GURL&, Classification classification, bool /* uncertain */)>; |
| 33 |
| 34 explicit SafeSearchURLChecker(net::URLRequestContextGetter* context); |
| 35 SafeSearchURLChecker(net::URLRequestContextGetter* context, |
| 36 size_t cache_size); |
| 37 ~SafeSearchURLChecker() override; |
| 38 |
| 39 // Returns whether |callback| was run synchronously. |
| 40 bool CheckURL(const GURL& url, const CheckCallback& callback); |
| 41 |
| 42 void SetCacheTimeoutForTesting(const base::TimeDelta& timeout) { |
| 43 cache_timeout_ = timeout; |
| 44 } |
| 45 |
| 46 private: |
| 47 struct Check; |
| 48 struct CheckResult { |
| 49 CheckResult(Classification classification, bool uncertain); |
| 50 Classification classification; |
| 51 bool uncertain; |
| 52 base::TimeTicks timestamp; |
| 53 }; |
| 54 |
| 55 // net::URLFetcherDelegate implementation. |
| 56 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 57 |
| 58 net::URLRequestContextGetter* context_; |
| 59 |
| 60 ScopedVector<Check> checks_in_progress_; |
| 61 |
| 62 base::MRUCache<GURL, CheckResult> cache_; |
| 63 base::TimeDelta cache_timeout_; |
| 64 |
| 65 DISALLOW_COPY_AND_ASSIGN(SafeSearchURLChecker); |
| 66 }; |
| 67 |
| 68 #endif // CHROME_BROWSER_SAFE_SEARCH_API_SAFE_SEARCH_URL_CHECKER_H_ |
OLD | NEW |