merge with master
[framework/osp/ime.git] / src / app / FApp_ImeEntry.cpp
index a377bad..95b5312 100644 (file)
@@ -28,6 +28,7 @@
 #include <linux/capability.h>
 #include <privilege-control.h>
 #include <string.h>
+#include <unique_ptr.h>
 #include <unistd.h> // readlink()
 
 #define Uses_SCIM_CONFIG_BASE
 
 #include <FBase_StringConverter.h>
 
-static const char* LOG_TAG_IME = "IME";
+static const char* _LOG_TAG_IME = "IME";
 
-static const int MAX_LANGUAGE_BUFFER_SIZE = 371; // 53*7
-static const int MAX_BUFFER_SIZE = 256;
-static const int MAX_PACKAGE_NAME = 100;
-static const char* MANIFEST_FILE_PATH = "info/manifest.xml";
+static const int _MAX_LANGUAGE_BUFFER_SIZE = 371; // 53*7
+static const int _MAX_BUFFER_SIZE = 256;
+static const int _MAX_PACKAGE_NAME = 100;
+static const char* _MANIFEST_FILE_PATH = "info/manifest.xml";
 
 static int __argc = 0;
 static char** __pArgv = NULL;
 
-static char __appId[MAX_BUFFER_SIZE];
-static char __uuid[MAX_BUFFER_SIZE];
-static char __name[MAX_BUFFER_SIZE];
-static char __language[MAX_LANGUAGE_BUFFER_SIZE];
+static char __appId[_MAX_BUFFER_SIZE];
+static char __uuid[_MAX_BUFFER_SIZE];
+static char __name[_MAX_BUFFER_SIZE];
+static char __language[_MAX_LANGUAGE_BUFFER_SIZE];
 
 #ifdef __cplusplus
 extern "C"
@@ -157,27 +158,27 @@ DoPreExec(const char* bin_path)
        handle = dlopen("libosp-env-config.so", RTLD_LAZY | RTLD_GLOBAL);
        if (!handle)
        {
-               LOG(LOG_DEBUG, LOG_TAG_IME, "IME > Failed to dlopen libosp-env-config.so (%s)", dlerror());
+               LOG(LOG_DEBUG, _LOG_TAG_IME, "IME > Failed to dlopen libosp-env-config.so (%s)", dlerror());
                return -1;
        }
-       LOG(LOG_DEBUG, LOG_TAG_IME, "IME > dlopen() ok");
+       LOG(LOG_DEBUG, _LOG_TAG_IME, "IME > dlopen() ok");
 
        DoPreExecFunc = reinterpret_cast <int (*)(const char*, const char*)>(dlsym(handle, "do_pre_exec"));
        errormsg = dlerror();
        if (errormsg != NULL)
        {
-               LOG(LOG_DEBUG, LOG_TAG_IME, "IME > Failed to dlsym() (%s)", errormsg);
+               LOG(LOG_DEBUG, _LOG_TAG_IME, "IME > Failed to dlsym() (%s)", errormsg);
                dlclose(handle);
                return -1;
        }
-       LOG(LOG_DEBUG, LOG_TAG_IME, "IME > dlsym() ok");
+       LOG(LOG_DEBUG, _LOG_TAG_IME, "IME > dlsym() ok");
 
        ret = DoPreExecFunc(NULL, bin_path);
        if (ret < 0)
        {
-               LOG(LOG_DEBUG, LOG_TAG_IME, "IME > Failed to do_pre_exe() (%d)", ret);
+               LOG(LOG_DEBUG, _LOG_TAG_IME, "IME > Failed to do_pre_exe() (%d)", ret);
        }
-       LOG(LOG_DEBUG, LOG_TAG_IME, "IME > do_pre_exe() ok");
+       LOG(LOG_DEBUG, _LOG_TAG_IME, "IME > do_pre_exe() ok");
 
        dlclose(handle);
 
@@ -206,7 +207,7 @@ GetDirFdFromPath(const char* pPath)
                }   
        }   
 
-       LOG(LOG_DEBUG, LOG_TAG_IME, "%s {%d) > wrong path format: %s.", __func__, __LINE__, pPath);
+       LOG(LOG_DEBUG, _LOG_TAG_IME, "%s {%d) > wrong path format: %s.", __func__, __LINE__, pPath);
 
        return -1; 
 
@@ -215,7 +216,7 @@ OUT:
 
        const int fd = open(dname, O_RDONLY | O_CLOEXEC | O_DIRECTORY);
 
-       LOG(LOG_DEBUG, LOG_TAG_IME, "%s {%d) > directory path: %s, fd: %d", __func__, __LINE__, dname, fd);
+       LOG(LOG_DEBUG, _LOG_TAG_IME, "%s {%d) > directory path: %s, fd: %d", __func__, __LINE__, dname, fd);
 
        return fd; 
 }
@@ -227,7 +228,7 @@ GetPackageNameFromAppIdAppExecutableName(const char appId[], const char exeName[
 
        snprintf(pkgName, maxPkgName, "%s.%s", appId, exeName);
 
-       LOG(LOG_DEBUG, LOG_TAG_IME, "IME, packageName is %s", pkgName);
+       LOG(LOG_DEBUG, _LOG_TAG_IME, "IME, packageName is %s", pkgName);
 
        return true;
 }
@@ -241,7 +242,7 @@ PrintArgs(int argc, char* argv[])
        do
        {
                value = argv[i];
-               LOG(LOG_DEBUG, LOG_TAG_IME, "%s {%d) > %dth arg: [%s]", __func__, __LINE__, i, value);
+               LOG(LOG_DEBUG, _LOG_TAG_IME, "%s {%d) > %dth arg: [%s]", __func__, __LINE__, i, value);
                i++;
        }
        while (value);
@@ -267,7 +268,7 @@ ParseXmlNode(const xmlNodePtr pXmlNode)
                                        xmlFree(pXmlChar);
                                }
 
-                               LOG(LOG_DEBUG, LOG_TAG_IME, "%s {%d) > __appId is %s.", __func__, __LINE__, __appId);
+                               LOG(LOG_DEBUG, _LOG_TAG_IME, "%s {%d) > __appId is %s.", __func__, __LINE__, __appId);
                        }
                        else if (!xmlStrcmp(pItorNode->name, reinterpret_cast<const xmlChar*>("Uuid")))
                        {
@@ -280,7 +281,7 @@ ParseXmlNode(const xmlNodePtr pXmlNode)
                                        xmlFree(pXmlChar);
                                }
 
-                               LOG(LOG_DEBUG, LOG_TAG_IME, "%s {%d) > __uuid is %s.", __func__, __LINE__, __uuid);
+                               LOG(LOG_DEBUG, _LOG_TAG_IME, "%s {%d) > __uuid is %s.", __func__, __LINE__, __uuid);
                        }
                        else if (!xmlStrcmp(pItorNode->name, reinterpret_cast<const xmlChar*>("DisplayName")))
                        {
@@ -293,11 +294,11 @@ ParseXmlNode(const xmlNodePtr pXmlNode)
                                        xmlFree(pXmlChar);
                                }
 
-                               LOG(LOG_DEBUG, LOG_TAG_IME, "%s {%d) > __name is %s.", __func__, __LINE__, __name);
+                               LOG(LOG_DEBUG, _LOG_TAG_IME, "%s {%d) > __name is %s.", __func__, __LINE__, __name);
                        }
                        else if (!xmlStrcmp(pItorNode->name, reinterpret_cast<const xmlChar*>("Language")))
                        {
-                               const int LANGUAGE_CODE_LENGTH = 3;
+                               const int _LANGUAGE_CODE_LENGTH = 3;
 
                                xmlChar* pXmlChar = null;
                                pXmlChar = xmlNodeGetContent(pItorNode);
@@ -309,28 +310,25 @@ ParseXmlNode(const xmlNodePtr pXmlNode)
                                                strcat(__language, ",");
                                        }
 
-                                       char languageCode[LANGUAGE_CODE_LENGTH + 1];
-                                       strncpy(languageCode, reinterpret_cast<char*>(pXmlChar), LANGUAGE_CODE_LENGTH);
-
+                                       char languageCode[_LANGUAGE_CODE_LENGTH + 1];
+                                       memset(languageCode, '\0', _LANGUAGE_CODE_LENGTH + 1);
+                                       strncpy(languageCode, reinterpret_cast<char*>(pXmlChar), _LANGUAGE_CODE_LENGTH);
                                        Tizen::Base::String twoLetterLanguageCode = Tizen::Locales::Locale::LanguageCodeToTwoLetterLanguageCodeString(Tizen::Locales::Locale::StringToLanguageCode(languageCode));
 
                                        // append language code
-                                       char* pLanguageCode = null;
-                                       pLanguageCode = Tizen::Base::_StringConverter::CopyToCharArrayN(twoLetterLanguageCode.GetPointer());
-
+                                       std::unique_ptr<char[]> pLanguageCode(Tizen::Base::_StringConverter::CopyToCharArrayN(twoLetterLanguageCode.GetPointer()));
                                        if (pLanguageCode)
                                        {
-                                               strcat(__language, pLanguageCode);
-                                               delete[] pLanguageCode;
+                                               strcat(__language, pLanguageCode.get());
                                        }
 
                                        // append country code
-                                       strcat(__language, reinterpret_cast<char*>(pXmlChar + LANGUAGE_CODE_LENGTH));
+                                       strcat(__language, reinterpret_cast<char*>(pXmlChar + _LANGUAGE_CODE_LENGTH));
 
                                        xmlFree(pXmlChar);
                                }
 
-                               LOG(LOG_DEBUG, LOG_TAG_IME, "%s {%d) > __language is %s.", __func__, __LINE__, __language);
+                               LOG(LOG_DEBUG, _LOG_TAG_IME, "%s {%d) > __language is %s.", __func__, __LINE__, __language);
                        }
 
                        if (pItorNode->children)
@@ -339,29 +337,27 @@ ParseXmlNode(const xmlNodePtr pXmlNode)
                        }
                }
        }
-
-       return;
 }
 
 static void
 ParseImeInfo(const char* filePath)
 {
-       LOG(LOG_DEBUG, LOG_TAG_IME, "%s {%d) > The filePath is %s.", __func__, __LINE__, filePath);
+       LOG(LOG_DEBUG, _LOG_TAG_IME, "%s {%d) > The filePath is %s.", __func__, __LINE__, filePath);
 
        xmlDocPtr pXmlDoc = null;
        xmlNodePtr pRootElement = null;
 
        pXmlDoc = xmlParseFile(filePath);
-       if (!pXmlDoc)
+       if (pXmlDoc == null)
        {
-               LOG(LOG_DEBUG, LOG_TAG_IME, "%s {%d) > xmlParseFile() has failed.", __func__, __LINE__);
+               LOG(LOG_DEBUG, _LOG_TAG_IME, "%s {%d) > xmlParseFile() has failed.", __func__, __LINE__);
                goto CATCH;
        }
 
        pRootElement = xmlDocGetRootElement(pXmlDoc);
-       if (!pRootElement)
+       if (pRootElement == null)
        {
-               LOG(LOG_DEBUG, LOG_TAG_IME, "%s {%d) > xmlDocGetRootElement() has falied.", __func__, __LINE__);
+               LOG(LOG_DEBUG, _LOG_TAG_IME, "%s {%d) > xmlDocGetRootElement() has falied.", __func__, __LINE__);
                goto CATCH;
        }
 
@@ -375,70 +371,62 @@ CATCH:
        }
 
        xmlCleanupParser();
-
-       return;
 }
 
 void _OSP_EXPORT_
 scim_module_init (void)
 {
-       LOG(LOG_DEBUG, LOG_TAG_IME, "%s {%d) > scim_module_init() has been called.", __func__, __LINE__);
-
-       return;
+       LOG(LOG_DEBUG, _LOG_TAG_IME, "%s {%d) > scim_module_init() has been called.", __func__, __LINE__);
 }
 
 void _OSP_EXPORT_
 scim_module_exit (void)
 {
-       LOG(LOG_DEBUG, LOG_TAG_IME, "%s {%d) > scim_module_exit() has been called.", __func__, __LINE__);
-
-       return;
+       LOG(LOG_DEBUG, _LOG_TAG_IME, "%s {%d) > scim_module_exit() has been called.", __func__, __LINE__);
 }
 
 void _OSP_EXPORT_
 scim_helper_module_set_path_info(const char* path)
 {
-       LOG(LOG_DEBUG, LOG_TAG_IME, "%s {%d) > The path is %s.", __func__, __LINE__, path);
+       LOG(LOG_DEBUG, _LOG_TAG_IME, "%s {%d) > The path is %s.", __func__, __LINE__, path);
 
-       memset(__appId, '\0', MAX_BUFFER_SIZE);
-       memset(__uuid, '\0', MAX_BUFFER_SIZE);
-       memset(__name, '\0', MAX_BUFFER_SIZE);
-       memset(__language, '\0', MAX_LANGUAGE_BUFFER_SIZE);
+       memset(__appId, '\0', _MAX_BUFFER_SIZE);
+       memset(__uuid, '\0', _MAX_BUFFER_SIZE);
+       memset(__name, '\0', _MAX_BUFFER_SIZE);
+       memset(__language, '\0', _MAX_LANGUAGE_BUFFER_SIZE);
 
-       char symbolicLinkPath[MAX_BUFFER_SIZE];
-       memset(symbolicLinkPath, '\0', MAX_BUFFER_SIZE);
-       ssize_t numberOfBytes = -1;
-       numberOfBytes = readlink(path, symbolicLinkPath, MAX_BUFFER_SIZE);
-       LOG(LOG_DEBUG, LOG_TAG_IME, "%s {%d) > The numberOfBytes is %d.", __func__, __LINE__, numberOfBytes);
+       char symbolicLinkPath[_MAX_BUFFER_SIZE];
+       memset(symbolicLinkPath, '\0', _MAX_BUFFER_SIZE);
+       ssize_t byteCount = -1;
+       byteCount = readlink(path, symbolicLinkPath, _MAX_BUFFER_SIZE);
+       LOG(LOG_DEBUG, _LOG_TAG_IME, "%s {%d) > The byteCount is %d.", __func__, __LINE__, byteCount);
 
        // parse ime info
        const char* pSeparator = strstr(symbolicLinkPath, "/bin");
 
-       char manifestFilePath[MAX_BUFFER_SIZE];
-       memset(manifestFilePath, '\0', MAX_BUFFER_SIZE);
+       char manifestFilePath[_MAX_BUFFER_SIZE];
+       memset(manifestFilePath, '\0', _MAX_BUFFER_SIZE);
 
        strncpy(manifestFilePath, symbolicLinkPath, pSeparator - symbolicLinkPath + 1);
-       strcat(manifestFilePath, MANIFEST_FILE_PATH);
+       strcat(manifestFilePath, _MANIFEST_FILE_PATH);
 
        ParseImeInfo(manifestFilePath);
 
        // initialize ime info
        InitImeInfo(__uuid, __name, __language);
-
-       return;
 }
 
 bool _OSP_EXPORT_
 scim_helper_module_get_helper_info (unsigned int idx, scim::HelperInfo& info)
 {
-       LOG(LOG_DEBUG, LOG_TAG_IME, "%s {%d) > scim_helper_module_get_helper_info() has been called.", __func__, __LINE__);
+       LOG(LOG_DEBUG, _LOG_TAG_IME, "%s {%d) > scim_helper_module_get_helper_info() has been called.", __func__, __LINE__);
 
        if (idx == 0)
        {
                info = scim::HelperInfo(__uuid, __name, "", "",
                                                                scim::SCIM_HELPER_STAND_ALONE | scim::SCIM_HELPER_NEED_SCREEN_INFO | scim::SCIM_HELPER_AUTO_RESTART);
 
-               LOG(LOG_DEBUG, LOG_TAG_IME, "%s {%d) > __uuid is %s and __name is %s.", __func__, __LINE__, __uuid, __name);
+               LOG(LOG_DEBUG, _LOG_TAG_IME, "%s {%d) > __uuid is %s and __name is %s.", __func__, __LINE__, __uuid, __name);
 
                return true;
        }
@@ -449,7 +437,7 @@ scim_helper_module_get_helper_info (unsigned int idx, scim::HelperInfo& info)
 scim::String _OSP_EXPORT_
 scim_helper_module_get_helper_language (unsigned int idx)
 {
-       LOG(LOG_DEBUG, LOG_TAG_IME, "%s {%d) > __language is %s.", __func__, __LINE__, __language);
+       LOG(LOG_DEBUG, _LOG_TAG_IME, "%s {%d) > __language is %s.", __func__, __LINE__, __language);
 
        return __language;
 }
@@ -457,7 +445,7 @@ scim_helper_module_get_helper_language (unsigned int idx)
 unsigned int _OSP_EXPORT_
 scim_helper_module_number_of_helpers (void)
 {
-       LOG(LOG_DEBUG, LOG_TAG_IME, "%s {%d) > scim_helper_module_number_of_helpers() has been called.", __func__, __LINE__);
+       LOG(LOG_DEBUG, _LOG_TAG_IME, "%s {%d) > scim_helper_module_number_of_helpers() has been called.", __func__, __LINE__);
 
        return 1;
 }
@@ -467,28 +455,29 @@ scim_helper_module_set_arg_info(int argc, char* argv[])
 {
        __argc = argc;
        __pArgv = argv;
-
-       return;
 }
 
 void _OSP_EXPORT_
 scim_helper_module_run_helper(const scim::String& uuid, const scim::ConfigPointer& config, const scim::String& display)
 {
-       LOG(LOG_DEBUG, LOG_TAG_IME, "%s {%d) > scim_helper_module_run_helper() has been called.", __func__, __LINE__);
+       LOG(LOG_DEBUG, _LOG_TAG_IME, "%s {%d) > scim_helper_module_run_helper() has been called.", __func__, __LINE__);
 
        // get binaryPath
-       char binaryPath[MAX_BUFFER_SIZE];
-       memset(binaryPath, '\0', MAX_BUFFER_SIZE);
+       char binaryPath[_MAX_BUFFER_SIZE];
+       memset(binaryPath, '\0', _MAX_BUFFER_SIZE);
 
-       snprintf(binaryPath, MAX_BUFFER_SIZE, "/opt/apps/%s/bin/%s", __appId, __name);
-       LOG(LOG_DEBUG, LOG_TAG_IME, "%s {%d) > The binary path is %s.", __func__, __LINE__, binaryPath);
+       snprintf(binaryPath, _MAX_BUFFER_SIZE - 1, "/opt/apps/%s/bin/%s", __appId, __name);
+       LOG(LOG_DEBUG, _LOG_TAG_IME, "%s {%d) > The binary path is %s.", __func__, __LINE__, binaryPath);
 
        // set __pArgv
-       LOG(LOG_DEBUG, LOG_TAG_IME, "%s {%d) > __pArgv[0] is %s.", __func__, __LINE__, __pArgv[0]);
-       snprintf(*__pArgv, strlen(binaryPath) + 1, "%s", binaryPath);
+       int argvCount = strlen(__pArgv[0]);
+       LOG(LOG_DEBUG, _LOG_TAG_IME, "%s {%d) > __pArgv[0] is %s and the length of __pArgv[0] is %d.", __func__, __LINE__, __pArgv[0], argvCount);
+
+       memset(__pArgv[0], '\0', argvCount);
+       snprintf(__pArgv[0], strlen(binaryPath) + 1, "%s", binaryPath);
        PrintArgs(__argc, __pArgv);
 
-       LOG(LOG_DEBUG, LOG_TAG_IME, "%s {%d) > uid is %d and euid is %d.", __func__, __LINE__, getuid(), geteuid());
+       LOG(LOG_DEBUG, _LOG_TAG_IME, "%s {%d) > uid is %d and euid is %d.", __func__, __LINE__, getuid(), geteuid());
 
        if (getuid() == 0)
        {
@@ -496,10 +485,10 @@ scim_helper_module_run_helper(const scim::String& uuid, const scim::ConfigPointe
                DoPreExec(binaryPath);
 
                // adjust privilege
-               char packageName[MAX_PACKAGE_NAME];
-               memset(packageName, '\0', MAX_PACKAGE_NAME);
+               char packageName[_MAX_PACKAGE_NAME];
+               memset(packageName, '\0', _MAX_PACKAGE_NAME);
 
-               GetPackageNameFromAppIdAppExecutableName(__appId, __name, packageName, MAX_PACKAGE_NAME);
+               GetPackageNameFromAppIdAppExecutableName(__appId, __name, packageName, _MAX_PACKAGE_NAME);
                AdjustPrivilege(packageName);
        }
 
@@ -509,7 +498,7 @@ scim_helper_module_run_helper(const scim::String& uuid, const scim::ConfigPointe
        int fd = GetDirFdFromPath(__pArgv[0]);
        if (fd == -1)
        {
-               LOG(LOG_DEBUG, LOG_TAG_IME, "%s {%d) > Openning a executable binary in the designated path has failed."
+               LOG(LOG_DEBUG, _LOG_TAG_IME, "%s {%d) > Openning a executable binary in the designated path has failed."
                        , __func__, __LINE__);
                return;
        }
@@ -519,10 +508,8 @@ scim_helper_module_run_helper(const scim::String& uuid, const scim::ConfigPointe
        //
        OspMain(__argc, __pArgv);
 
-       LOG(LOG_DEBUG, LOG_TAG_IME, "%s {%d) > scim_helper_module_run_helper() has been terminated.", __func__, __LINE__);
-       LOG(LOG_DEBUG, LOG_TAG_IME, "%s {%d) > uid is %d and euid is %d.", __func__, __LINE__, getuid(), geteuid());
-
-       return;
+       LOG(LOG_DEBUG, _LOG_TAG_IME, "%s {%d) > scim_helper_module_run_helper() has been terminated.", __func__, __LINE__);
+       LOG(LOG_DEBUG, _LOG_TAG_IME, "%s {%d) > uid is %d and euid is %d.", __func__, __LINE__, getuid(), geteuid());
 }
 
 #ifdef __cplusplus