Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 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 | #ifndef SHILL_KEY_FILE_STORE_ |
| 6 | #define SHILL_KEY_FILE_STORE_ |
| 7 | |
| 8 | #include <base/file_path.h> |
| 9 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
| 10 | |
Darin Petkov | 86964e0 | 2011-06-29 13:49:28 -0700 | [diff] [blame] | 11 | #include "shill/crypto_provider.h" |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 12 | #include "shill/glib.h" |
| 13 | #include "shill/store_interface.h" |
| 14 | |
| 15 | namespace shill { |
| 16 | |
| 17 | // A key file store implementation of the store interface. See |
| 18 | // http://www.gtk.org/api/2.6/glib/glib-Key-value-file-parser.html for details |
| 19 | // of the key file format. |
| 20 | class KeyFileStore : public StoreInterface { |
| 21 | public: |
Darin Petkov | b2841fd | 2011-06-30 12:54:12 -0700 | [diff] [blame] | 22 | explicit KeyFileStore(GLib *glib); |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 23 | virtual ~KeyFileStore(); |
| 24 | |
| 25 | void set_path(const FilePath &path) { path_ = path; } |
| 26 | const FilePath &path() const { return path_; } |
| 27 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 28 | // Returns true if the store exists and is non-empty. |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 29 | bool IsNonEmpty() const; |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 30 | |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 31 | // Opens the store. Returns true on success. This method must be |
| 32 | // invoked before using any of the getters or setters. |
| 33 | // This method does not complete gracefully if invoked on a store |
| 34 | // that has been opened already but not closed yet. |
| 35 | bool Open(); |
| 36 | |
| 37 | // Closes the store and flushes it to persistent storage. Returns true on |
| 38 | // success. Note that the store is considered closed even if Close returns |
| 39 | // false. |
| 40 | // This method does not complete gracefully if invoked on a store |
| 41 | // that has not been opened successfully or has been closed already. |
| 42 | bool Close(); |
| 43 | |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 44 | // Inherited from StoreInterface. |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 45 | virtual bool Flush(); |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 46 | virtual std::set<std::string> GetGroups() const; |
| 47 | virtual std::set<std::string> GetGroupsWithKey(const std::string &key) const; |
| 48 | virtual bool ContainsGroup(const std::string &group) const; |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 49 | virtual bool DeleteKey(const std::string &group, const std::string &key); |
| 50 | virtual bool DeleteGroup(const std::string &group); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 51 | virtual bool SetHeader(const std::string &header); |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 52 | virtual bool GetString(const std::string &group, |
| 53 | const std::string &key, |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 54 | std::string *value) const; |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 55 | virtual bool SetString(const std::string &group, |
| 56 | const std::string &key, |
| 57 | const std::string &value); |
| 58 | virtual bool GetBool(const std::string &group, |
| 59 | const std::string &key, |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 60 | bool *value) const; |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 61 | virtual bool SetBool(const std::string &group, |
| 62 | const std::string &key, |
| 63 | bool value); |
| 64 | virtual bool GetInt(const std::string &group, |
| 65 | const std::string &key, |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 66 | int *value) const; |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 67 | virtual bool SetInt(const std::string &group, |
| 68 | const std::string &key, |
| 69 | int value); |
Paul Stewart | dab3b5a | 2012-07-11 18:25:10 -0700 | [diff] [blame] | 70 | virtual bool GetUint64(const std::string &group, |
| 71 | const std::string &key, |
| 72 | uint64 *value) const; |
| 73 | virtual bool SetUint64(const std::string &group, |
| 74 | const std::string &key, |
| 75 | uint64 value); |
Darin Petkov | b2841fd | 2011-06-30 12:54:12 -0700 | [diff] [blame] | 76 | virtual bool GetStringList(const std::string &group, |
| 77 | const std::string &key, |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 78 | std::vector<std::string> *value) const; |
Darin Petkov | b2841fd | 2011-06-30 12:54:12 -0700 | [diff] [blame] | 79 | virtual bool SetStringList(const std::string &group, |
| 80 | const std::string &key, |
| 81 | const std::vector<std::string> &value); |
Darin Petkov | 86964e0 | 2011-06-29 13:49:28 -0700 | [diff] [blame] | 82 | virtual bool GetCryptedString(const std::string &group, |
| 83 | const std::string &key, |
| 84 | std::string *value); |
| 85 | virtual bool SetCryptedString(const std::string &group, |
| 86 | const std::string &key, |
| 87 | const std::string &value); |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 88 | private: |
| 89 | FRIEND_TEST(KeyFileStoreTest, OpenClose); |
| 90 | FRIEND_TEST(KeyFileStoreTest, OpenFail); |
| 91 | |
| 92 | void ReleaseKeyFile(); |
| 93 | |
| 94 | GLib *glib_; |
Darin Petkov | 86964e0 | 2011-06-29 13:49:28 -0700 | [diff] [blame] | 95 | CryptoProvider crypto_; |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 96 | GKeyFile *key_file_; |
| 97 | FilePath path_; |
Darin Petkov | 86964e0 | 2011-06-29 13:49:28 -0700 | [diff] [blame] | 98 | |
| 99 | DISALLOW_COPY_AND_ASSIGN(KeyFileStore); |
Darin Petkov | 083047b | 2011-06-23 20:42:48 -0700 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | } // namespace shill |
| 103 | |
| 104 | #endif // SHILL_KEY_FILE_STORE_ |