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.
7 /** @suppress {duplicate} */
8 var remoting = remoting || {};
13 var BlackholeState = {
20 * A SignalStrategy wrapper that performs DNS blackhole check.
22 * @param {remoting.SignalStrategy} signalStrategy
24 * @implements {remoting.SignalStrategy}
26 remoting.DnsBlackholeChecker = function(signalStrategy) {
28 this.signalStrategy_ = signalStrategy;
29 this.signalStrategy_.setStateChangedCallback(
30 this.onWrappedSignalStrategyStateChanged_.bind(this));
32 /** @type {?function(remoting.SignalStrategy.State):void} @private */
33 this.onStateChangedCallback_ = null;
36 this.state_ = remoting.SignalStrategy.State.NOT_CONNECTED;
39 this.blackholeState_ = BlackholeState.PENDING;
41 /** @type {?XMLHttpRequest} @private */
49 remoting.DnsBlackholeChecker.URL_TO_REQUEST_ =
50 "https://chromoting-client.talkgadget.google.com/talkgadget/oauth/" +
51 "chrome-remote-desktop-client";
54 * @param {?function(remoting.SignalStrategy.State):void} onStateChangedCallback
55 * Callback to call on state change.
57 remoting.DnsBlackholeChecker.prototype.setStateChangedCallback = function(
58 onStateChangedCallback) {
59 this.onStateChangedCallback_ = onStateChangedCallback;
63 * @param {?function(Element):void} onIncomingStanzaCallback Callback to call on
66 remoting.DnsBlackholeChecker.prototype.setIncomingStanzaCallback =
67 function(onIncomingStanzaCallback) {
68 this.signalStrategy_.setIncomingStanzaCallback(onIncomingStanzaCallback);
71 /** @return {remoting.SignalStrategy.Type} The signal strategy type. */
72 remoting.DnsBlackholeChecker.prototype.getType = function() {
73 return this.signalStrategy_.getType();
77 * @param {string} server
78 * @param {string} username
79 * @param {string} authToken
81 remoting.DnsBlackholeChecker.prototype.connect = function(server,
84 base.debug.assert(this.onStateChangedCallback_ != null);
86 this.signalStrategy_.connect(server, username, authToken);
88 this.xhr_ = remoting.xhr.start({
90 url: remoting.DnsBlackholeChecker.URL_TO_REQUEST_,
91 onDone: this.onHttpRequestDone_.bind(this)
95 remoting.DnsBlackholeChecker.prototype.getState = function() {
99 remoting.DnsBlackholeChecker.prototype.getError = function() {
100 if (this.blackholeState_ == BlackholeState.BLOCKED) {
101 return remoting.Error.NOT_AUTHORIZED;
104 return this.signalStrategy_.getError();
107 remoting.DnsBlackholeChecker.prototype.getJid = function() {
108 base.debug.assert(this.state_ == remoting.SignalStrategy.State.CONNECTED);
109 return this.signalStrategy_.getJid();
112 remoting.DnsBlackholeChecker.prototype.dispose = function() {
117 base.dispose(this.signalStrategy_);
118 this.setState_(remoting.SignalStrategy.State.CLOSED);
122 * @param {remoting.LogToServer} logToServer The LogToServer instance for the
125 remoting.DnsBlackholeChecker.prototype.sendConnectionSetupResults = function(
127 this.signalStrategy_.sendConnectionSetupResults(logToServer)
130 /** @param {string} message */
131 remoting.DnsBlackholeChecker.prototype.sendMessage = function(message) {
132 base.debug.assert(this.state_ == remoting.SignalStrategy.State.CONNECTED);
133 this.signalStrategy_.sendMessage(message);
136 /** @param {remoting.SignalStrategy.State} state */
137 remoting.DnsBlackholeChecker.prototype.onWrappedSignalStrategyStateChanged_ =
139 switch (this.blackholeState_) {
140 case BlackholeState.PENDING:
141 // Stay in HANDSHAKE state if we are still waiting for the HTTP request.
142 if (state != remoting.SignalStrategy.State.CONNECTED) {
143 this.setState_(state);
146 case BlackholeState.OPEN:
147 this.setState_(state);
149 case BlackholeState.BLOCKED:
150 // In case DNS blackhole is active the external state stays FAILED.
156 * @param {XMLHttpRequest} xhr
159 remoting.DnsBlackholeChecker.prototype.onHttpRequestDone_ = function(xhr) {
161 if (xhr.status >= 200 && xhr.status <= 299) {
162 console.log("DNS blackhole check succeeded.");
163 this.blackholeState_ = BlackholeState.OPEN;
164 if (this.signalStrategy_.getState() ==
165 remoting.SignalStrategy.State.CONNECTED) {
166 this.setState_(remoting.SignalStrategy.State.CONNECTED);
169 console.error("DNS blackhole check failed: " + xhr.status + " " +
170 xhr.statusText + ". Response URL: " + xhr.responseURL +
171 ". Response Text: " + xhr.responseText);
172 this.blackholeState_ = BlackholeState.BLOCKED;
173 base.dispose(this.signalStrategy_);
174 this.setState_(remoting.SignalStrategy.State.FAILED);
179 * @param {remoting.SignalStrategy.State} newState
182 remoting.DnsBlackholeChecker.prototype.setState_ = function(newState) {
183 if (this.state_ != newState) {
184 this.state_ = newState;
185 this.onStateChangedCallback_(this.state_);