OLD | NEW |
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 #ifndef REMOTING_CLIENT_CLIENT_TELEMETRY_LOGGER_H_ | 5 #ifndef REMOTING_CLIENT_CLIENT_TELEMETRY_LOGGER_H_ |
6 #define REMOTING_CLIENT_CLIENT_TELEMETRY_LOGGER_H_ | 6 #define REMOTING_CLIENT_CLIENT_TELEMETRY_LOGGER_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "remoting/base/chromoting_event.h" | 14 #include "remoting/base/chromoting_event.h" |
15 #include "remoting/base/chromoting_event_log_writer.h" | 15 #include "remoting/base/chromoting_event_log_writer.h" |
16 #include "remoting/base/url_request.h" | 16 #include "remoting/base/url_request.h" |
17 #include "remoting/protocol/connection_to_host.h" | 17 #include "remoting/protocol/connection_to_host.h" |
18 #include "remoting/protocol/performance_tracker.h" | 18 #include "remoting/protocol/performance_tracker.h" |
19 | 19 |
20 namespace remoting { | 20 namespace remoting { |
21 | 21 |
22 // ClientTelemetryLogger sends client log entries to the telemetry server. | 22 // ClientTelemetryLogger sends client log entries to the telemetry server. |
23 // The logger should be run entirely on one single thread. | 23 // The logger should be run entirely on one single thread. |
24 // TODO(yuweih): Implement new features that session_logger.js provides. | 24 // TODO(yuweih): Implement new features that session_logger.js provides. |
25 // TODO(BUG 680752): Refactor this so that it's bound to only one session. | |
26 class ClientTelemetryLogger { | 25 class ClientTelemetryLogger { |
27 public: | 26 public: |
28 explicit ClientTelemetryLogger(ChromotingEvent::Mode mode); | 27 // |log_writer| must outlive ClientTelemetryLogger. |
| 28 ClientTelemetryLogger(ChromotingEventLogWriter* log_writer, |
| 29 ChromotingEvent::Mode mode); |
29 ~ClientTelemetryLogger(); | 30 ~ClientTelemetryLogger(); |
30 | 31 |
31 // Sets the host info to be posted along with other log data. By default | 32 // Sets the host info to be posted along with other log data. By default |
32 // no host info will be logged. | 33 // no host info will be logged. |
33 void SetHostInfo(const std::string& host_version, | 34 void SetHostInfo(const std::string& host_version, |
34 ChromotingEvent::Os host_os, | 35 ChromotingEvent::Os host_os, |
35 const std::string& host_os_version); | 36 const std::string& host_os_version); |
36 | 37 |
37 // Start must be called before posting logs or setting auth token or closure. | |
38 void Start(std::unique_ptr<UrlRequestFactory> request_factory, | |
39 const std::string& telemetry_base_url); | |
40 | |
41 void LogSessionStateChange(ChromotingEvent::SessionState state, | 38 void LogSessionStateChange(ChromotingEvent::SessionState state, |
42 ChromotingEvent::ConnectionError error); | 39 ChromotingEvent::ConnectionError error); |
43 | 40 |
44 // TODO(yuweih): Investigate possibility of making PerformanceTracker const. | 41 // TODO(yuweih): Investigate possibility of making PerformanceTracker const. |
45 void LogStatistics(protocol::PerformanceTracker* perf_tracker); | 42 void LogStatistics(protocol::PerformanceTracker* perf_tracker); |
46 | 43 |
47 // Authorization: The caller can either | |
48 // 1. have its own fetching schedule and manually call |SetAuthToken| when the | |
49 // token is fetched or renewed | |
50 // 2. or call |SetAuthClosure| and expect the logger to run the closure when | |
51 // it needs new token. See comments below. | |
52 | |
53 // Sets the auth token immediately. | |
54 void SetAuthToken(const std::string& token); | |
55 | |
56 // Sets the authorization closure. The closure should call |SetAuthToken| to | |
57 // set the token. The closure will be run when the logger needs authorization | |
58 // to send out the logs. | |
59 void SetAuthClosure(const base::Closure& closure); | |
60 | |
61 const std::string& session_id() const { return session_id_; } | 44 const std::string& session_id() const { return session_id_; } |
62 | 45 |
63 void SetSessionIdGenerationTimeForTest(base::TimeTicks gen_time); | 46 void SetSessionIdGenerationTimeForTest(base::TimeTicks gen_time); |
64 // Start the logger with a given log writer. Do not call Start before or | |
65 // after calling this function. | |
66 void StartForTest(std::unique_ptr<ChromotingEventLogWriter> writer); | |
67 | 47 |
68 static ChromotingEvent::SessionState TranslateState( | 48 static ChromotingEvent::SessionState TranslateState( |
69 protocol::ConnectionToHost::State state); | 49 protocol::ConnectionToHost::State state); |
70 | 50 |
71 static ChromotingEvent::ConnectionError TranslateError( | 51 static ChromotingEvent::ConnectionError TranslateError( |
72 protocol::ErrorCode state); | 52 protocol::ErrorCode state); |
73 | 53 |
74 private: | 54 private: |
75 struct HostInfo; | 55 struct HostInfo; |
76 | 56 |
77 void FillEventContext(ChromotingEvent* event) const; | 57 void FillEventContext(ChromotingEvent* event) const; |
78 | 58 |
79 // Generates a new random session ID. | 59 // Generates a new random session ID. |
80 void GenerateSessionId(); | 60 void GenerateSessionId(); |
81 | 61 |
82 // Expire the session ID if the maximum duration has been exceeded. | 62 // If not session ID has been set, simply generates a new one without sending |
83 // Sends SessionIdOld and SessionIdNew events describing the change of id. | 63 // any logs, otherwise expire the session ID if the maximum duration has been |
84 void ExpireSessionIdIfOutdated(); | 64 // exceeded, and sends SessionIdOld and SessionIdNew events describing the |
| 65 // change of id. |
| 66 void RefreshSessionIdIfOutdated(); |
85 | 67 |
86 ChromotingEvent MakeStatsEvent(protocol::PerformanceTracker* perf_tracker); | 68 ChromotingEvent MakeStatsEvent(protocol::PerformanceTracker* perf_tracker); |
87 ChromotingEvent MakeSessionStateChangeEvent( | 69 ChromotingEvent MakeSessionStateChangeEvent( |
88 ChromotingEvent::SessionState state, | 70 ChromotingEvent::SessionState state, |
89 ChromotingEvent::ConnectionError error); | 71 ChromotingEvent::ConnectionError error); |
90 ChromotingEvent MakeSessionIdOldEvent(); | 72 ChromotingEvent MakeSessionIdOldEvent(); |
91 ChromotingEvent MakeSessionIdNewEvent(); | 73 ChromotingEvent MakeSessionIdNewEvent(); |
92 | 74 |
93 // A randomly generated session ID to be attached to log messages. This | 75 // A randomly generated session ID to be attached to log messages. This |
94 // is regenerated at the start of a new session. | 76 // is regenerated at the start of a new session. |
95 std::string session_id_; | 77 std::string session_id_; |
96 | 78 |
97 base::TimeTicks session_start_time_; | 79 base::TimeTicks session_start_time_; |
98 | 80 |
99 base::TimeTicks session_id_generation_time_; | 81 base::TimeTicks session_id_generation_time_; |
100 | 82 |
101 ChromotingEvent::Mode mode_; | 83 ChromotingEvent::Mode mode_; |
102 | 84 |
103 std::unique_ptr<HostInfo> host_info_; | 85 std::unique_ptr<HostInfo> host_info_; |
104 | 86 |
105 // The log writer that actually sends log to the server. | 87 // The log writer that actually sends log to the server. |
106 std::unique_ptr<ChromotingEventLogWriter> log_writer_; | 88 ChromotingEventLogWriter* log_writer_; |
107 | 89 |
108 base::ThreadChecker thread_checker_; | 90 base::ThreadChecker thread_checker_; |
109 | 91 |
110 DISALLOW_COPY_AND_ASSIGN(ClientTelemetryLogger); | 92 DISALLOW_COPY_AND_ASSIGN(ClientTelemetryLogger); |
111 }; | 93 }; |
112 | 94 |
113 } // namespace remoting | 95 } // namespace remoting |
114 | 96 |
115 #endif // REMOTING_CLIENT_CLIENT_TELEMETRY_LOGGER_H_ | 97 #endif // REMOTING_CLIENT_CLIENT_TELEMETRY_LOGGER_H_ |
OLD | NEW |