[Widget] Added getWidget/getWidgets() implementation.
authorTomasz Marciniak <t.marciniak@samsung.com>
Wed, 2 Mar 2016 08:53:41 +0000 (09:53 +0100)
committerLukasz Bardeli <l.bardeli@samsung.com>
Thu, 14 Apr 2016 09:32:15 +0000 (11:32 +0200)
[Verification] Code compiles without errors.

Change-Id: Iabb7bd05a94db610ace05e1f0ce59fa9c700266c
Signed-off-by: Tomasz Marciniak <t.marciniak@samsung.com>
src/widget/widget.gyp
src/widget/widget_api.js
src/widget/widget_instance.cc
src/widget/widget_instance.h
src/widget/widget_utils.cc [new file with mode: 0644]
src/widget/widget_utils.h [new file with mode: 0644]

index 305fd65da505af27400cd3d98f0eeb90f515b950..ddd48849069b35fcb112ca7ce526459cdfe80a5f 100644 (file)
@@ -15,6 +15,8 @@
         'widget_extension.h',
         'widget_instance.cc',
         'widget_instance.h',
+        'widget_utils.cc',
+        'widget_utils.h',
       ],
       'conditions': [
         ['tizen == 1', {
index bef94fd4caf8d8f3db611553761404ddc086069d..adc38e908a3aeb8219dd5694e3adfa2a955491e7 100755 (executable)
 
 var validator = xwalk.utils.validator;
 var converter = xwalk.utils.converter;
-var type = xwalk.utils.type;
+var types = validator.Types;
 var native = new xwalk.utils.NativeManager(extension);
 
+function createWidgets(e) {
+  var widgets_array = [];
+  var widgets = native.getResultObject(e);
+
+  widgets.forEach(function (data) {
+    widgets_array.push(new Widget(data));
+  });
+
+  return widgets_array;
+};
+
+function Widget(data) {
+  Object.defineProperties(this, {
+    id: {
+      value: data.id,
+      writable: false,
+      enumerable: true
+    },
+    applicationId: {
+      value: data.applicationId,
+      writable: false,
+      enumerable: true
+    },
+    setupApplicationId: {
+      value: data.setupApplicationId ? data.setupApplicationId : null,
+      writable: false,
+      enumerable: true
+    },
+    packageId: {
+      value: data.packageId,
+      writable: false,
+      enumerable: true
+    },
+    noDisplay: {
+      value: data.noDisplay,
+      writable: false,
+      enumerable: true
+    },
+  });
+};
+
 function WidgetManager() {
-}
+};
+
+WidgetManager.prototype.getWidget = function() {
+  var args = validator.validateMethod(arguments, [{
+    name : 'widgetId',
+    type : types.STRING,
+  }]);
+
+  var callArgs = {};
+  callArgs.widgetId = args.widgetId;
+
+  var ret = native.callSync('WidgetManager_getWidget', callArgs);
+
+  if (native.isFailure(ret)) {
+    throw native.getErrorObject(ret);
+  } else {
+    return new Widget(native.getResultObject(ret));
+  }
+};
+
+WidgetManager.prototype.getWidgets = function() {
+  var args = validator.validateMethod(arguments, [{
+    name : 'successCallback',
+    type : types.FUNCTION,
+  }, {
+    name : 'errorCallback',
+    type : types.FUNCTION,
+    optional : true,
+    nullable : true
+  }, {
+    name : 'packageId',
+    type : types.STRING,
+    optional : true,
+    nullable : true
+  }]);
+
+  var callback = function(result) {
+    if (native.isFailure(result)) {
+      native.callIfPossible(args.errorCallback, native.getErrorObject(result));
+    } else {
+      var widgets = createWidgets(result);
+      args.successCallback(widgets);
+    }
+  };
+
+  var callArgs = {};
+  if (args.packageId) {
+    callArgs.packageId = args.packageId;
+  }
+
+  var result = native.call('WidgetManager_getWidgets', callArgs, callback);
+  if (native.isFailure(result)) {
+    throw native.getErrorObject(result);
+  }
+};
 
 //Exports
 exports = new WidgetManager();
index 8ad28c0ed732dd3b06a990906dadb653b0a0c48e..adbdc227b7c879cd35de2856fdc1c73e9b35092d 100755 (executable)
 
 #include "widget/widget_instance.h"
 
+#include <thread>
+
+#include <widget_service.h>
+#include <widget_errno.h>
+
+#include "widget/widget_utils.h"
+
 namespace extension {
 namespace widget {
 
+using common::TizenResult;
+using common::TizenSuccess;
+
 namespace {
 const std::string kPrivilegeWidget = "http://tizen.org/privilege/widget.viewer";
 
+int WidgetListCb(const char* pkgid, const char* widget_id, int is_primary, void* data) {
+  ScopeLogger();
+
+  //is_primary is not supported by native api
+  picojson::array* array = static_cast<picojson::array*>(data);
+
+  if (!array) {
+    LoggerW("User data is null");
+    return WIDGET_ERROR_NONE;
+  }
+
+  picojson::value val = picojson::value(picojson::object());
+
+  auto result = WidgetUtils::WidgetToJson(widget_id, &val.get<picojson::object>(), pkgid);
+  if (result) {
+    array->push_back(val);
+  }
+
+  return WIDGET_ERROR_NONE;
+}
+
+int WidgetListByPkgIdCb(const char* widget_id, int is_primary, void* data) {
+  ScopeLogger();
+
+  //is_primary is not supported by native api
+  picojson::array* array = static_cast<picojson::array*>(data);
+
+  if (!array) {
+    LoggerW("User data is null");
+    return WIDGET_ERROR_NONE;
+  }
+
+  picojson::value val = picojson::value(picojson::object());
+
+  auto result = WidgetUtils::WidgetToJson(widget_id, &val.get<picojson::object>());
+  if (result) {
+    array->push_back(val);
+  }
+
+  return WIDGET_ERROR_NONE;
+}
 }  // namespace
 
 WidgetInstance::WidgetInstance() {
@@ -29,14 +80,16 @@ WidgetInstance::WidgetInstance() {
   using std::placeholders::_1;
   using std::placeholders::_2;
 
-#define REGISTER_SYNC(c,x) \
-  RegisterSyncHandler(c, std::bind(&WidgetInstance::x, this, _1, _2));
+#define REGISTER_SYNC(c, x) \
+  RegisterSyncHandler(c, std::bind(&WidgetInstance::x, this, _1));
 
+  REGISTER_SYNC("WidgetManager_getWidget", GetWidget);
 #undef REGISTER_SYNC
 
 #define REGISTER_ASYNC(c, x) \
   RegisterHandler(c, std::bind(&WidgetInstance::x, this, _1, _2));
 
+  REGISTER_ASYNC("WidgetManager_getWidgets", GetWidgets);
 #undef REGISTER_ASYNC
 }
 
@@ -44,5 +97,64 @@ WidgetInstance::~WidgetInstance() {
   ScopeLogger();
 }
 
+TizenResult WidgetInstance::GetWidget(const picojson::object& args) {
+  ScopeLogger();
+
+  //CHECK_PRIVILEGE_ACCESS(kPrivilegeWidget, &out);
+  CHECK_EXIST(args, kWidgetId, out)
+
+  std::string widget_id = args.find(kWidgetId)->second.get<std::string>();
+
+  picojson::value value {picojson::object{}};
+  auto* obj = &value.get<picojson::object>();
+
+  auto result = WidgetUtils::WidgetToJson(widget_id.c_str(), obj);
+  if (!result) {
+    LogAndReturnTizenError(result, ("GetWidget() failed"));
+  }
+
+  return TizenSuccess(value);
+}
+
+TizenResult WidgetInstance::GetWidgets(const picojson::object& args,
+                                               const common::AsyncToken& token) {
+  ScopeLogger();
+
+  //CHECK_PRIVILEGE_ACCESS(kPrivilegeWidget, &out);
+
+  std::string pkgid;
+  const auto id = args.find(kPackageId);
+  if (args.end() != id) {
+    pkgid = id->second.get<std::string>();
+  }
+
+  auto get_widgets = [this, pkgid](const common::AsyncToken& token) -> void {
+    int ret = WIDGET_ERROR_NONE;
+    picojson::value response{picojson::array{}};
+    auto* array = &response.get<picojson::array>();
+
+    if (pkgid.empty()) {
+      ret = widget_service_get_widget_list(WidgetListCb, array);
+    } else {
+      ret = widget_service_get_widget_list_by_pkgid(pkgid.c_str(), WidgetListByPkgIdCb, array);
+    }
+
+    TizenResult result = TizenSuccess();
+
+    if (WIDGET_ERROR_NONE != ret) {
+      LoggerE("widget_service_get_widget_list() failed");
+      result = WidgetUtils::ConvertErrorCode(ret);
+    } else {
+      result = TizenSuccess{response};
+    }
+
+    this->Post(token, result);
+  };
+
+  std::thread(get_widgets, token).detach();
+
+  return TizenSuccess();
+}
+
 } // namespace widget
 } // namespace extension
index 7f1e3a1f72c4d93fb530ea172fcad1023e1e48ab..71d5a3ef791acb24ac872e81a8c4deee27ce608c 100755 (executable)
@@ -27,6 +27,11 @@ class WidgetInstance : public common::TizenInstance {
   WidgetInstance();
   virtual ~WidgetInstance();
 
+ private:
+  //WidgetManager
+  common::TizenResult GetWidget(picojson::object const& args);
+  common::TizenResult GetWidgets(picojson::object const& args, const common::AsyncToken& token);
+
 };
 
 } // namespace widget
diff --git a/src/widget/widget_utils.cc b/src/widget/widget_utils.cc
new file mode 100644 (file)
index 0000000..eb42511
--- /dev/null
@@ -0,0 +1,115 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+
+#include "widget_utils.h"
+
+#include <widget_service.h>
+#include <widget_errno.h>
+
+namespace extension {
+namespace widget {
+
+const std::string kWidgetId = "widgetId";
+const std::string kPackageId = "packageId";
+const std::string kId = "id";
+const std::string kApplicationId = "applicationId";
+const std::string kSetupApplicationId = "setupApplicationId";
+const std::string kNoDisplay = "noDisplay";
+
+using common::TizenResult;
+using common::TizenSuccess;
+
+TizenResult WidgetUtils::ConvertErrorCode(int error) {
+  switch (error) {
+    case WIDGET_ERROR_NONE:
+      return TizenSuccess();
+    case WIDGET_ERROR_IO_ERROR:
+      return common::IoError(error);
+    case WIDGET_ERROR_INVALID_PARAMETER:
+      return common::InvalidValuesError(error);
+    case WIDGET_ERROR_RESOURCE_BUSY:
+      return common::ServiceNotAvailableError(error);
+    case WIDGET_ERROR_PERMISSION_DENIED:
+      return common::PermissionDeniedError(error);
+    case WIDGET_ERROR_TIMED_OUT:
+      return common::TimeoutError(error);
+    case WIDGET_ERROR_NOT_SUPPORTED:
+    case WIDGET_ERROR_DISABLED:
+      return common::NotSupportedError(error);
+    case WIDGET_ERROR_CANCELED:
+      return common::OperationCanceledError(error);
+    case WIDGET_ERROR_OUT_OF_MEMORY:
+    case WIDGET_ERROR_FILE_NO_SPACE_ON_DEVICE:
+    case WIDGET_ERROR_FAULT:
+    case WIDGET_ERROR_ALREADY_EXIST:
+    case WIDGET_ERROR_ALREADY_STARTED:
+    case WIDGET_ERROR_NOT_EXIST:
+    default:
+      return common::AbortError(error);
+  }
+}
+
+TizenResult WidgetUtils::WidgetToJson(const char* id, picojson::object* out, const char* pkgid) {
+  ScopeLogger();
+
+  //applicationId
+  char* tmp_str = widget_service_get_main_app_id(id);
+  if (!tmp_str) {
+    LogAndReturnTizenError(
+        ConvertErrorCode(get_last_result()), ("widget_service_get_main_app_id() failed"));
+  }
+  out->insert(std::make_pair(kApplicationId, picojson::value(tmp_str)));
+  free(tmp_str);
+
+  //setupApplicationId
+  tmp_str = widget_service_get_app_id_of_setup_app(id);
+  if (!tmp_str) {
+    if (WIDGET_ERROR_NONE != get_last_result()) {
+      LogAndReturnTizenError(
+          ConvertErrorCode(get_last_result()), ("widget_service_get_app_id_of_setup_app() failed"));
+    }
+  } else {
+    out->insert(std::make_pair(kSetupApplicationId, picojson::value(tmp_str)));
+    free(tmp_str);
+  }
+
+  //packageId
+  if (!pkgid) {
+    tmp_str = widget_service_get_package_id(id);
+    if (!tmp_str) {
+      LogAndReturnTizenError(
+          ConvertErrorCode(get_last_result()), ("widget_service_get_package_id() failed"));
+    }
+    out->insert(std::make_pair(kPackageId, picojson::value(tmp_str)));
+    free(tmp_str);
+  }
+
+  //noDisplay
+  bool tmp_bool = widget_service_get_nodisplay(id);
+  if (WIDGET_ERROR_NONE != get_last_result()) {
+    LogAndReturnTizenError(
+        ConvertErrorCode(get_last_result()), ("widget_service_get_nodisplay() failed"));
+  }
+  out->insert(std::make_pair(kNoDisplay, picojson::value(tmp_bool)));
+
+  //id
+  out->insert(std::make_pair(kId, picojson::value(id)));
+
+  return TizenSuccess();
+}
+
+} // widget
+} // extension
diff --git a/src/widget/widget_utils.h b/src/widget/widget_utils.h
new file mode 100644 (file)
index 0000000..4faa713
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+
+#ifndef WEBAPI_PLUGINS_WIDGET_WIDGET_UTILS_H__
+#define WEBAPI_PLUGINS_WIDGET_WIDGET_UTILS_H__
+
+#include <string>
+
+#include "common/tizen_result.h"
+
+namespace extension {
+namespace widget {
+
+#define CHECK_EXIST(args, name, out) \
+  if (args.end() == args.find(name)) { \
+    return common::TypeMismatchError(std::string(name) + " is required argument"); \
+  }
+
+extern const std::string kWidgetId;
+extern const std::string kPackageId;
+
+
+class WidgetUtils {
+ public:
+  static common::TizenResult ConvertErrorCode(int error);
+  static common::TizenResult WidgetToJson(const char* id, picojson::object* out, const char* pkgid = nullptr);
+
+};
+
+} // widget
+} // extension
+
+#endif // WEBAPI_PLUGINS_WIDGET_WIDGET_UTILS_H__