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 #include "remoting/host/gcd_state_updater.h"
7 #include "base/callback_helpers.h"
8 #include "base/strings/stringize_macros.h"
9 #include "base/time/time.h"
10 #include "base/values.h"
11 #include "remoting/base/constants.h"
12 #include "remoting/base/logging.h"
18 const int64 kTimerIntervalMinMs
= 1000;
19 const int64 kTimerIntervalMaxMs
= 5 * 60 * 1000; // 5 minutes
23 GcdStateUpdater::GcdStateUpdater(
24 const base::Closure
& on_update_successful_callback
,
25 const base::Closure
& on_unknown_host_id_error
,
26 SignalStrategy
* signal_strategy
,
27 scoped_ptr
<GcdRestClient
> gcd_rest_client
)
28 : on_update_successful_callback_(on_update_successful_callback
),
29 on_unknown_host_id_error_(on_unknown_host_id_error
),
30 signal_strategy_(signal_strategy
),
31 gcd_rest_client_(gcd_rest_client
.Pass()) {
32 DCHECK(signal_strategy_
);
33 DCHECK(thread_checker_
.CalledOnValidThread());
35 signal_strategy_
->AddListener(this);
37 // Update state if the |signal_strategy_| is already connected.
38 OnSignalStrategyStateChange(signal_strategy_
->GetState());
41 GcdStateUpdater::~GcdStateUpdater() {
42 signal_strategy_
->RemoveListener(this);
45 void GcdStateUpdater::SetHostOfflineReason(
46 const std::string
& host_offline_reason
,
47 const base::TimeDelta
& timeout
,
48 const base::Callback
<void(bool success
)>& ack_callback
) {
49 // TODO(jrw): Implement this. Refer to
50 // HeartbeatSender::SetHostOfflineReason.
54 void GcdStateUpdater::OnSignalStrategyStateChange(SignalStrategy::State state
) {
55 if (state
== SignalStrategy::CONNECTED
) {
56 timer_
.Start(FROM_HERE
,
57 base::TimeDelta::FromMilliseconds(kTimerIntervalMinMs
),
58 base::TimeDelta::FromMilliseconds(kTimerIntervalMaxMs
),
59 base::Bind(&GcdStateUpdater::MaybeSendStateUpdate
,
60 base::Unretained(this)));
61 } else if (state
== SignalStrategy::DISCONNECTED
) {
66 bool GcdStateUpdater::OnSignalStrategyIncomingStanza(
67 const buzz::XmlElement
* stanza
) {
68 // Ignore all XMPP stanzas.
72 void GcdStateUpdater::OnPatchStateResult(GcdRestClient::Result result
) {
73 if (!timer_
.IsRunning()) {
77 if (result
== GcdRestClient::NETWORK_ERROR
||
78 pending_request_jid_
!= signal_strategy_
->GetLocalJid()) {
79 // Continue exponential backoff.
84 if (result
== GcdRestClient::SUCCESS
) {
85 if (!on_update_successful_callback_
.is_null()) {
86 on_unknown_host_id_error_
.Reset();
87 base::ResetAndReturn(&on_update_successful_callback_
).Run();
89 } else if (result
== GcdRestClient::NO_SUCH_HOST
) {
90 if (!on_unknown_host_id_error_
.is_null()) {
91 on_update_successful_callback_
.Reset();
92 base::ResetAndReturn(&on_unknown_host_id_error_
).Run();
95 // For any other error, do nothing since there's no way to handle
96 // it and the error will already have been logged at this point.
100 void GcdStateUpdater::MaybeSendStateUpdate() {
101 DCHECK_EQ(signal_strategy_
->GetState(), SignalStrategy::CONNECTED
);
103 // Don't send a request if there is already another request pending.
104 // This avoids having multiple outstanding requests, which would be
105 // a problem since there's no guarantee that the reqests will
106 // complete in order.
107 if (gcd_rest_client_
->HasPendingRequest()) {
111 // Construct an update to the remote state.
112 scoped_ptr
<base::DictionaryValue
> patch(new base::DictionaryValue
);
113 scoped_ptr
<base::DictionaryValue
> base_state(new base::DictionaryValue
);
114 pending_request_jid_
= signal_strategy_
->GetLocalJid();
115 base_state
->SetString("_jabberId", pending_request_jid_
);
116 base_state
->SetString("_hostVersion", STRINGIZE(VERSION
));
117 patch
->Set("base", base_state
.Pass());
119 // Send the update to GCD.
120 gcd_rest_client_
->PatchState(
122 base::Bind(&GcdStateUpdater::OnPatchStateResult
, base::Unretained(this)));
125 } // namespace remoting