Remove base.debug.assert.
[chromium-blink-merge.git] / remoting / webapp / base / js / dns_blackhole_checker.js
blob9c3adddf7e709a7a61e563a9bec4f8a549c5ba37
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.
5 'use strict';
7 /** @suppress {duplicate} */
8 var remoting = remoting || {};
10 (function() {
12 /** @enum */
13 var BlackholeState = {
14   PENDING: 0,
15   OPEN: 1,
16   BLOCKED: 2
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));
32   /** @private {?function(remoting.SignalStrategy.State):void} */
33   this.onStateChangedCallback_ = null;
35   /** @private */
36   this.state_ = remoting.SignalStrategy.State.NOT_CONNECTED;
38   /** @private */
39   this.blackholeState_ = BlackholeState.PENDING;
41   /** @private {?remoting.Xhr} */
42   this.xhr_ = null;
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";
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;
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);
71 /** @return {remoting.SignalStrategy.Type} The signal strategy type. */
72 remoting.DnsBlackholeChecker.prototype.getType = function() {
73   return this.signalStrategy_.getType();
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   console.assert(this.onStateChangedCallback_ != null,
85                  'No state change callback registered.');
87   this.signalStrategy_.connect(server, username, authToken);
89   this.xhr_ = new remoting.Xhr({
90     method: 'GET',
91     url: remoting.DnsBlackholeChecker.URL_TO_REQUEST_
92   });
93   this.xhr_.start().then(this.onHttpRequestDone_.bind(this));
96 remoting.DnsBlackholeChecker.prototype.getState = function() {
97   return this.state_;
100 remoting.DnsBlackholeChecker.prototype.getError = function() {
101   if (this.blackholeState_ == BlackholeState.BLOCKED) {
102     return new remoting.Error(remoting.Error.Tag.NOT_AUTHORIZED);
103   }
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() {
115   this.xhr_ = null;
116   base.dispose(this.signalStrategy_);
117   this.setState_(remoting.SignalStrategy.State.CLOSED);
120 remoting.DnsBlackholeChecker.prototype.sendConnectionSetupResults = function(
121     logger) {
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_ =
134     function(state) {
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);
140       }
141       break;
142     case BlackholeState.OPEN:
143       this.setState_(state);
144       break;
145     case BlackholeState.BLOCKED:
146       // In case DNS blackhole is active the external state stays FAILED.
147       break;
148   }
152  * @param {!remoting.Xhr.Response} response
153  * @private
154  */
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.
159     return;
160   }
162   this.xhr_ = null;
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);
169     }
170   } else {
171     console.error("DNS blackhole check failed: " + response.status + " " +
172                   response.statusText + ". Response URL: " +
173                   response.url + ". Response Text: " +
174                   response.getText());
175     this.blackholeState_ = BlackholeState.BLOCKED;
176     base.dispose(this.signalStrategy_);
177     this.setState_(remoting.SignalStrategy.State.FAILED);
178   }
182  * @param {remoting.SignalStrategy.State} newState
183  * @private
184  */
185 remoting.DnsBlackholeChecker.prototype.setState_ = function(newState) {
186   if (this.state_ != newState) {
187     this.state_ = newState;
188     this.onStateChangedCallback_(this.state_);
189   }
192 }());