Merge branch 'tizen_3.0' into tizen 19/139519/1 accepted/tizen/unified/20170719.132325 submit/tizen/20170719.080849 submit/tizen/20170719.081707
authorLukasz Bardeli <l.bardeli@samsung.com>
Wed, 19 Jul 2017 07:57:08 +0000 (09:57 +0200)
committerLukasz Bardeli <l.bardeli@samsung.com>
Wed, 19 Jul 2017 07:58:07 +0000 (09:58 +0200)
Change-Id: I5d4245b4f62c0802e9d73397df97d097683d05b9

packaging/webapi-plugins.spec
src/application/application_utils.cc
src/common/tools.cc
src/common/tools.h
src/contact/contact_util.cc
src/exif/exif_api.js
src/exif/exif_instance.cc
src/systeminfo/systeminfo_api.js

index dcb78cb..c69cac6 100644 (file)
@@ -8,7 +8,7 @@
 %define crosswalk_extensions_path %{_libdir}/%{crosswalk_extensions}
 
 Name:       webapi-plugins
-Version:    1.91
+Version:    1.92
 Release:    0
 License:    Apache-2.0 and BSD-3-Clause and MIT
 Group:      Development/Libraries
index 8d56999..060dd09 100644 (file)
@@ -77,7 +77,6 @@ void ApplicationUtils::CreateApplicationInformation(const pkgmgrinfo_appinfo_h h
   // categories
   picojson::value categories = picojson::value(picojson::array());
   picojson::array& categories_array = categories.get<picojson::array>();
-  app_info->insert(std::make_pair("categories", categories));
 
   ret = pkgmgrinfo_appinfo_foreach_category(
       handle,
@@ -92,6 +91,8 @@ void ApplicationUtils::CreateApplicationInformation(const pkgmgrinfo_appinfo_h h
   },
   &categories_array);
 
+  app_info->insert(std::make_pair("categories", categories));
+
   if (PMINFO_R_OK != ret) {
     LoggerE("Failed to get categories: %d (%s)", ret, get_error_message(ret));
   }
index 638352f..cd8beea 100644 (file)
@@ -19,6 +19,7 @@
 #include <privilegemgr/privilege_manager.h>
 #include <app_manager.h>
 #include <pkgmgr-info.h>
+#include <sys/stat.h>
 
 #ifdef PRIVILEGE_USE_DB
 #include <sqlite3.h>
@@ -480,5 +481,59 @@ char* BinToHex(const unsigned char* bin, int size, char* hex, int hex_size) {
   return hex;
 }
 
+bool IsPathValid(const std::string& path) {
+  LoggerD("Enter");
+
+  /*
+   * Directory dot-referencing is not allowed
+   */
+  return std::string::npos == path.find("/../") &&
+       std::string::npos == path.find("/./") &&
+       0 != path.find("./") &&
+       0 != path.find("../") &&
+       path.length() - 2 != path.rfind("/.") &&
+       path.length() - 3 != path.rfind("/..");
+}
+
+PlatformResult CheckFileStatus(const std::string& path) {
+  LoggerD("Enter");
+
+  struct stat buf;
+
+  if (stat(path.c_str(), &buf)) {
+    LoggerD("Failed to stat path: %s", path.c_str());
+
+    if (ENOENT == errno) {
+      return PlatformResult(ErrorCode::NOT_FOUND_ERR, "File does not exist: " + path);
+    } else if (EACCES == errno) {
+      return PlatformResult(ErrorCode::IO_ERR, "The user cannot access the file: " + path);
+    }
+
+    LoggerD("stat() error: %s", common::tools::GetErrorString(errno).c_str());
+    return PlatformResult(ErrorCode::UNKNOWN_ERR, "Cannot get status of the file: " + path);
+  }
+
+  if (!S_ISREG(buf.st_mode)) {
+      return PlatformResult(ErrorCode::NOT_FOUND_ERR, "Path does not point to a regular file: "
+              + path);
+  }
+
+  if (!(S_IRUSR & buf.st_mode)) {
+    return PlatformResult(ErrorCode::IO_ERR, "The user cannot read the file: " + path);
+  }
+
+  return PlatformResult(ErrorCode::NO_ERROR);
+}
+
+PlatformResult CheckFileAvailability(const std::string& path) {
+  LoggerD("Enter");
+
+  if (!IsPathValid(path)) {
+    return PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Invalid path: " + path);
+  }
+
+  return CheckFileStatus(path);
+}
+
 }  // namespace tools
 }  // namespace common
index 5d8eec6..fc905ec 100644 (file)
@@ -85,6 +85,12 @@ int HexToInt(char c);
 unsigned char* HexToBin(const char* hex, int size, unsigned char* bin, int bin_size);
 char* BinToHex(const unsigned char* bin, int size, char* hex, int hex_size);
 
+bool IsPathValid(const std::string& path);
+
+PlatformResult CheckFileStatus(const std::string& path);
+
+PlatformResult CheckFileAvailability(const std::string& path);
+
 }  // namespace tools
 }  // namespace common
 
index bcccd3b..6251ee1 100755 (executable)
@@ -584,39 +584,29 @@ PlatformResult ExportContactNameToContactsRecord(
 
   for (auto& nickname : nicknames) {
     contacts_record_h nickname_record = nullptr;
-    err = contacts_record_get_child_record_at_p(
-        contacts_record, _contacts_contact.nickname, 0, &nickname_record);
-    if (CONTACTS_ERROR_NONE != err && nullptr == nickname_record) {
-      err = contacts_record_create(_contacts_nickname._uri, &nickname_record);
-      PlatformResult status =
-          ContactUtil::ErrorChecker(err, "Contacts record create error");
-      if (status.IsError()) {
-        LoggerE("Error: %s", status.message().c_str());
-        return status;
-      }
-
-      update = false;
+    err = contacts_record_create(_contacts_nickname._uri, &nickname_record);
+    PlatformResult status = ContactUtil::ErrorChecker(err, "Contacts record create error");
+    if (status.IsError()) {
+      LoggerE("Error: %s", status.message().c_str());
+      return status;
     }
     ContactsRecordHPtr nickname_ptr(&nickname_record, ContactsDeleter);
 
-    PlatformResult status =
-        ContactUtil::SetStrInRecord(*nickname_ptr, _contacts_nickname.name,
-                                    JsonCast<JsonString>(nickname).c_str());
+    status = ContactUtil::SetStrInRecord(*nickname_ptr, _contacts_nickname.name,
+                                         JsonCast<JsonString>(nickname).c_str());
     if (status.IsError()) {
       LoggerE("Error: %s", status.message().c_str());
       return status;
     }
 
-    if (!update) {
-      err = contacts_record_add_child_record(
-          contacts_record, _contacts_contact.nickname, *nickname_ptr);
-      PlatformResult status =
-          ContactUtil::ErrorChecker(err, "Contacts record add child error");
-      if (status.IsError()) {
-        LoggerE("Error: %s", status.message().c_str());
-        return status;
-      }
+    err = contacts_record_add_child_record(contacts_record, _contacts_contact.nickname,
+                                           *nickname_ptr);
+    status = ContactUtil::ErrorChecker(err, "Contacts record add child error");
+    if (status.IsError()) {
+      LoggerE("Error: %s", status.message().c_str());
+      return status;
     }
+
     // Do not delete record, it is passed to the platform
     nickname_ptr.release();
   }
index d209d8a..3040336 100644 (file)
@@ -205,7 +205,6 @@ ExifManager.prototype.getExifInfo = function() {
     if (native_.isFailure(result)) {
       native_.callIfPossible(args.errorCallback, native_.getErrorObject(result));
     } else {
-
       // call to c++ code. Fields that do not exist are undefined.
       var exifInfoNative = native_.getResultObject(result);
 
@@ -218,15 +217,7 @@ ExifManager.prototype.getExifInfo = function() {
     }
   };
 
-  tizen.filesystem.resolve(args.uri,
-      function() {
-        native_.call('ExifManager_getExifInfo', {'uri': args.uri}, callback);
-      },
-      function() {
-        native_.callIfPossible(args.errorCallback, new WebAPIException(
-            WebAPIException.NOT_FOUND_ERR,
-            'File can not be found.'));
-      });
+  native_.call('ExifManager_getExifInfo', {'uri': args.uri}, callback);
 };
 
 ExifManager.prototype.saveExifInfo = function() {
@@ -316,15 +307,7 @@ ExifManager.prototype.getThumbnail = function() {
     }
   };
 
-  tizen.filesystem.resolve(args.uri,
-      function() {
-        native_.call('ExifManager_getThumbnail', {'uri': args.uri}, _callback);
-      },
-      function() {
-        native_.callIfPossible(args.errorCallback, new WebAPIException(
-            WebAPIException.NOT_FOUND_ERR,
-            'File can not be found.'));
-      });
+  native_.call('ExifManager_getThumbnail', {'uri': args.uri}, _callback);
 };
 
 tizen.ExifInformation = function() {
index 8be6a26..6f0101a 100755 (executable)
@@ -26,6 +26,7 @@
 #include "common/logger.h"
 #include "common/platform_result.h"
 #include "common/task-queue.h"
+#include "common/tools.h"
 
 #include "exif/exif_information.h"
 #include "exif/exif_util.h"
@@ -62,6 +63,13 @@ void ExifInstance::ExifManagerGetExifInfo(const picojson::value& args, picojson:
       PlatformResult status(ErrorCode::NO_ERROR);
 
       const std::string &file_path = ExifUtil::convertUriToPath(uri);
+
+      PlatformResult fileAvailability(common::tools::CheckFileAvailability(file_path));
+      if (!fileAvailability) {
+        LogAndReportError(fileAvailability, &response->get<picojson::object>());
+        return;
+      }
+
       LoggerD("file_path = %s", file_path.c_str());
 
       status = GetExifInfo::LoadFromURI(uri, &result);
@@ -128,6 +136,14 @@ void ExifInstance::ExifManagerGetThumbnail(const picojson::value& args,
       JsonValue result = JsonValue(JsonObject());
       JsonObject &result_obj = result.get<JsonObject>();
 
+      PlatformResult fileAvailability(common::tools::CheckFileAvailability(file_path));
+      if (!fileAvailability) {
+        LogAndReportError(fileAvailability, &response->get<picojson::object>());
+        return;
+      }
+
+      LoggerD("file_path = %s", file_path.c_str());
+
       std::string ext = file_path.substr(file_path.find_last_of(".") + 1);
       std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
 
index 0a099a5..90e370f 100644 (file)
@@ -911,10 +911,20 @@ function _systeminfoBatteryListenerCallback(eventObj) {
             var propObj = !listener.isArrayType ?
                     _createProperty(property, eventObj.result.array[0]) :
                         _createPropertyArray(property, eventObj.result);
-            var executeCall = (T_.isUndefined(listener.lowThreshold) ||
-                    (propObj.level <= listener.lowThreshold)) ||
-                    (T_.isUndefined(listener.highThreshold) ||
-                            (propObj.level >= listener.highThreshold));
+            /*
+             * According to documentation, the condition should look like this:
+             *
+             * (T_.isUndefined(listener.lowThreshold) && T_.isUndefined(listener.highThreshold)) ||
+             * (!T_.isUndefined(listener.lowThreshold) && !T_.isUndefined(listener.highThreshold) && (propObj.level <= listener.lowThreshold || propObj.level >= listener.highThreshold)) ||
+             * (!T_.isUndefined(listener.lowThreshold) && (propObj.level <= listener.lowThreshold)) ||
+             * (!T_.isUndefined(listener.highThreshold) && (propObj.level >= listener.highThreshold))
+             *
+             * but it can be optimized like this:
+             */
+            var executeCall = (T_.isUndefined(listener.lowThreshold) && T_.isUndefined(listener.highThreshold)) ||
+                              (!T_.isUndefined(listener.lowThreshold) && propObj.level <= listener.lowThreshold) ||
+                              (!T_.isUndefined(listener.highThreshold) && propObj.level >= listener.highThreshold);
+
             if (executeCall) {
                 listener.callback(propObj);
             }
@@ -932,10 +942,12 @@ function _systeminfoCpuListenerCallback(eventObj) {
             var propObj = !listener.isArrayType ?
                     _createProperty(property, eventObj.result.array[0]) :
                         _createPropertyArray(property, eventObj.result);
-            var executeCall = (T_.isUndefined(listener.lowThreshold) ||
-                    (propObj.load <= listener.lowThreshold)) ||
-                    (T_.isUndefined(listener.highThreshold) ||
-                            (propObj.load >= listener.highThreshold));
+            /*
+             * Optimized condition:
+             * */
+            var executeCall = (T_.isUndefined(listener.lowThreshold) && T_.isUndefined(listener.highThreshold)) ||
+                              (!T_.isUndefined(listener.lowThreshold) && propObj.load <= listener.lowThreshold) ||
+                              (!T_.isUndefined(listener.highThreshold) && propObj.load >= listener.highThreshold);
             if (executeCall) {
                 listener.callback(propObj);
             }
@@ -968,10 +980,12 @@ function _systeminfoDisplayListenerCallback(eventObj) {
             var propObj = !listener.isArrayType ?
                     _createProperty(property, eventObj.result.array[0]) :
                         _createPropertyArray(property, eventObj.result);
-            var executeCall = (T_.isUndefined(listener.lowThreshold) ||
-                    (propObj.brightness <= listener.lowThreshold)) ||
-                    (T_.isUndefined(listener.highThreshold) ||
-                            (propObj.brightness >= listener.highThreshold));
+            /*
+             * Optimized condition:
+             * */
+            var executeCall = (T_.isUndefined(listener.lowThreshold) && T_.isUndefined(listener.highThreshold)) ||
+                              (!T_.isUndefined(listener.lowThreshold) && propObj.brightness <= listener.lowThreshold) ||
+                              (!T_.isUndefined(listener.highThreshold) && propObj.brightness >= listener.highThreshold);
             if (executeCall) {
                 listener.callback(propObj);
             }