| Index: chromeos/dbus/icmp_packet_sender.cc
|
| diff --git a/chromeos/dbus/icmp_packet_sender.cc b/chromeos/dbus/icmp_packet_sender.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..4a72de236867d6f8e0bf556e77786d1131fb9ecb
|
| --- /dev/null
|
| +++ b/chromeos/dbus/icmp_packet_sender.cc
|
| @@ -0,0 +1,73 @@
|
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "icmp_packet_sender.h"
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/chromeos/chromeos_version.h"
|
| +#include "base/json/json_reader.h"
|
| +#include "base/memory/singleton.h"
|
| +#include "base/strings/string_number_conversions.h"
|
| +
|
| +#include "chromeos/dbus/dbus_thread_manager.h"
|
| +#include "chromeos/dbus/debug_daemon_client.h"
|
| +#include "chromeos/dbus/permission_broker_client.h"
|
| +
|
| +using base::Value;
|
| +
|
| +namespace chromeos {
|
| +
|
| +ICMPPacketSender::ICMPPacketSender() {}
|
| +
|
| +ICMPPacketSender::~ICMPPacketSender() {}
|
| +
|
| +ICMPPacketSender* ICMPPacketSender::GetInstance() {
|
| + return Singleton<ICMPPacketSender>::get();
|
| +}
|
| +
|
| +void ICMPPacketSender::Send(const std::string& ip,
|
| + int* ttl, int* timeout, int* size,
|
| + const SendCallback& callback) {
|
| + chromeos::DebugDaemonClient* debugd_client =
|
| + chromeos::DBusThreadManager::Get()->GetDebugDaemonClient();
|
| + std::map<std::string, std::string> config;
|
| + if (ttl)
|
| + config["ttl"] = base::IntToString(*ttl);
|
| + if (timeout)
|
| + config["timeout"] = base::IntToString(*timeout);
|
| + if (size)
|
| + config["size"] = base::IntToString(*size);
|
| + debugd_client->TestICMPWithOptions(ip, config,
|
| + base::Bind(&ICMPPacketSender::OnSend,
|
| + base::Unretained(this),
|
| + callback));
|
| +}
|
| +
|
| +void ICMPPacketSender::OnSend(const SendCallback& callback,
|
| + bool succeeded, const std::string& status) {
|
| + if (succeeded) {
|
| + // Parses the result and returns IP and latency.
|
| + scoped_ptr<Value> parsed_value(base::JSONReader::Read(status));
|
| + base::DictionaryValue* result;
|
| + if (parsed_value.get() && parsed_value->GetAsDictionary(&result)) {
|
| + base::DictionaryValue::Iterator iterator(*result);
|
| + // Returns the first item.
|
| + if (!iterator.IsAtEnd()) {
|
| + std::string ip = iterator.key();
|
| + const base::DictionaryValue* info;
|
| + if (iterator.value().GetAsDictionary(&info)) {
|
| + int latency;
|
| + if (info->GetInteger("time", &latency)) {
|
| + callback.Run(true, ip, latency);
|
| + return;
|
| + }
|
| + }
|
| + }
|
| + }
|
| + }
|
| +
|
| + callback.Run(false, "", 0);
|
| +}
|
| +
|
| +} // namespace chromeos
|
|
|