Roll src/third_party/WebKit 3529d49:06e8485 (svn 202554:202555)
[chromium-blink-merge.git] / remoting / webapp / base / js / dns_blackhole_checker.js
blob69f9caa6125c27e29dde6ed1e8f6e317ef6ee81a
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;
44   /** @const @private */
45   this.url_ =
46       remoting.settings.TALK_GADGET_URL + '/oauth/chrome-remote-desktop-client';
49 /**
50  * @param {?function(remoting.SignalStrategy.State):void} onStateChangedCallback
51  *   Callback to call on state change.
52  */
53 remoting.DnsBlackholeChecker.prototype.setStateChangedCallback = function(
54     onStateChangedCallback) {
55   this.onStateChangedCallback_ = onStateChangedCallback;
58 /**
59  * @param {?function(Element):void} onIncomingStanzaCallback Callback to call on
60  *     incoming messages.
61  */
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();
72 /**
73  * @param {string} server
74  * @param {string} username
75  * @param {string} authToken
76  */
77 remoting.DnsBlackholeChecker.prototype.connect = function(server,
78                                                           username,
79                                                           authToken) {
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({
86     method: 'GET',
87     url: this.url_
88   });
89   this.xhr_.start().then(this.onHttpRequestDone_.bind(this));
92 remoting.DnsBlackholeChecker.prototype.getState = function() {
93   return this.state_;
96 remoting.DnsBlackholeChecker.prototype.getError = function() {
97   if (this.blackholeState_ == BlackholeState.BLOCKED) {
98     return new remoting.Error(remoting.Error.Tag.NOT_AUTHORIZED);
99   }
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() {
111   this.xhr_ = null;
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_ =
125     function(state) {
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);
131       }
132       break;
133     case BlackholeState.OPEN:
134       this.setState_(state);
135       break;
136     case BlackholeState.BLOCKED:
137       // In case DNS blackhole is active the external state stays FAILED.
138       break;
139   }
143  * @param {!remoting.Xhr.Response} response
144  * @private
145  */
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.
150     return;
151   }
153   this.xhr_ = null;
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);
160     }
161   } else {
162     console.error("DNS blackhole check failed: " + response.status + " " +
163                   response.statusText + ". Response URL: " +
164                   response.url + ". Response Text: " +
165                   response.getText());
166     this.blackholeState_ = BlackholeState.BLOCKED;
167     base.dispose(this.signalStrategy_);
168     this.setState_(remoting.SignalStrategy.State.FAILED);
169   }
173  * @param {remoting.SignalStrategy.State} newState
174  * @private
175  */
176 remoting.DnsBlackholeChecker.prototype.setState_ = function(newState) {
177   if (this.state_ != newState) {
178     this.state_ = newState;
179     this.onStateChangedCallback_(this.state_);
180   }
183 }());