blob: 96d9fc05958c6cd3b55fec1dff4a61dbdace3dec [file] [log] [blame]
Bertrand SIMONNET52e5b992015-08-10 15:18:00 -07001/*
2 * Copyright (C) 2015 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 */
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080016
17#ifndef METRICS_PERSISTENT_INTEGER_H_
18#define METRICS_PERSISTENT_INTEGER_H_
19
Ben Chanf05ab402014-08-07 00:54:59 -070020#include <stdint.h>
21
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080022#include <string>
23
Bertrand SIMONNETa7bc1c12015-11-25 13:29:48 -080024#include <base/files/file_path.h>
25
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080026namespace chromeos_metrics {
27
28// PersistentIntegers is a named 64-bit integer value backed by a file.
29// The in-memory value acts as a write-through cache of the file value.
30// If the backing file doesn't exist or has bad content, the value is 0.
31
32class PersistentInteger {
33 public:
Bertrand SIMONNETa7bc1c12015-11-25 13:29:48 -080034 PersistentInteger(const std::string& name, const base::FilePath& directory);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080035
36 // Virtual only because of mock.
37 virtual ~PersistentInteger();
38
39 // Sets the value. This writes through to the backing file.
Ben Chanf05ab402014-08-07 00:54:59 -070040 void Set(int64_t v);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080041
42 // Gets the value. May sync from backing file first.
Ben Chanf05ab402014-08-07 00:54:59 -070043 int64_t Get();
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080044
45 // Returns the name of the object.
46 std::string Name() { return name_; }
47
48 // Convenience function for Get() followed by Set(0).
Ben Chanf05ab402014-08-07 00:54:59 -070049 int64_t GetAndClear();
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080050
51 // Convenience function for v = Get, Set(v + x).
52 // Virtual only because of mock.
Ben Chanf05ab402014-08-07 00:54:59 -070053 virtual void Add(int64_t x);
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080054
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080055 private:
56 static const int kVersion = 1001;
57
Luigi Semenzatoa5f4fe62014-04-15 09:31:47 -070058 // Writes |value_| to the backing file, creating it if necessary.
59 void Write();
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080060
61 // Reads the value from the backing file, stores it in |value_|, and returns
Luigi Semenzatoa5f4fe62014-04-15 09:31:47 -070062 // true if the backing file is valid. Returns false otherwise, and creates
63 // a valid backing file as a side effect.
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080064 bool Read();
65
Ben Chanf05ab402014-08-07 00:54:59 -070066 int64_t value_;
67 int32_t version_;
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080068 std::string name_;
Bertrand SIMONNETa7bc1c12015-11-25 13:29:48 -080069 base::FilePath backing_file_path_;
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080070 bool synced_;
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080071};
72
Bertrand SIMONNETe6cfd642014-07-09 16:35:23 -070073} // namespace chromeos_metrics
Luigi Semenzato2fd51cc2014-02-26 11:53:16 -080074
75#endif // METRICS_PERSISTENT_INTEGER_H_