Bertrand SIMONNET | 601ecf8 | 2014-10-06 13:14:27 -0700 | [diff] [blame^] | 1 | // Copyright 2014 The Chromium OS 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 | // Wrapper around /etc/os-release and /etc/os-release.d. |
| 6 | // Standard fields can come from both places depending on how we set them. They |
| 7 | // should always be accessed through this interface. |
| 8 | |
| 9 | #ifndef LIBCHROMEOS_CHROMEOS_OSRELEASE_READER_H_ |
| 10 | #define LIBCHROMEOS_CHROMEOS_OSRELEASE_READER_H_ |
| 11 | |
| 12 | #include <string> |
| 13 | |
| 14 | #include <chromeos/chromeos_export.h> |
| 15 | #include <chromeos/key_value_store.h> |
| 16 | #include <gtest/gtest_prod.h> |
| 17 | |
| 18 | namespace chromeos { |
| 19 | |
| 20 | class CHROMEOS_EXPORT OsReleaseReader { |
| 21 | public: |
| 22 | // Create an empty reader |
| 23 | OsReleaseReader() = default; |
| 24 | |
| 25 | // Loads the key=value pairs from either /etc/os-release.d/<KEY> or |
| 26 | // /etc/os-release. |
| 27 | // Returns false on errors. |
| 28 | bool Load(); |
| 29 | |
| 30 | // Same as the private Load method. |
| 31 | // This need to be public so that services can use it in testing mode (for |
| 32 | // autotest tests for example). |
| 33 | // This should not be used in production so suffix it with TestingOnly to |
| 34 | // make it obvious. |
| 35 | bool LoadTestingOnly(const base::FilePath& root_dir); |
| 36 | |
| 37 | // Getter for the given key. Returns whether the key was found on the store. |
| 38 | bool GetString(const std::string& key, std::string* value) const; |
| 39 | |
| 40 | private: |
| 41 | // The map storing all the key-value pairs. |
| 42 | KeyValueStore store_; |
| 43 | |
| 44 | // os-release can be lazily loaded if need be. |
| 45 | bool initialized_; |
| 46 | |
| 47 | // Load the data from a given root_dir. Return false on errors. |
| 48 | CHROMEOS_PRIVATE bool Load(const base::FilePath& root_dir); |
| 49 | |
| 50 | DISALLOW_COPY_AND_ASSIGN(OsReleaseReader); |
| 51 | }; |
| 52 | |
| 53 | } // namespace chromeos |
| 54 | |
| 55 | #endif // LIBCHROMEOS_CHROMEOS_OSRELEASE_READER_H_ |