From: Szymon Jastrzebski Date: Wed, 18 Oct 2017 09:48:32 +0000 (+0200) Subject: [VoiceControl] Refactoring/reformatting module X-Git-Tag: submit/tizen_4.0/20171018.120310~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=756ad8be2759ac4b5b24013935d68db65a3fc01c;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [VoiceControl] Refactoring/reformatting module + refactor the code and replace static VoiceControlClient into member of VoiceControlInstace to prevent possible crashes + moving VcConvertCmdType function into the vc_utils [Verification] Code compiles Change-Id: Ibe76be9af93df3825917c7fb2115afb0d8826d76 Signed-off-by: Szymon Jastrzebski --- diff --git a/src/voicecontrol/voicecontrol_client.cc b/src/voicecontrol/voicecontrol_client.cc old mode 100755 new mode 100644 index bf42449e..debba64a --- a/src/voicecontrol/voicecontrol_client.cc +++ b/src/voicecontrol/voicecontrol_client.cc @@ -14,15 +14,15 @@ * limitations under the License. */ -#include "voicecontrol/voicecontrol_util.h" #include "voicecontrol/voicecontrol_client.h" #include #include +#include "voicecontrol/voicecontrol_util.h" #include -#include "common/picojson.h" #include "common/logger.h" +#include "common/picojson.h" #include "common/platform_exception.h" #include "common/scope_exit.h" #include "common/tools.h" @@ -30,29 +30,16 @@ namespace extension { namespace voicecontrol { -VoiceControlClient* VoiceControlClient::GetInstance() { - ScopeLogger(); - static VoiceControlClient instance; - - return &instance; -} - VoiceControlClient::VoiceControlClient() : is_result_cb_set(false), is_lang_cb_set(false) { } VoiceControlClient::~VoiceControlClient() { } -int VcConvertCmdType(const std::string &type) { - // TODO add switch here when also other types of command will be also supported. - // For now, only foreground is supported, we are returning it as default value. - return VC_COMMAND_TYPE_FOREGROUND; -} - -common::PlatformResult VoiceControlClient::GetCurrentLanguage(std::string *language) { +common::PlatformResult VoiceControlClient::GetCurrentLanguage(std::string* language) { ScopeLogger(); - char *lang = nullptr; + char* lang = nullptr; int ret = VC_ERROR_NONE; ret = vc_get_current_language(&lang); @@ -67,7 +54,8 @@ common::PlatformResult VoiceControlClient::GetCurrentLanguage(std::string *langu return MakeErrorObject(ret); } -common::PlatformResult VoiceControlClient::SetCommandList(const std::vector& list, const std::string& type) { +common::PlatformResult VoiceControlClient::SetCommandList(const std::vector& list, + const std::string& type) { ScopeLogger(); int ret = VC_ERROR_NONE; @@ -90,7 +78,8 @@ common::PlatformResult VoiceControlClient::SetCommandList(const std::vector::type, int (*)(vc_cmd_h)> ptr(vc_command, &vc_cmd_destroy); + std::unique_ptr::type, int (*)(vc_cmd_h)> ptr(vc_command, + &vc_cmd_destroy); ret = vc_cmd_set_command(ptr.get(), (*it).get(COMMAND).get().c_str()); if (VC_ERROR_NONE != ret) { @@ -151,7 +140,6 @@ common::PlatformResult VoiceControlClient::RemoveLanguageChangeListener() { return MakeErrorObject(VC_ERROR_NONE); } - const bool VoiceControlClient::IsResultListenerSet() { ScopeLogger(); @@ -166,5 +154,5 @@ const bool VoiceControlClient::IsLanguageChangedListenerSet() { #undef CHECK_EXIST -} // namespace voicecontrol -} // namespace extension +} // namespace voicecontrol +} // namespace extension diff --git a/src/voicecontrol/voicecontrol_client.h b/src/voicecontrol/voicecontrol_client.h old mode 100755 new mode 100644 index 7f067304..117d99c8 --- a/src/voicecontrol/voicecontrol_client.h +++ b/src/voicecontrol/voicecontrol_client.h @@ -17,22 +17,20 @@ #ifndef VOICECONTROL_VOICECONTROL_CLIENT_H_ #define VOICECONTROL_VOICECONTROL_CLIENT_H_ -#include "common/extension.h" #include +#include "common/extension.h" namespace extension { namespace voicecontrol { class VoiceControlClient { - public: - static VoiceControlClient* GetInstance(); - public: VoiceControlClient(); ~VoiceControlClient(); - common::PlatformResult GetCurrentLanguage(std::string *language); - common::PlatformResult SetCommandList(const std::vector& list, const std::string& type); + common::PlatformResult GetCurrentLanguage(std::string* language); + common::PlatformResult SetCommandList(const std::vector& list, + const std::string& type); common::PlatformResult UnsetCommandList(const std::string& type); common::PlatformResult AddResultListener(); common::PlatformResult RemoveResultListener(); @@ -47,7 +45,7 @@ class VoiceControlClient { bool is_lang_cb_set; }; -} // namespace voicecontrol -} // namespace extension +} // namespace voicecontrol +} // namespace extension -#endif // VOICECONTROL_VOICECONTROL_CLIENT_H_ \ No newline at end of file +#endif // VOICECONTROL_VOICECONTROL_CLIENT_H_ \ No newline at end of file diff --git a/src/voicecontrol/voicecontrol_extension.cc b/src/voicecontrol/voicecontrol_extension.cc old mode 100755 new mode 100644 index f3bd17a8..66e1f8c8 --- a/src/voicecontrol/voicecontrol_extension.cc +++ b/src/voicecontrol/voicecontrol_extension.cc @@ -29,14 +29,12 @@ VoicecontrolExtension::VoicecontrolExtension() { SetExtensionName("tizen.voicecontrol"); SetJavaScriptAPI(kSource_voicecontrol_api); - const char* entry_points[] = { - "tizen.VoiceControlCommand", - NULL - }; + const char* entry_points[] = {"tizen.VoiceControlCommand", NULL}; SetExtraJSEntryPoints(entry_points); } -VoicecontrolExtension::~VoicecontrolExtension() {} +VoicecontrolExtension::~VoicecontrolExtension() { +} common::Instance* VoicecontrolExtension::CreateInstance() { return new extension::voicecontrol::VoiceControlInstance; diff --git a/src/voicecontrol/voicecontrol_extension.h b/src/voicecontrol/voicecontrol_extension.h old mode 100755 new mode 100644 index 5d989920..aab4fd3c --- a/src/voicecontrol/voicecontrol_extension.h +++ b/src/voicecontrol/voicecontrol_extension.h @@ -28,4 +28,4 @@ class VoicecontrolExtension : public common::Extension { virtual common::Instance* CreateInstance(); }; -#endif // VOICECONTROL_VOICECONTROL_EXTENSION_H_ +#endif // VOICECONTROL_VOICECONTROL_EXTENSION_H_ diff --git a/src/voicecontrol/voicecontrol_instance.cc b/src/voicecontrol/voicecontrol_instance.cc old mode 100755 new mode 100644 index 034b416e..7a11ceb4 --- a/src/voicecontrol/voicecontrol_instance.cc +++ b/src/voicecontrol/voicecontrol_instance.cc @@ -15,15 +15,15 @@ */ #include "voicecontrol/voicecontrol_instance.h" -#include "voicecontrol/voicecontrol_client.h" -#include "voicecontrol/voicecontrol_util.h" #include #include +#include "voicecontrol/voicecontrol_client.h" +#include "voicecontrol/voicecontrol_util.h" #include -#include "common/picojson.h" #include "common/logger.h" +#include "common/picojson.h" #include "common/platform_exception.h" #include "common/scope_exit.h" #include "common/tools.h" @@ -34,96 +34,15 @@ namespace voicecontrol { namespace { // The privileges that are required in Voicecontrol API const std::string kPrivilegeVoiceControl = "http://tizen.org/privilege/recorder"; - -void ReplyAsync(VoiceControlInstance* instance, const std::string& listenerId, bool isSuccess, picojson::object& param) { - ScopeLogger(); - param["listenerId"] = picojson::value(listenerId); - param["status"] = picojson::value(isSuccess ? "success" : "error"); - - /* All callback functions are called by voice control framework */ - /* Send result from callback to javascript */ - picojson::value result = picojson::value(param); - - instance->PostMessage(instance, result.serialize().c_str()); -} - - - -/* Define callback functions for voice control */ -void VcResultCb(vc_result_event_e event, vc_cmd_list_h cmd_list, const char* result, void *user_data) { - ScopeLogger(); - VoiceControlInstance *instance = static_cast(user_data); - - if (nullptr != instance && VoiceControlClient::GetInstance()->IsResultListenerSet()) { - LoggerD("VcResultCb"); - picojson::object param = picojson::object(); - param["event"] = picojson::value(std::string(((event == VC_RESULT_EVENT_RESULT_SUCCESS) ? "SUCCESS" : "FAILURE"))); - - int len = 0; - int ret = VC_ERROR_NONE; - - ret = vc_cmd_list_get_count(cmd_list, &len); - if (VC_ERROR_NONE != ret) { - LoggerE("Fail to get the count of the result list"); - return; - } - - picojson::array& value_vector = param.insert(std::make_pair("list", - picojson::value(picojson::array(len)))).first->second.get(); - - ret = vc_cmd_list_first(cmd_list); - for (auto i = 0; VC_ERROR_NONE == ret && i < len; i++) { - LoggerD("Current (%d), Count (%d)", i, len); - vc_cmd_h vc_command = nullptr; - char* command = nullptr; - SCOPE_EXIT { - free(command); - }; - - ret = vc_cmd_list_get_current(cmd_list, &vc_command); - if (VC_ERROR_NONE != ret) { - break; - } - ret = vc_cmd_get_command(vc_command, &command); - if (VC_ERROR_NONE != ret) { - break; - } - - if (nullptr != command) { - LoggerD("Command (%s)", command); - value_vector[i] = picojson::value(command); - } - - ret = vc_cmd_list_next(cmd_list); - } - - param["results"] = picojson::value(std::string(result)); - - ReplyAsync(instance, ON_RESULT, true, param); - } -} - -void VcLanguageChangedCb(const char* previous, const char* current, void* user_data) { - ScopeLogger(); - VoiceControlInstance *instance = static_cast(user_data); - if (nullptr != instance && VoiceControlClient::GetInstance()->IsLanguageChangedListenerSet()) { - LoggerD("VcLanguageChangedCb"); - picojson::object param = picojson::object(); - param["previous"] = picojson::value(std::string(previous)); - param["current"] = picojson::value(std::string(current)); - - ReplyAsync(instance, ON_LANG, true, param); - } -} -} // namespace +} // namespace using namespace common; using namespace extension::voicecontrol; -VoiceControlInstance::VoiceControlInstance() { +VoiceControlInstance::VoiceControlInstance() : voice_control_client() { using namespace std::placeholders; - #define REGISTER_SYNC(c,x) \ - RegisterSyncHandler(c, std::bind(&VoiceControlInstance::x, this, _1, _2)); +#define REGISTER_SYNC(c, x) \ + RegisterSyncHandler(c, std::bind(&VoiceControlInstance::x, this, _1, _2)); REGISTER_SYNC("VoiceControlClient_getCurrentLanguage", GetCurrentLanguage); REGISTER_SYNC("VoiceControlClient_setCommandList", SetCommandList); REGISTER_SYNC("VoiceControlClient_unsetCommandList", UnsetCommandList); @@ -133,7 +52,7 @@ VoiceControlInstance::VoiceControlInstance() { REGISTER_SYNC("VoiceControlClient_removeLanguageChangeListener", RemoveLanguageChangeListener); REGISTER_SYNC("VoiceControlClient_release", Release); REGISTER_SYNC("VoiceControlClientManager_getVoiceControlClient", GetVoiceControlClient); - #undef REGISTER_SYNC +#undef REGISTER_SYNC } VoiceControlInstance::~VoiceControlInstance() { @@ -144,13 +63,14 @@ VoiceControlInstance::~VoiceControlInstance() { } } -#define CHECK_EXIST(args, name, out) \ - if (!args.contains(name)) {\ - ReportError(TypeMismatchException(name + " is required argument"), out);\ - return;\ +#define CHECK_EXIST(args, name, out) \ + if (!args.contains(name)) { \ + ReportError(TypeMismatchException(name + " is required argument"), out); \ + return; \ } -void VoiceControlInstance::GetVoiceControlClient(const picojson::value& args, picojson::object& out) { +void VoiceControlInstance::GetVoiceControlClient(const picojson::value& args, + picojson::object& out) { ScopeLogger(); CHECK_PRIVILEGE_ACCESS(kPrivilegeVoiceControl, &out); @@ -188,7 +108,7 @@ void VoiceControlInstance::GetVoiceControlClient(const picojson::value& args, pi } ret = vc_prepare_sync(); - if (VC_ERROR_NONE != ret) { + if (VC_ERROR_NONE != ret) { LogAndReportError(MakeErrorObject(ret), &out); return; } @@ -196,7 +116,6 @@ void VoiceControlInstance::GetVoiceControlClient(const picojson::value& args, pi ReportSuccess(out); } - void VoiceControlInstance::Release(const picojson::value& args, picojson::object& out) { ScopeLogger(); int ret = VC_ERROR_NONE; @@ -232,7 +151,7 @@ void VoiceControlInstance::GetCurrentLanguage(const picojson::value& args, picoj ScopeLogger(); std::string language; - common::PlatformResult result = VoiceControlClient::GetInstance()->GetCurrentLanguage(&language); + common::PlatformResult result = voice_control_client.GetCurrentLanguage(&language); if (result.IsSuccess()) { ReportSuccess(picojson::value(language), out); @@ -248,10 +167,10 @@ void VoiceControlInstance::SetCommandList(const picojson::value& args, picojson: CHECK_EXIST(args, TYPE, out) CHECK_EXIST(args, LIST, out) - std::string type = args.get(TYPE).get(); + const std::string& type = args.get(TYPE).get(); std::vector list = args.get(LIST).get(); - common::PlatformResult result = VoiceControlClient::GetInstance()->SetCommandList(list, type); + common::PlatformResult result = voice_control_client.SetCommandList(list, type); if (result.IsSuccess()) { ReportSuccess(out); @@ -266,9 +185,9 @@ void VoiceControlInstance::UnsetCommandList(const picojson::value& args, picojso CHECK_EXIST(args, TYPE, out) - std::string type = args.get(TYPE).get(); + const std::string& type = args.get(TYPE).get(); - common::PlatformResult result = VoiceControlClient::GetInstance()->UnsetCommandList(type); + common::PlatformResult result = voice_control_client.UnsetCommandList(type); if (result.IsSuccess()) { ReportSuccess(out); @@ -280,7 +199,7 @@ void VoiceControlInstance::UnsetCommandList(const picojson::value& args, picojso void VoiceControlInstance::AddResultListener(const picojson::value& args, picojson::object& out) { ScopeLogger(); - common::PlatformResult result = VoiceControlClient::GetInstance()->AddResultListener(); + common::PlatformResult result = voice_control_client.AddResultListener(); if (result.IsSuccess()) { ReportSuccess(out); @@ -289,10 +208,11 @@ void VoiceControlInstance::AddResultListener(const picojson::value& args, picojs } } -void VoiceControlInstance::RemoveResultListener(const picojson::value& args, picojson::object& out) { +void VoiceControlInstance::RemoveResultListener(const picojson::value& args, + picojson::object& out) { ScopeLogger(); - common::PlatformResult result = VoiceControlClient::GetInstance()->RemoveResultListener(); + common::PlatformResult result = voice_control_client.RemoveResultListener(); if (result.IsSuccess()) { ReportSuccess(out); @@ -301,10 +221,11 @@ void VoiceControlInstance::RemoveResultListener(const picojson::value& args, pic } } -void VoiceControlInstance::AddLanguageChangeListener(const picojson::value& args, picojson::object& out) { +void VoiceControlInstance::AddLanguageChangeListener(const picojson::value& args, + picojson::object& out) { ScopeLogger(); - common::PlatformResult result = VoiceControlClient::GetInstance()->AddLanguageChangeListener(); + common::PlatformResult result = voice_control_client.AddLanguageChangeListener(); if (result.IsSuccess()) { ReportSuccess(out); @@ -313,10 +234,11 @@ void VoiceControlInstance::AddLanguageChangeListener(const picojson::value& args } } -void VoiceControlInstance::RemoveLanguageChangeListener(const picojson::value& args, picojson::object& out) { +void VoiceControlInstance::RemoveLanguageChangeListener(const picojson::value& args, + picojson::object& out) { ScopeLogger(); - common::PlatformResult result = VoiceControlClient::GetInstance()->RemoveLanguageChangeListener(); + common::PlatformResult result = voice_control_client.RemoveLanguageChangeListener(); if (result.IsSuccess()) { ReportSuccess(out); @@ -325,7 +247,95 @@ void VoiceControlInstance::RemoveLanguageChangeListener(const picojson::value& a } } +void VoiceControlInstance::ReplyAsync(VoiceControlInstance* instance, const std::string& listenerId, + bool isSuccess, picojson::object& param) { + ScopeLogger(); + param["listenerId"] = picojson::value(listenerId); + param["status"] = picojson::value(isSuccess ? "success" : "error"); + + /* All callback functions are called by voice control framework */ + /* Send result from callback to javascript */ + picojson::value result = picojson::value(param); + + instance->PostMessage(instance, result.serialize().c_str()); +} + +/* Define callback functions for voice control */ +void VoiceControlInstance::VcResultCb(vc_result_event_e event, vc_cmd_list_h cmd_list, + const char* result, void* user_data) { + ScopeLogger(); + VoiceControlInstance* instance = static_cast(user_data); + + if (nullptr != instance && instance->voice_control_client.IsResultListenerSet()) { + LoggerD("VcResultCb"); + picojson::object param = picojson::object(); + param["event"] = + picojson::value(((event == VC_RESULT_EVENT_RESULT_SUCCESS) ? "SUCCESS" : "FAILURE")); + + int len = 0; + int ret = VC_ERROR_NONE; + + ret = vc_cmd_list_get_count(cmd_list, &len); + if (VC_ERROR_NONE != ret) { + LoggerE("Fail to get the count of the result list"); + return; + } + + ret = vc_cmd_list_first(cmd_list); + if (VC_ERROR_NONE != ret) { + LoggerE("Fail to get the first element of the result list"); + } + + picojson::array& value_vector = + param.insert(std::make_pair("list", picojson::value(picojson::array(len)))) + .first->second.get(); + param["results"] = picojson::value(result); + + for (int i = 0; VC_ERROR_NONE == ret && i < len; i++) { + LoggerD("Current (%d), Count (%d)", i, len); + vc_cmd_h vc_command = nullptr; + char* command = nullptr; + SCOPE_EXIT { + free(command); + }; + + ret = vc_cmd_list_get_current(cmd_list, &vc_command); + if (VC_ERROR_NONE != ret) { + break; + } + + ret = vc_cmd_get_command(vc_command, &command); + if (VC_ERROR_NONE != ret) { + break; + } + + if (nullptr != command) { + LoggerD("Command (%s)", command); + value_vector[i] = picojson::value(command); + } + + ret = vc_cmd_list_next(cmd_list); + } + + ReplyAsync(instance, ON_RESULT, true, param); + } +} + +void VoiceControlInstance::VcLanguageChangedCb(const char* previous, const char* current, + void* user_data) { + ScopeLogger(); + VoiceControlInstance* instance = static_cast(user_data); + if (nullptr != instance && instance->voice_control_client.IsLanguageChangedListenerSet()) { + LoggerD("VcLanguageChangedCb"); + picojson::object param = picojson::object(); + param["previous"] = picojson::value(previous); + param["current"] = picojson::value(current); + + ReplyAsync(instance, ON_LANG, true, param); + } +} + #undef CHECK_EXIST -} // namespace voicecontrol -} // namespace extension +} // namespace voicecontrol +} // namespace extension diff --git a/src/voicecontrol/voicecontrol_instance.h b/src/voicecontrol/voicecontrol_instance.h old mode 100755 new mode 100644 index 633482e2..03d2aace --- a/src/voicecontrol/voicecontrol_instance.h +++ b/src/voicecontrol/voicecontrol_instance.h @@ -17,7 +17,10 @@ #ifndef VOICECONTROL_VOICECONTROL_INSTANCE_H_ #define VOICECONTROL_VOICECONTROL_INSTANCE_H_ +#include + #include "common/extension.h" +#include "voicecontrol/voicecontrol_client.h" namespace extension { namespace voicecontrol { @@ -38,9 +41,17 @@ class VoiceControlInstance : public common::ParsedInstance { void RemoveResultListener(const picojson::value& args, picojson::object& out); void AddLanguageChangeListener(const picojson::value& args, picojson::object& out); void RemoveLanguageChangeListener(const picojson::value& args, picojson::object& out); + + static void VcLanguageChangedCb(const char* previous, const char* current, void* user_data); + static void VcResultCb(vc_result_event_e event, vc_cmd_list_h cmd_list, const char* result, + void* user_data); + static void ReplyAsync(VoiceControlInstance* instance, const std::string& listenerId, + bool isSuccess, picojson::object& param); + + VoiceControlClient voice_control_client; }; -} // namespace voicecontrol -} // namespace extension +} // namespace voicecontrol +} // namespace extension -#endif // VOICECONTROL_VOICECONTROL_INSTANCE_H_ \ No newline at end of file +#endif // VOICECONTROL_VOICECONTROL_INSTANCE_H_ \ No newline at end of file diff --git a/src/voicecontrol/voicecontrol_util.cc b/src/voicecontrol/voicecontrol_util.cc old mode 100755 new mode 100644 index b51bbdc6..f54c1da5 --- a/src/voicecontrol/voicecontrol_util.cc +++ b/src/voicecontrol/voicecontrol_util.cc @@ -56,5 +56,11 @@ common::PlatformResult MakeErrorObject(const int ret) { } } -} // namespace voicecontrol -} // namespace extension +int VcConvertCmdType(const std::string& type) { + // TODO add switch here when also other types of command will be also supported. + // For now, only foreground is supported, we return it as the default value. + return VC_COMMAND_TYPE_FOREGROUND; +} + +} // namespace voicecontrol +} // namespace extension diff --git a/src/voicecontrol/voicecontrol_util.h b/src/voicecontrol/voicecontrol_util.h old mode 100755 new mode 100644 index 3071aa99..a8fff258 --- a/src/voicecontrol/voicecontrol_util.h +++ b/src/voicecontrol/voicecontrol_util.h @@ -23,32 +23,33 @@ namespace extension { namespace voicecontrol { namespace { - /* Error message text */ - const std::string NO_ERROR = "No error"; - const std::string INVALID_PARAMETER = "Invalid parameter!"; - const std::string OUT_OF_MEMORY = "Out of memory!"; - const std::string OPERATION_FAILED = "Operation failed!"; - const std::string PERMISSION_DENIED = "Permission denied!"; - const std::string NOT_SUPPORTED = "Not supported!"; - - /* Parameter name */ - const std::string CALLBACK_ID = "callbackId"; - const std::string TYPE = "type"; - const std::string LIST = "list"; - const std::string COMMAND = "command"; - - /* Voice control command type */ - const std::string FOREGROUND = "FOREGROUND"; - - /* Listener types on javascript */ - const std::string ON_RESULT = "onresult"; - const std::string ON_LANG = "onlanguagechanged"; +/* Error message text */ +const std::string NO_ERROR = "No error"; +const std::string INVALID_PARAMETER = "Invalid parameter!"; +const std::string OUT_OF_MEMORY = "Out of memory!"; +const std::string OPERATION_FAILED = "Operation failed!"; +const std::string PERMISSION_DENIED = "Permission denied!"; +const std::string NOT_SUPPORTED = "Not supported!"; + +/* Parameter name */ +const std::string CALLBACK_ID = "callbackId"; +const std::string TYPE = "type"; +const std::string LIST = "list"; +const std::string COMMAND = "command"; + +/* Voice control command type */ +const std::string FOREGROUND = "FOREGROUND"; + +/* Listener types on javascript */ +const std::string ON_RESULT = "onresult"; +const std::string ON_LANG = "onlanguagechanged"; } - const std::string& MakeErrorMessage(const int ret); - common::PlatformResult MakeErrorObject(const int ret); +const std::string& MakeErrorMessage(const int ret); +common::PlatformResult MakeErrorObject(const int ret); +int VcConvertCmdType(const std::string& type); -} // namespace voicecontrol -} // namespace extension +} // namespace voicecontrol +} // namespace extension -#endif // VOICECONTROL_VOICECONTROL_UTIL_H_ \ No newline at end of file +#endif // VOICECONTROL_VOICECONTROL_UTIL_H_ \ No newline at end of file