storage.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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_REPORTING_STORAGE_STORAGE_H_
  5. #define COMPONENTS_REPORTING_STORAGE_STORAGE_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include <utility>
  10. #include "base/callback.h"
  11. #include "base/containers/flat_map.h"
  12. #include "base/files/file_path.h"
  13. #include "base/memory/ref_counted.h"
  14. #include "base/memory/scoped_refptr.h"
  15. #include "base/strings/string_piece.h"
  16. #include "components/reporting/compression/compression_module.h"
  17. #include "components/reporting/encryption/encryption_module_interface.h"
  18. #include "components/reporting/proto/synced/record.pb.h"
  19. #include "components/reporting/proto/synced/record_constants.pb.h"
  20. #include "components/reporting/storage/storage_configuration.h"
  21. #include "components/reporting/storage/storage_queue.h"
  22. #include "components/reporting/storage/storage_uploader_interface.h"
  23. #include "components/reporting/util/status.h"
  24. #include "components/reporting/util/statusor.h"
  25. #include "third_party/abseil-cpp/absl/types/optional.h"
  26. namespace reporting {
  27. // Storage represents the data to be collected, stored persistently and uploaded
  28. // according to the priority.
  29. class Storage : public base::RefCountedThreadSafe<Storage> {
  30. public:
  31. // Creates Storage instance, and returns it with the completion callback.
  32. static void Create(
  33. const StorageOptions& options,
  34. UploaderInterface::AsyncStartUploaderCb async_start_upload_cb,
  35. scoped_refptr<EncryptionModuleInterface> encryption_module,
  36. scoped_refptr<CompressionModule> compression_module,
  37. base::OnceCallback<void(StatusOr<scoped_refptr<Storage>>)> completion_cb);
  38. // Wraps and serializes Record (taking ownership of it), encrypts and writes
  39. // the resulting blob into the Storage (the last file of it) according to the
  40. // priority with the next sequencing id assigned. If file is going to
  41. // become too large, it is closed and new file is created.
  42. void Write(Priority priority,
  43. Record record,
  44. base::OnceCallback<void(Status)> completion_cb);
  45. // Confirms acceptance of the records according to the priority up to
  46. // |sequencing_id| (inclusively). All records with sequencing ids <= this
  47. // one can be removed from the Storage, and can no longer be uploaded.
  48. // If |force| is false (which is used in most cases), |sequencing_id| is
  49. // only accepted if no higher ids were confirmed before; otherwise it is
  50. // accepted unconditionally.
  51. void Confirm(Priority priority,
  52. absl::optional<int64_t> sequencing_id,
  53. bool force,
  54. base::OnceCallback<void(Status)> completion_cb);
  55. // Initiates upload of collected records according to the priority.
  56. // Called usually for a queue with an infinite or very large upload period.
  57. // Multiple |Flush| calls can safely run in parallel.
  58. // Returns error if cannot start upload.
  59. Status Flush(Priority priority);
  60. // If the server attached signed encryption key to the response, it needs to
  61. // be paased here.
  62. void UpdateEncryptionKey(SignedEncryptionInfo signed_encryption_key);
  63. const StorageOptions& options() const { return options_; }
  64. Storage(const Storage& other) = delete;
  65. Storage& operator=(const Storage& other) = delete;
  66. protected:
  67. virtual ~Storage();
  68. private:
  69. friend class base::RefCountedThreadSafe<Storage>;
  70. // Private bridge class.
  71. class QueueUploaderInterface;
  72. // Private helper class for key upload/download to the file system.
  73. class KeyInStorage;
  74. // Private helper class for initial key delivery from the server.
  75. // It can be invoked multiple times in parallel, but will only do
  76. // one server roundtrip and notify all requestors upon its completion.
  77. class KeyDelivery;
  78. // Private constructor, to be called by Create factory method only.
  79. // Queues need to be added afterwards.
  80. Storage(const StorageOptions& options,
  81. scoped_refptr<EncryptionModuleInterface> encryption_module,
  82. scoped_refptr<CompressionModule> compression_module,
  83. UploaderInterface::AsyncStartUploaderCb async_start_upload_cb);
  84. // Initializes the object by adding all queues for all priorities.
  85. // Must be called once and only once after construction.
  86. // Returns OK or error status, if anything failed to initialize.
  87. Status Init();
  88. // Helper method that selects queue by priority. Returns error
  89. // if priority does not match any queue.
  90. // Note: queues_ never change after initialization is finished, so there is no
  91. // need to protect or serialize access to it.
  92. StatusOr<scoped_refptr<StorageQueue>> GetQueue(Priority priority);
  93. // Immutable options, stored at the time of creation.
  94. const StorageOptions options_;
  95. // Encryption module.
  96. scoped_refptr<EncryptionModuleInterface> encryption_module_;
  97. // Internal module for initiail key delivery from server.
  98. std::unique_ptr<KeyDelivery> key_delivery_;
  99. // Compression module.
  100. scoped_refptr<CompressionModule> compression_module_;
  101. // Internal key management module.
  102. std::unique_ptr<KeyInStorage> key_in_storage_;
  103. // Map priority->StorageQueue.
  104. base::flat_map<Priority, scoped_refptr<StorageQueue>> queues_;
  105. // Upload provider callback.
  106. const UploaderInterface::AsyncStartUploaderCb async_start_upload_cb_;
  107. };
  108. } // namespace reporting
  109. #endif // COMPONENTS_REPORTING_STORAGE_STORAGE_H_