b002b27790eef0497fd8588d4c7ce8a31edd8e2b
[platform/framework/web/crosswalk-tizen.git] / src / common / app_db.cc
1 // Copyright 2015 Samsung Electronics Co, Ltd. 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 #include "common/app_db.h"
6
7 #include <app_preference.h>
8 #include <memory>
9
10 #include "common/string_utils.h"
11
12 namespace wrt {
13
14 namespace {
15   const char* kSectionPrefix = "_SECT_";
16   const char* kSectionSuffix = "_SECT_";
17
18 }  // namespace
19
20 class PreferenceAppDB : public AppDB {
21  public:
22   PreferenceAppDB();
23   virtual bool HasKey(const std::string& section,
24                       const std::string& key) const;
25   virtual std::string Get(const std::string& section,
26                           const std::string& key) const;
27   virtual void Set(const std::string& section,
28                    const std::string& key,
29                    const std::string& value);
30   virtual void GetKeys(const std::string& section,
31                        std::list<std::string>* keys) const;
32   virtual void Remove(const std::string& section,
33                       const std::string& key);
34 };
35
36 PreferenceAppDB::PreferenceAppDB() {
37 }
38
39 bool PreferenceAppDB::HasKey(const std::string& section,
40                              const std::string& key) const {
41   bool existed = false;
42   std::string combined_key = kSectionPrefix + section + kSectionSuffix + key;
43   return preference_is_existing(combined_key.c_str(), &existed) == 0 && existed;
44 }
45
46 std::string PreferenceAppDB::Get(const std::string& section,
47                                  const std::string& key) const {
48   std::string combined_key = kSectionPrefix + section + kSectionSuffix + key;
49   char* value;
50   if (preference_get_string(combined_key.c_str(), &value) == 0) {
51     std::unique_ptr<char, decltype(std::free)*> ptr {value, std::free};
52     return std::string(value);
53   }
54   return std::string();
55 }
56
57 void PreferenceAppDB::Set(const std::string& section,
58                           const std::string& key,
59                           const std::string& value) {
60   std::string combined_key = kSectionPrefix + section + kSectionSuffix + key;
61   preference_set_string(combined_key.c_str(), value.c_str());
62 }
63
64 void PreferenceAppDB::GetKeys(const std::string& section,
65                               std::list<std::string>* keys) const {
66   auto callback = [](const char* key, void *user_data) {
67     auto list = static_cast<std::list<std::string>*>(user_data);
68     if (utils::StartsWith(key, list->front())) {
69       list->push_back(key+list->front().size());
70     }
71     return true;
72   };
73   std::string key_prefix = kSectionPrefix + section + kSectionSuffix;
74   keys->push_front(key_prefix);
75   preference_foreach_item(callback, keys);
76   keys->pop_front();
77 }
78
79 void PreferenceAppDB::Remove(const std::string& section,
80                              const std::string& key) {
81   std::string combined_key = kSectionPrefix + section + kSectionSuffix + key;
82   preference_remove(combined_key.c_str());
83 }
84
85
86 AppDB* AppDB::GetInstance() {
87   static PreferenceAppDB instance;
88   return &instance;
89 }
90
91
92
93 }  // namespace wrt