Implement auto_file_key feature 61/237661/8
authorSangyoon Jang <jeremy.jang@samsung.com>
Thu, 2 Jul 2020 08:25:13 +0000 (17:25 +0900)
committerSangyoon Jang <jeremy.jang@samsung.com>
Tue, 14 Jul 2020 06:26:06 +0000 (15:26 +0900)
Change-Id: I5bb4ba4c4e704af4f4476c07a69f7825c8f28d0d
Signed-off-by: Sangyoon Jang <jeremy.jang@samsung.com>
src/theme_plugin/theme_info_builder.cc
src/theme_plugin/theme_info_builder.h
src/theme_plugin/theme_metadata_plugin.cc
src/theme_plugin/theme_parser.cc
src/theme_plugin/theme_parser.h
test/unit_tests/test_samples/test_theme.json
test/unit_tests/test_theme_parser.cc

index 7a68df1..f459921 100644 (file)
@@ -4,6 +4,22 @@
 
 #include "theme_plugin/theme_info_builder.h"
 
+#include <algorithm>
+#include <string>
+
+#include "theme/utils/logging.h"
+
+namespace {
+
+const char kThemeFilesPrefix[] = "theme/files";
+
+bool StartsWith(const std::string& str, const std::string& substr) {
+  return str.length() >= substr.length() &&
+      std::equal(substr.begin(), substr.end(), str.begin());
+}
+
+}  // namespace
+
 namespace ttm {
 namespace plugin {
 
@@ -27,5 +43,22 @@ loader::ThemeInfo ThemeInfoBuilder::Build() {
   return loader::ThemeInfo(bundle_);
 }
 
+void ThemeInfoBuilder::GenerateFileKey() {
+  std::vector<tizen_base::Bundle::KeyInfo> keyinfos = bundle_.GetKeys();
+  for (const auto& keyinfo : keyinfos) {
+    std::string key = keyinfo.GetName();
+    if (!StartsWith(key, kThemeFilesPrefix))
+      continue;
+
+    size_t dot_pos = key.find_last_of(".");
+    if (dot_pos == std::string::npos)
+      continue;
+
+    std::string newkey = key.substr(0, dot_pos);
+    LOG(DEBUG) << "Adding generated key: " << newkey;
+    bundle_.Add(newkey, bundle_.GetString(key));
+  }
+}
+
 }  // namespace plugin
 }  // namespace ttm
index 130fdf7..2099dc4 100644 (file)
@@ -23,6 +23,7 @@ class ThemeInfoBuilder {
   ThemeInfoBuilder& PutStringArray(const std::string& key,
       const std::vector<std::string>& value);
   loader::ThemeInfo Build();
+  void GenerateFileKey();
 
  private:
   tizen_base::Bundle bundle_;
index 56678a9..430ece6 100644 (file)
@@ -26,6 +26,7 @@ using ttm::plugin::ThemeParser;
 namespace {
 
 const char kAutosetKey[] = "http://tizen.org/metadata/theme/auto_set";
+const char kAutoFileKeyKey[] = "http://tizen.org/metadata/theme/auto_file_key";
 const char kDefaultThemeKey[] = "http://tizen.org/metadata/theme/default";
 const char kThemeKey[] = "http://tizen.org/metadata/theme";
 
@@ -49,7 +50,8 @@ std::string GetThemeFilePath(const char* pkgid) {
 }
 
 bool ParseAndCommitTheme(ThemeParser::ThemeOperation op, const std::string& id,
-    const std::string& pkgid, const std::string& path, bool is_default) {
+    const std::string& pkgid, const std::string& path, bool is_default,
+    bool auto_file_key_gen) {
   uid_t uid;
   int r = pkgmgr_installer_info_get_target_uid(&uid);
   if (r < 0) {
@@ -57,7 +59,7 @@ bool ParseAndCommitTheme(ThemeParser::ThemeOperation op, const std::string& id,
     return false;
   }
 
-  ThemeParser parser(path);
+  ThemeParser parser(path, auto_file_key_gen);
   ThemeInfo theme = parser.Inflate(id, pkgid, uid, is_default);
   if (!theme.IsValid()) {
     LOG(ERROR) << "Failed to parse theme";
@@ -102,6 +104,7 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(
     return -1;
 
   bool is_default = false;
+  bool auto_file_key_gen = false;
   std::string json_file;
   for (__metadata_t* md : GListRange<__metadata_t*>(metadata)) {
     // Assume that there is a only json file for theme
@@ -109,6 +112,8 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(
       json_file = root_path + md->value;
     } else if (!strcmp(md->key, kDefaultThemeKey)) {
       is_default = true;
+    } else if (!strcmp(md->key, kAutoFileKeyKey)) {
+      auto_file_key_gen = true;
     } else if (!strcmp(md->key, kAutosetKey)) {
       if (IsPlatformPrivilege()) {
         LOG(INFO) << "Found auto_set attribute. "
@@ -122,7 +127,7 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(
   }
 
   if (!ParseAndCommitTheme(ThemeParser::ThemeOperation::ADD, appid, pkgid,
-      json_file, is_default))
+      json_file, is_default, auto_file_key_gen))
     return -1;
   return 0;
 }
@@ -133,16 +138,19 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_UPGRADE(
   if (root_path.empty())
     return -1;
 
+  bool auto_file_key_gen = false;
   std::string json_file;
   for (__metadata_t* md : GListRange<__metadata_t*>(metadata)) {
     // Assume that there is a only json file for theme
     if (!strcmp(md->key, kThemeKey))
       json_file = root_path + md->value;
+    else if (!strcmp(md->key, kAutoFileKeyKey))
+      auto_file_key_gen = true;
   }
 
   // Upgrading default theme is not allowed
   if (!ParseAndCommitTheme(ThemeParser::ThemeOperation::UPDATE, appid, pkgid,
-      json_file, false))
+      json_file, false, auto_file_key_gen))
     return -1;
   return 0;
 }
@@ -162,7 +170,7 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(
 
   // Removing default theme is not allowed
   if (!ParseAndCommitTheme(ThemeParser::ThemeOperation::REMOVE, appid, pkgid,
-      json_file, false))
+      json_file, false, false))
     return -1;
   return 0;
 }
index c7f6f3b..3431bcc 100644 (file)
@@ -100,6 +100,9 @@ loader::ThemeInfo ThemeParser::Inflate(const std::string id,
     return {};
   }
 
+  if (auto_file_key_gen_)
+    builder.GenerateFileKey();
+
   return builder.Build();
 }
 
index e1f486f..91505b6 100644 (file)
@@ -25,7 +25,8 @@ class ThemeParser {
     REMOVE
   };
 
-  explicit ThemeParser(const std::string& path) : path_(path) {}
+  explicit ThemeParser(const std::string& path, bool auto_file_key_gen)
+      : path_(path), auto_file_key_gen_(auto_file_key_gen) {}
   loader::ThemeInfo Inflate(const std::string id, const std::string pkgid,
       uid_t uid, bool is_default);
   bool Commit(ThemeOperation operation, const loader::ThemeInfo& theme);
@@ -39,6 +40,7 @@ class ThemeParser {
       std::string parent_key, ThemeInfoBuilder* builder);
 
   std::string path_;
+  bool auto_file_key_gen_;
 };
 
 }  // namespace plugin
index ee49513..8ac0120 100644 (file)
                 },
                 "keyboard": {
                         "keypad_bg_color": "#050a28"
+                },
+                "files": {
+                        "shared/res/image1.png": "shared/res/image1.png",
+                        "shared/res/image2.svg": "shared/res/image2.svg"
                 }
         }
 }
index f6ca49d..629bbf0 100644 (file)
@@ -34,8 +34,8 @@ class ThemeParserTest : public testing::Test {
 };
 
 TEST_F(ThemeParserTest, Inflate) {
-  ttm::plugin::ThemeParser parser("test_samples/test_theme.json");
-  auto info = parser.Inflate("testid", "testpkgid", 5001, false);
+  ttm::plugin::ThemeParser parser("test_samples/test_theme.json", true);
+  auto info = parser.Inflate("testid", "testpkgid", 5001, true);
   EXPECT_EQ(info.GetId(), "testid");
   EXPECT_EQ(info.GetPkgid(), "testpkgid");
   EXPECT_EQ(info.GetUid(), 5001);
@@ -63,11 +63,19 @@ TEST_F(ThemeParserTest, Inflate) {
       "https://www.download.gogowatch/get");
   EXPECT_EQ(info.GetStringArray("theme/preview"),
       std::vector<std::string>({"GOGO_Preview.png"}));
+  EXPECT_EQ(info.GetString("theme/files/shared/res/image1.png"),
+      "shared/res/image1.png");
+  EXPECT_EQ(info.GetString("theme/files/shared/res/image1"),
+      "shared/res/image1.png");
+  EXPECT_EQ(info.GetString("theme/files/shared/res/image2.svg"),
+      "shared/res/image2.svg");
+  EXPECT_EQ(info.GetString("theme/files/shared/res/image2"),
+      "shared/res/image2.svg");
 }
 
 TEST_F(ThemeParserTest, MultidimensionalArray) {
   ttm::plugin::ThemeParser parser(
-      "test_samples/test_multidimensional_array.json");
+      "test_samples/test_multidimensional_array.json", false);
   auto info = parser.Inflate("testid", "testpkgid", 5001, false);
   std::vector<std::string> array_value({"1", "2", "3"});
   EXPECT_EQ(info.GetStringArray("1D_array"), array_value);
@@ -81,21 +89,21 @@ TEST_F(ThemeParserTest, MultidimensionalArray) {
 
 TEST_F(ThemeParserTest, InvalidJson) {
   ttm::plugin::ThemeParser parser(
-      "test_samples/test_invalid_json.json");
+      "test_samples/test_invalid_json.json", false);
   auto info = parser.Inflate("testid", "testpkgid", 5001, false);
   EXPECT_FALSE(info.IsValid());
 }
 
 TEST_F(ThemeParserTest, DifferentArrayElementType) {
   ttm::plugin::ThemeParser parser(
-      "test_samples/test_different_array_element_type.json");
+      "test_samples/test_different_array_element_type.json", false);
   auto info = parser.Inflate("testid", "testpkgid", 5001, false);
   EXPECT_FALSE(info.IsValid());
 }
 
 TEST_F(ThemeParserTest, EmptyArray) {
   ttm::plugin::ThemeParser parser(
-      "test_samples/test_empty_array.json");
+      "test_samples/test_empty_array.json", false);
   auto info = parser.Inflate("testid", "testpkgid", 5001, false);
   EXPECT_FALSE(info.IsValid());
 }