OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 COMPONENTS_SUBRESOURCE_FILTER_CONTENT_BROWSER_SUBRESOURCE_FILTER_SAFE_BR
OWSING_CLIENT_H_ |
| 6 #define COMPONENTS_SUBRESOURCE_FILTER_CONTENT_BROWSER_SUBRESOURCE_FILTER_SAFE_BR
OWSING_CLIENT_H_ |
| 7 |
| 8 #include <memory> |
| 9 |
| 10 #include "base/macros.h" |
| 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/weak_ptr.h" |
| 13 #include "components/safe_browsing_db/v4_local_database_manager.h" |
| 14 #include "url/gurl.h" |
| 15 |
| 16 namespace base { |
| 17 class SingleThreadTaskRunner; |
| 18 class Timer; |
| 19 } // namespace base |
| 20 |
| 21 namespace subresource_filter { |
| 22 |
| 23 class SubresourceFilterSafeBrowsingActivationThrottle; |
| 24 class SubresourceFilterSafeBrowsingClientRequest; |
| 25 |
| 26 // Created on the UI thread but used on the IO thread to communicate with the |
| 27 // safe browsing service. |
| 28 // |
| 29 // The class is expected to accompany a single navigation. If a check request |
| 30 // comes in for URL B while URL A is in flight, we cancel the check to URL A. |
| 31 // |
| 32 // Consumers of the class need some way of determining the order of requests, so |
| 33 // they send in request_ids which are paired with each check. |
| 34 class SubresourceFilterSafeBrowsingClient { |
| 35 public: |
| 36 SubresourceFilterSafeBrowsingClient( |
| 37 std::unique_ptr<base::Timer> timer, |
| 38 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> |
| 39 database_manager, |
| 40 const base::WeakPtr<SubresourceFilterSafeBrowsingActivationThrottle>& |
| 41 throttle, |
| 42 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner); |
| 43 |
| 44 ~SubresourceFilterSafeBrowsingClient(); |
| 45 |
| 46 void CheckUrlOnIO(const GURL& url, int request_id); |
| 47 |
| 48 void OnCheckBrowseUrlResult( |
| 49 SubresourceFilterSafeBrowsingClientRequest* request, |
| 50 safe_browsing::SBThreatType threat_type, |
| 51 const safe_browsing::ThreatMetadata& metadata); |
| 52 |
| 53 private: |
| 54 // Only set from the default for testing. |
| 55 std::unique_ptr<base::Timer> timer_; |
| 56 std::unique_ptr<SubresourceFilterSafeBrowsingClientRequest> current_request_; |
| 57 |
| 58 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> database_manager_; |
| 59 |
| 60 base::WeakPtr<SubresourceFilterSafeBrowsingActivationThrottle> throttle_; |
| 61 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; |
| 62 |
| 63 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterSafeBrowsingClient); |
| 64 }; |
| 65 |
| 66 // This class is scoped to a single database check, and it lives on the IO |
| 67 // thread exclusively. |
| 68 class SubresourceFilterSafeBrowsingClientRequest |
| 69 : public safe_browsing::SafeBrowsingDatabaseManager::Client { |
| 70 public: |
| 71 SubresourceFilterSafeBrowsingClientRequest( |
| 72 base::Timer* timer, |
| 73 const GURL& url, |
| 74 int request_id, |
| 75 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> |
| 76 database_manager, |
| 77 SubresourceFilterSafeBrowsingClient* client); |
| 78 ~SubresourceFilterSafeBrowsingClientRequest() override; |
| 79 |
| 80 void Start(); |
| 81 |
| 82 void OnCheckBrowseUrlResult( |
| 83 const GURL& url, |
| 84 safe_browsing::SBThreatType threat_type, |
| 85 const safe_browsing::ThreatMetadata& metadata) override; |
| 86 |
| 87 const GURL& url() const { return url_; } |
| 88 |
| 89 int request_id() const { return request_id_; } |
| 90 |
| 91 // Maximum time in milliseconds to wait for the Safe Browsing service to |
| 92 // verify a URL. After this amount of time the outstanding check will be |
| 93 // aborted, and the URL will be treated as if it didn't belong to the |
| 94 // Subresource Filter only list. |
| 95 static constexpr base::TimeDelta kCheckURLTimeout = |
| 96 base::TimeDelta::FromSeconds(5); |
| 97 |
| 98 private: |
| 99 // Callback for when the safe browsing check has taken longer than |
| 100 // kCheckURLTimeout. |
| 101 void OnCheckUrlTimeout(); |
| 102 |
| 103 const GURL url_; |
| 104 const int request_id_; |
| 105 |
| 106 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> database_manager_; |
| 107 SubresourceFilterSafeBrowsingClient* client_ = nullptr; |
| 108 |
| 109 // Timer to abort the safe browsing check if it takes too long. Must outlive |
| 110 // this class. |
| 111 base::Timer* timer_; |
| 112 |
| 113 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterSafeBrowsingClientRequest); |
| 114 }; |
| 115 |
| 116 } // namespace subresource_filter |
| 117 |
| 118 #endif // COMPONENTS_SUBRESOURCE_FILTER_CONTENT_BROWSER_SUBRESOURCE_FILTER_SAFE
_BROWSING_CLIENT_H_ |
OLD | NEW |