From c1de74cb8c7f2845bfa45303b8e8c723ce74defc Mon Sep 17 00:00:00 2001 From: Seungkeun Lee Date: Tue, 5 Jan 2016 11:25:55 +0900 Subject: [PATCH] New AppFW Module implements - initial version - xwalk style moudle Change-Id: I0b40628790f2acf79699595d324b53b9164463d4 --- modules/tizen-app-control/package.json | 10 + modules/tizen-app-control/tizen-app-control.js | 212 ++++ .../app_common_extension.cc | 161 +++ .../app_common_extension.h | 49 + modules/tizen-application-common/build.gyp | 19 + modules/tizen-application-common/package.json | 13 + modules/tizen-application-common/picojson.h | 1037 ++++++++++++++++++++ .../tizen-application-common_api.js | 139 +++ modules/tizen-application/appfw.cc | 125 +++ modules/tizen-application/appfw.h | 53 + modules/tizen-application/build.gyp | 23 + modules/tizen-application/package.json | 15 + modules/tizen-application/picojson.h | 1037 ++++++++++++++++++++ modules/tizen-application/tizen-application_api.js | 211 ++++ modules/tizen-application/ui_app_extension.cc | 132 +++ modules/tizen-application/ui_app_extension.h | 52 + 16 files changed, 3288 insertions(+) create mode 100644 modules/tizen-app-control/package.json create mode 100755 modules/tizen-app-control/tizen-app-control.js create mode 100755 modules/tizen-application-common/app_common_extension.cc create mode 100755 modules/tizen-application-common/app_common_extension.h create mode 100755 modules/tizen-application-common/build.gyp create mode 100755 modules/tizen-application-common/package.json create mode 100644 modules/tizen-application-common/picojson.h create mode 100755 modules/tizen-application-common/tizen-application-common_api.js create mode 100755 modules/tizen-application/appfw.cc create mode 100755 modules/tizen-application/appfw.h create mode 100755 modules/tizen-application/build.gyp create mode 100755 modules/tizen-application/package.json create mode 100644 modules/tizen-application/picojson.h create mode 100755 modules/tizen-application/tizen-application_api.js create mode 100755 modules/tizen-application/ui_app_extension.cc create mode 100755 modules/tizen-application/ui_app_extension.h diff --git a/modules/tizen-app-control/package.json b/modules/tizen-app-control/package.json new file mode 100644 index 0000000..2bba106 --- /dev/null +++ b/modules/tizen-app-control/package.json @@ -0,0 +1,10 @@ +{ + "name": "tizen-app-control", + "version": "0.0.1", + "description": "Module for AppControl", + "main": "tizen-app-control.js", + "author": { + "name": "Seungkeun Lee", + "email": "sngn.lee@samsung.com" + } +} diff --git a/modules/tizen-app-control/tizen-app-control.js b/modules/tizen-app-control/tizen-app-control.js new file mode 100755 index 0000000..1698a5a --- /dev/null +++ b/modules/tizen-app-control/tizen-app-control.js @@ -0,0 +1,212 @@ +'use strict'; + +var internalKeys = { + 'operation': '__APP_SVC_OP_TYPE__', + 'uri': '__APP_SVC_URI__', + 'mime': '__APP_SVC_MIME_TYPE__', + 'category': '__APP_SVC_CATEGORY__', +}; + +var internalMap = Symbol(); + /** + * @class AppControl + * @constructor + * @param {string} operation + * @param {Object} config {'uri', 'mime', 'category'} + */ +class AppControl { + constructor(operation, config) { + this[internalMap] = new Map(); + this.data = new Map(); + if (config && config['json']) { + for (let key in config['json']) { + if (key.startsWith('__AUL_') || key.startsWith('__APP_SVC_')) { + this[internalMap].set(key, config['json'][key]); + } else { + this.data.set(key, config['json'][key]); + } + } + } else { + this.operation = operation; + if (config) { + if (config['uri']) { + this.uri = config['uri']; + } + if (config['mime']) { + this.mime = config['mime']; + } + if (config['category']) { + this.category = config['category']; + } + } + } + } + + /** + * @attribute operation + * @type {string} + */ + get operation() { + return this[internalMap].get(internalKeys['operation']); + } + + set operation(value) { + this[internalMap].set(internalKeys['operation'], value); + } + + /** + * @attribute uri + * @type {string} + */ + get uri() { + return this[internalMap].get(internalKeys['uri']); + } + set uri(value) { + this[internalMap].set(internalKeys['uri'], value); + } + + /** + * @attribute mime + * @type {string} + */ + get mime() { + return this[internalMap].get(internalKeys['mime']); + } + set mime(value) { + this[internalMap].set(internalKeys['mime'], value); + } + + /** + * @attribute category + * @type {string} + */ + get category() { + return this[internalMap].get(internalKeys['category']); + } + set category(value) { + this[internalMap].set(internalKeys['category'], value); + } + + /** + * @method toJSON + * + * @return {string} + */ + toJSON() { + var obj = {}; + this[internalMap].forEach(function(v, k) { + obj[k] = v; + }); + this['data'].forEach(function(v, k) { + obj[k] = v; + }); + return JSON.stringify(obj); + } +}; + +/** + * @method fromJSON + * + * @param {string} json + * + * @return {AppControl} + */ +AppControl.fromJSON = function(json) { + return new AppControl(undefined, {'json': JSON.parse(json)}); +}; + + +// Definition for the app_control operation: main operation for an explicit launch. +AppControl.OPERATION_MAIN = 'http://tizen.org/appcontrol/operation/main'; + +// Definition for the app_control operation: default operation for an explicit launch. +AppControl.OPERATION_DEFAULT = 'http://tizen.org/appcontrol/operation/default'; + +// Definition for the app_control operation: provides an explicit editable access to the given data. +AppControl.OPERATION_EDIT = 'http://tizen.org/appcontrol/operation/edit'; + +// Definition for the app_control operation: displays the data. +AppControl.OPERATION_VIEW = 'http://tizen.org/appcontrol/operation/view'; + +// Definition for the app_control operation: picks an item from the data, returning what is selected. +AppControl.OPERATION_PICK = 'http://tizen.org/appcontrol/operation/pick'; + +// Definition for the app_control operation: creates content, returning what is created. +AppControl.OPERATION_CREATE_CONTENT = 'http://tizen.org/appcontrol/operation/create_content'; + +// Definition for the app_control operation: performs a call to someone specified by the data. +AppControl.OPERATION_CALL = 'http://tizen.org/appcontrol/operation/call'; + +// Definition for the app_control operation: delivers some data to someone else. +AppControl.OPERATION_SEND = 'http://tizen.org/appcontrol/operation/send'; + +// Definition for the app_control operation: delivers text data to someone else. +AppControl.OPERATION_SEND_TEXT = 'http://tizen.org/appcontrol/operation/send_text'; + +// Definition for the app_control operation: shares an item with someone else. +AppControl.OPERATION_SHARE = 'http://tizen.org/appcontrol/operation/share'; + +// Definition for the app_control operation: shares multiple items with someone else. +AppControl.OPERATION_MULTI_SHARE = 'http://tizen.org/appcontrol/operation/multi_share'; + +// Definition for the app_control operation: shares text data with someone else. +AppControl.OPERATION_SHARE_TEXT = 'http://tizen.org/appcontrol/operation/share_text'; + + +// Definition for the app_control operation: dials a number as specified by the data. +AppControl.OPERATION_DIAL = 'http://tizen.org/appcontrol/operation/dial'; + +// Definition for the app_control operation: performs a search. +AppControl.OPERATION_SEARCH = 'http://tizen.org/appcontrol/operation/search'; + +// Definition for the app_control operation: downloads an item. +AppControl.OPERATION_DOWNLOAD = 'http://tizen.org/appcontrol/operation/download'; + +// Definition for the app_control operation: prints content. +AppControl.OPERATION_PRINT = 'http://tizen.org/appcontrol/operation/print'; + +// Definition for the app_control operation: composes. +AppControl.OPERATION_COMPOSE = 'http://tizen.org/appcontrol/operation/compose'; + +// Definition for app_control optional data: the subject of a message. +AppControl.DATA_SUBJECT = 'http://tizen.org/appcontrol/data/subject'; + +// Definition for app_control optional data: e-mail addresses. +AppControl.DATA_TO = 'http://tizen.org/appcontrol/data/to'; + +// Definition for app_control optional data: e-mail addresses that should be carbon copied. +AppControl.DATA_CC = 'http://tizen.org/appcontrol/data/cc'; + +// Definition for app_control optional data: e-mail addresses that should be blind carbon copied. +AppControl.DATA_BCC = 'http://tizen.org/appcontrol/data/bcc'; + +// Definition for app_control optional data: the content of the data is associated with APP_CONTROL_OPERATION_SEND. +AppControl.DATA_TEXT = 'http://tizen.org/appcontrol/data/text'; + +// Definition for app_control optional data: the title of the data. +AppControl.DATA_TITLE = 'http://tizen.org/appcontrol/data/title'; + +// Definition for app_control optional data: the path of a selected item. +AppControl.DATA_SELECTED = 'http://tizen.org/appcontrol/data/selected'; + +// Definition for app_control optional data: multiple item path to deliver. +AppControl.DATA_PATH = 'http://tizen.org/appcontrol/data/path'; + +// Definition for app_control optional data: the selection type. +AppControl.DATA_SELECTION_MODE = 'http://tizen.org/appcontrol/data/selection_mode'; + + +/* + * @module tizen-app-control + * + * + * ``` + * var AppControl = require('tizen-app-control'); + * + * var appcontrol = new AppControl(AppControl.OPERATION_VIEW); + * + * var appcontrol2 = AppControl.fromJSON(string); + * + * ``` + */ +module.exports = AppControl; diff --git a/modules/tizen-application-common/app_common_extension.cc b/modules/tizen-application-common/app_common_extension.cc new file mode 100755 index 0000000..49ab58e --- /dev/null +++ b/modules/tizen-application-common/app_common_extension.cc @@ -0,0 +1,161 @@ +/* + * 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 "app_common_extension.h" + +#include +#include +#include + +#ifdef LOG_TAG +#undef LOG_TAG +#endif +#define LOG_TAG "JSNative" + +namespace appfw { + +xwalk::XWalkExtensionInstance* AppCommonExtension::CreateInstance() { + return new AppCommonInstance(); +} + +void AppCommonInstance::Initialize() { + LOGD("Created tizen-application-common instance"); +} + +void AppCommonInstance::HandleMessage(const char* msg) { + // parse json object +} + +void AppCommonInstance::HandleSyncMessage(const char* msg) { + // parse json object + picojson::value value; + std::string err; + picojson::parse(value, msg, msg + strlen(msg), &err); + if (!err.empty()) { + LOGE("Ignoring message. Can't parse msessage : %s", err.c_str()); + return; + } + if (!value.is()) { + LOGE("Ignoring message. It is not an object."); + return; + } + + // handle synchronous messages + std::string cmd = value.get("cmd").to_str(); + picojson::object result; + if (cmd == "id") { + char* id = nullptr; + app_get_id(&id); + if (id) { + result["data"] = picojson::value(id); + free(id); + } + } else if (cmd == "name") { + char* name = nullptr; + app_get_name(&name); + if (name) { + result["data"] = picojson::value(name); + free(name); + } + } else if (cmd == "version") { + char* version = nullptr; + app_get_name(&version); + if (version) { + result["data"] = picojson::value(version); + free(version); + } + } else if (cmd == "datapath") { + char* datapath = app_get_data_path(); + if (datapath) { + result["data"] = picojson::value(datapath); + free(datapath); + } + } else if (cmd == "respath") { + char* respath = app_get_resource_path(); + if (respath) { + result["data"] = picojson::value(respath); + free(respath); + } + } else if (cmd == "cachepath") { + char* cachepath = app_get_cache_path(); + if (cachepath) { + result["data"] = picojson::value(cachepath); + free(cachepath); + } + } else if (cmd == "sharedrespath") { + char* sharedrespath = app_get_shared_resource_path(); + if (sharedrespath) { + result["data"] = picojson::value(sharedrespath); + free(sharedrespath); + } + } else if (cmd == "appcontrol_response") { + HandleAppcontrolResponse(value, &result); + } else { + LOGW("Ignoring message. It is not an object."); + return; + } + SendSyncReply(picojson::value(result).serialize().c_str()); +} + +void AppCommonInstance::HandleAppcontrolResponse(const picojson::value& value, + picojson::object* result) { + if (!value.get("appcontrol").is() || + !value.get("data").is()) { + LOGE("Ignoring message. Wrong arguments were passed"); + return; + } + std::string appcontrol_json = value.get("appcontrol").to_str(); + + // TODO(sngn.lee): to be implemented + // create request from json + // appcontrol_h request; + // app_control_create_from_json(&request, json.c_str()); + + auto extra_data = value.get("data").get(); + app_control_h response; + app_control_create(&response); + for (auto& item : extra_data) { + auto& key = item.first; + auto& value = item.second; + if (value.is()) { + auto& array_value = value.get(); + std::vector data_store; + std::vector pointer_store; + for (auto& array_item : array_value) { + data_store.push_back(array_item.to_str()); + pointer_store.push_back(data_store.back().c_str()); + } + app_control_add_extra_data_array(response, + key.c_str(), + pointer_store.data(), + pointer_store.size()); + } else { + app_control_add_extra_data(response, + key.c_str(), + value.to_str().c_str()); + } + } + + // TODO(sngn.lee): to be implemented + // app_control_reply_to_launch_request(response, request, 0); + // app_control_destroy(request); + app_control_destroy(response); + result->insert(std::make_pair("result", picojson::value("OK"))); +} + +} // namespace appfw + +EXPORT_XWALK_EXTENSION(tizen_application_common, appfw::AppCommonExtension); diff --git a/modules/tizen-application-common/app_common_extension.h b/modules/tizen-application-common/app_common_extension.h new file mode 100755 index 0000000..888f291 --- /dev/null +++ b/modules/tizen-application-common/app_common_extension.h @@ -0,0 +1,49 @@ +/* + * 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 APP_COMMON_EXTENSION_H_ +#define APP_COMMON_EXTENSION_H_ + +#include +#include "picojson.h" + +namespace appfw { + +class AppCommonExtension : public xwalk::XWalkExtension { + public: + // @override + xwalk::XWalkExtensionInstance* CreateInstance(); +}; + +class AppCommonInstance : public xwalk::XWalkExtensionInstance { + public: + // @override + void Initialize(); + + // @override + void HandleMessage(const char* msg); + + // @override + void HandleSyncMessage(const char* msg); + private: + static void HandleAppcontrolResponse(const picojson::value& json, + picojson::object* result); +}; + +} // namespace appfw + +#endif // APP_COMMON_EXTENSION_H_ + diff --git a/modules/tizen-application-common/build.gyp b/modules/tizen-application-common/build.gyp new file mode 100755 index 0000000..09e896d --- /dev/null +++ b/modules/tizen-application-common/build.gyp @@ -0,0 +1,19 @@ +{ + 'targets': [ + { + 'target_name': 'tizen-application-common', + 'sources': [ + 'tizen-application-common_api.js', + 'app_common_extension.h', + 'app_common_extension.cc', + 'picojson.h', + ], + 'variables': { + 'packages': [ + 'dlog', + 'capi-appfw-application', + ], + }, + }, + ], +} diff --git a/modules/tizen-application-common/package.json b/modules/tizen-application-common/package.json new file mode 100755 index 0000000..e9b0213 --- /dev/null +++ b/modules/tizen-application-common/package.json @@ -0,0 +1,13 @@ +{ + "name": "tizen-application-common", + "version": "0.0.1", + "description": "Base module for application", + "main": "tizen-application-common.xwalk", + "dependencies": { + "tizen-app-control": ">= 0.0.1" + }, + "author": { + "name": "Seungkeun Lee", + "email": "sngn.lee@samsung.com" + } +} diff --git a/modules/tizen-application-common/picojson.h b/modules/tizen-application-common/picojson.h new file mode 100644 index 0000000..0ae6851 --- /dev/null +++ b/modules/tizen-application-common/picojson.h @@ -0,0 +1,1037 @@ +/* + * Copyright 2009-2010 Cybozu Labs, Inc. + * Copyright 2011 Kazuho Oku + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY CYBOZU LABS, INC. ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL CYBOZU LABS, INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are + * those of the authors and should not be interpreted as representing official + * policies, either expressed or implied, of Cybozu Labs, Inc. + * + */ +#ifndef picojson_h +#define picojson_h + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef _MSC_VER + #define SNPRINTF _snprintf_s + #pragma warning(push) + #pragma warning(disable : 4244) // conversion from int to char +#else + #define SNPRINTF snprintf +#endif + +namespace picojson { + + enum { + null_type, + boolean_type, + number_type, + string_type, + array_type, + object_type + }; + + struct null {}; + + class value { + public: + typedef std::vector array; + typedef std::map object; + union _storage { + bool boolean_; + double number_; + std::string* string_; + array* array_; + object* object_; + }; + protected: + int type_; + _storage u_; + public: + value(); + value(int type, bool); + explicit value(bool b); + explicit value(double n); + explicit value(const std::string& s); + explicit value(const array& a); + explicit value(const object& o); + explicit value(const char* s); + value(const char* s, size_t len); + ~value(); + value(const value& x); + value& operator=(const value& x); + void swap(value& x); + template bool is() const; + template const T& get() const; + template T& get(); + bool evaluate_as_boolean() const; + const value& get(size_t idx) const; + const value& get(const std::string& key) const; + bool contains(size_t idx) const; + bool contains(const std::string& key) const; + std::string to_str() const; + template void serialize(Iter os) const; + std::string serialize() const; + private: + template value(const T*); // intentionally defined to block implicit conversion of pointer to bool + }; + + typedef value::array array; + typedef value::object object; + + inline value::value() : type_(null_type) {} + + inline value::value(int type, bool) : type_(type) { + switch (type) { +#define INIT(p, v) case p##type: u_.p = v; break + INIT(boolean_, false); + INIT(number_, 0.0); + INIT(string_, new std::string()); + INIT(array_, new array()); + INIT(object_, new object()); +#undef INIT + default: break; + } + } + + inline value::value(bool b) : type_(boolean_type) { + u_.boolean_ = b; + } + + inline value::value(double n) : type_(number_type) { + u_.number_ = n; + } + + inline value::value(const std::string& s) : type_(string_type) { + u_.string_ = new std::string(s); + } + + inline value::value(const array& a) : type_(array_type) { + u_.array_ = new array(a); + } + + inline value::value(const object& o) : type_(object_type) { + u_.object_ = new object(o); + } + + inline value::value(const char* s) : type_(string_type) { + u_.string_ = new std::string(s); + } + + inline value::value(const char* s, size_t len) : type_(string_type) { + u_.string_ = new std::string(s, len); + } + + inline value::~value() { + switch (type_) { +#define DEINIT(p) case p##type: delete u_.p; break + DEINIT(string_); + DEINIT(array_); + DEINIT(object_); +#undef DEINIT + default: break; + } + } + + inline value::value(const value& x) : type_(x.type_) { + switch (type_) { +#define INIT(p, v) case p##type: u_.p = v; break + INIT(string_, new std::string(*x.u_.string_)); + INIT(array_, new array(*x.u_.array_)); + INIT(object_, new object(*x.u_.object_)); +#undef INIT + default: + u_ = x.u_; + break; + } + } + + inline value& value::operator=(const value& x) { + if (this != &x) { + this->~value(); + new (this) value(x); + } + return *this; + } + + inline void value::swap(value& x) { + std::swap(type_, x.type_); + std::swap(u_, x.u_); + } + +#define IS(ctype, jtype) \ + template <> inline bool value::is() const { \ + return type_ == jtype##_type; \ + } + IS(null, null) + IS(bool, boolean) + IS(int, number) + IS(double, number) + IS(std::string, string) + IS(array, array) + IS(object, object) +#undef IS + +#define GET(ctype, var) \ + template <> inline const ctype& value::get() const { \ + assert("type mismatch! call vis() before get()" \ + && is()); \ + return var; \ + } \ + template <> inline ctype& value::get() { \ + assert("type mismatch! call is() before get()" \ + && is()); \ + return var; \ + } + GET(bool, u_.boolean_) + GET(double, u_.number_) + GET(std::string, *u_.string_) + GET(array, *u_.array_) + GET(object, *u_.object_) +#undef GET + + inline bool value::evaluate_as_boolean() const { + switch (type_) { + case null_type: + return false; + case boolean_type: + return u_.boolean_; + case number_type: + return u_.number_ != 0; + case string_type: + return ! u_.string_->empty(); + default: + return true; + } + } + + inline const value& value::get(size_t idx) const { + static value s_null; + assert(is()); + return idx < u_.array_->size() ? (*u_.array_)[idx] : s_null; + } + + inline const value& value::get(const std::string& key) const { + static value s_null; + assert(is()); + object::const_iterator i = u_.object_->find(key); + return i != u_.object_->end() ? i->second : s_null; + } + + inline bool value::contains(size_t idx) const { + assert(is()); + return idx < u_.array_->size(); + } + + inline bool value::contains(const std::string& key) const { + assert(is()); + object::const_iterator i = u_.object_->find(key); + return i != u_.object_->end(); + } + + inline std::string value::to_str() const { + switch (type_) { + case null_type: return "null"; + case boolean_type: return u_.boolean_ ? "true" : "false"; + case number_type: { + char buf[256]; + double tmp; + SNPRINTF(buf, sizeof(buf), fabs(u_.number_) < (1ULL << 53) && modf(u_.number_, &tmp) == 0 ? "%.f" : "%.17g", u_.number_); + return buf; + } + case string_type: return *u_.string_; + case array_type: return "array"; + case object_type: return "object"; + default: assert(0); +#ifdef _MSC_VER + __assume(0); +#endif + } + return std::string(); + } + + template void copy(const std::string& s, Iter oi) { + std::copy(s.begin(), s.end(), oi); + } + + template void serialize_str(const std::string& s, Iter oi) { + *oi++ = '"'; + for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) { + switch (*i) { +#define MAP(val, sym) case val: copy(sym, oi); break + MAP('"', "\\\""); + MAP('\\', "\\\\"); + MAP('/', "\\/"); + MAP('\b', "\\b"); + MAP('\f', "\\f"); + MAP('\n', "\\n"); + MAP('\r', "\\r"); + MAP('\t', "\\t"); +#undef MAP + default: + if ((unsigned char)*i < 0x20 || *i == 0x7f) { + char buf[7]; + SNPRINTF(buf, sizeof(buf), "\\u%04x", *i & 0xff); + copy(buf, buf + 6, oi); + } else { + *oi++ = *i; + } + break; + } + } + *oi++ = '"'; + } + + template void value::serialize(Iter oi) const { + switch (type_) { + case string_type: + serialize_str(*u_.string_, oi); + break; + case array_type: { + *oi++ = '['; + for (array::const_iterator i = u_.array_->begin(); + i != u_.array_->end(); + ++i) { + if (i != u_.array_->begin()) { + *oi++ = ','; + } + i->serialize(oi); + } + *oi++ = ']'; + break; + } + case object_type: { + *oi++ = '{'; + for (object::const_iterator i = u_.object_->begin(); + i != u_.object_->end(); + ++i) { + if (i != u_.object_->begin()) { + *oi++ = ','; + } + serialize_str(i->first, oi); + *oi++ = ':'; + i->second.serialize(oi); + } + *oi++ = '}'; + break; + } + default: + copy(to_str(), oi); + break; + } + } + + inline std::string value::serialize() const { + std::string s; + serialize(std::back_inserter(s)); + return s; + } + + template class input { + protected: + Iter cur_, end_; + int last_ch_; + bool ungot_; + int line_; + public: + input(const Iter& first, const Iter& last) : cur_(first), end_(last), last_ch_(-1), ungot_(false), line_(1) {} + int getc() { + if (ungot_) { + ungot_ = false; + return last_ch_; + } + if (cur_ == end_) { + last_ch_ = -1; + return -1; + } + if (last_ch_ == '\n') { + line_++; + } + last_ch_ = *cur_++ & 0xff; + return last_ch_; + } + void ungetc() { + if (last_ch_ != -1) { + assert(! ungot_); + ungot_ = true; + } + } + Iter cur() const { return cur_; } + int line() const { return line_; } + void skip_ws() { + while (1) { + int ch = getc(); + if (! (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r')) { + ungetc(); + break; + } + } + } + bool expect(int expect) { + skip_ws(); + if (getc() != expect) { + ungetc(); + return false; + } + return true; + } + bool match(const std::string& pattern) { + for (std::string::const_iterator pi(pattern.begin()); + pi != pattern.end(); + ++pi) { + if (getc() != *pi) { + ungetc(); + return false; + } + } + return true; + } + }; + + template inline int _parse_quadhex(input &in) { + int uni_ch = 0, hex; + for (int i = 0; i < 4; i++) { + if ((hex = in.getc()) == -1) { + return -1; + } + if ('0' <= hex && hex <= '9') { + hex -= '0'; + } else if ('A' <= hex && hex <= 'F') { + hex -= 'A' - 0xa; + } else if ('a' <= hex && hex <= 'f') { + hex -= 'a' - 0xa; + } else { + in.ungetc(); + return -1; + } + uni_ch = uni_ch * 16 + hex; + } + return uni_ch; + } + + template inline bool _parse_codepoint(String& out, input& in) { + int uni_ch; + if ((uni_ch = _parse_quadhex(in)) == -1) { + return false; + } + if (0xd800 <= uni_ch && uni_ch <= 0xdfff) { + if (0xdc00 <= uni_ch) { + // a second 16-bit of a surrogate pair appeared + return false; + } + // first 16-bit of surrogate pair, get the next one + if (in.getc() != '\\' || in.getc() != 'u') { + in.ungetc(); + return false; + } + int second = _parse_quadhex(in); + if (! (0xdc00 <= second && second <= 0xdfff)) { + return false; + } + uni_ch = ((uni_ch - 0xd800) << 10) | ((second - 0xdc00) & 0x3ff); + uni_ch += 0x10000; + } + if (uni_ch < 0x80) { + out.push_back(uni_ch); + } else { + if (uni_ch < 0x800) { + out.push_back(0xc0 | (uni_ch >> 6)); + } else { + if (uni_ch < 0x10000) { + out.push_back(0xe0 | (uni_ch >> 12)); + } else { + out.push_back(0xf0 | (uni_ch >> 18)); + out.push_back(0x80 | ((uni_ch >> 12) & 0x3f)); + } + out.push_back(0x80 | ((uni_ch >> 6) & 0x3f)); + } + out.push_back(0x80 | (uni_ch & 0x3f)); + } + return true; + } + + template inline bool _parse_string(String& out, input& in) { + while (1) { + int ch = in.getc(); + if (ch < ' ') { + in.ungetc(); + return false; + } else if (ch == '"') { + return true; + } else if (ch == '\\') { + if ((ch = in.getc()) == -1) { + return false; + } + switch (ch) { +#define MAP(sym, val) case sym: out.push_back(val); break + MAP('"', '\"'); + MAP('\\', '\\'); + MAP('/', '/'); + MAP('b', '\b'); + MAP('f', '\f'); + MAP('n', '\n'); + MAP('r', '\r'); + MAP('t', '\t'); +#undef MAP + case 'u': + if (! _parse_codepoint(out, in)) { + return false; + } + break; + default: + return false; + } + } else { + out.push_back(ch); + } + } + return false; + } + + template inline bool _parse_array(Context& ctx, input& in) { + if (! ctx.parse_array_start()) { + return false; + } + if (in.expect(']')) { + return true; + } + size_t idx = 0; + do { + if (! ctx.parse_array_item(in, idx)) { + return false; + } + idx++; + } while (in.expect(',')); + return in.expect(']'); + } + + template inline bool _parse_object(Context& ctx, input& in) { + if (! ctx.parse_object_start()) { + return false; + } + if (in.expect('}')) { + return true; + } + do { + std::string key; + if (! in.expect('"') + || ! _parse_string(key, in) + || ! in.expect(':')) { + return false; + } + if (! ctx.parse_object_item(in, key)) { + return false; + } + } while (in.expect(',')); + return in.expect('}'); + } + + template inline bool _parse_number(double& out, input& in) { + std::string num_str; + while (1) { + int ch = in.getc(); + if (('0' <= ch && ch <= '9') || ch == '+' || ch == '-' || ch == '.' + || ch == 'e' || ch == 'E') { + num_str.push_back(ch); + } else { + in.ungetc(); + break; + } + } + char* endp; + out = strtod(num_str.c_str(), &endp); + return endp == num_str.c_str() + num_str.size(); + } + + template inline bool _parse(Context& ctx, input& in) { + in.skip_ws(); + int ch = in.getc(); + switch (ch) { +#define IS(ch, text, op) case ch: \ + if (in.match(text) && op) { \ + return true; \ + } else { \ + return false; \ + } + IS('n', "ull", ctx.set_null()); + IS('f', "alse", ctx.set_bool(false)); + IS('t', "rue", ctx.set_bool(true)); +#undef IS + case '"': + return ctx.parse_string(in); + case '[': + return _parse_array(ctx, in); + case '{': + return _parse_object(ctx, in); + default: + if (('0' <= ch && ch <= '9') || ch == '-') { + in.ungetc(); + double f; + if (_parse_number(f, in)) { + ctx.set_number(f); + return true; + } else { + return false; + } + } + break; + } + in.ungetc(); + return false; + } + + class deny_parse_context { + public: + bool set_null() { return false; } + bool set_bool(bool) { return false; } + bool set_number(double) { return false; } + template bool parse_string(input&) { return false; } + bool parse_array_start() { return false; } + template bool parse_array_item(input&, size_t) { + return false; + } + bool parse_object_start() { return false; } + template bool parse_object_item(input&, const std::string&) { + return false; + } + }; + + class default_parse_context { + protected: + value* out_; + public: + default_parse_context(value* out) : out_(out) {} + bool set_null() { + *out_ = value(); + return true; + } + bool set_bool(bool b) { + *out_ = value(b); + return true; + } + bool set_number(double f) { + *out_ = value(f); + return true; + } + template bool parse_string(input& in) { + *out_ = value(string_type, false); + return _parse_string(out_->get(), in); + } + bool parse_array_start() { + *out_ = value(array_type, false); + return true; + } + template bool parse_array_item(input& in, size_t) { + array& a = out_->get(); + a.push_back(value()); + default_parse_context ctx(&a.back()); + return _parse(ctx, in); + } + bool parse_object_start() { + *out_ = value(object_type, false); + return true; + } + template bool parse_object_item(input& in, const std::string& key) { + object& o = out_->get(); + default_parse_context ctx(&o[key]); + return _parse(ctx, in); + } + private: + default_parse_context(const default_parse_context&); + default_parse_context& operator=(const default_parse_context&); + }; + + class null_parse_context { + public: + struct dummy_str { + void push_back(int) {} + }; + public: + null_parse_context() {} + bool set_null() { return true; } + bool set_bool(bool) { return true; } + bool set_number(double) { return true; } + template bool parse_string(input& in) { + dummy_str s; + return _parse_string(s, in); + } + bool parse_array_start() { return true; } + template bool parse_array_item(input& in, size_t) { + return _parse(*this, in); + } + bool parse_object_start() { return true; } + template bool parse_object_item(input& in, const std::string&) { + return _parse(*this, in); + } + private: + null_parse_context(const null_parse_context&); + null_parse_context& operator=(const null_parse_context&); + }; + + // obsolete, use the version below + template inline std::string parse(value& out, Iter& pos, const Iter& last) { + std::string err; + pos = parse(out, pos, last, &err); + return err; + } + + template inline Iter _parse(Context& ctx, const Iter& first, const Iter& last, std::string* err) { + input in(first, last); + if (! _parse(ctx, in) && err != NULL) { + char buf[64]; + SNPRINTF(buf, sizeof(buf), "syntax error at line %d near: ", in.line()); + *err = buf; + while (1) { + int ch = in.getc(); + if (ch == -1 || ch == '\n') { + break; + } else if (ch >= ' ') { + err->push_back(ch); + } + } + } + return in.cur(); + } + + template inline Iter parse(value& out, const Iter& first, const Iter& last, std::string* err) { + default_parse_context ctx(&out); + return _parse(ctx, first, last, err); + } + + inline std::string parse(value& out, std::istream& is) { + std::string err; + parse(out, std::istreambuf_iterator(is.rdbuf()), + std::istreambuf_iterator(), &err); + return err; + } + + template struct last_error_t { + static std::string s; + }; + template std::string last_error_t::s; + + inline void set_last_error(const std::string& s) { + last_error_t::s = s; + } + + inline const std::string& get_last_error() { + return last_error_t::s; + } + + inline bool operator==(const value& x, const value& y) { + if (x.is()) + return y.is(); +#define PICOJSON_CMP(type) \ + if (x.is()) \ + return y.is() && x.get() == y.get() + PICOJSON_CMP(bool); + PICOJSON_CMP(double); + PICOJSON_CMP(std::string); + PICOJSON_CMP(array); + PICOJSON_CMP(object); +#undef PICOJSON_CMP + assert(0); +#ifdef _MSC_VER + __assume(0); +#endif + return false; + } + + inline bool operator!=(const value& x, const value& y) { + return ! (x == y); + } +} + +namespace std { + template<> inline void swap(picojson::value& x, picojson::value& y) + { + x.swap(y); + } +} + +inline std::istream& operator>>(std::istream& is, picojson::value& x) +{ + picojson::set_last_error(std::string()); + std::string err = picojson::parse(x, is); + if (! err.empty()) { + picojson::set_last_error(err); + is.setstate(std::ios::failbit); + } + return is; +} + +inline std::ostream& operator<<(std::ostream& os, const picojson::value& x) +{ + x.serialize(std::ostream_iterator(os)); + return os; +} +#ifdef _MSC_VER + #pragma warning(pop) +#endif + +#endif +#ifdef TEST_PICOJSON +#ifdef _MSC_VER + #pragma warning(disable : 4127) // conditional expression is constant +#endif + +using namespace std; + +static void plan(int num) +{ + printf("1..%d\n", num); +} + +static bool success = true; + +static void ok(bool b, const char* name = "") +{ + static int n = 1; + if (! b) + success = false; + printf("%s %d - %s\n", b ? "ok" : "ng", n++, name); +} + +template void is(const T& x, const T& y, const char* name = "") +{ + if (x == y) { + ok(true, name); + } else { + ok(false, name); + } +} + +#include +#include +#include +#include + +int main(void) +{ + plan(85); + + // constructors +#define TEST(expr, expected) \ + is(picojson::value expr .serialize(), string(expected), "picojson::value" #expr) + + TEST( (true), "true"); + TEST( (false), "false"); + TEST( (42.0), "42"); + TEST( (string("hello")), "\"hello\""); + TEST( ("hello"), "\"hello\""); + TEST( ("hello", 4), "\"hell\""); + + { + double a = 1; + for (int i = 0; i < 1024; i++) { + picojson::value vi(a); + std::stringstream ss; + ss << vi; + picojson::value vo; + ss >> vo; + double b = vo.get(); + if ((i < 53 && a != b) || fabs(a - b) / b > 1e-8) { + printf("ng i=%d a=%.18e b=%.18e\n", i, a, b); + } + a *= 2; + } + } + +#undef TEST + +#define TEST(in, type, cmp, serialize_test) { \ + picojson::value v; \ + const char* s = in; \ + string err = picojson::parse(v, s, s + strlen(s)); \ + ok(err.empty(), in " no error"); \ + ok(v.is(), in " check type"); \ + is(v.get(), cmp, in " correct output"); \ + is(*s, '\0', in " read to eof"); \ + if (serialize_test) { \ + is(v.serialize(), string(in), in " serialize"); \ + } \ + } + TEST("false", bool, false, true); + TEST("true", bool, true, true); + TEST("90.5", double, 90.5, false); + TEST("1.7976931348623157e+308", double, DBL_MAX, false); + TEST("\"hello\"", string, string("hello"), true); + TEST("\"\\\"\\\\\\/\\b\\f\\n\\r\\t\"", string, string("\"\\/\b\f\n\r\t"), + true); + TEST("\"\\u0061\\u30af\\u30ea\\u30b9\"", string, + string("a\xe3\x82\xaf\xe3\x83\xaa\xe3\x82\xb9"), false); + TEST("\"\\ud840\\udc0b\"", string, string("\xf0\xa0\x80\x8b"), false); +#undef TEST + +#define TEST(type, expr) { \ + picojson::value v; \ + const char *s = expr; \ + string err = picojson::parse(v, s, s + strlen(s)); \ + ok(err.empty(), "empty " #type " no error"); \ + ok(v.is(), "empty " #type " check type"); \ + ok(v.get().empty(), "check " #type " array size"); \ + } + TEST(array, "[]"); + TEST(object, "{}"); +#undef TEST + + { + picojson::value v; + const char *s = "[1,true,\"hello\"]"; + string err = picojson::parse(v, s, s + strlen(s)); + ok(err.empty(), "array no error"); + ok(v.is(), "array check type"); + is(v.get().size(), size_t(3), "check array size"); + ok(v.contains(0), "check contains array[0]"); + ok(v.get(0).is(), "check array[0] type"); + is(v.get(0).get(), 1.0, "check array[0] value"); + ok(v.contains(1), "check contains array[1]"); + ok(v.get(1).is(), "check array[1] type"); + ok(v.get(1).get(), "check array[1] value"); + ok(v.contains(2), "check contains array[2]"); + ok(v.get(2).is(), "check array[2] type"); + is(v.get(2).get(), string("hello"), "check array[2] value"); + ok(!v.contains(3), "check not contains array[3]"); + } + + { + picojson::value v; + const char *s = "{ \"a\": true }"; + string err = picojson::parse(v, s, s + strlen(s)); + ok(err.empty(), "object no error"); + ok(v.is(), "object check type"); + is(v.get().size(), size_t(1), "check object size"); + ok(v.contains("a"), "check contains property"); + ok(v.get("a").is(), "check bool property exists"); + is(v.get("a").get(), true, "check bool property value"); + is(v.serialize(), string("{\"a\":true}"), "serialize object"); + ok(!v.contains("z"), "check not contains property"); + } + +#define TEST(json, msg) do { \ + picojson::value v; \ + const char *s = json; \ + string err = picojson::parse(v, s, s + strlen(s)); \ + is(err, string("syntax error at line " msg), msg); \ + } while (0) + TEST("falsoa", "1 near: oa"); + TEST("{]", "1 near: ]"); + TEST("\n\bbell", "2 near: bell"); + TEST("\"abc\nd\"", "1 near: "); +#undef TEST + + { + picojson::value v1, v2; + const char *s; + string err; + s = "{ \"b\": true, \"a\": [1,2,\"three\"], \"d\": 2 }"; + err = picojson::parse(v1, s, s + strlen(s)); + s = "{ \"d\": 2.0, \"b\": true, \"a\": [1,2,\"three\"] }"; + err = picojson::parse(v2, s, s + strlen(s)); + ok((v1 == v2), "check == operator in deep comparison"); + } + + { + picojson::value v1, v2; + const char *s; + string err; + s = "{ \"b\": true, \"a\": [1,2,\"three\"], \"d\": 2 }"; + err = picojson::parse(v1, s, s + strlen(s)); + s = "{ \"d\": 2.0, \"a\": [1,\"three\"], \"b\": true }"; + err = picojson::parse(v2, s, s + strlen(s)); + ok((v1 != v2), "check != operator for array in deep comparison"); + } + + { + picojson::value v1, v2; + const char *s; + string err; + s = "{ \"b\": true, \"a\": [1,2,\"three\"], \"d\": 2 }"; + err = picojson::parse(v1, s, s + strlen(s)); + s = "{ \"d\": 2.0, \"a\": [1,2,\"three\"], \"b\": false }"; + err = picojson::parse(v2, s, s + strlen(s)); + ok((v1 != v2), "check != operator for object in deep comparison"); + } + + { + picojson::value v1, v2; + const char *s; + string err; + s = "{ \"b\": true, \"a\": [1,2,\"three\"], \"d\": 2 }"; + err = picojson::parse(v1, s, s + strlen(s)); + picojson::object& o = v1.get(); + o.erase("b"); + picojson::array& a = o["a"].get(); + picojson::array::iterator i; + i = std::remove(a.begin(), a.end(), picojson::value(std::string("three"))); + a.erase(i, a.end()); + s = "{ \"a\": [1,2], \"d\": 2 }"; + err = picojson::parse(v2, s, s + strlen(s)); + ok((v1 == v2), "check erase()"); + } + + ok(picojson::value(3.0).serialize() == "3", + "integral number should be serialized as a integer"); + + { + const char* s = "{ \"a\": [1,2], \"d\": 2 }"; + picojson::null_parse_context ctx; + string err; + picojson::_parse(ctx, s, s + strlen(s), &err); + ok(err.empty(), "null_parse_context"); + } + + { + picojson::value v1, v2; + v1 = picojson::value(true); + swap(v1, v2); + ok(v1.is(), "swap (null)"); + ok(v2.get() == true, "swap (bool)"); + + v1 = picojson::value("a"); + v2 = picojson::value(1.0); + swap(v1, v2); + ok(v1.get() == 1.0, "swap (dobule)"); + ok(v2.get() == "a", "swap (string)"); + + v1 = picojson::value(picojson::object()); + v2 = picojson::value(picojson::array()); + swap(v1, v2); + ok(v1.is(), "swap (array)"); + ok(v2.is(), "swap (object)"); + } + + return success ? 0 : 1; +} + +#endif diff --git a/modules/tizen-application-common/tizen-application-common_api.js b/modules/tizen-application-common/tizen-application-common_api.js new file mode 100755 index 0000000..6420e51 --- /dev/null +++ b/modules/tizen-application-common/tizen-application-common_api.js @@ -0,0 +1,139 @@ +'use strict'; + +function native_call(method, parameter) { + var args = {}; + args['cmd'] = method; + args = Object.assign(args, parameter); + try { + return JSON.parse(extension.internal.sendSyncMessage(JSON.stringify(args))); + } catch (e) { + console.log(e.message); + return {}; + } +} + +var AppControl = require('tizen-app-control'); + +var callerAppId = Symbol(); +/** + * @class RequestedAppControl + */ +class RequestedAppControl extends AppControl { + constructor(config) { + super('', {json: config}); + this[callerAppId] = config['__AUL_CALLER_APPID__']; + } + + /** + * .... + * @method response + * @param {Map, Object} data + */ + response(data) { + var data_obj = data; + if (data instanceof Map) { + data.forEach(function(v, k) { + data_obj[k] = v; + }); + } + var args = {}; + args['appcontrol'] = this.toJSON(); + args['data'] = data_obj; + native_call('appcontrol_response', args); + } + + /** + * @attribute callerAppID + * @type {string} + * @readonly + */ + get callerAppID() { + return this[callerAppId]; + } +}; + +var EE = require('events'); +/** + * @class ApplicationCommon +* @extends Node.EventEmmitter + */ +class ApplicationCommon extends EE { + constructor() { + super(); + } + + get id() { + return native_call('id')['data']; + } + + get name() { + return native_call('name')['data']; + } + + get version() { + return native_call('version')['data']; + } + + get dataPath() { + return native_call('datapath')['data']; + } + + get resPath() { + return native_call('respath')['data']; + } + + get cachePath() { + return native_call('cachepath')['data']; + } + + get sharedResPath() { + return native_call('sharedrespath')['data']; + } + + /** + * .... + * @event lowmemory + * @param {string} status + * * "NORMAL" + * * "SOFT_WARNING" + * * "HARD_WARNING" + * @since 3.0 + */ + + + /** + * .... + * @event lowbattery + * @param {string} status + * * "POWER_OFF " + * * "CRITICAL_LOW" + * @since 3.0 + */ + + /** + * .... + * @event languagechange + * + * @since 3.0 + */ + + /** + * .... + * @event regionchange + * @param {string} language + * + * @since 3.0 + */ + + /** + * .... + * @event orientationchange + * @param {string} orientation + * + * @since 3.0 + */ + +}; + +exports.ApplicationCommon = ApplicationCommon; +exports.RequestedAppControl = RequestedAppControl; diff --git a/modules/tizen-application/appfw.cc b/modules/tizen-application/appfw.cc new file mode 100755 index 0000000..b18200e --- /dev/null +++ b/modules/tizen-application/appfw.cc @@ -0,0 +1,125 @@ +/* + * 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 "appfw.h" + +#include +#include +#include +#include +#include + +#ifdef LOG_TAG +#undef LOG_TAG +#endif +#define LOG_TAG "JSNative" + +namespace appfw { +AppFW* AppFW::GetInstance() { + static AppFW instance; + return &instance; +} + +AppFW::AppFW() : initialized_(false), + create_handler_(nullptr), + service_handler_(nullptr), + terminate_handler_(nullptr), + pause_handler_(nullptr), + resume_handler_(nullptr) { +} + +AppFW::~AppFW() { +} + +bool AppFW::Init(int argc, char* argv[]) { + if (initialized_) + return false; + + initialized_ = true; + static struct appcore_ops ops; + + ops.create = [](void* data) { + AppFW* appfw = static_cast(data); + if (appfw->create_handler_) + appfw->create_handler_(); + return 0; + }; + + ops.terminate = [](void* data) { + AppFW* appfw = static_cast(data); + if (appfw->terminate_handler_) + appfw->terminate_handler_(); + return 0; + }; + + ops.pause = [](void* data) { + AppFW* appfw = static_cast(data); + if (appfw->pause_handler_) + appfw->pause_handler_(); + return 0; + }; + + ops.resume = [](void* data) { + AppFW* appfw = static_cast(data); + if (appfw->resume_handler_) + appfw->resume_handler_(); + return 0; + }; + + ops.reset = [](bundle* b, void* data) { + AppFW* appfw = static_cast(data); + if (appfw->service_handler_) { + char* json = nullptr; + if (bundle_to_json(b, &json) == 0) { + appfw->service_handler_(json); + free(json); + } + } + return 0; + }; + + ops.data = this; + + appcore_efl_init("js-binding", &argc, &argv, &ops); + + return true; +} + +void AppFW::Deinit() { + if (initialized_) { + initialized_ = false; + appcore_efl_fini(); + } +} + +void AppFW::set_create_handler(std::function handler) { + create_handler_ = handler; +} +void AppFW::set_service_handler( + std::function handler) { + service_handler_ = handler; +} +void AppFW::set_terminate_handler(std::function handler) { + terminate_handler_ = handler; +} +void AppFW::set_pause_handler(std::function handler) { + pause_handler_ = handler; +} +void AppFW::set_resume_handler(std::function handler) { + resume_handler_ = handler; +} + +} // namespace appfw diff --git a/modules/tizen-application/appfw.h b/modules/tizen-application/appfw.h new file mode 100755 index 0000000..d2a1a5c --- /dev/null +++ b/modules/tizen-application/appfw.h @@ -0,0 +1,53 @@ +/* + * 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 TIZEN_APPLICATION_APPFW_H_ +#define TIZEN_APPLICATION_APPFW_H_ + + +#include +#include + +namespace appfw { + +class AppFW { + public: + static AppFW* GetInstance(); + bool Init(int argc, char* argv[]); + void Deinit(); + void set_create_handler(std::function handler); + void set_service_handler( + std::function handler); + void set_terminate_handler(std::function handler); + void set_pause_handler(std::function handler); + void set_resume_handler(std::function handler); + + // TODO(sngn.lee) : Add system event callback. eg) low battery, low memory... + + private: + AppFW(); + ~AppFW(); + bool initialized_; + std::function create_handler_; + std::function service_handler_; + std::function terminate_handler_; + std::function pause_handler_; + std::function resume_handler_; +}; + +} // namespace appfw + +#endif // TIZEN_APPLICATION_APPFW_H_ diff --git a/modules/tizen-application/build.gyp b/modules/tizen-application/build.gyp new file mode 100755 index 0000000..ede945a --- /dev/null +++ b/modules/tizen-application/build.gyp @@ -0,0 +1,23 @@ +{ + 'targets': [ + { + 'target_name': 'tizen-application', + 'sources': [ + 'tizen-application_api.js', + 'ui_app_extension.h', + 'ui_app_extension.cc', + 'picojson.h', + 'appfw.cc', + 'appfw.h', + ], + 'variables': { + 'packages': [ + 'dlog', + 'appcore-efl', + 'aul', + ], + }, + }, + ], +} + diff --git a/modules/tizen-application/package.json b/modules/tizen-application/package.json new file mode 100755 index 0000000..187f277 --- /dev/null +++ b/modules/tizen-application/package.json @@ -0,0 +1,15 @@ +{ + "name": "tizen-application", + "version": "0.0.1", + "description": "Module for UIApplication", + "main": "tizen-application.xwalk", + "dependencies": { + "tizen-application-common": ">= 0.0.1", + "tizen-app-control": ">= 0.0.1", + "gcontext": ">= 0.0.1" + }, + "author": { + "name": "Seungkeun Lee", + "email": "sngn.lee@samsung.com" + } +} diff --git a/modules/tizen-application/picojson.h b/modules/tizen-application/picojson.h new file mode 100644 index 0000000..0ae6851 --- /dev/null +++ b/modules/tizen-application/picojson.h @@ -0,0 +1,1037 @@ +/* + * Copyright 2009-2010 Cybozu Labs, Inc. + * Copyright 2011 Kazuho Oku + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY CYBOZU LABS, INC. ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL CYBOZU LABS, INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are + * those of the authors and should not be interpreted as representing official + * policies, either expressed or implied, of Cybozu Labs, Inc. + * + */ +#ifndef picojson_h +#define picojson_h + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef _MSC_VER + #define SNPRINTF _snprintf_s + #pragma warning(push) + #pragma warning(disable : 4244) // conversion from int to char +#else + #define SNPRINTF snprintf +#endif + +namespace picojson { + + enum { + null_type, + boolean_type, + number_type, + string_type, + array_type, + object_type + }; + + struct null {}; + + class value { + public: + typedef std::vector array; + typedef std::map object; + union _storage { + bool boolean_; + double number_; + std::string* string_; + array* array_; + object* object_; + }; + protected: + int type_; + _storage u_; + public: + value(); + value(int type, bool); + explicit value(bool b); + explicit value(double n); + explicit value(const std::string& s); + explicit value(const array& a); + explicit value(const object& o); + explicit value(const char* s); + value(const char* s, size_t len); + ~value(); + value(const value& x); + value& operator=(const value& x); + void swap(value& x); + template bool is() const; + template const T& get() const; + template T& get(); + bool evaluate_as_boolean() const; + const value& get(size_t idx) const; + const value& get(const std::string& key) const; + bool contains(size_t idx) const; + bool contains(const std::string& key) const; + std::string to_str() const; + template void serialize(Iter os) const; + std::string serialize() const; + private: + template value(const T*); // intentionally defined to block implicit conversion of pointer to bool + }; + + typedef value::array array; + typedef value::object object; + + inline value::value() : type_(null_type) {} + + inline value::value(int type, bool) : type_(type) { + switch (type) { +#define INIT(p, v) case p##type: u_.p = v; break + INIT(boolean_, false); + INIT(number_, 0.0); + INIT(string_, new std::string()); + INIT(array_, new array()); + INIT(object_, new object()); +#undef INIT + default: break; + } + } + + inline value::value(bool b) : type_(boolean_type) { + u_.boolean_ = b; + } + + inline value::value(double n) : type_(number_type) { + u_.number_ = n; + } + + inline value::value(const std::string& s) : type_(string_type) { + u_.string_ = new std::string(s); + } + + inline value::value(const array& a) : type_(array_type) { + u_.array_ = new array(a); + } + + inline value::value(const object& o) : type_(object_type) { + u_.object_ = new object(o); + } + + inline value::value(const char* s) : type_(string_type) { + u_.string_ = new std::string(s); + } + + inline value::value(const char* s, size_t len) : type_(string_type) { + u_.string_ = new std::string(s, len); + } + + inline value::~value() { + switch (type_) { +#define DEINIT(p) case p##type: delete u_.p; break + DEINIT(string_); + DEINIT(array_); + DEINIT(object_); +#undef DEINIT + default: break; + } + } + + inline value::value(const value& x) : type_(x.type_) { + switch (type_) { +#define INIT(p, v) case p##type: u_.p = v; break + INIT(string_, new std::string(*x.u_.string_)); + INIT(array_, new array(*x.u_.array_)); + INIT(object_, new object(*x.u_.object_)); +#undef INIT + default: + u_ = x.u_; + break; + } + } + + inline value& value::operator=(const value& x) { + if (this != &x) { + this->~value(); + new (this) value(x); + } + return *this; + } + + inline void value::swap(value& x) { + std::swap(type_, x.type_); + std::swap(u_, x.u_); + } + +#define IS(ctype, jtype) \ + template <> inline bool value::is() const { \ + return type_ == jtype##_type; \ + } + IS(null, null) + IS(bool, boolean) + IS(int, number) + IS(double, number) + IS(std::string, string) + IS(array, array) + IS(object, object) +#undef IS + +#define GET(ctype, var) \ + template <> inline const ctype& value::get() const { \ + assert("type mismatch! call vis() before get()" \ + && is()); \ + return var; \ + } \ + template <> inline ctype& value::get() { \ + assert("type mismatch! call is() before get()" \ + && is()); \ + return var; \ + } + GET(bool, u_.boolean_) + GET(double, u_.number_) + GET(std::string, *u_.string_) + GET(array, *u_.array_) + GET(object, *u_.object_) +#undef GET + + inline bool value::evaluate_as_boolean() const { + switch (type_) { + case null_type: + return false; + case boolean_type: + return u_.boolean_; + case number_type: + return u_.number_ != 0; + case string_type: + return ! u_.string_->empty(); + default: + return true; + } + } + + inline const value& value::get(size_t idx) const { + static value s_null; + assert(is()); + return idx < u_.array_->size() ? (*u_.array_)[idx] : s_null; + } + + inline const value& value::get(const std::string& key) const { + static value s_null; + assert(is()); + object::const_iterator i = u_.object_->find(key); + return i != u_.object_->end() ? i->second : s_null; + } + + inline bool value::contains(size_t idx) const { + assert(is()); + return idx < u_.array_->size(); + } + + inline bool value::contains(const std::string& key) const { + assert(is()); + object::const_iterator i = u_.object_->find(key); + return i != u_.object_->end(); + } + + inline std::string value::to_str() const { + switch (type_) { + case null_type: return "null"; + case boolean_type: return u_.boolean_ ? "true" : "false"; + case number_type: { + char buf[256]; + double tmp; + SNPRINTF(buf, sizeof(buf), fabs(u_.number_) < (1ULL << 53) && modf(u_.number_, &tmp) == 0 ? "%.f" : "%.17g", u_.number_); + return buf; + } + case string_type: return *u_.string_; + case array_type: return "array"; + case object_type: return "object"; + default: assert(0); +#ifdef _MSC_VER + __assume(0); +#endif + } + return std::string(); + } + + template void copy(const std::string& s, Iter oi) { + std::copy(s.begin(), s.end(), oi); + } + + template void serialize_str(const std::string& s, Iter oi) { + *oi++ = '"'; + for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) { + switch (*i) { +#define MAP(val, sym) case val: copy(sym, oi); break + MAP('"', "\\\""); + MAP('\\', "\\\\"); + MAP('/', "\\/"); + MAP('\b', "\\b"); + MAP('\f', "\\f"); + MAP('\n', "\\n"); + MAP('\r', "\\r"); + MAP('\t', "\\t"); +#undef MAP + default: + if ((unsigned char)*i < 0x20 || *i == 0x7f) { + char buf[7]; + SNPRINTF(buf, sizeof(buf), "\\u%04x", *i & 0xff); + copy(buf, buf + 6, oi); + } else { + *oi++ = *i; + } + break; + } + } + *oi++ = '"'; + } + + template void value::serialize(Iter oi) const { + switch (type_) { + case string_type: + serialize_str(*u_.string_, oi); + break; + case array_type: { + *oi++ = '['; + for (array::const_iterator i = u_.array_->begin(); + i != u_.array_->end(); + ++i) { + if (i != u_.array_->begin()) { + *oi++ = ','; + } + i->serialize(oi); + } + *oi++ = ']'; + break; + } + case object_type: { + *oi++ = '{'; + for (object::const_iterator i = u_.object_->begin(); + i != u_.object_->end(); + ++i) { + if (i != u_.object_->begin()) { + *oi++ = ','; + } + serialize_str(i->first, oi); + *oi++ = ':'; + i->second.serialize(oi); + } + *oi++ = '}'; + break; + } + default: + copy(to_str(), oi); + break; + } + } + + inline std::string value::serialize() const { + std::string s; + serialize(std::back_inserter(s)); + return s; + } + + template class input { + protected: + Iter cur_, end_; + int last_ch_; + bool ungot_; + int line_; + public: + input(const Iter& first, const Iter& last) : cur_(first), end_(last), last_ch_(-1), ungot_(false), line_(1) {} + int getc() { + if (ungot_) { + ungot_ = false; + return last_ch_; + } + if (cur_ == end_) { + last_ch_ = -1; + return -1; + } + if (last_ch_ == '\n') { + line_++; + } + last_ch_ = *cur_++ & 0xff; + return last_ch_; + } + void ungetc() { + if (last_ch_ != -1) { + assert(! ungot_); + ungot_ = true; + } + } + Iter cur() const { return cur_; } + int line() const { return line_; } + void skip_ws() { + while (1) { + int ch = getc(); + if (! (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r')) { + ungetc(); + break; + } + } + } + bool expect(int expect) { + skip_ws(); + if (getc() != expect) { + ungetc(); + return false; + } + return true; + } + bool match(const std::string& pattern) { + for (std::string::const_iterator pi(pattern.begin()); + pi != pattern.end(); + ++pi) { + if (getc() != *pi) { + ungetc(); + return false; + } + } + return true; + } + }; + + template inline int _parse_quadhex(input &in) { + int uni_ch = 0, hex; + for (int i = 0; i < 4; i++) { + if ((hex = in.getc()) == -1) { + return -1; + } + if ('0' <= hex && hex <= '9') { + hex -= '0'; + } else if ('A' <= hex && hex <= 'F') { + hex -= 'A' - 0xa; + } else if ('a' <= hex && hex <= 'f') { + hex -= 'a' - 0xa; + } else { + in.ungetc(); + return -1; + } + uni_ch = uni_ch * 16 + hex; + } + return uni_ch; + } + + template inline bool _parse_codepoint(String& out, input& in) { + int uni_ch; + if ((uni_ch = _parse_quadhex(in)) == -1) { + return false; + } + if (0xd800 <= uni_ch && uni_ch <= 0xdfff) { + if (0xdc00 <= uni_ch) { + // a second 16-bit of a surrogate pair appeared + return false; + } + // first 16-bit of surrogate pair, get the next one + if (in.getc() != '\\' || in.getc() != 'u') { + in.ungetc(); + return false; + } + int second = _parse_quadhex(in); + if (! (0xdc00 <= second && second <= 0xdfff)) { + return false; + } + uni_ch = ((uni_ch - 0xd800) << 10) | ((second - 0xdc00) & 0x3ff); + uni_ch += 0x10000; + } + if (uni_ch < 0x80) { + out.push_back(uni_ch); + } else { + if (uni_ch < 0x800) { + out.push_back(0xc0 | (uni_ch >> 6)); + } else { + if (uni_ch < 0x10000) { + out.push_back(0xe0 | (uni_ch >> 12)); + } else { + out.push_back(0xf0 | (uni_ch >> 18)); + out.push_back(0x80 | ((uni_ch >> 12) & 0x3f)); + } + out.push_back(0x80 | ((uni_ch >> 6) & 0x3f)); + } + out.push_back(0x80 | (uni_ch & 0x3f)); + } + return true; + } + + template inline bool _parse_string(String& out, input& in) { + while (1) { + int ch = in.getc(); + if (ch < ' ') { + in.ungetc(); + return false; + } else if (ch == '"') { + return true; + } else if (ch == '\\') { + if ((ch = in.getc()) == -1) { + return false; + } + switch (ch) { +#define MAP(sym, val) case sym: out.push_back(val); break + MAP('"', '\"'); + MAP('\\', '\\'); + MAP('/', '/'); + MAP('b', '\b'); + MAP('f', '\f'); + MAP('n', '\n'); + MAP('r', '\r'); + MAP('t', '\t'); +#undef MAP + case 'u': + if (! _parse_codepoint(out, in)) { + return false; + } + break; + default: + return false; + } + } else { + out.push_back(ch); + } + } + return false; + } + + template inline bool _parse_array(Context& ctx, input& in) { + if (! ctx.parse_array_start()) { + return false; + } + if (in.expect(']')) { + return true; + } + size_t idx = 0; + do { + if (! ctx.parse_array_item(in, idx)) { + return false; + } + idx++; + } while (in.expect(',')); + return in.expect(']'); + } + + template inline bool _parse_object(Context& ctx, input& in) { + if (! ctx.parse_object_start()) { + return false; + } + if (in.expect('}')) { + return true; + } + do { + std::string key; + if (! in.expect('"') + || ! _parse_string(key, in) + || ! in.expect(':')) { + return false; + } + if (! ctx.parse_object_item(in, key)) { + return false; + } + } while (in.expect(',')); + return in.expect('}'); + } + + template inline bool _parse_number(double& out, input& in) { + std::string num_str; + while (1) { + int ch = in.getc(); + if (('0' <= ch && ch <= '9') || ch == '+' || ch == '-' || ch == '.' + || ch == 'e' || ch == 'E') { + num_str.push_back(ch); + } else { + in.ungetc(); + break; + } + } + char* endp; + out = strtod(num_str.c_str(), &endp); + return endp == num_str.c_str() + num_str.size(); + } + + template inline bool _parse(Context& ctx, input& in) { + in.skip_ws(); + int ch = in.getc(); + switch (ch) { +#define IS(ch, text, op) case ch: \ + if (in.match(text) && op) { \ + return true; \ + } else { \ + return false; \ + } + IS('n', "ull", ctx.set_null()); + IS('f', "alse", ctx.set_bool(false)); + IS('t', "rue", ctx.set_bool(true)); +#undef IS + case '"': + return ctx.parse_string(in); + case '[': + return _parse_array(ctx, in); + case '{': + return _parse_object(ctx, in); + default: + if (('0' <= ch && ch <= '9') || ch == '-') { + in.ungetc(); + double f; + if (_parse_number(f, in)) { + ctx.set_number(f); + return true; + } else { + return false; + } + } + break; + } + in.ungetc(); + return false; + } + + class deny_parse_context { + public: + bool set_null() { return false; } + bool set_bool(bool) { return false; } + bool set_number(double) { return false; } + template bool parse_string(input&) { return false; } + bool parse_array_start() { return false; } + template bool parse_array_item(input&, size_t) { + return false; + } + bool parse_object_start() { return false; } + template bool parse_object_item(input&, const std::string&) { + return false; + } + }; + + class default_parse_context { + protected: + value* out_; + public: + default_parse_context(value* out) : out_(out) {} + bool set_null() { + *out_ = value(); + return true; + } + bool set_bool(bool b) { + *out_ = value(b); + return true; + } + bool set_number(double f) { + *out_ = value(f); + return true; + } + template bool parse_string(input& in) { + *out_ = value(string_type, false); + return _parse_string(out_->get(), in); + } + bool parse_array_start() { + *out_ = value(array_type, false); + return true; + } + template bool parse_array_item(input& in, size_t) { + array& a = out_->get(); + a.push_back(value()); + default_parse_context ctx(&a.back()); + return _parse(ctx, in); + } + bool parse_object_start() { + *out_ = value(object_type, false); + return true; + } + template bool parse_object_item(input& in, const std::string& key) { + object& o = out_->get(); + default_parse_context ctx(&o[key]); + return _parse(ctx, in); + } + private: + default_parse_context(const default_parse_context&); + default_parse_context& operator=(const default_parse_context&); + }; + + class null_parse_context { + public: + struct dummy_str { + void push_back(int) {} + }; + public: + null_parse_context() {} + bool set_null() { return true; } + bool set_bool(bool) { return true; } + bool set_number(double) { return true; } + template bool parse_string(input& in) { + dummy_str s; + return _parse_string(s, in); + } + bool parse_array_start() { return true; } + template bool parse_array_item(input& in, size_t) { + return _parse(*this, in); + } + bool parse_object_start() { return true; } + template bool parse_object_item(input& in, const std::string&) { + return _parse(*this, in); + } + private: + null_parse_context(const null_parse_context&); + null_parse_context& operator=(const null_parse_context&); + }; + + // obsolete, use the version below + template inline std::string parse(value& out, Iter& pos, const Iter& last) { + std::string err; + pos = parse(out, pos, last, &err); + return err; + } + + template inline Iter _parse(Context& ctx, const Iter& first, const Iter& last, std::string* err) { + input in(first, last); + if (! _parse(ctx, in) && err != NULL) { + char buf[64]; + SNPRINTF(buf, sizeof(buf), "syntax error at line %d near: ", in.line()); + *err = buf; + while (1) { + int ch = in.getc(); + if (ch == -1 || ch == '\n') { + break; + } else if (ch >= ' ') { + err->push_back(ch); + } + } + } + return in.cur(); + } + + template inline Iter parse(value& out, const Iter& first, const Iter& last, std::string* err) { + default_parse_context ctx(&out); + return _parse(ctx, first, last, err); + } + + inline std::string parse(value& out, std::istream& is) { + std::string err; + parse(out, std::istreambuf_iterator(is.rdbuf()), + std::istreambuf_iterator(), &err); + return err; + } + + template struct last_error_t { + static std::string s; + }; + template std::string last_error_t::s; + + inline void set_last_error(const std::string& s) { + last_error_t::s = s; + } + + inline const std::string& get_last_error() { + return last_error_t::s; + } + + inline bool operator==(const value& x, const value& y) { + if (x.is()) + return y.is(); +#define PICOJSON_CMP(type) \ + if (x.is()) \ + return y.is() && x.get() == y.get() + PICOJSON_CMP(bool); + PICOJSON_CMP(double); + PICOJSON_CMP(std::string); + PICOJSON_CMP(array); + PICOJSON_CMP(object); +#undef PICOJSON_CMP + assert(0); +#ifdef _MSC_VER + __assume(0); +#endif + return false; + } + + inline bool operator!=(const value& x, const value& y) { + return ! (x == y); + } +} + +namespace std { + template<> inline void swap(picojson::value& x, picojson::value& y) + { + x.swap(y); + } +} + +inline std::istream& operator>>(std::istream& is, picojson::value& x) +{ + picojson::set_last_error(std::string()); + std::string err = picojson::parse(x, is); + if (! err.empty()) { + picojson::set_last_error(err); + is.setstate(std::ios::failbit); + } + return is; +} + +inline std::ostream& operator<<(std::ostream& os, const picojson::value& x) +{ + x.serialize(std::ostream_iterator(os)); + return os; +} +#ifdef _MSC_VER + #pragma warning(pop) +#endif + +#endif +#ifdef TEST_PICOJSON +#ifdef _MSC_VER + #pragma warning(disable : 4127) // conditional expression is constant +#endif + +using namespace std; + +static void plan(int num) +{ + printf("1..%d\n", num); +} + +static bool success = true; + +static void ok(bool b, const char* name = "") +{ + static int n = 1; + if (! b) + success = false; + printf("%s %d - %s\n", b ? "ok" : "ng", n++, name); +} + +template void is(const T& x, const T& y, const char* name = "") +{ + if (x == y) { + ok(true, name); + } else { + ok(false, name); + } +} + +#include +#include +#include +#include + +int main(void) +{ + plan(85); + + // constructors +#define TEST(expr, expected) \ + is(picojson::value expr .serialize(), string(expected), "picojson::value" #expr) + + TEST( (true), "true"); + TEST( (false), "false"); + TEST( (42.0), "42"); + TEST( (string("hello")), "\"hello\""); + TEST( ("hello"), "\"hello\""); + TEST( ("hello", 4), "\"hell\""); + + { + double a = 1; + for (int i = 0; i < 1024; i++) { + picojson::value vi(a); + std::stringstream ss; + ss << vi; + picojson::value vo; + ss >> vo; + double b = vo.get(); + if ((i < 53 && a != b) || fabs(a - b) / b > 1e-8) { + printf("ng i=%d a=%.18e b=%.18e\n", i, a, b); + } + a *= 2; + } + } + +#undef TEST + +#define TEST(in, type, cmp, serialize_test) { \ + picojson::value v; \ + const char* s = in; \ + string err = picojson::parse(v, s, s + strlen(s)); \ + ok(err.empty(), in " no error"); \ + ok(v.is(), in " check type"); \ + is(v.get(), cmp, in " correct output"); \ + is(*s, '\0', in " read to eof"); \ + if (serialize_test) { \ + is(v.serialize(), string(in), in " serialize"); \ + } \ + } + TEST("false", bool, false, true); + TEST("true", bool, true, true); + TEST("90.5", double, 90.5, false); + TEST("1.7976931348623157e+308", double, DBL_MAX, false); + TEST("\"hello\"", string, string("hello"), true); + TEST("\"\\\"\\\\\\/\\b\\f\\n\\r\\t\"", string, string("\"\\/\b\f\n\r\t"), + true); + TEST("\"\\u0061\\u30af\\u30ea\\u30b9\"", string, + string("a\xe3\x82\xaf\xe3\x83\xaa\xe3\x82\xb9"), false); + TEST("\"\\ud840\\udc0b\"", string, string("\xf0\xa0\x80\x8b"), false); +#undef TEST + +#define TEST(type, expr) { \ + picojson::value v; \ + const char *s = expr; \ + string err = picojson::parse(v, s, s + strlen(s)); \ + ok(err.empty(), "empty " #type " no error"); \ + ok(v.is(), "empty " #type " check type"); \ + ok(v.get().empty(), "check " #type " array size"); \ + } + TEST(array, "[]"); + TEST(object, "{}"); +#undef TEST + + { + picojson::value v; + const char *s = "[1,true,\"hello\"]"; + string err = picojson::parse(v, s, s + strlen(s)); + ok(err.empty(), "array no error"); + ok(v.is(), "array check type"); + is(v.get().size(), size_t(3), "check array size"); + ok(v.contains(0), "check contains array[0]"); + ok(v.get(0).is(), "check array[0] type"); + is(v.get(0).get(), 1.0, "check array[0] value"); + ok(v.contains(1), "check contains array[1]"); + ok(v.get(1).is(), "check array[1] type"); + ok(v.get(1).get(), "check array[1] value"); + ok(v.contains(2), "check contains array[2]"); + ok(v.get(2).is(), "check array[2] type"); + is(v.get(2).get(), string("hello"), "check array[2] value"); + ok(!v.contains(3), "check not contains array[3]"); + } + + { + picojson::value v; + const char *s = "{ \"a\": true }"; + string err = picojson::parse(v, s, s + strlen(s)); + ok(err.empty(), "object no error"); + ok(v.is(), "object check type"); + is(v.get().size(), size_t(1), "check object size"); + ok(v.contains("a"), "check contains property"); + ok(v.get("a").is(), "check bool property exists"); + is(v.get("a").get(), true, "check bool property value"); + is(v.serialize(), string("{\"a\":true}"), "serialize object"); + ok(!v.contains("z"), "check not contains property"); + } + +#define TEST(json, msg) do { \ + picojson::value v; \ + const char *s = json; \ + string err = picojson::parse(v, s, s + strlen(s)); \ + is(err, string("syntax error at line " msg), msg); \ + } while (0) + TEST("falsoa", "1 near: oa"); + TEST("{]", "1 near: ]"); + TEST("\n\bbell", "2 near: bell"); + TEST("\"abc\nd\"", "1 near: "); +#undef TEST + + { + picojson::value v1, v2; + const char *s; + string err; + s = "{ \"b\": true, \"a\": [1,2,\"three\"], \"d\": 2 }"; + err = picojson::parse(v1, s, s + strlen(s)); + s = "{ \"d\": 2.0, \"b\": true, \"a\": [1,2,\"three\"] }"; + err = picojson::parse(v2, s, s + strlen(s)); + ok((v1 == v2), "check == operator in deep comparison"); + } + + { + picojson::value v1, v2; + const char *s; + string err; + s = "{ \"b\": true, \"a\": [1,2,\"three\"], \"d\": 2 }"; + err = picojson::parse(v1, s, s + strlen(s)); + s = "{ \"d\": 2.0, \"a\": [1,\"three\"], \"b\": true }"; + err = picojson::parse(v2, s, s + strlen(s)); + ok((v1 != v2), "check != operator for array in deep comparison"); + } + + { + picojson::value v1, v2; + const char *s; + string err; + s = "{ \"b\": true, \"a\": [1,2,\"three\"], \"d\": 2 }"; + err = picojson::parse(v1, s, s + strlen(s)); + s = "{ \"d\": 2.0, \"a\": [1,2,\"three\"], \"b\": false }"; + err = picojson::parse(v2, s, s + strlen(s)); + ok((v1 != v2), "check != operator for object in deep comparison"); + } + + { + picojson::value v1, v2; + const char *s; + string err; + s = "{ \"b\": true, \"a\": [1,2,\"three\"], \"d\": 2 }"; + err = picojson::parse(v1, s, s + strlen(s)); + picojson::object& o = v1.get(); + o.erase("b"); + picojson::array& a = o["a"].get(); + picojson::array::iterator i; + i = std::remove(a.begin(), a.end(), picojson::value(std::string("three"))); + a.erase(i, a.end()); + s = "{ \"a\": [1,2], \"d\": 2 }"; + err = picojson::parse(v2, s, s + strlen(s)); + ok((v1 == v2), "check erase()"); + } + + ok(picojson::value(3.0).serialize() == "3", + "integral number should be serialized as a integer"); + + { + const char* s = "{ \"a\": [1,2], \"d\": 2 }"; + picojson::null_parse_context ctx; + string err; + picojson::_parse(ctx, s, s + strlen(s), &err); + ok(err.empty(), "null_parse_context"); + } + + { + picojson::value v1, v2; + v1 = picojson::value(true); + swap(v1, v2); + ok(v1.is(), "swap (null)"); + ok(v2.get() == true, "swap (bool)"); + + v1 = picojson::value("a"); + v2 = picojson::value(1.0); + swap(v1, v2); + ok(v1.get() == 1.0, "swap (dobule)"); + ok(v2.get() == "a", "swap (string)"); + + v1 = picojson::value(picojson::object()); + v2 = picojson::value(picojson::array()); + swap(v1, v2); + ok(v1.is(), "swap (array)"); + ok(v2.is(), "swap (object)"); + } + + return success ? 0 : 1; +} + +#endif diff --git a/modules/tizen-application/tizen-application_api.js b/modules/tizen-application/tizen-application_api.js new file mode 100755 index 0000000..db4ae11 --- /dev/null +++ b/modules/tizen-application/tizen-application_api.js @@ -0,0 +1,211 @@ +'use strict'; + +var async_message_id = 0; +var async_map = new Map(); + +function native_sync_call(method, parameter) { + var args = {}; + args['cmd'] = method; + args = Object.assign(args, parameter); + try { + return JSON.parse(extension.internal.sendSyncMessage(JSON.stringify(args))); + } catch (e) { + console.log(e.message); + return {}; + } +} + +function native_async_call(method, parameter, cb) { + var args = {}; + args['cmd'] = method; + args = Object.assign(args, parameter); + var asyncid = async_message_id++; + args['asyncid'] = 'asyncid_' + asyncid; + async_map.set(args['asyncid'], cb); + extension.postMessage(JSON.stringify(args)); +} + +function registEventHandler(app) { + var handler = (function(self) { + return function(json) { + var msg = JSON.parse(json); + if (msg['asyncid'] && async_map.has(msg['asyncid'])) { + var cb = async_map.get(msg['asyncid']); + async_map.delete(msg['asyncid']); + if (cb instanceof Function) { + cb(msg); + } else { + console.log('cb is not function'); + } + } else { + self.__event_handle__(msg); + } + }; + })(app); + extension.setMessageListener(handler); +} + +var ApplicationCommon = require('tizen-application-common').ApplicationCommon; + + +var started = Symbol(); +/** + * @class Application +* @extends ApplicationCommon + */ +class Application extends ApplicationCommon { + constructor() { + super(); + this[started] = false; + registEventHandler(this); + } + + __event_handle__(msg) { + if (msg['event'] == 'appcontrol') { + var RequestedAppControl = + require('tizen-application-common').RequestedAppControl; + var json = JSON.parse(msg['data']); + var request = new RequestedAppControl(json); + this.emit('appcontrol', request); + } else if ( + [ + 'pause', 'resume', 'terminate', 'languagechange', 'lowmemory', + 'lowbattery', 'regionchange', 'orientationchange' + ].indexOf(msg['event']) >= 0) { + this.emit(msg['event'], msg['data']); + } else { + console.log('invalid event was passed'); + } + } + + /** + * @method start + * @return {Promise<>} + */ + start() { + if (this[started]) + return Promise.reject(new Error('already started')); + + this[started] = true; + return new Promise(function(resolve, reject) { + require('gcontext').init(); + process.title = process.argv[1]; + var args = {}; + args['argv'] = process.argv.slice(1); + native_async_call('start', args, function(result) { + if (result['result'] == 'OK') { + resolve(); + } else { + reject(new Error(result['reason'])); + } + }); + }); + } + + /** + * .... + * @event appcontrol + * @param {AppControl} appcontrol + * @since 3.0 + */ + + /** + * .... + * @event pause + * @since 3.0 + */ + + /** + * .... + * @event resume + * @since 3.0 + */ + + /** + * .... + * @event terminate + * @since 3.0 + */ + + /** + * .... + * @event lowmemory + * @param {string} status + * * "NORMAL" + * * "SOFT_WARNING" + * * "HARD_WARNING" + * @since 3.0 + */ + + + /** + * .... + * @event lowbattery + * @param {string} status + * * "POWER_OFF " + * * "CRITICAL_LOW" + * @since 3.0 + */ + + /** + * .... + * @event languagechange + * + * @since 3.0 + */ + + /** + * .... + * @event regionchange + * @param {string} language + * + * @since 3.0 + */ + + /** + * .... + * @event orientationchange + * @param {string} orientation + * + * @since 3.0 + */ + +}; + + +/* + * @module tizen-application + * + * 'tizen-application' module exports a instance of Application class. + * + * ``` + * var AppControl = require('tizeen-app-control'); + * + * var app = require('tizen-application'); + * app.on('appcontrol', function(requested){ + * if (requested.operation == AppControl.OPERATION_MAIN) { + * // main .. + * } else { + * .... + * } + * }); + * + * app.on('pause', function() { + * // pause ... + * }); + * + * app.on('terminate', function(){ + * // release resources + * }); + * + * app.start().then(function() { + * console.log(app.name); + * console.log(app.id); + * + * }).catch(function(e){ + * console.log(e.message); + * }); + * + * ``` + */ +exports = new Application(); diff --git a/modules/tizen-application/ui_app_extension.cc b/modules/tizen-application/ui_app_extension.cc new file mode 100755 index 0000000..5c8ce8b --- /dev/null +++ b/modules/tizen-application/ui_app_extension.cc @@ -0,0 +1,132 @@ +/* + * 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 "ui_app_extension.h" + +#include + +#include "picojson.h" +#include "appfw.h" + +#ifdef LOG_TAG +#undef LOG_TAG +#endif +#define LOG_TAG "JSNative" + +namespace appfw { + +xwalk::XWalkExtensionInstance* UiAppExtension::CreateInstance() { + return new UiAppInstance(); +} + +UiAppInstance::UiAppInstance() { +} + +UiAppInstance::~UiAppInstance() { + AppFW::GetInstance()->Deinit(); +} + +void UiAppInstance::Initialize() { + LOGD("Created tizen-application instance"); + AppFW::GetInstance()->set_terminate_handler([this]() { + FireSimpleEvent("terminate"); + }); + AppFW::GetInstance()->set_pause_handler([this]() { + FireSimpleEvent("pause"); + }); + AppFW::GetInstance()->set_resume_handler([this]() { + FireSimpleEvent("resume"); + }); + AppFW::GetInstance()->set_service_handler([this](const std::string& json) { + FireSimpleEvent("appcontrol", json); + }); +} + +void UiAppInstance::HandleMessage(const char* msg) { + picojson::value value; + std::string err; + picojson::parse(value, msg, msg + strlen(msg), &err); + if (!err.empty()) { + LOGE("Ignoring message. Can't parse msessage : %s", err.c_str()); + return; + } + if (!value.is()) { + LOGE("Ignoring message. It is not an object."); + return; + } + + auto& request = value.get(); + auto found = request.find("asyncid"); + if (found == request.end()) { + LOGE("asyncid was not existed"); + return; + } + + auto cmd = request["cmd"].to_str(); + + if (cmd == "start") { + auto& argv = request["argv"]; + std::vector argv_pointer; + if (argv.is()) { + auto& argv_array = argv.get(); + for (auto& item : argv_array) { + argv_pointer.push_back(strdup(item.to_str().c_str())); + } + } + HandleStart(request["asyncid"].to_str(), + argv_pointer.size(), argv_pointer.data()); + for (auto& item : argv_pointer) { + free(item); + } + } + // parse json object +} + +void UiAppInstance::HandleSyncMessage(const char* msg) { + // parse json object +} + +void UiAppInstance::FireSimpleEvent(const std::string& event, + const std::string& data) { + picojson::value::object obj; + obj["event"] = picojson::value(event); + if (data.length() > 0) { + obj["data"] = picojson::value(data); + } + PostMessage(picojson::value(obj).serialize().c_str()); +} + +void UiAppInstance::HandleStart(const std::string& async_id, + int argc, char** argv) { + AppFW::GetInstance()->set_create_handler([async_id, this]() { + picojson::value::object obj; + obj["asyncid"] = picojson::value(async_id); + obj["result"] = picojson::value("OK"); + PostMessage(picojson::value(obj).serialize().c_str()); + AppFW::GetInstance()->set_create_handler(nullptr); + }); + if(!AppFW::GetInstance()->Init(argc, argv)) { + picojson::value::object obj; + obj["asyncid"] = picojson::value(async_id); + obj["result"] = picojson::value("Fail"); + obj["reason"] = picojson::value("Already started"); + PostMessage(picojson::value(obj).serialize().c_str()); + } +} + +} // namespace appfw + +EXPORT_XWALK_EXTENSION(tizen_application, appfw::UiAppExtension); diff --git a/modules/tizen-application/ui_app_extension.h b/modules/tizen-application/ui_app_extension.h new file mode 100755 index 0000000..4e7240a --- /dev/null +++ b/modules/tizen-application/ui_app_extension.h @@ -0,0 +1,52 @@ +/* + * 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 UI_APP_EXTENSION_H_ +#define UI_APP_EXTENSION_H_ + +#include +#include + +namespace appfw { + +class UiAppExtension : public xwalk::XWalkExtension { + public: + // @override + xwalk::XWalkExtensionInstance* CreateInstance(); +}; + +class UiAppInstance : public xwalk::XWalkExtensionInstance { + public: + UiAppInstance(); + virtual ~UiAppInstance(); + // @override + void Initialize(); + + // @override + void HandleMessage(const char* msg); + + // @override + void HandleSyncMessage(const char* msg); + private: + void FireSimpleEvent(const std::string& event, + const std::string& data = std::string()); + void HandleStart(const std::string& aync_id, int argc, char** argv); +}; + +} // namespace appfw + +#endif // UI_APP_EXTENSION_H_ + -- 2.7.4