[Preference] -part of the API implemented
authorAndrzej Popowski <a.popowski@samsung.com>
Fri, 1 Apr 2016 13:31:52 +0000 (15:31 +0200)
committerLukasz Bardeli <l.bardeli@samsung.com>
Mon, 11 Apr 2016 10:40:09 +0000 (12:40 +0200)
Change-Id: Ibb4caf4496ec64e7ec798ce7a0acbdfba55ae717
Signed-off-by: Andrzej Popowski <a.popowski@samsung.com>
packaging/webapi-plugins.spec
src/preference/preference.gyp [new file with mode: 0644]
src/preference/preference_api.js [new file with mode: 0644]
src/preference/preference_extension.cc [new file with mode: 0644]
src/preference/preference_extension.h [new file with mode: 0644]
src/preference/preference_instance.cc [new file with mode: 0644]
src/preference/preference_instance.h [new file with mode: 0644]
src/preference/preference_manager.cc [new file with mode: 0644]
src/preference/preference_manager.h [new file with mode: 0644]
src/tizen-wrt.gyp

index 05d1a7807413a7d539abe04e29c5de14849a233f..80731091a2e534763618df5bbae3024de3ffebc6 100644 (file)
@@ -80,6 +80,7 @@ Source0:    %{name}-%{version}.tar.gz
 %define tizen_feature_notification_support            1
 %define tizen_feature_package_support                 1
 %define tizen_feature_power_support                   1
+%define tizen_feature_preference_support              1
 %define tizen_feature_push_support                    1
 %if 0%{?tizen_is_emulator}
 %define tizen_feature_se_support                      0
@@ -178,6 +179,7 @@ Source0:    %{name}-%{version}.tar.gz
 %define tizen_feature_notification_support            1
 %define tizen_feature_package_support                 1
 %define tizen_feature_power_support                   1
+%define tizen_feature_preference_support              1
 %define tizen_feature_push_support                    1
 %if 0%{?model_build_feature_smartcard }
 %define tizen_feature_se_support                      1
@@ -245,6 +247,7 @@ Source0:    %{name}-%{version}.tar.gz
 %define tizen_feature_notification_support            0
 %define tizen_feature_package_support                 1
 %define tizen_feature_power_support                   0
+%define tizen_feature_preference_support              0
 %define tizen_feature_push_support                    0
 %define tizen_feature_se_support                      0
 %define tizen_feature_sensor_support                  0
@@ -479,6 +482,7 @@ GYP_OPTIONS="$GYP_OPTIONS -Dtizen_feature_nfc_support=%{?tizen_feature_nfc_suppo
 GYP_OPTIONS="$GYP_OPTIONS -Dtizen_feature_notification_support=%{?tizen_feature_notification_support}"
 GYP_OPTIONS="$GYP_OPTIONS -Dtizen_feature_package_support=%{?tizen_feature_package_support}"
 GYP_OPTIONS="$GYP_OPTIONS -Dtizen_feature_power_support=%{?tizen_feature_power_support}"
+GYP_OPTIONS="$GYP_OPTIONS -Dtizen_feature_preference_support=%{?tizen_feature_preference_support}"
 GYP_OPTIONS="$GYP_OPTIONS -Dtizen_feature_push_support=%{?tizen_feature_push_support}"
 GYP_OPTIONS="$GYP_OPTIONS -Dtizen_feature_sap_support=%{?tizen_feature_sap_support}"
 GYP_OPTIONS="$GYP_OPTIONS -Dtizen_feature_sensor_support=%{?tizen_feature_sensor_support}"
diff --git a/src/preference/preference.gyp b/src/preference/preference.gyp
new file mode 100644 (file)
index 0000000..0b5161b
--- /dev/null
@@ -0,0 +1,32 @@
+{
+  'includes':[
+    '../common/common.gypi',
+  ],
+  'targets': [
+    {
+      'target_name': 'tizen_preference',
+      'type': 'loadable_module',
+      'dependencies': [
+        '../common/common.gyp:tizen_common',
+      ],
+      'sources': [
+        'preference_api.js',
+        'preference_extension.cc',
+        'preference_extension.h',
+        'preference_instance.cc',
+        'preference_instance.h',
+        'preference_manager.cc',
+        'preference_manager.h',
+      ],
+      'conditions': [
+        ['tizen == 1', {
+          'variables': {
+            'packages': [
+              'capi-appfw-application',
+            ]
+          },
+        }],
+      ],
+    },
+  ],
+}
diff --git a/src/preference/preference_api.js b/src/preference/preference_api.js
new file mode 100644 (file)
index 0000000..9ddd75e
--- /dev/null
@@ -0,0 +1,180 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+
+
+var validator_ = xwalk.utils.validator;
+var type_ = xwalk.utils.type;
+var types_ = validator_.Types;
+var native_ = new xwalk.utils.NativeManager(extension);
+
+
+function PreferenceManager() {
+}
+
+
+PreferenceManager.prototype.getAll = function() {
+  // ...
+};
+
+PreferenceManager.prototype.setValue = function() {
+  var args = validator_.validateArgs(arguments, [
+    { name: 'key', type: types_.STRING },
+    { name: 'value', type: types_.SIMPLE_TYPE }
+  ]);
+
+  var result = native_.callSync('PreferenceManager_setValue',
+                                {
+                                   key: args.key,
+                                   value: args.value
+                                }
+  );
+
+  if (native_.isFailure(result)) {
+    throw native_.getErrorObject(result);
+  }
+};
+
+
+PreferenceManager.prototype.getValue = function() {
+  var args = validator_.validateArgs(arguments, [
+    { name: 'key', type: types_.STRING }
+  ]);
+
+  var result = native_.callSync('PreferenceManager_getValue', { key: args.key });
+
+  if (native_.isFailure(result)) {
+    throw native_.getErrorObject(result);
+  } else {
+    return native_.getResultObject(result);
+  }
+};
+
+
+PreferenceManager.prototype.remove = function() {
+  var args = validator_.validateArgs(arguments, [
+    { name: 'key', type: types_.STRING }
+  ]);
+
+  var result = native_.callSync('PreferenceManager_remove', { key: args.key });
+
+  if (native_.isFailure(result)) {
+    throw native_.getErrorObject(result);
+  }
+};
+
+
+PreferenceManager.prototype.removeAll = function() {
+  var result = native_.callSync('PreferenceManager_removeAll', {});
+
+  if (native_.isFailure(result)) {
+    throw native_.getErrorObject(result);
+  }
+};
+
+
+PreferenceManager.prototype.exists = function() {
+  var args = validator_.validateArgs(arguments, [
+    { name: 'key', type: types_.STRING }
+  ]);
+
+  var result = native_.callSync('PreferenceManager_exists', { key: args.key });
+
+  if (native_.isFailure(result)) {
+    throw native_.getErrorObject(result);
+  } else {
+    return native_.getResultObject(result);
+  }
+};
+
+
+var PREFERENCE_LISTENER = 'PREFERENCE_CHANGED';
+
+
+function PreferenceListeners() {
+  var that = this;
+  this.appListener = function (result) {
+    var data = native_.getResultObject(result);
+    var key = data.key;
+    var value = data.value;
+
+    if (that.instances[key]) {
+      var listener = that.instances[key];
+      if (type_.isFunction(listener)) {
+        listener({ key: key, value: value });
+      }
+    }
+  };
+}
+
+
+PreferenceListeners.prototype.instances = {};
+
+
+PreferenceListeners.prototype.addListener = function(key, listener) {
+  if (type_.isEmptyObject(this.instances)) {
+    native_.addListener(PREFERENCE_LISTENER, this.appListener);
+  }
+
+  this.instances[key] = listener;
+};
+
+
+PreferenceListeners.prototype.removeListener = function(key) {
+  if (this.instances[key]) {
+    delete this.instances[key];
+    if (type_.isEmptyObject(this.instances)) {
+      native_.removeListener(PREFERENCE_LISTENER);
+    }
+  }
+};
+
+
+var _preferenceListeners = new PreferenceListeners();
+
+
+PreferenceManager.prototype.setChangeListener = function() {
+  var args = validator_.validateArgs(arguments, [
+    { name: 'key', type: types_.STRING },
+    { name: 'listener', type: types_.FUNCTION }
+  ]);
+
+  var result = native_.callSync('PreferenceManager_setChangeListener', { key: args.key });
+  if (native_.isFailure(result)) {
+    throw native_.getErrorObject(result);
+  }
+
+  _preferenceListeners.addListener(args.key, args.listener);
+}
+
+
+PreferenceManager.prototype.unsetChangeListener = function() {
+  var args = validator_.validateArgs(arguments, [
+    { name: 'key', type: types_.STRING }
+  ]);
+
+  var result = native_.callSync('PreferenceManager_unsetChangeListener', { key: args.key });
+
+  if (native_.isFailure(result)) {
+    throw native_.getErrorObject(result);
+  }
+
+  _preferenceListeners.removeListener(args.key);
+};
+
+
+// Exports
+exports = new PreferenceManager();
+
diff --git a/src/preference/preference_extension.cc b/src/preference/preference_extension.cc
new file mode 100644 (file)
index 0000000..45d388f
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+
+#include "preference/preference_extension.h"
+
+#include "preference/preference_instance.h"
+
+// This will be generated from preference_api.js
+extern const char kSource_preference_api[];
+
+common::Extension* CreateExtension() {
+  return new PreferenceExtension;
+}
+
+PreferenceExtension::PreferenceExtension() {
+  SetExtensionName("tizen.preference");
+  SetJavaScriptAPI(kSource_preference_api);
+}
+
+PreferenceExtension::~PreferenceExtension() {}
+
+common::Instance* PreferenceExtension::CreateInstance() {
+  return new extension::preference::PreferenceInstance;
+}
diff --git a/src/preference/preference_extension.h b/src/preference/preference_extension.h
new file mode 100644 (file)
index 0000000..1df8fd1
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+
+#ifndef PREFERENCE_PREFERENCE_EXTENSION_H_
+#define PREFERENCE_PREFERENCE_EXTENSION_H_
+
+#include "common/extension.h"
+
+class PreferenceExtension : public common::Extension {
+ public:
+  PreferenceExtension();
+  virtual ~PreferenceExtension();
+
+ private:
+  virtual common::Instance* CreateInstance();
+};
+
+#endif // PREFERENCE_PREFERENCE_EXTENSION_H_
+
diff --git a/src/preference/preference_instance.cc b/src/preference/preference_instance.cc
new file mode 100644 (file)
index 0000000..1b6d2c5
--- /dev/null
@@ -0,0 +1,135 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+
+#include "common/logger.h"
+#include "common/picojson.h"
+#include "common/task-queue.h"
+#include "common/tools.h"
+#include "common/scope_exit.h"
+#include "preference/preference_instance.h"
+
+
+namespace extension {
+namespace preference {
+
+namespace {
+const char* kKey = "key";
+const char* kValue = "value";
+const common::ListenerToken kPreferenceChangeListenerToken{"PREFERENCE_CHANGED"};
+} // namespace
+
+#define CHECK_EXIST(args, name) \
+  if (args.end() == args.find(name)) { \
+    return common::TypeMismatchError(std::string(name) + " is required argument"); \
+  }
+
+
+PreferenceInstance::PreferenceInstance() {
+  ScopeLogger();
+
+  using std::placeholders::_1;
+  using std::placeholders::_2;
+
+#define REGISTER(c,x) \
+    RegisterSyncHandler(c, std::bind(&PreferenceInstance::x, this, _1));
+  REGISTER("PreferenceManager_setValue", SetValue);
+  REGISTER("PreferenceManager_getValue", GetValue);
+  REGISTER("PreferenceManager_remove", Remove);
+  REGISTER("PreferenceManager_removeAll", RemoveAll);
+  REGISTER("PreferenceManager_exists", Exists);
+  REGISTER("PreferenceManager_setChangeListener", SetChangeListener);
+  REGISTER("PreferenceManager_unsetChangeListener", UnsetChangeListener);
+#undef REGISTER
+#define REGISTER_ASYNC(c,x) \
+    RegisterHandler(c, std::bind(&PreferenceInstance::x, this, _1, _2));
+  REGISTER_ASYNC("PreferenceManager_getAll", GetAll);
+#undef REGISTER_ASYNC
+}
+
+PreferenceInstance::~PreferenceInstance()
+{
+  ScopeLogger();
+}
+
+common::TizenResult PreferenceInstance::GetAll(const picojson::object& args, const common::AsyncToken& token) {
+  ScopeLogger();
+  // ...
+  return common::TizenSuccess();
+}
+
+common::TizenResult PreferenceInstance::SetValue(const picojson::object& args) {
+  ScopeLogger();
+
+  CHECK_EXIST(args, kKey)
+  CHECK_EXIST(args, kValue)
+
+  const auto& key = args.find(kKey)->second.get<std::string>();
+  const auto& value = args.find(kValue)->second;
+
+  return manager_.SetValue(key, value);
+}
+
+common::TizenResult PreferenceInstance::GetValue(const picojson::object& args) {
+  ScopeLogger();
+
+  CHECK_EXIST(args, kKey)
+  const auto& key = args.find(kKey)->second.get<std::string>();
+  return manager_.GetValue(key);
+}
+
+common::TizenResult PreferenceInstance::Remove(const picojson::object& args) {
+  ScopeLogger();
+
+  CHECK_EXIST(args, kKey)
+  const auto& key = args.find(kKey)->second.get<std::string>();
+  return manager_.Remove(key);
+}
+
+common::TizenResult PreferenceInstance::RemoveAll(const picojson::object& args) {
+  ScopeLogger();
+  return manager_.RemoveAll();
+}
+
+common::TizenResult PreferenceInstance::Exists(const picojson::object& args) {
+  ScopeLogger();
+
+  CHECK_EXIST(args, kKey)
+  const auto& key = args.find(kKey)->second.get<std::string>();
+  return manager_.Exists(key);
+}
+
+common::TizenResult PreferenceInstance::SetChangeListener(const picojson::object& args) {
+  ScopeLogger();
+  CHECK_EXIST(args, kKey)
+  const auto& key = args.find(kKey)->second.get<std::string>();
+
+  common::PostCallback callback = [this](const common::TizenResult&, const picojson::value& v) {
+    Post(kPreferenceChangeListenerToken, common::TizenSuccess{v});
+  };
+
+  return manager_.SetChangeListener(key, callback);
+}
+
+common::TizenResult PreferenceInstance::UnsetChangeListener(const picojson::object& args) {
+  ScopeLogger();
+
+  CHECK_EXIST(args, kKey)
+  const auto& key = args.find(kKey)->second.get<std::string>();
+  return manager_.UnsetChangeListener(key);
+}
+
+} // namespace preference
+} // namespace extension
diff --git a/src/preference/preference_instance.h b/src/preference/preference_instance.h
new file mode 100644 (file)
index 0000000..946bc72
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+
+#ifndef PREFERENCE_PREFERENCE_INSTANCE_H_
+#define PREFERENCE_PREFERENCE_INSTANCE_H_
+
+#include "common/tizen_instance.h"
+#include "preference/preference_manager.h"
+
+namespace extension {
+namespace preference {
+
+class PreferenceInstance : public common::TizenInstance {
+public:
+  PreferenceInstance();
+  virtual ~PreferenceInstance();
+
+private:
+  common::TizenResult GetAll(const picojson::object& args, const common::AsyncToken& token);
+  common::TizenResult SetValue(const picojson::object& args);
+  common::TizenResult GetValue(const picojson::object& args);
+  common::TizenResult Remove(const picojson::object& args);
+  common::TizenResult RemoveAll(const picojson::object& args);
+  common::TizenResult Exists(const picojson::object& args);
+  common::TizenResult SetChangeListener(const picojson::object& args);
+  common::TizenResult UnsetChangeListener(const picojson::object& args);
+
+  PreferenceManager manager_;
+};
+
+} // namespace preference
+} // namespace extension
+
+#endif // PREFERENCE_PREFERENCE_INSTANCE_H_
diff --git a/src/preference/preference_manager.cc b/src/preference/preference_manager.cc
new file mode 100644 (file)
index 0000000..a4d889d
--- /dev/null
@@ -0,0 +1,212 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+
+#include <app_preference.h>
+#include "common/logger.h"
+#include "common/tools.h"
+#include "preference/preference_manager.h"
+#include "preference/preference_instance.h"
+
+namespace extension {
+namespace preference {
+
+namespace {
+const char* kKey = "key";
+const char* kValue = "value";
+
+common::TizenResult MakeErrResult(int ret, const char* err_msg) {
+  LoggerE("%s", err_msg);
+  switch (ret) {
+    case PREFERENCE_ERROR_INVALID_PARAMETER:
+      return common::InvalidValuesError(std::string(err_msg));
+
+    case PREFERENCE_ERROR_OUT_OF_MEMORY:
+      return common::AbortError(std::string(err_msg));
+
+    case PREFERENCE_ERROR_IO_ERROR:
+      return common::IoError(std::string(err_msg));
+
+    case PREFERENCE_ERROR_NO_KEY:
+      return common::NotFoundError(std::string(err_msg));
+
+    default:
+      return common::AbortError(std::string(err_msg));
+  }
+}
+
+int GetValueInternal(const std::string& key, picojson::value* val) {
+  char* result_str = nullptr;
+  int ret = preference_get_string(key.c_str(), &result_str);
+
+  if (ret == PREFERENCE_ERROR_NONE) {
+    *val = picojson::value(std::string(result_str));
+    free(result_str);
+  } else {
+    double result_double = 0;
+    ret = preference_get_double(key.c_str(), &result_double);
+
+    if (ret == PREFERENCE_ERROR_NONE) {
+      *val = picojson::value(result_double);
+    } else {
+      bool result_bool = false;
+      ret = preference_get_boolean(key.c_str(), &result_bool);
+
+      if (ret == PREFERENCE_ERROR_NONE) {
+        *val = picojson::value(result_bool);
+      } else {
+        int result_int = 0;
+        ret = preference_get_int(key.c_str(), &result_int);
+
+        if (ret == PREFERENCE_ERROR_NONE) {
+          *val = picojson::value(static_cast<double>(result_int));
+        }
+      }
+    }
+  }
+  return ret;
+}
+} // namespace
+
+common::TizenResult PreferenceManager::SetValue(const std::string& key, const picojson::value& value) {
+  ScopeLogger();
+
+  int ret = PREFERENCE_ERROR_NONE;
+
+  if (value.is<bool>()) {
+    ret = preference_set_boolean(key.c_str(), value.get<bool>());
+  } else if (value.is<double>()) {
+    ret = preference_set_double(key.c_str(), value.get<double>());
+  } else if (value.is<std::string>()) {
+    ret = preference_set_string(key.c_str(), value.get<std::string>().c_str());
+  } else {
+    ret = PREFERENCE_ERROR_INVALID_PARAMETER;
+  }
+
+  if (ret == PREFERENCE_ERROR_NONE) {
+    return common::TizenSuccess();
+  } else {
+    return MakeErrResult(ret, "preference_set_ function error");
+  }
+}
+
+common::TizenResult PreferenceManager::GetValue(const std::string& key) {
+  ScopeLogger();
+
+  picojson::value val;
+
+  int ret = GetValueInternal(key, &val);
+
+  if (ret == PREFERENCE_ERROR_NONE) {
+    return common::TizenSuccess(val);
+  } else {
+    return MakeErrResult(ret, "preference_set_ function error");
+  }
+}
+
+common::TizenResult PreferenceManager::Remove(const std::string& key) {
+  ScopeLogger();
+
+  int ret = preference_remove(key.c_str());
+
+  if (ret == PREFERENCE_ERROR_NONE) {
+    return common::TizenSuccess();
+  } else {
+    return MakeErrResult(ret, "preference_remove function error");
+  }
+}
+
+common::TizenResult PreferenceManager::RemoveAll() {
+  ScopeLogger();
+
+  int ret = preference_remove_all();
+
+  if (ret == PREFERENCE_ERROR_NONE) {
+    return common::TizenSuccess();
+  } else {
+    return MakeErrResult(ret, "preference_remove_all function error");
+  }
+}
+
+common::TizenResult PreferenceManager::Exists(const std::string& key) {
+  ScopeLogger();
+
+  bool is_existing = false;
+  int ret = preference_is_existing(key.c_str(), &is_existing);
+
+  if (ret == PREFERENCE_ERROR_NONE) {
+    return common::TizenSuccess(picojson::value(is_existing));
+  } else {
+    return MakeErrResult(ret, "preference_is_existing function error");
+  }
+}
+
+common::TizenResult PreferenceManager::SetChangeListener(const std::string& key,
+                                                         common::PostCallback callback) {
+  ScopeLogger();
+
+  if (!post_callback_) {
+    post_callback_ = callback;
+  }
+
+  int ret = preference_set_changed_cb(key.c_str(), ChangedCb, (void*) this);
+
+  if (ret == PREFERENCE_ERROR_NONE) {
+    return common::TizenSuccess();
+  } else {
+    return MakeErrResult(ret, "preference_set_changed_cb function error");
+  }
+}
+
+common::TizenResult PreferenceManager::UnsetChangeListener(const std::string& key) {
+  ScopeLogger();
+
+  if (post_callback_) {
+    int ret = preference_unset_changed_cb(key.c_str());
+
+    if (ret == PREFERENCE_ERROR_NONE) {
+      return common::TizenSuccess();
+    } else {
+      return MakeErrResult(ret, "preference_unset_changed_cb function error");
+    }
+  } else {
+    return common::TizenSuccess();
+  }
+}
+
+void PreferenceManager::ChangedCb(const char* key, void* user_data) {
+  ScopeLogger();
+
+  PreferenceManager* manager = static_cast<PreferenceManager*>(user_data);
+
+  picojson::value result_val{picojson::object{}};
+  picojson::object& result_obj = result_val.get<picojson::object>();
+
+  result_obj.insert(std::make_pair(kKey, picojson::value(std::string(key))));
+
+  picojson::value val;
+
+  if (GetValueInternal(key, &val) == PREFERENCE_ERROR_NONE) {
+    result_obj.insert(std::make_pair(kValue, val));
+    if (manager->post_callback_) {
+      manager->post_callback_(common::TizenSuccess(), result_val);
+    }
+  } else {
+    LoggerE("preference_set_ function error");
+  }
+}
+
+} // namespace preference
+} // namespace extension
diff --git a/src/preference/preference_manager.h b/src/preference/preference_manager.h
new file mode 100644 (file)
index 0000000..d68e230
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+
+#ifndef PREFERENCE_PREFERENCE_MANAGER_H_
+#define PREFERENCE_PREFERENCE_MANAGER_H_
+
+#include "common/picojson.h"
+#include "common/tizen_instance.h"
+
+namespace extension {
+namespace preference {
+
+class PreferenceManager {
+public:
+  common::TizenResult SetValue(const std::string& key, const picojson::value& value);
+  common::TizenResult GetValue(const std::string& key);
+  common::TizenResult Remove(const std::string& key);
+  common::TizenResult RemoveAll(void);
+  common::TizenResult Exists(const std::string& key);
+  common::TizenResult SetChangeListener(const std::string& key, common::PostCallback callback);
+  common::TizenResult UnsetChangeListener(const std::string& key);
+
+private:
+  static void ChangedCb(const char* key, void* user_data);
+  common::PostCallback post_callback_;
+};
+
+} // namespace preference
+} // namespace extension
+
+#endif // PREFERENCE_PREFERENCE_MANAGER_H_
index 4d2a4837ae92c884a17f8adfba12e0a2be98ea48..a784dec543d98b6907dfde7437d3692fee9ca13b 100755 (executable)
             ],
           },
         ],
+        [
+          'tizen_feature_preference_support==1', {
+            'dependencies': [
+              'preference/preference.gyp:*',
+            ],
+          },
+        ],
         [
           'tizen_feature_push_support==1', {
             'dependencies': [