Chromium Code Reviews| 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/base/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 kMaxSendAttempts = 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 weak_factory_(this) { | |
| 23 weak_ptr_ = weak_factory_.GetWeakPtr(); | |
|
Sergey Ulanov
2017/01/19 00:50:38
this can be initialized in the initializer list ab
Yuwei
2017/01/19 23:00:00
Obsolete.
BTW I don't think you can move weak_ptr
Sergey Ulanov
2017/01/23 00:36:49
Yes, it's better to keep WeakFactory last, but in
Yuwei
2017/01/23 02:19:47
Acknowledged.
| |
| 24 } | |
| 22 TelemetryLogWriter::~TelemetryLogWriter() {} | 25 TelemetryLogWriter::~TelemetryLogWriter() {} |
| 23 | 26 |
| 24 void TelemetryLogWriter::SetAuthToken(const std::string& auth_token) { | 27 void TelemetryLogWriter::SetAuthToken(const std::string& auth_token) { |
| 25 DCHECK(thread_checker_.CalledOnValidThread()); | 28 DCHECK(thread_checker_.CalledOnValidThread()); |
| 26 auth_token_ = auth_token; | 29 auth_token_ = auth_token; |
| 27 SendPendingEntries(); | 30 SendPendingEntries(); |
| 28 } | 31 } |
| 29 | 32 |
| 30 void TelemetryLogWriter::SetAuthClosure(const base::Closure& closure) { | 33 void TelemetryLogWriter::SetAuthClosure(const base::Closure& closure) { |
| 31 DCHECK(thread_checker_.CalledOnValidThread()); | 34 DCHECK(thread_checker_.CalledOnValidThread()); |
| 32 auth_closure_ = closure; | 35 auth_closure_ = closure; |
| 33 } | 36 } |
| 34 | 37 |
| 35 void TelemetryLogWriter::Log(const ChromotingEvent& entry) { | 38 void TelemetryLogWriter::Log(const ChromotingEvent& entry) { |
| 36 DCHECK(thread_checker_.CalledOnValidThread()); | 39 DCHECK(thread_checker_.CalledOnValidThread()); |
| 37 pending_entries_.push_back(entry); | 40 pending_entries_.push_back(entry); |
| 38 SendPendingEntries(); | 41 SendPendingEntries(); |
| 39 } | 42 } |
| 40 | 43 |
| 44 base::WeakPtr<TelemetryLogWriter> TelemetryLogWriter::GetWeakPtr() { | |
| 45 return weak_ptr_; | |
| 46 } | |
| 47 | |
| 41 void TelemetryLogWriter::SendPendingEntries() { | 48 void TelemetryLogWriter::SendPendingEntries() { |
| 42 DCHECK(thread_checker_.CalledOnValidThread()); | 49 DCHECK(thread_checker_.CalledOnValidThread()); |
| 43 if (request_ || pending_entries_.empty()) { | 50 if (request_ || pending_entries_.empty()) { |
| 44 return; | 51 return; |
| 45 } | 52 } |
| 46 | 53 |
| 47 std::unique_ptr<base::ListValue> events(new base::ListValue()); | 54 std::unique_ptr<base::ListValue> events(new base::ListValue()); |
| 48 while (!pending_entries_.empty()) { | 55 while (!pending_entries_.empty()) { |
| 49 ChromotingEvent& entry = pending_entries_.front(); | 56 ChromotingEvent& entry = pending_entries_.front(); |
| 50 events->Append(entry.CopyDictionaryValue()); | 57 events->Append(entry.CopyDictionaryValue()); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 107 request_.reset(); // This may also destroy the result. | 114 request_.reset(); // This may also destroy the result. |
| 108 if (should_call_auth_closure) { | 115 if (should_call_auth_closure) { |
| 109 VLOG(1) << "Request is unauthorized. Trying to call the auth closure..."; | 116 VLOG(1) << "Request is unauthorized. Trying to call the auth closure..."; |
| 110 auth_closure_.Run(); | 117 auth_closure_.Run(); |
| 111 } else { | 118 } else { |
| 112 SendPendingEntries(); | 119 SendPendingEntries(); |
| 113 } | 120 } |
| 114 } | 121 } |
| 115 | 122 |
| 116 } // namespace remoting | 123 } // namespace remoting |
| OLD | NEW |