1 // Copyright 2014 The Chromium Authors. 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.
7 * AppLauncher is an interface that allows the client code to launch and close
8 * the app without knowing the implementation difference between a v1 app and
12 * var appLauncher = new remoting.V1AppLauncher();
14 * appLauncher.launch({arg1:'someValue'}).then(function(id){
19 * appLauncher.close(appId);
24 /** @suppress {duplicate} */
25 var remoting
= remoting
|| {};
28 remoting
.AppLauncher = function() {};
31 * @param {Object=} opt_launchArgs
32 * @return {Promise} The promise will resolve when the app is launched. It will
33 * provide the caller with the appId (which is either the id of the hosting tab
34 * or window). The caller can use the appId to close the app.
36 remoting
.AppLauncher
.prototype.launch = function(opt_launchArgs
) { };
39 * @param {string} id The id of the app to close.
40 * @return {Promise} The promise will resolve when the app is closed.
42 remoting
.AppLauncher
.prototype.close = function(id
) {};
46 * @implements {remoting.AppLauncher}
48 remoting
.V1AppLauncher = function() {};
50 remoting
.V1AppLauncher
.prototype.launch = function(opt_launchArgs
) {
51 var url
= base
.urlJoin('main.html', opt_launchArgs
);
54 * @param {function(*=):void} resolve
55 * @param {function(*=):void} reject
57 return new Promise(function(resolve
, reject
) {
58 chrome
.tabs
.create({ url
: url
, selected
: true },
59 /** @param {chrome.Tab} tab The created tab. */
62 reject(new Error(chrome
.runtime
.lastError
.message
));
64 resolve(String(tab
.id
));
70 remoting
.V1AppLauncher
.prototype.close = function(id
) {
72 * @param {function(*=):void} resolve
73 * @param {function(*=):void} reject
75 return new Promise(function(resolve
, reject
) {
76 /** @param {chrome.Tab} tab The retrieved tab. */
77 chrome
.tabs
.get(id
, function(tab
) {
79 reject(new Error(chrome
.runtime
.lastError
.message
));
81 chrome
.tabs
.remove(tab
.id
, resolve
);
90 * @implements {remoting.AppLauncher}
92 remoting
.V2AppLauncher = function() {};
98 remoting
.V2AppLauncher
.nextWindowId_
= 0;
100 remoting
.V2AppLauncher
.prototype.launch = function(opt_launchArgs
) {
101 var url
= base
.urlJoin('main.html', opt_launchArgs
);
104 * @param {function(*=):void} resolve
105 * @param {function(*=):void} reject
107 return new Promise(function(resolve
, reject
) {
108 chrome
.app
.window
.create(url
, {
112 'id': String(remoting
.V2AppLauncher
.nextWindowId_
++)
114 /** @param {AppWindow} appWindow */
115 function(appWindow
) {
117 reject(new Error(chrome
.runtime
.lastError
.message
));
119 resolve(appWindow
.id
);
125 remoting
.V2AppLauncher
.prototype.close = function(id
) {
126 var appWindow
= chrome
.app
.window
.get(id
);
128 return Promise
.reject(new Error(chrome
.runtime
.lastError
.message
));
131 return Promise
.resolve();