Specifiy that this supports M47
[platform/framework/web/crosswalk-tizen.git] / common / resource_manager.cc
old mode 100644 (file)
new mode 100755 (executable)
index 86d7155..8881428
@@ -17,6 +17,7 @@
 #include "common/resource_manager.h"
 
 #include <sys/types.h>
+#include <sys/stat.h>
 #include <aul.h>
 #include <pkgmgr-info.h>
 #include <stdio.h>
@@ -117,6 +118,9 @@ static bool CompareMime(const std::string& info_mime,
 
 static bool CompareUri(const std::string& info_uri,
                        const std::string& request_uri) {
+  if (info_uri == "*")
+    return true;
+
   if (request_uri.empty())
     return info_uri.empty();
 
@@ -182,7 +186,9 @@ bool ResourceManager::Resource::operator==(const Resource& res) {
 
 ResourceManager::ResourceManager(ApplicationData* application_data,
                                  LocaleManager* locale_manager)
-    : application_data_(application_data), locale_manager_(locale_manager) {
+    : application_data_(application_data),
+      locale_manager_(locale_manager),
+      security_model_version_(0) {
   if (application_data != NULL) {
     appid_ = application_data->tizen_application_info()->id();
     if (application_data->csp_info() != NULL ||
@@ -200,22 +206,25 @@ ResourceManager::GetDefaultResource() {
   std::string src;
   std::string type;
   std::string encoding = kDefaultEncoding;
-  auto content_info = application_data_->content_info();
-  if (content_info) {
-    src = content_info->src();
-    type = content_info->type();
-    encoding = content_info->encoding();
-    LOGGER(DEBUG) << "src: " << src;
-    LOGGER(DEBUG) << "type: " << type;
-    LOGGER(DEBUG) << "encoding: " << encoding;
-  } else {
-    LOGGER(DEBUG) << "content_info is null";
+
+  std::shared_ptr<const wgt::parse::ContentInfo> content_info;
+  if (application_data_) {
+    content_info = application_data_->content_info();
+    if (content_info) {
+      src = content_info->src();
+      type = content_info->type();
+      encoding = content_info->encoding();
+      LOGGER(DEBUG) << "src: " << src;
+      LOGGER(DEBUG) << "type: " << type;
+      LOGGER(DEBUG) << "encoding: " << encoding;
+    } else {
+      LOGGER(DEBUG) << "content_info is null";
+    }
   }
 
   // Check that tizen:content src is external page
-  if (content_info && content_info->is_tizen_content()
-      && (utils::StartsWith(src, kSchemeTypeHttp) ||
-          utils::StartsWith(src, kSchemeTypeHttps))) {
+  if (utils::StartsWith(src, kSchemeTypeHttp) ||
+      utils::StartsWith(src, kSchemeTypeHttps)) {
     LOGGER(DEBUG) << "tizen content_info's src is an external page";
     return std::unique_ptr<Resource>(new Resource(src, type, encoding));
   }
@@ -316,7 +325,7 @@ std::string ResourceManager::GetLocalizedPath(const std::string& origin) {
     if (utils::StartsWith(url, check)) {
       url.erase(0, check.length());
     } else {
-      LOGGER(ERROR) << "Invalid appid";
+      LOGGER(ERROR) << "Invalid uri: {scheme:app} uri=" << origin;
       return result;
     }
   } else if (utils::StartsWith(url, file_scheme)) {
@@ -329,7 +338,7 @@ std::string ResourceManager::GetLocalizedPath(const std::string& origin) {
   }
 
   if (url.empty()) {
-    LOGGER(ERROR) << "URL Localization error";
+    LOGGER(ERROR) << "Invalid uri: uri=" << origin;
     return result;
   }
 
@@ -359,7 +368,7 @@ std::string ResourceManager::GetLocalizedPath(const std::string& origin) {
     return result;
   }
 
-  LOGGER(ERROR) << "URL Localization error";
+  LOGGER(ERROR) << "Invalid uri: uri=" << origin << ", decoded=" << file_path;
   return result;
 }
 
@@ -510,12 +519,12 @@ bool ResourceManager::CheckAllowNavigation(const std::string& url) {
     bool prefix_wild = false;
     bool suffix_wild = false;
     std::string a_domain = a_domain_info.domain();
-    if (utils::StartsWith(a_domain, "*.")) {
+    if (utils::StartsWith(a_domain, "*")) {
       prefix_wild = true;
       // *.domain.com -> .domain.com
       a_domain = a_domain.substr(1);
     }
-    if (utils::EndsWith(a_domain, ".*")) {
+    if (utils::EndsWith(a_domain, "*")) {
       suffix_wild = true;
       // domain.* -> domain.
       a_domain = a_domain.substr(0, a_domain.length() - 1);
@@ -571,37 +580,21 @@ std::string ResourceManager::DecryptResource(const std::string& path) {
     src_path.erase(0, strlen(kSchemeTypeFile));
   }
 
-  FILE *src = fopen(src_path.c_str(), "rb");
-  if (!src) {
-    LOGGER(ERROR) << "Cannot open file for decryption: " << src_path;
-    return path;
-  }
-
-  // Read filesize
-  fseek(src, 0, SEEK_END);
-  size_t src_len = ftell(src);
-  rewind(src);
-
-  // Read buffer from the source file
-  std::unique_ptr<char> src_buf(new char[src_len]);
-  if (src_len != fread(src_buf.get(), sizeof(char), src_len, src)) {
-    LOGGER(ERROR) << "Read error, file: " << src_path;
-    fclose(src);
-    return path;
-  }
-  fclose(src);
+  // Remove the parameters at the end of an href attribute
+  size_t end_of_path = src_path.find_first_of("?#");
+  if (end_of_path != std::string::npos)
+    src_path = src_path.substr(0, end_of_path);
 
   // checking web app type
   static bool inited = false;
   static bool is_global = false;
   static bool is_preload = false;
 
-  int ret;
   std::string pkg_id = application_data_->pkg_id();
   if (!inited) {
     inited = true;
     pkgmgrinfo_pkginfo_h handle;
-    ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkg_id.c_str(), getuid(), &handle);
+    int ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkg_id.c_str(), getuid(), &handle);
     if (ret != PMINFO_R_OK) {
       LOGGER(ERROR) << "Could not get handle for pkginfo : pkg_id = "
                     << pkg_id;
@@ -625,58 +618,114 @@ std::string ResourceManager::DecryptResource(const std::string& path) {
     }
   }
 
-  wae_app_type_e app_type = WAE_DOWNLOADED_NORMAL_APP;
-  if (is_global) {
-    app_type = WAE_DOWNLOADED_GLOBAL_APP;
-  } else if (is_preload) {
-    app_type = WAE_PRELOADED_APP;
-  }
-
-  // decrypt buffer with wae functions
-  uint8_t* dst_buf = nullptr;
-  size_t dst_len = 0;
-  ret = wae_decrypt_web_application(pkg_id.c_str(),
-                                    app_type,
-                                    reinterpret_cast<uint8_t*>(src_buf.get()),
-                                    src_len,
-                                    &dst_buf,
-                                    &dst_len);
-  if (WAE_ERROR_NONE != ret) {
-    switch (ret) {
-    case WAE_ERROR_INVALID_PARAMETER:
-      LOGGER(ERROR) << "Error during decryption: WAE_ERROR_INVALID_PARAMETER";
-      break;
-    case WAE_ERROR_PERMISSION_DENIED:
-      LOGGER(ERROR) << "Error during decryption: WAE_ERROR_PERMISSION_DENIED";
-      break;
-    case WAE_ERROR_NO_KEY:
-      LOGGER(ERROR) << "Error during decryption: WAE_ERROR_NO_KEY";
-      break;
-    case WAE_ERROR_KEY_MANAGER:
-      LOGGER(ERROR) << "Error during decryption: WAE_ERROR_KEY_MANAGER";
-      break;
-    case WAE_ERROR_CRYPTO:
-      LOGGER(ERROR) << "Error during decryption: WAE_ERROR_CRYPTO";
-      break;
-    case WAE_ERROR_UNKNOWN:
-      LOGGER(ERROR) << "Error during decryption: WAE_ERROR_UNKNOWN";
-      break;
-    default:
-      LOGGER(ERROR) << "Error during decryption: UNKNOWN";
-      break;
+  struct stat buf;
+  memset(&buf, 0, sizeof(buf));
+  if (stat(src_path.c_str(), &buf) == 0) {
+    const std::size_t file_size = buf.st_size;
+    std::unique_ptr<unsigned char[]> in_chunk;
+
+    if (0 == file_size) {
+      LOGGER(ERROR) << src_path.c_str() << " size is 0, so decryption is skiped";
+      return path;
     }
-    return path;
-  }
 
-  // change to data schem
-  std::stringstream dst_str;
-  std::string content_type = GetMimeFromUri(path);
-  std::string encoded = utils::Base64Encode(dst_buf, dst_len);
-  dst_str << "data:" << content_type << ";base64," << encoded;
+    FILE *src = fopen(src_path.c_str(), "rb");
+    if (src == NULL) {
+      LOGGER(ERROR) << "Cannot open file for decryption: " << path;
+      return path;
+    }
 
-  std::free(dst_buf);
+    // Read buffer from the source file
+    std::unique_ptr<unsigned char[]> decrypted_str(new unsigned char[file_size]);
+    int decrypted_size = 0;
+
+    do {
+      unsigned char get_dec_size[5];
+      memset(get_dec_size, 0x00, sizeof(get_dec_size));
+
+      size_t read_size =
+        fread(get_dec_size, 1, 4, src);
+      if (0 != read_size) {
+        unsigned int read_buf_size = 0;
+        std::istringstream(std::string((char*)get_dec_size)) >> read_buf_size;
+        if (read_buf_size == 0) {
+          LOGGER(ERROR) << "Failed to read resource";
+          fclose(src);
+          return path;
+        }
+        in_chunk.reset(new unsigned char[read_buf_size]);
+
+        size_t dec_read_size =
+          fread(in_chunk.get(), 1, read_buf_size, src);
+        if (0 != dec_read_size) {
+          unsigned char* decrypted_data = nullptr;
+          size_t decrypted_len = 0;
+          int ret;
+          if (is_global) {
+            ret = wae_decrypt_global_web_application(pkg_id.c_str(),
+                                                     is_preload,
+                                                     in_chunk.get(),
+                                                     (int)dec_read_size,
+                                                     &decrypted_data,
+                                                     &decrypted_len);
+          } else {
+            ret = wae_decrypt_web_application(getuid(),
+                                              pkg_id.c_str(),
+                                              in_chunk.get(),
+                                              (int)dec_read_size,
+                                              &decrypted_data,
+                                              &decrypted_len);
+          }
+
+          if (WAE_ERROR_NONE != ret) {
+            LOGGER(ERROR) << "Error during decryption: ";
+            switch (ret) {
+            case WAE_ERROR_INVALID_PARAMETER:
+              LOGGER(ERROR) << "WAE_ERROR_INVALID_PARAMETER";
+              break;
+            case WAE_ERROR_PERMISSION_DENIED:
+              LOGGER(ERROR) << "WAE_ERROR_PERMISSION_DENIED";
+              break;
+            case WAE_ERROR_NO_KEY:
+              LOGGER(ERROR) << "WAE_ERROR_NO_KEY";
+              break;
+            case WAE_ERROR_KEY_MANAGER:
+              LOGGER(ERROR) << "WAE_ERROR_KEY_MANAGER";
+              break;
+            case WAE_ERROR_CRYPTO:
+              LOGGER(ERROR) << "WAE_ERROR_CRYPTO";
+              break;
+            case WAE_ERROR_UNKNOWN:
+              LOGGER(ERROR) << "WAE_ERROR_UNKNOWN";
+              break;
+            default:
+              LOGGER(ERROR) << "UNKNOWN";
+              break;
+            }
+            fclose(src);
+            return path;
+          }
+
+          memcpy(decrypted_str.get() + decrypted_size, decrypted_data, decrypted_len);
+          decrypted_size += decrypted_len;
+          std::free(decrypted_data);
+        }
+      }
+    } while(0 == std::feof(src));
+    fclose(src);
+    memset(decrypted_str.get() + decrypted_size, '\n', file_size - decrypted_size);
 
-  return dst_str.str();
+    // change to data schem
+    std::stringstream dst_str;
+    std::string content_type = GetMimeFromUri(path);
+    std::string encoded = utils::Base64Encode(decrypted_str.get(), decrypted_size);
+    dst_str << "data:" << content_type << ";base64," << encoded;
+
+    decrypted_str.reset(new unsigned char[file_size]);
+
+    return dst_str.str();
+  }
+  return path;
 }
 
 }  // namespace common