Attempt to fix flaky PlatformAppBrowserTest.Messaging.
[chromium-blink-merge.git] / rlz / lib / rlz_value_store.h
blob4bd1f093b83af5110556f65d0cb379a0c127d913
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"
12 #if defined(OS_WIN)
13 #include "rlz/win/lib/lib_mutex.h"
14 #endif
16 #if defined(OS_MACOSX)
17 #include "base/mac/scoped_nsautorelease_pool.h"
18 #endif
21 #include <string>
22 #include <vector>
24 namespace base {
25 class FilePath;
28 namespace rlz_lib {
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() {}
36 enum AccessType { kReadAccess, kWriteAccess };
37 virtual bool HasAccess(AccessType type) = 0;
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;
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;
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;
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;
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;
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();
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();
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
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);
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_