value_store.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright 2014 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_VALUE_STORE_VALUE_STORE_H_
  5. #define COMPONENTS_VALUE_STORE_VALUE_STORE_H_
  6. #include <stddef.h>
  7. #include <memory>
  8. #include <string>
  9. #include <utility>
  10. #include <vector>
  11. #include "base/values.h"
  12. #include "components/value_store/value_store_change.h"
  13. namespace value_store {
  14. // Interface for a storage area for Value objects.
  15. class ValueStore {
  16. public:
  17. // Status codes returned from storage methods.
  18. enum StatusCode {
  19. OK,
  20. // The failure was due to some kind of database corruption. Depending on
  21. // what is corrupted, some part of the database may be recoverable.
  22. //
  23. // For example, if the on-disk representation of leveldb is corrupted, it's
  24. // likely the whole database will need to be wiped and started again.
  25. //
  26. // If a single key has been committed with an invalid JSON representation,
  27. // just that key can be deleted without affecting the rest of the database.
  28. CORRUPTION,
  29. // The failure was due to the store being read-only (for example, policy).
  30. READ_ONLY,
  31. // The failure was due to the store running out of space.
  32. QUOTA_EXCEEDED,
  33. // Any other error.
  34. OTHER_ERROR,
  35. };
  36. enum BackingStoreRestoreStatus {
  37. // No restore attempted.
  38. RESTORE_NONE,
  39. // Corrupted backing store successfully deleted.
  40. DB_RESTORE_DELETE_SUCCESS,
  41. // Corrupted backing store cannot be deleted.
  42. DB_RESTORE_DELETE_FAILURE,
  43. // Corrupted backing store successfully repaired.
  44. DB_RESTORE_REPAIR_SUCCESS,
  45. // Corrupted value successfully deleted.
  46. VALUE_RESTORE_DELETE_SUCCESS,
  47. // Corrupted value cannot be deleted.
  48. VALUE_RESTORE_DELETE_FAILURE,
  49. };
  50. // The status (result) of an operation on a ValueStore.
  51. struct Status {
  52. Status();
  53. Status(StatusCode code,
  54. BackingStoreRestoreStatus restore_status,
  55. const std::string& message);
  56. Status(StatusCode code, const std::string& message);
  57. Status(Status&& other);
  58. ~Status();
  59. Status& operator=(Status&& rhs);
  60. bool ok() const { return code == OK; }
  61. bool IsCorrupted() const { return code == CORRUPTION; }
  62. // Merge |status| into this object. Any members (either |code|,
  63. // |restore_status|, or |message| in |status| will be used, but only if this
  64. // object's members are at their default value.
  65. void Merge(const Status& status);
  66. // The status code.
  67. StatusCode code = OK;
  68. BackingStoreRestoreStatus restore_status = RESTORE_NONE;
  69. // Message associated with the status (error) if there is one.
  70. std::string message;
  71. };
  72. // The result of a read operation (Get).
  73. class ReadResult {
  74. public:
  75. ReadResult(base::Value::Dict settings, Status status);
  76. explicit ReadResult(Status status);
  77. ReadResult(ReadResult&& other);
  78. ~ReadResult();
  79. ReadResult& operator=(ReadResult&& rhs);
  80. ReadResult(const ReadResult&) = delete;
  81. ReadResult& operator=(const ReadResult&) = delete;
  82. // Gets the settings read from the storage. Note that this represents
  83. // the root object. If you request the value for key "foo", that value will
  84. // be in |settings|.|foo|.
  85. //
  86. // Must only be called if there is no error.
  87. base::Value::Dict& settings() { return settings_; }
  88. base::Value::Dict PassSettings() { return std::move(settings_); }
  89. Status PassStatus() { return std::move(status_); }
  90. const Status& status() const { return status_; }
  91. private:
  92. base::Value::Dict settings_;
  93. Status status_;
  94. };
  95. // The result of a write operation (Set/Remove/Clear).
  96. class WriteResult {
  97. public:
  98. WriteResult(ValueStoreChangeList changes, Status status);
  99. explicit WriteResult(Status status);
  100. WriteResult(WriteResult&& other);
  101. ~WriteResult();
  102. WriteResult& operator=(WriteResult&& rhs);
  103. WriteResult(const WriteResult&) = delete;
  104. WriteResult& operator=(const WriteResult&) = delete;
  105. // Gets the list of changes to the settings which resulted from the write.
  106. // Won't be present if the NO_GENERATE_CHANGES WriteOptions was given.
  107. // Only call if no error.
  108. const ValueStoreChangeList& changes() const { return changes_; }
  109. ValueStoreChangeList PassChanges() { return std::move(changes_); }
  110. const Status& status() const { return status_; }
  111. private:
  112. ValueStoreChangeList changes_;
  113. Status status_;
  114. };
  115. // Options for write operations.
  116. enum WriteOptionsValues {
  117. // Callers should usually use this.
  118. DEFAULTS = 0,
  119. // Ignore any quota restrictions.
  120. IGNORE_QUOTA = 1 << 1,
  121. // Don't generate the changes for a WriteResult.
  122. NO_GENERATE_CHANGES = 1 << 2,
  123. };
  124. typedef int WriteOptions;
  125. virtual ~ValueStore() = default;
  126. // Gets the amount of space being used by a single value, in bytes.
  127. // Note: The GetBytesInUse methods are only used by extension settings at the
  128. // moment. If these become more generally useful, the
  129. // SettingsStorageQuotaEnforcer and WeakUnlimitedSettingsStorage classes
  130. // should be moved to the value_store directory.
  131. virtual size_t GetBytesInUse(const std::string& key) = 0;
  132. // Gets the total amount of space being used by multiple values, in bytes.
  133. virtual size_t GetBytesInUse(const std::vector<std::string>& keys) = 0;
  134. // Gets the total amount of space being used by this storage area, in bytes.
  135. virtual size_t GetBytesInUse() = 0;
  136. // Gets a single value from storage.
  137. virtual ReadResult Get(const std::string& key) = 0;
  138. // Gets multiple values from storage.
  139. virtual ReadResult Get(const std::vector<std::string>& keys) = 0;
  140. // Gets all values from storage.
  141. virtual ReadResult Get() = 0;
  142. // Sets a single key to a new value.
  143. virtual WriteResult Set(WriteOptions options,
  144. const std::string& key,
  145. const base::Value& value) = 0;
  146. // Sets multiple keys to new values.
  147. virtual WriteResult Set(WriteOptions options,
  148. const base::Value::Dict& values) = 0;
  149. // Removes a key from the storage.
  150. virtual WriteResult Remove(const std::string& key) = 0;
  151. // Removes multiple keys from the storage.
  152. virtual WriteResult Remove(const std::vector<std::string>& keys) = 0;
  153. // Clears the storage.
  154. virtual WriteResult Clear() = 0;
  155. };
  156. } // namespace value_store
  157. #endif // COMPONENTS_VALUE_STORE_VALUE_STORE_H_