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_BROWSING_CLIENT_H_ 6 #define COMPONENTS_SUBRESOURCE_FILTER_CONTENT_BROWSER_SUBRESOURCE_FILTER_SAFE_BROWSING_CLIENT_H_ 7 8 #include <stddef.h> 9 10 #include <memory> 11 12 #include "base/containers/flat_map.h" 13 #include "base/macros.h" 14 #include "base/memory/ref_counted.h" 15 #include "base/memory/weak_ptr.h" 16 #include "base/time/time.h" 17 #include "components/safe_browsing/core/db/util.h" 18 19 class GURL; 20 21 namespace base { 22 class SingleThreadTaskRunner; 23 namespace trace_event { 24 class TracedValue; 25 } // namespace trace_event 26 } // namespace base 27 28 namespace safe_browsing { 29 class SafeBrowsingDatabaseManager; 30 } // namespace safe_browsing 31 32 namespace subresource_filter { 33 34 class SubresourceFilterSafeBrowsingActivationThrottle; 35 class SubresourceFilterSafeBrowsingClientRequest; 36 37 // Created on the UI thread but used on the IO thread to communicate with the 38 // safe browsing service. 39 // 40 // The class is expected to accompany a single navigation, and can maintain many 41 // database requests. It will cancel any outgoing requests when it is destroyed. 42 class SubresourceFilterSafeBrowsingClient { 43 public: 44 struct CheckResult { 45 size_t request_id = 0; 46 safe_browsing::SBThreatType threat_type = 47 safe_browsing::SBThreatType::SB_THREAT_TYPE_SAFE; 48 49 // The metadata should generally be lightweight enough to copy around 50 // without performance implications. Refactor this class if that ever 51 // changes. 52 safe_browsing::ThreatMetadata threat_metadata; 53 base::TimeTicks start_time; 54 bool finished = false; 55 56 std::unique_ptr<base::trace_event::TracedValue> ToTracedValue() const; 57 }; 58 59 SubresourceFilterSafeBrowsingClient( 60 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> 61 database_manager, 62 base::WeakPtr<SubresourceFilterSafeBrowsingActivationThrottle> throttle, 63 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, 64 scoped_refptr<base::SingleThreadTaskRunner> throttle_task_runner); 65 66 ~SubresourceFilterSafeBrowsingClient(); 67 68 void CheckUrlOnIO(const GURL& url, 69 size_t request_id, 70 base::TimeTicks start_time); 71 72 void OnCheckBrowseUrlResult( 73 SubresourceFilterSafeBrowsingClientRequest* request, 74 const CheckResult& check_result); 75 76 private: 77 // This is stored as a map to allow for ergonomic deletion. 78 base::flat_map<SubresourceFilterSafeBrowsingClientRequest*, 79 std::unique_ptr<SubresourceFilterSafeBrowsingClientRequest>> 80 requests_; 81 82 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> database_manager_; 83 84 base::WeakPtr<SubresourceFilterSafeBrowsingActivationThrottle> throttle_; 85 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; 86 scoped_refptr<base::SingleThreadTaskRunner> throttle_task_runner_; 87 88 DISALLOW_COPY_AND_ASSIGN(SubresourceFilterSafeBrowsingClient); 89 }; 90 91 } // namespace subresource_filter 92 93 #endif // COMPONENTS_SUBRESOURCE_FILTER_CONTENT_BROWSER_SUBRESOURCE_FILTER_SAFE_BROWSING_CLIENT_H_ 94