Include all dupe types (event when value is zero) in scan stats.
[chromium-blink-merge.git] / remoting / host / gcd_state_updater.cc
blobf4921844f717dda83fe320ea3572c90462eca766
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"
14 namespace remoting {
16 namespace {
18 const int64 kTimerIntervalMinMs = 1000;
19 const int64 kTimerIntervalMaxMs = 5 * 60 * 1000; // 5 minutes
21 } // namespace
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 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) {
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.
51 NOTIMPLEMENTED();
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) {
62 timer_.Stop();
66 bool GcdStateUpdater::OnSignalStrategyIncomingStanza(
67 const buzz::XmlElement* stanza) {
68 // Ignore all XMPP stanzas.
69 return false;
72 void GcdStateUpdater::OnPatchStateStatus(GcdRestClient::Status status) {
73 has_pending_state_request_ = false;
75 if (!timer_.IsRunning()) {
76 return;
79 if (status == GcdRestClient::NETWORK_ERROR ||
80 pending_request_jid_ != signal_strategy_->GetLocalJid()) {
81 // Continue exponential backoff.
82 return;
85 timer_.Stop();
86 if (status == GcdRestClient::SUCCESS) {
87 if (!on_update_successful_callback_.is_null()) {
88 on_unknown_host_id_error_.Reset();
89 base::ResetAndReturn(&on_update_successful_callback_).Run();
91 } else if (status == GcdRestClient::NO_SUCH_HOST) {
92 if (!on_unknown_host_id_error_.is_null()) {
93 on_update_successful_callback_.Reset();
94 base::ResetAndReturn(&on_unknown_host_id_error_).Run();
96 } else {
97 // For any other error, do nothing since there's no way to handle
98 // it and the error will already have been logged at this point.
102 void GcdStateUpdater::MaybeSendStateUpdate() {
103 DCHECK_EQ(signal_strategy_->GetState(), SignalStrategy::CONNECTED);
105 // Don't send a request if there is already another request pending.
106 // This avoids having multiple outstanding requests, which would be
107 // a problem since there's no guarantee that the reqests will
108 // complete in order.
109 if (has_pending_state_request_) {
110 return;
113 has_pending_state_request_ = true;
115 // Construct an update to the remote state.
116 scoped_ptr<base::DictionaryValue> patch(new base::DictionaryValue);
117 pending_request_jid_ = signal_strategy_->GetLocalJid();
118 patch->SetString("_jabberId", pending_request_jid_);
119 patch->SetString("_hostVersion", STRINGIZE(VERSION));
121 // Send the update to GCD.
122 gcd_rest_client_->PatchState(
123 patch.Pass(),
124 base::Bind(&GcdStateUpdater::OnPatchStateStatus, base::Unretained(this)));
127 } // namespace remoting