GN + Android: extract android_standalone_library rule.
[chromium-blink-merge.git] / remoting / webapp / crd / js / app_launcher.js
blobb506a6cbbe4fd4a72c7eb97381ef35f495709f83
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 'use strict';
24 /** @suppress {duplicate} */
25 var remoting = remoting || {};
27 /** @interface */
28 remoting.AppLauncher = function() {};
30 /**
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) { };
38 /**
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) {};
44 /**
45 * @constructor
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);
53 /**
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. */
60 function(tab) {
61 if (!tab) {
62 reject(new Error(chrome.runtime.lastError.message));
63 } else {
64 resolve(String(tab.id));
66 });
67 });
70 remoting.V1AppLauncher.prototype.close = function(id) {
71 /**
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) {
78 if (!tab) {
79 reject(new Error(chrome.runtime.lastError.message));
80 } else {
81 chrome.tabs.remove(tab.id, resolve);
83 });
84 });
88 /**
89 * @constructor
90 * @implements {remoting.AppLauncher}
92 remoting.V2AppLauncher = function() {};
94 /**
95 * @type {number}
96 * @private
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, {
109 'width': 800,
110 'height': 600,
111 'frame': 'none',
112 'id': String(remoting.V2AppLauncher.nextWindowId_++)
114 /** @param {AppWindow} appWindow */
115 function(appWindow) {
116 if (!appWindow) {
117 reject(new Error(chrome.runtime.lastError.message));
118 } else {
119 resolve(appWindow.id);
125 remoting.V2AppLauncher.prototype.close = function(id) {
126 var appWindow = chrome.app.window.get(id);
127 if (!appWindow) {
128 return Promise.reject(new Error(chrome.runtime.lastError.message));
130 appWindow.close();
131 return Promise.resolve();