breadcrumb_persistent_storage_manager.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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_BREADCRUMBS_CORE_BREADCRUMB_PERSISTENT_STORAGE_MANAGER_H_
  5. #define COMPONENTS_BREADCRUMBS_CORE_BREADCRUMB_PERSISTENT_STORAGE_MANAGER_H_
  6. #include <string>
  7. #include <vector>
  8. #include "base/callback.h"
  9. #include "base/files/file_path.h"
  10. #include "base/time/time.h"
  11. #include "base/timer/timer.h"
  12. #include "components/breadcrumbs/core/breadcrumb_manager_observer.h"
  13. #include "components/breadcrumbs/core/breadcrumbs_status.h"
  14. #include "components/breadcrumbs/core/crash_reporter_breadcrumb_constants.h"
  15. #include "third_party/abseil-cpp/absl/types/optional.h"
  16. namespace breadcrumbs {
  17. class BreadcrumbManager;
  18. class BreadcrumbManagerKeyedService;
  19. // The filesize for the file at |breadcrumbs_file_path_|. The file will always
  20. // be this constant size because it is accessed using a memory mapped file. The
  21. // file is twice as large as |kMaxDataLength| which leaves room for appending
  22. // breadcrumb events. Once the file is full of events, the contents will be
  23. // reduced to kMaxDataLength.
  24. constexpr size_t kPersistedFilesizeInBytes = kMaxDataLength * 2;
  25. // Stores breadcrumb events to and retrieves them from a file on disk.
  26. // Persisting these events allows access to breadcrumb events from previous
  27. // application sessions.
  28. class BreadcrumbPersistentStorageManager : public BreadcrumbManagerObserver {
  29. public:
  30. // Breadcrumbs will be stored in a file in |directory|.
  31. explicit BreadcrumbPersistentStorageManager(
  32. const base::FilePath& directory,
  33. base::RepeatingCallback<bool()> is_metrics_enabled_callback);
  34. ~BreadcrumbPersistentStorageManager() override;
  35. BreadcrumbPersistentStorageManager(
  36. const BreadcrumbPersistentStorageManager&) = delete;
  37. BreadcrumbPersistentStorageManager& operator=(
  38. const BreadcrumbPersistentStorageManager&) = delete;
  39. // Returns the stored breadcrumb events from disk to |callback|.
  40. void GetStoredEvents(
  41. base::OnceCallback<void(std::vector<std::string>)> callback);
  42. // Starts observing |manager| for events. Existing events will be persisted
  43. // immediately.
  44. void MonitorBreadcrumbManager(BreadcrumbManager* manager);
  45. // Starts observing |service| for events. Existing events will be persisted
  46. // immediately.
  47. void MonitorBreadcrumbManagerService(BreadcrumbManagerKeyedService* service);
  48. // Stops observing |manager|.
  49. void StopMonitoringBreadcrumbManager(BreadcrumbManager* manager);
  50. // Stops observing |service|.
  51. void StopMonitoringBreadcrumbManagerService(
  52. BreadcrumbManagerKeyedService* service);
  53. private:
  54. // Returns whether metrics consent has been provided and the persistent
  55. // storage manager can therefore create its breadcrumbs files. Deletes any
  56. // existing breadcrumbs files if consent has been revoked.
  57. bool CheckForFileConsent();
  58. // Initializes |file_position_| to |file_size| and writes any events so far.
  59. void InitializeFilePosition(size_t file_size);
  60. // Writes |pending_breadcrumbs_| to |breadcrumbs_file_| if it fits, otherwise
  61. // rewrites the file. NOTE: Writing may be delayed if the file has recently
  62. // been written into.
  63. void WriteEvents();
  64. // Appends events in |pending_breadcrumbs| to |existing events|, then writes
  65. // the combined events to |breadcrumbs_file_|, overwriting any existing
  66. // persisted breadcrumbs.
  67. void CombineEventsAndRewriteAllBreadcrumbs(
  68. const std::vector<std::string> pending_breadcrumbs,
  69. std::vector<std::string> existing_events);
  70. // Writes events from observed managers to |breadcrumbs_file_|, overwriting
  71. // any existing persisted breadcrumbs.
  72. void RewriteAllExistingBreadcrumbs();
  73. // Writes breadcrumbs stored in |pending_breadcrumbs_| to |breadcrumbs_file_|.
  74. void WritePendingBreadcrumbs();
  75. // BreadcrumbManagerObserver
  76. void EventAdded(BreadcrumbManager* manager,
  77. const std::string& event) override;
  78. void OldEventsRemoved(BreadcrumbManager* manager) override;
  79. // Individual breadcrumbs that have not yet been written to disk.
  80. std::string pending_breadcrumbs_;
  81. // The last time a breadcrumb was written to |breadcrumbs_file_|. This
  82. // timestamp prevents breadcrumbs from being written to disk too often.
  83. base::TimeTicks last_written_time_;
  84. // A timer to delay writing to disk too often.
  85. base::OneShotTimer write_timer_;
  86. // The path to the file for storing persisted breadcrumbs.
  87. const base::FilePath breadcrumbs_file_path_;
  88. // The path to the temporary file for writing persisted breadcrumbs.
  89. const base::FilePath breadcrumbs_temp_file_path_;
  90. // The current size of breadcrumbs written to |breadcrumbs_file_path_|.
  91. // NOTE: The optional will not have a value until the size of the existing
  92. // file, if any, is retrieved.
  93. absl::optional<size_t> file_position_;
  94. // Used to check whether the user has consented to metrics reporting.
  95. // Breadcrumbs should only be written to persistent storage if true.
  96. base::RepeatingCallback<bool()> is_metrics_enabled_callback_;
  97. // The SequencedTaskRunner on which File IO operations are performed.
  98. scoped_refptr<base::SequencedTaskRunner> task_runner_;
  99. base::WeakPtrFactory<BreadcrumbPersistentStorageManager> weak_ptr_factory_;
  100. };
  101. } // namespace breadcrumbs
  102. #endif // COMPONENTS_BREADCRUMBS_CORE_BREADCRUMB_PERSISTENT_STORAGE_MANAGER_H_