1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/system_logs/traffic_counters_log_source.h"
#include <sstream>
#include <string>
#include <utility>
#include "ash/public/cpp/network_config_service.h"
#include "base/functional/bind.h"
#include "base/json/json_writer.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "chrome/browser/ash/net/network_health/network_health_manager.h"
#include "chromeos/ash/components/network/network_event_log.h"
#include "content/public/browser/browser_thread.h"
namespace system_logs {
namespace {
constexpr char kTrafficCountersEntry[] = "traffic-counters";
constexpr char kSource[] = "source";
constexpr char kRxBytes[] = "rx_bytes";
constexpr char kTxBytes[] = "tx_bytes";
constexpr char kTrafficCountersKey[] = "traffic_counters";
constexpr char kLastResetTimeKey[] = "last_reset_time";
constexpr char kNotAvailable[] = "Not Available";
std::string GetSourceString(
chromeos::network_config::mojom::TrafficCounterSource source) {
std::stringstream ss;
ss << source;
return ss.str();
}
base::Value::List ParseTrafficCounters(
const std::vector<chromeos::network_config::mojom::TrafficCounterPtr>&
traffic_counters) {
base::Value::List traffic_counters_list;
for (const auto& tc : traffic_counters) {
base::Value::Dict traffic_counter;
traffic_counter.Set(kSource, GetSourceString(tc->source));
traffic_counter.Set(kRxBytes, static_cast<double>(tc->rx_bytes));
traffic_counter.Set(kTxBytes, static_cast<double>(tc->tx_bytes));
traffic_counters_list.Append(std::move(traffic_counter));
}
return traffic_counters_list;
}
} // namespace
TrafficCountersLogSource::TrafficCountersLogSource()
: SystemLogsSource("TrafficCountersLog") {
ash::GetNetworkConfigService(
remote_cros_network_config_.BindNewPipeAndPassReceiver());
ash::network_health::NetworkHealthManager::GetInstance()->BindHealthReceiver(
network_health_service_.BindNewPipeAndPassReceiver());
}
TrafficCountersLogSource::~TrafficCountersLogSource() = default;
void TrafficCountersLogSource::Fetch(SysLogsSourceCallback callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
DCHECK(!callback.is_null());
callback_ = std::move(callback);
traffic_counters_.clear();
network_health_service_->GetRecentlyActiveNetworks(
base::BindOnce(&TrafficCountersLogSource::OnGetRecentlyActiveNetworks,
weak_factory_.GetWeakPtr()));
}
void TrafficCountersLogSource::OnGetRecentlyActiveNetworks(
const std::vector<std::string>& guids) {
total_guids_ = guids.size();
for (const std::string& guid : guids) {
remote_cros_network_config_->RequestTrafficCounters(
guid,
base::BindOnce(&TrafficCountersLogSource::OnTrafficCountersReceived,
weak_factory_.GetWeakPtr(), guid));
}
}
void TrafficCountersLogSource::OnTrafficCountersReceived(
const std::string& guid,
std::vector<chromeos::network_config::mojom::TrafficCounterPtr>
traffic_counters) {
remote_cros_network_config_->GetManagedProperties(
guid, base::BindOnce(&TrafficCountersLogSource::OnGetManagedProperties,
weak_factory_.GetWeakPtr(), guid,
std::move(traffic_counters)));
}
void TrafficCountersLogSource::OnGetManagedProperties(
const std::string& guid,
std::vector<chromeos::network_config::mojom::TrafficCounterPtr>
traffic_counters,
chromeos::network_config::mojom::ManagedPropertiesPtr managed_properties) {
base::Value::Dict tc_dict;
tc_dict.Set(kTrafficCountersKey, ParseTrafficCounters(traffic_counters));
if (managed_properties && managed_properties->traffic_counter_properties &&
managed_properties->traffic_counter_properties->friendly_date
.has_value()) {
tc_dict.Set(
kLastResetTimeKey,
managed_properties->traffic_counter_properties->friendly_date.value());
} else {
tc_dict.Set(kLastResetTimeKey, kNotAvailable);
}
traffic_counters_.Set(guid, std::move(tc_dict));
SendResponseIfDone();
}
void TrafficCountersLogSource::SendResponseIfDone() {
total_guids_--;
if (total_guids_ > 0) {
return;
}
std::map<std::string, std::string> response;
std::string json;
base::JSONWriter::WriteWithOptions(
traffic_counters_, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json);
response[kTrafficCountersEntry] = std::move(json);
std::move(callback_).Run(
std::make_unique<SystemLogsResponse>(std::move(response)));
}
} // namespace system_logs
|