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 <map> |
| 9 #include <memory> |
| 10 |
| 11 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/time/time.h" |
| 15 #include "base/timer/timer.h" |
| 16 #include "components/safe_browsing_db/v4_local_database_manager.h" |
| 17 #include "url/gurl.h" |
| 18 |
| 19 namespace base { |
| 20 class SingleThreadTaskRunner; |
| 21 } // namespace base |
| 22 |
| 23 namespace subresource_filter { |
| 24 |
| 25 class SubresourceFilterSafeBrowsingActivationThrottle; |
| 26 class SubresourceFilterSafeBrowsingClientRequest; |
| 27 |
| 28 // Created on the UI thread but used on the IO thread to communicate with the |
| 29 // safe browsing service. |
| 30 // |
| 31 // The class is expected to accompany a single navigation. If a check request |
| 32 // comes in for URL B while URL A is in flight, we cancel the check to URL A. |
| 33 class SubresourceFilterSafeBrowsingClient { |
| 34 public: |
| 35 SubresourceFilterSafeBrowsingClient( |
| 36 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> |
| 37 database_manager, |
| 38 const base::WeakPtr<SubresourceFilterSafeBrowsingActivationThrottle>& |
| 39 throttle, |
| 40 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, |
| 41 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner); |
| 42 |
| 43 ~SubresourceFilterSafeBrowsingClient(); |
| 44 |
| 45 void CheckUrlOnIO(const GURL& url, size_t request_id); |
| 46 |
| 47 void OnCheckBrowseUrlResult( |
| 48 SubresourceFilterSafeBrowsingClientRequest* request, |
| 49 safe_browsing::SBThreatType threat_type, |
| 50 const safe_browsing::ThreatMetadata& metadata); |
| 51 |
| 52 private: |
| 53 // This is stored as a map to allow for ergonomic deletion. |
| 54 std::map<SubresourceFilterSafeBrowsingClientRequest*, |
| 55 std::unique_ptr<SubresourceFilterSafeBrowsingClientRequest>> |
| 56 requests_; |
| 57 |
| 58 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> database_manager_; |
| 59 |
| 60 base::WeakPtr<SubresourceFilterSafeBrowsingActivationThrottle> throttle_; |
| 61 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; |
| 62 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; |
| 63 |
| 64 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterSafeBrowsingClient); |
| 65 }; |
| 66 |
| 67 // This class is scoped to a single database check, and it lives on the IO |
| 68 // thread exclusively. |
| 69 class SubresourceFilterSafeBrowsingClientRequest |
| 70 : public safe_browsing::SafeBrowsingDatabaseManager::Client { |
| 71 public: |
| 72 SubresourceFilterSafeBrowsingClientRequest( |
| 73 const GURL& url, |
| 74 size_t request_id, |
| 75 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> |
| 76 database_manager, |
| 77 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, |
| 78 SubresourceFilterSafeBrowsingClient* client); |
| 79 ~SubresourceFilterSafeBrowsingClientRequest() override; |
| 80 |
| 81 void Start(); |
| 82 |
| 83 void OnCheckBrowseUrlResult( |
| 84 const GURL& url, |
| 85 safe_browsing::SBThreatType threat_type, |
| 86 const safe_browsing::ThreatMetadata& metadata) override; |
| 87 |
| 88 const GURL& url() const { return url_; } |
| 89 size_t 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 size_t 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. |
| 110 base::OneShotTimer timer_; |
| 111 |
| 112 base::TimeTicks start_time_; |
| 113 |
| 114 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterSafeBrowsingClientRequest); |
| 115 }; |
| 116 |
| 117 } // namespace subresource_filter |
| 118 |
| 119 #endif // COMPONENTS_SUBRESOURCE_FILTER_CONTENT_BROWSER_SUBRESOURCE_FILTER_SAFE
_BROWSING_CLIENT_H_ |
OLD | NEW |