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