Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(44)

Side by Side Diff: remoting/client/client_telemetry_logger.cc

Issue 2643483003: [Remoting Android] Refactor ClientTelemetryLogger (Closed)
Patch Set: PTAL Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "remoting/client/client_telemetry_logger.h" 5 #include "remoting/client/client_telemetry_logger.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/rand_util.h" 8 #include "base/rand_util.h"
9 #include "remoting/base/telemetry_log_writer.h" 9 #include "remoting/base/telemetry_log_writer.h"
10 10
11 namespace { 11 namespace {
12 12
13 const char kSessionIdAlphabet[] = 13 const char kSessionIdAlphabet[] =
14 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; 14 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
15 const int kSessionIdLength = 20; 15 const int kSessionIdLength = 20;
16
17 const int kMaxSessionIdAgeDays = 1; 16 const int kMaxSessionIdAgeDays = 1;
18 17
19 } // namespace 18 } // namespace
20 19
21 namespace remoting { 20 namespace remoting {
22 21
23 struct ClientTelemetryLogger::HostInfo { 22 struct ClientTelemetryLogger::HostInfo {
24 const std::string host_version; 23 const std::string host_version;
25 const ChromotingEvent::Os host_os; 24 const ChromotingEvent::Os host_os;
26 const std::string host_os_version; 25 const std::string host_os_version;
27 }; 26 };
28 27
29 ClientTelemetryLogger::ClientTelemetryLogger(ChromotingEvent::Mode mode) 28 ClientTelemetryLogger::ClientTelemetryLogger(
30 : mode_(mode) {} 29 base::WeakPtr<ChromotingEventLogWriter> log_writer,
Sergey Ulanov 2017/01/19 00:50:38 ChromotingJniRuntime is guaranteed to outlive Chro
Yuwei 2017/01/19 23:00:00 Done. You are right. It should be safe enough to u
30 ChromotingEvent::Mode mode)
31 : mode_(mode), log_writer_(log_writer) {}
31 32
32 ClientTelemetryLogger::~ClientTelemetryLogger() {} 33 ClientTelemetryLogger::~ClientTelemetryLogger() {}
33 34
34 void ClientTelemetryLogger::SetHostInfo(const std::string& host_version, 35 void ClientTelemetryLogger::SetHostInfo(const std::string& host_version,
35 ChromotingEvent::Os host_os, 36 ChromotingEvent::Os host_os,
36 const std::string& host_os_version) { 37 const std::string& host_os_version) {
37 host_info_.reset(new HostInfo{host_version, host_os, host_os_version}); 38 host_info_.reset(new HostInfo{host_version, host_os, host_os_version});
38 } 39 }
39 40
40 void ClientTelemetryLogger::Start(
41 std::unique_ptr<UrlRequestFactory> request_factory,
42 const std::string& telemetry_base_url) {
43 DCHECK(!log_writer_);
44 DCHECK(thread_checker_.CalledOnValidThread());
45 log_writer_.reset(
46 new TelemetryLogWriter(telemetry_base_url, std::move(request_factory)));
47 }
48
49 void ClientTelemetryLogger::LogSessionStateChange( 41 void ClientTelemetryLogger::LogSessionStateChange(
50 ChromotingEvent::SessionState state, 42 ChromotingEvent::SessionState state,
51 ChromotingEvent::ConnectionError error) { 43 ChromotingEvent::ConnectionError error) {
52 DCHECK(log_writer_) << "Please call Start before posting logs.";
53 DCHECK(thread_checker_.CalledOnValidThread()); 44 DCHECK(thread_checker_.CalledOnValidThread());
54 ExpireSessionIdIfOutdated(); 45 RefreshSessionIdIfOutdated();
55
56 if (session_id_.empty()) {
57 GenerateSessionId();
58 }
59 if (session_start_time_.is_null()) { 46 if (session_start_time_.is_null()) {
60 session_start_time_ = base::TimeTicks::Now(); 47 session_start_time_ = base::TimeTicks::Now();
61 } 48 }
62 49
63 ChromotingEvent event = 50 ChromotingEvent event =
64 ClientTelemetryLogger::MakeSessionStateChangeEvent(state, error); 51 ClientTelemetryLogger::MakeSessionStateChangeEvent(state, error);
65 log_writer_->Log(event); 52 log_writer_->Log(event);
Sergey Ulanov 2017/01/19 00:50:38 Since log_writer_ is a weak pointer now, you need
Yuwei 2017/01/19 23:00:00 Just required that log_writer_ outlives the class.
66 53
67 if (ChromotingEvent::IsEndOfSession(state)) { 54 if (ChromotingEvent::IsEndOfSession(state)) {
68 session_id_.clear(); 55 session_id_.clear();
69 session_start_time_ = base::TimeTicks(); 56 session_start_time_ = base::TimeTicks();
70 } 57 }
71 } 58 }
72 59
73 void ClientTelemetryLogger::LogStatistics( 60 void ClientTelemetryLogger::LogStatistics(
74 protocol::PerformanceTracker* perf_tracker) { 61 protocol::PerformanceTracker* perf_tracker) {
75 DCHECK(log_writer_) << "Please call Start before posting logs.";
76 DCHECK(thread_checker_.CalledOnValidThread()); 62 DCHECK(thread_checker_.CalledOnValidThread());
77 ExpireSessionIdIfOutdated(); 63 RefreshSessionIdIfOutdated();
78 64
79 ChromotingEvent event = MakeStatsEvent(perf_tracker); 65 ChromotingEvent event = MakeStatsEvent(perf_tracker);
80 log_writer_->Log(event); 66 log_writer_->Log(event);
81 } 67 }
82 68
83 void ClientTelemetryLogger::SetAuthToken(const std::string& token) {
84 DCHECK(log_writer_) << "Please call Start before setting the token.";
85 DCHECK(thread_checker_.CalledOnValidThread());
86 log_writer_->SetAuthToken(token);
87 }
88
89 void ClientTelemetryLogger::SetAuthClosure(const base::Closure& closure) {
90 DCHECK(log_writer_) << "Please call Start before setting the closure.";
91 DCHECK(thread_checker_.CalledOnValidThread());
92 log_writer_->SetAuthClosure(closure);
93 }
94
95 void ClientTelemetryLogger::SetSessionIdGenerationTimeForTest( 69 void ClientTelemetryLogger::SetSessionIdGenerationTimeForTest(
96 base::TimeTicks gen_time) { 70 base::TimeTicks gen_time) {
97 session_id_generation_time_ = gen_time; 71 session_id_generation_time_ = gen_time;
98 } 72 }
99 73
100 void ClientTelemetryLogger::StartForTest(
101 std::unique_ptr<ChromotingEventLogWriter> writer) {
102 DCHECK(!log_writer_);
103 log_writer_ = std::move(writer);
104 }
105
106 // static 74 // static
107 ChromotingEvent::SessionState ClientTelemetryLogger::TranslateState( 75 ChromotingEvent::SessionState ClientTelemetryLogger::TranslateState(
108 protocol::ConnectionToHost::State state) { 76 protocol::ConnectionToHost::State state) {
109 switch (state) { 77 switch (state) {
110 case protocol::ConnectionToHost::State::INITIALIZING: 78 case protocol::ConnectionToHost::State::INITIALIZING:
111 return ChromotingEvent::SessionState::INITIALIZING; 79 return ChromotingEvent::SessionState::INITIALIZING;
112 case protocol::ConnectionToHost::State::CONNECTING: 80 case protocol::ConnectionToHost::State::CONNECTING:
113 return ChromotingEvent::SessionState::CONNECTING; 81 return ChromotingEvent::SessionState::CONNECTING;
114 case protocol::ConnectionToHost::State::AUTHENTICATED: 82 case protocol::ConnectionToHost::State::AUTHENTICATED:
115 return ChromotingEvent::SessionState::AUTHENTICATED; 83 return ChromotingEvent::SessionState::AUTHENTICATED;
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 152
185 void ClientTelemetryLogger::GenerateSessionId() { 153 void ClientTelemetryLogger::GenerateSessionId() {
186 session_id_.resize(kSessionIdLength); 154 session_id_.resize(kSessionIdLength);
187 for (int i = 0; i < kSessionIdLength; i++) { 155 for (int i = 0; i < kSessionIdLength; i++) {
188 const int alphabet_size = arraysize(kSessionIdAlphabet) - 1; 156 const int alphabet_size = arraysize(kSessionIdAlphabet) - 1;
189 session_id_[i] = kSessionIdAlphabet[base::RandGenerator(alphabet_size)]; 157 session_id_[i] = kSessionIdAlphabet[base::RandGenerator(alphabet_size)];
190 } 158 }
191 session_id_generation_time_ = base::TimeTicks::Now(); 159 session_id_generation_time_ = base::TimeTicks::Now();
192 } 160 }
193 161
194 void ClientTelemetryLogger::ExpireSessionIdIfOutdated() { 162 void ClientTelemetryLogger::RefreshSessionIdIfOutdated() {
195 if (session_id_.empty()) { 163 if (session_id_.empty()) {
164 GenerateSessionId();
196 return; 165 return;
197 } 166 }
198 167
199 base::TimeDelta max_age = base::TimeDelta::FromDays(kMaxSessionIdAgeDays); 168 base::TimeDelta max_age = base::TimeDelta::FromDays(kMaxSessionIdAgeDays);
200 if (base::TimeTicks::Now() - session_id_generation_time_ > max_age) { 169 if (base::TimeTicks::Now() - session_id_generation_time_ > max_age) {
201 // Log the old session ID. 170 // Log the old session ID.
202 ChromotingEvent event = MakeSessionIdOldEvent(); 171 ChromotingEvent event = MakeSessionIdOldEvent();
203 log_writer_->Log(event); 172 log_writer_->Log(event);
204 173
205 // Generate a new session ID. 174 // Generate a new session ID.
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 return event; 227 return event;
259 } 228 }
260 229
261 ChromotingEvent ClientTelemetryLogger::MakeSessionIdNewEvent() { 230 ChromotingEvent ClientTelemetryLogger::MakeSessionIdNewEvent() {
262 ChromotingEvent event(ChromotingEvent::Type::SESSION_ID_NEW); 231 ChromotingEvent event(ChromotingEvent::Type::SESSION_ID_NEW);
263 FillEventContext(&event); 232 FillEventContext(&event);
264 return event; 233 return event;
265 } 234 }
266 235
267 } // namespace remoting 236 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698