rlz_value_store_chromeos.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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_CHROMEOS_LIB_RLZ_VALUE_STORE_CHROMEOS_H_
6#define RLZ_CHROMEOS_LIB_RLZ_VALUE_STORE_CHROMEOS_H_
7
8#include "base/prefs/persistent_pref_store.h"
9#include "base/threading/non_thread_safe.h"
10#include "rlz/lib/rlz_value_store.h"
11
12namespace base {
13class ListValue;
14class SequencedTaskRunner;
15class Value;
16}
17
18template <typename T> struct DefaultSingletonTraits;
19
20namespace rlz_lib {
21
22// An implementation of RlzValueStore for ChromeOS. Unlike Mac and Win
23// counterparts, it's non thread-safe and should only be accessed on a single
24// Thread instance that has a MessageLoop.
25class RlzValueStoreChromeOS : public RlzValueStore,
26                              public base::NonThreadSafe {
27 public:
28  static RlzValueStoreChromeOS* GetInstance();
29
30  // Sets the MessageLoopProxy that underlying PersistentPrefStore will post I/O
31  // tasks to. Must be called before the first GetInstance() call.
32  static void SetIOTaskRunner(base::SequencedTaskRunner* io_task_runner);
33
34  // Resets the store to its initial state. Should only be used for testing.
35  // Same restrictions as for calling GetInstance() for the first time apply,
36  // i.e. must call SetIOTaskRunner first.
37  static void ResetForTesting();
38
39  // RlzValueStore overrides:
40  virtual bool HasAccess(AccessType type) OVERRIDE;
41
42  virtual bool WritePingTime(Product product, int64 time) OVERRIDE;
43  virtual bool ReadPingTime(Product product, int64* time) OVERRIDE;
44  virtual bool ClearPingTime(Product product) OVERRIDE;
45
46  virtual bool WriteAccessPointRlz(AccessPoint access_point,
47                                   const char* new_rlz) OVERRIDE;
48  virtual bool ReadAccessPointRlz(AccessPoint access_point,
49                                  char* rlz,
50                                  size_t rlz_size) OVERRIDE;
51  virtual bool ClearAccessPointRlz(AccessPoint access_point) OVERRIDE;
52
53  virtual bool AddProductEvent(Product product, const char* event_rlz) OVERRIDE;
54  virtual bool ReadProductEvents(Product product,
55                                 std::vector<std::string>* events) OVERRIDE;
56  virtual bool ClearProductEvent(Product product,
57                                 const char* event_rlz) OVERRIDE;
58  virtual bool ClearAllProductEvents(Product product) OVERRIDE;
59
60  virtual bool AddStatefulEvent(Product product,
61                                const char* event_rlz) OVERRIDE;
62  virtual bool IsStatefulEvent(Product product,
63                               const char* event_rlz) OVERRIDE;
64  virtual bool ClearAllStatefulEvents(Product product) OVERRIDE;
65
66  virtual void CollectGarbage() OVERRIDE;
67
68 private:
69  friend struct DefaultSingletonTraits<RlzValueStoreChromeOS>;
70
71  // Used by JsonPrefStore for write operations.
72  static base::SequencedTaskRunner* io_task_runner_;
73
74  static bool created_;
75
76  RlzValueStoreChromeOS();
77  virtual ~RlzValueStoreChromeOS();
78
79  // Initializes RLZ store.
80  void ReadPrefs();
81
82  // Retrieves list at path |list_name| from JSON store.
83  base::ListValue* GetList(std::string list_name);
84  // Adds |value| to list at |list_name| path in JSON store.
85  bool AddValueToList(std::string list_name, base::Value* value);
86  // Removes |value| from list at |list_name| path in JSON store.
87  bool RemoveValueFromList(std::string list_name, const base::Value& value);
88
89  // Store with RLZ data.
90  scoped_refptr<PersistentPrefStore> rlz_store_;
91
92  DISALLOW_COPY_AND_ASSIGN(RlzValueStoreChromeOS);
93};
94
95}  // namespace rlz_lib
96
97#endif  // RLZ_CHROMEOS_LIB_RLZ_VALUE_STORE_CHROMEOS_H_
98