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 for app launching closing implementations. | |
8 * It nows the client code to launch and close the app without knowing the | |
9 * implementation difference between launching a v1 app and a v2 app. | |
Jamie
2014/08/12 02:24:56
Can you clean up this comment? Specifically, I don
kelvinp
2014/08/12 18:22:44
Done.
| |
10 */ | |
11 | |
12 'use strict'; | |
13 | |
14 /** @suppress {duplicate} */ | |
15 var remoting = remoting || {}; | |
16 | |
17 /** @interface */ | |
18 remoting.AppLauncher = function() {}; | |
19 | |
20 /** | |
21 * @param {Object=} opt_launchArgs | |
22 * @return {Promise} | |
Jamie
2014/08/12 02:24:56
The methods of this interface need descriptions, o
Jamie
2014/08/12 02:24:56
Make it clear that when the Promise resolves, it w
kelvinp
2014/08/12 18:22:44
Done.
| |
23 */ | |
24 remoting.AppLauncher.prototype.launch = function(opt_launchArgs) { }; | |
Jamie
2014/08/12 02:24:56
Nit: No space between braces.
kelvinp
2014/08/12 18:22:44
Done.
| |
25 | |
26 /** | |
27 * @param {string} id The id of the tab/window to close. | |
28 * @return {Promise} | |
29 */ | |
30 remoting.AppLauncher.prototype.close = function(id) {}; | |
31 | |
32 /** | |
33 * @param {string} url | |
34 * @param {Object.<string>=} opt_params | |
35 * @return {string} | |
36 */ | |
37 remoting.AppLauncher.urlJoin = function(url, opt_params) { | |
Jamie
2014/08/12 02:24:56
Interfaces should not have concrete methods, and i
kelvinp
2014/08/12 18:22:44
Moved to base.js
| |
38 if (!opt_params) { | |
39 return url; | |
40 } | |
41 var queryParameters = []; | |
42 for (var key in opt_params) { | |
43 queryParameters.push(encodeURIComponent(key) + "=" + | |
44 encodeURIComponent(opt_params[key])); | |
45 } | |
46 return url + '?' + queryParameters.join('&'); | |
47 }; | |
48 | |
49 /** | |
50 * @constructor | |
51 * @implements {remoting.AppLauncher} | |
52 */ | |
53 remoting.V1AppLauncher = function() {}; | |
54 | |
55 remoting.V1AppLauncher.prototype.launch = function(opt_launchArgs) { | |
56 var url = remoting.AppLauncher.urlJoin('main.html', opt_launchArgs); | |
57 | |
58 /** | |
59 * @param {function(*=):void} resolve | |
60 * @param {function(*=):void} reject | |
61 */ | |
62 return new Promise(function(resolve, reject) { | |
63 chrome.tabs.create({ url: url, selected: true}, | |
Jamie
2014/08/12 02:24:56
Nit: No space after '{' (or add one after 'true').
kelvinp
2014/08/12 18:22:44
Done.
| |
64 /** @param {chrome.Tab} tab The created tab. */ | |
65 function(tab) { | |
66 if (!tab) { | |
67 reject(chrome.runtime.lastError); | |
Jamie
2014/08/12 02:24:56
Is it better to use new Error to get a stack trace
| |
68 } else { | |
69 resolve(tab.id); | |
70 } | |
71 }); | |
72 }); | |
73 }; | |
74 | |
75 remoting.V1AppLauncher.prototype.close = function(id) { | |
76 /** | |
77 * @param {function(*=):void} resolve | |
78 * @param {function(*=):void} reject | |
79 */ | |
80 return new Promise(function(resolve, reject) { | |
81 /** @param {chrome.Tab} tab The retrieved tab. */ | |
82 chrome.tabs.get(id, function(tab) { | |
83 if (!tab) { | |
84 reject(chrome.runtime.lastError.message); | |
Jamie
2014/08/12 02:24:56
Why .message here but not in launch?
kelvinp
2014/08/12 18:22:44
Good catch. Typo in launch
| |
85 } else { | |
86 chrome.tabs.remove(tab.id, function(){ | |
Jamie
2014/08/12 02:24:56
Do you need the wrapper function?
kelvinp
2014/08/12 18:22:44
Done.
| |
87 resolve(); | |
88 }); | |
89 } | |
90 }); | |
91 }); | |
92 }; | |
93 | |
94 | |
95 /** | |
96 * @constructor | |
97 * @implements {remoting.AppLauncher} | |
98 */ | |
99 remoting.V2AppLauncher = function() {}; | |
100 | |
101 remoting.V2AppLauncher.nextWindowId_ = 0; | |
Jamie
2014/08/12 02:24:56
@type and @private annotations, please.
| |
102 | |
103 remoting.V2AppLauncher.prototype.launch = function(opt_launchArgs) { | |
104 var url = remoting.AppLauncher.urlJoin('main.html', opt_launchArgs); | |
105 | |
106 /** | |
107 * @param {function(*=):void} resolve | |
108 * @param {function(*=):void} reject | |
109 */ | |
110 return new Promise(function(resolve, reject) { | |
111 chrome.app.window.create(url, { | |
112 'width': 800, | |
113 'height': 600, | |
114 'frame': 'none', | |
115 'id': "hangout" + remoting.V2AppLauncher.nextWindowId_++ | |
Jamie
2014/08/12 02:24:56
I think the id would be better managed by the call
kelvinp
2014/08/12 18:22:44
You are right. Prefix removed.
| |
116 }, | |
117 /** @param {AppWindow} appWindow */ | |
118 function(appWindow) { | |
119 if (!appWindow) { | |
120 reject(chrome.runtime.lastError.message); | |
121 } else { | |
122 resolve(appWindow.id); | |
123 } | |
124 }); | |
125 }); | |
126 }; | |
127 | |
128 remoting.V2AppLauncher.prototype.close = function(id) { | |
129 var appWindow = chrome.app.window.get(id); | |
130 if (!appWindow) { | |
131 return Promise.reject(chrome.runtime.lastError.message); | |
132 } | |
133 appWindow.close(); | |
134 return Promise.resolve(); | |
135 }; | |
OLD | NEW |