From: Tomasz Marciniak Date: Mon, 11 Apr 2016 13:03:27 +0000 (+0200) Subject: [WidgetService] Changed Widget into WidgetService. X-Git-Tag: submit/tizen/20160418.070226^2~2^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=70645bb7214cc956acbbe7e0c930aac492bbde78;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [WidgetService] Changed Widget into WidgetService. [Verification] Code compiles. Change-Id: Ifd388ae5e985af02d0692dc9b926f69e74ac1fa9 Signed-off-by: Tomasz Marciniak --- diff --git a/packaging/webapi-plugins.spec b/packaging/webapi-plugins.spec index e897ae64..6e4de040 100644 --- a/packaging/webapi-plugins.spec +++ b/packaging/webapi-plugins.spec @@ -97,7 +97,7 @@ Source0: %{name}-%{version}.tar.gz %endif %define tizen_feature_time_support 1 %define tizen_feature_web_setting_support 1 -%define tizen_feature_widget_support 0 +%define tizen_feature_widget_service_support 0 %if 0%{?tizen_is_emulator} %define tizen_feature_wi_fi_support 0 %else @@ -194,7 +194,7 @@ Source0: %{name}-%{version}.tar.gz %define tizen_feature_telephony_support 0 %define tizen_feature_time_support 1 %define tizen_feature_web_setting_support 0 -%define tizen_feature_widget_support 0 +%define tizen_feature_widget_service_support 0 %define tizen_feature_wi_fi_support 1 %define tizen_feature_inputdevice_support 1 %define tizen_feature_tvinputdevice_support 0 @@ -259,7 +259,7 @@ Source0: %{name}-%{version}.tar.gz %define tizen_feature_telephony_support 0 %define tizen_feature_time_support 1 %define tizen_feature_web_setting_support 1 -%define tizen_feature_widget_support 0 +%define tizen_feature_widget_service_support 0 %define tizen_feature_wi_fi_support 1 %define tizen_feature_inputdevice_support 0 %define tizen_feature_tvinputdevice_support 1 @@ -500,7 +500,7 @@ GYP_OPTIONS="$GYP_OPTIONS -Dtizen_feature_telephony_support=%{?tizen_feature_tel GYP_OPTIONS="$GYP_OPTIONS -Dtizen_feature_time_support=%{?tizen_feature_time_support}" GYP_OPTIONS="$GYP_OPTIONS -Dtizen_feature_inputdevice_support=%{?tizen_feature_inputdevice_support}" GYP_OPTIONS="$GYP_OPTIONS -Dtizen_feature_web_setting_support=%{?tizen_feature_web_setting_support}" -GYP_OPTIONS="$GYP_OPTIONS -Dtizen_feature_widget_support=%{?tizen_feature_widget_support}" +GYP_OPTIONS="$GYP_OPTIONS -Dtizen_feature_widget_service_support=%{?tizen_feature_widget_service_support}" GYP_OPTIONS="$GYP_OPTIONS -Dtizen_feature_wi_fi_support=%{?tizen_feature_wi_fi_support}" GYP_OPTIONS="$GYP_OPTIONS -Dtizen_feature_tvinputdevice_support=%{?tizen_feature_tvinputdevice_support}" diff --git a/src/tizen-wrt.gyp b/src/tizen-wrt.gyp index 1f1f240c..3505a137 100755 --- a/src/tizen-wrt.gyp +++ b/src/tizen-wrt.gyp @@ -295,9 +295,9 @@ }, ], [ - 'tizen_feature_widget_support==1', { + 'tizen_feature_widget_service_support==1', { 'dependencies': [ - 'widget/widget.gyp:*', + 'widgetservice/widgetservice.gyp:*', ], }, ], diff --git a/src/widget/widget.gyp b/src/widget/widget.gyp deleted file mode 100644 index ddd48849..00000000 --- a/src/widget/widget.gyp +++ /dev/null @@ -1,32 +0,0 @@ -{ - 'includes':[ - '../common/common.gypi', - ], - 'targets': [ - { - 'target_name': 'tizen_widget', - 'type': 'loadable_module', - 'dependencies': [ - '../common/common.gyp:tizen_common', - ], - 'sources': [ - 'widget_api.js', - 'widget_extension.cc', - 'widget_extension.h', - 'widget_instance.cc', - 'widget_instance.h', - 'widget_utils.cc', - 'widget_utils.h', - ], - 'conditions': [ - ['tizen == 1', { - 'variables': { - 'packages': [ - 'widget_service', - ] - }, - }], - ], - }, - ], -} diff --git a/src/widget/widget_api.js b/src/widget/widget_api.js deleted file mode 100755 index f714d287..00000000 --- a/src/widget/widget_api.js +++ /dev/null @@ -1,489 +0,0 @@ -/* - * 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. - */ - -var validator = xwalk.utils.validator; -var converter = xwalk.utils.converter; -var types = validator.Types; -var T = xwalk.utils.type; -var native = new xwalk.utils.NativeManager(extension); - -var WidgetSizeType = { - S_1x1 : '1x1', - S_2x1 : '2x1', - S_2x2 : '2x2', - S_4x1 : '4x1', - S_4x2 : '4x2', - S_4x3 : '4x3', - S_4x4 : '4x4', - S_4x5 : '4x5', - S_4x6 : '4x6', - EASY_1x1 : 'EASY_1x1', - EASY_3x1 : 'EASY_3x1', - EASY_3x3 : 'EASY_3x3', - FULL : 'FULL', -}; - -function createObjects(data, func, widget) { - var array = []; - var objects = native.getResultObject(data); - - objects.forEach(function (d) { - array.push(new func(d, widget)); - }); - - return array; -}; - -function WidgetSize(data) { - Object.defineProperties(this, { - width: { - value: data.width, - writable: false, - enumerable: true - }, - height: { - value: data.height, - writable: false, - enumerable: true - }, - }); -}; - -function WidgetVariant(data) { - Object.defineProperties(this, { - sizeType: { - value: data.sizeType, - writable: false, - enumerable: true - }, - width: { - value: data.width, - writable: false, - enumerable: true - }, - height: { - value: data.height, - writable: false, - enumerable: true - }, - previewImagePath: { - value: data.previewImagePath, - writable: false, - enumerable: true - }, - needsMouseEvents: { - value: data.needsMouseEvents, - writable: false, - enumerable: true - }, - needsTouchEffect: { - value: data.needsTouchEffect, - writable: false, - enumerable: true - }, - needsFrame: { - value: data.needsFrame, - writable: false, - enumerable: true - }, - }); -}; - -function WidgetInstance(data, widget) { - Object.defineProperties(this, { - widget: { - value: widget, - writable: false, - enumerable: true - }, - id: { - value: data.id, - writable: false, - enumerable: true - }, - }); -}; - -WidgetInstance.prototype.changeUpdatePeriod = function() { - var args = validator.validateMethod(arguments, [{ - name : 'period', - type : types.DOUBLE, - }]); - - var callArgs = {}; - callArgs.widgetId = this.widget.id; - callArgs.instanceId = this.id; - callArgs.period = args.period; - - var ret = native.callSync('WidgetInstance_changeUpdatePeriod', callArgs); - - if (native.isFailure(ret)) { - throw native.getErrorObject(ret); - } -}; - -WidgetInstance.prototype.sendContent = function() { - var args = validator.validateMethod(arguments, [{ - name : 'data', - type: types.DICTIONARY, - }, { - name : 'force', - type : types.BOOLEAN, - }]); - - var callArgs = {}; - callArgs.widgetId = this.widget.id; - callArgs.instanceId = this.id; - callArgs.data = args.data; - callArgs.force = args.force; - - var ret = native.callSync('WidgetInstance_sendContent', callArgs); - - if (native.isFailure(ret)) { - throw native.getErrorObject(ret); - } -}; - -WidgetInstance.prototype.getContent = function() { - var args = validator.validateMethod(arguments, [{ - name : 'successCallback', - type : types.FUNCTION, - }, { - name : 'errorCallback', - type : types.FUNCTION, - }]); - - var callArgs = {}; - callArgs.widgetId = this.widget.id; - callArgs.instanceId = this.id; - - var callback = function(result) { - if (native.isFailure(result)) { - args.errorCallback(native.getErrorObject(result)); - } else { - //TODO what is type of returned data - args.successCallback(native.getResultObject(result)); - } - }; - - var result = native.call('WidgetInstance_getContent', callArgs, callback); - if (native.isFailure(result)) { - throw native.getErrorObject(result); - } -}; - -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 - }, - }); -}; - -Widget.prototype.getName = function() { - var args = validator.validateMethod(arguments, [{ - name : 'lang', - type : types.STRING, - optional : true, - nullable : true - }]); - - var callArgs = {}; - callArgs.widgetId = this.id; - - if (args.lang) { - callArgs.lang = args.lang; - } - - var ret = native.callSync('Widget_getName', callArgs); - - if (native.isFailure(ret)) { - throw native.getErrorObject(ret); - } else { - return native.getResultObject(ret); - } -}; - -Widget.prototype.getInstances = function() { - var args = validator.validateMethod(arguments, [{ - name : 'successCallback', - type : types.FUNCTION, - }, { - name : 'errorCallback', - type : types.FUNCTION, - optional : true, - nullable : true - }]); - - var callback = function(result) { - if (native.isFailure(result)) { - native.callIfPossible(args.errorCallback, native.getErrorObject(result)); - } else { - var instances = createObjects(result, WidgetInstance, this); - args.successCallback(instances); - } - }.bind(this); - - var callArgs = {}; - callArgs.widgetId = this.id; - - var result = native.call('Widget_getInstances', callArgs, callback); - if (native.isFailure(result)) { - throw native.getErrorObject(result); - } -}; - -Widget.prototype.getVariant = function() { - var args = validator.validateMethod(arguments, [{ - name : 'sizeType', - type: types.ENUM, - values: T.getValues(WidgetSizeType) - }]); - - var callArgs = {}; - callArgs.widgetId = this.id; - callArgs.sizeType = args.sizeType; - - var ret = native.callSync('Widget_getVariant', callArgs); - - if (native.isFailure(ret)) { - throw native.getErrorObject(ret); - } else { - return new WidgetVariant(native.getResultObject(ret)); - } -}; - -Widget.prototype.getVariants = function() { - var args = validator.validateMethod(arguments, [{ - name : 'successCallback', - type : types.FUNCTION, - }, { - name : 'errorCallback', - type : types.FUNCTION, - optional : true, - nullable : true - }]); - - var callback = function(result) { - if (native.isFailure(result)) { - native.callIfPossible(args.errorCallback, native.getErrorObject(result)); - } else { - var variants = createObjects(result, WidgetVariant); - args.successCallback(variants); - } - }; - - var callArgs = {}; - callArgs.widgetId = this.id; - - var result = native.call('Widget_getVariants', callArgs, callback); - if (native.isFailure(result)) { - throw native.getErrorObject(result); - } -}; - -function ListenerManager(native, listenerName) { - this.listeners = {}; - this.nextId = 1; - this.nativeSet = false; - this.native = native; - this.listenerName = listenerName; -}; - -ListenerManager.prototype.onListenerCalled = function(msg) { - for (var watchId in this.listeners) { - if (this.listeners.hasOwnProperty(watchId) && this.listeners[watchId][msg.action]) { - this.listeners[watchId](this.native.getResultObject(msg)); - } - } -}; - -ListenerManager.prototype.addListener = function(callback) { - var id = this.nextId; - if (!this.nativeSet) { - this.native.addListener(this.listenerName, this.onListenerCalled.bind(this)); - this.nativeSet = true; - } - this.listeners[id] = callback; - ++this.nextId; - return id; -}; - -ListenerManager.prototype.removeListener = function(watchId) { - if (this.listeners[watchId] === null || this.listeners[watchId] === undefined) { - throw new WebAPIException(0, 'Watch id not found.', 'NotFoundError'); - } - - if (this.listeners.hasOwnProperty(watchId)) { - delete this.listeners[watchId]; - } -}; - -var WIDGET_CHANGE_LISTENER = 'WidgetChangeCallback'; -var widgetChangeListener = new ListenerManager(native, WIDGET_CHANGE_LISTENER); - -Widget.prototype.addChangeListener = function() { - var args = validator.validateMethod(arguments, [{ - name : 'eventCallback', - type : types.FUNCTION, - }]); - - var result = native.callSync('Widget_addChangeListener', {widgetId : this.id}); - if (native.isFailure(result)) { - throw native.getErrorObject(result); - } - - var func = function(msg) { - if (msg.widgetId === this.id) { - args.eventCallback(msg.instanceId, msg.event); - } - }.bind(this); - - return widgetChangeListener.addListener(func); -}; - -Widget.prototype.removeChangeListener = function() { - var args = validator.validateMethod(arguments, [{ - name : 'watchId', - type : types.LONG, - }]); - - widgetChangeListener.removeListener(args.watchId); - - var result = native.callSync('Widget_removeChangeListener', {widgetId : this.id}); - if (native.isFailure(result)) { - throw native.getErrorObject(result); - } -}; - -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 = createObjects(result, Widget); - 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); - } -}; - -WidgetManager.prototype.getPrimaryWidgetId = function() { - var args = validator.validateMethod(arguments, [{ - name : 'id', - type : types.STRING, - }]); - - var callArgs = {}; - callArgs.id = args.id; - - var ret = native.callSync('WidgetManager_getPrimaryWidgetId', callArgs); - - if (native.isFailure(ret)) { - throw native.getErrorObject(ret); - } else { - return native.getResultObject(ret); - } -}; - -WidgetManager.prototype.getSize = function() { - var args = validator.validateMethod(arguments, [{ - name : 'sizeType', - type: types.ENUM, - values: T.getValues(WidgetSizeType) - }]); - - var callArgs = {}; - callArgs.sizeType = args.sizeType; - - var ret = native.callSync('WidgetManager_getSize', callArgs); - - if (native.isFailure(ret)) { - throw native.getErrorObject(ret); - } else { - return new WidgetSize(native.getResultObject(ret)); - } -}; - -//Exports -exports = new WidgetManager(); diff --git a/src/widget/widget_extension.cc b/src/widget/widget_extension.cc deleted file mode 100755 index 9381b492..00000000 --- a/src/widget/widget_extension.cc +++ /dev/null @@ -1,37 +0,0 @@ -/* - * 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/widget_extension.h" - -#include "widget/widget_instance.h" - -// This will be generated from widget_api.js -extern const char kSource_widget_api[]; - -common::Extension* CreateExtension() { - return new WidgetExtension; -} - -WidgetExtension::WidgetExtension() { - SetExtensionName("tizen.widget"); - SetJavaScriptAPI(kSource_widget_api); -} - -WidgetExtension::~WidgetExtension() {} - -common::Instance* WidgetExtension::CreateInstance() { - return new extension::widget::WidgetInstance(); -} diff --git a/src/widget/widget_extension.h b/src/widget/widget_extension.h deleted file mode 100755 index 0d9d4610..00000000 --- a/src/widget/widget_extension.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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 WIDGET_WIDGET_EXTENSION_H_ -#define WIDGET_WIDGET_EXTENSION_H_ - -#include "common/extension.h" - -class WidgetExtension : public common::Extension { - public: - WidgetExtension(); - virtual ~WidgetExtension(); - - private: - virtual common::Instance* CreateInstance(); -}; - -#endif // WIDGET_WIDGET_EXTENSION_H_ diff --git a/src/widget/widget_instance.cc b/src/widget/widget_instance.cc deleted file mode 100755 index 120e52d4..00000000 --- a/src/widget/widget_instance.cc +++ /dev/null @@ -1,613 +0,0 @@ -/* - * 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/widget_instance.h" - -#include - -#include -#include -#include -#include - -#include "widget/widget_utils.h" -#include "common/scope_exit.h" - -namespace extension { -namespace widget { - -using common::TizenResult; -using common::TizenSuccess; - -std::mutex WidgetInstance::listener_mutex_; - -namespace { -const common::ListenerToken kWidgetChangeCallbackToken{"WidgetChangeCallback"}; - -const std::string kPrivilegeWidget = "http://tizen.org/privilege/widget.viewer"; - -const std::string kLang = "lang"; -const std::string kInstanceId = "instanceId"; -const std::string kPeriod = "period"; -const std::string kForce = "force"; -const std::string kData = "data"; -const std::string kEvent = "event"; - -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; -} - -int WidgetInstanceCb(const char* widget_id, const char* instance_id, void* data) { - ScopeLogger(); - - picojson::array* array = static_cast(data); - - if (!array) { - LoggerW("User data is null"); - return WIDGET_ERROR_NONE; - } - - array->push_back(picojson::value(instance_id)); - - return WIDGET_ERROR_NONE; -} - -int WidgetLifecycleCb(const char* widget_id, widget_lifecycle_event_e lifecycle_event, - const char* widget_instance_id, void* data) { - ScopeLogger(); - - //WIDGET_LIFE_CYCLE_EVENT_MAX event is not supported - if (WIDGET_LIFE_CYCLE_EVENT_RESUME < lifecycle_event) { - LoggerW("Unknown event type"); - return WIDGET_ERROR_NONE; - } - - WidgetInstance* instance = static_cast(data); - - if (!instance) { - LoggerW("User data is null"); - return WIDGET_ERROR_NONE; - } - - picojson::value response = picojson::value(picojson::object()); - auto& obj = response.get(); - - obj.insert(std::make_pair(kWidgetId, picojson::value(widget_id))); - obj.insert(std::make_pair(kInstanceId, picojson::value(widget_instance_id))); - obj.insert(std::make_pair(kEvent, picojson::value(WidgetUtils::FromEventType(lifecycle_event)))); - - instance->CallWidgetLifecycleListener(widget_id, response); - - return WIDGET_ERROR_NONE; -} - -} // namespace - -WidgetInstance::WidgetInstance() { - ScopeLogger(); - using std::placeholders::_1; - using std::placeholders::_2; - -#define REGISTER_SYNC(c, x) \ - RegisterSyncHandler(c, std::bind(&WidgetInstance::x, this, _1)); - - REGISTER_SYNC("WidgetManager_getWidget", GetWidget); - REGISTER_SYNC("WidgetManager_getPrimaryWidgetId", GetPrimaryWidgetId); - REGISTER_SYNC("WidgetManager_getSize", GetSize); - REGISTER_SYNC("Widget_getName", GetName); - REGISTER_SYNC("Widget_getVariant", GetVariant); - REGISTER_SYNC("Widget_addChangeListener", AddChangeListener); - REGISTER_SYNC("Widget_removeChangeListener", RemoveChangeListener); - REGISTER_SYNC("WidgetInstance_changeUpdatePeriod", ChangeUpdatePeriod); - REGISTER_SYNC("WidgetInstance_sendContent", SendContent); - -#undef REGISTER_SYNC - -#define REGISTER_ASYNC(c, x) \ - RegisterHandler(c, std::bind(&WidgetInstance::x, this, _1, _2)); - - REGISTER_ASYNC("WidgetManager_getWidgets", GetWidgets); - REGISTER_ASYNC("Widget_getInstances", GetInstances); - REGISTER_ASYNC("Widget_getVariants", GetVariants); - REGISTER_ASYNC("WidgetInstance_getContent", GetContent); -#undef REGISTER_ASYNC -} - -WidgetInstance::~WidgetInstance() { - ScopeLogger(); - - std::lock_guard lock(listener_mutex_); - for (auto& it : listener_map_) { - int ret = widget_service_unset_lifecycle_event_cb(it.first.c_str(), nullptr); - if (WIDGET_ERROR_NONE != ret) { - LoggerE("widget_service_unset_lifecycle_event_cb() failed"); - } - } - - listener_map_.clear(); -} - -TizenResult WidgetInstance::GetWidget(const picojson::object& args) { - ScopeLogger(); - - //CHECK_PRIVILEGE_ACCESS(kPrivilegeWidget, &out); - CHECK_EXIST(args, kWidgetId, out) - - const auto& 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(); -} - -TizenResult WidgetInstance::GetPrimaryWidgetId(const picojson::object& args) { - ScopeLogger(); - - //CHECK_PRIVILEGE_ACCESS(kPrivilegeWidget, &out); - CHECK_EXIST(args, kId, out) - - const auto& id = args.find(kId)->second.get(); - - char* widget_id = widget_service_get_widget_id(id.c_str()); - if (!widget_id) { - LogAndReturnTizenError( - WidgetUtils::ConvertErrorCode(get_last_result()), ("widget_service_get_widget_id() failed")); - } - - SCOPE_EXIT { - free(widget_id); - }; - - return TizenSuccess(picojson::value(widget_id)); -} - -TizenResult WidgetInstance::GetSize(const picojson::object& args) { - ScopeLogger(); - - CHECK_EXIST(args, kSizeType, out) - - widget_size_type_e type = WidgetUtils::ToSizeType(args.find(kSizeType)->second.get()); - if (WIDGET_SIZE_TYPE_UNKNOWN == type) { - LogAndReturnTizenError(common::InvalidValuesError(), ("incorrect size type")); - } - - picojson::value value{picojson::object{}}; - auto* obj = &value.get(); - - auto result = WidgetUtils::SizeToJson(type, obj); - if (!result) { - LogAndReturnTizenError(result, ("GetSize() failed")); - } - - return TizenSuccess(value); -} - -TizenResult WidgetInstance::GetName(picojson::object const& args) { - ScopeLogger(); - - //CHECK_PRIVILEGE_ACCESS(kPrivilegeWidget, &out); - CHECK_EXIST(args, kWidgetId, out) - - const auto& widget_id = args.find(kWidgetId)->second.get(); - char* lang = nullptr; - - const auto lang_it = args.find(kLang); - if (args.end() != lang_it) { - lang = const_cast(lang_it->second.get().c_str()); - } - - char* name = widget_service_get_name(widget_id.c_str(), lang); - if (!name) { - LogAndReturnTizenError( - WidgetUtils::ConvertErrorCode(get_last_result()), ("widget_service_get_name() failed")); - } - - SCOPE_EXIT { - free(name); - }; - - return TizenSuccess(picojson::value(name)); -} - -TizenResult WidgetInstance::GetInstances(picojson::object const& args, const common::AsyncToken& token) { - ScopeLogger(); - - CHECK_EXIST(args, kWidgetId, out) - - const auto& widget_id = args.find(kWidgetId)->second.get(); - - auto get_instances = [this, widget_id](const common::AsyncToken& token) -> void { - picojson::value response{picojson::array{}}; - auto* array = &response.get(); - - int ret = widget_service_get_widget_instance_list(widget_id.c_str(), WidgetInstanceCb, array); - - TizenResult result = TizenSuccess(); - - if (WIDGET_ERROR_NONE != ret) { - LoggerE("widget_service_get_widget_instance_list() failed"); - result = WidgetUtils::ConvertErrorCode(ret); - } else { - result = TizenSuccess{response}; - } - - this->Post(token, result); - }; - - std::thread(get_instances, token).detach(); - - return TizenSuccess(); -} - -TizenResult WidgetInstance::GetVariant(picojson::object const& args) { - ScopeLogger(); - - CHECK_EXIST(args, kWidgetId, out) - CHECK_EXIST(args, kSizeType, out) - - const auto& widget_id = args.find(kWidgetId)->second.get(); - const auto& type = args.find(kSizeType)->second.get(); - - widget_size_type_e size_type = WidgetUtils::ToSizeType(type); - if (WIDGET_SIZE_TYPE_UNKNOWN == size_type) { - LogAndReturnTizenError(common::InvalidValuesError(), ("incorrect size type")); - } - - picojson::value value{picojson::object{}}; - auto* obj = &value.get(); - - auto result = WidgetUtils::SizeToJson(size_type, obj); - if (!result) { - LogAndReturnTizenError(result, ("GetVariant() failed")); - } - - result = WidgetUtils::WidgetVariantToJson(widget_id.c_str(), size_type, obj); - if (!result) { - LogAndReturnTizenError(result, ("GetVariant() failed")); - } - - //sizeType - obj->insert(std::make_pair(kSizeType, picojson::value(type))); - - return TizenSuccess(value); -} - -TizenResult WidgetInstance::GetVariants(picojson::object const& args, const common::AsyncToken& token) { - ScopeLogger(); - - //CHECK_PRIVILEGE_ACCESS(kPrivilegeWidget, &out); - CHECK_EXIST(args, kWidgetId, out) - - const auto& widget_id = args.find(kWidgetId)->second.get(); - - auto get_variants = [this, widget_id](const common::AsyncToken& token) -> void { - int count = 0; - int* type_array = nullptr; - int ret = widget_service_get_supported_size_types(widget_id.c_str(), &count, &type_array); - - if (WIDGET_ERROR_NONE != ret) { - LoggerE("widget_service_get_supported_size_types() failed"); - this->Post(token, WidgetUtils::ConvertErrorCode(ret)); - return; - } - - //it is not mentioned in header file if array should be freed by caller - //but in widget_service_get_supported_size_types definition it is allocated - //so it should be released when it is not needed anymore - SCOPE_EXIT { - free(type_array); - }; - - TizenResult result = TizenSuccess(); - picojson::value response{picojson::array{}}; - auto& array = response.get(); - - for (int i = 0; i < count; i++) { - picojson::value val = picojson::value(picojson::object()); - picojson::object* obj = &val.get(); - - widget_size_type_e size_type = static_cast(type_array[i]); - result = WidgetUtils::SizeToJson(size_type, obj); - if (!result) { - break; - } - - result = WidgetUtils::WidgetVariantToJson(widget_id.c_str(), size_type, obj); - if (!result) { - break; - } - - obj->insert(std::make_pair(kSizeType, picojson::value(WidgetUtils::FromSizeType(size_type)))); - array.push_back(val); - } - - if (!result) { - this->Post(token, result); - } else { - this->Post(token, TizenSuccess{response}); - } - }; - - std::thread(get_variants, token).detach(); - - return TizenSuccess(); -} - -void WidgetInstance::CallWidgetLifecycleListener(const std::string& widget_id, - const picojson::value& response) { - ScopeLogger(); - - std::lock_guard lock(listener_mutex_); - const auto it = listener_map_.find(widget_id); - if (listener_map_.end() != it) { - Post(kWidgetChangeCallbackToken, TizenSuccess{response}); - return; - } - - LoggerW("widget id was not found."); -} - -TizenResult WidgetInstance::AddChangeListener(picojson::object const& args) { - ScopeLogger(); - - //CHECK_PRIVILEGE_ACCESS(kPrivilegeWidget, &out); - CHECK_EXIST(args, kWidgetId, out) - - const auto& widget_id = args.find(kWidgetId)->second.get(); - - std::lock_guard lock(listener_mutex_); - auto it = listener_map_.find(widget_id); - if (listener_map_.end() != it) { - it->second++; - return TizenSuccess(); - } - - int ret = widget_service_set_lifecycle_event_cb(widget_id.c_str(), WidgetLifecycleCb , this); - if (WIDGET_ERROR_NONE != ret) { - LogAndReturnTizenError( - WidgetUtils::ConvertErrorCode(ret), ("widget_service_set_lifecycle_event_cb() failed")); - } - - listener_map_[widget_id]++; - - return TizenSuccess(); -} - -TizenResult WidgetInstance::RemoveChangeListener(picojson::object const& args) { - ScopeLogger(); - - CHECK_EXIST(args, kWidgetId, out) - - const auto& widget_id = args.find(kWidgetId)->second.get(); - - std::lock_guard lock(listener_mutex_); - auto it = listener_map_.find(widget_id); - if (listener_map_.end() == it) { - LoggerW("Listener id not found"); - return TizenSuccess(); - } - - if (!(--it->second)) { - int ret = widget_service_unset_lifecycle_event_cb(widget_id.c_str(), nullptr); - if (WIDGET_ERROR_NONE != ret) { - LogAndReturnTizenError( - WidgetUtils::ConvertErrorCode(ret), ("widget_service_unset_lifecycle_event_cb() failed")); - } - listener_map_.erase(it); - } - - return TizenSuccess(); -} - -TizenResult WidgetInstance::ChangeUpdatePeriod(picojson::object const& args) { - ScopeLogger(); - - CHECK_EXIST(args, kWidgetId, out) - CHECK_EXIST(args, kInstanceId, out) - CHECK_EXIST(args, kPeriod, out) - - const auto& widget_id = args.find(kWidgetId)->second.get(); - const auto& instance_id = args.find(kInstanceId)->second.get(); - const double period = args.find(kPeriod)->second.get(); - - int ret = widget_service_change_period(widget_id.c_str(), instance_id.c_str(), period); - - if (WIDGET_ERROR_NONE != ret) { - LogAndReturnTizenError( - WidgetUtils::ConvertErrorCode(ret), ("widget_service_change_period() failed")); - } - - return TizenSuccess(); -} - -TizenResult WidgetInstance::SendContent(picojson::object const& args) { - ScopeLogger(); - - CHECK_EXIST(args, kWidgetId, out) - CHECK_EXIST(args, kInstanceId, out) - CHECK_EXIST(args, kData, out) - CHECK_EXIST(args, kForce, out) - - const auto& widget_id = args.find(kWidgetId)->second.get(); - const auto& instance_id = args.find(kInstanceId)->second.get(); - const int force = args.find(kForce)->second.get() ? 1 : 0; - - bundle* data = bundle_create(); - int ret = get_last_result(); - if (BUNDLE_ERROR_NONE != ret) { - LogAndReturnTizenError(common::AbortError(ret), ("bundle_create() failed")); - } - - SCOPE_EXIT { - bundle_free(data); - }; - - ret = bundle_add(data, kData.c_str(), args.find(kData)->second.serialize().c_str()); - if (BUNDLE_ERROR_NONE != ret) { - LogAndReturnTizenError(common::AbortError(ret), ("bundle_add() failed")); - } - - ret = widget_service_trigger_update(widget_id.c_str(), instance_id.c_str(), data, force); - if (WIDGET_ERROR_NONE != ret) { - LogAndReturnTizenError( - WidgetUtils::ConvertErrorCode(ret), ("widget_service_trigger_update() failed")); - } - - return TizenSuccess(); -} - -TizenResult WidgetInstance::GetContent(picojson::object const& args, const common::AsyncToken& token) { - ScopeLogger(); - - CHECK_EXIST(args, kWidgetId, out) - CHECK_EXIST(args, kInstanceId, out) - - const auto& widget_id = args.find(kWidgetId)->second.get(); - const auto& instance_id = args.find(kInstanceId)->second.get(); - - auto get_content = [this, widget_id, instance_id](const common::AsyncToken& token) -> void { - bundle* bundle_data = bundle_create(); - - int ret = get_last_result(); - if (BUNDLE_ERROR_NONE != ret) { - LoggerE("bundle_create() failed"); - this->Post(token, common::AbortError(ret)); - return; - } - - SCOPE_EXIT { - bundle_free(bundle_data); - }; - - ret = widget_service_get_content_of_widget_instance(widget_id.c_str(), - instance_id.c_str(), &bundle_data); - if (WIDGET_ERROR_NONE != ret) { - LoggerE("widget_service_get_content_of_widget_instance() failed"); - this->Post(token, WidgetUtils::ConvertErrorCode(ret)); - return; - } - - char* data_str = nullptr; - ret = bundle_get_str(bundle_data, kData.c_str(), &data_str); - if (BUNDLE_ERROR_NONE != ret) { - LoggerE("bundle_get_str() failed"); - this->Post(token, common::AbortError(ret)); - return; - } - - picojson::value response; - std::string err; - picojson::parse(response, data_str, data_str + strlen(data_str), &err); - if (!err.empty()) { - LoggerE("Failed to parse bundle data() failed [%s]", err.c_str()); - this->Post(token, common::AbortError()); - return; - } - - this->Post(token, TizenSuccess{response}); - }; - - std::thread(get_content, token).detach(); - - return TizenSuccess(); -} - -} // namespace widget -} // namespace extension diff --git a/src/widget/widget_instance.h b/src/widget/widget_instance.h deleted file mode 100755 index 313421cd..00000000 --- a/src/widget/widget_instance.h +++ /dev/null @@ -1,58 +0,0 @@ -/* - * 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 WIDGET_WIDGET_INSTANCE_H_ -#define WIDGET_WIDGET_INSTANCE_H_ - -#include -#include - -#include "common/tizen_instance.h" - -namespace extension { -namespace widget { - -class WidgetInstance : public common::TizenInstance { - public: - WidgetInstance(); - virtual ~WidgetInstance(); - void CallWidgetLifecycleListener(const std::string& widget_id, const picojson::value& response); - private: - //WidgetManager - common::TizenResult GetWidget(picojson::object const& args); - common::TizenResult GetWidgets(picojson::object const& args, const common::AsyncToken& token); - common::TizenResult GetPrimaryWidgetId(picojson::object const& args); - common::TizenResult GetSize(picojson::object const& args); - //Widget - common::TizenResult GetName(picojson::object const& args); - common::TizenResult GetInstances(picojson::object const& args, const common::AsyncToken& token); - common::TizenResult GetVariant(picojson::object const& args); - common::TizenResult GetVariants(picojson::object const& args, const common::AsyncToken& token); - common::TizenResult AddChangeListener(picojson::object const& args); - common::TizenResult RemoveChangeListener(picojson::object const& args); - //WidgetInstance - common::TizenResult ChangeUpdatePeriod(picojson::object const& args); - common::TizenResult SendContent(picojson::object const& args); - common::TizenResult GetContent(picojson::object const& args, const common::AsyncToken& token); - - static std::mutex listener_mutex_; - std::map listener_map_; -}; - -} // namespace widget -} // namespace extension - -#endif // WIDGET_WIDGET_INSTANCE_H_ diff --git a/src/widget/widget_utils.cc b/src/widget/widget_utils.cc deleted file mode 100644 index 41de85c9..00000000 --- a/src/widget/widget_utils.cc +++ /dev/null @@ -1,246 +0,0 @@ -/* - * 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 - -namespace extension { -namespace widget { - -namespace { - -#define WIDGET_SIZE_TYPE_E \ - X(WIDGET_SIZE_TYPE_1x1, "1x1") \ - X(WIDGET_SIZE_TYPE_2x1, "2x1") \ - X(WIDGET_SIZE_TYPE_2x2, "2x2") \ - X(WIDGET_SIZE_TYPE_4x1, "4x1") \ - X(WIDGET_SIZE_TYPE_4x2, "4x2") \ - X(WIDGET_SIZE_TYPE_4x3, "4x3") \ - X(WIDGET_SIZE_TYPE_4x4, "4x4") \ - X(WIDGET_SIZE_TYPE_4x5, "4x5") \ - X(WIDGET_SIZE_TYPE_4x6, "4x6") \ - X(WIDGET_SIZE_TYPE_EASY_1x1, "EASY_1x1") \ - X(WIDGET_SIZE_TYPE_EASY_3x1, "EASY_3x1") \ - X(WIDGET_SIZE_TYPE_EASY_3x3, "EASY_3x3") \ - X(WIDGET_SIZE_TYPE_FULL, "FULL") \ - XD(WIDGET_SIZE_TYPE_UNKNOWN, "unknown") - -#define WIDGET_LIFECYCLE_EVENT_E \ - X(WIDGET_LIFE_CYCLE_EVENT_CREATE, "CREATE") \ - X(WIDGET_LIFE_CYCLE_EVENT_DESTROY, "DESTROY") \ - X(WIDGET_LIFE_CYCLE_EVENT_PAUSE, "PAUSE") \ - X(WIDGET_LIFE_CYCLE_EVENT_RESUME, "RESUME") \ - XD(WIDGET_LIFE_CYCLE_EVENT_MAX, "unknown") - -} // namespace - -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"; -const std::string kSizeType = "sizeType"; -const std::string kWidth = "width"; -const std::string kHeight = "height"; -const std::string kNeedsMouseEvents = "needsMouseEvents"; -const std::string kNeedsTouchEffect = "needsTouchEffect"; -const std::string kNeedsFrame = "needsFrame"; -const std::string kPreviewImagePath = "previewImagePath"; - -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(); -} - -TizenResult WidgetUtils::SizeToJson(widget_size_type_e type, picojson::object* out) { - ScopeLogger(); - - int width = 0; - int height = 0; - - int ret = widget_service_get_size(type, &width, &height); - if (WIDGET_ERROR_NONE != ret) { - LogAndReturnTizenError(ConvertErrorCode(ret), ("widget_service_get_size() failed")); - } - - out->insert(std::make_pair(kWidth, picojson::value(static_cast(width)))); - out->insert(std::make_pair(kHeight, picojson::value(static_cast(height)))); - - return TizenSuccess(); -} - -TizenResult WidgetUtils::WidgetVariantToJson( - const char* id, widget_size_type_e type, picojson::object* out) { - ScopeLogger(); - - bool tmp = false; - - //needsMouseEvents - int ret = widget_service_get_need_of_mouse_event(id, type, &tmp); - if (WIDGET_ERROR_NONE != ret) { - LogAndReturnTizenError( - ConvertErrorCode(ret), ("widget_service_get_need_of_mouse_event() failed")); - } - out->insert(std::make_pair(kNeedsMouseEvents, picojson::value(tmp))); - - //needsTouchEffect - ret = widget_service_get_need_of_touch_effect(id, type, &tmp); - if (WIDGET_ERROR_NONE != ret) { - LogAndReturnTizenError( - ConvertErrorCode(ret), ("widget_service_get_need_of_touch_effect() failed")); - } - out->insert(std::make_pair(kNeedsTouchEffect, picojson::value(tmp))); - - //needsFrame - ret = widget_service_get_need_of_frame(id, type, &tmp); - if (WIDGET_ERROR_NONE != ret) { - LogAndReturnTizenError( - ConvertErrorCode(ret), ("widget_service_get_need_of_frame() failed")); - } - out->insert(std::make_pair(kNeedsFrame, picojson::value(tmp))); - - //previewImagePath - char* path = widget_service_get_preview_image_path(id, type); - if (!path) { - LogAndReturnTizenError( - ConvertErrorCode(get_last_result()), ("widget_service_get_preview_image_path() failed")); - } - out->insert(std::make_pair(kPreviewImagePath, picojson::value(path))); - free(path); - - return TizenSuccess(); -} - -#define X(v, s) case v: return s; -#define XD(v, s) \ - default: \ - LoggerE("Unknown value: %d, returning default: %s", e, s); \ - return s; - -std::string WidgetUtils::FromSizeType(widget_size_type_e e) { - ScopeLogger(); - - switch (e) { - WIDGET_SIZE_TYPE_E - } -} - -std::string WidgetUtils::FromEventType(widget_lifecycle_event_e e) { - ScopeLogger(); - - switch (e) { - WIDGET_LIFECYCLE_EVENT_E - } -} - -#undef X -#undef XD - -#define X(v, s) if (e == s) return v; -#define XD(v, s) \ - LoggerE("Unknown value: %s, returning default: %d", e.c_str(), v); \ - return v; - -widget_size_type_e WidgetUtils::ToSizeType(const std::string& e) { - ScopeLogger(); - - WIDGET_SIZE_TYPE_E -} - -#undef X -#undef XD - -} // widget -} // extension diff --git a/src/widget/widget_utils.h b/src/widget/widget_utils.h deleted file mode 100644 index a82eedeb..00000000 --- a/src/widget/widget_utils.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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 - -#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; -extern const std::string kId; -extern const std::string kSizeType; -extern const std::string kWidth; -extern const std::string kHeight; - -class WidgetUtils { - public: - static widget_size_type_e ToSizeType(const std::string& e); - static std::string FromSizeType(widget_size_type_e e); - static std::string FromEventType(widget_lifecycle_event_e e); - static common::TizenResult ConvertErrorCode(int error); - static common::TizenResult WidgetToJson(const char* id, picojson::object* out, const char* pkgid = nullptr); - static common::TizenResult SizeToJson(widget_size_type_e type, picojson::object* out); - static common::TizenResult WidgetVariantToJson(const char* id, widget_size_type_e type, picojson::object* out); - -}; - -} // widget -} // extension - -#endif // WEBAPI_PLUGINS_WIDGET_WIDGET_UTILS_H__ diff --git a/src/widgetservice/widgetservice.gyp b/src/widgetservice/widgetservice.gyp new file mode 100644 index 00000000..371f8eb2 --- /dev/null +++ b/src/widgetservice/widgetservice.gyp @@ -0,0 +1,32 @@ +{ + 'includes':[ + '../common/common.gypi', + ], + 'targets': [ + { + 'target_name': 'tizen_widgetservice', + 'type': 'loadable_module', + 'dependencies': [ + '../common/common.gyp:tizen_common', + ], + 'sources': [ + 'widgetservice_api.js', + 'widgetservice_extension.cc', + 'widgetservice_extension.h', + 'widgetservice_instance.cc', + 'widgetservice_instance.h', + 'widgetservice_utils.cc', + 'widgetservice_utils.h', + ], + 'conditions': [ + ['tizen == 1', { + 'variables': { + 'packages': [ + 'widget_service', + ] + }, + }], + ], + }, + ], +} diff --git a/src/widgetservice/widgetservice_api.js b/src/widgetservice/widgetservice_api.js new file mode 100755 index 00000000..9435b505 --- /dev/null +++ b/src/widgetservice/widgetservice_api.js @@ -0,0 +1,489 @@ +/* + * 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. + */ + +var validator = xwalk.utils.validator; +var converter = xwalk.utils.converter; +var types = validator.Types; +var T = xwalk.utils.type; +var native = new xwalk.utils.NativeManager(extension); + +var WidgetSizeType = { + S_1x1 : '1x1', + S_2x1 : '2x1', + S_2x2 : '2x2', + S_4x1 : '4x1', + S_4x2 : '4x2', + S_4x3 : '4x3', + S_4x4 : '4x4', + S_4x5 : '4x5', + S_4x6 : '4x6', + EASY_1x1 : 'EASY_1x1', + EASY_3x1 : 'EASY_3x1', + EASY_3x3 : 'EASY_3x3', + FULL : 'FULL', +}; + +function createObjects(data, func, widget) { + var array = []; + var objects = native.getResultObject(data); + + objects.forEach(function (d) { + array.push(new func(d, widget)); + }); + + return array; +}; + +function WidgetSize(data) { + Object.defineProperties(this, { + width: { + value: data.width, + writable: false, + enumerable: true + }, + height: { + value: data.height, + writable: false, + enumerable: true + }, + }); +}; + +function WidgetVariant(data) { + Object.defineProperties(this, { + sizeType: { + value: data.sizeType, + writable: false, + enumerable: true + }, + width: { + value: data.width, + writable: false, + enumerable: true + }, + height: { + value: data.height, + writable: false, + enumerable: true + }, + previewImagePath: { + value: data.previewImagePath, + writable: false, + enumerable: true + }, + needsMouseEvents: { + value: data.needsMouseEvents, + writable: false, + enumerable: true + }, + needsTouchEffect: { + value: data.needsTouchEffect, + writable: false, + enumerable: true + }, + needsFrame: { + value: data.needsFrame, + writable: false, + enumerable: true + }, + }); +}; + +function WidgetInstance(data, widget) { + Object.defineProperties(this, { + widget: { + value: widget, + writable: false, + enumerable: true + }, + id: { + value: data.id, + writable: false, + enumerable: true + }, + }); +}; + +WidgetInstance.prototype.changeUpdatePeriod = function() { + var args = validator.validateMethod(arguments, [{ + name : 'period', + type : types.DOUBLE, + }]); + + var callArgs = {}; + callArgs.widgetId = this.widget.id; + callArgs.instanceId = this.id; + callArgs.period = args.period; + + var ret = native.callSync('WidgetInstance_changeUpdatePeriod', callArgs); + + if (native.isFailure(ret)) { + throw native.getErrorObject(ret); + } +}; + +WidgetInstance.prototype.sendContent = function() { + var args = validator.validateMethod(arguments, [{ + name : 'data', + type: types.DICTIONARY, + }, { + name : 'force', + type : types.BOOLEAN, + }]); + + var callArgs = {}; + callArgs.widgetId = this.widget.id; + callArgs.instanceId = this.id; + callArgs.data = args.data; + callArgs.force = args.force; + + var ret = native.callSync('WidgetInstance_sendContent', callArgs); + + if (native.isFailure(ret)) { + throw native.getErrorObject(ret); + } +}; + +WidgetInstance.prototype.getContent = function() { + var args = validator.validateMethod(arguments, [{ + name : 'successCallback', + type : types.FUNCTION, + }, { + name : 'errorCallback', + type : types.FUNCTION, + }]); + + var callArgs = {}; + callArgs.widgetId = this.widget.id; + callArgs.instanceId = this.id; + + var callback = function(result) { + if (native.isFailure(result)) { + args.errorCallback(native.getErrorObject(result)); + } else { + //TODO what is type of returned data + args.successCallback(native.getResultObject(result)); + } + }; + + var result = native.call('WidgetInstance_getContent', callArgs, callback); + if (native.isFailure(result)) { + throw native.getErrorObject(result); + } +}; + +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 + }, + }); +}; + +Widget.prototype.getName = function() { + var args = validator.validateMethod(arguments, [{ + name : 'lang', + type : types.STRING, + optional : true, + nullable : true + }]); + + var callArgs = {}; + callArgs.widgetId = this.id; + + if (args.lang) { + callArgs.lang = args.lang; + } + + var ret = native.callSync('Widget_getName', callArgs); + + if (native.isFailure(ret)) { + throw native.getErrorObject(ret); + } else { + return native.getResultObject(ret); + } +}; + +Widget.prototype.getInstances = function() { + var args = validator.validateMethod(arguments, [{ + name : 'successCallback', + type : types.FUNCTION, + }, { + name : 'errorCallback', + type : types.FUNCTION, + optional : true, + nullable : true + }]); + + var callback = function(result) { + if (native.isFailure(result)) { + native.callIfPossible(args.errorCallback, native.getErrorObject(result)); + } else { + var instances = createObjects(result, WidgetInstance, this); + args.successCallback(instances); + } + }.bind(this); + + var callArgs = {}; + callArgs.widgetId = this.id; + + var result = native.call('Widget_getInstances', callArgs, callback); + if (native.isFailure(result)) { + throw native.getErrorObject(result); + } +}; + +Widget.prototype.getVariant = function() { + var args = validator.validateMethod(arguments, [{ + name : 'sizeType', + type: types.ENUM, + values: T.getValues(WidgetSizeType) + }]); + + var callArgs = {}; + callArgs.widgetId = this.id; + callArgs.sizeType = args.sizeType; + + var ret = native.callSync('Widget_getVariant', callArgs); + + if (native.isFailure(ret)) { + throw native.getErrorObject(ret); + } else { + return new WidgetVariant(native.getResultObject(ret)); + } +}; + +Widget.prototype.getVariants = function() { + var args = validator.validateMethod(arguments, [{ + name : 'successCallback', + type : types.FUNCTION, + }, { + name : 'errorCallback', + type : types.FUNCTION, + optional : true, + nullable : true + }]); + + var callback = function(result) { + if (native.isFailure(result)) { + native.callIfPossible(args.errorCallback, native.getErrorObject(result)); + } else { + var variants = createObjects(result, WidgetVariant); + args.successCallback(variants); + } + }; + + var callArgs = {}; + callArgs.widgetId = this.id; + + var result = native.call('Widget_getVariants', callArgs, callback); + if (native.isFailure(result)) { + throw native.getErrorObject(result); + } +}; + +function ListenerManager(native, listenerName) { + this.listeners = {}; + this.nextId = 1; + this.nativeSet = false; + this.native = native; + this.listenerName = listenerName; +}; + +ListenerManager.prototype.onListenerCalled = function(msg) { + for (var watchId in this.listeners) { + if (this.listeners.hasOwnProperty(watchId) && this.listeners[watchId][msg.action]) { + this.listeners[watchId](this.native.getResultObject(msg)); + } + } +}; + +ListenerManager.prototype.addListener = function(callback) { + var id = this.nextId; + if (!this.nativeSet) { + this.native.addListener(this.listenerName, this.onListenerCalled.bind(this)); + this.nativeSet = true; + } + this.listeners[id] = callback; + ++this.nextId; + return id; +}; + +ListenerManager.prototype.removeListener = function(watchId) { + if (this.listeners[watchId] === null || this.listeners[watchId] === undefined) { + throw new WebAPIException(0, 'Watch id not found.', 'NotFoundError'); + } + + if (this.listeners.hasOwnProperty(watchId)) { + delete this.listeners[watchId]; + } +}; + +var WIDGET_CHANGE_LISTENER = 'WidgetChangeCallback'; +var widgetChangeListener = new ListenerManager(native, WIDGET_CHANGE_LISTENER); + +Widget.prototype.addChangeListener = function() { + var args = validator.validateMethod(arguments, [{ + name : 'eventCallback', + type : types.FUNCTION, + }]); + + var result = native.callSync('Widget_addChangeListener', {widgetId : this.id}); + if (native.isFailure(result)) { + throw native.getErrorObject(result); + } + + var func = function(msg) { + if (msg.widgetId === this.id) { + args.eventCallback(msg.instanceId, msg.event); + } + }.bind(this); + + return widgetChangeListener.addListener(func); +}; + +Widget.prototype.removeChangeListener = function() { + var args = validator.validateMethod(arguments, [{ + name : 'watchId', + type : types.LONG, + }]); + + widgetChangeListener.removeListener(args.watchId); + + var result = native.callSync('Widget_removeChangeListener', {widgetId : this.id}); + if (native.isFailure(result)) { + throw native.getErrorObject(result); + } +}; + +function WidgetServiceManager() { +}; + +WidgetServiceManager.prototype.getWidget = function() { + var args = validator.validateMethod(arguments, [{ + name : 'widgetId', + type : types.STRING, + }]); + + var callArgs = {}; + callArgs.widgetId = args.widgetId; + + var ret = native.callSync('WidgetServiceManager_getWidget', callArgs); + + if (native.isFailure(ret)) { + throw native.getErrorObject(ret); + } else { + return new Widget(native.getResultObject(ret)); + } +}; + +WidgetServiceManager.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 = createObjects(result, Widget); + args.successCallback(widgets); + } + }; + + var callArgs = {}; + if (args.packageId) { + callArgs.packageId = args.packageId; + } + + var result = native.call('WidgetServiceManager_getWidgets', callArgs, callback); + if (native.isFailure(result)) { + throw native.getErrorObject(result); + } +}; + +WidgetServiceManager.prototype.getPrimaryWidgetId = function() { + var args = validator.validateMethod(arguments, [{ + name : 'id', + type : types.STRING, + }]); + + var callArgs = {}; + callArgs.id = args.id; + + var ret = native.callSync('WidgetServiceManager_getPrimaryWidgetId', callArgs); + + if (native.isFailure(ret)) { + throw native.getErrorObject(ret); + } else { + return native.getResultObject(ret); + } +}; + +WidgetServiceManager.prototype.getSize = function() { + var args = validator.validateMethod(arguments, [{ + name : 'sizeType', + type: types.ENUM, + values: T.getValues(WidgetSizeType) + }]); + + var callArgs = {}; + callArgs.sizeType = args.sizeType; + + var ret = native.callSync('WidgetServiceManager_getSize', callArgs); + + if (native.isFailure(ret)) { + throw native.getErrorObject(ret); + } else { + return new WidgetSize(native.getResultObject(ret)); + } +}; + +//Exports +exports = new WidgetServiceManager(); diff --git a/src/widgetservice/widgetservice_extension.cc b/src/widgetservice/widgetservice_extension.cc new file mode 100755 index 00000000..9bdbfcad --- /dev/null +++ b/src/widgetservice/widgetservice_extension.cc @@ -0,0 +1,37 @@ +/* + * 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 "widgetservice/widgetservice_extension.h" + +#include "widgetservice/widgetservice_instance.h" + +// This will be generated from widget_api.js +extern const char kSource_widgetservice_api[]; + +common::Extension* CreateExtension() { + return new WidgetExtension; +} + +WidgetExtension::WidgetExtension() { + SetExtensionName("tizen.widgetservice"); + SetJavaScriptAPI(kSource_widgetservice_api); +} + +WidgetExtension::~WidgetExtension() {} + +common::Instance* WidgetExtension::CreateInstance() { + return new extension::widgetservice::WidgetServiceInstance(); +} diff --git a/src/widgetservice/widgetservice_extension.h b/src/widgetservice/widgetservice_extension.h new file mode 100755 index 00000000..db83db28 --- /dev/null +++ b/src/widgetservice/widgetservice_extension.h @@ -0,0 +1,31 @@ +/* + * 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 WIDGETSERVICE_WIDGET_EXTENSION_H_ +#define WIDGETSERVICE_WIDGET_EXTENSION_H_ + +#include "common/extension.h" + +class WidgetExtension : public common::Extension { + public: + WidgetExtension(); + virtual ~WidgetExtension(); + + private: + virtual common::Instance* CreateInstance(); +}; + +#endif // WIDGETSERVICE_WIDGET_EXTENSION_H_ diff --git a/src/widgetservice/widgetservice_instance.cc b/src/widgetservice/widgetservice_instance.cc new file mode 100755 index 00000000..81ac3c95 --- /dev/null +++ b/src/widgetservice/widgetservice_instance.cc @@ -0,0 +1,613 @@ +/* + * 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 "widgetservice/widgetservice_instance.h" + +#include + +#include +#include +#include +#include + +#include "widgetservice/widgetservice_utils.h" +#include "common/scope_exit.h" + +namespace extension { +namespace widgetservice { + +using common::TizenResult; +using common::TizenSuccess; + +std::mutex WidgetServiceInstance::listener_mutex_; + +namespace { +const common::ListenerToken kWidgetChangeCallbackToken{"WidgetChangeCallback"}; + +const std::string kPrivilegeWidget = "http://tizen.org/privilege/widget.viewer"; + +const std::string kLang = "lang"; +const std::string kInstanceId = "instanceId"; +const std::string kPeriod = "period"; +const std::string kForce = "force"; +const std::string kData = "data"; +const std::string kEvent = "event"; + +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 = WidgetServiceUtils::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 = WidgetServiceUtils::WidgetToJson(widget_id, &val.get()); + if (result) { + array->push_back(val); + } + + return WIDGET_ERROR_NONE; +} + +int WidgetInstanceCb(const char* widget_id, const char* instance_id, void* data) { + ScopeLogger(); + + picojson::array* array = static_cast(data); + + if (!array) { + LoggerW("User data is null"); + return WIDGET_ERROR_NONE; + } + + array->push_back(picojson::value(instance_id)); + + return WIDGET_ERROR_NONE; +} + +int WidgetLifecycleCb(const char* widget_id, widget_lifecycle_event_e lifecycle_event, + const char* widget_instance_id, void* data) { + ScopeLogger(); + + //WIDGET_LIFE_CYCLE_EVENT_MAX event is not supported + if (WIDGET_LIFE_CYCLE_EVENT_RESUME < lifecycle_event) { + LoggerW("Unknown event type"); + return WIDGET_ERROR_NONE; + } + + WidgetServiceInstance* instance = static_cast(data); + + if (!instance) { + LoggerW("User data is null"); + return WIDGET_ERROR_NONE; + } + + picojson::value response = picojson::value(picojson::object()); + auto& obj = response.get(); + + obj.insert(std::make_pair(kWidgetId, picojson::value(widget_id))); + obj.insert(std::make_pair(kInstanceId, picojson::value(widget_instance_id))); + obj.insert(std::make_pair(kEvent, picojson::value(WidgetServiceUtils::FromEventType(lifecycle_event)))); + + instance->CallWidgetLifecycleListener(widget_id, response); + + return WIDGET_ERROR_NONE; +} + +} // namespace + +WidgetServiceInstance::WidgetServiceInstance() { + ScopeLogger(); + using std::placeholders::_1; + using std::placeholders::_2; + +#define REGISTER_SYNC(c, x) \ + RegisterSyncHandler(c, std::bind(&WidgetServiceInstance::x, this, _1)); + + REGISTER_SYNC("WidgetServiceManager_getWidget", GetWidget); + REGISTER_SYNC("WidgetServiceManager_getPrimaryWidgetId", GetPrimaryWidgetId); + REGISTER_SYNC("WidgetServiceManager_getSize", GetSize); + REGISTER_SYNC("Widget_getName", GetName); + REGISTER_SYNC("Widget_getVariant", GetVariant); + REGISTER_SYNC("Widget_addChangeListener", AddChangeListener); + REGISTER_SYNC("Widget_removeChangeListener", RemoveChangeListener); + REGISTER_SYNC("WidgetInstance_changeUpdatePeriod", ChangeUpdatePeriod); + REGISTER_SYNC("WidgetInstance_sendContent", SendContent); + +#undef REGISTER_SYNC + +#define REGISTER_ASYNC(c, x) \ + RegisterHandler(c, std::bind(&WidgetServiceInstance::x, this, _1, _2)); + + REGISTER_ASYNC("WidgetServiceManager_getWidgets", GetWidgets); + REGISTER_ASYNC("Widget_getInstances", GetInstances); + REGISTER_ASYNC("Widget_getVariants", GetVariants); + REGISTER_ASYNC("WidgetInstance_getContent", GetContent); +#undef REGISTER_ASYNC +} + +WidgetServiceInstance::~WidgetServiceInstance() { + ScopeLogger(); + + std::lock_guard lock(listener_mutex_); + for (auto& it : listener_map_) { + int ret = widget_service_unset_lifecycle_event_cb(it.first.c_str(), nullptr); + if (WIDGET_ERROR_NONE != ret) { + LoggerE("widget_service_unset_lifecycle_event_cb() failed"); + } + } + + listener_map_.clear(); +} + +TizenResult WidgetServiceInstance::GetWidget(const picojson::object& args) { + ScopeLogger(); + + //CHECK_PRIVILEGE_ACCESS(kPrivilegeWidget, &out); + CHECK_EXIST(args, kWidgetId, out) + + const auto& widget_id = args.find(kWidgetId)->second.get(); + + picojson::value value {picojson::object{}}; + auto* obj = &value.get(); + + auto result = WidgetServiceUtils::WidgetToJson(widget_id.c_str(), obj); + if (!result) { + LogAndReturnTizenError(result, ("GetWidget() failed")); + } + + return TizenSuccess(value); +} + +TizenResult WidgetServiceInstance::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 = WidgetServiceUtils::ConvertErrorCode(ret); + } else { + result = TizenSuccess{response}; + } + + this->Post(token, result); + }; + + std::thread(get_widgets, token).detach(); + + return TizenSuccess(); +} + +TizenResult WidgetServiceInstance::GetPrimaryWidgetId(const picojson::object& args) { + ScopeLogger(); + + //CHECK_PRIVILEGE_ACCESS(kPrivilegeWidget, &out); + CHECK_EXIST(args, kId, out) + + const auto& id = args.find(kId)->second.get(); + + char* widget_id = widget_service_get_widget_id(id.c_str()); + if (!widget_id) { + LogAndReturnTizenError( + WidgetServiceUtils::ConvertErrorCode(get_last_result()), ("widget_service_get_widget_id() failed")); + } + + SCOPE_EXIT { + free(widget_id); + }; + + return TizenSuccess(picojson::value(widget_id)); +} + +TizenResult WidgetServiceInstance::GetSize(const picojson::object& args) { + ScopeLogger(); + + CHECK_EXIST(args, kSizeType, out) + + widget_size_type_e type = WidgetServiceUtils::ToSizeType(args.find(kSizeType)->second.get()); + if (WIDGET_SIZE_TYPE_UNKNOWN == type) { + LogAndReturnTizenError(common::InvalidValuesError(), ("incorrect size type")); + } + + picojson::value value{picojson::object{}}; + auto* obj = &value.get(); + + auto result = WidgetServiceUtils::SizeToJson(type, obj); + if (!result) { + LogAndReturnTizenError(result, ("GetSize() failed")); + } + + return TizenSuccess(value); +} + +TizenResult WidgetServiceInstance::GetName(picojson::object const& args) { + ScopeLogger(); + + //CHECK_PRIVILEGE_ACCESS(kPrivilegeWidget, &out); + CHECK_EXIST(args, kWidgetId, out) + + const auto& widget_id = args.find(kWidgetId)->second.get(); + char* lang = nullptr; + + const auto lang_it = args.find(kLang); + if (args.end() != lang_it) { + lang = const_cast(lang_it->second.get().c_str()); + } + + char* name = widget_service_get_name(widget_id.c_str(), lang); + if (!name) { + LogAndReturnTizenError( + WidgetServiceUtils::ConvertErrorCode(get_last_result()), ("widget_service_get_name() failed")); + } + + SCOPE_EXIT { + free(name); + }; + + return TizenSuccess(picojson::value(name)); +} + +TizenResult WidgetServiceInstance::GetInstances(picojson::object const& args, const common::AsyncToken& token) { + ScopeLogger(); + + CHECK_EXIST(args, kWidgetId, out) + + const auto& widget_id = args.find(kWidgetId)->second.get(); + + auto get_instances = [this, widget_id](const common::AsyncToken& token) -> void { + picojson::value response{picojson::array{}}; + auto* array = &response.get(); + + int ret = widget_service_get_widget_instance_list(widget_id.c_str(), WidgetInstanceCb, array); + + TizenResult result = TizenSuccess(); + + if (WIDGET_ERROR_NONE != ret) { + LoggerE("widget_service_get_widget_instance_list() failed"); + result = WidgetServiceUtils::ConvertErrorCode(ret); + } else { + result = TizenSuccess{response}; + } + + this->Post(token, result); + }; + + std::thread(get_instances, token).detach(); + + return TizenSuccess(); +} + +TizenResult WidgetServiceInstance::GetVariant(picojson::object const& args) { + ScopeLogger(); + + CHECK_EXIST(args, kWidgetId, out) + CHECK_EXIST(args, kSizeType, out) + + const auto& widget_id = args.find(kWidgetId)->second.get(); + const auto& type = args.find(kSizeType)->second.get(); + + widget_size_type_e size_type = WidgetServiceUtils::ToSizeType(type); + if (WIDGET_SIZE_TYPE_UNKNOWN == size_type) { + LogAndReturnTizenError(common::InvalidValuesError(), ("incorrect size type")); + } + + picojson::value value{picojson::object{}}; + auto* obj = &value.get(); + + auto result = WidgetServiceUtils::SizeToJson(size_type, obj); + if (!result) { + LogAndReturnTizenError(result, ("GetVariant() failed")); + } + + result = WidgetServiceUtils::WidgetVariantToJson(widget_id.c_str(), size_type, obj); + if (!result) { + LogAndReturnTizenError(result, ("GetVariant() failed")); + } + + //sizeType + obj->insert(std::make_pair(kSizeType, picojson::value(type))); + + return TizenSuccess(value); +} + +TizenResult WidgetServiceInstance::GetVariants(picojson::object const& args, const common::AsyncToken& token) { + ScopeLogger(); + + //CHECK_PRIVILEGE_ACCESS(kPrivilegeWidget, &out); + CHECK_EXIST(args, kWidgetId, out) + + const auto& widget_id = args.find(kWidgetId)->second.get(); + + auto get_variants = [this, widget_id](const common::AsyncToken& token) -> void { + int count = 0; + int* type_array = nullptr; + int ret = widget_service_get_supported_size_types(widget_id.c_str(), &count, &type_array); + + if (WIDGET_ERROR_NONE != ret) { + LoggerE("widget_service_get_supported_size_types() failed"); + this->Post(token, WidgetServiceUtils::ConvertErrorCode(ret)); + return; + } + + //it is not mentioned in header file if array should be freed by caller + //but in widget_service_get_supported_size_types definition it is allocated + //so it should be released when it is not needed anymore + SCOPE_EXIT { + free(type_array); + }; + + TizenResult result = TizenSuccess(); + picojson::value response{picojson::array{}}; + auto& array = response.get(); + + for (int i = 0; i < count; i++) { + picojson::value val = picojson::value(picojson::object()); + picojson::object* obj = &val.get(); + + widget_size_type_e size_type = static_cast(type_array[i]); + result = WidgetServiceUtils::SizeToJson(size_type, obj); + if (!result) { + break; + } + + result = WidgetServiceUtils::WidgetVariantToJson(widget_id.c_str(), size_type, obj); + if (!result) { + break; + } + + obj->insert(std::make_pair(kSizeType, picojson::value(WidgetServiceUtils::FromSizeType(size_type)))); + array.push_back(val); + } + + if (!result) { + this->Post(token, result); + } else { + this->Post(token, TizenSuccess{response}); + } + }; + + std::thread(get_variants, token).detach(); + + return TizenSuccess(); +} + +void WidgetServiceInstance::CallWidgetLifecycleListener(const std::string& widget_id, + const picojson::value& response) { + ScopeLogger(); + + std::lock_guard lock(listener_mutex_); + const auto it = listener_map_.find(widget_id); + if (listener_map_.end() != it) { + Post(kWidgetChangeCallbackToken, TizenSuccess{response}); + return; + } + + LoggerW("widget id was not found."); +} + +TizenResult WidgetServiceInstance::AddChangeListener(picojson::object const& args) { + ScopeLogger(); + + //CHECK_PRIVILEGE_ACCESS(kPrivilegeWidget, &out); + CHECK_EXIST(args, kWidgetId, out) + + const auto& widget_id = args.find(kWidgetId)->second.get(); + + std::lock_guard lock(listener_mutex_); + auto it = listener_map_.find(widget_id); + if (listener_map_.end() != it) { + it->second++; + return TizenSuccess(); + } + + int ret = widget_service_set_lifecycle_event_cb(widget_id.c_str(), WidgetLifecycleCb , this); + if (WIDGET_ERROR_NONE != ret) { + LogAndReturnTizenError( + WidgetServiceUtils::ConvertErrorCode(ret), ("widget_service_set_lifecycle_event_cb() failed")); + } + + listener_map_[widget_id]++; + + return TizenSuccess(); +} + +TizenResult WidgetServiceInstance::RemoveChangeListener(picojson::object const& args) { + ScopeLogger(); + + CHECK_EXIST(args, kWidgetId, out) + + const auto& widget_id = args.find(kWidgetId)->second.get(); + + std::lock_guard lock(listener_mutex_); + auto it = listener_map_.find(widget_id); + if (listener_map_.end() == it) { + LoggerW("Listener id not found"); + return TizenSuccess(); + } + + if (!(--it->second)) { + int ret = widget_service_unset_lifecycle_event_cb(widget_id.c_str(), nullptr); + if (WIDGET_ERROR_NONE != ret) { + LogAndReturnTizenError( + WidgetServiceUtils::ConvertErrorCode(ret), ("widget_service_unset_lifecycle_event_cb() failed")); + } + listener_map_.erase(it); + } + + return TizenSuccess(); +} + +TizenResult WidgetServiceInstance::ChangeUpdatePeriod(picojson::object const& args) { + ScopeLogger(); + + CHECK_EXIST(args, kWidgetId, out) + CHECK_EXIST(args, kInstanceId, out) + CHECK_EXIST(args, kPeriod, out) + + const auto& widget_id = args.find(kWidgetId)->second.get(); + const auto& instance_id = args.find(kInstanceId)->second.get(); + const double period = args.find(kPeriod)->second.get(); + + int ret = widget_service_change_period(widget_id.c_str(), instance_id.c_str(), period); + + if (WIDGET_ERROR_NONE != ret) { + LogAndReturnTizenError( + WidgetServiceUtils::ConvertErrorCode(ret), ("widget_service_change_period() failed")); + } + + return TizenSuccess(); +} + +TizenResult WidgetServiceInstance::SendContent(picojson::object const& args) { + ScopeLogger(); + + CHECK_EXIST(args, kWidgetId, out) + CHECK_EXIST(args, kInstanceId, out) + CHECK_EXIST(args, kData, out) + CHECK_EXIST(args, kForce, out) + + const auto& widget_id = args.find(kWidgetId)->second.get(); + const auto& instance_id = args.find(kInstanceId)->second.get(); + const int force = args.find(kForce)->second.get() ? 1 : 0; + + bundle* data = bundle_create(); + int ret = get_last_result(); + if (BUNDLE_ERROR_NONE != ret) { + LogAndReturnTizenError(common::AbortError(ret), ("bundle_create() failed")); + } + + SCOPE_EXIT { + bundle_free(data); + }; + + ret = bundle_add(data, kData.c_str(), args.find(kData)->second.serialize().c_str()); + if (BUNDLE_ERROR_NONE != ret) { + LogAndReturnTizenError(common::AbortError(ret), ("bundle_add() failed")); + } + + ret = widget_service_trigger_update(widget_id.c_str(), instance_id.c_str(), data, force); + if (WIDGET_ERROR_NONE != ret) { + LogAndReturnTizenError( + WidgetServiceUtils::ConvertErrorCode(ret), ("widget_service_trigger_update() failed")); + } + + return TizenSuccess(); +} + +TizenResult WidgetServiceInstance::GetContent(picojson::object const& args, const common::AsyncToken& token) { + ScopeLogger(); + + CHECK_EXIST(args, kWidgetId, out) + CHECK_EXIST(args, kInstanceId, out) + + const auto& widget_id = args.find(kWidgetId)->second.get(); + const auto& instance_id = args.find(kInstanceId)->second.get(); + + auto get_content = [this, widget_id, instance_id](const common::AsyncToken& token) -> void { + bundle* bundle_data = bundle_create(); + + int ret = get_last_result(); + if (BUNDLE_ERROR_NONE != ret) { + LoggerE("bundle_create() failed"); + this->Post(token, common::AbortError(ret)); + return; + } + + SCOPE_EXIT { + bundle_free(bundle_data); + }; + + ret = widget_service_get_content_of_widget_instance(widget_id.c_str(), + instance_id.c_str(), &bundle_data); + if (WIDGET_ERROR_NONE != ret) { + LoggerE("widget_service_get_content_of_widget_instance() failed"); + this->Post(token, WidgetServiceUtils::ConvertErrorCode(ret)); + return; + } + + char* data_str = nullptr; + ret = bundle_get_str(bundle_data, kData.c_str(), &data_str); + if (BUNDLE_ERROR_NONE != ret) { + LoggerE("bundle_get_str() failed"); + this->Post(token, common::AbortError(ret)); + return; + } + + picojson::value response; + std::string err; + picojson::parse(response, data_str, data_str + strlen(data_str), &err); + if (!err.empty()) { + LoggerE("Failed to parse bundle data() failed [%s]", err.c_str()); + this->Post(token, common::AbortError()); + return; + } + + this->Post(token, TizenSuccess{response}); + }; + + std::thread(get_content, token).detach(); + + return TizenSuccess(); +} + +} // namespace widgetservice +} // namespace extension diff --git a/src/widgetservice/widgetservice_instance.h b/src/widgetservice/widgetservice_instance.h new file mode 100755 index 00000000..8aadbfaa --- /dev/null +++ b/src/widgetservice/widgetservice_instance.h @@ -0,0 +1,58 @@ +/* + * 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 WIDGETSERVICE_WIDGET_INSTANCE_H_ +#define WIDGETSERVICE_WIDGET_INSTANCE_H_ + +#include +#include + +#include "common/tizen_instance.h" + +namespace extension { +namespace widgetservice { + +class WidgetServiceInstance : public common::TizenInstance { + public: + WidgetServiceInstance(); + virtual ~WidgetServiceInstance(); + void CallWidgetLifecycleListener(const std::string& widget_id, const picojson::value& response); + private: + //WidgetManager + common::TizenResult GetWidget(picojson::object const& args); + common::TizenResult GetWidgets(picojson::object const& args, const common::AsyncToken& token); + common::TizenResult GetPrimaryWidgetId(picojson::object const& args); + common::TizenResult GetSize(picojson::object const& args); + //Widget + common::TizenResult GetName(picojson::object const& args); + common::TizenResult GetInstances(picojson::object const& args, const common::AsyncToken& token); + common::TizenResult GetVariant(picojson::object const& args); + common::TizenResult GetVariants(picojson::object const& args, const common::AsyncToken& token); + common::TizenResult AddChangeListener(picojson::object const& args); + common::TizenResult RemoveChangeListener(picojson::object const& args); + //WidgetInstance + common::TizenResult ChangeUpdatePeriod(picojson::object const& args); + common::TizenResult SendContent(picojson::object const& args); + common::TizenResult GetContent(picojson::object const& args, const common::AsyncToken& token); + + static std::mutex listener_mutex_; + std::map listener_map_; +}; + +} // namespace widgetservice +} // namespace extension + +#endif // WIDGETSERVICE_WIDGET_INSTANCE_H_ diff --git a/src/widgetservice/widgetservice_utils.cc b/src/widgetservice/widgetservice_utils.cc new file mode 100644 index 00000000..7ba52e0a --- /dev/null +++ b/src/widgetservice/widgetservice_utils.cc @@ -0,0 +1,246 @@ +/* + * 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 "widgetservice_utils.h" + +#include + +namespace extension { +namespace widgetservice { + +namespace { + +#define WIDGET_SIZE_TYPE_E \ + X(WIDGET_SIZE_TYPE_1x1, "1x1") \ + X(WIDGET_SIZE_TYPE_2x1, "2x1") \ + X(WIDGET_SIZE_TYPE_2x2, "2x2") \ + X(WIDGET_SIZE_TYPE_4x1, "4x1") \ + X(WIDGET_SIZE_TYPE_4x2, "4x2") \ + X(WIDGET_SIZE_TYPE_4x3, "4x3") \ + X(WIDGET_SIZE_TYPE_4x4, "4x4") \ + X(WIDGET_SIZE_TYPE_4x5, "4x5") \ + X(WIDGET_SIZE_TYPE_4x6, "4x6") \ + X(WIDGET_SIZE_TYPE_EASY_1x1, "EASY_1x1") \ + X(WIDGET_SIZE_TYPE_EASY_3x1, "EASY_3x1") \ + X(WIDGET_SIZE_TYPE_EASY_3x3, "EASY_3x3") \ + X(WIDGET_SIZE_TYPE_FULL, "FULL") \ + XD(WIDGET_SIZE_TYPE_UNKNOWN, "unknown") + +#define WIDGET_LIFECYCLE_EVENT_E \ + X(WIDGET_LIFE_CYCLE_EVENT_CREATE, "CREATE") \ + X(WIDGET_LIFE_CYCLE_EVENT_DESTROY, "DESTROY") \ + X(WIDGET_LIFE_CYCLE_EVENT_PAUSE, "PAUSE") \ + X(WIDGET_LIFE_CYCLE_EVENT_RESUME, "RESUME") \ + XD(WIDGET_LIFE_CYCLE_EVENT_MAX, "unknown") + +} // namespace + +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"; +const std::string kSizeType = "sizeType"; +const std::string kWidth = "width"; +const std::string kHeight = "height"; +const std::string kNeedsMouseEvents = "needsMouseEvents"; +const std::string kNeedsTouchEffect = "needsTouchEffect"; +const std::string kNeedsFrame = "needsFrame"; +const std::string kPreviewImagePath = "previewImagePath"; + +using common::TizenResult; +using common::TizenSuccess; + +TizenResult WidgetServiceUtils::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 WidgetServiceUtils::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(); +} + +TizenResult WidgetServiceUtils::SizeToJson(widget_size_type_e type, picojson::object* out) { + ScopeLogger(); + + int width = 0; + int height = 0; + + int ret = widget_service_get_size(type, &width, &height); + if (WIDGET_ERROR_NONE != ret) { + LogAndReturnTizenError(ConvertErrorCode(ret), ("widget_service_get_size() failed")); + } + + out->insert(std::make_pair(kWidth, picojson::value(static_cast(width)))); + out->insert(std::make_pair(kHeight, picojson::value(static_cast(height)))); + + return TizenSuccess(); +} + +TizenResult WidgetServiceUtils::WidgetVariantToJson( + const char* id, widget_size_type_e type, picojson::object* out) { + ScopeLogger(); + + bool tmp = false; + + //needsMouseEvents + int ret = widget_service_get_need_of_mouse_event(id, type, &tmp); + if (WIDGET_ERROR_NONE != ret) { + LogAndReturnTizenError( + ConvertErrorCode(ret), ("widget_service_get_need_of_mouse_event() failed")); + } + out->insert(std::make_pair(kNeedsMouseEvents, picojson::value(tmp))); + + //needsTouchEffect + ret = widget_service_get_need_of_touch_effect(id, type, &tmp); + if (WIDGET_ERROR_NONE != ret) { + LogAndReturnTizenError( + ConvertErrorCode(ret), ("widget_service_get_need_of_touch_effect() failed")); + } + out->insert(std::make_pair(kNeedsTouchEffect, picojson::value(tmp))); + + //needsFrame + ret = widget_service_get_need_of_frame(id, type, &tmp); + if (WIDGET_ERROR_NONE != ret) { + LogAndReturnTizenError( + ConvertErrorCode(ret), ("widget_service_get_need_of_frame() failed")); + } + out->insert(std::make_pair(kNeedsFrame, picojson::value(tmp))); + + //previewImagePath + char* path = widget_service_get_preview_image_path(id, type); + if (!path) { + LogAndReturnTizenError( + ConvertErrorCode(get_last_result()), ("widget_service_get_preview_image_path() failed")); + } + out->insert(std::make_pair(kPreviewImagePath, picojson::value(path))); + free(path); + + return TizenSuccess(); +} + +#define X(v, s) case v: return s; +#define XD(v, s) \ + default: \ + LoggerE("Unknown value: %d, returning default: %s", e, s); \ + return s; + +std::string WidgetServiceUtils::FromSizeType(widget_size_type_e e) { + ScopeLogger(); + + switch (e) { + WIDGET_SIZE_TYPE_E + } +} + +std::string WidgetServiceUtils::FromEventType(widget_lifecycle_event_e e) { + ScopeLogger(); + + switch (e) { + WIDGET_LIFECYCLE_EVENT_E + } +} + +#undef X +#undef XD + +#define X(v, s) if (e == s) return v; +#define XD(v, s) \ + LoggerE("Unknown value: %s, returning default: %d", e.c_str(), v); \ + return v; + +widget_size_type_e WidgetServiceUtils::ToSizeType(const std::string& e) { + ScopeLogger(); + + WIDGET_SIZE_TYPE_E +} + +#undef X +#undef XD + +} // widgetservice +} // extension diff --git a/src/widgetservice/widgetservice_utils.h b/src/widgetservice/widgetservice_utils.h new file mode 100644 index 00000000..5f6a2644 --- /dev/null +++ b/src/widgetservice/widgetservice_utils.h @@ -0,0 +1,56 @@ +/* + * 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_WIDGETSERVICE_WIDGET_UTILS_H__ +#define WEBAPI_PLUGINS_WIDGETSERVICE_WIDGET_UTILS_H__ + +#include + +#include + +#include "common/tizen_result.h" + +namespace extension { +namespace widgetservice { + +#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; +extern const std::string kId; +extern const std::string kSizeType; +extern const std::string kWidth; +extern const std::string kHeight; + +class WidgetServiceUtils { + public: + static widget_size_type_e ToSizeType(const std::string& e); + static std::string FromSizeType(widget_size_type_e e); + static std::string FromEventType(widget_lifecycle_event_e e); + static common::TizenResult ConvertErrorCode(int error); + static common::TizenResult WidgetToJson(const char* id, picojson::object* out, const char* pkgid = nullptr); + static common::TizenResult SizeToJson(widget_size_type_e type, picojson::object* out); + static common::TizenResult WidgetVariantToJson(const char* id, widget_size_type_e type, picojson::object* out); + +}; + +} // widgetservice +} // extension + +#endif // WEBAPI_PLUGINS_WIDGETSERVICE_WIDGET_UTILS_H__