blob: bcbc7bcf3ecf72a591658b3a33f4fdd25cee37fe [file] [log] [blame]
Darin Petkov083047b2011-06-23 20:42:48 -07001// 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 Petkov86964e02011-06-29 13:49:28 -070011#include "shill/crypto_provider.h"
Darin Petkov083047b2011-06-23 20:42:48 -070012#include "shill/glib.h"
13#include "shill/store_interface.h"
14
15namespace 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.
20class KeyFileStore : public StoreInterface {
21 public:
Darin Petkovb2841fd2011-06-30 12:54:12 -070022 explicit KeyFileStore(GLib *glib);
Darin Petkov083047b2011-06-23 20:42:48 -070023 virtual ~KeyFileStore();
24
25 void set_path(const FilePath &path) { path_ = path; }
26 const FilePath &path() const { return path_; }
27
Paul Stewart5dc40aa2011-10-28 19:43:43 -070028 // Returns true if the store exists and is non-empty.
Paul Stewart0756db92012-01-27 08:34:47 -080029 bool IsNonEmpty() const;
Paul Stewart5dc40aa2011-10-28 19:43:43 -070030
Chris Masone9d779932011-08-25 16:33:41 -070031 // 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 Petkov083047b2011-06-23 20:42:48 -070044 // Inherited from StoreInterface.
Chris Masoneb9c00592011-10-06 13:10:39 -070045 virtual bool Flush();
Paul Stewart0756db92012-01-27 08:34:47 -080046 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 Petkov083047b2011-06-23 20:42:48 -070049 virtual bool DeleteKey(const std::string &group, const std::string &key);
50 virtual bool DeleteGroup(const std::string &group);
Paul Stewart5dc40aa2011-10-28 19:43:43 -070051 virtual bool SetHeader(const std::string &header);
Darin Petkov083047b2011-06-23 20:42:48 -070052 virtual bool GetString(const std::string &group,
53 const std::string &key,
Paul Stewart0756db92012-01-27 08:34:47 -080054 std::string *value) const;
Darin Petkov083047b2011-06-23 20:42:48 -070055 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 Stewart0756db92012-01-27 08:34:47 -080060 bool *value) const;
Darin Petkov083047b2011-06-23 20:42:48 -070061 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 Stewart0756db92012-01-27 08:34:47 -080066 int *value) const;
Darin Petkov083047b2011-06-23 20:42:48 -070067 virtual bool SetInt(const std::string &group,
68 const std::string &key,
69 int value);
Paul Stewartdab3b5a2012-07-11 18:25:10 -070070 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 Petkovb2841fd2011-06-30 12:54:12 -070076 virtual bool GetStringList(const std::string &group,
77 const std::string &key,
Paul Stewart0756db92012-01-27 08:34:47 -080078 std::vector<std::string> *value) const;
Darin Petkovb2841fd2011-06-30 12:54:12 -070079 virtual bool SetStringList(const std::string &group,
80 const std::string &key,
81 const std::vector<std::string> &value);
Darin Petkov86964e02011-06-29 13:49:28 -070082 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 Petkov083047b2011-06-23 20:42:48 -070088 private:
89 FRIEND_TEST(KeyFileStoreTest, OpenClose);
90 FRIEND_TEST(KeyFileStoreTest, OpenFail);
91
92 void ReleaseKeyFile();
93
94 GLib *glib_;
Darin Petkov86964e02011-06-29 13:49:28 -070095 CryptoProvider crypto_;
Darin Petkov083047b2011-06-23 20:42:48 -070096 GKeyFile *key_file_;
97 FilePath path_;
Darin Petkov86964e02011-06-29 13:49:28 -070098
99 DISALLOW_COPY_AND_ASSIGN(KeyFileStore);
Darin Petkov083047b2011-06-23 20:42:48 -0700100};
101
102} // namespace shill
103
104#endif // SHILL_KEY_FILE_STORE_