rlz_value_store.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright (c) 2012 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 RLZ_LIB_RLZ_VALUE_STORE_H_
  5. #define RLZ_LIB_RLZ_VALUE_STORE_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <memory>
  9. #include <string>
  10. #include <vector>
  11. #include "build/build_config.h"
  12. #include "rlz/lib/rlz_enums.h"
  13. #if BUILDFLAG(IS_WIN)
  14. #include "rlz/win/lib/lib_mutex.h"
  15. #endif
  16. #if BUILDFLAG(IS_APPLE)
  17. #include "base/mac/scoped_nsautorelease_pool.h"
  18. #endif
  19. namespace base {
  20. class FilePath;
  21. }
  22. namespace rlz_lib {
  23. // Abstracts away rlz's key value store. On windows, this usually writes to
  24. // the registry. On mac, it writes to an NSDefaults object.
  25. class RlzValueStore {
  26. public:
  27. virtual ~RlzValueStore() {}
  28. enum AccessType { kReadAccess, kWriteAccess };
  29. virtual bool HasAccess(AccessType type) = 0;
  30. // Ping times.
  31. virtual bool WritePingTime(Product product, int64_t time) = 0;
  32. virtual bool ReadPingTime(Product product, int64_t* time) = 0;
  33. virtual bool ClearPingTime(Product product) = 0;
  34. // Access point RLZs.
  35. virtual bool WriteAccessPointRlz(AccessPoint access_point,
  36. const char* new_rlz) = 0;
  37. virtual bool ReadAccessPointRlz(AccessPoint access_point,
  38. char* rlz, // At most kMaxRlzLength + 1 bytes
  39. size_t rlz_size) = 0;
  40. virtual bool ClearAccessPointRlz(AccessPoint access_point) = 0;
  41. virtual bool UpdateExistingAccessPointRlz(const std::string& brand) = 0;
  42. // Product events.
  43. // Stores |event_rlz| for product |product| as product event.
  44. virtual bool AddProductEvent(Product product, const char* event_rlz) = 0;
  45. // Appends all events for |product| to |events|, in arbirtrary order.
  46. virtual bool ReadProductEvents(Product product,
  47. std::vector<std::string>* events) = 0;
  48. // Removes the stored event |event_rlz| for |product| if it exists.
  49. virtual bool ClearProductEvent(Product product, const char* event_rlz) = 0;
  50. // Removes all stored product events for |product|.
  51. virtual bool ClearAllProductEvents(Product product) = 0;
  52. // Stateful events.
  53. // Stores |event_rlz| for product |product| as stateful event.
  54. virtual bool AddStatefulEvent(Product product, const char* event_rlz) = 0;
  55. // Checks if |event_rlz| has been stored as stateful event for |product|.
  56. virtual bool IsStatefulEvent(Product product, const char* event_rlz) = 0;
  57. // Removes all stored stateful events for |product|.
  58. virtual bool ClearAllStatefulEvents(Product product) = 0;
  59. // Tells the value store to clean up unimportant internal data structures, for
  60. // example empty registry folders, that might remain after clearing other
  61. // data. Best-effort.
  62. virtual void CollectGarbage() = 0;
  63. };
  64. // All methods of RlzValueStore must stays consistent even when accessed from
  65. // multiple threads in multiple processes. To enforce this through the type
  66. // system, the only way to access the RlzValueStore is through a
  67. // ScopedRlzValueStoreLock, which is a cross-process lock. It is active while
  68. // it is in scope. If the class fails to acquire a lock, its GetStore() method
  69. // returns NULL. If the lock fails to be acquired, it must not be taken
  70. // recursively. That is, all user code should look like this:
  71. // ScopedRlzValueStoreLock lock;
  72. // RlzValueStore* store = lock.GetStore();
  73. // if (!store)
  74. // return some_error_code;
  75. // ...
  76. class ScopedRlzValueStoreLock {
  77. public:
  78. ScopedRlzValueStoreLock();
  79. ~ScopedRlzValueStoreLock();
  80. // Returns a RlzValueStore protected by a cross-process lock, or NULL if the
  81. // lock can't be obtained. The lifetime of the returned object is limited to
  82. // the lifetime of this ScopedRlzValueStoreLock object.
  83. RlzValueStore* GetStore();
  84. private:
  85. std::unique_ptr<RlzValueStore> store_;
  86. #if BUILDFLAG(IS_WIN)
  87. LibMutex lock_;
  88. #elif BUILDFLAG(IS_APPLE)
  89. base::mac::ScopedNSAutoreleasePool autorelease_pool_;
  90. #endif
  91. };
  92. #if BUILDFLAG(IS_POSIX)
  93. namespace testing {
  94. // Prefix |directory| to the path where the RLZ data file lives, for tests.
  95. void SetRlzStoreDirectory(const base::FilePath& directory);
  96. // Returns the path of the file used as data store.
  97. std::string RlzStoreFilenameStr();
  98. } // namespace testing
  99. #endif // BUILDFLAG(IS_POSIX)
  100. } // namespace rlz_lib
  101. #endif // RLZ_LIB_RLZ_VALUE_STORE_H_