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