Fix resource localize logic
authorSeungkeun Lee <sngn.lee@samsung.com>
Tue, 1 Sep 2015 07:37:10 +0000 (16:37 +0900)
committerSeungkeun Lee <sngn.lee@samsung.com>
Fri, 18 Sep 2015 09:07:33 +0000 (18:07 +0900)
 - Input Url could be localed url including locale folder
 - Input Url was encoded with URL encode

Change-Id: I956d17fd29efd2a756d9e9d0605a489c49c2c744

common/common.gyp
common/resource_manager.cc
common/resource_manager.h
common/string_utils.cc
common/string_utils.h
packaging/crosswalk-tizen.spec

index 170384d..c39de75 100644 (file)
@@ -50,6 +50,7 @@
           'capi-appfw-package-manager',
           'capi-appfw-application',
           'capi-system-system-settings',
+          'libcurl'
         ],
       },
       'direct_dependent_settings': {
index 6b2c864..fce50ce 100755 (executable)
@@ -288,6 +288,7 @@ std::string ResourceManager::GetLocalizedPath(const std::string& origin) {
     return find->second;
   }
   std::string& result = locale_cache_[origin];
+  result = origin;
   std::string url = origin;
 
   std::string suffix;
@@ -323,6 +324,7 @@ std::string ResourceManager::GetLocalizedPath(const std::string& origin) {
     return result;
   }
 
+  std::string file_path = utils::UrlDecode(RemoveLocalePath(url));
   for (auto& locales : locale_manager_->system_locales()) {
     // check ../locales/
     std::string app_locale_path = resource_base_path_ + locale_path;
@@ -335,14 +337,14 @@ std::string ResourceManager::GetLocalizedPath(const std::string& origin) {
     if (!Exists(app_localized_path)) {
       continue;
     }
-    std::string resource_path = app_localized_path + url;
+    std::string resource_path = app_localized_path + file_path;
     if (Exists(resource_path)) {
       result = "file://" + resource_path + suffix;
       return result;
     }
   }
 
-  std::string default_locale = resource_base_path_ + url;
+  std::string default_locale = resource_base_path_ + file_path;
   if (Exists(default_locale)) {
     result = "file://" + default_locale + suffix;
     return result;
@@ -352,6 +354,25 @@ std::string ResourceManager::GetLocalizedPath(const std::string& origin) {
   return result;
 }
 
+std::string ResourceManager::RemoveLocalePath(const std::string& path) {
+  std::string locale_path = "locales/";
+  std::string result_path = path.at(0) == '/' ? path : "/" + path;
+  if (!utils::StartsWith(result_path, resource_base_path_)) {
+    return path;
+  }
+
+  result_path = result_path.substr(resource_base_path_.length());
+  if (!utils::StartsWith(result_path, locale_path)) {
+    return result_path;
+  }
+
+  size_t found = result_path.find_first_of('/', locale_path.length());
+  if (found != std::string::npos) {
+    result_path = result_path.substr(found+1);
+  }
+  return result_path;
+}
+
 void ResourceManager::set_base_resource_path(const std::string& path) {
   if (path.empty()) {
     return;
index 16c5c98..19d0af0 100755 (executable)
@@ -86,6 +86,7 @@ class ResourceManager {
   bool Exists(const std::string& path);
   bool CheckWARP(const std::string& url);
   bool CheckAllowNavigation(const std::string& url);
+  std::string RemoveLocalePath(const std::string& path);
 
   std::string resource_base_path_;
   std::string appid_;
index 2049e7d..ee115d3 100644 (file)
 #include <time.h>
 #include <math.h>
 #include <uuid/uuid.h>
+#include <curl/curl.h>
+
 #include <string>
 #include <vector>
 #include <sstream>
 #include <iomanip>
 #include <algorithm>
+#include <memory>
+
 
 namespace common {
 namespace utils {
 
+namespace {
+std::unique_ptr<CURL, decltype(curl_easy_cleanup)*>
+    g_curl {nullptr, curl_easy_cleanup};
+}  // namespace
+
 std::string GenerateUUID() {
   char tmp[37];
   uuid_t uuid;
@@ -80,5 +89,27 @@ bool SplitString(const std::string &str,
   return true;
 }
 
+std::string UrlDecode(const std::string& url) {
+  if (g_curl.get() == nullptr) {
+    g_curl.reset(curl_easy_init());
+  }
+  std::unique_ptr<char, decltype(curl_free)*> decoded_str {
+      curl_easy_unescape(g_curl.get(), url.c_str(), url.length(), NULL),
+      curl_free };
+
+  return decoded_str.get() != nullptr ? std::string(decoded_str.get()) : url;
+}
+
+std::string UrlEncode(const std::string& url) {
+  if (g_curl.get() == nullptr) {
+    g_curl.reset(curl_easy_init());
+  }
+  std::unique_ptr<char, decltype(curl_free)*> encoded_str {
+      curl_easy_escape(g_curl.get(), url.c_str(), url.length()),
+      curl_free };
+
+  return encoded_str.get() != nullptr ? std::string(encoded_str.get()) : url;
+}
+
 }  // namespace utils
 }  // namespace common
index 7a60463..1532b27 100644 (file)
@@ -32,6 +32,9 @@ std::string GetCurrentMilliSeconds();
 bool SplitString(const std::string &str,
                  std::string *part_1, std::string *part_2, const char delim);
 
+std::string UrlEncode(const std::string& url);
+std::string UrlDecode(const std::string& url);
+
 }  // namespace utils
 }  // namespace common
 
index 1e1f0d9..1108211 100644 (file)
@@ -47,6 +47,7 @@ BuildRequires: pkgconfig(notification)
 BuildRequires: pkgconfig(sqlite3)
 BuildRequires: pkgconfig(ecore-wayland)
 BuildRequires: pkgconfig(chromium-efl)
+BuildRequires: pkgconfig(libcurl)
 
 %description
 Crosswalk Runtime and AppShell for Tizen 3.0 and later