| 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 'use strict'; | |
| 6 | |
| 7 /** @suppress {duplicate} */ | |
| 8 var remoting = remoting || {}; | |
| 9 | |
| 10 (function() { | |
| 11 | |
| 12 /** @enum */ | |
| 13 var BlackholeState = { | |
| 14 PENDING: 0, | |
| 15 OPEN: 1, | |
| 16 BLOCKED: 2 | |
| 17 }; | |
| 18 | |
| 19 /** | |
| 20 * A SignalStrategy wrapper that performs DNS blackhole check. | |
| 21 * | |
| 22 * @param {remoting.SignalStrategy} signalStrategy | |
| 23 * @constructor | |
| 24 * @implements {remoting.SignalStrategy} | |
| 25 */ | |
| 26 remoting.DnsBlackholeChecker = function(signalStrategy) { | |
| 27 /** @private */ | |
| 28 this.signalStrategy_ = signalStrategy; | |
| 29 this.signalStrategy_.setStateChangedCallback( | |
| 30 this.onWrappedSignalStrategyStateChanged_.bind(this)); | |
| 31 | |
| 32 /** @private {?function(remoting.SignalStrategy.State):void} */ | |
| 33 this.onStateChangedCallback_ = null; | |
| 34 | |
| 35 /** @private */ | |
| 36 this.state_ = remoting.SignalStrategy.State.NOT_CONNECTED; | |
| 37 | |
| 38 /** @private */ | |
| 39 this.blackholeState_ = BlackholeState.PENDING; | |
| 40 | |
| 41 /** @private {?remoting.Xhr} */ | |
| 42 this.xhr_ = null; | |
| 43 }; | |
| 44 | |
| 45 /** | |
| 46 * @const | |
| 47 * @private | |
| 48 */ | |
| 49 remoting.DnsBlackholeChecker.URL_TO_REQUEST_ = | |
| 50 "https://chromoting-client.talkgadget.google.com/talkgadget/oauth/" + | |
| 51 "chrome-remote-desktop-client"; | |
| 52 | |
| 53 /** | |
| 54 * @param {?function(remoting.SignalStrategy.State):void} onStateChangedCallback | |
| 55 * Callback to call on state change. | |
| 56 */ | |
| 57 remoting.DnsBlackholeChecker.prototype.setStateChangedCallback = function( | |
| 58 onStateChangedCallback) { | |
| 59 this.onStateChangedCallback_ = onStateChangedCallback; | |
| 60 }; | |
| 61 | |
| 62 /** | |
| 63 * @param {?function(Element):void} onIncomingStanzaCallback Callback to call on | |
| 64 * incoming messages. | |
| 65 */ | |
| 66 remoting.DnsBlackholeChecker.prototype.setIncomingStanzaCallback = | |
| 67 function(onIncomingStanzaCallback) { | |
| 68 this.signalStrategy_.setIncomingStanzaCallback(onIncomingStanzaCallback); | |
| 69 }; | |
| 70 | |
| 71 /** @return {remoting.SignalStrategy.Type} The signal strategy type. */ | |
| 72 remoting.DnsBlackholeChecker.prototype.getType = function() { | |
| 73 return this.signalStrategy_.getType(); | |
| 74 }; | |
| 75 | |
| 76 /** | |
| 77 * @param {string} server | |
| 78 * @param {string} username | |
| 79 * @param {string} authToken | |
| 80 */ | |
| 81 remoting.DnsBlackholeChecker.prototype.connect = function(server, | |
| 82 username, | |
| 83 authToken) { | |
| 84 base.debug.assert(this.onStateChangedCallback_ != null); | |
| 85 | |
| 86 this.signalStrategy_.connect(server, username, authToken); | |
| 87 | |
| 88 this.xhr_ = new remoting.Xhr({ | |
| 89 method: 'GET', | |
| 90 url: remoting.DnsBlackholeChecker.URL_TO_REQUEST_ | |
| 91 }); | |
| 92 this.xhr_.start().then(this.onHttpRequestDone_.bind(this)); | |
| 93 }; | |
| 94 | |
| 95 remoting.DnsBlackholeChecker.prototype.getState = function() { | |
| 96 return this.state_; | |
| 97 }; | |
| 98 | |
| 99 remoting.DnsBlackholeChecker.prototype.getError = function() { | |
| 100 if (this.blackholeState_ == BlackholeState.BLOCKED) { | |
| 101 return new remoting.Error(remoting.Error.Tag.NOT_AUTHORIZED); | |
| 102 } | |
| 103 | |
| 104 return this.signalStrategy_.getError(); | |
| 105 }; | |
| 106 | |
| 107 remoting.DnsBlackholeChecker.prototype.getJid = function() { | |
| 108 base.debug.assert(this.state_ == remoting.SignalStrategy.State.CONNECTED); | |
| 109 return this.signalStrategy_.getJid(); | |
| 110 }; | |
| 111 | |
| 112 remoting.DnsBlackholeChecker.prototype.dispose = function() { | |
| 113 this.xhr_ = null; | |
| 114 base.dispose(this.signalStrategy_); | |
| 115 this.setState_(remoting.SignalStrategy.State.CLOSED); | |
| 116 }; | |
| 117 | |
| 118 /** | |
| 119 * @param {remoting.LogToServer} logToServer The LogToServer instance for the | |
| 120 * connection. | |
| 121 */ | |
| 122 remoting.DnsBlackholeChecker.prototype.sendConnectionSetupResults = function( | |
| 123 logToServer) { | |
| 124 this.signalStrategy_.sendConnectionSetupResults(logToServer) | |
| 125 }; | |
| 126 | |
| 127 /** @param {string} message */ | |
| 128 remoting.DnsBlackholeChecker.prototype.sendMessage = function(message) { | |
| 129 base.debug.assert(this.state_ == remoting.SignalStrategy.State.CONNECTED); | |
| 130 this.signalStrategy_.sendMessage(message); | |
| 131 }; | |
| 132 | |
| 133 /** @param {remoting.SignalStrategy.State} state */ | |
| 134 remoting.DnsBlackholeChecker.prototype.onWrappedSignalStrategyStateChanged_ = | |
| 135 function(state) { | |
| 136 switch (this.blackholeState_) { | |
| 137 case BlackholeState.PENDING: | |
| 138 // Stay in HANDSHAKE state if we are still waiting for the HTTP request. | |
| 139 if (state != remoting.SignalStrategy.State.CONNECTED) { | |
| 140 this.setState_(state); | |
| 141 } | |
| 142 break; | |
| 143 case BlackholeState.OPEN: | |
| 144 this.setState_(state); | |
| 145 break; | |
| 146 case BlackholeState.BLOCKED: | |
| 147 // In case DNS blackhole is active the external state stays FAILED. | |
| 148 break; | |
| 149 } | |
| 150 }; | |
| 151 | |
| 152 /** | |
| 153 * @param {!remoting.Xhr.Response} response | |
| 154 * @private | |
| 155 */ | |
| 156 remoting.DnsBlackholeChecker.prototype.onHttpRequestDone_ = function(response) { | |
| 157 if (this.xhr_ == null) { | |
| 158 // This happens when the dispose() method is called while a | |
| 159 // request is pending. | |
| 160 return; | |
| 161 } | |
| 162 | |
| 163 this.xhr_ = null; | |
| 164 if (response.status >= 200 && response.status <= 299) { | |
| 165 console.log("DNS blackhole check succeeded."); | |
| 166 this.blackholeState_ = BlackholeState.OPEN; | |
| 167 if (this.signalStrategy_.getState() == | |
| 168 remoting.SignalStrategy.State.CONNECTED) { | |
| 169 this.setState_(remoting.SignalStrategy.State.CONNECTED); | |
| 170 } | |
| 171 } else { | |
| 172 console.error("DNS blackhole check failed: " + response.status + " " + | |
| 173 response.statusText + ". Response URL: " + | |
| 174 response.url + ". Response Text: " + | |
| 175 response.getText()); | |
| 176 this.blackholeState_ = BlackholeState.BLOCKED; | |
| 177 base.dispose(this.signalStrategy_); | |
| 178 this.setState_(remoting.SignalStrategy.State.FAILED); | |
| 179 } | |
| 180 }; | |
| 181 | |
| 182 /** | |
| 183 * @param {remoting.SignalStrategy.State} newState | |
| 184 * @private | |
| 185 */ | |
| 186 remoting.DnsBlackholeChecker.prototype.setState_ = function(newState) { | |
| 187 if (this.state_ != newState) { | |
| 188 this.state_ = newState; | |
| 189 this.onStateChangedCallback_(this.state_); | |
| 190 } | |
| 191 }; | |
| 192 | |
| 193 }()); | |
| OLD | NEW |