Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / remoting / webapp / crd / js / dns_blackhole_checker.js
blob6bc3a7d2abe3b9e506e273de982fa07c84a73b59
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   /** @type {?function(remoting.SignalStrategy.State):void} @private */
33   this.onStateChangedCallback_ = null;
35   /** @private */
36   this.state_ = remoting.SignalStrategy.State.NOT_CONNECTED;
38   /** @private */
39   this.blackholeState_ = BlackholeState.PENDING;
41   /** @type {?XMLHttpRequest} @private */
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   base.debug.assert(this.onStateChangedCallback_ != null);
86   this.signalStrategy_.connect(server, username, authToken);
88   this.xhr_ = remoting.xhr.start({
89     method: 'GET',
90     url: remoting.DnsBlackholeChecker.URL_TO_REQUEST_,
91     onDone: this.onHttpRequestDone_.bind(this)
92   });
95 remoting.DnsBlackholeChecker.prototype.getState = function() {
96   return this.state_;
99 remoting.DnsBlackholeChecker.prototype.getError = function() {
100   if (this.blackholeState_ == BlackholeState.BLOCKED) {
101     return remoting.Error.NOT_AUTHORIZED;
102   }
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() {
113   if (this.xhr_) {
114     this.xhr_.abort();
115     this.xhr_ = null;
116   }
117   base.dispose(this.signalStrategy_);
118   this.setState_(remoting.SignalStrategy.State.CLOSED);
122  * @param {remoting.LogToServer} logToServer The LogToServer instance for the
123  *     connection.
124  */
125 remoting.DnsBlackholeChecker.prototype.sendConnectionSetupResults = function(
126     logToServer) {
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_ =
138     function(state) {
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);
144       }
145       break;
146     case BlackholeState.OPEN:
147       this.setState_(state);
148       break;
149     case BlackholeState.BLOCKED:
150       // In case DNS blackhole is active the external state stays FAILED.
151       break;
152   }
156  * @param {XMLHttpRequest} xhr
157  * @private
158  */
159 remoting.DnsBlackholeChecker.prototype.onHttpRequestDone_ = function(xhr) {
160   this.xhr_ = null;
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);
167     }
168   } else {
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);
175   }
179  * @param {remoting.SignalStrategy.State} newState
180  * @private
181  */
182 remoting.DnsBlackholeChecker.prototype.setState_ = function(newState) {
183   if (this.state_ != newState) {
184     this.state_ = newState;
185     this.onStateChangedCallback_(this.state_);
186   }
189 }());