Revert of Remove OneClickSigninHelper since it is no longer used. (patchset #5 id...
[chromium-blink-merge.git] / remoting / host / signaling_connector.cc
blob7359e15486f1ed1823a6f71582d9f641d33a049e
1 // Copyright (c) 2012 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 #include "remoting/host/signaling_connector.h"
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/strings/string_util.h"
10 #include "google_apis/google_api_keys.h"
11 #include "net/url_request/url_fetcher.h"
12 #include "net/url_request/url_request_context_getter.h"
13 #include "remoting/base/logging.h"
14 #include "remoting/host/dns_blackhole_checker.h"
16 namespace remoting {
18 namespace {
20 // The delay between reconnect attempts will increase exponentially up
21 // to the maximum specified here.
22 const int kMaxReconnectDelaySeconds = 10 * 60;
24 } // namespace
26 SignalingConnector::SignalingConnector(
27 XmppSignalStrategy* signal_strategy,
28 scoped_ptr<DnsBlackholeChecker> dns_blackhole_checker,
29 scoped_ptr<OAuthTokenGetter> oauth_token_getter,
30 const base::Closure& auth_failed_callback)
31 : signal_strategy_(signal_strategy),
32 auth_failed_callback_(auth_failed_callback),
33 dns_blackhole_checker_(dns_blackhole_checker.Pass()),
34 oauth_token_getter_(oauth_token_getter.Pass()),
35 reconnect_attempts_(0) {
36 DCHECK(!auth_failed_callback_.is_null());
37 DCHECK(dns_blackhole_checker_.get());
38 net::NetworkChangeNotifier::AddConnectionTypeObserver(this);
39 net::NetworkChangeNotifier::AddIPAddressObserver(this);
40 signal_strategy_->AddListener(this);
41 ScheduleTryReconnect();
44 SignalingConnector::~SignalingConnector() {
45 signal_strategy_->RemoveListener(this);
46 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
47 net::NetworkChangeNotifier::RemoveIPAddressObserver(this);
50 void SignalingConnector::OnSignalStrategyStateChange(
51 SignalStrategy::State state) {
52 DCHECK(CalledOnValidThread());
54 if (state == SignalStrategy::CONNECTED) {
55 HOST_LOG << "Signaling connected.";
56 reconnect_attempts_ = 0;
57 } else if (state == SignalStrategy::DISCONNECTED) {
58 HOST_LOG << "Signaling disconnected.";
59 reconnect_attempts_++;
61 // If authentication failed then we have an invalid OAuth token,
62 // inform the upper layer about it.
63 if (signal_strategy_->GetError() == SignalStrategy::AUTHENTICATION_FAILED) {
64 auth_failed_callback_.Run();
65 } else {
66 ScheduleTryReconnect();
71 bool SignalingConnector::OnSignalStrategyIncomingStanza(
72 const buzz::XmlElement* stanza) {
73 return false;
76 void SignalingConnector::OnConnectionTypeChanged(
77 net::NetworkChangeNotifier::ConnectionType type) {
78 DCHECK(CalledOnValidThread());
79 if (type != net::NetworkChangeNotifier::CONNECTION_NONE &&
80 signal_strategy_->GetState() == SignalStrategy::DISCONNECTED) {
81 HOST_LOG << "Network state changed to online.";
82 ResetAndTryReconnect();
86 void SignalingConnector::OnIPAddressChanged() {
87 DCHECK(CalledOnValidThread());
88 if (signal_strategy_->GetState() == SignalStrategy::DISCONNECTED) {
89 HOST_LOG << "IP address has changed.";
90 ResetAndTryReconnect();
94 void SignalingConnector::OnAccessToken(OAuthTokenGetter::Status status,
95 const std::string& user_email,
96 const std::string& access_token) {
97 DCHECK(CalledOnValidThread());
99 if (status == OAuthTokenGetter::AUTH_ERROR) {
100 auth_failed_callback_.Run();
101 return;
102 } else if (status == OAuthTokenGetter::NETWORK_ERROR) {
103 OnNetworkError();
104 return;
107 DCHECK_EQ(status, OAuthTokenGetter::SUCCESS);
108 HOST_LOG << "Received user info.";
110 signal_strategy_->SetAuthInfo(user_email, access_token);
112 // Now that we've refreshed the token and verified that it's for the correct
113 // user account, try to connect using the new token.
114 DCHECK_EQ(signal_strategy_->GetState(), SignalStrategy::DISCONNECTED);
115 signal_strategy_->Connect();
118 void SignalingConnector::OnNetworkError() {
119 DCHECK(CalledOnValidThread());
120 reconnect_attempts_++;
121 ScheduleTryReconnect();
124 void SignalingConnector::ScheduleTryReconnect() {
125 DCHECK(CalledOnValidThread());
126 if (timer_.IsRunning() || net::NetworkChangeNotifier::IsOffline())
127 return;
128 int delay_s = std::min(1 << reconnect_attempts_,
129 kMaxReconnectDelaySeconds);
130 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(delay_s),
131 this, &SignalingConnector::TryReconnect);
134 void SignalingConnector::ResetAndTryReconnect() {
135 DCHECK(CalledOnValidThread());
136 signal_strategy_->Disconnect();
137 reconnect_attempts_ = 0;
138 timer_.Stop();
139 ScheduleTryReconnect();
142 void SignalingConnector::TryReconnect() {
143 DCHECK(CalledOnValidThread());
144 DCHECK(dns_blackhole_checker_.get());
146 // This will check if this machine is allowed to access the chromoting
147 // host talkgadget.
148 dns_blackhole_checker_->CheckForDnsBlackhole(
149 base::Bind(&SignalingConnector::OnDnsBlackholeCheckerDone,
150 base::Unretained(this)));
153 void SignalingConnector::OnDnsBlackholeCheckerDone(bool allow) {
154 DCHECK(CalledOnValidThread());
156 // Unable to access the host talkgadget. Don't allow the connection, but
157 // schedule a reconnect in case this is a transient problem rather than
158 // an outright block.
159 if (!allow) {
160 reconnect_attempts_++;
161 HOST_LOG << "Talkgadget check failed. Scheduling reconnect. Attempt "
162 << reconnect_attempts_;
163 ScheduleTryReconnect();
164 return;
167 if (signal_strategy_->GetState() == SignalStrategy::DISCONNECTED) {
168 HOST_LOG << "Attempting to connect signaling.";
169 oauth_token_getter_->CallWithToken(
170 base::Bind(&SignalingConnector::OnAccessToken, AsWeakPtr()));
174 } // namespace remoting