| 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 (function() { | |
| 9 | |
| 10 'use strict'; | |
| 11 | |
| 12 /** | |
| 13 * @param {remoting.SessionConnector} sessionConnector | |
| 14 * @param {remoting.Host} host | |
| 15 * | |
| 16 * @constructor | |
| 17 */ | |
| 18 remoting.Me2MeConnectFlow = function(sessionConnector, host) { | |
| 19 /** @private */ | |
| 20 this.host_ = host; | |
| 21 /** @private */ | |
| 22 this.connector_ = sessionConnector; | |
| 23 }; | |
| 24 | |
| 25 remoting.Me2MeConnectFlow.prototype.start = function() { | |
| 26 var webappVersion = chrome.runtime.getManifest().version; | |
| 27 var needsUpdateDialog = new remoting.HostNeedsUpdateDialog( | |
| 28 document.getElementById('host-needs-update-dialog'), this.host_); | |
| 29 var that = this; | |
| 30 | |
| 31 needsUpdateDialog.showIfNecessary(webappVersion).then(function() { | |
| 32 return that.host_.options.load(); | |
| 33 }).then(function() { | |
| 34 that.connect_(); | |
| 35 }).catch(function(/** remoting.Error */ error) { | |
| 36 if (error.hasTag(remoting.Error.Tag.CANCELLED)) { | |
| 37 remoting.setMode(remoting.AppMode.HOME); | |
| 38 } | |
| 39 }); | |
| 40 }; | |
| 41 | |
| 42 /** @private */ | |
| 43 remoting.Me2MeConnectFlow.prototype.connect_ = function() { | |
| 44 remoting.setMode(remoting.AppMode.CLIENT_CONNECTING); | |
| 45 var host = this.host_; | |
| 46 | |
| 47 /** | |
| 48 * @param {string} tokenUrl Token-issue URL received from the host. | |
| 49 * @param {string} hostPublicKey Host public key (DER and Base64 encoded). | |
| 50 * @param {string} scope OAuth scope to request the token for. | |
| 51 * @param {function(string, string):void} onThirdPartyTokenFetched Callback. | |
| 52 */ | |
| 53 var fetchThirdPartyToken = function( | |
| 54 tokenUrl, hostPublicKey, scope, onThirdPartyTokenFetched) { | |
| 55 var thirdPartyTokenFetcher = new remoting.ThirdPartyTokenFetcher( | |
| 56 tokenUrl, hostPublicKey, scope, host.tokenUrlPatterns, | |
| 57 onThirdPartyTokenFetched); | |
| 58 thirdPartyTokenFetcher.fetchToken(); | |
| 59 }; | |
| 60 | |
| 61 /** | |
| 62 * @param {boolean} supportsPairing | |
| 63 * @param {function(string):void} onPinFetched | |
| 64 */ | |
| 65 var requestPin = function(supportsPairing, onPinFetched) { | |
| 66 var pinDialog = | |
| 67 new remoting.PinDialog(document.getElementById('pin-dialog'), host); | |
| 68 pinDialog.show(supportsPairing).then(function(/** string */ pin) { | |
| 69 remoting.setMode(remoting.AppMode.CLIENT_CONNECTING); | |
| 70 onPinFetched(pin); | |
| 71 /** @type {boolean} */ | |
| 72 remoting.pairingRequested = pinDialog.pairingRequested(); | |
| 73 }).catch(function(/** remoting.Error */ error) { | |
| 74 base.debug.assert(error.hasTag(remoting.Error.Tag.CANCELLED)); | |
| 75 remoting.setMode(remoting.AppMode.HOME); | |
| 76 }); | |
| 77 }; | |
| 78 | |
| 79 var pairingInfo = host.options.pairingInfo; | |
| 80 this.connector_.connectMe2Me(host, requestPin, fetchThirdPartyToken, | |
| 81 pairingInfo.clientId, pairingInfo.sharedSecret); | |
| 82 }; | |
| 83 | |
| 84 | |
| 85 /** | |
| 86 * @param {HTMLElement} rootElement | |
| 87 * @param {remoting.Host} host | |
| 88 * @constructor | |
| 89 */ | |
| 90 remoting.HostNeedsUpdateDialog = function(rootElement, host) { | |
| 91 /** @private */ | |
| 92 this.host_ = host; | |
| 93 /** @private */ | |
| 94 this.rootElement_ = rootElement; | |
| 95 /** @private {base.Deferred} */ | |
| 96 this.deferred_ = null; | |
| 97 /** @private {base.Disposables} */ | |
| 98 this.eventHooks_ = null; | |
| 99 | |
| 100 l10n.localizeElementFromTag( | |
| 101 rootElement.querySelector('.host-needs-update-message'), | |
| 102 /*i18n-content*/'HOST_NEEDS_UPDATE_TITLE', host.hostName); | |
| 103 }; | |
| 104 | |
| 105 /** | |
| 106 * Shows the HostNeedsUpdateDialog if the user is trying to connect to a legacy | |
| 107 * host. | |
| 108 * | |
| 109 * @param {string} webappVersion | |
| 110 * @return {Promise} Promise that resolves when no update is required or | |
| 111 * when the user ignores the update warning. Rejects with | |
| 112 * |remoting.Error.CANCELLED| if the user cancels the connection. | |
| 113 */ | |
| 114 remoting.HostNeedsUpdateDialog.prototype.showIfNecessary = | |
| 115 function(webappVersion) { | |
| 116 if (!remoting.Host.needsUpdate(this.host_, webappVersion)) { | |
| 117 return Promise.resolve(); | |
| 118 } | |
| 119 | |
| 120 this.eventHooks_ = new base.Disposables( | |
| 121 new base.DomEventHook(this.rootElement_.querySelector('.connect-button'), | |
| 122 'click', this.onOK_.bind(this), false), | |
| 123 new base.DomEventHook(this.rootElement_.querySelector('.cancel-button'), | |
| 124 'click', this.onCancel_.bind(this), false)); | |
| 125 | |
| 126 base.debug.assert(this.deferred_ === null); | |
| 127 this.deferred_ = new base.Deferred(); | |
| 128 | |
| 129 remoting.setMode(remoting.AppMode.CLIENT_HOST_NEEDS_UPGRADE); | |
| 130 | |
| 131 return this.deferred_.promise(); | |
| 132 }; | |
| 133 | |
| 134 /** @private */ | |
| 135 remoting.HostNeedsUpdateDialog.prototype.cleanup_ = function() { | |
| 136 base.dispose(this.eventHooks_); | |
| 137 this.eventHooks_ = null; | |
| 138 this.deferred_ = null; | |
| 139 }; | |
| 140 | |
| 141 | |
| 142 /** @private */ | |
| 143 remoting.HostNeedsUpdateDialog.prototype.onOK_ = function() { | |
| 144 this.deferred_.resolve(); | |
| 145 this.cleanup_(); | |
| 146 }; | |
| 147 | |
| 148 /** @private */ | |
| 149 remoting.HostNeedsUpdateDialog.prototype.onCancel_ = function() { | |
| 150 this.deferred_.reject(new remoting.Error(remoting.Error.Tag.CANCELLED)); | |
| 151 this.cleanup_(); | |
| 152 }; | |
| 153 | |
| 154 /** | |
| 155 * @param {HTMLElement} rootElement | |
| 156 * @param {remoting.Host} host | |
| 157 * @constructor | |
| 158 */ | |
| 159 remoting.PinDialog = function(rootElement, host) { | |
| 160 /** @private */ | |
| 161 this.rootElement_ = rootElement; | |
| 162 /** @private */ | |
| 163 this.pairingCheckbox_ = rootElement.querySelector('.pairing-checkbox'); | |
| 164 /** @private */ | |
| 165 this.pinInput_ = rootElement.querySelector('.pin-inputField'); | |
| 166 /** @private */ | |
| 167 this.host_ = host; | |
| 168 /** @private */ | |
| 169 this.dialog_ = new remoting.InputDialog( | |
| 170 remoting.AppMode.CLIENT_PIN_PROMPT, | |
| 171 this.rootElement_.querySelector('form'), | |
| 172 this.pinInput_, | |
| 173 this.rootElement_.querySelector('.cancel-button')); | |
| 174 }; | |
| 175 | |
| 176 | |
| 177 /** | |
| 178 * @param {boolean} supportsPairing | |
| 179 * @return {Promise<string>} Promise that resolves with the PIN or rejects with | |
| 180 * |remoting.Error.CANCELLED| if the user cancels the connection. | |
| 181 */ | |
| 182 remoting.PinDialog.prototype.show = function(supportsPairing) { | |
| 183 // Reset the UI. | |
| 184 this.pairingCheckbox_.checked = false; | |
| 185 this.rootElement_.querySelector('.pairing-section').hidden = !supportsPairing; | |
| 186 var message = this.rootElement_.querySelector('.pin-message'); | |
| 187 l10n.localizeElement(message, this.host_.hostName); | |
| 188 this.pinInput_.value = ''; | |
| 189 return this.dialog_.show(); | |
| 190 }; | |
| 191 | |
| 192 /** @return {boolean} */ | |
| 193 remoting.PinDialog.prototype.pairingRequested = function() { | |
| 194 return this.pairingCheckbox_.checked; | |
| 195 }; | |
| 196 | |
| 197 })(); | |
| OLD | NEW |