From 55dfbd7da6776f6688edd1dd6e50bb8702d47164 Mon Sep 17 00:00:00 2001 From: "jh9216.park" Date: Wed, 26 May 2021 23:05:23 -0400 Subject: [PATCH] Use cache to get system locale Change-Id: I955f1624be77e8c70a7ed73262169fe9d4769de3 Signed-off-by: jh9216.park --- src/client/pkginfo_client.cc | 9 ++---- src/common/system_locale.cc | 68 ++++++++++++++++++++++++++++++++++++++++++++ src/common/system_locale.hh | 45 +++++++++++++++++++++++++++++ src/pkgmgrinfo_private.c | 33 --------------------- src/pkgmgrinfo_private.h | 1 - src/server/runner.cc | 21 ++++---------- src/server/runner.hh | 7 +++-- 7 files changed, 126 insertions(+), 58 deletions(-) create mode 100644 src/common/system_locale.cc create mode 100644 src/common/system_locale.hh diff --git a/src/client/pkginfo_client.cc b/src/client/pkginfo_client.cc index ea8dcee..e877a66 100644 --- a/src/client/pkginfo_client.cc +++ b/src/client/pkginfo_client.cc @@ -17,10 +17,10 @@ #include "query_request_handler.hh" #include "set_cert_request_handler.hh" #include "set_pkginfo_request_handler.hh" +#include "system_locale.hh" #include "pkgmgrinfo_debug.h" - namespace pkgmgr_client { constexpr const char SOCK_PATH[] = "/run/pkgmgr-info-server"; @@ -155,12 +155,9 @@ bool PkgInfoClient::RequestHandlerDirectAccess() { tizen_base::Parcel p; p.WriteParcelable(*parcel_.get()); std::vector raw = p.GetRaw(); - std::unique_ptr locale( - _get_system_locale(), std::free); - if (locale.get() == nullptr) - return false; - handler->HandleRequest(&raw[0], raw.size(), locale.get()); + handler->HandleRequest(&raw[0], raw.size(), + pkgmgr_common::SystemLocale::GetInst().Get()); auto result = handler->ExtractResult(); if (result.size() == 0) return true; diff --git a/src/common/system_locale.cc b/src/common/system_locale.cc new file mode 100644 index 0000000..557011f --- /dev/null +++ b/src/common/system_locale.cc @@ -0,0 +1,68 @@ +// Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved +// Use of this source code is governed by an apache-2.0 license that can be +// found in the LICENSE file. + +#include "system_locale.hh" + +namespace { + +constexpr const char DEFAULT_LOCALE[] = "No Locale"; + +} // namespace + +namespace pkgmgr_common { + +SystemLocale& SystemLocale::GetInst() { + static SystemLocale inst; + + return inst; +} + +SystemLocale::SystemLocale() { + vconf_notify_key_changed(VCONFKEY_LANGSET, LanChangedCb, this); + char* lang = vconf_get_str(VCONFKEY_LANGSET); + lang_ = lang ? std::string(lang) : ""; + free(lang); + SetLocale(); +} + +SystemLocale::~SystemLocale() { + vconf_ignore_key_changed(VCONFKEY_LANGSET, LanChangedCb); +} + +void SystemLocale::RegisterEvent(IEvent* listener) { + listener_ = listener; +} + +void SystemLocale::UnRegisterEvent() { + listener_ = nullptr; +} + +const std::string& SystemLocale::Get() const { + return locale_; +} + +void SystemLocale::SetLocale() { + if (lang_.empty() || lang_.length() < 5) { + locale_ = DEFAULT_LOCALE; + return; + } + + char local_buf[6] = {0, }; + snprintf(local_buf, sizeof(local_buf), "%c%c-%c%c", + lang_[0], lang_[1], tolower(lang_[3]), tolower(lang_[4])); + locale_ = local_buf; + if (listener_) + listener_->OnChanged(locale_); +} + +void SystemLocale::LanChangedCb(keynode_t* node, void* user_data) { + char* val = vconf_keynode_get_str(node); + if (val == nullptr) + return; + SystemLocale* sl = reinterpret_cast(user_data); + sl->lang_ = val; + sl->SetLocale(); +} + +} // namespace pkgmgr_common \ No newline at end of file diff --git a/src/common/system_locale.hh b/src/common/system_locale.hh new file mode 100644 index 0000000..49196c6 --- /dev/null +++ b/src/common/system_locale.hh @@ -0,0 +1,45 @@ +// Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved +// Use of this source code is governed by an apache-2.0 license that can be +// found in the LICENSE file. + +#ifndef COMMON_SYSTEM_LOCALE_HH_ +#define COMMON_SYSTEM_LOCALE_HH_ + +#include + +#include + +namespace pkgmgr_common { + +#ifndef EXPORT_API +#define EXPORT_API __attribute__((visibility("default"))) +#endif + +class EXPORT_API SystemLocale { + public: + class IEvent { + public: + virtual ~IEvent() = default; + virtual void OnChanged(const std::string& locale) = 0; + }; + + static SystemLocale& GetInst(); + const std::string& Get() const; + void RegisterEvent(IEvent* listener); + void UnRegisterEvent(); + + private: + SystemLocale(); + ~SystemLocale(); + void SetLocale(); + static void LanChangedCb(keynode_t* node, void* user_data); + + private: + std::string lang_; + std::string locale_; + IEvent* listener_ = nullptr; +}; + +} // namespace pkgmgr_common + +#endif // COMMON_SYSTEM_LOCALE_HH_ \ No newline at end of file diff --git a/src/pkgmgrinfo_private.c b/src/pkgmgrinfo_private.c index f1fc39d..87d940c 100644 --- a/src/pkgmgrinfo_private.c +++ b/src/pkgmgrinfo_private.c @@ -494,39 +494,6 @@ API int _add_label_info_into_list(const char *locale, char *value, GList **label return PMINFO_R_OK; } -API char *_get_system_locale(void) -{ - char *lang; - char *locale; - - lang = vconf_get_str(VCONFKEY_LANGSET); - if (lang == NULL) { - locale = strdup(DEFAULT_LOCALE); - if (locale == NULL) { - LOGE("out of memory"); - return NULL; - } - return locale; - } - - locale = malloc(sizeof(char) * 6); - if (locale == NULL) { - LOGE("out of memory"); - free(lang); - return NULL; - } - - strncpy(locale, lang, 2); - locale[2] = '-'; - locale[3] = tolower(lang[3]); - locale[4] = tolower(lang[4]); - locale[5] = '\0'; - - free(lang); - - return locale; -} - API int __pkginfo_check_installed_storage(package_x *pkginfo) { char buf[MAX_QUERY_LEN] = {'\0'}; diff --git a/src/pkgmgrinfo_private.h b/src/pkgmgrinfo_private.h index fe7170e..7a9d547 100644 --- a/src/pkgmgrinfo_private.h +++ b/src/pkgmgrinfo_private.h @@ -325,7 +325,6 @@ void _pkgmgrinfo_node_destroy(pkgmgrinfo_node_x *node); int _check_create_cert_db(void); void _save_column_int(sqlite3_stmt *stmt, int idx, int *i); void _save_column_str(sqlite3_stmt *stmt, int idx, char **str); -char *_get_system_locale(void); int __get_filter_condition(gpointer data, uid_t uid, char **condition, GList **param); int __get_metadata_filter_condition(gpointer data, char **condition, GList **param); int _add_icon_info_into_list(const char *locale, char *value, GList **icon); diff --git a/src/server/runner.cc b/src/server/runner.cc index 866a91b..d192c71 100644 --- a/src/server/runner.cc +++ b/src/server/runner.cc @@ -44,19 +44,14 @@ Runner::Runner(unsigned int thread_num) { auto condition = static_cast( G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL); sid_ = g_unix_fd_add(server_->GetFd(), condition, OnReceiveRequest, this); - vconf_notify_key_changed(VCONFKEY_LANGSET, OnLanguageChange, this); - std::unique_ptr locale( - _get_system_locale(), std::free); - if (locale.get() == nullptr) - return; - - thread_pool_->SetLocale(locale.get()); + pkgmgr_common::SystemLocale::GetInst().RegisterEvent(this); + thread_pool_->SetLocale(pkgmgr_common::SystemLocale::GetInst().Get()); LOGI("Start Runner"); } Runner::~Runner() { g_source_remove(sid_); - vconf_ignore_key_changed(VCONFKEY_LANGSET, OnLanguageChange); + pkgmgr_common::SystemLocale::GetInst().UnRegisterEvent(); } int Runner::OnReceiveRequest(int fd, GIOCondition cond, void* user_data) { @@ -77,14 +72,8 @@ int Runner::OnReceiveRequest(int fd, GIOCondition cond, void* user_data) { return G_SOURCE_CONTINUE; } -void Runner::OnLanguageChange(keynode_t* key, void* user_data) { - auto runner = static_cast(user_data); - std::unique_ptr locale( - _get_system_locale(), std::free); - if (locale.get() == nullptr) - return; - - runner->thread_pool_->SetLocale(locale.get()); +void Runner::OnChanged(const std::string& locale) { + thread_pool_->SetLocale(locale); } bool Runner::QueueRequest(int client_fd) { diff --git a/src/server/runner.hh b/src/server/runner.hh index 678e89e..c7b7a05 100644 --- a/src/server/runner.hh +++ b/src/server/runner.hh @@ -26,6 +26,7 @@ #include "server_socket.hh" #include "worker_thread.hh" +#include "system_locale.hh" namespace pkgmgr_server { @@ -33,14 +34,16 @@ namespace pkgmgr_server { #define EXPORT_API __attribute__((visibility("default"))) #endif -class EXPORT_API Runner { +class EXPORT_API Runner + : public pkgmgr_common::SystemLocale::IEvent { public: Runner(unsigned int thread_num); ~Runner(); + void OnChanged(const std::string& locale) override; + private: static int OnReceiveRequest(int fd, GIOCondition cond, void* user_data); - static void OnLanguageChange(keynode_t* key, void* user_data); bool QueueRequest(int client_fd); private: -- 2.7.4