store_update_data.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2019 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_OPTIMIZATION_GUIDE_CORE_STORE_UPDATE_DATA_H_
  5. #define COMPONENTS_OPTIMIZATION_GUIDE_CORE_STORE_UPDATE_DATA_H_
  6. #include <string>
  7. #include "base/sequence_checker.h"
  8. #include "base/time/time.h"
  9. #include "base/version.h"
  10. #include "components/leveldb_proto/public/proto_database.h"
  11. #include "third_party/abseil-cpp/absl/types/optional.h"
  12. namespace optimization_guide {
  13. namespace proto {
  14. class Hint;
  15. class PredictionModel;
  16. class StoreEntry;
  17. } // namespace proto
  18. using EntryVector =
  19. leveldb_proto::ProtoDatabase<proto::StoreEntry>::KeyEntryVector;
  20. // Holds hint or prediction model data for updating the OptimizationGuideStore.
  21. class StoreUpdateData {
  22. public:
  23. StoreUpdateData(const StoreUpdateData&) = delete;
  24. StoreUpdateData& operator=(const StoreUpdateData&) = delete;
  25. ~StoreUpdateData();
  26. // Creates an update data object for a component hint update.
  27. static std::unique_ptr<StoreUpdateData> CreateComponentStoreUpdateData(
  28. const base::Version& component_version);
  29. // Creates an update data object for a fetched hint update.
  30. static std::unique_ptr<StoreUpdateData> CreateFetchedStoreUpdateData(
  31. base::Time fetch_update_time);
  32. // Creates an update data object for a prediction model update.
  33. static std::unique_ptr<StoreUpdateData> CreatePredictionModelStoreUpdateData(
  34. base::Time expiry_time);
  35. // Returns the component version of a component hint update.
  36. const absl::optional<base::Version> component_version() const {
  37. return component_version_;
  38. }
  39. // Returns the next update time for the entries in the store update.
  40. const absl::optional<base::Time> update_time() const { return update_time_; }
  41. // Returns the expiry time for the hints in a fetched hint update.
  42. const absl::optional<base::Time> expiry_time() const { return expiry_time_; }
  43. // Moves |hint| into this update data. After MoveHintIntoUpdateData() is
  44. // called, |hint| is no longer valid.
  45. void MoveHintIntoUpdateData(proto::Hint&& hint);
  46. // Copies |prediction_model| into this update data.
  47. void CopyPredictionModelIntoUpdateData(
  48. const proto::PredictionModel& prediction_model);
  49. // Returns the store entry updates along with ownership to them.
  50. std::unique_ptr<EntryVector> TakeUpdateEntries();
  51. private:
  52. StoreUpdateData(absl::optional<base::Version> component_version,
  53. absl::optional<base::Time> fetch_update_time,
  54. absl::optional<base::Time> expiry_time);
  55. explicit StoreUpdateData(base::Time expiry_time);
  56. // The component version of the update data for a component update.
  57. absl::optional<base::Version> component_version_;
  58. // The time when the entries in this update need to be updated.
  59. absl::optional<base::Time> update_time_;
  60. // The time when entries in this update expire.
  61. absl::optional<base::Time> expiry_time_;
  62. // The prefix to add to the key of every store entry. It is set
  63. // during construction for appropriate type of update.
  64. std::string entry_key_prefix_;
  65. // The vector of entries to save.
  66. std::unique_ptr<EntryVector> entries_to_save_;
  67. SEQUENCE_CHECKER(sequence_checker_);
  68. };
  69. } // namespace optimization_guide
  70. #endif // COMPONENTS_OPTIMIZATION_GUIDE_CORE_STORE_UPDATE_DATA_H_