Refactoring AppDB
authorSeungkeun Lee <sngn.lee@samsung.com>
Wed, 13 May 2015 02:13:07 +0000 (11:13 +0900)
committerSeungkeun Lee <sngn.lee@samsung.com>
Wed, 13 May 2015 02:13:07 +0000 (11:13 +0900)
Change-Id: I0b801b78e0158a09daca3210d8f206cea7173109

src/common/CMakeLists.txt
src/common/app_db.cc [new file with mode: 0755]
src/common/app_db.h [new file with mode: 0755]
src/runtime/CMakeLists.txt
src/runtime/app_db.cc [deleted file]
src/runtime/app_db.h [deleted file]
src/runtime/web_application.cc

index 64b7697..1f71db4 100755 (executable)
@@ -32,6 +32,7 @@ SET(TARGET_COMMON_STATIC_SRCS
   ${BASE_SRCDIR}/common/application_data.cc
   ${BASE_SRCDIR}/common/app_control.cc
   ${BASE_SRCDIR}/common/locale_manager.cc
+  ${BASE_SRCDIR}/common/app_db.cc
 )
 
 INCLUDE_DIRECTORIES(${TARGET_COMMON_STATIC_INCS})
diff --git a/src/common/app_db.cc b/src/common/app_db.cc
new file mode 100755 (executable)
index 0000000..322814a
--- /dev/null
@@ -0,0 +1,93 @@
+// Copyright 2015 Samsung Electronics Co, Ltd. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "common/app_db.h"
+
+#include <app_preference.h>
+#include <memory>
+
+#include "common/string_utils.h"
+
+namespace wrt {
+
+namespace {
+  const char* kSectionPrefix = "_SECT_";
+  const char* kSectionSuffix = "_SECT_";
+
+}  // namespace
+
+class PreferenceAppDB : public AppDB {
+ public:
+  PreferenceAppDB();
+  virtual bool HasKey(const std::string& section,
+                      const std::string& key) const;
+  virtual std::string Get(const std::string& section,
+                          const std::string& key) const;
+  virtual void Set(const std::string& section,
+                   const std::string& key,
+                   const std::string& value);
+  virtual void GetKeys(const std::string& section,
+                       std::list<std::string>* keys) const;
+  virtual void Remove(const std::string& section,
+                      const std::string& key);
+};
+
+PreferenceAppDB::PreferenceAppDB() {
+}
+
+bool PreferenceAppDB::HasKey(const std::string& section,
+                             const std::string& key) const {
+  bool existed = false;
+  std::string combined_key = kSectionPrefix + section + kSectionSuffix + key;
+  return preference_is_existing(combined_key.c_str(), &existed) == 0 && existed;
+}
+
+std::string PreferenceAppDB::Get(const std::string& section,
+                                 const std::string& key) const {
+  std::string combined_key = kSectionPrefix + section + kSectionSuffix + key;
+  char* value;
+  if (preference_get_string(combined_key.c_str(), &value) == 0) {
+    std::unique_ptr<char, decltype(std::free)*> ptr {value, std::free};
+    return std::string(value);
+  }
+  return std::string();
+}
+
+void PreferenceAppDB::Set(const std::string& section,
+                          const std::string& key,
+                          const std::string& value) {
+  std::string combined_key = kSectionPrefix + section + kSectionSuffix + key;
+  preference_set_string(combined_key.c_str(), value.c_str());
+}
+
+void PreferenceAppDB::GetKeys(const std::string& section,
+                              std::list<std::string>* keys) const {
+  auto callback = [](const char* key, void *user_data) {
+    auto list = static_cast<std::list<std::string>*>(user_data);
+    if (utils::StartsWith(key, list->front())) {
+      list->push_back(key);
+    }
+    return true;
+  };
+  std::string key_prefix = kSectionPrefix + section + kSectionSuffix;
+  keys->push_front(key_prefix);
+  preference_foreach_item(callback, keys);
+  keys->pop_front();
+}
+
+void PreferenceAppDB::Remove(const std::string& section,
+                             const std::string& key) {
+  std::string combined_key = kSectionPrefix + section + kSectionSuffix + key;
+  preference_remove(combined_key.c_str());
+}
+
+
+AppDB* AppDB::GetInstance() {
+  static PreferenceAppDB instance;
+  return &instance;
+}
+
+
+
+}  // namespace wrt
diff --git a/src/common/app_db.h b/src/common/app_db.h
new file mode 100755 (executable)
index 0000000..fa01ca6
--- /dev/null
@@ -0,0 +1,28 @@
+// Copyright 2015 Samsung Electronics Co, Ltd. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef WRT_RUNTIME_APPDB_H_
+#define WRT_RUNTIME_APPDB_H_
+
+#include <string>
+#include <list>
+
+namespace wrt {
+class AppDB {
+ public:
+  static AppDB* GetInstance();
+  virtual bool HasKey(const std::string& section,
+                      const std::string& key) const = 0;
+  virtual std::string Get(const std::string& section,
+                          const std::string& key) const = 0;
+  virtual void Set(const std::string& section,
+                   const std::string& key,
+                   const std::string& value) = 0;
+  virtual void GetKeys(const std::string& section,
+                       std::list<std::string>* keys) const = 0;
+  virtual void Remove(const std::string& section,
+                      const std::string& key) = 0;
+};
+}  // namespace wrt
+#endif  // WRT_RUNTIME_APPDB_H_
index 38fce98..be15afa 100755 (executable)
@@ -47,7 +47,6 @@ SET(TARGET_RUNTIME_SRCS
   ${BASE_SRCDIR}/runtime/web_view.cc
   ${BASE_SRCDIR}/runtime/web_view_impl.cc
   ${BASE_SRCDIR}/runtime/vibration_manager.cc
-  ${BASE_SRCDIR}/runtime/app_db.cc
   ${BASE_SRCDIR}/runtime/notification_manager.cc
 )
 
diff --git a/src/runtime/app_db.cc b/src/runtime/app_db.cc
deleted file mode 100755 (executable)
index 48ccd33..0000000
+++ /dev/null
@@ -1,58 +0,0 @@
-// Copyright 2015 Samsung Electronics Co, Ltd. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "runtime/app_db.h"
-
-#include <app_preference.h>
-#include <memory>
-
-namespace wrt {
-
-class PreferenceAppDB : public AppDB {
- public:
-  PreferenceAppDB();
-  virtual bool HasKey(const std::string& key) const;
-  virtual std::string Get(const std::string& key) const;
-  virtual void Set(const std::string& key, const std::string& value);
-  virtual void GetKeys(std::list<std::string>* keys) const;
-};
-
-PreferenceAppDB::PreferenceAppDB() {
-}
-
-bool PreferenceAppDB::HasKey(const std::string& key) const {
-  bool existed = false;
-  return preference_is_existing(key.c_str(), &existed) == 0 && existed;
-}
-
-std::string PreferenceAppDB::Get(const std::string& key) const {
-  char* value;
-  if (preference_get_string(key.c_str(), &value) == 0) {
-    std::unique_ptr<char, decltype(std::free)*> ptr {value, std::free};
-    return std::string(value);
-  }
-  return std::string();
-}
-
-void PreferenceAppDB::Set(const std::string& key, const std::string& value) {
-  preference_set_string(key.c_str(), value.c_str());
-}
-
-void PreferenceAppDB::GetKeys(std::list<std::string>* keys) const {
-  auto callback = [](const char* key, void *user_data) {
-    auto list = static_cast<std::list<std::string>*>(user_data);
-    list->push_back(key);
-    return true;
-  };
-  preference_foreach_item(callback, keys);
-}
-
-AppDB* AppDB::GetInstance() {
-  static PreferenceAppDB instance;
-  return &instance;
-}
-
-
-
-}  // namespace wrt
diff --git a/src/runtime/app_db.h b/src/runtime/app_db.h
deleted file mode 100755 (executable)
index 5fe72eb..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-// Copyright 2015 Samsung Electronics Co, Ltd. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef WRT_RUNTIME_APPDB_H_
-#define WRT_RUNTIME_APPDB_H_
-
-#include <string>
-#include <list>
-
-namespace wrt {
-class AppDB {
- public:
-  static AppDB* GetInstance();
-  virtual bool HasKey(const std::string& key) const = 0;
-  virtual std::string Get(const std::string& key) const = 0;
-  virtual void Set(const std::string& key, const std::string& value) = 0;
-  virtual void GetKeys(std::list<std::string>* keys) const = 0;
-};
-}  // namespace wrt
-#endif  // WRT_RUNTIME_APPDB_H_
index dbb07e7..7709690 100755 (executable)
@@ -24,7 +24,7 @@
 #include "common/locale_manager.h"
 #include "common/application_data.h"
 #include "common/resource_manager.h"
-#include "runtime/app_db.h"
+#include "common/app_db.h"
 #include "runtime/notification_manager.h"
 
 namespace wrt {
@@ -93,6 +93,7 @@ const char* kGeolocationPermissionPrefix = "__WRT_GEOPERM_";
 const char* kNotificationPermissionPrefix = "__WRT_NOTIPERM_";
 const char* kQuotaPermissionPrefix = "__WRT_QUOTAPERM_";
 const char* kCertificateAllowPrefix = "__WRT_CERTIPERM_";
+const char* kDBPrivateSection = "private";
 
 
 bool FindPrivilege(wrt::ApplicationData* app_data,
@@ -581,7 +582,8 @@ void WebApplication::OnNotificationPermissionRequest(
     const std::string& url,
     std::function<void(bool)> result_handler) {
   auto db = AppDB::GetInstance();
-  std::string reminder = db->Get(kNotificationPermissionPrefix + url);
+  std::string reminder = db->Get(kDBPrivateSection,
+                                 kNotificationPermissionPrefix + url);
   if (reminder == "allowed") {
     result_handler(true);
   } else if (reminder == "denied") {
@@ -609,7 +611,8 @@ void WebApplication::OnGeolocationPermissionRequest(
     const std::string& url,
     std::function<void(bool)> result_handler) {
   auto db = AppDB::GetInstance();
-  std::string reminder = db->Get(kGeolocationPermissionPrefix + url);
+  std::string reminder = db->Get(kDBPrivateSection,
+                                 kGeolocationPermissionPrefix + url);
   if (reminder == "allowed") {
     result_handler(true);
   } else if (reminder == "denied") {
@@ -637,7 +640,8 @@ void WebApplication::OnQuotaExceed(
     const std::string& url,
     std::function<void(bool)> result_handler) {
   auto db = AppDB::GetInstance();
-  std::string reminder = db->Get(kQuotaPermissionPrefix + url);
+  std::string reminder = db->Get(kDBPrivateSection,
+                                 kQuotaPermissionPrefix + url);
   if (reminder == "allowed") {
     result_handler(true);
   } else if (reminder == "denied") {
@@ -673,7 +677,8 @@ void WebApplication::OnCertificateAllowRequest(
       const std::string& pem,
       std::function<void(bool allow)> result_handler) {
   auto db = AppDB::GetInstance();
-  std::string reminder = db->Get(kCertificateAllowPrefix + pem);
+  std::string reminder = db->Get(kDBPrivateSection,
+                                 kCertificateAllowPrefix + pem);
   if (reminder == "allowed") {
     result_handler(true);
   } else if (reminder == "denied") {