safe_browsing_api_handler_bridge.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2016 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. // Glue to pass Safe Browsing API requests between Chrome and GMSCore.
  6. #ifndef COMPONENTS_SAFE_BROWSING_ANDROID_SAFE_BROWSING_API_HANDLER_BRIDGE_H_
  7. #define COMPONENTS_SAFE_BROWSING_ANDROID_SAFE_BROWSING_API_HANDLER_BRIDGE_H_
  8. #include <jni.h>
  9. #include "base/android/jni_android.h"
  10. #include "base/callback.h"
  11. #include "components/safe_browsing/core/browser/db/v4_protocol_manager_util.h"
  12. class GURL;
  13. namespace safe_browsing {
  14. class UrlCheckInterceptor;
  15. struct ThreatMetadata;
  16. class SafeBrowsingApiHandlerBridge {
  17. public:
  18. using ResponseCallback =
  19. base::OnceCallback<void(SBThreatType, const ThreatMetadata&)>;
  20. SafeBrowsingApiHandlerBridge() = default;
  21. ~SafeBrowsingApiHandlerBridge();
  22. SafeBrowsingApiHandlerBridge(const SafeBrowsingApiHandlerBridge&) = delete;
  23. SafeBrowsingApiHandlerBridge& operator=(const SafeBrowsingApiHandlerBridge&) =
  24. delete;
  25. // Returns a reference to the singleton.
  26. static SafeBrowsingApiHandlerBridge& GetInstance();
  27. // Makes Native-to-Java call to check the URL against Safe Browsing lists.
  28. void StartURLCheck(std::unique_ptr<ResponseCallback> callback,
  29. const GURL& url,
  30. const SBThreatTypeSet& threat_types);
  31. bool StartCSDAllowlistCheck(const GURL& url);
  32. bool StartHighConfidenceAllowlistCheck(const GURL& url);
  33. void SetInterceptorForTesting(UrlCheckInterceptor* interceptor) {
  34. interceptor_for_testing_ = interceptor;
  35. }
  36. private:
  37. // Used as a key to identify unique requests sent to Java to get Safe Browsing
  38. // reputation from GmsCore.
  39. jlong next_callback_id_ = 0;
  40. UrlCheckInterceptor* interceptor_for_testing_ = nullptr;
  41. };
  42. // Interface allowing simplified interception of calls to
  43. // SafeBrowsingApiHandlerBridge. Intended for use only in tests.
  44. class UrlCheckInterceptor {
  45. public:
  46. virtual ~UrlCheckInterceptor(){};
  47. virtual void Check(
  48. std::unique_ptr<SafeBrowsingApiHandlerBridge::ResponseCallback> callback,
  49. const GURL& url) const = 0;
  50. };
  51. } // namespace safe_browsing
  52. #endif // COMPONENTS_SAFE_BROWSING_ANDROID_SAFE_BROWSING_API_HANDLER_BRIDGE_H_