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/client_status_logger.h"
7 #include "base/logging.h"
8 #include "base/macros.h"
9 #include "base/rand_util.h"
10 #include "remoting/client/chromoting_stats.h"
11 #include "remoting/client/server_log_entry_client.h"
13 using remoting::protocol::ConnectionToHost
;
17 const char kSessionIdAlphabet
[] =
18 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
19 const int kSessionIdLength
= 20;
21 const int kMaxSessionIdAgeDays
= 1;
23 bool IsStartOfSession(ConnectionToHost::State state
) {
24 return state
== ConnectionToHost::INITIALIZING
||
25 state
== ConnectionToHost::CONNECTING
||
26 state
== ConnectionToHost::AUTHENTICATED
||
27 state
== ConnectionToHost::CONNECTED
;
30 bool IsEndOfSession(ConnectionToHost::State state
) {
31 return state
== ConnectionToHost::FAILED
||
32 state
== ConnectionToHost::CLOSED
;
35 bool ShouldAddDuration(ConnectionToHost::State state
) {
36 // Duration is added to log entries at the end of the session, as well as at
37 // some intermediate states where it is relevant (e.g. to determine how long
38 // it took for a session to become CONNECTED).
39 return IsEndOfSession(state
) || state
== ConnectionToHost::CONNECTED
;
46 ClientStatusLogger::ClientStatusLogger(ServerLogEntry::Mode mode
,
47 SignalStrategy
* signal_strategy
,
48 const std::string
& directory_bot_jid
)
49 : log_to_server_(mode
, signal_strategy
, directory_bot_jid
) {
52 ClientStatusLogger::~ClientStatusLogger() {
55 void ClientStatusLogger::LogSessionStateChange(
56 protocol::ConnectionToHost::State state
,
57 protocol::ErrorCode error
) {
58 DCHECK(CalledOnValidThread());
60 scoped_ptr
<ServerLogEntry
> entry(
61 MakeLogEntryForSessionStateChange(state
, error
));
62 AddClientFieldsToLogEntry(entry
.get());
63 entry
->AddModeField(log_to_server_
.mode());
65 MaybeExpireSessionId();
66 if (IsStartOfSession(state
)) {
67 // Maybe set the session ID and start time.
68 if (session_id_
.empty()) {
71 if (session_start_time_
.is_null()) {
72 session_start_time_
= base::TimeTicks::Now();
76 if (!session_id_
.empty()) {
77 AddSessionIdToLogEntry(entry
.get(), session_id_
);
80 // Maybe clear the session start time and log the session duration.
81 if (ShouldAddDuration(state
) && !session_start_time_
.is_null()) {
82 AddSessionDurationToLogEntry(entry
.get(),
83 base::TimeTicks::Now() - session_start_time_
);
86 if (IsEndOfSession(state
)) {
87 session_start_time_
= base::TimeTicks();
91 log_to_server_
.Log(*entry
.get());
94 void ClientStatusLogger::LogStatistics(ChromotingStats
* statistics
) {
95 DCHECK(CalledOnValidThread());
97 MaybeExpireSessionId();
99 scoped_ptr
<ServerLogEntry
> entry(MakeLogEntryForStatistics(statistics
));
100 AddClientFieldsToLogEntry(entry
.get());
101 entry
->AddModeField(log_to_server_
.mode());
102 AddSessionIdToLogEntry(entry
.get(), session_id_
);
103 log_to_server_
.Log(*entry
.get());
106 void ClientStatusLogger::SetSignalingStateForTest(SignalStrategy::State state
) {
107 log_to_server_
.OnSignalStrategyStateChange(state
);
110 void ClientStatusLogger::GenerateSessionId() {
111 session_id_
.resize(kSessionIdLength
);
112 for (int i
= 0; i
< kSessionIdLength
; i
++) {
113 const int alphabet_size
= arraysize(kSessionIdAlphabet
) - 1;
114 session_id_
[i
] = kSessionIdAlphabet
[base::RandGenerator(alphabet_size
)];
116 session_id_generation_time_
= base::TimeTicks::Now();
119 void ClientStatusLogger::MaybeExpireSessionId() {
120 if (session_id_
.empty()) {
124 base::TimeDelta max_age
= base::TimeDelta::FromDays(kMaxSessionIdAgeDays
);
125 if (base::TimeTicks::Now() - session_id_generation_time_
> max_age
) {
126 // Log the old session ID.
127 scoped_ptr
<ServerLogEntry
> entry(MakeLogEntryForSessionIdOld(session_id_
));
128 entry
->AddModeField(log_to_server_
.mode());
129 log_to_server_
.Log(*entry
.get());
131 // Generate a new session ID.
134 // Log the new session ID.
135 entry
= MakeLogEntryForSessionIdNew(session_id_
);
136 entry
->AddModeField(log_to_server_
.mode());
137 log_to_server_
.Log(*entry
.get());
141 } // namespace remoting