OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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. |
| 4 |
| 5 #include "remoting/host/minimum_heartbeat_supporter.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/time/time.h" |
| 9 #include "net/base/network_change_notifier.h" |
| 10 #include "net/socket/client_socket_factory.h" |
| 11 #include "remoting/base/auto_thread_task_runner.h" |
| 12 #include "remoting/base/logging.h" |
| 13 #include "remoting/base/url_request_context_getter.h" |
| 14 #include "remoting/host/ack_or_timeout_reporter.h" |
| 15 #include "remoting/host/chromoting_host_context.h" |
| 16 #include "remoting/host/dns_blackhole_checker.h" |
| 17 #include "remoting/host/heartbeat_sender.h" |
| 18 #include "remoting/host/signaling_connector.h" |
| 19 #include "remoting/signaling/xmpp_signal_strategy.h" |
| 20 |
| 21 namespace remoting { |
| 22 |
| 23 MinimumHeartbeatSupporter::MinimumHeartbeatSupporter( |
| 24 scoped_refptr<AutoThreadTaskRunner> network_task_runner, |
| 25 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier, |
| 26 scoped_ptr<XmppSignalStrategy> signal_strategy, |
| 27 scoped_ptr<SignalingConnector> signaling_connector, |
| 28 scoped_ptr<HeartbeatSender> heartbeat_sender) |
| 29 : network_task_runner_(network_task_runner), |
| 30 network_change_notifier_(network_change_notifier.Pass()), |
| 31 signal_strategy_(signal_strategy.Pass()), |
| 32 signaling_connector_(signaling_connector.Pass()), |
| 33 heartbeat_sender_(heartbeat_sender.Pass()) { |
| 34 } |
| 35 |
| 36 scoped_refptr<MinimumHeartbeatSupporter> MinimumHeartbeatSupporter::Create( |
| 37 const base::Closure& on_heartbeat_successful_callback, |
| 38 const base::Closure& on_unknown_host_id_error_callback, |
| 39 const base::Closure& on_auth_failed_callback, |
| 40 const ChromotingHostContext& host_context, |
| 41 const XmppSignalStrategy::XmppServerConfig& xmpp_server_config, |
| 42 const std::string& talkgadget_prefix, |
| 43 const std::string& host_id, |
| 44 const scoped_refptr<const RsaKeyPair>& key_pair, |
| 45 const std::string& directory_bot_jid, |
| 46 const std::string& oauth_refresh_token, |
| 47 bool use_service_account) { |
| 48 DCHECK(host_context.network_task_runner()->BelongsToCurrentThread()); |
| 49 |
| 50 // Create a NetworkChangeNotifier for use by the signaling connector. |
| 51 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier( |
| 52 net::NetworkChangeNotifier::Create()); |
| 53 |
| 54 scoped_ptr<XmppSignalStrategy> signal_strategy( |
| 55 new XmppSignalStrategy( |
| 56 net::ClientSocketFactory::GetDefaultFactory(), |
| 57 host_context.url_request_context_getter(), |
| 58 xmpp_server_config)); |
| 59 |
| 60 scoped_ptr<DnsBlackholeChecker> dns_blackhole_checker( |
| 61 new DnsBlackholeChecker( |
| 62 host_context.url_request_context_getter(), |
| 63 talkgadget_prefix)); |
| 64 |
| 65 scoped_ptr<SignalingConnector> signaling_connector( |
| 66 new SignalingConnector( |
| 67 signal_strategy.get(), |
| 68 dns_blackhole_checker.Pass(), |
| 69 on_auth_failed_callback)); |
| 70 |
| 71 if (!oauth_refresh_token.empty()) { |
| 72 scoped_ptr<OAuthTokenGetter::OAuthCredentials> oauth_credentials( |
| 73 new OAuthTokenGetter::OAuthCredentials( |
| 74 xmpp_server_config.username, |
| 75 oauth_refresh_token, |
| 76 use_service_account)); |
| 77 |
| 78 scoped_ptr<OAuthTokenGetter> oauth_token_getter( |
| 79 new OAuthTokenGetter( |
| 80 oauth_credentials.Pass(), |
| 81 host_context.url_request_context_getter(), |
| 82 false)); |
| 83 |
| 84 signaling_connector->EnableOAuth(oauth_token_getter.Pass()); |
| 85 } |
| 86 |
| 87 scoped_ptr<HeartbeatSender> heartbeat_sender( |
| 88 new HeartbeatSender( |
| 89 on_heartbeat_successful_callback, |
| 90 on_unknown_host_id_error_callback, |
| 91 host_id, |
| 92 signal_strategy.get(), |
| 93 key_pair, |
| 94 directory_bot_jid)); |
| 95 |
| 96 return new MinimumHeartbeatSupporter( |
| 97 host_context.network_task_runner(), |
| 98 network_change_notifier.Pass(), |
| 99 signal_strategy.Pass(), |
| 100 signaling_connector.Pass(), |
| 101 heartbeat_sender.Pass()); |
| 102 } |
| 103 |
| 104 MinimumHeartbeatSupporter::~MinimumHeartbeatSupporter() { |
| 105 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 106 |
| 107 HOST_LOG << "MinimumHeartbeatSupporter destroyed."; |
| 108 } |
| 109 |
| 110 void MinimumHeartbeatSupporter::SendHostOfflineReason( |
| 111 const std::string& host_offline_reason, |
| 112 const base::TimeDelta& timeout) { |
| 113 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 114 |
| 115 HOST_LOG << "SendHostOfflineReason: trying to send " |
| 116 << host_offline_reason |
| 117 << " to the bot"; |
| 118 |
| 119 ReportAckOrTimeout( |
| 120 // Passing partially bound |SetHostOfflineReason| as |function_that_acks| |
| 121 base::Bind( |
| 122 &HeartbeatSender::SetHostOfflineReason, |
| 123 base::Unretained(heartbeat_sender_.get()), |
| 124 host_offline_reason), |
| 125 timeout, |
| 126 network_task_runner_, |
| 127 // The callback below will keep |this| alive until either ack or timeout. |
| 128 base::Bind(&MinimumHeartbeatSupporter::OnAckOrTimeout, this)); |
| 129 } |
| 130 |
| 131 void MinimumHeartbeatSupporter::OnAckOrTimeout(AckOrTimeout ack_or_timeout) |
| 132 { |
| 133 DCHECK(network_task_runner_->BelongsToCurrentThread()); |
| 134 switch (ack_or_timeout) { |
| 135 case Ack: |
| 136 HOST_LOG << "SendHostOfflineReason: got ack"; |
| 137 break; |
| 138 case Timeout: |
| 139 HOST_LOG << "SendHostOfflineReason: timed out"; |
| 140 break; |
| 141 default: |
| 142 NOTREACHED(); |
| 143 break; |
| 144 } |
| 145 } |
| 146 |
| 147 } // namespace remoting |
| 148 |
OLD | NEW |