1 // Copyright (c) 2014 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
10 // Return a callback ID number which will be contained by the native message.
11 setup: function(callback) {
12 var id = ++this._next_id;
13 this._callbacks[id] = callback;
17 dispatch: function(m) {
19 var callback = this._callbacks[id];
20 callback.call(null, m);
21 delete this._callbacks[id];
25 var appInfoEventCallbacks = {
28 key: '_appInfoEventCallback',
30 // Return a callback ID number which can be unregistered later.
31 addCallback: function(callback) {
32 if (!this._callbacks.length) {
33 var result = sendSyncMessage({ cmd: 'RegisterAppInfoEvent' });
34 if (result.error != null)
35 throw new tizen.WebAPIException(result.error);
38 var id = ++this._next_id;
39 this._callbacks.push({func: callback, id: id});
43 removeCallback: function(id) {
44 for (var i = 0, len = this._callbacks.length; i < len; i++) {
45 if (id === this._callbacks[i].id)
49 throw new tizen.WebAPIException(tizen.WebAPIException.NOT_FOUND_ERR);
51 this._callbacks.splice(i, 1);
52 if (!this._callbacks.length) {
53 var result = sendSyncMessage({ cmd: 'UnregisterAppInfoEvent' });
54 if (result.error != null)
55 throw new tizen.WebAPIException(result.error);
59 dispatch: function(m) {
60 var callbacks = this._callbacks.slice();
61 for (var i = 0, len = callbacks.length; i < len; i++)
62 callbacks[i].func.call(null, m);
66 extension.setMessageListener(function(msg) {
67 var m = JSON.parse(msg);
68 if (typeof m[asyncCallbacks.key] === 'number') {
69 asyncCallbacks.dispatch(m);
71 if (m[asyncCallbacks.key] === appInfoEventCallbacks.key)
72 appInfoEventCallbacks.dispatch(m);
74 console.error('unexpected message received' + msg);
78 // Post async message to extension with callbackId saved. The extension will return
79 // a message with the same callbackId to the callback set in setMessageListener.
80 function postMessage(msg, callbackId) {
81 msg[asyncCallbacks.key] = callbackId;
82 extension.postMessage(JSON.stringify(msg));
85 function sendSyncMessage(msg) {
86 return JSON.parse(extension.internal.sendSyncMessage(JSON.stringify(msg)));
89 function defineReadOnlyProperty(object, key, value) {
90 Object.defineProperty(object, key, {
97 // Define Application interface, the getRequestedAppControl method will not be
99 function Application(appInfo, contextId) {
100 defineReadOnlyProperty(this, 'appInfo', appInfo);
101 defineReadOnlyProperty(this, 'contextId', contextId);
104 Application.prototype.exit = function() {
105 var result = sendSyncMessage({ cmd: 'ExitCurrentApp' });
106 if (result.error != null)
107 throw new tizen.WebAPIException(result.error);
110 Application.prototype.hide = function() {
111 var result = sendSyncMessage({ cmd: 'HideCurrentApp' });
112 if (result.error != null)
113 throw new tizen.WebAPIException(result.error);
116 // ApplicationContext interface.
117 function ApplicationContext(json) {
118 defineReadOnlyProperty(this, 'id', json.id);
119 defineReadOnlyProperty(this, 'appId', json.appId);
122 // ApplicationInformation interface.
123 function ApplicationInformation(json) {
124 for (var field in json) {
125 var val = json[field];
126 if (field === 'installDate')
127 val = new Date(val * 1000);
128 defineReadOnlyProperty(this, field, val);
132 // ApplicationMetaData interface.
133 function ApplicationMetaData(json) {
134 defineReadOnlyProperty(this, 'key', json.key);
135 defineReadOnlyProperty(this, 'value', json.value);
138 exports.getAppInfo = function(appId) {
139 if (typeof appId !== 'string' && appId != undefined)
140 throw new tizen.WebAPIException(tizen.WebAPIException.TYPE_MISMATCH_ERR);
142 var result = sendSyncMessage({ cmd: 'GetAppInfo', id: appId });
143 if (result.error != null)
144 throw new tizen.WebAPIException(result.error);
145 return new ApplicationInformation(result.data);
148 exports.getAppsInfo = function(onsuccess, onerror) {
149 if ((typeof onsuccess !== 'function') ||
150 (onerror && typeof onerror !== 'function'))
151 throw new tizen.WebAPIException(tizen.WebAPIException.TYPE_MISMATCH_ERR);
153 var callbackId = asyncCallbacks.setup(function(result) {
154 if (result.error != null) {
157 return onerror(new tizen.WebAPIError(result.error));
161 for (var i = 0, len = result.data.length; i < len; ++i)
162 appsInfo.push(new ApplicationInformation(result.data[i]));
163 return onsuccess(appsInfo);
166 var msg = { cmd: 'GetAppsInfo' };
167 postMessage(msg, callbackId);
170 exports.getAppContext = function(contextId) {
171 if (contextId && !/^\d+$/.test(contextId))
172 throw new tizen.WebAPIException(tizen.WebAPIException.TYPE_MISMATCH_ERR);
174 var result = sendSyncMessage({ cmd: 'GetAppContext', id: contextId});
176 throw new tizen.WebAPIException(result.error);
178 return new ApplicationContext(result.data);
181 exports.getAppsContext = function(onsuccess, onerror) {
182 if ((typeof onsuccess !== 'function') ||
183 (onerror && typeof onerror !== 'function'))
184 throw new tizen.WebAPIException(tizen.WebAPIException.TYPE_MISMATCH_ERR);
186 var callbackId = asyncCallbacks.setup(function(result) {
187 if (result.error != null) {
190 return onerror(new tizen.WebAPIError(result.error));
194 for (var i = 0, len = result.data.length; i < len; ++i)
195 contexts.push(new ApplicationContext(result.data[i]));
196 return onsuccess(contexts);
199 var msg = { cmd: 'GetAppsContext' };
200 postMessage(msg, callbackId);
203 exports.getCurrentApplication = function() {
204 var result = sendSyncMessage({ cmd: 'GetCurrentApp' });
206 throw new tizen.WebAPIException(result.error);
208 var appInfo = new ApplicationInformation(result.data.appInfo);
209 return new Application(appInfo, result.data.appContext.id);
212 exports.kill = function(contextId, onsuccess, onerror) {
213 if ((!/^\d+$/.test(contextId)) ||
214 (onsuccess && typeof onsuccess !== 'function') ||
215 (onerror && typeof onerror !== 'function'))
216 throw new tizen.WebAPIException(tizen.WebAPIException.TYPE_MISMATCH_ERR);
218 var callbackId = asyncCallbacks.setup(function(result) {
219 if (result.error != null) {
222 return onerror(new tizen.WebAPIError(result.code));
227 var msg = { cmd: 'KillApp', id: contextId };
228 postMessage(msg, callbackId);
231 exports.launch = function(appId, onsuccess, onerror) {
232 if ((typeof appId !== 'string') ||
233 (onsuccess && typeof onsuccess !== 'function') ||
234 (onerror && typeof onerror !== 'function'))
235 throw new tizen.WebAPIException(tizen.WebAPIException.TYPE_MISMATCH_ERR);
237 var callbackId = asyncCallbacks.setup(function(result) {
238 if (result.error != null) {
241 return onerror(new tizen.WebAPIError(result.error));
246 var msg = { cmd: 'LaunchApp', id: appId };
247 postMessage(msg, callbackId);
250 exports.addAppInfoEventListener = function(eventCallback) {
251 if (typeof eventCallback !== 'object' ||
252 typeof eventCallback.oninstalled !== 'function' ||
253 typeof eventCallback.onupdated !== 'function' ||
254 typeof eventCallback.onuninstalled !== 'function')
255 throw new tizen.WebAPIException(tizen.WebAPIException.TYPE_MISMATCH_ERR);
257 var watchId = appInfoEventCallbacks.addCallback(function(result) {
258 if (result.installed) {
259 result.installed.forEach(function(appInfo) {
260 var appInfo = new ApplicationInformation(appInfo);
261 eventCallback.oninstalled(appInfo);
264 if (result.updated) {
265 result.updated.forEach(function(appInfo) {
266 var appInfo = new ApplicationInformation(appInfo);
267 eventCallback.onupdated(appInfo);
270 if (result.uninstalled) {
271 result.uninstalled.forEach(function(appId) {
272 eventCallback.onuninstalled(appId);
280 exports.removeAppInfoEventListener = function(watchId) {
281 if (typeof watchId !== 'number')
282 throw new tizen.WebAPIException(tizen.WebAPIException.TYPE_MISMATCH_ERR);
284 appInfoEventCallbacks.removeCallback(watchId);
287 exports.getAppMetaData = function(appId) {
288 if (typeof appId !== 'string' && appId != undefined)
289 throw new tizen.WebAPIException(tizen.WebAPIException.TYPE_MISMATCH_ERR);
291 var result = sendSyncMessage({ cmd: 'GetAppMetaData', id: appId });
292 if (result.error != null)
293 throw new tizen.WebAPIException(result.error);
296 for (var i = 0, len = result.data.length; i < len; ++i)
297 data.push(new ApplicationMetaData(result.data[i]));