aw_safe_browsing_allowlist_manager.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #ifndef ANDROID_WEBVIEW_BROWSER_SAFE_BROWSING_AW_SAFE_BROWSING_ALLOWLIST_MANAGER_H_
  5. #define ANDROID_WEBVIEW_BROWSER_SAFE_BROWSING_AW_SAFE_BROWSING_ALLOWLIST_MANAGER_H_
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "base/callback_forward.h"
  10. #include "base/memory/ref_counted.h"
  11. #include "url/gurl.h"
  12. namespace base {
  13. class SequencedTaskRunner;
  14. } // namespace base
  15. namespace android_webview {
  16. struct TrieNode;
  17. // This class tracks the allowlisting policies for Safebrowsing. The class
  18. // interacts with UI thread, where the allowlist is set, and then checks
  19. // for the URLs in IO thread. The allowed entries are not checked for
  20. // Safebrowsing.
  21. //
  22. // The class must be constructed on the UI thread.
  23. //
  24. // Update allowlist tasks from the UI thread are post to the IO thread.
  25. //
  26. // Encoding and the allowlisting rules:
  27. // The allowlist is set in Java and plumbed to native through JNI, making
  28. // them UTF-8 encoded wide strings.
  29. //
  30. // Each rule should take one of these:
  31. // HOSTNAME
  32. // .HOSTNAME
  33. // IPV4_LITERAL
  34. // IPV6_LITERAL_WITH_BRACKETS
  35. //
  36. // All other rules, including wildcards, are invalid.
  37. //
  38. // The hostname with a leading dot means an exact match, otherwise subdomains
  39. // are also matched. This particular rule is similar to admiministration
  40. // policy format:
  41. // https://www.chromium.org/administrators/url-blocklist-filter-format
  42. //
  43. // The expected number of entries on the list should be 100s at most, however
  44. // the size is not enforced here. The list size can be enforced at
  45. // Java level if necessary.
  46. //
  47. class AwSafeBrowsingAllowlistManager {
  48. public:
  49. // Must be constructed on the UI thread.
  50. // |background_task_runner| is used to build the filter list in a background
  51. // thread.
  52. // |io_task_runner| must be backed by the IO thread.
  53. AwSafeBrowsingAllowlistManager(
  54. const scoped_refptr<base::SequencedTaskRunner>& background_task_runner,
  55. const scoped_refptr<base::SequencedTaskRunner>& io_task_runner);
  56. AwSafeBrowsingAllowlistManager(const AwSafeBrowsingAllowlistManager&) =
  57. delete;
  58. AwSafeBrowsingAllowlistManager& operator=(
  59. const AwSafeBrowsingAllowlistManager&) = delete;
  60. virtual ~AwSafeBrowsingAllowlistManager();
  61. // Returns true if |url| is allowed by the current allowlist. Must be
  62. // called from the IO thread.
  63. bool IsUrlAllowed(const GURL& url) const;
  64. // Replace the current host allowlist with a new one.
  65. void SetAllowlistOnUIThread(std::vector<std::string>&& rules,
  66. base::OnceCallback<void(bool)> callback);
  67. private:
  68. // Builds allowlist on background thread.
  69. void BuildAllowlist(const std::vector<std::string>& rules,
  70. base::OnceCallback<void(bool)> callback);
  71. // Replaces the current allowlist. Must be called on the IO thread.
  72. void SetAllowlist(std::unique_ptr<TrieNode> allowlist);
  73. scoped_refptr<base::SequencedTaskRunner> background_task_runner_;
  74. scoped_refptr<base::SequencedTaskRunner> io_task_runner_;
  75. scoped_refptr<base::SequencedTaskRunner> ui_task_runner_;
  76. std::unique_ptr<TrieNode> allowlist_;
  77. };
  78. } // namespace android_webview
  79. #endif // ANDROID_WEBVIEW_BROWSER_SAFE_BROWSING_AW_SAFE_BROWSING_ALLOWLIST_MANAGER_H_