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 var START_FULLSCREEN = 'start-fullscreen'; | 118 var START_FULLSCREEN = 'start-fullscreen'; |
109 /** @param {Object} values */ | 119 /** @param {Object} values */ |
110 var onValues = function(values) { | 120 var onValues = function(values) { |
111 /** @type {string} */ | 121 /** @type {string} */ |
112 var state = values[START_FULLSCREEN] ? 'fullscreen' : 'normal'; | 122 var state = values[START_FULLSCREEN] ? 'fullscreen' : 'normal'; |
113 chrome.app.window.create(url, { | 123 chrome.app.window.create(url, { |
114 'width': 800, | 124 'width': 800, |
115 'height': 600, | 125 'height': 600, |
116 'frame': 'none', | 126 'frame': 'none', |
117 'id': String(remoting.V2AppLauncher.nextWindowId_++), | 127 'id': String(getNextWindowId()), |
118 'state': state | 128 'state': state |
119 }, | 129 }, |
120 /** @param {AppWindow=} appWindow */ | 130 /** @param {AppWindow=} appWindow */ |
121 function(appWindow) { | 131 function(appWindow) { |
122 if (!appWindow) { | 132 if (!appWindow) { |
123 reject(new Error(chrome.runtime.lastError.message)); | 133 reject(new Error(chrome.runtime.lastError.message)); |
124 } else { | 134 } else { |
125 resolve(appWindow.id); | 135 resolve(appWindow.id); |
126 } | 136 } |
127 }); | 137 }); |
128 }; | 138 }; |
129 chrome.storage.local.get(START_FULLSCREEN, onValues); | 139 chrome.storage.local.get(START_FULLSCREEN, onValues); |
130 }); | 140 }); |
131 }; | 141 }; |
132 | 142 |
133 remoting.V2AppLauncher.prototype.close = function(id) { | 143 remoting.V2AppLauncher.prototype.close = function(id) { |
134 var appWindow = chrome.app.window.get(id); | 144 /** |
135 if (!appWindow) { | 145 * @param {function(*=):void} resolve |
136 return Promise.reject(new Error(chrome.runtime.lastError.message)); | 146 * @param {function(*=):void} reject |
137 } | 147 */ |
138 appWindow.close(); | 148 return new Promise(function(resolve, reject) { |
139 return Promise.resolve(); | 149 var appWindow = chrome.app.window.get(id); |
| 150 if (!appWindow) { |
| 151 return Promise.reject(new Error(chrome.runtime.lastError.message)); |
| 152 } |
| 153 appWindow.onClosed.addListener(resolve); |
| 154 appWindow.close(); |
| 155 }); |
140 }; | 156 }; |
| 157 |
| 158 /** |
| 159 * @return {number} the next available window id. |
| 160 */ |
| 161 function getNextWindowId() { |
| 162 var appWindows = chrome.app.window.getAll(); |
| 163 var lastId = /** @type(number) */ (0); |
| 164 appWindows.forEach(function(appWindow) { |
| 165 base.debug.assert(Number.isInteger(appWindow.id), |
| 166 "Window Id should be an integer"); |
| 167 var id = parseInt(appWindow.id, 10); |
| 168 if (lastId <= id) { |
| 169 lastId = id; |
| 170 } |
| 171 }); |
| 172 return ++lastId; |
| 173 } |
| 174 |
| 175 })(); |
OLD | NEW |