revise handling callback
authorYoungjae Shin <yj99.shin@samsung.com>
Tue, 5 Nov 2019 07:55:58 +0000 (16:55 +0900)
committerYoungjae Shin <yj99.shin@samsung.com>
Thu, 19 Mar 2020 04:30:37 +0000 (13:30 +0900)
12 files changed:
unittests/mdsp_test_vconf.cpp
vconf/VconfAction.cpp [new file with mode: 0644]
vconf/VconfAction.h [new file with mode: 0644]
vconf/VconfActionBool.cpp
vconf/VconfActionBool.h
vconf/VconfActionDbl.cpp
vconf/VconfActionDbl.h
vconf/VconfActionInt.cpp
vconf/VconfActionInt.h
vconf/VconfActionStr.cpp
vconf/VconfActionStr.h
vconf/VconfPlugin.cpp

index c90023b..9f80af0 100644 (file)
@@ -43,28 +43,28 @@ protected:
                cb2Called = false;
        }
 
-       static void shoudNotBeCalled(const std::string &key, void *userData)
+       static void shoudNotBeCalled(void *userData)
        {
-               ERR("This Callback(%s) should not be called", key.c_str());
+               ERR("This Callback(%s) should not be called", (char*)userData);
                g_main_loop_quit(loop);
                GTEST_FAIL();
        }
 
-       static void valChangedCb(const std::string &key, void *userData)
+       static void valChangedCb(void *userData)
        {
-               DBG("%s changed callback called!", key.c_str());
+               DBG("%s changed callback called!", (char*)userData);
                g_main_loop_quit(loop);
        }
 
-       static void callback1(const std::string &key, void *userData)
+       static void callback1(void *userData)
        {
-               DBG("%s changed callback called!", key.c_str());
+               DBG("%s changed callback called!", (char*)userData);
                cb1Called = true;
        }
 
-       static void callback2(const std::string &key, void *userData)
+       static void callback2(void *userData)
        {
-               DBG("%s changed callback called!", key.c_str());
+               DBG("%s changed callback called!", (char*)userData);
                cb2Called = true;
                g_main_loop_quit(loop);
        }
@@ -181,20 +181,26 @@ TEST_F(VconfPluginTest, VconfStr)
 TEST_F(VconfPluginTest, callbackPluginVconf)
 {
        int oldVal = 0;
+       const char *key = "db.setting.psmode";
        int ret = vconf_get_int("db/setting/psmode", &oldVal);
        EXPECT_EQ(ret, 0);
 
-       ret = plugin->setChangedCallback(valChangedCb, "db.setting.psmode", nullptr);
+       PluginAction *action;
+       ret = plugin->set(key, 0, &action);
+       EXPECT_EQ(ret, MODES_ERROR_NONE);
+
+       ret = action->setChangedCallback(valChangedCb, (void*)key);
        EXPECT_EQ(ret, MODES_ERROR_NONE);
        g_idle_add(changedCallbackidler, nullptr);
        g_main_loop_run(loop);
 
-       ret = plugin->unSetChangedCallback(valChangedCb, "db.setting.psmode", nullptr);
+       // It should be removed by the changed callback procedure.
+       ret = action->unSetChangedCallback(valChangedCb, (void*)key);
        EXPECT_EQ(ret, MODES_ERROR_NO_DATA);
 
-       ret = plugin->setChangedCallback(shoudNotBeCalled, "db.setting.psmode", nullptr);
+       ret = action->setChangedCallback(shoudNotBeCalled, (void*)key);
        EXPECT_EQ(ret, MODES_ERROR_NONE);
-       ret = plugin->unSetChangedCallback(shoudNotBeCalled, "db.setting.psmode", nullptr);
+       ret = action->unSetChangedCallback(shoudNotBeCalled, (void*)key);
        EXPECT_EQ(ret, MODES_ERROR_NONE);
        vconf_set_int("db/setting/psmode", oldVal);
 }
@@ -202,20 +208,25 @@ TEST_F(VconfPluginTest, callbackPluginVconf)
 TEST_F(VconfPluginTest, callbackPluginVconfReset)
 {
        int ret;
-       ret = plugin->set("db.setting.psmode", 4, nullptr);
+       const char *key = "db.setting.psmode";
+
+       PluginAction *action1;
+       ret = plugin->set(key, 4, &action1);
        EXPECT_EQ(ret, MODES_ERROR_NONE);
-       ret = plugin->setChangedCallback(callback1, "db.setting.psmode", nullptr);
+
+       ret = action1->setChangedCallback(callback1, nullptr);
        EXPECT_EQ(ret, MODES_ERROR_NONE);
-       ret = plugin->set("db.setting.psmode", 2, nullptr);
+       PluginAction *action2;
+       ret = plugin->set(key, 2, &action2);
        EXPECT_EQ(ret, MODES_ERROR_NONE);
        EXPECT_TRUE(cb1Called);
-       ret = plugin->setChangedCallback(callback2, "db.setting.psmode", nullptr);
+       ret = action2->setChangedCallback(callback2, nullptr);
        EXPECT_EQ(ret, MODES_ERROR_NONE);
        g_idle_add(changedCallbackidler, nullptr);
        g_main_loop_run(loop);
        EXPECT_TRUE(cb2Called);
 
-       ret = plugin->unSetChangedCallback(callback2, "db.setting.psmode", nullptr);
+       ret = action2->unSetChangedCallback(callback2, nullptr);
        EXPECT_EQ(ret, MODES_ERROR_NO_DATA);
        vconf_set_int("db/setting/psmode", 0);
 }
diff --git a/vconf/VconfAction.cpp b/vconf/VconfAction.cpp
new file mode 100644 (file)
index 0000000..e31d745
--- /dev/null
@@ -0,0 +1,104 @@
+/*
+ * Copyright (c) 2019 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 "VconfAction.h"
+
+#include <string>
+#include <algorithm>
+#include <vconf.h>
+#include "plugin-log.h"
+
+MODES_NAMESPACE_USE;
+
+std::map<std::string, VconfAction::CallbackData> VconfAction::callbackMap;
+
+VconfAction::VconfAction(const std::string &name, const std::string &vconfKey)
+       : PluginAction(name), key(vconfKey)
+{
+}
+
+int VconfAction::setChangedCallback(valueChangedCB callback, void *userData)
+{
+       callbackMap[key].userData = userData;
+       callbackMap[key].callback = callback;
+
+       int ret = vconf_notify_key_changed(key.c_str(), vconfChangedCallback, &callbackMap[key]);
+       if (VCONF_OK != ret) {
+               ERR("vconf_notify_key_changed(%s) Fail(%s)", key.c_str(), get_error_message(ret));
+               return MODES_ERROR_SYSTEM;
+       }
+
+       DBG("setChangedCallback(%s) Success", key.c_str());
+       return MODES_ERROR_NONE;
+}
+
+int VconfAction::unSetChangedCallback(valueChangedCB callback, void * userData)
+{
+       auto found = callbackMap.find(key);
+       if (found == callbackMap.end()) {
+               ERR("No Changed Callback(%s)", key.c_str());
+               return MODES_ERROR_NO_DATA;
+       }
+
+       callbackMap.erase(found);
+       int ret = vconf_ignore_key_changed(key.c_str(), vconfChangedCallback);
+       if (VCONF_OK != ret) {
+               ERR("vconf_ignore_key_changed(%s) Fail(%s)", key.c_str(), get_error_message(ret));
+               return MODES_ERROR_SYSTEM;
+       }
+
+       DBG("unSetChangedCallback(%s) Success", key.c_str());
+       return MODES_ERROR_NONE;
+}
+
+int VconfAction::handleChange()
+{
+       auto found = callbackMap.find(key);
+       if (callbackMap.end() != found) {
+               DBG("Acition(%s) already exist", key.c_str());
+               found->second.callback(found->second.userData);
+
+               callbackMap.erase(found);
+               int ret = vconf_ignore_key_changed(key.c_str(), vconfChangedCallback);
+               if (VCONF_OK != ret) {
+                       ERR("vconf_ignore_key_changed(%s) Fail(%s)", key.c_str(), get_error_message(ret));
+                       return MODES_ERROR_SYSTEM;
+               }
+       }
+       return MODES_ERROR_NONE;
+}
+
+void VconfAction::vconfChangedCallback(keynode_t *node, void *userData)
+{
+       CallbackData *cbData = (CallbackData*)userData;
+
+       RET_IF(NULL == userData);
+
+       auto found = callbackMap.find(node->keyname);
+       if (&(found->second) != cbData) {
+               ERR("Unknown callbackData(%s)", node->keyname);
+               return;
+       }
+
+       std::string newKey(node->keyname);
+       std::replace(newKey.begin(), newKey.end(), '/', '.');
+       cbData->callback(cbData->userData);
+       DBG("Action(%s) is Changed", node->keyname);
+
+       callbackMap.erase(found);
+       int ret = vconf_ignore_key_changed(node->keyname, vconfChangedCallback);
+       if (VCONF_OK != ret)
+               ERR("vconf_ignore_key_changed(%s) Fail(%s)", node->keyname, get_error_message(ret));
+}
diff --git a/vconf/VconfAction.h b/vconf/VconfAction.h
new file mode 100644 (file)
index 0000000..fe374c2
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2019 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.
+ */
+#pragma once
+
+#include <map>
+#include <string>
+#include <vconf.h>
+#include <PluginAction.h>
+#include "plugin-def.h"
+
+MODES_NAMESPACE_BEGIN
+
+class VconfAction : public PluginAction {
+public:
+       VconfAction(const std::string &name, const std::string &vconfKey);
+       virtual ~VconfAction() = default;
+
+       int setChangedCallback(valueChangedCB callback, void *userData) override;
+       int unSetChangedCallback(valueChangedCB callback, void *userData) override;
+protected:
+       int handleChange();
+       std::string key;
+private:
+       struct CallbackData {
+               void *userData;
+               valueChangedCB callback;
+       };
+       static void vconfChangedCallback(keynode_t *node, void *userData);
+       static std::map<std::string, CallbackData> callbackMap;
+};
+
+MODES_NAMESPACE_END
+
index e61b5ab..6062db2 100644 (file)
@@ -21,8 +21,8 @@
 
 MODES_NAMESPACE_USE;
 
-VconfActionBool::VconfActionBool(const std::string &vconfKey)
-       : PluginAction("boolVconf"), key(vconfKey), oldVal(false)
+VconfActionBool::VconfActionBool(const std::string &key)
+       : VconfAction("boolVconf", key), oldVal(false)
 {
 }
 
@@ -31,6 +31,12 @@ int VconfActionBool::set(bool val)
        int ret;
        DBG("setBool(%s, %d)", key.c_str(), val);
 
+       ret = handleChange();
+       if (MODES_ERROR_NONE != ret) {
+               ERR("handleChange(%s) Fail(%d)", key.c_str(), ret);
+               return ret;
+       }
+
        int prev;
        ret = vconf_get_bool(key.c_str(), &prev);
        if (0 != ret) {
index 9d85615..4d74dc2 100644 (file)
 #pragma once
 
 #include <string>
-#include <PluginAction.h>
+#include "VconfAction.h"
 #include "plugin-def.h"
 
 MODES_NAMESPACE_BEGIN
 
-class VconfActionBool : public PluginAction {
+class VconfActionBool : public VconfAction {
 public:
-       VconfActionBool(const std::string &vconfKey);
+       VconfActionBool(const std::string &key);
        virtual ~VconfActionBool() = default;
 
        int set(bool val);
@@ -32,7 +32,6 @@ public:
        std::string serialize() override;
        int parse(const std::string &archive) override;
 private:
-       std::string key;
        bool oldVal;
 };
 
index 0a41472..1d7cc3c 100644 (file)
@@ -21,8 +21,8 @@
 
 MODES_NAMESPACE_USE;
 
-VconfActionDbl::VconfActionDbl(const std::string &vconfKey)
-       : PluginAction("dblVconf"), key(vconfKey), oldVal(0.0)
+VconfActionDbl::VconfActionDbl(const std::string &key)
+       : VconfAction("dblVconf", key), oldVal(0.0)
 {
 }
 
@@ -31,6 +31,12 @@ int VconfActionDbl::set(double val)
        int ret;
        DBG("setDbl(%s, %f)", key.c_str(), val);
 
+       ret = handleChange();
+       if (MODES_ERROR_NONE != ret) {
+               ERR("handleChange(%s) Fail(%d)", key.c_str(), ret);
+               return ret;
+       }
+
        ret = vconf_get_dbl(key.c_str(), &oldVal);
        if (0 != ret) {
                ERR("vconf_get_dbl(%s) Fail(%d)", key.c_str(), ret);
index 2e499d7..f83cf51 100644 (file)
 #pragma once
 
 #include <string>
-#include <PluginAction.h>
+#include "VconfAction.h"
 #include "plugin-def.h"
 
 MODES_NAMESPACE_BEGIN
 
-class VconfActionDbl : public PluginAction {
+class VconfActionDbl : public VconfAction {
 public:
-       VconfActionDbl(const std::string &vconfKey);
+       VconfActionDbl(const std::string &key);
        virtual ~VconfActionDbl() = default;
 
        int set(double val);
@@ -32,7 +32,6 @@ public:
        std::string serialize() override;
        int parse(const std::string &archive) override;
 private:
-       std::string key;
        double oldVal;
 };
 
index dd8c340..05f6d24 100644 (file)
@@ -21,8 +21,8 @@
 
 MODES_NAMESPACE_USE;
 
-VconfActionInt::VconfActionInt(const std::string &vconfKey)
-       : PluginAction("intVconf"), key(vconfKey), oldVal(0)
+VconfActionInt::VconfActionInt(const std::string &key)
+       : VconfAction("intVconf", key), oldVal(0)
 {
 }
 
@@ -31,6 +31,12 @@ int VconfActionInt::set(int val)
        int ret;
        DBG("setInt(%s, %d)", key.c_str(), val);
 
+       ret = handleChange();
+       if (MODES_ERROR_NONE != ret) {
+               ERR("handleChange(%s) Fail(%d)", key.c_str(), ret);
+               return ret;
+       }
+
        ret = vconf_get_int(key.c_str(), &oldVal);
        if (0 != ret) {
                ERR("vconf_get_int(%s) Fail(%d)", key.c_str(), ret);
index 8bd4542..66b0b8a 100644 (file)
 #pragma once
 
 #include <string>
-#include <PluginAction.h>
+#include "VconfAction.h"
 #include "plugin-def.h"
 
 MODES_NAMESPACE_BEGIN
 
-class VconfActionInt : public PluginAction {
+class VconfActionInt : public VconfAction {
 public:
-       VconfActionInt(const std::string &vconfKey);
+       VconfActionInt(const std::string &key);
        virtual ~VconfActionInt() = default;
 
        int set(int val);
@@ -32,7 +32,6 @@ public:
        std::string serialize() override;
        int parse(const std::string &archive) override;
 private:
-       std::string key;
        int oldVal;
 };
 
index d5076e4..cfa2715 100644 (file)
@@ -20,8 +20,8 @@
 
 MODES_NAMESPACE_USE;
 
-VconfActionStr::VconfActionStr(const std::string &vconfKey)
-       : PluginAction("strVconf"), key(vconfKey)
+VconfActionStr::VconfActionStr(const std::string &key)
+       : VconfAction("strVconf", key)
 {
 }
 
@@ -30,6 +30,12 @@ int VconfActionStr::set(const std::string &val)
        int ret;
        DBG("setStr(%s, %s)", key.c_str(), val.c_str());
 
+       ret = handleChange();
+       if (MODES_ERROR_NONE != ret) {
+               ERR("handleChange(%s) Fail(%d)", key.c_str(), ret);
+               return ret;
+       }
+
        char *prev = vconf_get_str(key.c_str());
        if (NULL == prev) {
                ERR("vconf_get_str(%s) Fail()", key.c_str());
index e5080e1..e6a5ec0 100644 (file)
 #pragma once
 
 #include <string>
-#include <PluginAction.h>
+#include "VconfAction.h"
 #include "plugin-def.h"
 
 MODES_NAMESPACE_BEGIN
 
-class VconfActionStr : public PluginAction {
+class VconfActionStr : public VconfAction {
 public:
-       VconfActionStr(const std::string &vconfKey);
+       VconfActionStr(const std::string &key);
        ~VconfActionStr() = default;
 
        int set(const std::string &val);
@@ -32,7 +32,6 @@ public:
        std::string serialize() override;
        int parse(const std::string &archive) override;
 private:
-       std::string key;
        std::string oldVal;
 };
 
index 6fa86ce..b549057 100644 (file)
@@ -13,7 +13,6 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-#include <map>
 #include <string>
 #include <algorithm>
 #include <vconf.h>
@@ -43,19 +42,6 @@ public:
        double getDouble(const std::string &key) override;
        bool getBool(const std::string &key) override;
        std::string getString(const std::string &key) override;
-
-       int setChangedCallback(valueChangedCb callback, const std::string &key, void *userData);
-       int unSetChangedCallback(valueChangedCb callback, const std::string &key, void *userData);
-private:
-       struct CallbackData {
-               void *userData;
-               valueChangedCb *callback;
-       };
-
-       int handleChange(const std::string &key);
-       static void vconfChangedCallback(keynode_t *node, void *userData);
-
-       static std::map<std::string, CallbackData> callbackMap;
 };
 
 extern "C" API Plugin *objectCreate(void)
@@ -68,8 +54,6 @@ extern "C" API void objectDelete(Plugin *plugin)
        delete plugin;
 }
 
-std::map<std::string, VconfPlugin::CallbackData> VconfPlugin::callbackMap;
-
 VconfPlugin::VconfPlugin()
 {
        setName("vconf");
@@ -83,12 +67,6 @@ int VconfPlugin::set(const std::string &key, int val, PluginAction **piAction)
        std::string newKey(key);
        std::replace(newKey.begin(), newKey.end(), '.', '/');
 
-       ret = handleChange(newKey);
-       if (MODES_ERROR_NONE != ret) {
-               ERR("handleChange(%s) Fail(%d)", newKey.c_str(), ret);
-               return ret;
-       }
-
        VconfActionInt *action = new VconfActionInt(newKey);
        ret = action->set(val);
        if (piAction)
@@ -106,12 +84,6 @@ int VconfPlugin::set(const std::string &key, double val, PluginAction **piAction
        std::string newKey(key);
        std::replace(newKey.begin(), newKey.end(), '.', '/');
 
-       ret = handleChange(newKey);
-       if (MODES_ERROR_NONE != ret) {
-               ERR("handleChange(%s) Fail(%d)", newKey.c_str(), ret);
-               return ret;
-       }
-
        VconfActionDbl *action = new VconfActionDbl(newKey);
        ret = action->set(val);
        if (piAction)
@@ -129,12 +101,6 @@ int VconfPlugin::set(const std::string &key, bool val, PluginAction **piAction)
        std::string newKey(key);
        std::replace(newKey.begin(), newKey.end(), '.', '/');
 
-       ret = handleChange(newKey);
-       if (MODES_ERROR_NONE != ret) {
-               ERR("handleChange(%s) Fail(%d)", newKey.c_str(), ret);
-               return ret;
-       }
-
        VconfActionBool *action = new VconfActionBool(newKey);
        ret = action->set(val);
        if (piAction)
@@ -151,14 +117,8 @@ int VconfPlugin::set(const std::string &key, const std::string &val, PluginActio
        std::string newKey(key);
        std::replace(newKey.begin(), newKey.end(), '.', '/');
 
-       int ret = handleChange(newKey);
-       if (MODES_ERROR_NONE != ret) {
-               ERR("handleChange(%s) Fail(%d)", newKey.c_str(), ret);
-               return ret;
-       }
-
        VconfActionStr *action = new VconfActionStr(newKey);
-       ret = action->set(val);
+       int ret = action->set(val);
        if (piAction)
                *piAction = action;
        else
@@ -254,83 +214,3 @@ std::string VconfPlugin::getString(const std::string &key)
 
        return action.get();
 }
-
-int VconfPlugin::setChangedCallback(valueChangedCb callback, const std::string &key, void *userData)
-{
-       std::string newKey(key);
-       std::replace(newKey.begin(), newKey.end(), '.', '/');
-
-       callbackMap[newKey].userData = userData;
-       callbackMap[newKey].callback = callback;
-
-       int ret = vconf_notify_key_changed(newKey.c_str(), VconfPlugin::vconfChangedCallback, &callbackMap[newKey]);
-       if (VCONF_OK != ret) {
-               ERR("vconf_notify_key_changed(%s) Fail(%s)", newKey.c_str(), get_error_message(ret));
-               return MODES_ERROR_SYSTEM;
-       }
-
-       DBG("setChangedCallback(%s) Success", newKey.c_str());
-       return MODES_ERROR_NONE;
-}
-
-int VconfPlugin::unSetChangedCallback(valueChangedCb callback, const std::string &key, void *userData)
-{
-       std::string newKey(key);
-       std::replace(newKey.begin(), newKey.end(), '.', '/');
-
-       auto found = callbackMap.find(newKey);
-       if (found == callbackMap.end()) {
-               ERR("No Changed Callback(%s)", newKey.c_str());
-               return MODES_ERROR_NO_DATA;
-       }
-
-       callbackMap.erase(found);
-       int ret = vconf_ignore_key_changed(newKey.c_str(), VconfPlugin::vconfChangedCallback);
-       if (VCONF_OK != ret) {
-               ERR("vconf_ignore_key_changed(%s) Fail(%s)", newKey.c_str(), get_error_message(ret));
-               return MODES_ERROR_SYSTEM;
-       }
-
-       DBG("unSetChangedCallback(%s) Success", newKey.c_str());
-       return MODES_ERROR_NONE;
-}
-
-int VconfPlugin::handleChange(const std::string &key)
-{
-       auto found = callbackMap.find(key);
-       if (callbackMap.end() != found) {
-               DBG("Acition(%s) already exist", key.c_str());
-               found->second.callback(key, found->second.userData);
-
-               callbackMap.erase(found);
-               int ret = vconf_ignore_key_changed(key.c_str(), VconfPlugin::vconfChangedCallback);
-               if (VCONF_OK != ret) {
-                       ERR("vconf_ignore_key_changed(%s) Fail(%s)", key.c_str(), get_error_message(ret));
-                       return MODES_ERROR_SYSTEM;
-               }
-       }
-       return MODES_ERROR_NONE;
-}
-
-void VconfPlugin::vconfChangedCallback(keynode_t *node, void *userData)
-{
-       CallbackData *cbData = (CallbackData*)userData;
-
-       RET_IF(NULL == userData);
-
-       auto found = callbackMap.find(node->keyname);
-       if (&(found->second) != cbData) {
-               ERR("Unknown callbackData(%s)", node->keyname);
-               return;
-       }
-
-       std::string newKey(node->keyname);
-       std::replace(newKey.begin(), newKey.end(), '/', '.');
-       cbData->callback(node->keyname, cbData->userData);
-       DBG("Action(%s) is Changed", node->keyname);
-
-       callbackMap.erase(found);
-       int ret = vconf_ignore_key_changed(node->keyname, VconfPlugin::vconfChangedCallback);
-       if (VCONF_OK != ret)
-               ERR("vconf_ignore_key_changed(%s) Fail(%s)", node->keyname, get_error_message(ret));
-}