| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "remoting/client/client_status_logger.h" | |
| 6 | |
| 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" | |
| 12 | |
| 13 using remoting::protocol::ConnectionToHost; | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 const char kSessionIdAlphabet[] = | |
| 18 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; | |
| 19 const int kSessionIdLength = 20; | |
| 20 | |
| 21 const int kMaxSessionIdAgeDays = 1; | |
| 22 | |
| 23 bool IsStartOfSession(ConnectionToHost::State state) { | |
| 24 return state == ConnectionToHost::INITIALIZING || | |
| 25 state == ConnectionToHost::CONNECTING || | |
| 26 state == ConnectionToHost::AUTHENTICATED || | |
| 27 state == ConnectionToHost::CONNECTED; | |
| 28 } | |
| 29 | |
| 30 bool IsEndOfSession(ConnectionToHost::State state) { | |
| 31 return state == ConnectionToHost::FAILED || | |
| 32 state == ConnectionToHost::CLOSED; | |
| 33 } | |
| 34 | |
| 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; | |
| 40 } | |
| 41 | |
| 42 } // namespace | |
| 43 | |
| 44 namespace remoting { | |
| 45 | |
| 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) { | |
| 50 } | |
| 51 | |
| 52 ClientStatusLogger::~ClientStatusLogger() { | |
| 53 } | |
| 54 | |
| 55 void ClientStatusLogger::LogSessionStateChange( | |
| 56 protocol::ConnectionToHost::State state, | |
| 57 protocol::ErrorCode error) { | |
| 58 DCHECK(CalledOnValidThread()); | |
| 59 | |
| 60 std::unique_ptr<ServerLogEntry> entry( | |
| 61 MakeLogEntryForSessionStateChange(state, error)); | |
| 62 AddClientFieldsToLogEntry(entry.get()); | |
| 63 entry->AddModeField(log_to_server_.mode()); | |
| 64 | |
| 65 MaybeExpireSessionId(); | |
| 66 if (IsStartOfSession(state)) { | |
| 67 // Maybe set the session ID and start time. | |
| 68 if (session_id_.empty()) { | |
| 69 GenerateSessionId(); | |
| 70 } | |
| 71 if (session_start_time_.is_null()) { | |
| 72 session_start_time_ = base::TimeTicks::Now(); | |
| 73 } | |
| 74 } | |
| 75 | |
| 76 if (!session_id_.empty()) { | |
| 77 AddSessionIdToLogEntry(entry.get(), session_id_); | |
| 78 } | |
| 79 | |
| 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_); | |
| 84 } | |
| 85 | |
| 86 if (IsEndOfSession(state)) { | |
| 87 session_start_time_ = base::TimeTicks(); | |
| 88 session_id_.clear(); | |
| 89 } | |
| 90 | |
| 91 log_to_server_.Log(*entry.get()); | |
| 92 } | |
| 93 | |
| 94 void ClientStatusLogger::LogStatistics( | |
| 95 protocol::PerformanceTracker* perf_tracker) { | |
| 96 DCHECK(CalledOnValidThread()); | |
| 97 | |
| 98 MaybeExpireSessionId(); | |
| 99 | |
| 100 std::unique_ptr<ServerLogEntry> entry( | |
| 101 MakeLogEntryForStatistics(perf_tracker)); | |
| 102 AddClientFieldsToLogEntry(entry.get()); | |
| 103 entry->AddModeField(log_to_server_.mode()); | |
| 104 AddSessionIdToLogEntry(entry.get(), session_id_); | |
| 105 log_to_server_.Log(*entry.get()); | |
| 106 } | |
| 107 | |
| 108 void ClientStatusLogger::SetSignalingStateForTest(SignalStrategy::State state) { | |
| 109 log_to_server_.OnSignalStrategyStateChange(state); | |
| 110 } | |
| 111 | |
| 112 void ClientStatusLogger::GenerateSessionId() { | |
| 113 session_id_.resize(kSessionIdLength); | |
| 114 for (int i = 0; i < kSessionIdLength; i++) { | |
| 115 const int alphabet_size = arraysize(kSessionIdAlphabet) - 1; | |
| 116 session_id_[i] = kSessionIdAlphabet[base::RandGenerator(alphabet_size)]; | |
| 117 } | |
| 118 session_id_generation_time_ = base::TimeTicks::Now(); | |
| 119 } | |
| 120 | |
| 121 void ClientStatusLogger::MaybeExpireSessionId() { | |
| 122 if (session_id_.empty()) { | |
| 123 return; | |
| 124 } | |
| 125 | |
| 126 base::TimeDelta max_age = base::TimeDelta::FromDays(kMaxSessionIdAgeDays); | |
| 127 if (base::TimeTicks::Now() - session_id_generation_time_ > max_age) { | |
| 128 // Log the old session ID. | |
| 129 std::unique_ptr<ServerLogEntry> entry( | |
| 130 MakeLogEntryForSessionIdOld(session_id_)); | |
| 131 entry->AddModeField(log_to_server_.mode()); | |
| 132 log_to_server_.Log(*entry.get()); | |
| 133 | |
| 134 // Generate a new session ID. | |
| 135 GenerateSessionId(); | |
| 136 | |
| 137 // Log the new session ID. | |
| 138 entry = MakeLogEntryForSessionIdNew(session_id_); | |
| 139 entry->AddModeField(log_to_server_.mode()); | |
| 140 log_to_server_.Log(*entry.get()); | |
| 141 } | |
| 142 } | |
| 143 | |
| 144 } // namespace remoting | |
| OLD | NEW |