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