permission_auditing_service.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2020 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_PERMISSIONS_PERMISSION_AUDITING_SERVICE_H_
  5. #define COMPONENTS_PERMISSIONS_PERMISSION_AUDITING_SERVICE_H_
  6. #include "base/callback_forward.h"
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/memory/scoped_refptr.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/task/sequenced_task_runner.h"
  11. #include "base/timer/timer.h"
  12. #include "components/content_settings/core/common/content_settings_types.h"
  13. #include "components/keyed_service/core/keyed_service.h"
  14. #include "url/origin.h"
  15. namespace base {
  16. class FilePath;
  17. class Time;
  18. class TimeDelta;
  19. } // namespace base
  20. namespace permissions {
  21. class PermissionAuditingDatabase;
  22. struct PermissionUsageSession;
  23. // Keeps a client-side log of when websites use permission-gated capabilities to
  24. // allow the user to audit usage.
  25. //
  26. // For each combination of permission type and origin, the history is
  27. // stored on disk as a series of PermissionUsageSessions. Sessions expire
  28. // at the latest after 3 months, or when browsing data or history is cleared.
  29. class PermissionAuditingService
  30. : public KeyedService,
  31. public base::SupportsWeakPtr<PermissionAuditingService> {
  32. public:
  33. typedef base::OnceCallback<void(std::vector<PermissionUsageSession>)>
  34. PermissionUsageHistoryCallback;
  35. typedef base::OnceCallback<void(absl::optional<base::Time>)>
  36. LastPermissionUsageTimeCallback;
  37. explicit PermissionAuditingService(
  38. scoped_refptr<base::SequencedTaskRunner> backend_task_runner);
  39. ~PermissionAuditingService() override;
  40. // Initializes Permission Auditing database in `database_path`.
  41. void Init(const base::FilePath& database_path);
  42. // Starts the periodic deletions of outdated sessions.
  43. void StartPeriodicCullingOfExpiredSessions();
  44. // Appends a new permission usage `session` of the given permission `type` on
  45. // a given `origin`. `session` must be valid according to IsValid().
  46. // Operation will fail if a session with the same primary key, that is,
  47. // origin, type, and usage start time, already exists.
  48. void StorePermissionUsage(const PermissionUsageSession& session);
  49. // Returns the detailed history stored for the permission `type` on a given
  50. // `origin` from the specified `start_time` inclusive. The `origin` must not
  51. // be opaque. History is provided via `result_callback`. History isn't ordered
  52. // in any way.
  53. void GetPermissionUsageHistory(
  54. ContentSettingsType type,
  55. const url::Origin& origin,
  56. base::Time start_time,
  57. PermissionUsageHistoryCallback result_callback);
  58. // Returns when the given permission `type` was last used on a given `origin`.
  59. // The `origin` must not be opaque. Time is provided via `result_callback`.
  60. void GetLastPermissionUsageTime(
  61. ContentSettingsType type,
  62. const url::Origin& origin,
  63. LastPermissionUsageTimeCallback result_callback);
  64. // Updates the usage end time for a specific usage session. The session is
  65. // identified by the primary key {`type`, `origin`, `start_time`}, and must
  66. // already exist. `start_time` and `new_end_time` must be not null, and
  67. // `start_time` must be less than or equal to `new_end_time`.
  68. void UpdateEndTime(ContentSettingsType type,
  69. const url::Origin& origin,
  70. base::Time start_time,
  71. base::Time new_end_time);
  72. // Deletes permission usage sessions, which started or ended in the given
  73. // time range inclusive. A null `start_time` or `end_time` time is treated as
  74. // -inf and +inf, respectively.
  75. void DeleteSessionsBetween(base::Time start, base::Time end);
  76. // Returns sessions maximum lifetime.
  77. static base::TimeDelta GetUsageSessionMaxAge();
  78. // Returns the time delta between two consequent expiration iterations.
  79. static base::TimeDelta GetUsageSessionCullingInterval();
  80. private:
  81. void ExpireOldSessions();
  82. scoped_refptr<base::SequencedTaskRunner> backend_task_runner_;
  83. // Lives on the |backend_task_runner_|, and must only be accessed on that
  84. // sequence. It is safe to assume the database is alive as long as |db_| is
  85. // non-null.
  86. raw_ptr<PermissionAuditingDatabase> db_ = nullptr;
  87. base::RepeatingTimer timer_;
  88. };
  89. } // namespace permissions
  90. #endif // COMPONENTS_PERMISSIONS_PERMISSION_AUDITING_SERVICE_H_