opt_out_blocklist.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Copyright 2018 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 COMPONENTS_BLOCKLIST_OPT_OUT_BLOCKLIST_OPT_OUT_BLOCKLIST_H_
  5. #define COMPONENTS_BLOCKLIST_OPT_OUT_BLOCKLIST_OPT_OUT_BLOCKLIST_H_
  6. #include <stdint.h>
  7. #include <map>
  8. #include <memory>
  9. #include <string>
  10. #include <vector>
  11. #include "base/callback.h"
  12. #include "base/containers/queue.h"
  13. #include "base/memory/raw_ptr.h"
  14. #include "base/memory/weak_ptr.h"
  15. #include "base/sequence_checker.h"
  16. #include "base/time/time.h"
  17. #include "components/blocklist/opt_out_blocklist/opt_out_blocklist_data.h"
  18. namespace base {
  19. class Clock;
  20. }
  21. namespace blocklist {
  22. class BlocklistData;
  23. class OptOutBlocklistDelegate;
  24. class OptOutStore;
  25. class OptOutBlocklist {
  26. public:
  27. // |opt_out_store| is the backing store to retrieve and store blocklist
  28. // information, and can be null. When |opt_out_store| is null, the in-memory
  29. // data will be immediately loaded to empty. If |opt_out_store| is non-null,
  30. // it will be used to load the in-memory map asynchronously.
  31. // |blocklist_delegate| is a single object listening for blocklist events, and
  32. // it is guaranteed to outlive the life time of |this|.
  33. OptOutBlocklist(std::unique_ptr<OptOutStore> opt_out_store,
  34. base::Clock* clock,
  35. OptOutBlocklistDelegate* blocklist_delegate);
  36. OptOutBlocklist(const OptOutBlocklist&) = delete;
  37. OptOutBlocklist& operator=(const OptOutBlocklist&) = delete;
  38. virtual ~OptOutBlocklist();
  39. // Creates the BlocklistData that backs the blocklist.
  40. void Init();
  41. // Asynchronously deletes all entries in the in-memory blocklist. Informs
  42. // the backing store to delete entries between |begin_time| and |end_time|,
  43. // and reloads entries into memory from the backing store. If the embedder
  44. // passed in a null store, resets all history in the in-memory blocklist.
  45. void ClearBlockList(base::Time begin_time, base::Time end_time);
  46. // Asynchronously adds a new navigation to to the in-memory blocklist and
  47. // backing store. |opt_out| is whether the user opted out of the action. If
  48. // the in memory map has reached the max number of hosts allowed, and
  49. // |host_name| is a new host, a host will be evicted based on recency of the
  50. // hosts most recent opt out. It returns the time used for recording the
  51. // moment when the navigation is added for logging.
  52. base::Time AddEntry(const std::string& host_name, bool opt_out, int type);
  53. // Synchronously determines if the action should be allowed for |host_name|
  54. // and |type|. Returns the reason the blocklist disallowed the action, or
  55. // kAllowed if the action is allowed. Record checked reasons in
  56. // |passed_reasons|. |ignore_long_term_block_list_rules| will cause session,
  57. // type, and host rules, but the session rule will still be queried.
  58. BlocklistReason IsLoadedAndAllowed(
  59. const std::string& host_name,
  60. int type,
  61. bool ignore_long_term_block_list_rules,
  62. std::vector<BlocklistReason>* passed_reasons) const;
  63. protected:
  64. // Whether the session rule should be enabled. |duration| specifies how long a
  65. // user remains blocklisted. |history| specifies how many entries should be
  66. // evaluated; |threshold| specifies how many opt outs would cause
  67. // blocklisting. I.e., the most recent |history| are looked at and if
  68. // |threshold| (or more) of them are opt outs, the user is considered
  69. // blocklisted unless the most recent opt out was longer than |duration| ago.
  70. // This rule only considers entries within this session (it does not use the
  71. // data that was persisted in previous sessions). When the blocklist is
  72. // cleared, this rule is reset as if it were a new session. Queried in Init().
  73. virtual bool ShouldUseSessionPolicy(base::TimeDelta* duration,
  74. size_t* history,
  75. int* threshold) const = 0;
  76. // Whether the persistent rule should be enabled. |duration| specifies how
  77. // long a user remains blocklisted. |history| specifies how many entries
  78. // should be evaluated; |threshold| specifies how many opt outs would cause
  79. // blocklisting. I.e., the most recent |history| are looked at and if
  80. // |threshold| (or more) of them are opt outs, the user is considered
  81. // blocklisted unless the most recent opt out was longer than |duration| ago.
  82. // Queried in Init().
  83. virtual bool ShouldUsePersistentPolicy(base::TimeDelta* duration,
  84. size_t* history,
  85. int* threshold) const = 0;
  86. // Whether the host rule should be enabled. |duration| specifies how long a
  87. // host remains blocklisted. |history| specifies how many entries should be
  88. // evaluated per host; |threshold| specifies how many opt outs would cause
  89. // blocklisting. I.e., the most recent |history| entries per host are looked
  90. // at and if |threshold| (or more) of them are opt outs, the host is
  91. // considered blocklisted unless the most recent opt out was longer than
  92. // |duration| ago. |max_hosts| will limit the number of hosts stored in this
  93. // class when non-zero.
  94. // Queried in Init().
  95. virtual bool ShouldUseHostPolicy(base::TimeDelta* duration,
  96. size_t* history,
  97. int* threshold,
  98. size_t* max_hosts) const = 0;
  99. // Whether the type rule should be enabled. |duration| specifies how long a
  100. // type remains blocklisted. |history| specifies how many entries should be
  101. // evaluated per type; |threshold| specifies how many opt outs would cause
  102. // blocklisting.
  103. // I.e., the most recent |history| entries per type are looked at and if
  104. // |threshold| (or more) of them are opt outs, the type is considered
  105. // blocklisted unless the most recent opt out was longer than |duration| ago.
  106. // Queried in Init().
  107. virtual bool ShouldUseTypePolicy(base::TimeDelta* duration,
  108. size_t* history,
  109. int* threshold) const = 0;
  110. // The allowed types and what version they are. Should be empty unless the
  111. // caller will not be using the blocklist in the session. It is used to remove
  112. // stale entries from the database and to DCHECK that other methods are not
  113. // using disallowed types. Queried in Init().
  114. virtual BlocklistData::AllowedTypesAndVersions GetAllowedTypes() const = 0;
  115. private:
  116. // Synchronous version of AddEntry method. |time| is the time
  117. // stamp of when the navigation was determined to be an opt-out or non-opt
  118. // out.
  119. void AddEntrySync(const std::string& host_name,
  120. bool opt_out,
  121. int type,
  122. base::Time time);
  123. // Synchronous version of ClearBlockList method.
  124. void ClearBlockListSync(base::Time begin_time, base::Time end_time);
  125. // Callback passed to the backing store when loading block list information.
  126. // Takes ownership of |blocklist_data|.
  127. void LoadBlockListDone(std::unique_ptr<BlocklistData> blocklist_data);
  128. // Called while waiting for the blocklist to be loaded from the backing
  129. // store.
  130. // Enqueues a task to run when when loading blocklist information has
  131. // completed. Maintains the order that tasks were called in.
  132. void QueuePendingTask(base::OnceClosure callback);
  133. // An in-memory representation of the various rules of the blocklist. This is
  134. // null while reading from the backing store.
  135. std::unique_ptr<BlocklistData> blocklist_data_;
  136. // Whether the blocklist is done being loaded from the backing store.
  137. bool loaded_;
  138. // The backing store of the blocklist information.
  139. std::unique_ptr<OptOutStore> opt_out_store_;
  140. // Callbacks to be run after loading information from the backing store has
  141. // completed.
  142. base::queue<base::OnceClosure> pending_callbacks_;
  143. raw_ptr<base::Clock> clock_;
  144. // The delegate listening to this blocklist. |blocklist_delegate_| lifetime is
  145. // guaranteed to outlive |this|.
  146. raw_ptr<OptOutBlocklistDelegate> blocklist_delegate_;
  147. SEQUENCE_CHECKER(sequence_checker_);
  148. base::WeakPtrFactory<OptOutBlocklist> weak_factory_{this};
  149. };
  150. } // namespace blocklist
  151. #endif // COMPONENTS_BLOCKLIST_OPT_OUT_BLOCKLIST_OPT_OUT_BLOCKLIST_H_