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_SAFE_BROWSING_CORE_DB_ALLOWLIST_CHECKER_CLIENT_H_ 6 #define COMPONENTS_SAFE_BROWSING_CORE_DB_ALLOWLIST_CHECKER_CLIENT_H_ 7 8 #include "base/callback.h" 9 #include "base/memory/scoped_refptr.h" 10 #include "base/memory/weak_ptr.h" 11 #include "base/timer/timer.h" 12 #include "components/safe_browsing/core/db/database_manager.h" 13 #include "url/gurl.h" 14 15 namespace safe_browsing { 16 17 // This provides a simpler interface to 18 // SafeBrowsingDatabaseManager::CheckCsdWhitelistUrl() for callers that 19 // don't want to track their own clients. 20 21 class AllowlistCheckerClient : public SafeBrowsingDatabaseManager::Client { 22 public: 23 using BoolCallback = base::OnceCallback<void(bool /* is_whitelisted */)>; 24 25 // Static method to lookup |url| on the CSD allowlist. |callback| will be 26 // called when the lookup result is known, or on time out, or if the 27 // |database_manager| gets shut down, whichever happens first. 28 // Must be called on IO thread. 29 static void StartCheckCsdWhitelist( 30 scoped_refptr<SafeBrowsingDatabaseManager> database_manager, 31 const GURL& url, 32 BoolCallback callback_for_result); 33 34 // Static method to lookup |url| on the high confidence allowlist. |callback| 35 // will be called when the lookup result is known, or on time out, or if the 36 // |database_manager| gets shut down, whichever happens first. 37 // Must be called on IO thread. 38 static void StartCheckHighConfidenceAllowlist( 39 scoped_refptr<SafeBrowsingDatabaseManager> database_manager, 40 const GURL& url, 41 BoolCallback callback_for_result); 42 43 // public constructor for use with std::make_unique 44 AllowlistCheckerClient( 45 BoolCallback callback_for_result, 46 scoped_refptr<SafeBrowsingDatabaseManager> database_manager, 47 bool default_does_match_allowlist); 48 49 ~AllowlistCheckerClient() override; 50 51 // SafeBrowsingDatabaseMananger::Client impl 52 void OnCheckWhitelistUrlResult(bool is_whitelisted) override; 53 void OnCheckUrlForHighConfidenceAllowlist(bool did_match_allowlist) override; 54 55 private: 56 // Helper method to instantiate a AllowlistCheckerClient object. 57 static std::unique_ptr<AllowlistCheckerClient> GetAllowlistCheckerClient( 58 scoped_refptr<SafeBrowsingDatabaseManager> database_manager, 59 const GURL& url, 60 base::OnceCallback<void(bool)>* callback_for_result, 61 bool default_does_match_allowlist); 62 63 // Invokes |callback_for_result_| if the allowlist lookup completed 64 // synchronously i.e if |match| is |MATCH| or |NO_MATCH|. If, however, |match| 65 // is |ASYNC|, it releases the ownership of |client| so that it can be deleted 66 // in |OnCheckUrlResult| later. 67 static void InvokeCallbackOrRelease( 68 AsyncMatch match, 69 std::unique_ptr<AllowlistCheckerClient> client); 70 71 AllowlistCheckerClient() = delete; 72 73 // Calls the |callback_for_result_| with the result of the lookup or timeout. 74 void OnCheckUrlResult(bool did_match_allowlist); 75 76 // Called when the call to CheckCsdWhitelistUrl times out. 77 void OnTimeout(); 78 79 // For setting up timeout behavior. 80 base::OneShotTimer timer_; 81 82 // The method to call when the match result is known. 83 BoolCallback callback_for_result_; 84 85 scoped_refptr<SafeBrowsingDatabaseManager> database_manager_; 86 87 // Whether to report allowlist match in any of the following cases: 88 // a) On timeout, or 89 // b) If the list is unavailable. 90 bool default_does_match_allowlist_; 91 92 base::WeakPtrFactory<AllowlistCheckerClient> weak_factory_{this}; 93 }; 94 95 } // namespace safe_browsing 96 97 #endif // COMPONENTS_SAFE_BROWSING_CORE_DB_ALLOWLIST_CHECKER_CLIENT_H_ 98