OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 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. |
| 10 * |
| 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 * }); |
| 17 * |
| 18 * To close an app: |
| 19 * appLauncher.close(appId); |
| 20 */ |
| 21 |
| 22 'use strict'; |
| 23 |
| 24 /** @suppress {duplicate} */ |
| 25 var remoting = remoting || {}; |
| 26 |
| 27 /** @interface */ |
| 28 remoting.AppLauncher = function() {}; |
| 29 |
| 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. |
| 35 */ |
| 36 remoting.AppLauncher.prototype.launch = function(opt_launchArgs) { }; |
| 37 |
| 38 /** |
| 39 * @param {string} id The id of the app to close. |
| 40 * @return {Promise} The promise will resolve when the app is closed. |
| 41 */ |
| 42 remoting.AppLauncher.prototype.close = function(id) {}; |
| 43 |
| 44 /** |
| 45 * @constructor |
| 46 * @implements {remoting.AppLauncher} |
| 47 */ |
| 48 remoting.V1AppLauncher = function() {}; |
| 49 |
| 50 remoting.V1AppLauncher.prototype.launch = function(opt_launchArgs) { |
| 51 var url = base.urlJoin('main.html', opt_launchArgs); |
| 52 |
| 53 /** |
| 54 * @param {function(*=):void} resolve |
| 55 * @param {function(*=):void} reject |
| 56 */ |
| 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(tab.id); |
| 65 } |
| 66 }); |
| 67 }); |
| 68 }; |
| 69 |
| 70 remoting.V1AppLauncher.prototype.close = function(id) { |
| 71 /** |
| 72 * @param {function(*=):void} resolve |
| 73 * @param {function(*=):void} reject |
| 74 */ |
| 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); |
| 82 } |
| 83 }); |
| 84 }); |
| 85 }; |
| 86 |
| 87 |
| 88 /** |
| 89 * @constructor |
| 90 * @implements {remoting.AppLauncher} |
| 91 */ |
| 92 remoting.V2AppLauncher = function() {}; |
| 93 |
| 94 /** |
| 95 * @type {number} |
| 96 * @private |
| 97 */ |
| 98 remoting.V2AppLauncher.nextWindowId_ = 0; |
| 99 |
| 100 remoting.V2AppLauncher.prototype.launch = function(opt_launchArgs) { |
| 101 var url = base.urlJoin('main.html', opt_launchArgs); |
| 102 |
| 103 /** |
| 104 * @param {function(*=):void} resolve |
| 105 * @param {function(*=):void} reject |
| 106 */ |
| 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_++) |
| 113 }, |
| 114 /** @param {AppWindow} appWindow */ |
| 115 function(appWindow) { |
| 116 if (!appWindow) { |
| 117 reject(new Error(chrome.runtime.lastError.message)); |
| 118 } else { |
| 119 resolve(appWindow.id); |
| 120 } |
| 121 }); |
| 122 }); |
| 123 }; |
| 124 |
| 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)); |
| 129 } |
| 130 appWindow.close(); |
| 131 return Promise.resolve(); |
| 132 }; |
OLD | NEW |