OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview | 6 * @fileoverview |
7 * AppLauncher is an interface that allows the client code to launch and close | 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 | 8 * the app without knowing the implementation difference between a v1 app and |
9 * a v2 app. | 9 * a v2 app. |
10 * | 10 * |
11 * To launch an app: | 11 * To launch an app: |
12 * var appLauncher = new remoting.V1AppLauncher(); | 12 * var appLauncher = new remoting.V1AppLauncher(); |
13 * var appId = ""; | 13 * var appId = ""; |
14 * appLauncher.launch({arg1:'someValue'}).then(function(id){ | 14 * appLauncher.launch({arg1:'someValue'}).then(function(id){ |
15 * appId = id; | 15 * appId = id; |
16 * }); | 16 * }); |
17 * | 17 * |
18 * To close an app: | 18 * To close an app: |
19 * appLauncher.close(appId); | 19 * appLauncher.close(appId); |
20 */ | 20 */ |
21 | 21 |
22 'use strict'; | |
23 | |
24 /** @suppress {duplicate} */ | 22 /** @suppress {duplicate} */ |
25 var remoting = remoting || {}; | 23 var remoting = remoting || {}; |
26 | 24 |
| 25 (function() { |
| 26 |
| 27 'use strict'; |
| 28 |
27 /** @interface */ | 29 /** @interface */ |
28 remoting.AppLauncher = function() {}; | 30 remoting.AppLauncher = function() {}; |
29 | 31 |
30 /** | 32 /** |
31 * @param {Object=} opt_launchArgs | 33 * @param {Object=} opt_launchArgs |
32 * @return {Promise} The promise will resolve when the app is launched. It will | 34 * @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 | 35 * 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 * or window). The caller can use the appId to close the app. |
35 */ | 37 */ |
36 remoting.AppLauncher.prototype.launch = function(opt_launchArgs) { }; | 38 remoting.AppLauncher.prototype.launch = function(opt_launchArgs) { }; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 }); | 86 }); |
85 }; | 87 }; |
86 | 88 |
87 | 89 |
88 /** | 90 /** |
89 * @constructor | 91 * @constructor |
90 * @implements {remoting.AppLauncher} | 92 * @implements {remoting.AppLauncher} |
91 */ | 93 */ |
92 remoting.V2AppLauncher = function() {}; | 94 remoting.V2AppLauncher = function() {}; |
93 | 95 |
| 96 var APP_MAIN_URL = 'main.html'; |
| 97 |
94 /** | 98 /** |
95 * @type {number} | 99 * @param {string} id |
96 * @private | |
97 */ | 100 */ |
98 remoting.V2AppLauncher.nextWindowId_ = 0; | 101 remoting.V2AppLauncher.prototype.restart = function(id) { |
| 102 this.close(id).then(function() { |
| 103 // Not using the launch() method because we want to launch a new window with |
| 104 // the same id, such that the size and positioning of the original window |
| 105 // can be preserved. |
| 106 return chrome.app.window.create(APP_MAIN_URL, {'id' : id, 'frame': 'none'}); |
| 107 }); |
| 108 }; |
99 | 109 |
100 remoting.V2AppLauncher.prototype.launch = function(opt_launchArgs) { | 110 remoting.V2AppLauncher.prototype.launch = function(opt_launchArgs) { |
101 var url = base.urlJoin('main.html', opt_launchArgs); | 111 var url = base.urlJoin(APP_MAIN_URL, opt_launchArgs); |
102 | 112 |
103 /** | 113 /** |
104 * @param {function(*=):void} resolve | 114 * @param {function(*=):void} resolve |
105 * @param {function(*=):void} reject | 115 * @param {function(*=):void} reject |
106 */ | 116 */ |
107 return new Promise(function(resolve, reject) { | 117 return new Promise(function(resolve, reject) { |
108 chrome.app.window.create(url, { | 118 chrome.app.window.create(url, { |
109 'width': 800, | 119 'width': 800, |
110 'height': 600, | 120 'height': 600, |
111 'frame': 'none', | 121 'frame': 'none', |
112 'id': String(remoting.V2AppLauncher.nextWindowId_++) | 122 'id': String(getNextWindowId()) |
113 }, | 123 }, |
114 /** @param {AppWindow=} appWindow */ | 124 /** @param {AppWindow=} appWindow */ |
115 function(appWindow) { | 125 function(appWindow) { |
116 if (!appWindow) { | 126 if (!appWindow) { |
117 reject(new Error(chrome.runtime.lastError.message)); | 127 reject(new Error(chrome.runtime.lastError.message)); |
118 } else { | 128 } else { |
119 resolve(appWindow.id); | 129 resolve(appWindow.id); |
120 } | 130 } |
121 }); | 131 }); |
122 }); | 132 }); |
123 }; | 133 }; |
124 | 134 |
125 remoting.V2AppLauncher.prototype.close = function(id) { | 135 remoting.V2AppLauncher.prototype.close = function(id) { |
126 var appWindow = chrome.app.window.get(id); | 136 /** |
127 if (!appWindow) { | 137 * @param {function(*=):void} resolve |
128 return Promise.reject(new Error(chrome.runtime.lastError.message)); | 138 * @param {function(*=):void} reject |
129 } | 139 */ |
130 appWindow.close(); | 140 return new Promise(function(resolve, reject) { |
131 return Promise.resolve(); | 141 var appWindow = chrome.app.window.get(id); |
| 142 if (!appWindow) { |
| 143 return Promise.reject(new Error(chrome.runtime.lastError.message)); |
| 144 } |
| 145 appWindow.onClosed.addListener(resolve); |
| 146 appWindow.close(); |
| 147 }); |
132 }; | 148 }; |
| 149 |
| 150 /** |
| 151 * @return {number} the next available window id. |
| 152 */ |
| 153 function getNextWindowId() { |
| 154 var appWindows = chrome.app.window.getAll(); |
| 155 var lastId = /** @type(number) */ (0); |
| 156 appWindows.forEach(function(appWindow) { |
| 157 base.debug.assert(Number.isInteger(appWindow.id), |
| 158 "Window Id should be an integer"); |
| 159 var id = parseInt(appWindow.id, 10); |
| 160 if (lastId <= id) { |
| 161 lastId = id; |
| 162 } |
| 163 }); |
| 164 return ++lastId; |
| 165 } |
| 166 |
| 167 })(); |
OLD | NEW |