Use cache to get system locale 59/258859/6
authorjh9216.park <jh9216.park@samsung.com>
Thu, 27 May 2021 03:05:23 +0000 (23:05 -0400)
committerjh9216.park <jh9216.park@samsung.com>
Fri, 28 May 2021 00:37:11 +0000 (20:37 -0400)
Change-Id: I955f1624be77e8c70a7ed73262169fe9d4769de3
Signed-off-by: jh9216.park <jh9216.park@samsung.com>
src/client/pkginfo_client.cc
src/common/system_locale.cc [new file with mode: 0644]
src/common/system_locale.hh [new file with mode: 0644]
src/pkgmgrinfo_private.c
src/pkgmgrinfo_private.h
src/server/runner.cc
src/server/runner.hh

index ea8dcee..e877a66 100644 (file)
 #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<uint8_t> raw = p.GetRaw();
-  std::unique_ptr<char, decltype(std::free)*> 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 (file)
index 0000000..557011f
--- /dev/null
@@ -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<SystemLocale*>(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 (file)
index 0000000..49196c6
--- /dev/null
@@ -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 <vconf.h>
+
+#include <string>
+
+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
index f1fc39d..87d940c 100644 (file)
@@ -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'};
index fe7170e..7a9d547 100644 (file)
@@ -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);
index 866a91b..d192c71 100644 (file)
@@ -44,19 +44,14 @@ Runner::Runner(unsigned int thread_num) {
   auto condition = static_cast<GIOCondition>(
       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<char, decltype(std::free)*> 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<Runner*>(user_data);
-  std::unique_ptr<char, decltype(std::free)*> 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) {
index 678e89e..c7b7a05 100644 (file)
@@ -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: