opt_out_blocklist_item.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #ifndef COMPONENTS_BLOCKLIST_OPT_OUT_BLOCKLIST_OPT_OUT_BLOCKLIST_ITEM_H_
  5. #define COMPONENTS_BLOCKLIST_OPT_OUT_BLOCKLIST_OPT_OUT_BLOCKLIST_ITEM_H_
  6. #include <stdint.h>
  7. #include <map>
  8. #include <memory>
  9. #include <queue>
  10. #include "base/callback.h"
  11. #include "base/time/time.h"
  12. #include "third_party/abseil-cpp/absl/types/optional.h"
  13. namespace blocklist {
  14. // Stores the recent block list history for a single host. Stores
  15. // |stored_history_length| of the most recent actions. To determine action
  16. // eligibility fewer than |opt_out_block_list_threshold| out of the past
  17. // |stored_history_length| navigations must be opt outs. |block_list_duration|
  18. // is the amount of time that elapses until the host is no longer on the block
  19. // list.
  20. class OptOutBlocklistItem {
  21. public:
  22. OptOutBlocklistItem(size_t stored_history_length,
  23. int opt_out_block_list_threshold,
  24. base::TimeDelta block_list_duration);
  25. OptOutBlocklistItem(const OptOutBlocklistItem&) = delete;
  26. OptOutBlocklistItem& operator=(const OptOutBlocklistItem&) = delete;
  27. ~OptOutBlocklistItem();
  28. // Adds a new navigation at the specified |entry_time|.
  29. void AddEntry(bool opt_out, base::Time entry_time);
  30. // Whether the action corresponding to |this| should be disallowed.
  31. bool IsBlockListed(base::Time now) const;
  32. absl::optional<base::Time> most_recent_opt_out_time() const {
  33. return most_recent_opt_out_time_;
  34. }
  35. size_t OptOutRecordsSizeForTesting() const;
  36. private:
  37. // An action to |this| is represented by time and whether the action was an
  38. // opt out.
  39. class OptOutRecord {
  40. public:
  41. OptOutRecord(base::Time entry_time, bool opt_out);
  42. OptOutRecord(const OptOutRecord&) = delete;
  43. OptOutRecord& operator=(const OptOutRecord&) = delete;
  44. ~OptOutRecord();
  45. OptOutRecord(OptOutRecord&&) noexcept;
  46. OptOutRecord& operator=(OptOutRecord&&) noexcept;
  47. // Used to determine eviction priority.
  48. bool operator<(const OptOutRecord& other) const;
  49. // The time that the opt out state was determined.
  50. base::Time entry_time() const { return entry_time_; }
  51. // Whether the user opted out of the action.
  52. bool opt_out() const { return opt_out_; }
  53. private:
  54. // The time that the opt out state was determined.
  55. base::Time entry_time_;
  56. // Whether the user opted out of the action.
  57. bool opt_out_;
  58. };
  59. // The number of entries to store to determine action eligibility.
  60. const size_t max_stored_history_length_;
  61. // The number opt outs in recent history that will trigger blocklisting.
  62. const int opt_out_block_list_threshold_;
  63. // The amount of time to block list a domain after the most recent opt out.
  64. const base::TimeDelta max_block_list_duration_;
  65. // The |max_stored_history_length_| most recent action. Is maintained as a
  66. // priority queue that has high priority for items that should be evicted
  67. // (i.e., they are old).
  68. std::priority_queue<OptOutRecord> opt_out_records_;
  69. // Time of the most recent opt out.
  70. absl::optional<base::Time> most_recent_opt_out_time_;
  71. // The total number of opt outs currently in |opt_out_records_|.
  72. int total_opt_out_;
  73. };
  74. } // namespace blocklist
  75. #endif // COMPONENTS_BLOCKLIST_OPT_OUT_BLOCKLIST_OPT_OUT_BLOCKLIST_ITEM_H_