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 #include "remoting/signaling/telemetry_log_writer.h" | 5 #include "remoting/base/telemetry_log_writer.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/json/json_string_value_serializer.h" | 9 #include "base/json/json_string_value_serializer.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "net/http/http_status_code.h" | 11 #include "net/http/http_status_code.h" |
12 | 12 |
13 namespace remoting { | 13 namespace remoting { |
14 | 14 |
15 const int kMaxTries = 5; | 15 const int kMaxSendAttempts = 5; |
16 | 16 |
17 TelemetryLogWriter::TelemetryLogWriter( | 17 TelemetryLogWriter::TelemetryLogWriter( |
18 const std::string& telemetry_base_url, | 18 const std::string& telemetry_base_url, |
19 std::unique_ptr<UrlRequestFactory> request_factory) | 19 std::unique_ptr<UrlRequestFactory> request_factory) |
20 : telemetry_base_url_(telemetry_base_url), | 20 : telemetry_base_url_(telemetry_base_url), |
21 request_factory_(std::move(request_factory)) {} | 21 request_factory_(std::move(request_factory)) {} |
22 TelemetryLogWriter::~TelemetryLogWriter() {} | 22 TelemetryLogWriter::~TelemetryLogWriter() {} |
23 | 23 |
24 void TelemetryLogWriter::SetAuthToken(const std::string& auth_token) { | 24 void TelemetryLogWriter::SetAuthToken(const std::string& auth_token) { |
25 DCHECK(CalledOnValidThread()); | 25 DCHECK(thread_checker_.CalledOnValidThread()); |
26 auth_token_ = auth_token; | 26 auth_token_ = auth_token; |
27 SendPendingEntries(); | 27 SendPendingEntries(); |
28 } | 28 } |
29 | 29 |
30 void TelemetryLogWriter::SetAuthClosure(const base::Closure& closure) { | 30 void TelemetryLogWriter::SetAuthClosure(const base::Closure& closure) { |
31 DCHECK(CalledOnValidThread()); | 31 DCHECK(thread_checker_.CalledOnValidThread()); |
32 auth_closure_ = closure; | 32 auth_closure_ = closure; |
33 } | 33 } |
34 | 34 |
35 void TelemetryLogWriter::Log(const ChromotingEvent& entry) { | 35 void TelemetryLogWriter::Log(const ChromotingEvent& entry) { |
36 DCHECK(CalledOnValidThread()); | 36 DCHECK(thread_checker_.CalledOnValidThread()); |
37 pending_entries_.push_back(entry); | 37 pending_entries_.push_back(entry); |
38 SendPendingEntries(); | 38 SendPendingEntries(); |
39 } | 39 } |
40 | 40 |
41 void TelemetryLogWriter::SendPendingEntries() { | 41 void TelemetryLogWriter::SendPendingEntries() { |
42 DCHECK(CalledOnValidThread()); | 42 DCHECK(thread_checker_.CalledOnValidThread()); |
43 if (request_ || pending_entries_.empty()) { | 43 if (request_ || pending_entries_.empty()) { |
44 return; | 44 return; |
45 } | 45 } |
46 | 46 |
47 base::ListValue* events = new base::ListValue(); | 47 std::unique_ptr<base::ListValue> events(new base::ListValue()); |
48 while (!pending_entries_.empty()) { | 48 while (!pending_entries_.empty()) { |
49 ChromotingEvent& entry = pending_entries_.front(); | 49 ChromotingEvent& entry = pending_entries_.front(); |
50 events->Append(entry.CopyDictionaryValue()); | 50 events->Append(entry.CopyDictionaryValue()); |
51 entry.IncrementTryCount(); | 51 entry.IncrementSendAttempts(); |
52 sending_entries_.push_back(std::move(entry)); | 52 sending_entries_.push_back(std::move(entry)); |
53 pending_entries_.pop_front(); | 53 pending_entries_.pop_front(); |
54 } | 54 } |
55 base::DictionaryValue log_dictionary; | 55 base::DictionaryValue log_dictionary; |
56 log_dictionary.Set("event", std::unique_ptr<base::Value>(events)); | 56 log_dictionary.Set("event", std::move(events)); |
57 | 57 |
58 std::string json; | 58 std::string json; |
59 JSONStringValueSerializer serializer(&json); | 59 JSONStringValueSerializer serializer(&json); |
60 if (!serializer.Serialize(log_dictionary)) { | 60 if (!serializer.Serialize(log_dictionary)) { |
61 LOG(ERROR) << "Failed to serialize log to JSON."; | 61 LOG(ERROR) << "Failed to serialize log to JSON."; |
62 return; | 62 return; |
63 } | 63 } |
64 PostJsonToServer(json); | 64 PostJsonToServer(json); |
65 } | 65 } |
66 | 66 |
67 void TelemetryLogWriter::PostJsonToServer(const std::string& json) { | 67 void TelemetryLogWriter::PostJsonToServer(const std::string& json) { |
68 DCHECK(CalledOnValidThread()); | 68 DCHECK(thread_checker_.CalledOnValidThread()); |
69 DCHECK(!request_); | 69 DCHECK(!request_); |
70 request_ = request_factory_->CreateUrlRequest(UrlRequest::Type::POST, | 70 request_ = request_factory_->CreateUrlRequest(UrlRequest::Type::POST, |
71 telemetry_base_url_); | 71 telemetry_base_url_); |
72 if (!auth_token_.empty()) { | 72 if (!auth_token_.empty()) { |
73 request_->AddHeader("Authorization:Bearer " + auth_token_); | 73 request_->AddHeader("Authorization:Bearer " + auth_token_); |
74 } | 74 } |
75 | 75 |
76 VLOG(1) << "Posting log to telemetry server: " << json; | 76 VLOG(1) << "Posting log to telemetry server: " << json; |
77 | 77 |
78 request_->SetPostData("application/json", json); | 78 request_->SetPostData("application/json", json); |
79 request_->Start( | 79 request_->Start( |
80 base::Bind(&TelemetryLogWriter::OnSendLogResult, base::Unretained(this))); | 80 base::Bind(&TelemetryLogWriter::OnSendLogResult, base::Unretained(this))); |
81 } | 81 } |
82 | 82 |
83 void TelemetryLogWriter::OnSendLogResult( | 83 void TelemetryLogWriter::OnSendLogResult( |
84 const remoting::UrlRequest::Result& result) { | 84 const remoting::UrlRequest::Result& result) { |
85 DCHECK(CalledOnValidThread()); | 85 DCHECK(thread_checker_.CalledOnValidThread()); |
86 DCHECK(request_); | 86 DCHECK(request_); |
87 if (!result.success || result.status != net::HTTP_OK) { | 87 if (!result.success || result.status != net::HTTP_OK) { |
88 LOG(WARNING) << "Error occur when sending logs to the telemetry server, " | 88 LOG(WARNING) << "Error occur when sending logs to the telemetry server, " |
89 << "status: " << result.status; | 89 << "status: " << result.status; |
90 VLOG(1) << "Response body: \n" | 90 VLOG(1) << "Response body: \n" |
91 << "body: " << result.response_body; | 91 << "body: " << result.response_body; |
92 | |
93 // Reverse iterating + push_front in order to restore the order of logs. | |
94 for (auto i = sending_entries_.rbegin(); i < sending_entries_.rend(); i++) { | |
95 ChromotingEvent& entry = *i; | |
Sergey Ulanov
2016/05/12 22:48:05
Don't need this. Just use *i below.
Yuwei
2016/05/13 00:40:45
Done.
| |
96 if (entry.send_attempts() < kMaxSendAttempts) { | |
Sergey Ulanov
2016/05/12 22:48:04
replace this with
if (entry.send_attempts() >= kM
Yuwei
2016/05/13 00:40:45
Done. Good catch :)
| |
97 pending_entries_.push_front(std::move(entry)); | |
98 } | |
99 } | |
92 } else { | 100 } else { |
93 VLOG(1) << "Successfully sent " << sending_entries_.size() | 101 VLOG(1) << "Successfully sent " << sending_entries_.size() |
94 << " log(s) to telemetry server."; | 102 << " log(s) to telemetry server."; |
95 sending_entries_.clear(); | |
96 } | 103 } |
97 while (!sending_entries_.empty()) { | 104 sending_entries_.clear(); |
98 ChromotingEvent& entry = sending_entries_.front(); | |
99 if (entry.try_count() < kMaxTries) { | |
100 pending_entries_.push_back(std::move(entry)); | |
101 } | |
102 sending_entries_.pop_front(); | |
103 } | |
104 bool should_call_auth_closure = | 105 bool should_call_auth_closure = |
105 result.status == net::HTTP_UNAUTHORIZED && !auth_closure_.is_null(); | 106 result.status == net::HTTP_UNAUTHORIZED && !auth_closure_.is_null(); |
106 request_.reset(); // This may also destroy the result. | 107 request_.reset(); // This may also destroy the result. |
107 if (should_call_auth_closure) { | 108 if (should_call_auth_closure) { |
108 VLOG(1) << "Request is unauthorized. Trying to call the auth closure..."; | 109 VLOG(1) << "Request is unauthorized. Trying to call the auth closure..."; |
109 auth_closure_.Run(); | 110 auth_closure_.Run(); |
110 } else { | 111 } else { |
111 SendPendingEntries(); | 112 SendPendingEntries(); |
112 } | 113 } |
113 } | 114 } |
114 | 115 |
115 } // namespace remoting | 116 } // namespace remoting |
OLD | NEW |