OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 /** @suppress {duplicate} */ | |
6 var remoting = remoting || {}; | |
7 | |
8 /** | |
9 * Type definition for the RunApplicationResponse returned by the API. | |
10 * @typedef {{ | |
11 * status: string, | |
12 * hostJid: string, | |
13 * authorizationCode: string, | |
14 * sharedSecret: string, | |
15 * host: { | |
16 * applicationId: string, | |
17 * hostId: string | |
18 * } | |
19 * }} | |
20 */ | |
21 remoting.AppHostResponse; | |
22 | |
23 (function() { | |
24 | |
25 'use strict'; | |
26 | |
27 /** | |
28 * @constructor | |
29 * @implements {remoting.Activity} | |
30 */ | |
31 remoting.AppRemotingActivity = function() { | |
32 /** @private {remoting.AppConnectedView} */ | |
33 this.connectedView_ = null; | |
34 }; | |
35 | |
36 remoting.AppRemotingActivity.prototype.dispose = function() { | |
37 base.dispose(this.connectedView_); | |
38 this.connectedView_ = null; | |
39 remoting.LoadingWindow.close(); | |
40 }; | |
41 | |
42 remoting.AppRemotingActivity.prototype.start = function() { | |
43 remoting.LoadingWindow.show(); | |
44 var that = this; | |
45 return remoting.identity.getToken().then(function(/** string */ token) { | |
46 return that.getAppHostInfo_(token); | |
47 }).then(function(/** !remoting.Xhr.Response */ response) { | |
48 that.onAppHostResponse_(response); | |
49 }); | |
50 }; | |
51 | |
52 /** | |
53 * @param {string} token | |
54 * @return {Promise<!remoting.Xhr.Response>} | |
55 * @private | |
56 */ | |
57 remoting.AppRemotingActivity.prototype.getAppHostInfo_ = function(token) { | |
58 var url = remoting.settings.APP_REMOTING_API_BASE_URL + '/applications/' + | |
59 remoting.settings.getAppRemotingApplicationId() + '/run'; | |
60 return new remoting.Xhr({ | |
61 method: 'POST', | |
62 url: url, | |
63 oauthToken: token | |
64 }).start(); | |
65 }; | |
66 | |
67 /** | |
68 * @param {!remoting.Xhr.Response} xhrResponse | |
69 * @private | |
70 */ | |
71 remoting.AppRemotingActivity.prototype.onAppHostResponse_ = | |
72 function(xhrResponse) { | |
73 if (xhrResponse.status == 200) { | |
74 var response = /** @type {remoting.AppHostResponse} */ | |
75 (base.jsonParseSafe(xhrResponse.getText())); | |
76 if (response && | |
77 response.status && | |
78 response.status == 'done' && | |
79 response.hostJid && | |
80 response.authorizationCode && | |
81 response.sharedSecret && | |
82 response.host && | |
83 response.host.hostId) { | |
84 var hostJid = response.hostJid; | |
85 var host = new remoting.Host(response.host.hostId); | |
86 host.jabberId = hostJid; | |
87 host.authorizationCode = response.authorizationCode; | |
88 host.sharedSecret = response.sharedSecret; | |
89 | |
90 remoting.setMode(remoting.AppMode.CLIENT_CONNECTING); | |
91 | |
92 var idleDetector = new remoting.IdleDetector( | |
93 document.getElementById('idle-dialog'), | |
94 remoting.app.disconnect.bind(remoting.app)); | |
95 | |
96 /** | |
97 * @param {string} tokenUrl Token-issue URL received from the host. | |
98 * @param {string} hostPublicKey Host public key (DER and Base64 | |
99 * encoded). | |
100 * @param {string} scope OAuth scope to request the token for. | |
101 * @param {function(string, string):void} onThirdPartyTokenFetched | |
102 * Callback. | |
103 */ | |
104 var fetchThirdPartyToken = function( | |
105 tokenUrl, hostPublicKey, scope, onThirdPartyTokenFetched) { | |
106 // Use the authentication tokens returned by the app-remoting server. | |
107 onThirdPartyTokenFetched(host['authorizationCode'], | |
108 host['sharedSecret']); | |
109 }; | |
110 | |
111 var connector = remoting.SessionConnector.factory.createConnector( | |
112 document.getElementById('client-container'), | |
113 remoting.app_capabilities(), this); | |
garykac
2015/04/15 00:52:37
Having this call out to the global might make this
| |
114 | |
115 connector.connect(remoting.Application.Mode.APP_REMOTING, host, | |
116 new remoting.CredentialsProvider( | |
117 {fetchThirdPartyToken: fetchThirdPartyToken})); | |
118 } else if (response && response.status == 'pending') { | |
119 this.onError(new remoting.Error( | |
120 remoting.Error.Tag.SERVICE_UNAVAILABLE)); | |
121 } | |
122 } else { | |
123 console.error('Invalid "runApplication" response from server.'); | |
124 this.onError(remoting.Error.fromHttpStatus(xhrResponse.status)); | |
125 } | |
126 }; | |
127 | |
128 /** | |
129 * @param {remoting.ConnectionInfo} connectionInfo | |
130 */ | |
131 remoting.AppRemotingActivity.prototype.onConnected = function(connectionInfo) { | |
132 this.connectedView_ = new remoting.AppConnectedView( | |
133 document.getElementById('client-container'), connectionInfo); | |
134 | |
135 // Map Cmd to Ctrl on Mac since hosts typically use Ctrl for keyboard | |
136 // shortcuts, but we want them to act as natively as possible. | |
137 if (remoting.platformIsMac()) { | |
138 connectionInfo.plugin().setRemapKeys('0x0700e3>0x0700e0,0x0700e7>0x0700e4'); | |
139 } | |
140 }; | |
141 | |
142 remoting.AppRemotingActivity.prototype.onDisconnected = function() { | |
143 base.dispose(this.connectedView_); | |
144 this.connectedView_ = null; | |
145 chrome.app.window.current().close(); | |
146 }; | |
147 | |
148 /** | |
149 * @param {!remoting.Error} error | |
150 */ | |
151 remoting.AppRemotingActivity.prototype.onConnectionFailed = function(error) { | |
152 this.onError(error); | |
153 }; | |
154 | |
155 /** | |
156 * @param {!remoting.Error} error The error to be localized and displayed. | |
157 */ | |
158 remoting.AppRemotingActivity.prototype.onError = function(error) { | |
159 console.error('Connection failed: ' + error.toString()); | |
160 remoting.LoadingWindow.close(); | |
161 remoting.MessageWindow.showErrorMessage( | |
162 chrome.i18n.getMessage(/*i18n-content*/'CONNECTION_FAILED'), | |
163 chrome.i18n.getMessage(error.getTag())); | |
164 base.dispose(this.connectedView_); | |
165 this.connectedView_ = null; | |
166 }; | |
167 | |
168 })(); | |
OLD | NEW |