| Bertrand SIMONNET | 52e5b99 | 2015-08-10 15:18:00 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 16 |  | 
|  | 17 | #ifndef METRICS_PERSISTENT_INTEGER_H_ | 
|  | 18 | #define METRICS_PERSISTENT_INTEGER_H_ | 
|  | 19 |  | 
| Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 20 | #include <stdint.h> | 
|  | 21 |  | 
| Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 22 | #include <string> | 
|  | 23 |  | 
| Bertrand SIMONNET | a7bc1c1 | 2015-11-25 13:29:48 -0800 | [diff] [blame] | 24 | #include <base/files/file_path.h> | 
|  | 25 |  | 
| Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 26 | namespace 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 |  | 
|  | 32 | class PersistentInteger { | 
|  | 33 | public: | 
| Bertrand SIMONNET | a7bc1c1 | 2015-11-25 13:29:48 -0800 | [diff] [blame] | 34 | PersistentInteger(const std::string& name, const base::FilePath& directory); | 
| Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 35 |  | 
|  | 36 | // Virtual only because of mock. | 
|  | 37 | virtual ~PersistentInteger(); | 
|  | 38 |  | 
|  | 39 | // Sets the value.  This writes through to the backing file. | 
| Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 40 | void Set(int64_t v); | 
| Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 41 |  | 
|  | 42 | // Gets the value.  May sync from backing file first. | 
| Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 43 | int64_t Get(); | 
| Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 44 |  | 
|  | 45 | // Returns the name of the object. | 
|  | 46 | std::string Name() { return name_; } | 
|  | 47 |  | 
|  | 48 | // Convenience function for Get() followed by Set(0). | 
| Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 49 | int64_t GetAndClear(); | 
| Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 50 |  | 
|  | 51 | // Convenience function for v = Get, Set(v + x). | 
|  | 52 | // Virtual only because of mock. | 
| Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 53 | virtual void Add(int64_t x); | 
| Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 54 |  | 
| Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 55 | private: | 
|  | 56 | static const int kVersion = 1001; | 
|  | 57 |  | 
| Luigi Semenzato | a5f4fe6 | 2014-04-15 09:31:47 -0700 | [diff] [blame] | 58 | // Writes |value_| to the backing file, creating it if necessary. | 
|  | 59 | void Write(); | 
| Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 60 |  | 
|  | 61 | // Reads the value from the backing file, stores it in |value_|, and returns | 
| Luigi Semenzato | a5f4fe6 | 2014-04-15 09:31:47 -0700 | [diff] [blame] | 62 | // true if the backing file is valid.  Returns false otherwise, and creates | 
|  | 63 | // a valid backing file as a side effect. | 
| Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 64 | bool Read(); | 
|  | 65 |  | 
| Ben Chan | f05ab40 | 2014-08-07 00:54:59 -0700 | [diff] [blame] | 66 | int64_t value_; | 
|  | 67 | int32_t version_; | 
| Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 68 | std::string name_; | 
| Bertrand SIMONNET | a7bc1c1 | 2015-11-25 13:29:48 -0800 | [diff] [blame] | 69 | base::FilePath backing_file_path_; | 
| Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 70 | bool synced_; | 
| Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 71 | }; | 
|  | 72 |  | 
| Bertrand SIMONNET | e6cfd64 | 2014-07-09 16:35:23 -0700 | [diff] [blame] | 73 | }  // namespace chromeos_metrics | 
| Luigi Semenzato | 2fd51cc | 2014-02-26 11:53:16 -0800 | [diff] [blame] | 74 |  | 
|  | 75 | #endif  // METRICS_PERSISTENT_INTEGER_H_ |