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