Bring the browser window to the front when clicking on deprecated accelerator notific...
[chromium-blink-merge.git] / remoting / client / client_status_logger.cc
blobc7cc0fe00d4ade1e540fb16666f67bb8719e33fa
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/server_log_entry_client.h"
11 #include "remoting/protocol/performance_tracker.h"
13 using remoting::protocol::ConnectionToHost;
15 namespace {
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;
42 } // namespace
44 namespace remoting {
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()) {
69 GenerateSessionId();
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();
88 session_id_.clear();
91 log_to_server_.Log(*entry.get());
94 void ClientStatusLogger::LogStatistics(
95 protocol::PerformanceTracker* perf_tracker) {
96 DCHECK(CalledOnValidThread());
98 MaybeExpireSessionId();
100 scoped_ptr<ServerLogEntry> entry(MakeLogEntryForStatistics(perf_tracker));
101 AddClientFieldsToLogEntry(entry.get());
102 entry->AddModeField(log_to_server_.mode());
103 AddSessionIdToLogEntry(entry.get(), session_id_);
104 log_to_server_.Log(*entry.get());
107 void ClientStatusLogger::SetSignalingStateForTest(SignalStrategy::State state) {
108 log_to_server_.OnSignalStrategyStateChange(state);
111 void ClientStatusLogger::GenerateSessionId() {
112 session_id_.resize(kSessionIdLength);
113 for (int i = 0; i < kSessionIdLength; i++) {
114 const int alphabet_size = arraysize(kSessionIdAlphabet) - 1;
115 session_id_[i] = kSessionIdAlphabet[base::RandGenerator(alphabet_size)];
117 session_id_generation_time_ = base::TimeTicks::Now();
120 void ClientStatusLogger::MaybeExpireSessionId() {
121 if (session_id_.empty()) {
122 return;
125 base::TimeDelta max_age = base::TimeDelta::FromDays(kMaxSessionIdAgeDays);
126 if (base::TimeTicks::Now() - session_id_generation_time_ > max_age) {
127 // Log the old session ID.
128 scoped_ptr<ServerLogEntry> entry(MakeLogEntryForSessionIdOld(session_id_));
129 entry->AddModeField(log_to_server_.mode());
130 log_to_server_.Log(*entry.get());
132 // Generate a new session ID.
133 GenerateSessionId();
135 // Log the new session ID.
136 entry = MakeLogEntryForSessionIdNew(session_id_);
137 entry->AddModeField(log_to_server_.mode());
138 log_to_server_.Log(*entry.get());
142 } // namespace remoting