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.
5 #include "remoting/client/log_to_server.h"
7 #include "base/macros.h"
8 #include "base/rand_util.h"
9 #include "remoting/base/constants.h"
10 #include "remoting/client/chromoting_stats.h"
11 #include "remoting/jingle_glue/iq_sender.h"
12 #include "remoting/jingle_glue/signal_strategy.h"
13 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h"
14 #include "third_party/libjingle/source/talk/xmpp/constants.h"
17 using buzz::XmlElement;
18 using remoting::protocol::ConnectionToHost;
22 const char kSessionIdAlphabet[] =
23 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
24 const int kSessionIdLength = 20;
26 const int kMaxSessionIdAgeDays = 1;
28 bool IsStartOfSession(ConnectionToHost::State state) {
29 return state == ConnectionToHost::INITIALIZING ||
30 state == ConnectionToHost::CONNECTING ||
31 state == ConnectionToHost::AUTHENTICATED ||
32 state == ConnectionToHost::CONNECTED;
35 bool IsEndOfSession(ConnectionToHost::State state) {
36 return state == ConnectionToHost::FAILED ||
37 state == ConnectionToHost::CLOSED;
40 bool ShouldAddDuration(ConnectionToHost::State state) {
41 // Duration is added to log entries at the end of the session, as well as at
42 // some intermediate states where it is relevant (e.g. to determine how long
43 // it took for a session to become CONNECTED).
44 return IsEndOfSession(state) || state == ConnectionToHost::CONNECTED;
53 LogToServer::LogToServer(ServerLogEntry::Mode mode,
54 SignalStrategy* signal_strategy,
55 const std::string& directory_bot_jid)
57 signal_strategy_(signal_strategy),
58 directory_bot_jid_(directory_bot_jid) {
59 signal_strategy_->AddListener(this);
62 LogToServer::~LogToServer() {
63 signal_strategy_->RemoveListener(this);
66 void LogToServer::LogSessionStateChange(
67 protocol::ConnectionToHost::State state,
68 protocol::ErrorCode error) {
69 DCHECK(CalledOnValidThread());
71 scoped_ptr<ServerLogEntry> entry(
72 ServerLogEntry::MakeForSessionStateChange(state, error));
73 entry->AddClientFields();
74 entry->AddModeField(mode_);
76 MaybeExpireSessionId();
77 if (IsStartOfSession(state)) {
78 // Maybe set the session ID and start time.
79 if (session_id_.empty()) {
82 if (session_start_time_.is_null()) {
83 session_start_time_ = base::TimeTicks::Now();
87 if (!session_id_.empty()) {
88 entry->AddSessionId(session_id_);
91 // Maybe clear the session start time and log the session duration.
92 if (ShouldAddDuration(state) && !session_start_time_.is_null()) {
93 entry->AddSessionDuration(base::TimeTicks::Now() - session_start_time_);
96 if (IsEndOfSession(state)) {
97 session_start_time_ = base::TimeTicks();
104 void LogToServer::LogStatistics(ChromotingStats* statistics) {
105 DCHECK(CalledOnValidThread());
107 MaybeExpireSessionId();
109 scoped_ptr<ServerLogEntry> entry(
110 ServerLogEntry::MakeForStatistics(statistics));
111 entry->AddClientFields();
112 entry->AddModeField(mode_);
113 entry->AddSessionId(session_id_);
117 void LogToServer::OnSignalStrategyStateChange(SignalStrategy::State state) {
118 DCHECK(CalledOnValidThread());
120 if (state == SignalStrategy::CONNECTED) {
121 iq_sender_.reset(new IqSender(signal_strategy_));
122 SendPendingEntries();
123 } else if (state == SignalStrategy::DISCONNECTED) {
128 bool LogToServer::OnSignalStrategyIncomingStanza(
129 const buzz::XmlElement* stanza) {
133 void LogToServer::Log(const ServerLogEntry& entry) {
134 pending_entries_.push_back(entry);
135 SendPendingEntries();
138 void LogToServer::SendPendingEntries() {
139 if (iq_sender_ == NULL) {
142 if (pending_entries_.empty()) {
145 // Make one stanza containing all the pending entries.
146 scoped_ptr<XmlElement> stanza(ServerLogEntry::MakeStanza());
147 while (!pending_entries_.empty()) {
148 ServerLogEntry& entry = pending_entries_.front();
149 stanza->AddElement(entry.ToStanza().release());
150 pending_entries_.pop_front();
152 // Send the stanza to the server.
153 scoped_ptr<IqRequest> req = iq_sender_->SendIq(
154 buzz::STR_SET, directory_bot_jid_, stanza.Pass(),
155 IqSender::ReplyCallback());
156 // We ignore any response, so let the IqRequest be destroyed.
160 void LogToServer::GenerateSessionId() {
161 session_id_.resize(kSessionIdLength);
162 for (int i = 0; i < kSessionIdLength; i++) {
163 const int alphabet_size = arraysize(kSessionIdAlphabet) - 1;
164 session_id_[i] = kSessionIdAlphabet[base::RandGenerator(alphabet_size)];
166 session_id_generation_time_ = base::TimeTicks::Now();
169 void LogToServer::MaybeExpireSessionId() {
170 if (session_id_.empty()) {
174 base::TimeDelta max_age = base::TimeDelta::FromDays(kMaxSessionIdAgeDays);
175 if (base::TimeTicks::Now() - session_id_generation_time_ > max_age) {
176 // Log the old session ID.
177 scoped_ptr<ServerLogEntry> entry(
178 ServerLogEntry::MakeForSessionIdOld(session_id_));
179 entry->AddModeField(mode_);
182 // Generate a new session ID.
185 // Log the new session ID.
186 entry = ServerLogEntry::MakeForSessionIdNew(session_id_);
187 entry->AddModeField(mode_);
192 } // namespace client
194 } // namespace remoting