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.
5 #ifndef RLZ_VALUE_STORE_H_
6 #define RLZ_VALUE_STORE_H_
8 #include "base/basictypes.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "rlz/lib/rlz_enums.h"
13 #include "rlz/win/lib/lib_mutex.h"
16 #if defined(OS_MACOSX)
17 #include "base/mac/scoped_nsautorelease_pool.h"
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.
34 virtual ~RlzValueStore() {}
36 enum AccessType
{ kReadAccess
, kWriteAccess
};
37 virtual bool HasAccess(AccessType type
) = 0;
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;
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
50 virtual bool ClearAccessPointRlz(AccessPoint access_point
) = 0;
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;
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;
71 // Tells the value store to clean up unimportant internal data structures, for
72 // example empty registry folders, that might remain after clearing other
74 virtual void CollectGarbage() = 0;
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();
87 // return some_error_code;
89 class ScopedRlzValueStoreLock
{
91 ScopedRlzValueStoreLock();
92 ~ScopedRlzValueStoreLock();
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();
100 scoped_ptr
<RlzValueStore
> store_
;
103 #elif defined(OS_MACOSX)
104 base::mac::ScopedNSAutoreleasePool autorelease_pool_
;
108 #if defined(OS_POSIX)
110 // Prefix |directory| to the path where the RLZ data file lives, for tests.
111 void SetRlzStoreDirectory(const base::FilePath
& directory
);
113 // Returns the path of the file used as data store.
114 std::string
RlzStoreFilenameStr();
115 } // namespace testing
116 #endif // defined(OS_POSIX)
118 } // namespace rlz_lib
120 #endif // RLZ_VALUE_STORE_H_