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 base.debug.assert(base.isBackgroundPage(), | |
103 'restart() can only be called from the background page'); | |
Jamie
2015/01/23 23:33:26
It would be better if this code file was simply no
kelvinp
2015/01/28 00:26:45
Done.
| |
104 this.close(id).then(function() { | |
105 return chrome.app.window.create(APP_MAIN_URL, {'id' : id, 'frame': 'none'}); | |
Jamie
2015/01/23 23:33:26
Please add a comment explaining why we're not re-u
kelvinp
2015/01/28 00:26:45
Done.
| |
106 }); | |
107 }; | |
99 | 108 |
100 remoting.V2AppLauncher.prototype.launch = function(opt_launchArgs) { | 109 remoting.V2AppLauncher.prototype.launch = function(opt_launchArgs) { |
101 var url = base.urlJoin('main.html', opt_launchArgs); | 110 var url = base.urlJoin(APP_MAIN_URL, opt_launchArgs); |
102 | 111 |
103 /** | 112 /** |
104 * @param {function(*=):void} resolve | 113 * @param {function(*=):void} resolve |
105 * @param {function(*=):void} reject | 114 * @param {function(*=):void} reject |
106 */ | 115 */ |
107 return new Promise(function(resolve, reject) { | 116 return new Promise(function(resolve, reject) { |
108 chrome.app.window.create(url, { | 117 chrome.app.window.create(url, { |
109 'width': 800, | 118 'width': 800, |
110 'height': 600, | 119 'height': 600, |
111 'frame': 'none', | 120 'frame': 'none', |
112 'id': String(remoting.V2AppLauncher.nextWindowId_++) | 121 'id': String(getNextWindowId()) |
113 }, | 122 }, |
114 /** @param {AppWindow=} appWindow */ | 123 /** @param {AppWindow=} appWindow */ |
115 function(appWindow) { | 124 function(appWindow) { |
116 if (!appWindow) { | 125 if (!appWindow) { |
117 reject(new Error(chrome.runtime.lastError.message)); | 126 reject(new Error(chrome.runtime.lastError.message)); |
118 } else { | 127 } else { |
119 resolve(appWindow.id); | 128 resolve(appWindow.id); |
120 } | 129 } |
121 }); | 130 }); |
122 }); | 131 }); |
123 }; | 132 }; |
124 | 133 |
125 remoting.V2AppLauncher.prototype.close = function(id) { | 134 remoting.V2AppLauncher.prototype.close = function(id) { |
126 var appWindow = chrome.app.window.get(id); | 135 /** |
127 if (!appWindow) { | 136 * @param {function(*=):void} resolve |
128 return Promise.reject(new Error(chrome.runtime.lastError.message)); | 137 * @param {function(*=):void} reject |
129 } | 138 */ |
130 appWindow.close(); | 139 return new Promise(function(resolve, reject) { |
131 return Promise.resolve(); | 140 var appWindow = chrome.app.window.get(id); |
141 if (!appWindow) { | |
142 return Promise.reject(new Error(chrome.runtime.lastError.message)); | |
143 } | |
144 appWindow.onClosed.addListener(resolve); | |
145 appWindow.close(); | |
146 }); | |
132 }; | 147 }; |
148 | |
149 /** | |
150 * @return {number} the next available window id. | |
151 */ | |
152 function getNextWindowId() { | |
153 var appWindows = chrome.app.window.getAll(); | |
154 var lastId = /** @type(number) */ (0); | |
155 appWindows.forEach(function(appWindow) { | |
156 base.debug.assert(Number.isInteger(appWindow.id), | |
157 "Window Id should be an integer"); | |
158 var id = parseInt(appWindow.id, 10); | |
159 if (lastId <= id) { | |
160 lastId = id; | |
161 } | |
162 }); | |
163 return ++lastId; | |
164 } | |
165 | |
166 })(); | |
OLD | NEW |