OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
Lambros
2014/11/14 01:24:57
2014, no '(c)'
Łukasz Anforowicz
2014/11/17 18:17:02
Thanks. Done.
| |
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/cancelable_callback.h" | |
8 #include "base/message_loop/message_loop.h" | |
9 #include "base/message_loop/message_loop_proxy.h" | |
10 #include "base/time/time.h" | |
11 #include "net/base/network_change_notifier.h" | |
12 #include "net/socket/client_socket_factory.h" | |
13 #include "remoting/base/logging.h" | |
14 #include "remoting/host/chromoting_host_context.h" | |
15 #include "remoting/host/dns_blackhole_checker.h" | |
16 #include "remoting/host/heartbeat_sender.h" | |
17 #include "remoting/host/signaling_connector.h" | |
18 #include "remoting/signaling/xmpp_signal_strategy.h" | |
19 | |
20 namespace remoting { | |
21 | |
22 MinimumHeartbeatSupporter::MinimumHeartbeatSupporter( | |
23 /* TODO(lukasza): code review please: I heard that when passing | |
Lambros
2014/11/14 01:24:57
Remove TODO
Łukasz Anforowicz
2014/11/17 18:17:02
Done.
| |
24 * a non-null pointer, one should use a reference rather than | |
25 * a pointer in C++. I still used a pointer below, because | |
26 * I was not quite sure how to convert a scoped_ptr<T> into T&. | |
27 * Should I repent? */ | |
Lambros
2014/11/14 01:24:57
I don't feel strongly. Passing by const reference
Łukasz Anforowicz
2014/11/17 18:17:02
Ah, ok. For some reason I thought that I would ha
| |
28 const ChromotingHostContext* host_context, | |
29 const XmppSignalStrategy::XmppServerConfig& xmpp_server_config, | |
30 const std::string& talkgadget_prefix, | |
31 const std::string& host_id, | |
32 scoped_refptr<RsaKeyPair> key_pair, | |
33 const std::string& directory_bot_jid, | |
34 const std::string& oauth_refresh_token, | |
35 bool use_service_account) | |
36 : base::RefCountedDeleteOnMessageLoop<MinimumHeartbeatSupporter>( | |
37 base::MessageLoopProxy::current()), | |
38 network_task_runner_(host_context->network_task_runner()), | |
39 listener_(nullptr) { | |
40 DCHECK(network_task_runner_->BelongsToCurrentThread()); | |
41 | |
42 // Create a NetworkChangeNotifier for use by the signaling connector. | |
43 network_change_notifier_.reset( | |
44 net::NetworkChangeNotifier::Create()); | |
Lambros
2014/11/14 01:24:57
nit: Fits on one line.
Łukasz Anforowicz
2014/11/17 18:17:02
Done.
| |
45 | |
46 signal_strategy_.reset( | |
47 new XmppSignalStrategy( | |
48 net::ClientSocketFactory::GetDefaultFactory(), | |
49 host_context->url_request_context_getter(), | |
50 xmpp_server_config)); | |
51 | |
52 scoped_ptr<DnsBlackholeChecker> dns_blackhole_checker( | |
53 new DnsBlackholeChecker( | |
54 host_context->url_request_context_getter(), | |
55 talkgadget_prefix)); | |
56 | |
57 signaling_connector_.reset( | |
58 new SignalingConnector( | |
59 signal_strategy_.get(), | |
60 dns_blackhole_checker.Pass(), | |
61 base::Bind( | |
62 &MinimumHeartbeatSupporter::OnAuthFailed, | |
63 base::Unretained(this)))); | |
64 | |
65 if (!oauth_refresh_token.empty()) { | |
66 scoped_ptr<OAuthTokenGetter::OAuthCredentials> oauth_credentials( | |
67 new OAuthTokenGetter::OAuthCredentials( | |
68 xmpp_server_config.username, oauth_refresh_token, | |
69 use_service_account)); | |
70 | |
71 oauth_token_getter_.reset( | |
72 new OAuthTokenGetter( | |
73 oauth_credentials.Pass(), | |
Lambros
2014/11/14 01:24:57
nit: Indentation should be +4, not +2.
Łukasz Anforowicz
2014/11/17 18:17:02
Done.
| |
74 host_context->url_request_context_getter(), | |
75 false)); | |
76 | |
77 signaling_connector_->EnableOAuth(oauth_token_getter_.get()); | |
78 } | |
79 | |
80 heartbeat_sender_.reset(new HeartbeatSender( | |
81 this, | |
82 host_id, | |
83 signal_strategy_.get(), | |
84 key_pair, | |
85 directory_bot_jid)); | |
86 } | |
87 | |
88 MinimumHeartbeatSupporter::~MinimumHeartbeatSupporter() { | |
89 DCHECK(network_task_runner_->BelongsToCurrentThread()); | |
90 | |
91 // TODO(lukasza): code review please: anything we can do here? | |
Lambros
2014/11/14 01:24:57
Remove TODO.
Łukasz Anforowicz
2014/11/17 18:17:02
Done.
| |
92 // We also need to ensure that fields we own | |
93 // are not used by frames higher on the callstack | |
Lambros
2014/11/14 01:24:57
Are these fields exposed to things higher up in th
Łukasz Anforowicz
2014/11/17 18:17:02
MinimumHeartbeatSupporter object is the owner, but
| |
94 // (so we don't pull the rug from under them | |
95 // by reclaiming the memory they might be actively using). | |
96 // In theory we could abuse thread_collision_warner.h, | |
97 // but I am not convinced this is a very good idea. | |
98 | |
99 // order of destroying fields below is important | |
100 heartbeat_sender_.reset(); | |
101 signaling_connector_.reset(); | |
102 oauth_token_getter_.reset(); | |
103 signal_strategy_.reset(); | |
104 network_change_notifier_.reset(); | |
105 | |
106 HOST_LOG << "MinimumHeartbeatSupporter is ready to die " | |
107 << "and allow the host process to exit"; | |
108 } | |
109 | |
110 SignalStrategy* MinimumHeartbeatSupporter::GetSignalStrategy() { | |
111 return signal_strategy_.get(); | |
112 } | |
113 | |
114 void MinimumHeartbeatSupporter::SetListener(Listener* listener) { | |
115 DCHECK(network_task_runner_->BelongsToCurrentThread()); | |
116 listener_ = listener; | |
117 } | |
118 | |
119 void MinimumHeartbeatSupporter::OnAuthFailed() { | |
120 DCHECK(network_task_runner_->BelongsToCurrentThread()); | |
121 if (listener_ != nullptr) | |
122 { | |
123 listener_->OnAuthFailed(); | |
124 } | |
125 } | |
126 | |
127 void MinimumHeartbeatSupporter::OnHeartbeatSuccessful() { | |
128 DCHECK(network_task_runner_->BelongsToCurrentThread()); | |
129 if (listener_ != nullptr) | |
130 { | |
131 listener_->OnHeartbeatSuccessful(); | |
132 } | |
133 } | |
134 | |
135 void MinimumHeartbeatSupporter::OnUnknownHostIdError() { | |
136 DCHECK(network_task_runner_->BelongsToCurrentThread()); | |
137 if (listener_ != nullptr) | |
138 { | |
139 listener_->OnUnknownHostIdError(); | |
140 } | |
141 } | |
142 | |
143 void MinimumHeartbeatSupporter::SendHostOfflineReason( | |
144 const char* host_offline_reason, | |
145 const base::TimeDelta& timeout) { | |
146 DCHECK(network_task_runner_->BelongsToCurrentThread()); | |
147 DCHECK(!timeout_callback_.get()); | |
148 DCHECK(!ack_callback_.get()); | |
149 | |
150 HOST_LOG << "SendHostOfflineReason: trying to send " | |
151 << host_offline_reason | |
152 << " to the bot"; | |
153 | |
154 // the closures will own 1 refcount each to |this| | |
155 // so the closures will keep us alive until either a timeout or an ack happen | |
156 timeout_callback_.reset(new base::CancelableClosure( | |
157 base::Bind( | |
158 &MinimumHeartbeatSupporter::OnTimeout, | |
159 this))); | |
160 ack_callback_.reset(new base::CancelableClosure( | |
161 base::Bind( | |
162 &MinimumHeartbeatSupporter::OnAck, | |
163 this))); | |
164 | |
165 // need to get the closures first, as our callbacks might | |
166 // get called as soon as we pass them out and this can potentially | |
167 // invalidate timeout_callback_ and ack_callback_ | |
168 base::Closure local_timeout_callback = timeout_callback_->callback(); | |
169 base::Closure local_ack_callback = ack_callback_->callback(); | |
170 | |
171 network_task_runner_->PostDelayedTask( | |
172 FROM_HERE, local_timeout_callback, timeout); | |
173 heartbeat_sender_->SetHostOfflineReason( | |
174 host_offline_reason, local_ack_callback); | |
175 } | |
176 | |
177 void MinimumHeartbeatSupporter::OnAck() { | |
178 HOST_LOG << "SendHostOfflineReason: success - got ack"; | |
179 OnAckOrTimeout(); | |
180 } | |
181 | |
182 void MinimumHeartbeatSupporter::OnTimeout() { | |
183 HOST_LOG << "SendHostOfflineReason: timeout - will die silently..."; | |
184 OnAckOrTimeout(); | |
185 } | |
186 | |
187 void MinimumHeartbeatSupporter::OnAckOrTimeout() { | |
188 DCHECK(network_task_runner_->BelongsToCurrentThread()); | |
189 DCHECK(timeout_callback_.get()); | |
190 DCHECK(ack_callback_.get()); | |
191 | |
192 // |this| shouldn't be released until all our methods have safely | |
193 // unwound from the callstack | |
194 this->AddRef(); | |
Lambros
2014/11/14 01:24:57
This is smelly! :) Is there some nicer way we can
Łukasz Anforowicz
2014/11/17 18:17:02
Initially I planned to respond by adding a comment
| |
195 network_task_runner_->ReleaseSoon(FROM_HERE, this); | |
196 | |
197 // cancel the callbacks + release them = free-up our 2 ref-counts | |
198 timeout_callback_.reset(); | |
199 ack_callback_.reset(); | |
200 } | |
201 | |
202 } // namespace remoting | |
203 | |
OLD | NEW |