1//
2// Copyright (C) 2011 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//      http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17#ifndef UPDATE_ENGINE_COMMON_PREFS_INTERFACE_H_
18#define UPDATE_ENGINE_COMMON_PREFS_INTERFACE_H_
19
20#include <stdint.h>
21
22#include <string>
23
24namespace chromeos_update_engine {
25
26// The prefs interface allows access to a persistent preferences
27// store. The two reasons for providing this as an interface are
28// testing as well as easier switching to a new implementation in the
29// future, if necessary.
30
31class PrefsInterface {
32 public:
33  // Observer class to be notified about key value changes.
34  class ObserverInterface {
35   public:
36    virtual ~ObserverInterface() = default;
37
38    // Called when the value is set for the observed |key|.
39    virtual void OnPrefSet(const std::string& key) = 0;
40
41    // Called when the observed |key| is deleted.
42    virtual void OnPrefDeleted(const std::string& key) = 0;
43  };
44
45  virtual ~PrefsInterface() = default;
46
47  // Gets a string |value| associated with |key|. Returns true on
48  // success, false on failure (including when the |key| is not
49  // present in the store).
50  virtual bool GetString(const std::string& key, std::string* value) const = 0;
51
52  // Associates |key| with a string |value|. Returns true on success,
53  // false otherwise.
54  virtual bool SetString(const std::string& key, const std::string& value) = 0;
55
56  // Gets an int64_t |value| associated with |key|. Returns true on
57  // success, false on failure (including when the |key| is not
58  // present in the store).
59  virtual bool GetInt64(const std::string& key, int64_t* value) const = 0;
60
61  // Associates |key| with an int64_t |value|. Returns true on success,
62  // false otherwise.
63  virtual bool SetInt64(const std::string& key, const int64_t value) = 0;
64
65  // Gets a boolean |value| associated with |key|. Returns true on
66  // success, false on failure (including when the |key| is not
67  // present in the store).
68  virtual bool GetBoolean(const std::string& key, bool* value) const = 0;
69
70  // Associates |key| with a boolean |value|. Returns true on success,
71  // false otherwise.
72  virtual bool SetBoolean(const std::string& key, const bool value) = 0;
73
74  // Returns true if the setting exists (i.e. a file with the given key
75  // exists in the prefs directory)
76  virtual bool Exists(const std::string& key) const = 0;
77
78  // Returns true if successfully deleted the file corresponding to
79  // this key. Calling with non-existent keys does nothing.
80  virtual bool Delete(const std::string& key) = 0;
81
82  // Add an observer to watch whenever the given |key| is modified. The
83  // OnPrefSet() and OnPrefDelete() methods will be called whenever any of the
84  // Set*() methods or the Delete() method are called on the given key,
85  // respectively.
86  virtual void AddObserver(const std::string& key,
87                           ObserverInterface* observer) = 0;
88
89  // Remove an observer added with AddObserver(). The observer won't be called
90  // anymore for future Set*() and Delete() method calls.
91  virtual void RemoveObserver(const std::string& key,
92                              ObserverInterface* observer) = 0;
93};
94
95}  // namespace chromeos_update_engine
96
97#endif  // UPDATE_ENGINE_COMMON_PREFS_INTERFACE_H_
98