From: WonYoung Choi Date: Tue, 12 Apr 2016 02:57:21 +0000 (+0900) Subject: Add echo sample for xwalk-runtime X-Git-Tag: submit/tizen/20170830.020105~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=a568e939e93375cad014e1a4457a9940f957c669;p=platform%2Fframework%2Fweb%2Fxwalk-extensions-common.git Add echo sample for xwalk-runtime Change-Id: Ib6e07a472f490878b8f42802ffaa50e9345224e6 --- diff --git a/samples/echo/README b/samples/echo/README new file mode 100644 index 0000000..b5df451 --- /dev/null +++ b/samples/echo/README @@ -0,0 +1,10 @@ + +# Usages tizen.echo + +var ret = tizen.echo.syncEcho("Hello Xwalk!"); +console.log(ret); + +tizen.echo.echo("Hello Xwalk! Async mode", function(msg) { + console.log(msg); +}); + diff --git a/samples/echo/build.gyp b/samples/echo/build.gyp new file mode 100644 index 0000000..e2ef0e9 --- /dev/null +++ b/samples/echo/build.gyp @@ -0,0 +1,18 @@ +{ + 'targets': [ + { + 'target_name': 'echo', + 'type': 'shared_library', + 'sources': [ + 'echo_api.js', + 'echo_extension.h', + 'echo_extension.cc', + ], + 'variables': { + 'packages': [ + 'xwalk-extensions-common', + ], + }, + }, + ], +} diff --git a/samples/echo/echo_api.js b/samples/echo/echo_api.js new file mode 100644 index 0000000..6d5e275 --- /dev/null +++ b/samples/echo/echo_api.js @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2015 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 echoListener = null; + +extension.setMessageListener(function(msg) { + if (echoListener instanceof Function) { + echoListener(msg); + } +}); + +exports.echo = function(msg, callback) { + echoListener = callback; + extension.postMessage(msg); +}; + +exports.syncEcho = function(msg) { + return extension.internal.sendSyncMessage(msg); +}; diff --git a/samples/echo/echo_extension.cc b/samples/echo/echo_extension.cc new file mode 100644 index 0000000..eb85ec1 --- /dev/null +++ b/samples/echo/echo_extension.cc @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2015 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 "echo_extension.h" + +namespace sample { + +/** + * @class EchoExtension + */ + +/* @method EchoExtension::CreateInstance() + * + * CreateInstance() SHOULD be implemented in inherited class + * to return your own ExtensionInstance class. + */ +xwalk::XWalkExtensionInstance* EchoExtension::CreateInstance() { + return new EchoInstance; +} + +/** + * @class EchoInstance + */ + +/* @method EchoInstance::HandleMessage() + * + * HandleMessage() CAN be implemented if want to handle asyncronous messages + * sent by 'extension.postMessage()' in echo_api.js. + * Asyncronous response can be sent with PostMessage() and the sent response + * can be handled using 'extension.setMessageListener()' in echo_api.js also. + */ +void EchoInstance::HandleMessage(const char* msg) { + PostMessage(msg); +} + +/* @method EchoInstance::HandleSyncMessage() + * + * HandleSyncMessage() CAN be implemented if want to handle syncronous messages + * sent by 'extension.internal.sendSyncMessage()' in echo_api.js. + * This method should send response with SendSyncReply(). + */ +void EchoInstance::HandleSyncMessage(const char* msg) { + SendSyncReply(msg); +} + +} // namespace sample + +/* @macro EXPORT_XWALK_EXTENSION + * + * The implemented sub-class of XWalkExtension should be exported with + * EXPORT_XWALK_EXTENSION(name, class) macro. + */ +EXPORT_XWALK_EXTENSION(tizen.echo, sample::EchoExtension); diff --git a/samples/echo/echo_extension.h b/samples/echo/echo_extension.h new file mode 100644 index 0000000..52fbde7 --- /dev/null +++ b/samples/echo/echo_extension.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2015 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 ECHO_ECHO_EXTENSION_H_ +#define ECHO_ECHO_EXTENSION_H_ + +#include + +namespace sample { + +class EchoExtension : public xwalk::XWalkExtension { + public: + // @override + xwalk::XWalkExtensionInstance* CreateInstance(); +}; + +class EchoInstance : public xwalk::XWalkExtensionInstance { + public: + // @override + void HandleMessage(const char* msg); + + // @override + void HandleSyncMessage(const char* msg); +}; + +} // namespace sample + +#endif // ECHO_ECHO_EXTENSION_H_ diff --git a/samples/echo/packaging/echo.manifest b/samples/echo/packaging/echo.manifest new file mode 100644 index 0000000..75b0fa5 --- /dev/null +++ b/samples/echo/packaging/echo.manifest @@ -0,0 +1,5 @@ + + + + + diff --git a/samples/echo/packaging/echo.spec b/samples/echo/packaging/echo.spec new file mode 100644 index 0000000..c0ff816 --- /dev/null +++ b/samples/echo/packaging/echo.spec @@ -0,0 +1,40 @@ +%define crosswalk_extensions tizen-extensions-crosswalk +%define crosswalk_extensions_path %{_libdir}/%{crosswalk_extensions} + +Name: echo +Summary: Echo sample for xwalk-extensions-common +Version: 0.0.1 +Release: 1 +Group: Development/Libraries +License: Apache-2.0 and BSD-3-Clause +URL: https://www.tizen.org +Source0: %{name}-%{version}.tar.gz +Source1: %{name}.manifest + +BuildRequires: pkgconfig(xwalk-extensions-common) +BuildRequires: xwalk-gyp + +%description +Echo sample extension + +%prep +%setup -q +cp %{SOURCE1} . + +%build +xwalk-gyp build.gyp +make %{?jobs:-j%jobs} + +%install +rm -rf %{buildroot} +mkdir -p %{buildroot}%{crosswalk_extensions_path} +install -p -m 644 out/Default/lib.target/*.so %{buildroot}%{crosswalk_extensions_path} +install -p -m 644 tizen_echo.json %{buildroot}%{crosswalk_extensions_path} + +%clean +rm -rf %{buildroot} + +%files +%manifest %{name}.manifest +%{crosswalk_extensions_path}/*.so +%{crosswalk_extensions_path}/*.json diff --git a/samples/echo/tizen_echo.json b/samples/echo/tizen_echo.json new file mode 100644 index 0000000..26396ba --- /dev/null +++ b/samples/echo/tizen_echo.json @@ -0,0 +1,7 @@ +[ + { + "name":"tizen.echo", + "lib":"libecho.so", + "entry_points":[] + } +]