From 1ba3c04634d529c59b17d1b66b75e06f3c5e13d6 Mon Sep 17 00:00:00 2001 From: Tomasz Marciniak Date: Wed, 2 Mar 2016 09:53:41 +0100 Subject: [PATCH] [Widget] Added getWidget/getWidgets() implementation. [Verification] Code compiles without errors. Change-Id: Iabb7bd05a94db610ace05e1f0ce59fa9c700266c Signed-off-by: Tomasz Marciniak --- src/widget/widget.gyp | 2 + src/widget/widget_api.js | 99 ++++++++++++++++++++++++++++- src/widget/widget_instance.cc | 116 +++++++++++++++++++++++++++++++++- src/widget/widget_instance.h | 5 ++ src/widget/widget_utils.cc | 115 +++++++++++++++++++++++++++++++++ src/widget/widget_utils.h | 46 ++++++++++++++ 6 files changed, 379 insertions(+), 4 deletions(-) create mode 100644 src/widget/widget_utils.cc create mode 100644 src/widget/widget_utils.h diff --git a/src/widget/widget.gyp b/src/widget/widget.gyp index 305fd65d..ddd48849 100644 --- a/src/widget/widget.gyp +++ b/src/widget/widget.gyp @@ -15,6 +15,8 @@ 'widget_extension.h', 'widget_instance.cc', 'widget_instance.h', + 'widget_utils.cc', + 'widget_utils.h', ], 'conditions': [ ['tizen == 1', { diff --git a/src/widget/widget_api.js b/src/widget/widget_api.js index bef94fd4..adc38e90 100755 --- a/src/widget/widget_api.js +++ b/src/widget/widget_api.js @@ -16,11 +16,106 @@ 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(); diff --git a/src/widget/widget_instance.cc b/src/widget/widget_instance.cc index 8ad28c0e..adbdc227 100755 --- a/src/widget/widget_instance.cc +++ b/src/widget/widget_instance.cc @@ -16,12 +16,63 @@ #include "widget/widget_instance.h" +#include + +#include +#include + +#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(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(), 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(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()); + 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(); + + picojson::value value {picojson::object{}}; + auto* obj = &value.get(); + + 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(); + } + + auto get_widgets = [this, pkgid](const common::AsyncToken& token) -> void { + int ret = WIDGET_ERROR_NONE; + picojson::value response{picojson::array{}}; + auto* array = &response.get(); + + 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 diff --git a/src/widget/widget_instance.h b/src/widget/widget_instance.h index 7f1e3a1f..71d5a3ef 100755 --- a/src/widget/widget_instance.h +++ b/src/widget/widget_instance.h @@ -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 index 00000000..eb425113 --- /dev/null +++ b/src/widget/widget_utils.cc @@ -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 +#include + +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 index 00000000..4faa7134 --- /dev/null +++ b/src/widget/widget_utils.h @@ -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 + +#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__ -- 2.34.1