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';
89 * @return {Promise<string>} A promise that resolves with the id of the created
90 * window, which is guaranteed to be the same as the current window id.
92 remoting.V2AppLauncher.prototype.restart = function(id) {
93 return this.close(id).then(function() {
94 return createWindow_({'id' : id, 'frame': 'none'});
95 }).then(function (/** string */ newId) {
96 console.assert(newId === id, 'restart() should preserve the window id.');
102 * @param {!chrome.app.window.CreateWindowOptions=} opt_options
103 * @param {Object=} opt_launchArgs
104 * @return {Promise<string>} A promise that resolves with the id of the created
107 function createWindow_(opt_options, opt_launchArgs) {
108 var url = base.urlJoin(APP_MAIN_URL, opt_launchArgs);
109 var deferred = new base.Deferred();
111 chrome.app.window.create(url, opt_options,
112 function(/** chrome.app.window.AppWindow= */ appWindow) {
114 deferred.reject(new Error(chrome.runtime.lastError.message));
116 deferred.resolve(appWindow.id);
119 return deferred.promise();
122 remoting.V2AppLauncher.prototype.launch = function(opt_launchArgs) {
123 var deferred = new base.Deferred();
124 var START_FULLSCREEN = 'start-fullscreen';
126 /** @param {Object} values */
127 var onValues = function(values) {
128 /** @type {string} */
129 var state = values[START_FULLSCREEN] ? 'fullscreen' : 'normal';
134 'id': String(getNextWindowId()),
136 }, opt_launchArgs).then(
137 deferred.resolve.bind(deferred),
138 deferred.reject.bind(deferred)
141 chrome.storage.local.get(START_FULLSCREEN, onValues);
142 return deferred.promise();
145 remoting.V2AppLauncher.prototype.close = function(id) {
148 * @param {function(*=):void} resolve
149 * @param {function(*=):void} reject
151 function(resolve, reject) {
152 var appWindow = chrome.app.window.get(id);
154 return Promise.reject(new Error(chrome.runtime.lastError.message));
156 appWindow.onClosed.addListener(resolve);
162 * @return {number} The first available window id. Since Chrome remembers
163 * properties such as size and position for app windows, it is better
164 * to reuse window ids rather than allocating new ones.
166 function getNextWindowId() {
167 var appWindows = chrome.app.window.getAll();
169 for (; result <= appWindows.length; ++result) {
170 if (!chrome.app.window.get(String(result))) {