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 /** @private {?function(remoting.SignalStrategy.State):void} */
33 this.onStateChangedCallback_ = null;
36 this.state_ = remoting.SignalStrategy.State.NOT_CONNECTED;
39 this.blackholeState_ = BlackholeState.PENDING;
41 /** @private {?remoting.Xhr} */
44 /** @const @private */
46 remoting.settings.TALK_GADGET_URL + '/oauth/chrome-remote-desktop-client';
50 * @param {?function(remoting.SignalStrategy.State):void} onStateChangedCallback
51 * Callback to call on state change.
53 remoting.DnsBlackholeChecker.prototype.setStateChangedCallback = function(
54 onStateChangedCallback) {
55 this.onStateChangedCallback_ = onStateChangedCallback;
59 * @param {?function(Element):void} onIncomingStanzaCallback Callback to call on
62 remoting.DnsBlackholeChecker.prototype.setIncomingStanzaCallback =
63 function(onIncomingStanzaCallback) {
64 this.signalStrategy_.setIncomingStanzaCallback(onIncomingStanzaCallback);
67 /** @return {remoting.SignalStrategy.Type} The signal strategy type. */
68 remoting.DnsBlackholeChecker.prototype.getType = function() {
69 return this.signalStrategy_.getType();
73 * @param {string} server
74 * @param {string} username
75 * @param {string} authToken
77 remoting.DnsBlackholeChecker.prototype.connect = function(server,
80 console.assert(this.onStateChangedCallback_ != null,
81 'No state change callback registered.');
83 this.signalStrategy_.connect(server, username, authToken);
85 this.xhr_ = new remoting.Xhr({
89 this.xhr_.start().then(this.onHttpRequestDone_.bind(this));
92 remoting.DnsBlackholeChecker.prototype.getState = function() {
96 remoting.DnsBlackholeChecker.prototype.getError = function() {
97 if (this.blackholeState_ == BlackholeState.BLOCKED) {
98 return new remoting.Error(remoting.Error.Tag.NOT_AUTHORIZED);
101 return this.signalStrategy_.getError();
104 remoting.DnsBlackholeChecker.prototype.getJid = function() {
105 console.assert(this.state_ == remoting.SignalStrategy.State.CONNECTED,
106 'getJid() called in state ' + this.state_ + '.');
107 return this.signalStrategy_.getJid();
110 remoting.DnsBlackholeChecker.prototype.dispose = function() {
112 base.dispose(this.signalStrategy_);
113 this.setState_(remoting.SignalStrategy.State.CLOSED);
116 /** @param {string} message */
117 remoting.DnsBlackholeChecker.prototype.sendMessage = function(message) {
118 console.assert(this.state_ == remoting.SignalStrategy.State.CONNECTED,
119 'sendMessage() called in state ' + this.state_ + '.');
120 this.signalStrategy_.sendMessage(message);
123 /** @param {remoting.SignalStrategy.State} state */
124 remoting.DnsBlackholeChecker.prototype.onWrappedSignalStrategyStateChanged_ =
126 switch (this.blackholeState_) {
127 case BlackholeState.PENDING:
128 // Stay in HANDSHAKE state if we are still waiting for the HTTP request.
129 if (state != remoting.SignalStrategy.State.CONNECTED) {
130 this.setState_(state);
133 case BlackholeState.OPEN:
134 this.setState_(state);
136 case BlackholeState.BLOCKED:
137 // In case DNS blackhole is active the external state stays FAILED.
143 * @param {!remoting.Xhr.Response} response
146 remoting.DnsBlackholeChecker.prototype.onHttpRequestDone_ = function(response) {
147 if (this.xhr_ == null) {
148 // This happens when the dispose() method is called while a
149 // request is pending.
154 if (response.status >= 200 && response.status <= 299) {
155 console.log("DNS blackhole check succeeded.");
156 this.blackholeState_ = BlackholeState.OPEN;
157 if (this.signalStrategy_.getState() ==
158 remoting.SignalStrategy.State.CONNECTED) {
159 this.setState_(remoting.SignalStrategy.State.CONNECTED);
162 console.error("DNS blackhole check failed: " + response.status + " " +
163 response.statusText + ". Response URL: " +
164 response.url + ". Response Text: " +
166 this.blackholeState_ = BlackholeState.BLOCKED;
167 base.dispose(this.signalStrategy_);
168 this.setState_(remoting.SignalStrategy.State.FAILED);
173 * @param {remoting.SignalStrategy.State} newState
176 remoting.DnsBlackholeChecker.prototype.setState_ = function(newState) {
177 if (this.state_ != newState) {
178 this.state_ = newState;
179 this.onStateChangedCallback_(this.state_);