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);
22 /** @suppress {duplicate} */
23 var remoting
= remoting
|| {};
30 remoting
.AppLauncher = function() {};
33 * @param {Object=} opt_launchArgs
34 * @return {Promise} The promise will resolve when the app is launched. It will
35 * provide the caller with the appId (which is either the id of the hosting tab
36 * or window). The caller can use the appId to close the app.
38 remoting
.AppLauncher
.prototype.launch = function(opt_launchArgs
) { };
41 * @param {string} id The id of the app to close.
42 * @return {Promise} The promise will resolve when the app is closed.
44 remoting
.AppLauncher
.prototype.close = function(id
) {};
48 * @implements {remoting.AppLauncher}
50 remoting
.V1AppLauncher = function() {};
52 remoting
.V1AppLauncher
.prototype.launch = function(opt_launchArgs
) {
53 var url
= base
.urlJoin('main.html', opt_launchArgs
);
55 return new Promise(function(resolve
, reject
) {
56 chrome
.tabs
.create({ url
: url
, selected
: true }, function(/**Tab*/ tab
){
58 reject(new Error(chrome
.runtime
.lastError
.message
));
60 resolve(String(tab
.id
));
66 remoting
.V1AppLauncher
.prototype.close = function(id
) {
67 return new Promise(function(resolve
, reject
) {
68 chrome
.tabs
.get(parseInt(id
, 10), function(/** Tab */ tab
) {
70 reject(new Error(chrome
.runtime
.lastError
.message
));
72 chrome
.tabs
.remove(tab
.id
, resolve
);
81 * @implements {remoting.AppLauncher}
83 remoting
.V2AppLauncher = function() {};
85 var APP_MAIN_URL
= 'main.html';
90 remoting
.V2AppLauncher
.prototype.restart = function(id
) {
91 this.close(id
).then(function() {
92 // Not using the launch() method because we want to launch a new window with
93 // the same id, such that the size and positioning of the original window
95 chrome
.app
.window
.create(APP_MAIN_URL
, {'id' : id
, 'frame': 'none'});
99 remoting
.V2AppLauncher
.prototype.launch = function(opt_launchArgs
) {
100 var url
= base
.urlJoin(APP_MAIN_URL
, opt_launchArgs
);
104 * @param {function(*=):void} resolve
105 * @param {function(*=):void} reject
107 function(resolve
, reject
) {
108 var START_FULLSCREEN
= 'start-fullscreen';
109 /** @param {Object} values */
110 var onValues = function(values
) {
111 /** @type {string} */
112 var state
= values
[START_FULLSCREEN
] ? 'fullscreen' : 'normal';
113 chrome
.app
.window
.create(url
, {
117 'id': String(getNextWindowId()),
120 function(/** chrome.app.window.AppWindow= */ appWindow
) {
122 reject(new Error(chrome
.runtime
.lastError
.message
));
124 resolve(appWindow
.id
);
128 chrome
.storage
.local
.get(START_FULLSCREEN
, onValues
);
132 remoting
.V2AppLauncher
.prototype.close = function(id
) {
135 * @param {function(*=):void} resolve
136 * @param {function(*=):void} reject
138 function(resolve
, reject
) {
139 var appWindow
= chrome
.app
.window
.get(id
);
141 return Promise
.reject(new Error(chrome
.runtime
.lastError
.message
));
143 appWindow
.onClosed
.addListener(resolve
);
149 * @return {number} The first available window id. Since Chrome remembers
150 * properties such as size and position for app windows, it is better
151 * to reuse window ids rather than allocating new ones.
153 function getNextWindowId() {
154 var appWindows
= chrome
.app
.window
.getAll();
156 for (; result
<= appWindows
.length
; ++result
) {
157 if (!chrome
.app
.window
.get(String(result
))) {