// found in the LICENSE file.
//Object xwalk.JSON - guaranteed to not being modified by the application programmer
-var JSON_ = {stringify: JSON.stringify, parse: JSON.parse};
+var JSON_ = { stringify: JSON.stringify, parse: JSON.parse };
Object.freeze(JSON_);
exports.JSON = JSON_;
var _global = {};
if (typeof window != 'undefined') {
- _global = window;
-}
-else if (typeof global != 'undefined') {
- _global = global;
+ _global = window;
+} else if (typeof global != 'undefined') {
+ _global = global;
}
/**
* @deprecated Used only by validateArguments()
*/
var signature_to_type = {
- 'n': 'number',
- 'f': 'function',
- 'b': 'boolean',
- 's': 'string',
- 'o': 'object'
+ n: 'number',
+ f: 'function',
+ b: 'boolean',
+ s: 'string',
+ o: 'object'
};
var DateConverter = function() {};
DateConverter.prototype.toTZDate = function(v, isAllDay) {
- if (typeof v === 'number') {
- v = {
- UTCTimestamp: v
- };
- isAllDay = false;
- }
+ if (typeof v === 'number') {
+ v = {
+ UTCTimestamp: v
+ };
+ isAllDay = false;
+ }
- if (!(v instanceof _global.Object)) {
- return v;
- }
+ if (!(v instanceof _global.Object)) {
+ return v;
+ }
- if (isAllDay) {
- return new tizen.TZDate(v.year, v.month - 1, v.day,
- null, null, null, null, v.timezone || null);
- } else {
- return new tizen.TZDate(new Date(v.UTCTimestamp * 1000));
- }
+ if (isAllDay) {
+ return new tizen.TZDate(
+ v.year,
+ v.month - 1,
+ v.day,
+ null,
+ null,
+ null,
+ null,
+ v.timezone || null
+ );
+ } else {
+ return new tizen.TZDate(new Date(v.UTCTimestamp * 1000));
+ }
};
DateConverter.prototype.fromTZDate = function(v) {
- if (!tizen.TZDate || !(v instanceof tizen.TZDate)) {
- return v;
- }
-
- return {
- year: v.getFullYear(),
- month: v.getMonth(),
- day: v.getDate(),
- timezone: v.getTimezone(),
- UTCTimestamp: v._utcTimestamp / 1000
- };
+ if (!tizen.TZDate || !(v instanceof tizen.TZDate)) {
+ return v;
+ }
+ return {
+ year: v.getFullYear(),
+ month: v.getMonth(),
+ day: v.getDate(),
+ timezone: v.getTimezone(),
+ UTCTimestamp: v._utcTimestamp / 1000
+ };
};
var _dateConverter = new DateConverter();
/** @constructor */
function Utils() {
-
- /**
- * Cynara(since tizen 3.0) only support native privilege.
- * simply web privilege convert native privilege for checking access.
- */
- var privilege = {
- ACCOUNT_READ: 'http://tizen.org/privilege/account.read',
- ACCOUNT_WRITE: 'http://tizen.org/privilege/account.write',
- ALARM: 'http://tizen.org/privilege/alarm.get',
- APPLICATION_INFO: 'http://tizen.org/privilege/application.info',
- APPLICATION_LAUNCH: 'http://tizen.org/privilege/application.launch',
- APPMANAGER_CERTIFICATE: 'http://tizen.org/privilege/appmanager.certificate',
- APPMANAGER_KILL: 'http://tizen.org/privilege/appmanager.kill',
- BLUETOOTH_ADMIN: 'http://tizen.org/privilege/bluetooth.admin',
- BLUETOOTH_GAP: 'http://tizen.org/privilege/bluetooth.gap',
- BLUETOOTH_HEALTH: 'http://tizen.org/privilege/bluetooth.health',
- BLUETOOTH_SPP: 'http://tizen.org/privilege/bluetooth.spp',
- BLUETOOTHMANAGER: 'http://tizen.org/privilege/bluetoothmanager',
- BLUETOOTH: 'http://tizen.org/privilege/bluetooth',
- BOOKMARK_READ: 'http://tizen.org/privilege/bookmark.read',
- BOOKMARK_WRITE: 'http://tizen.org/privilege/bookmark.write',
- CALENDAR_READ: 'http://tizen.org/privilege/calendar.read',
- CALENDAR_WRITE: 'http://tizen.org/privilege/calendar.write',
- CALLHISTORY_READ: 'http://tizen.org/privilege/callhistory.read',
- CALLHISTORY_WRITE: 'http://tizen.org/privilege/callhistory.write',
- CONTACT_READ: 'http://tizen.org/privilege/contact.read',
- CONTACT_WRITE: 'http://tizen.org/privilege/contact.write',
- CONTENT_READ: 'http://tizen.org/privilege/content.write',
- CONTENT_WRITE: 'http://tizen.org/privilege/content.write',
- DATACONTROL_CONSUMER: 'http://tizen.org/privilege/datacontrol.consumer',
- DATASYNC: 'http://tizen.org/privilege/datasync',
- DOWNLOAD: 'http://tizen.org/privilege/download',
- FILESYSTEM_READ: 'http://tizen.org/privilege/filesystem.read',
- FILESYSTEM_WRITE: 'http://tizen.org/privilege/filesystem.write',
- HAPTIC: 'http://tizen.org/privilege/haptic',
- HEALTHINFO: 'http://tizen.org/privilege/healthinfo',
- INTERNET: 'http://tizen.org/privilege/internet',
- LED: 'http://tizen.org/privilege/led',
- LOCATION: 'http://tizen.org/privilege/location',
- MEDIACONTROLLER_SERVER: 'http://tizen.org/privilege/mediacontroller.server',
- MEDIACONTROLLER_CLIENT: 'http://tizen.org/privilege/mediacontroller.client',
- MESSAGING_READ: 'http://tizen.org/privilege/messaging.read',
- MESSAGING_WRITE: 'http://tizen.org/privilege/messaging.write',
- NETWORKBEARERSELECTION: 'http://tizen.org/privilege/networkbearerselection',
- NFC_ADMIN: 'http://tizen.org/privilege/nfc.admin',
- NFC_CARDEMULATION: 'http://tizen.org/privilege/nfc.cardemulation',
- NFC_COMMON: 'http://tizen.org/privilege/nfc.common',
- NFC_P2P: 'http://tizen.org/privilege/nfc.p2p',
- NFC_TAG: 'http://tizen.org/privilege/nfc.tag',
- NOTIFICATION: 'http://tizen.org/privilege/notification',
- PACKAGE_INFO: 'http://tizen.org/privilege/packagemanager.info',
- PACKAGEMANAGER_INSTALL: 'http://tizen.org/privilege/packagemanager.install',
- POWER: 'http://tizen.org/privilege/power',
- PUSH: 'http://tizen.org/privilege/push',
- SECUREELEMENT: 'http://tizen.org/privilege/secureelement',
- SETTING_ADMIN: 'http://tizen.org/privilege/systemsettings.admin',
- SETTING: 'http://tizen.org/privilege/setting',
- SYSTEM: 'http://tizen.org/privilege/system',
- SYSTEMMANAGER: 'http://tizen.org/privilege/systemmanager',
- TELEPHONY: 'http://tizen.org/privilege/telephony',
- VOLUME_SET: 'http://tizen.org/privilege/volume.set',
- WEBSETTING: 'http://tizen.org/privilege/websetting',
- TV_INPUT_DEVICE: 'http://tizen.org/privilege/tv.inputdevice'
- };
-
- Object.freeze(privilege);
-
- Object.defineProperty(this, 'privilege', {
- value: privilege,
- writable: false,
- enumerable: true,
- configurable: false
- });
+ /**
+ * Cynara(since tizen 3.0) only support native privilege.
+ * simply web privilege convert native privilege for checking access.
+ */
+ var privilege = {
+ ACCOUNT_READ: 'http://tizen.org/privilege/account.read',
+ ACCOUNT_WRITE: 'http://tizen.org/privilege/account.write',
+ ALARM: 'http://tizen.org/privilege/alarm.get',
+ APPLICATION_INFO: 'http://tizen.org/privilege/application.info',
+ APPLICATION_LAUNCH: 'http://tizen.org/privilege/application.launch',
+ APPMANAGER_CERTIFICATE: 'http://tizen.org/privilege/appmanager.certificate',
+ APPMANAGER_KILL: 'http://tizen.org/privilege/appmanager.kill',
+ BLUETOOTH_ADMIN: 'http://tizen.org/privilege/bluetooth.admin',
+ BLUETOOTH_GAP: 'http://tizen.org/privilege/bluetooth.gap',
+ BLUETOOTH_HEALTH: 'http://tizen.org/privilege/bluetooth.health',
+ BLUETOOTH_SPP: 'http://tizen.org/privilege/bluetooth.spp',
+ BLUETOOTHMANAGER: 'http://tizen.org/privilege/bluetoothmanager',
+ BLUETOOTH: 'http://tizen.org/privilege/bluetooth',
+ BOOKMARK_READ: 'http://tizen.org/privilege/bookmark.read',
+ BOOKMARK_WRITE: 'http://tizen.org/privilege/bookmark.write',
+ CALENDAR_READ: 'http://tizen.org/privilege/calendar.read',
+ CALENDAR_WRITE: 'http://tizen.org/privilege/calendar.write',
+ CALLHISTORY_READ: 'http://tizen.org/privilege/callhistory.read',
+ CALLHISTORY_WRITE: 'http://tizen.org/privilege/callhistory.write',
+ CONTACT_READ: 'http://tizen.org/privilege/contact.read',
+ CONTACT_WRITE: 'http://tizen.org/privilege/contact.write',
+ CONTENT_READ: 'http://tizen.org/privilege/content.write',
+ CONTENT_WRITE: 'http://tizen.org/privilege/content.write',
+ DATACONTROL_CONSUMER: 'http://tizen.org/privilege/datacontrol.consumer',
+ DATASYNC: 'http://tizen.org/privilege/datasync',
+ DOWNLOAD: 'http://tizen.org/privilege/download',
+ FILESYSTEM_READ: 'http://tizen.org/privilege/filesystem.read',
+ FILESYSTEM_WRITE: 'http://tizen.org/privilege/filesystem.write',
+ HAPTIC: 'http://tizen.org/privilege/haptic',
+ HEALTHINFO: 'http://tizen.org/privilege/healthinfo',
+ INTERNET: 'http://tizen.org/privilege/internet',
+ LED: 'http://tizen.org/privilege/led',
+ LOCATION: 'http://tizen.org/privilege/location',
+ MEDIACONTROLLER_SERVER: 'http://tizen.org/privilege/mediacontroller.server',
+ MEDIACONTROLLER_CLIENT: 'http://tizen.org/privilege/mediacontroller.client',
+ MESSAGING_READ: 'http://tizen.org/privilege/messaging.read',
+ MESSAGING_WRITE: 'http://tizen.org/privilege/messaging.write',
+ NETWORKBEARERSELECTION: 'http://tizen.org/privilege/networkbearerselection',
+ NFC_ADMIN: 'http://tizen.org/privilege/nfc.admin',
+ NFC_CARDEMULATION: 'http://tizen.org/privilege/nfc.cardemulation',
+ NFC_COMMON: 'http://tizen.org/privilege/nfc.common',
+ NFC_P2P: 'http://tizen.org/privilege/nfc.p2p',
+ NFC_TAG: 'http://tizen.org/privilege/nfc.tag',
+ NOTIFICATION: 'http://tizen.org/privilege/notification',
+ PACKAGE_INFO: 'http://tizen.org/privilege/packagemanager.info',
+ PACKAGEMANAGER_INSTALL: 'http://tizen.org/privilege/packagemanager.install',
+ POWER: 'http://tizen.org/privilege/power',
+ PUSH: 'http://tizen.org/privilege/push',
+ SECUREELEMENT: 'http://tizen.org/privilege/secureelement',
+ SETTING_ADMIN: 'http://tizen.org/privilege/systemsettings.admin',
+ SETTING: 'http://tizen.org/privilege/setting',
+ SYSTEM: 'http://tizen.org/privilege/system',
+ SYSTEMMANAGER: 'http://tizen.org/privilege/systemmanager',
+ TELEPHONY: 'http://tizen.org/privilege/telephony',
+ VOLUME_SET: 'http://tizen.org/privilege/volume.set',
+ WEBSETTING: 'http://tizen.org/privilege/websetting',
+ TV_INPUT_DEVICE: 'http://tizen.org/privilege/tv.inputdevice'
+ };
+
+ Object.freeze(privilege);
+
+ Object.defineProperty(this, 'privilege', {
+ value: privilege,
+ writable: false,
+ enumerable: true,
+ configurable: false
+ });
}
Utils.prototype.error = console.error.bind(console);
Utils.prototype.warn = console.warn.bind(console);
-Utils.prototype.log = _enableJsLogs ? console.log.bind(console) : function(){};
+Utils.prototype.log = _enableJsLogs ? console.log.bind(console) : function() {};
Utils.prototype.global = _global;
Utils.prototype.repackFilter = function(filter) {
- if (filter instanceof tizen.AttributeFilter) {
- return {
- filterType: 'AttributeFilter',
- attributeName: filter.attributeName,
- matchFlag: filter.matchFlag,
- matchValue: _dateConverter.fromTZDate(filter.matchValue)
- };
- }
- if (filter instanceof tizen.AttributeRangeFilter) {
- return {
- filterType: 'AttributeRangeFilter',
- attributeName: filter.attributeName,
- initialValue: _dateConverter.fromTZDate(filter.initialValue),
- endValue: _dateConverter.fromTZDate(filter.endValue)
- };
- }
- if (filter instanceof tizen.CompositeFilter) {
- var _f = [];
- var filters = filter.filters;
-
- for (var i = 0; i < filters.length; ++i) {
- _f.push(this.repackFilter(filters[i]));
+ if (filter instanceof tizen.AttributeFilter) {
+ return {
+ filterType: 'AttributeFilter',
+ attributeName: filter.attributeName,
+ matchFlag: filter.matchFlag,
+ matchValue: _dateConverter.fromTZDate(filter.matchValue)
+ };
}
+ if (filter instanceof tizen.AttributeRangeFilter) {
+ return {
+ filterType: 'AttributeRangeFilter',
+ attributeName: filter.attributeName,
+ initialValue: _dateConverter.fromTZDate(filter.initialValue),
+ endValue: _dateConverter.fromTZDate(filter.endValue)
+ };
+ }
+ if (filter instanceof tizen.CompositeFilter) {
+ var _f = [];
+ var filters = filter.filters;
- return {
- filterType: 'CompositeFilter',
- type: filter.type,
- filters: _f
- };
- }
+ for (var i = 0; i < filters.length; ++i) {
+ _f.push(this.repackFilter(filters[i]));
+ }
- return null;
+ return {
+ filterType: 'CompositeFilter',
+ type: filter.type,
+ filters: _f
+ };
+ }
+
+ return null;
};
/**
* @deprecated You should use xwalk.utils.validator.validateMethod() instead.
*/
Utils.prototype.validateArguments = function(signature, args) {
- var full_args = Array.prototype.slice.call(args);
+ var full_args = Array.prototype.slice.call(args);
- // After '?' everything is optional.
- var mandatory_len = signature.indexOf('?') === -1 ? signature.length : signature.indexOf('?');
+ // After '?' everything is optional.
+ var mandatory_len =
+ signature.indexOf('?') === -1 ? signature.length : signature.indexOf('?');
- if (full_args.length < mandatory_len)
- return false;
+ if (full_args.length < mandatory_len) return false;
- // Mandatory arguments.
- for (var i = 0; i < mandatory_len; i++) {
- if (typeof full_args[i] !== signature_to_type[signature[i]] || full_args[i] === null)
- return false;
- }
+ // Mandatory arguments.
+ for (var i = 0; i < mandatory_len; i++) {
+ if (
+ typeof full_args[i] !== signature_to_type[signature[i]] ||
+ full_args[i] === null
+ )
+ return false;
+ }
- // Optional args may be null.
- for (var i = mandatory_len; i < full_args.length && i < signature.length - 1; i++) {
- if (full_args[i] !== null && typeof full_args[i] !== signature_to_type[signature[i + 1]])
- return false;
- }
+ // Optional args may be null.
+ for (var i = mandatory_len; i < full_args.length && i < signature.length - 1; i++) {
+ if (
+ full_args[i] !== null &&
+ typeof full_args[i] !== signature_to_type[signature[i + 1]]
+ )
+ return false;
+ }
- return true;
+ return true;
};
Utils.prototype.validateObject = function(object, signature, attributes) {
- for (var i = 0; i < signature.length; i++) {
- if (object.hasOwnProperty(attributes[i]) &&
- typeof object[attributes[i]] !== signature_to_type[signature[i]]) {
- return false;
+ for (var i = 0; i < signature.length; i++) {
+ if (
+ object.hasOwnProperty(attributes[i]) &&
+ typeof object[attributes[i]] !== signature_to_type[signature[i]]
+ ) {
+ return false;
+ }
}
- }
- return true;
+ return true;
};
Utils.prototype.getPkgApiVersion = function() {
- var result = native_.callSync('Utils_getPkgApiVersion');
- if (native_.isFailure(result)) {
- throw native_.getErrorObject(result);
- }
- return native_.getResultObject(result);
+ var result = native_.callSync('Utils_getPkgApiVersion');
+ if (native_.isFailure(result)) {
+ throw native_.getErrorObject(result);
+ }
+ return native_.getResultObject(result);
};
Utils.prototype.checkPrivilegeAccess = function(privilege) {
- var result = native_.callSync('Utils_checkPrivilegeAccess', {
- privilege : _toString(privilege),
- });
-
- if (native_.isFailure(result)) {
- throw native_.getErrorObject(result);
- }
-};
-
-Utils.prototype.isAppVersionEarlierThan= function(ver) {
- var app_ver = this.getPkgApiVersion();
-
- var arr_ver = ver.split("."); // reference version
- var arr_app_ver = app_ver.split("."); // application version
- var num_ver;
- var num_app;
-
- var i;
- var length = Math.min(arr_ver.length, arr_app_ver.length);
- for (i = 0; i < length; i++) {
- num_ver = parseInt(arr_ver[i]);
- num_app = parseInt(arr_app_ver[i]);
- if (num_app < num_ver) {
- return true;
- } else if (num_app > num_ver) {
- return false;
+ var result = native_.callSync('Utils_checkPrivilegeAccess', {
+ privilege: _toString(privilege)
+ });
+
+ if (native_.isFailure(result)) {
+ throw native_.getErrorObject(result);
}
- }
+};
- if (arr_ver.length > arr_app_ver.length) {
- return true;
- }
- return false;
-}
+Utils.prototype.isAppVersionEarlierThan = function(ver) {
+ var app_ver = this.getPkgApiVersion();
+
+ var arr_ver = ver.split('.'); // reference version
+ var arr_app_ver = app_ver.split('.'); // application version
+ var num_ver;
+ var num_app;
+
+ var i;
+ var length = Math.min(arr_ver.length, arr_app_ver.length);
+ for (i = 0; i < length; i++) {
+ num_ver = parseInt(arr_ver[i]);
+ num_app = parseInt(arr_app_ver[i]);
+ if (num_app < num_ver) {
+ return true;
+ } else if (num_app > num_ver) {
+ return false;
+ }
+ }
-Utils.prototype.checkPrivilegeAccess4Ver = function(new_ver, new_priv, old_priv) {
- if (!this.isAppVersionEarlierThan(new_ver)) {
- this.checkPrivilegeAccess(new_priv);
- } else if (old_priv != undefined) {
- this.checkPrivilegeAccess(old_priv);
- }
-}
+ if (arr_ver.length > arr_app_ver.length) {
+ return true;
+ }
+ return false;
+};
-Utils.prototype.checkBackwardCompabilityPrivilegeAccess = function(current_privilege, previous_privilege) {
- var result = native_.callSync('Utils_checkBackwardCompabilityPrivilegeAccess', {
- current_privilege : _toString(current_privilege),
- previous_privilege : _toString(previous_privilege),
- });
+Utils.prototype.checkPrivilegeAccess4Ver = function(new_ver, new_priv, old_priv) {
+ if (!this.isAppVersionEarlierThan(new_ver)) {
+ this.checkPrivilegeAccess(new_priv);
+ } else if (old_priv != undefined) {
+ this.checkPrivilegeAccess(old_priv);
+ }
+};
- if (native_.isFailure(result)) {
- throw native_.getErrorObject(result);
- }
+Utils.prototype.checkBackwardCompabilityPrivilegeAccess = function(
+ current_privilege,
+ previous_privilege
+) {
+ var result = native_.callSync('Utils_checkBackwardCompabilityPrivilegeAccess', {
+ current_privilege: _toString(current_privilege),
+ previous_privilege: _toString(previous_privilege)
+ });
+
+ if (native_.isFailure(result)) {
+ throw native_.getErrorObject(result);
+ }
};
Utils.prototype.checkProfile = function() {
- var result = native_.callSync('Utils_checkProfile', {});
+ var result = native_.callSync('Utils_checkProfile', {});
- return native_.getResultObject(result);
+ return native_.getResultObject(result);
};
-
/////////////////////////////////////////////////////////////////////////////
/** @constructor */
var Type = function() {};
Type.prototype.isBoolean = function(obj) {
- return typeof obj === 'boolean';
+ return typeof obj === 'boolean';
};
Type.prototype.isObject = function(obj) {
- return (null !== obj && typeof obj === 'object' && !this.isArray(obj));
+ return null !== obj && typeof obj === 'object' && !this.isArray(obj);
};
Type.prototype.isArray = function(obj) {
- return Array.isArray(obj);
+ return Array.isArray(obj);
};
Type.prototype.isFunction = function(obj) {
- return typeof obj === 'function';
+ return typeof obj === 'function';
};
Type.prototype.isNumber = function(obj) {
- return typeof obj === 'number';
+ return typeof obj === 'number';
};
Type.prototype.isString = function(obj) {
- return typeof obj === 'string';
+ return typeof obj === 'string';
};
Type.prototype.isDate = function(obj) {
- return obj instanceof Date;
+ return obj instanceof Date;
};
Type.prototype.isNull = function(obj) {
- return obj === null;
+ return obj === null;
};
Type.prototype.isNullOrUndefined = function(obj) {
- return (obj === null || obj === undefined);
+ return obj === null || obj === undefined;
};
Type.prototype.isUndefined = function(obj) {
- return obj === void 0;
+ return obj === void 0;
};
Type.prototype.isA = function(obj, type) {
- var clas = Object.prototype.toString.call(obj).slice(8, -1);
- return (obj !== undefined) && (obj !== null) && (clas === type);
+ var clas = Object.prototype.toString.call(obj).slice(8, -1);
+ return obj !== undefined && obj !== null && clas === type;
};
Type.prototype.isEmptyObject = function(obj) {
- for (var property in obj) {
- if (obj.hasOwnProperty(property)) {
- return false;
+ for (var property in obj) {
+ if (obj.hasOwnProperty(property)) {
+ return false;
+ }
}
- }
- return true;
+ return true;
};
Type.prototype.hasProperty = function(obj, prop) {
- return prop in obj;
+ return prop in obj;
};
Type.prototype.arrayContains = function(arr, value) {
- return (arr.indexOf(value) > -1);
+ return arr.indexOf(value) > -1;
};
Type.prototype.getValues = function(obj) {
- var ret = [];
- for (var key in obj) {
- if (obj.hasOwnProperty(key)) {
- ret.push(obj[key]);
+ var ret = [];
+ for (var key in obj) {
+ if (obj.hasOwnProperty(key)) {
+ ret.push(obj[key]);
+ }
}
- }
- return ret;
+ return ret;
};
var _type = new Type();
-
-
/////////////////////////////////////////////////////////////////////////////
/** @constructor */
var Converter = function() {};
function _nullableGeneric(func, nullable, val) {
- if (_type.isNull(val) && nullable === true) {
- return val;
- } else {
- return func.apply(null, [].slice.call(arguments, 2));
- }
+ if (_type.isNull(val) && nullable === true) {
+ return val;
+ } else {
+ return func.apply(null, [].slice.call(arguments, 2));
+ }
}
function _toBoolean(val) {
- return Boolean(val);
+ return Boolean(val);
}
Converter.prototype.toBoolean = function(val, nullable) {
- return _nullableGeneric(_toBoolean, nullable, val);
+ return _nullableGeneric(_toBoolean, nullable, val);
};
function _toLong(val) {
- var ret = parseInt(val);
- return isNaN(ret) ? (val === true ? 1 : 0) : ret;
+ var ret = parseInt(val);
+ return isNaN(ret) ? (val === true ? 1 : 0) : ret;
}
Converter.prototype.toLong = function(val, nullable) {
- return _nullableGeneric(_toLong, nullable, val);
+ return _nullableGeneric(_toLong, nullable, val);
};
function _toLongLong(val) {
- // According to WebIDL specification this will not be a precise representation
- // of requested val. We're converting the val to signed long and then pass it
- // to C++ to get the value in required range.
- return native_.getResultObject(native_.callSync('Utils_toLongLong', {
- n : _toLong(val)
- }));
+ // According to WebIDL specification this will not be a precise representation
+ // of requested val. We're converting the val to signed long and then pass it
+ // to C++ to get the value in required range.
+ return native_.getResultObject(
+ native_.callSync('Utils_toLongLong', {
+ n: _toLong(val)
+ })
+ );
}
Converter.prototype.toLongLong = function(val, nullable) {
- return _nullableGeneric(_toLongLong, nullable, val);
+ return _nullableGeneric(_toLongLong, nullable, val);
};
function _toUnsignedLong(val) {
- return _toLong(val) >>> 0;
+ return _toLong(val) >>> 0;
}
Converter.prototype.toUnsignedLong = function(val, nullable) {
- return _nullableGeneric(_toUnsignedLong, nullable, val);
+ return _nullableGeneric(_toUnsignedLong, nullable, val);
};
function _toUnsignedLongLong(val) {
- // According to WebIDL specification this will not be a precise representation
- // of requested val. We're converting the val to signed long and then pass it
- // to C++ to get the value in required range.
- return native_.getResultObject(native_.callSync('Utils_toUnsignedLongLong', {
- n : _toLong(val)
- }));
+ // According to WebIDL specification this will not be a precise representation
+ // of requested val. We're converting the val to signed long and then pass it
+ // to C++ to get the value in required range.
+ return native_.getResultObject(
+ native_.callSync('Utils_toUnsignedLongLong', {
+ n: _toLong(val)
+ })
+ );
}
Converter.prototype.toUnsignedLongLong = function(val, nullable) {
- return _nullableGeneric(_toUnsignedLongLong, nullable, val);
+ return _nullableGeneric(_toUnsignedLongLong, nullable, val);
};
function _toShort(val) {
- return ((_toLong(val) + 32768) & 0xFFFF) - 32768;
+ return ((_toLong(val) + 32768) & 0xffff) - 32768;
}
Converter.prototype.toShort = function(val, nullable) {
- return _nullableGeneric(_toShort, nullable, val);
+ return _nullableGeneric(_toShort, nullable, val);
};
function _toUnsignedShort(val) {
- return (Math.abs(_toLong(val)) & 0xFFFF);
+ return Math.abs(_toLong(val)) & 0xffff;
}
Converter.prototype.toUnsignedShort = function(val, nullable) {
- return _nullableGeneric(_toUnsignedShort, nullable, val);
+ return _nullableGeneric(_toUnsignedShort, nullable, val);
};
function _toByte(val) {
- return ((_toLong(val) + 128) & 0xFF) - 128;
+ return ((_toLong(val) + 128) & 0xff) - 128;
}
Converter.prototype.toByte = function(val, nullable) {
- return _nullableGeneric(_toByte, nullable, val);
+ return _nullableGeneric(_toByte, nullable, val);
};
function _toOctet(val) {
- return _toLong(val) & 0xFF;
+ return _toLong(val) & 0xff;
}
Converter.prototype.toOctet = function(val, nullable) {
- return _nullableGeneric(_toOctet, nullable, val);
+ return _nullableGeneric(_toOctet, nullable, val);
};
function _toDouble(val) {
- var ret = Number(val);
- if (isNaN(ret) || !isFinite(ret)) {
- throw new WebAPIException(WebAPIException.TYPE_MISMATCH_ERR,
- 'Cannot convert ' + String(val) + ' to double.');
- }
- return ret;
+ var ret = Number(val);
+ if (isNaN(ret) || !isFinite(ret)) {
+ throw new WebAPIException(
+ WebAPIException.TYPE_MISMATCH_ERR,
+ 'Cannot convert ' + String(val) + ' to double.'
+ );
+ }
+ return ret;
}
Converter.prototype.toDouble = function(val, nullable) {
- return _nullableGeneric(_toDouble, nullable, val);
+ return _nullableGeneric(_toDouble, nullable, val);
};
function _toString(val) {
- return String(val);
+ return String(val);
}
Converter.prototype.toString = function(val, nullable) {
- return _nullableGeneric(_toString, nullable, val);
+ return _nullableGeneric(_toString, nullable, val);
};
function _toPlatformObject(val, types) {
- var t;
-
- if (_type.isArray(types)) {
- t = types;
- } else {
- t = [types];
- }
-
- if (_type.isArray(val)) {
- throw new WebAPIException(WebAPIException.TYPE_MISMATCH_ERR,
- 'Cannot convert ' + String(val) + ' to ' + String(t[0].name) + '.');
- }
-
- var match = false;
- for (var i = 0; i < t.length; ++i) {
- if (val instanceof t[i]) {
- return val;
+ var t;
+
+ if (_type.isArray(types)) {
+ t = types;
+ } else {
+ t = [types];
+ }
+
+ if (_type.isArray(val)) {
+ throw new WebAPIException(
+ WebAPIException.TYPE_MISMATCH_ERR,
+ 'Cannot convert ' + String(val) + ' to ' + String(t[0].name) + '.'
+ );
+ }
+
+ var match = false;
+ for (var i = 0; i < t.length; ++i) {
+ if (val instanceof t[i]) {
+ return val;
+ }
}
- }
- throw new WebAPIException(WebAPIException.TYPE_MISMATCH_ERR,
- 'Cannot convert ' + String(val) + ' to ' + String(t[0].name) + '.');
+ throw new WebAPIException(
+ WebAPIException.TYPE_MISMATCH_ERR,
+ 'Cannot convert ' + String(val) + ' to ' + String(t[0].name) + '.'
+ );
}
Converter.prototype.toPlatformObject = function(val, types, nullable) {
- return _nullableGeneric(_toPlatformObject, nullable, val, types);
+ return _nullableGeneric(_toPlatformObject, nullable, val, types);
};
function _toFunction(val) {
- if (_type.isFunction(val)) {
- return val;
- }
+ if (_type.isFunction(val)) {
+ return val;
+ }
- throw new WebAPIException(WebAPIException.TYPE_MISMATCH_ERR,
- 'Cannot convert ' + String(val) + ' to function.');
+ throw new WebAPIException(
+ WebAPIException.TYPE_MISMATCH_ERR,
+ 'Cannot convert ' + String(val) + ' to function.'
+ );
}
Converter.prototype.toFunction = function(val, nullable) {
- return _nullableGeneric(_toFunction, nullable, val);
+ return _nullableGeneric(_toFunction, nullable, val);
};
function _toArray(val) {
- if (_type.isArray(val)) {
- return val;
- }
+ if (_type.isArray(val)) {
+ return val;
+ }
- throw new WebAPIException(WebAPIException.TYPE_MISMATCH_ERR,
- 'Cannot convert ' + String(val) + ' to array.');
+ throw new WebAPIException(
+ WebAPIException.TYPE_MISMATCH_ERR,
+ 'Cannot convert ' + String(val) + ' to array.'
+ );
}
Converter.prototype.toArray = function(val, nullable) {
- return _nullableGeneric(_toArray, nullable, val);
+ return _nullableGeneric(_toArray, nullable, val);
};
function _toDictionary(val) {
- if (_type.isObject(val) || _type.isFunction(val)) {
- return val;
- }
+ if (_type.isObject(val) || _type.isFunction(val)) {
+ return val;
+ }
- throw new WebAPIException(WebAPIException.TYPE_MISMATCH_ERR,
- 'Cannot convert ' + String(val) + ' to dictionary.');
+ throw new WebAPIException(
+ WebAPIException.TYPE_MISMATCH_ERR,
+ 'Cannot convert ' + String(val) + ' to dictionary.'
+ );
}
Converter.prototype.toDictionary = function(val, nullable) {
- return _nullableGeneric(_toDictionary, nullable, val);
+ return _nullableGeneric(_toDictionary, nullable, val);
};
function _toEnum(val, e) {
- var v = _toString(val);
- if (_type.arrayContains(e, v)) {
- return v;
- }
+ var v = _toString(val);
+ if (_type.arrayContains(e, v)) {
+ return v;
+ }
- throw new WebAPIException(WebAPIException.TYPE_MISMATCH_ERR,
- 'Cannot convert ' + v + ' to enum.');
+ throw new WebAPIException(
+ WebAPIException.TYPE_MISMATCH_ERR,
+ 'Cannot convert ' + v + ' to enum.'
+ );
}
Converter.prototype.toEnum = function(val, e, nullable) {
- return _nullableGeneric(_toEnum, nullable, val, e);
+ return _nullableGeneric(_toEnum, nullable, val, e);
};
var _converter = new Converter();
/////////////////////////////////////////////////////////////////////////////
/** @constructor */
var Validator = function() {
- this.Types = {
- BOOLEAN: 'BOOLEAN',
- LONG: 'LONG',
- LONG_LONG: 'LONG_LONG',
- UNSIGNED_LONG: 'UNSIGNED_LONG',
- UNSIGNED_LONG_LONG: 'UNSIGNED_LONG_LONG',
- BYTE: 'BYTE',
- OCTET: 'OCTET',
- DOUBLE: 'DOUBLE',
- STRING: 'STRING',
- FUNCTION: 'FUNCTION',
- DICTIONARY: 'DICTIONARY',
- PLATFORM_OBJECT: 'PLATFORM_OBJECT',
- LISTENER: 'LISTENER',
- ARRAY: 'ARRAY',
- ENUM: 'ENUM',
- FILE_REFERENCE: 'FILE_REFERENCE',
- SIMPLE_TYPE: 'SIMPLE_TYPE' // Boolean, Number or String
- };
+ this.Types = {
+ BOOLEAN: 'BOOLEAN',
+ LONG: 'LONG',
+ LONG_LONG: 'LONG_LONG',
+ UNSIGNED_LONG: 'UNSIGNED_LONG',
+ UNSIGNED_LONG_LONG: 'UNSIGNED_LONG_LONG',
+ BYTE: 'BYTE',
+ OCTET: 'OCTET',
+ DOUBLE: 'DOUBLE',
+ STRING: 'STRING',
+ FUNCTION: 'FUNCTION',
+ DICTIONARY: 'DICTIONARY',
+ PLATFORM_OBJECT: 'PLATFORM_OBJECT',
+ LISTENER: 'LISTENER',
+ ARRAY: 'ARRAY',
+ ENUM: 'ENUM',
+ FILE_REFERENCE: 'FILE_REFERENCE',
+ SIMPLE_TYPE: 'SIMPLE_TYPE' // Boolean, Number or String
+ };
};
-
/**
* Verifies if arguments passed to function are valid.
*
* - values - required in case of some objects, value depends on type
* - validator - function which accepts a single parameter and returns true or false;
* if this property is present, this function will be executed,
- * argument converted to expected type is going to be passed to this function
+ * argument converted to expected type is going to be passed to this
+ * function
*
* @param {Array} a - arguments of a method
* @param {Array} d - description of expected arguments
* {
* name: 'first',
* type: Validator.Types.ARRAY,
- * values: Validator.Types.DOUBLE // converts elements, only primitive types are supported
+ * values: Validator.Types.DOUBLE // converts elements, only primitive types are
+ * supported
* }
* ]
* @code
* ]
*/
Validator.prototype.validateArgs = function(a, d) {
- var args = {has: {}};
-
- for (var i = 0; i < d.length; ++i) {
- var name = d[i].name;
- args.has[name] = (i < a.length);
-
- var optional = d[i].optional;
- var nullable = d[i].nullable;
- var val = a[i];
-
- if (args.has[name] || !optional) {
- var type = d[i].type;
- var values = d[i].values;
-
- switch (type) {
- case this.Types.BOOLEAN:
- val = _converter.toBoolean(val, nullable);
- break;
-
- case this.Types.LONG:
- val = _converter.toLong(val, nullable);
- break;
-
- case this.Types.LONG_LONG:
- val = _converter.toLongLong(val, nullable);
- break;
-
- case this.Types.UNSIGNED_LONG:
- val = _converter.toUnsignedLong(val, nullable);
- break;
-
- case this.Types.UNSIGNED_LONG_LONG:
- val = _converter.toUnsignedLongLong(val, nullable);
- break;
-
- case this.Types.BYTE:
- val = _converter.toByte(val, nullable);
- break;
-
- case this.Types.OCTET:
- val = _converter.toOctet(val, nullable);
- break;
-
- case this.Types.DOUBLE:
- val = _converter.toDouble(val, nullable);
- break;
-
- case this.Types.STRING:
- val = _converter.toString(val, nullable);
- break;
-
- case this.Types.FUNCTION:
- val = _converter.toFunction(val, nullable);
- break;
-
- case this.Types.DICTIONARY:
- val = _converter.toDictionary(val, nullable);
- break;
-
- case this.Types.PLATFORM_OBJECT:
- val = _converter.toPlatformObject(val, values, nullable);
- break;
-
- case this.Types.LISTENER:
- if (_type.isNull(val)) {
- if (!nullable) {
- throw new WebAPIException(WebAPIException.TYPE_MISMATCH_ERR,
- 'Argument "' + name + '" cannot be null.');
- }
- } else {
- if (!_type.isObject(val)) {
- throw new WebAPIException(WebAPIException.TYPE_MISMATCH_ERR,
- 'Argument "' + name + '" should be an object.');
- }
- for (var ii = 0; ii < values.length; ++ii) {
- if (_type.hasProperty(val, values[ii])) {
- val[values[ii]] = _converter.toFunction(val[values[ii]], false);
- }
- }
- }
- break;
+ var args = { has: {} };
+
+ for (var i = 0; i < d.length; ++i) {
+ var name = d[i].name;
+ args.has[name] = i < a.length;
+
+ var optional = d[i].optional;
+ var nullable = d[i].nullable;
+ var val = a[i];
- case this.Types.ARRAY:
- val = _converter.toArray(val, nullable);
- if (!_type.isNull(val) && values) {
- var func;
+ if (args.has[name] || !optional) {
+ var type = d[i].type;
+ var values = d[i].values;
+
+ switch (type) {
+ case this.Types.BOOLEAN:
+ val = _converter.toBoolean(val, nullable);
+ break;
+
+ case this.Types.LONG:
+ val = _converter.toLong(val, nullable);
+ break;
+
+ case this.Types.LONG_LONG:
+ val = _converter.toLongLong(val, nullable);
+ break;
+
+ case this.Types.UNSIGNED_LONG:
+ val = _converter.toUnsignedLong(val, nullable);
+ break;
+
+ case this.Types.UNSIGNED_LONG_LONG:
+ val = _converter.toUnsignedLongLong(val, nullable);
+ break;
+
+ case this.Types.BYTE:
+ val = _converter.toByte(val, nullable);
+ break;
+
+ case this.Types.OCTET:
+ val = _converter.toOctet(val, nullable);
+ break;
- switch (values) {
- case this.Types.BOOLEAN:
- func = _converter.toBoolean;
+ case this.Types.DOUBLE:
+ val = _converter.toDouble(val, nullable);
break;
- case this.Types.LONG:
- func = _converter.toLong;
+ case this.Types.STRING:
+ val = _converter.toString(val, nullable);
break;
- case this.Types.LONG_LONG:
- func = _converter.toLongLong;
+ case this.Types.FUNCTION:
+ val = _converter.toFunction(val, nullable);
break;
- case this.Types.UNSIGNED_LONG:
- func = _converter.toUnsignedLong;
+ case this.Types.DICTIONARY:
+ val = _converter.toDictionary(val, nullable);
break;
- case this.Types.UNSIGNED_LONG_LONG:
- func = _converter.toUnsignedLongLong;
+ case this.Types.PLATFORM_OBJECT:
+ val = _converter.toPlatformObject(val, values, nullable);
break;
- case this.Types.BYTE:
- func = _converter.toByte;
+ case this.Types.LISTENER:
+ if (_type.isNull(val)) {
+ if (!nullable) {
+ throw new WebAPIException(
+ WebAPIException.TYPE_MISMATCH_ERR,
+ 'Argument "' + name + '" cannot be null.'
+ );
+ }
+ } else {
+ if (!_type.isObject(val)) {
+ throw new WebAPIException(
+ WebAPIException.TYPE_MISMATCH_ERR,
+ 'Argument "' + name + '" should be an object.'
+ );
+ }
+ for (var ii = 0; ii < values.length; ++ii) {
+ if (_type.hasProperty(val, values[ii])) {
+ val[values[ii]] = _converter.toFunction(
+ val[values[ii]],
+ false
+ );
+ }
+ }
+ }
break;
- case this.Types.OCTET:
- func = _converter.toOctet;
+ case this.Types.ARRAY:
+ val = _converter.toArray(val, nullable);
+ if (!_type.isNull(val) && values) {
+ var func;
+
+ switch (values) {
+ case this.Types.BOOLEAN:
+ func = _converter.toBoolean;
+ break;
+
+ case this.Types.LONG:
+ func = _converter.toLong;
+ break;
+
+ case this.Types.LONG_LONG:
+ func = _converter.toLongLong;
+ break;
+
+ case this.Types.UNSIGNED_LONG:
+ func = _converter.toUnsignedLong;
+ break;
+
+ case this.Types.UNSIGNED_LONG_LONG:
+ func = _converter.toUnsignedLongLong;
+ break;
+
+ case this.Types.BYTE:
+ func = _converter.toByte;
+ break;
+
+ case this.Types.OCTET:
+ func = _converter.toOctet;
+ break;
+
+ case this.Types.DOUBLE:
+ func = _converter.toDouble;
+ break;
+
+ case this.Types.STRING:
+ func = _converter.toString;
+ break;
+
+ default:
+ func = function(val) {
+ if (!(val instanceof values)) {
+ throw new WebAPIException(
+ WebAPIException.TYPE_MISMATCH_ERR,
+ 'Items of array "' +
+ name +
+ '" should be of type: ' +
+ values +
+ '.'
+ );
+ }
+ return val;
+ };
+ }
+
+ for (var j = 0; j < val.length; ++j) {
+ val[j] = func(val[j]);
+ }
+ }
break;
- case this.Types.DOUBLE:
- func = _converter.toDouble;
+ case this.Types.ENUM:
+ val = _converter.toEnum(val, values, nullable);
break;
- case this.Types.STRING:
- func = _converter.toString;
+ case this.Types.FILE_REFERENCE:
+ if (
+ _type.isObject(val) &&
+ 'File' === val.constructor.name &&
+ val.fullPath
+ ) {
+ val = val.fullPath;
+ }
+ val = _converter.toString(val, nullable);
break;
- default:
- func = function(val) {
- if (!(val instanceof values)) {
- throw new WebAPIException(WebAPIException.TYPE_MISMATCH_ERR,
- 'Items of array "' + name + '" should be of type: ' + values + '.');
- }
- return val;
- };
+ case this.Types.SIMPLE_TYPE:
+ if (optional && _type.isUndefined(val)) {
+ break;
+ }
+ if (nullable && _type.isNull(val)) {
+ break;
+ }
+ if (
+ !_type.isBoolean(val) &&
+ !_type.isNumber(val) &&
+ !_type.isString(val)
+ ) {
+ throw new WebAPIException(
+ WebAPIException.TYPE_MISMATCH_ERR,
+ 'Argument "' + name + '" should be boolean, number or string.'
+ );
+ }
+ break;
+
+ default:
+ throw new WebAPIException(
+ WebAPIException.TYPE_MISMATCH_ERR,
+ 'Unknown type: "' + type + '".'
+ );
}
- for (var j = 0; j < val.length; ++j) {
- val[j] = func(val[j]);
+ var _validator = d[i].validator;
+
+ if (_type.isFunction(_validator) && !_validator(val)) {
+ throw new WebAPIException(
+ WebAPIException.TYPE_MISMATCH_ERR,
+ 'Argument "' + name + '" did not pass additional validation.'
+ );
}
- }
- break;
-
- case this.Types.ENUM:
- val = _converter.toEnum(val, values, nullable);
- break;
-
- case this.Types.FILE_REFERENCE:
- if (_type.isObject(val) && 'File' === val.constructor.name && val.fullPath) {
- val = val.fullPath;
- }
- val = _converter.toString(val, nullable);
- break;
-
- case this.Types.SIMPLE_TYPE:
- if (optional && _type.isUndefined(val)) {
- break;
- }
- if (nullable && _type.isNull(val)) {
- break;
- }
- if (!_type.isBoolean(val) && !_type.isNumber(val) && !_type.isString(val)) {
- throw new WebAPIException(WebAPIException.TYPE_MISMATCH_ERR,
- 'Argument "' + name + '" should be boolean, number or string.');
- }
- break;
-
- default:
- throw new WebAPIException(WebAPIException.TYPE_MISMATCH_ERR,
- 'Unknown type: "' + type + '".');
- }
-
- var _validator = d[i].validator;
-
- if (_type.isFunction(_validator) && !_validator(val)) {
- throw new WebAPIException(WebAPIException.TYPE_MISMATCH_ERR,
- 'Argument "' + name + '" did not pass additional validation.');
- }
-
- args[name] = val;
+
+ args[name] = val;
+ }
}
- }
- return args;
+ return args;
};
-
/**
* @deprecated Use validateArgs() instead.
*/
Validator.prototype.validateMethod = function(a, d) {
- return this.validateArgs(a, d);
+ return this.validateArgs(a, d);
};
-
/**
* Use this helper to ensure that constructor is invoked by "new" operator.
*
* @param {Function} instance
*/
Validator.prototype.isConstructorCall = function(obj, instance) {
- if (!(obj instanceof instance) || obj._previouslyConstructed) {
- // There is no TypeError exception in Tizen 2.3.0 API spec but it's required by current TCTs.
- // For Tizen compliance it's wrapped into WebAPIException.
- throw new WebAPIException('TypeError', 'Constructor cannot be called as function.');
- }
+ if (!(obj instanceof instance) || obj._previouslyConstructed) {
+ // There is no TypeError exception in Tizen 2.3.0 API spec but it's required by
+ // current TCTs.
+ // For Tizen compliance it's wrapped into WebAPIException.
+ throw new WebAPIException(
+ 'TypeError',
+ 'Constructor cannot be called as function.'
+ );
+ }
- Object.defineProperty(obj, '_previouslyConstructed', {
- value: true,
- writable: false,
- enumerable: false
- });
+ Object.defineProperty(obj, '_previouslyConstructed', {
+ value: true,
+ writable: false,
+ enumerable: false
+ });
};
-
/**
* @deprecated Use isConstructorCall() instead.
*/
Validator.prototype.validateConstructorCall = function(obj, instance) {
- this.isConstructorCall(obj, instance);
+ this.isConstructorCall(obj, instance);
};
var _validator = new Validator();
-
-
/////////////////////////////////////////////////////////////////////////////
/** @constructor */
var NativeManager = function(extension) {
-
- /**
- * @type {string}
- * @const
- */
- this.CALLBACK_ID_KEY = 'callbackId';
-
- /**
- * @type {string}
- * @const
- */
- this.LISTENER_ID_KEY = 'listenerId';
-
- /**
- * @type {Object}
- * @private
- */
- var extension_ = extension;
-
- /**
- * @type {number}
- * @private
- */
- var replyId_ = 0;
-
- /**
- * Map of async reply callbacks.
- *
- * @type {Object.<number, function>}
- * @protected
- */
- this.callbacks_ = {};
-
- /**
- * Map of registered listeners.
- *
- * @type {Object.<string, function>}
- * @protected
- */
- this.listeners_ = {};
-
- _validator.isConstructorCall(this, NativeManager);
-
- // TODO: Remove mockup if WRT implements sendRuntimeMessage
- // This is temporary mockup!
- extension.sendRuntimeMessage = extension.sendRuntimeMessage || function() {
- xwalk.utils.error('Runtime did not implement extension.sendRuntimeMessage!');
- throw new WebAPIException(WebAPIException.UNKNOWN_ERR,
- 'Runtime did not implement extension.sendRuntimeMessage!');
- };
-
- extension.sendRuntimeAsyncMessage = extension.sendRuntimeAsyncMessage || function() {
- xwalk.utils.error('Runtime did not implement extension.sendRuntimeAsyncMessage!');
- throw new WebAPIException(WebAPIException.UNKNOWN_ERR,
- 'Runtime did not implement extension.sendRuntimeAsyncMessage!');
- };
-
- extension.sendRuntimeSyncMessage = extension.sendRuntimeSyncMessage || function() {
- xwalk.utils.error('Runtime did not implement extension.sendRuntimeSyncMessage!');
- throw new WebAPIException(WebAPIException.UNKNOWN_ERR,
- 'Runtime did not implement extension.sendRuntimeSyncMessage!');
- };
-
- // check extension prototype
- if (!extension || !extension.internal ||
- !_type.isFunction(extension.postMessage) ||
- !_type.isFunction(extension.internal.sendSyncMessage) ||
- !_type.isFunction(extension.sendRuntimeMessage) ||
- !_type.isFunction(extension.sendRuntimeAsyncMessage) ||
- !_type.isFunction(extension.sendRuntimeSyncMessage) ||
- !_type.isFunction(extension.setMessageListener)) {
- throw new WebAPIException(WebAPIException.TYPE_MISMATCH_ERR,
- 'Wrong extension object passed');
- }
-
- Object.defineProperties(this, {
- nextReplyId: {
- get: function() {
- return ++replyId_;
- },
- enumerable: false
- },
- extension: {
- get: function() {
- return extension_;
- },
- enumerable: true
- }
- });
-
- extension_.setMessageListener(function(json) {
- try {
- var msg = JSON_.parse(json);
- } catch (error) {
- // Because of special handling of power lock in chromium, the special signals:
- // - __DisableChromiumInternalPowerLock
- // - __EnableChromiumInternalPowerLock
- // could occur. In such cases we are silently ignroing those messages.
- // TODO This is workaround for missing patch in chromium-efl package which should handle
- // this special message and don't forward it to webapi JS. After chromium-efl will
- // be updated, below checking should be removed.
- if (json.substring(0,2) === '__') {
- return;
- }
- xwalk.utils.error('Ignoring message - Invalid JSON received: ' + json);
- return;
- }
- var id;
-
- if (msg.hasOwnProperty(this.CALLBACK_ID_KEY)) {
- id = msg[this.CALLBACK_ID_KEY];
- delete msg[this.CALLBACK_ID_KEY];
-
- if (!_type.isFunction(this.callbacks_[id])) {
- xwalk.utils.error('Wrong callback identifier. Ignoring message.');
- return;
- }
-
- var f = this.callbacks_[id];
- setTimeout(function() {
- try {
- f(msg);
- } catch (e) {
- xwalk.utils.error('########## exception');
- xwalk.utils.error(e);
- }
- }, 0);
- delete this.callbacks_[id];
-
- return;
+ /**
+ * @type {string}
+ * @const
+ */
+ this.CALLBACK_ID_KEY = 'callbackId';
+
+ /**
+ * @type {string}
+ * @const
+ */
+ this.LISTENER_ID_KEY = 'listenerId';
+
+ /**
+ * @type {Object}
+ * @private
+ */
+ var extension_ = extension;
+
+ /**
+ * @type {number}
+ * @private
+ */
+ var replyId_ = 0;
+
+ /**
+ * Map of async reply callbacks.
+ *
+ * @type {Object.<number, function>}
+ * @protected
+ */
+ this.callbacks_ = {};
+
+ /**
+ * Map of registered listeners.
+ *
+ * @type {Object.<string, function>}
+ * @protected
+ */
+ this.listeners_ = {};
+
+ _validator.isConstructorCall(this, NativeManager);
+
+ // TODO: Remove mockup if WRT implements sendRuntimeMessage
+ // This is temporary mockup!
+ extension.sendRuntimeMessage =
+ extension.sendRuntimeMessage ||
+ function() {
+ xwalk.utils.error('Runtime did not implement extension.sendRuntimeMessage!');
+ throw new WebAPIException(
+ WebAPIException.UNKNOWN_ERR,
+ 'Runtime did not implement extension.sendRuntimeMessage!'
+ );
+ };
+
+ extension.sendRuntimeAsyncMessage =
+ extension.sendRuntimeAsyncMessage ||
+ function() {
+ xwalk.utils.error(
+ 'Runtime did not implement extension.sendRuntimeAsyncMessage!'
+ );
+ throw new WebAPIException(
+ WebAPIException.UNKNOWN_ERR,
+ 'Runtime did not implement extension.sendRuntimeAsyncMessage!'
+ );
+ };
+
+ extension.sendRuntimeSyncMessage =
+ extension.sendRuntimeSyncMessage ||
+ function() {
+ xwalk.utils.error(
+ 'Runtime did not implement extension.sendRuntimeSyncMessage!'
+ );
+ throw new WebAPIException(
+ WebAPIException.UNKNOWN_ERR,
+ 'Runtime did not implement extension.sendRuntimeSyncMessage!'
+ );
+ };
+
+ // check extension prototype
+ if (
+ !extension ||
+ !extension.internal ||
+ !_type.isFunction(extension.postMessage) ||
+ !_type.isFunction(extension.internal.sendSyncMessage) ||
+ !_type.isFunction(extension.sendRuntimeMessage) ||
+ !_type.isFunction(extension.sendRuntimeAsyncMessage) ||
+ !_type.isFunction(extension.sendRuntimeSyncMessage) ||
+ !_type.isFunction(extension.setMessageListener)
+ ) {
+ throw new WebAPIException(
+ WebAPIException.TYPE_MISMATCH_ERR,
+ 'Wrong extension object passed'
+ );
}
- if (msg.hasOwnProperty(this.LISTENER_ID_KEY)) {
- id = msg[this.LISTENER_ID_KEY];
- delete msg[this.LISTENER_ID_KEY];
-
- if (!_type.isFunction(this.listeners_[id])) {
- xwalk.utils.error('Wrong listener identifier. Ignoring message.');
- return;
- }
-
- var f = this.listeners_[id];
- setTimeout(function() {
- try {
- f(msg);
- } catch (e) {
- xwalk.utils.error('########## exception');
- xwalk.utils.error(e);
+ Object.defineProperties(this, {
+ nextReplyId: {
+ get: function() {
+ return ++replyId_;
+ },
+ enumerable: false
+ },
+ extension: {
+ get: function() {
+ return extension_;
+ },
+ enumerable: true
}
- }, 0);
-
- return;
- }
+ });
+
+ extension_.setMessageListener(
+ function(json) {
+ try {
+ var msg = JSON_.parse(json);
+ } catch (error) {
+ // Because of special handling of power lock in chromium, the special
+ // signals:
+ // - __DisableChromiumInternalPowerLock
+ // - __EnableChromiumInternalPowerLock
+ // could occur. In such cases we are silently ignroing those messages.
+ // TODO This is workaround for missing patch in chromium-efl package
+ // which should handle this special message and don't forward it to
+ // webapi JS. After chromium-efl will be updated, below checking should
+ // be removed.
+ if (json.substring(0, 2) === '__') {
+ return;
+ }
+ xwalk.utils.error('Ignoring message - Invalid JSON received: ' + json);
+ return;
+ }
+ var id;
+
+ if (msg.hasOwnProperty(this.CALLBACK_ID_KEY)) {
+ id = msg[this.CALLBACK_ID_KEY];
+ delete msg[this.CALLBACK_ID_KEY];
+
+ if (!_type.isFunction(this.callbacks_[id])) {
+ xwalk.utils.error('Wrong callback identifier. Ignoring message.');
+ return;
+ }
+
+ var f = this.callbacks_[id];
+ setTimeout(function() {
+ try {
+ f(msg);
+ } catch (e) {
+ xwalk.utils.error('########## exception');
+ xwalk.utils.error(e);
+ }
+ }, 0);
+ delete this.callbacks_[id];
+
+ return;
+ }
- xwalk.utils.error('Missing callback or listener identifier. Ignoring message.');
+ if (msg.hasOwnProperty(this.LISTENER_ID_KEY)) {
+ id = msg[this.LISTENER_ID_KEY];
+ delete msg[this.LISTENER_ID_KEY];
+
+ if (!_type.isFunction(this.listeners_[id])) {
+ xwalk.utils.error('Wrong listener identifier. Ignoring message.');
+ return;
+ }
+
+ var f = this.listeners_[id];
+ setTimeout(function() {
+ try {
+ f(msg);
+ } catch (e) {
+ xwalk.utils.error('########## exception');
+ xwalk.utils.error(e);
+ }
+ }, 0);
+
+ return;
+ }
- }.bind(this));
+ xwalk.utils.error(
+ 'Missing callback or listener identifier. Ignoring message.'
+ );
+ }.bind(this)
+ );
};
NativeManager.prototype.call = function(cmd, args, callback) {
- args = args || {};
+ args = args || {};
- var replyId = this.nextReplyId;
- args[this.CALLBACK_ID_KEY] = replyId;
- this.callbacks_[replyId] = callback;
+ var replyId = this.nextReplyId;
+ args[this.CALLBACK_ID_KEY] = replyId;
+ this.callbacks_[replyId] = callback;
- return this.callSync(cmd, args);
+ return this.callSync(cmd, args);
};
NativeManager.prototype.callSync = function(cmd, args) {
- var request = JSON_.stringify({
- cmd: cmd,
- args: args || {}
- });
-
- var response = this.extension.internal.sendSyncMessage(request);
- if( response === undefined ) {
- /* C++ extension didn't set sync response using Instance::SendSyncReply */
- throw new WebAPIException(WebAPIException.ABORT_ERR, "Internal error");
- }
- return JSON_.parse(response);
+ var request = JSON_.stringify({
+ cmd: cmd,
+ args: args || {}
+ });
+
+ var response = this.extension.internal.sendSyncMessage(request);
+ if (response === undefined) {
+ /* C++ extension didn't set sync response using Instance::SendSyncReply */
+ throw new WebAPIException(WebAPIException.ABORT_ERR, 'Internal error');
+ }
+ return JSON_.parse(response);
};
NativeManager.prototype.sendRuntimeMessage = function(msg, body) {
- return this.extension.sendRuntimeMessage(msg, body || '');
+ return this.extension.sendRuntimeMessage(msg, body || '');
};
NativeManager.prototype.sendRuntimeAsyncMessage = function(msg, body, callback) {
- var handler = function(response) {
- if (_type.isFunction(callback)) {
- var result = {};
- if ('success' === response.toLowerCase()) {
- result.status = 'success';
- } else {
- result.status = 'error';
- result.error = new WebAPIException(WebAPIException.UNKNOWN_ERR,
- 'Runtime message failure');
- }
- callback(result);
- }
- };
- return this.extension.sendRuntimeAsyncMessage(msg, body || '', handler);
+ var handler = function(response) {
+ if (_type.isFunction(callback)) {
+ var result = {};
+ if ('success' === response.toLowerCase()) {
+ result.status = 'success';
+ } else {
+ result.status = 'error';
+ result.error = new WebAPIException(
+ WebAPIException.UNKNOWN_ERR,
+ 'Runtime message failure'
+ );
+ }
+ callback(result);
+ }
+ };
+ return this.extension.sendRuntimeAsyncMessage(msg, body || '', handler);
};
NativeManager.prototype.sendRuntimeSyncMessage = function(msg, body) {
- return this.extension.sendRuntimeSyncMessage(msg, body || '');
+ return this.extension.sendRuntimeSyncMessage(msg, body || '');
};
NativeManager.prototype.addListener = function(name, callback) {
- if (!_type.isString(name) || !name.length) {
- throw new WebAPIException(WebAPIException.TYPE_MISMATCH_ERR);
- }
+ if (!_type.isString(name) || !name.length) {
+ throw new WebAPIException(WebAPIException.TYPE_MISMATCH_ERR);
+ }
- this.listeners_[name] = callback;
+ this.listeners_[name] = callback;
};
NativeManager.prototype.removeListener = function(name) {
- if (this.listeners_.hasOwnProperty(name)) {
- delete this.listeners_[name];
- }
+ if (this.listeners_.hasOwnProperty(name)) {
+ delete this.listeners_[name];
+ }
};
NativeManager.prototype.isListenerSet = function(name) {
- return this.listeners_.hasOwnProperty(name);
+ return this.listeners_.hasOwnProperty(name);
};
NativeManager.prototype.isSuccess = function(result) {
- return (result.status !== 'error');
+ return result.status !== 'error';
};
NativeManager.prototype.isFailure = function(result) {
- return !this.isSuccess(result);
+ return !this.isSuccess(result);
};
NativeManager.prototype.getResultObject = function(result) {
- return result.result;
+ return result.result;
};
NativeManager.prototype.getErrorObject = function(result) {
- return new WebAPIException(result.error);
+ return new WebAPIException(result.error);
};
NativeManager.prototype.callIfPossible = function(callback) {
- if (!_type.isNullOrUndefined(callback)) {
- callback.apply(callback, [].slice.call(arguments, 1));
- }
+ if (!_type.isNullOrUndefined(callback)) {
+ callback.apply(callback, [].slice.call(arguments, 1));
+ }
};
// WebAPIException and WebAPIError definition moved to Utils for compliance
// reasons with blink-wrt environment.
-// In blink-wrt the original Tizen module is loaded, which is not providing exception constructor.
+// In blink-wrt the original Tizen module is loaded, which is not providing
+// exception constructor.
// As modules needs exceptions internally so they are loaded here for now.
// See http://168.219.209.56/gerrit/#/c/23472/ for more details.
// In future exception definition could be moved back to Tizen module.
function __isObject(object) {
- return object instanceof _global.Object;
+ return object instanceof _global.Object;
}
function __isUndefined(object) {
- return object === void 0;
+ return object === void 0;
}
function __isNumber(object) {
- return typeof object === 'number';
+ return typeof object === 'number';
}
// WARNING! This list should be in sync with the equivalent enum
// located at tizen.h. Remember to update tizen.h if you change
// something here.
var errors = {
- NO_ERROR: 0,
- UNKNOWN_ERR: -1,
-
- INDEX_SIZE_ERR: 1,
- DOMSTRING_SIZE_ERR: 2,
- HIERARCHY_REQUEST_ERR: 3,
- WRONG_DOCUMENT_ERR: 4,
- INVALID_CHARACTER_ERR: 5,
- NO_DATA_ALLOWED_ERR: 6,
- NO_MODIFICATION_ALLOWED_ERR: 7,
- NOT_FOUND_ERR: 8,
- NOT_SUPPORTED_ERR: 9,
- INUSE_ATTRIBUTE_ERR: 10,
- INVALID_STATE_ERR: 11,
- SYNTAX_ERR: 12,
- INVALID_MODIFICATION_ERR: 13,
- NAMESPACE_ERR: 14,
- INVALID_ACCESS_ERR: 15,
- VALIDATION_ERR: 16,
- TYPE_MISMATCH_ERR: 17,
- SECURITY_ERR: 18,
- NETWORK_ERR: 19,
- ABORT_ERR: 20,
- URL_MISMATCH_ERR: 21,
- QUOTA_EXCEEDED_ERR: 22,
- TIMEOUT_ERR: 23,
- INVALID_NODE_TYPE_ERR: 24,
- DATA_CLONE_ERR: 25,
-
- // Error codes for these errors are not really defined anywhere.
- INVALID_VALUES_ERR: 100,
- IO_ERR: 101,
- PERMISSION_DENIED_ERR: 102,
- SERVICE_NOT_AVAILABLE_ERR: 103,
- DATABASE_ERR: 104,
- VERIFICATION_ERR: 105
+ NO_ERROR: 0,
+ UNKNOWN_ERR: -1,
+
+ INDEX_SIZE_ERR: 1,
+ DOMSTRING_SIZE_ERR: 2,
+ HIERARCHY_REQUEST_ERR: 3,
+ WRONG_DOCUMENT_ERR: 4,
+ INVALID_CHARACTER_ERR: 5,
+ NO_DATA_ALLOWED_ERR: 6,
+ NO_MODIFICATION_ALLOWED_ERR: 7,
+ NOT_FOUND_ERR: 8,
+ NOT_SUPPORTED_ERR: 9,
+ INUSE_ATTRIBUTE_ERR: 10,
+ INVALID_STATE_ERR: 11,
+ SYNTAX_ERR: 12,
+ INVALID_MODIFICATION_ERR: 13,
+ NAMESPACE_ERR: 14,
+ INVALID_ACCESS_ERR: 15,
+ VALIDATION_ERR: 16,
+ TYPE_MISMATCH_ERR: 17,
+ SECURITY_ERR: 18,
+ NETWORK_ERR: 19,
+ ABORT_ERR: 20,
+ URL_MISMATCH_ERR: 21,
+ QUOTA_EXCEEDED_ERR: 22,
+ TIMEOUT_ERR: 23,
+ INVALID_NODE_TYPE_ERR: 24,
+ DATA_CLONE_ERR: 25,
+
+ // Error codes for these errors are not really defined anywhere.
+ INVALID_VALUES_ERR: 100,
+ IO_ERR: 101,
+ PERMISSION_DENIED_ERR: 102,
+ SERVICE_NOT_AVAILABLE_ERR: 103,
+ DATABASE_ERR: 104,
+ VERIFICATION_ERR: 105
};
var code_to_name = {};
var name_to_code = {};
Object.keys(errors).forEach(function(key) {
- name_to_code[code_to_name[errors[key]]] = errors[key];
+ name_to_code[code_to_name[errors[key]]] = errors[key];
});
-
/**
* Generic exception interface.
*
* @param {number} code 16-bit error code.
- * @param {string} message An error message that describes the details of an encountered error.
+ * @param {string} message An error message that describes the details of
+ * an encountered error.
* @param {string} name An error type.
*/
var WebAPIException = function(code, message, name) {
- var code_ = 0;
- var name_ = code_to_name[code];
- var message_ = 'Unknown error';
+ var code_ = 0;
+ var name_ = code_to_name[code];
+ var message_ = 'Unknown error';
- switch (arguments.length) {
+ switch (arguments.length) {
case 1:
- var error = arguments[0];
- if (__isObject(error)) {
- code_ = error.code;
- name_ = error.name;
- message_ = error.message;
- if (__isUndefined(code_) && !__isUndefined(name_))
- code_ = name_to_code[name_];
- if (__isUndefined(name_) && !__isUndefined(code_))
- name_ = code_to_name[code_];
- } else if (__isNumber(error)) {
- // backward compatibility with crosswalk implementation
- code_ = error;
- name_ = code_to_name[code];
- message_ = name_;
- }
- break;
- case 2:
- if (__isNumber(arguments[0])) {
- code_ = arguments[0];
- if (!__isUndefined(code_to_name[code_])) {
- name_ = code_to_name[code_];
+ var error = arguments[0];
+ if (__isObject(error)) {
+ code_ = error.code;
+ name_ = error.name;
+ message_ = error.message;
+ if (__isUndefined(code_) && !__isUndefined(name_))
+ code_ = name_to_code[name_];
+ if (__isUndefined(name_) && !__isUndefined(code_))
+ name_ = code_to_name[code_];
+ } else if (__isNumber(error)) {
+ // backward compatibility with crosswalk implementation
+ code_ = error;
+ name_ = code_to_name[code];
+ message_ = name_;
}
- } else {
- name_ = String(arguments[0]);
- if (!__isUndefined(name_to_code[name_])) {
- code_ = name_to_code[name_];
+ break;
+ case 2:
+ if (__isNumber(arguments[0])) {
+ code_ = arguments[0];
+ if (!__isUndefined(code_to_name[code_])) {
+ name_ = code_to_name[code_];
+ }
+ } else {
+ name_ = String(arguments[0]);
+ if (!__isUndefined(name_to_code[name_])) {
+ code_ = name_to_code[name_];
+ }
}
- }
- message_ = String(arguments[1]);
- break;
+ message_ = String(arguments[1]);
+ break;
case 3:
- // backward compatibility with crosswalk implementation
- code_ = Number(arguments[0]);
- message_ = String(arguments[1]);
- name_ = String(arguments[2]);
- break;
+ // backward compatibility with crosswalk implementation
+ code_ = Number(arguments[0]);
+ message_ = String(arguments[1]);
+ name_ = String(arguments[2]);
+ break;
default:
- return;
- }
+ return;
+ }
- if (code_ > errors.DATA_CLONE_ERR) {
- code_ = 0;
- }
+ if (code_ > errors.DATA_CLONE_ERR) {
+ code_ = 0;
+ }
- // attributes
- Object.defineProperties(this, {
- code: {value: code_, writable: false, enumerable: true},
- name: {value: name_, writable: false, enumerable: true},
- message: {value: message_, writable: false, enumerable: true}
- });
+ // attributes
+ Object.defineProperties(this, {
+ code: { value: code_, writable: false, enumerable: true },
+ name: { value: name_, writable: false, enumerable: true },
+ message: { value: message_, writable: false, enumerable: true }
+ });
- this.constructor.prototype.__proto__ = Error.prototype;
- Error.captureStackTrace && Error.captureStackTrace(this, this.constructor); // V8-specific code
+ this.constructor.prototype.__proto__ = Error.prototype;
+ Error.captureStackTrace && Error.captureStackTrace(this, this.constructor);
+ // V8-specific code
};
WebAPIException.prototype.toString = function() {
- return this.name + ': ' + this.message;
+ return this.name + ': ' + this.message;
};
-
var error_constants = {};
for (var prop in errors) {
- error_constants[prop] = {value: errors[prop], writable: false, enumerable: true};
+ error_constants[prop] = { value: errors[prop], writable: false, enumerable: true };
}
Object.defineProperties(WebAPIException, error_constants);
Object.defineProperties(WebAPIException.prototype, error_constants);
-
// Export WebAPIException and WebAPIError into global scope.
// For compliance reasons their constructors should not be exported in tizen namespace,
// but should be available internally to allow throwing exceptions from modules.
var scope;
if (typeof window !== 'undefined') {
- scope = window;
-} else if(typeof global !== 'undefined') {
- scope = global;
+ scope = window;
+} else if (typeof global !== 'undefined') {
+ scope = global;
}
scope = scope || {};
scope.WebAPIException = WebAPIException;
Object.freeze(exports.utils);
Object.freeze(Utils.prototype);
Object.freeze(NativeManager.prototype);
-