storage_module.cc 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #include "components/reporting/storage/storage_module.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/bind.h"
  8. #include "base/callback.h"
  9. #include "base/containers/span.h"
  10. #include "base/logging.h"
  11. #include "base/memory/ptr_util.h"
  12. #include "components/reporting/compression/compression_module.h"
  13. #include "components/reporting/encryption/encryption_module_interface.h"
  14. #include "components/reporting/proto/synced/record.pb.h"
  15. #include "components/reporting/proto/synced/record_constants.pb.h"
  16. #include "components/reporting/storage/storage.h"
  17. #include "components/reporting/storage/storage_configuration.h"
  18. #include "components/reporting/storage/storage_module_interface.h"
  19. #include "components/reporting/storage/storage_uploader_interface.h"
  20. #include "components/reporting/util/status.h"
  21. #include "components/reporting/util/statusor.h"
  22. namespace reporting {
  23. StorageModule::StorageModule() = default;
  24. StorageModule::~StorageModule() = default;
  25. void StorageModule::AddRecord(Priority priority,
  26. Record record,
  27. EnqueueCallback callback) {
  28. storage_->Write(priority, std::move(record), std::move(callback));
  29. }
  30. void StorageModule::ReportSuccess(SequenceInformation sequence_information,
  31. bool force) {
  32. storage_->Confirm(
  33. sequence_information.priority(), sequence_information.sequencing_id(),
  34. force, base::BindOnce([](Status status) {
  35. if (!status.ok()) {
  36. LOG(ERROR) << "Unable to confirm record deletion: " << status;
  37. }
  38. }));
  39. }
  40. void StorageModule::Flush(Priority priority, FlushCallback callback) {
  41. std::move(callback).Run(storage_->Flush(priority));
  42. }
  43. void StorageModule::UpdateEncryptionKey(
  44. SignedEncryptionInfo signed_encryption_key) {
  45. storage_->UpdateEncryptionKey(std::move(signed_encryption_key));
  46. }
  47. // static
  48. void StorageModule::Create(
  49. const StorageOptions& options,
  50. UploaderInterface::AsyncStartUploaderCb async_start_upload_cb,
  51. scoped_refptr<EncryptionModuleInterface> encryption_module,
  52. scoped_refptr<CompressionModule> compression_module,
  53. base::OnceCallback<void(StatusOr<scoped_refptr<StorageModuleInterface>>)>
  54. callback) {
  55. scoped_refptr<StorageModule> instance =
  56. // Cannot base::MakeRefCounted, since constructor is protected.
  57. base::WrapRefCounted(new StorageModule());
  58. Storage::Create(
  59. options, async_start_upload_cb, encryption_module, compression_module,
  60. base::BindOnce(
  61. [](scoped_refptr<StorageModule> instance,
  62. base::OnceCallback<void(
  63. StatusOr<scoped_refptr<StorageModuleInterface>>)> callback,
  64. StatusOr<scoped_refptr<Storage>> storage) {
  65. if (!storage.ok()) {
  66. std::move(callback).Run(storage.status());
  67. return;
  68. }
  69. instance->storage_ = std::move(storage.ValueOrDie());
  70. std::move(callback).Run(std::move(instance));
  71. },
  72. std::move(instance), std::move(callback)));
  73. }
  74. } // namespace reporting