permission_auditing_database.h 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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_DATABASE_H_
  5. #define COMPONENTS_PERMISSIONS_PERMISSION_AUDITING_DATABASE_H_
  6. #include <vector>
  7. #include "base/time/time.h"
  8. #include "components/content_settings/core/common/content_settings_types.h"
  9. #include "components/permissions/permission_usage_session.h"
  10. #include "sql/database.h"
  11. #include "third_party/abseil-cpp/absl/types/optional.h"
  12. #include "url/origin.h"
  13. namespace base {
  14. class FilePath;
  15. } // namespace base
  16. namespace permissions {
  17. // Stores permission usage sessions for specific url origin and
  18. // ContentSettingType in an SQLite database. Additionally, handles the queries
  19. // about the last permission usage time for a specific origin.
  20. // Threading constraints:
  21. // 1) This class is not thread-safe, so each instance must be used on the same
  22. // sequence;
  23. // 2) Instances must be used on a sequence that can execute blocking tasks.
  24. class PermissionAuditingDatabase {
  25. public:
  26. PermissionAuditingDatabase();
  27. ~PermissionAuditingDatabase();
  28. PermissionAuditingDatabase(const PermissionAuditingDatabase&) = delete;
  29. PermissionAuditingDatabase& operator=(const PermissionAuditingDatabase&) =
  30. delete;
  31. PermissionAuditingDatabase(PermissionAuditingDatabase&&) = delete;
  32. PermissionAuditingDatabase& operator=(const PermissionAuditingDatabase&&) =
  33. delete;
  34. // Opens an existing database at `path` or creates a new one if none exists,
  35. // and returns true on success.
  36. bool Init(const base::FilePath& path);
  37. // Appends a new permission usage `session` of the given permission `type` on
  38. // a given `origin`. The `session` must be valid according to IsValid().
  39. // Operation will fail if a session with the same primary key, that
  40. // is, origin, type, and usage start time, already exists in the database.
  41. // Returns if the operation was successful.
  42. bool StorePermissionUsage(const PermissionUsageSession& session);
  43. // Returns the detailed history stored for the permission `type` on a given
  44. // `origin` from the specified `start_time`. The `origin` must not be opaque.
  45. std::vector<PermissionUsageSession> GetPermissionUsageHistory(
  46. ContentSettingsType type,
  47. const url::Origin& origin,
  48. base::Time start_time);
  49. // Returns when the given permission `type` was last used on a given `origin`.
  50. // Returns nullopt if no permission usages match the given constraints. The
  51. // `origin` must not be opaque.
  52. absl::optional<base::Time> GetLastPermissionUsageTime(
  53. ContentSettingsType type,
  54. const url::Origin& origin);
  55. // Updates the usage end time for a specific usage session. The session is
  56. // identified by the primary key {`type`, `origin`, `start_time`}, and must
  57. // already exist. `start_time` must be less than or equal to `new_end_time`.
  58. // Operation will fail if `start_time` or `new_end_time` is null. Returns if
  59. // the operation was successful.
  60. bool UpdateEndTime(ContentSettingsType type,
  61. const url::Origin& origin,
  62. base::Time start_time,
  63. base::Time new_end_time);
  64. // Deletes permission usage sessions, which started or ended in the given
  65. // time range. A null `start_time` or `end_time` time is treated as -inf and
  66. // +inf, respectively. Returns if the operation was successful.
  67. bool DeleteSessionsBetween(base::Time start_time, base::Time end_time);
  68. private:
  69. bool CreateSchema();
  70. // The SQL connection to database.
  71. sql::Database db_;
  72. };
  73. } // namespace permissions
  74. #endif // COMPONENTS_PERMISSIONS_PERMISSION_AUDITING_DATABASE_H_