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} */
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 console.assert(this.onStateChangedCallback_ != null,
85 'No state change callback registered.');
87 this.signalStrategy_.connect(server, username, authToken);
89 this.xhr_ = new remoting.Xhr({
91 url: remoting.DnsBlackholeChecker.URL_TO_REQUEST_
93 this.xhr_.start().then(this.onHttpRequestDone_.bind(this));
96 remoting.DnsBlackholeChecker.prototype.getState = function() {
100 remoting.DnsBlackholeChecker.prototype.getError = function() {
101 if (this.blackholeState_ == BlackholeState.BLOCKED) {
102 return new remoting.Error(remoting.Error.Tag.NOT_AUTHORIZED);
105 return this.signalStrategy_.getError();
108 remoting.DnsBlackholeChecker.prototype.getJid = function() {
109 console.assert(this.state_ == remoting.SignalStrategy.State.CONNECTED,
110 'getJid() called in state ' + this.state_ + '.');
111 return this.signalStrategy_.getJid();
114 remoting.DnsBlackholeChecker.prototype.dispose = function() {
116 base.dispose(this.signalStrategy_);
117 this.setState_(remoting.SignalStrategy.State.CLOSED);
120 remoting.DnsBlackholeChecker.prototype.sendConnectionSetupResults = function(
122 this.signalStrategy_.sendConnectionSetupResults(logger);
125 /** @param {string} message */
126 remoting.DnsBlackholeChecker.prototype.sendMessage = function(message) {
127 console.assert(this.state_ == remoting.SignalStrategy.State.CONNECTED,
128 'sendMessage() called in state ' + this.state_ + '.');
129 this.signalStrategy_.sendMessage(message);
132 /** @param {remoting.SignalStrategy.State} state */
133 remoting.DnsBlackholeChecker.prototype.onWrappedSignalStrategyStateChanged_ =
135 switch (this.blackholeState_) {
136 case BlackholeState.PENDING:
137 // Stay in HANDSHAKE state if we are still waiting for the HTTP request.
138 if (state != remoting.SignalStrategy.State.CONNECTED) {
139 this.setState_(state);
142 case BlackholeState.OPEN:
143 this.setState_(state);
145 case BlackholeState.BLOCKED:
146 // In case DNS blackhole is active the external state stays FAILED.
152 * @param {!remoting.Xhr.Response} response
155 remoting.DnsBlackholeChecker.prototype.onHttpRequestDone_ = function(response) {
156 if (this.xhr_ == null) {
157 // This happens when the dispose() method is called while a
158 // request is pending.
163 if (response.status >= 200 && response.status <= 299) {
164 console.log("DNS blackhole check succeeded.");
165 this.blackholeState_ = BlackholeState.OPEN;
166 if (this.signalStrategy_.getState() ==
167 remoting.SignalStrategy.State.CONNECTED) {
168 this.setState_(remoting.SignalStrategy.State.CONNECTED);
171 console.error("DNS blackhole check failed: " + response.status + " " +
172 response.statusText + ". Response URL: " +
173 response.url + ". Response Text: " +
175 this.blackholeState_ = BlackholeState.BLOCKED;
176 base.dispose(this.signalStrategy_);
177 this.setState_(remoting.SignalStrategy.State.FAILED);
182 * @param {remoting.SignalStrategy.State} newState
185 remoting.DnsBlackholeChecker.prototype.setState_ = function(newState) {
186 if (this.state_ != newState) {
187 this.state_ = newState;
188 this.onStateChangedCallback_(this.state_);