Fix ThemeInfo::GetPath() 31/255331/1
authorSangyoon Jang <jeremy.jang@samsung.com>
Wed, 17 Mar 2021 04:57:55 +0000 (13:57 +0900)
committerSangyoon Jang <jeremy.jang@samsung.com>
Wed, 17 Mar 2021 04:57:55 +0000 (13:57 +0900)
- Fix getting path when using auto generated file key.
- Fix crash when default theme is not installed.

Change-Id: I2ee0b8e25a0751097bdc36f0d70d4ae008fad6a7
Signed-off-by: Sangyoon Jang <jeremy.jang@samsung.com>
src/theme/loader/theme_info.cc

index 2451658..cf2f532 100644 (file)
@@ -52,6 +52,13 @@ bool StringCompareIgnoreCase(const std::string& str1, const std::string str2) {
       });
 }
 
+bool StartsWith(const std::string& str, const std::string& substr) {
+  return str.length() >= substr.length() &&
+      std::equal(substr.begin(), substr.end(), str.begin());
+}
+
+constexpr const char kThemeFilesPrefix[] = "/theme/files/";
+
 }  // namespace
 
 namespace ttm {
@@ -105,11 +112,19 @@ std::string ThemeInfo::GetString(const std::string& key) const {
 }
 
 std::string ThemeInfo::GetPath(const std::string& key) const {
+  std::string key_to_get;
+  if (StartsWith(key, kThemeFilesPrefix)) {
+    auto pos = key.rfind("/");
+    key_to_get = key.substr(0, pos);
+  } else {
+    key_to_get = key;
+  }
+
   bool is_overlayed = false;
   std::vector<std::string> selected_keys =
       bundle_.GetStringArray("selected_keys");
-  auto it = std::find(selected_keys.begin(), selected_keys.end(), key);
-  if (it != selected_keys.end())
+  auto it = std::find(selected_keys.begin(), selected_keys.end(), key_to_get);
+  if (it != selected_keys.end() || selected_keys.empty())
     is_overlayed = true;
 
   std::string pkgid;
@@ -119,7 +134,26 @@ std::string ThemeInfo::GetPath(const std::string& key) const {
     uid = GetUid();
   } else {
     pkgid = bundle_.GetString("default_pkgid");
-    uid = std::stoi(bundle_.GetString("default_uid"));
+    if (pkgid.empty()) {
+      LOG(WARNING) << "Default Theme may not installed";
+      return {};
+    }
+    std::string uid_str = bundle_.GetString("default_uid");
+    if (uid_str.empty()) {
+      LOG(WARNING) << "Default Theme may not installed";
+      return {};
+    }
+    try {
+      uid = std::stoi(uid_str);
+    } catch (const std::invalid_argument& e) {
+      LOG(ERROR) << "Exception occurred while getting default theme uid: "
+                 << e.what();
+      return {};
+    } catch (const std::out_of_range& e) {
+      LOG(ERROR) << "Exception occurred while getting default theme uid: "
+                 << e.what();
+      return {};
+    }
   }
 
   std::string root_path = GetPkgRootPath(pkgid, uid);
@@ -128,7 +162,11 @@ std::string ThemeInfo::GetPath(const std::string& key) const {
     return {};
   }
 
-  std::string val = bundle_.GetString(key);
+  std::string val = bundle_.GetString(key_to_get);
+  if (val.empty()) {
+    LOG(ERROR) << "Failed to get path for key: " << key;
+    return {};
+  }
   return root_path + "/" + val;
 }