Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / remoting / webapp / crd / js / app_launcher.js
blob25b39ac52f7f2d2fa01d6634826db6add819eb7b
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.
5 /**
6 * @fileoverview
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
9 * a v2 app.
11 * To launch an app:
12 * var appLauncher = new remoting.V1AppLauncher();
13 * var appId = "";
14 * appLauncher.launch({arg1:'someValue'}).then(function(id){
15 * appId = id;
16 * });
18 * To close an app:
19 * appLauncher.close(appId);
22 /** @suppress {duplicate} */
23 var remoting = remoting || {};
25 (function() {
27 'use strict';
29 /** @interface */
30 remoting.AppLauncher = function() {};
32 /**
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) { };
40 /**
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) {};
46 /**
47 * @constructor
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){
57 if (!tab) {
58 reject(new Error(chrome.runtime.lastError.message));
59 } else {
60 resolve(String(tab.id));
62 });
63 });
66 remoting.V1AppLauncher.prototype.close = function(id) {
67 return new Promise(function(resolve, reject) {
68 chrome.tabs.get(parseInt(id, 10), function(/** Tab */ tab) {
69 if (!tab) {
70 reject(new Error(chrome.runtime.lastError.message));
71 } else {
72 chrome.tabs.remove(tab.id, resolve);
74 });
75 });
79 /**
80 * @constructor
81 * @implements {remoting.AppLauncher}
83 remoting.V2AppLauncher = function() {};
85 var APP_MAIN_URL = 'main.html';
87 /**
88 * @param {string} id
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
94 // can be preserved.
95 chrome.app.window.create(APP_MAIN_URL, {'id' : id, 'frame': 'none'});
96 });
99 remoting.V2AppLauncher.prototype.launch = function(opt_launchArgs) {
100 var url = base.urlJoin(APP_MAIN_URL, opt_launchArgs);
102 return new Promise(
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, {
114 'width': 800,
115 'height': 600,
116 'frame': 'none',
117 'id': String(getNextWindowId()),
118 'state': state
120 function(/** chrome.app.window.AppWindow= */ appWindow) {
121 if (!appWindow) {
122 reject(new Error(chrome.runtime.lastError.message));
123 } else {
124 resolve(appWindow.id);
128 chrome.storage.local.get(START_FULLSCREEN, onValues);
132 remoting.V2AppLauncher.prototype.close = function(id) {
133 return new Promise(
135 * @param {function(*=):void} resolve
136 * @param {function(*=):void} reject
138 function(resolve, reject) {
139 var appWindow = chrome.app.window.get(id);
140 if (!appWindow) {
141 return Promise.reject(new Error(chrome.runtime.lastError.message));
143 appWindow.onClosed.addListener(resolve);
144 appWindow.close();
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();
155 var result = 1;
156 for (; result <= appWindows.length; ++result) {
157 if (!chrome.app.window.get(String(result))) {
158 break;
161 return result;
164 })();