Chromium Code Reviews| 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..874fed8ffcfd81cde39f42d68134673aedc4ab41 |
| --- /dev/null |
| +++ b/chromeos/dbus/icmp_packet_sender.cc |
| @@ -0,0 +1,75 @@ |
| +// 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 "base/values.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(); |
|
stevenjb
2013/06/17 17:24:24
Avoid using Singleton<> in classes with an explici
Bei Zhang
2013/06/18 18:00:41
Done. Moved to the same directory containing the a
|
| +} |
| + |
| +void ICMPPacketSender::Send(const std::string& ip, |
| + int* ttl, int* timeout, int* size, |
|
stevenjb
2013/06/17 17:24:24
one param per line
Bei Zhang
2013/06/18 18:00:41
Done.
|
| + const SendCallback& callback) { |
| + chromeos::DebugDaemonClient* debugd_client = |
| + chromeos::DBusThreadManager::Get()->GetDebugDaemonClient(); |
| + std::map<std::string, std::string> config; |
| + config["count"] = "1"; |
| + 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), |
|
stevenjb
2013/06/17 17:24:24
Avoid Untretained with DBus calls. It looks like O
Bei Zhang
2013/06/18 18:00:41
Done.
|
| + callback)); |
| +} |
| + |
| +void ICMPPacketSender::OnSend(const SendCallback& callback, |
| + bool succeeded, const std::string& status) { |
|
stevenjb
2013/06/17 17:24:24
One param per line.
Bei Zhang
2013/06/18 18:00:41
Done.
|
| + if (succeeded) { |
|
stevenjb
2013/06/17 17:24:24
Early exit:
if (!succeeded) {
callback.Run(fals
Bei Zhang
2013/06/18 18:00:41
Done. Changed the signature of the callback to hav
|
| + // 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)) { |
| + double latency; |
| + if (info->GetDouble("avg", &latency)) { |
| + callback.Run(true, ip, latency); |
| + return; |
| + } |
| + } |
| + } |
| + } |
| + } |
| + |
| + callback.Run(false, "", 0); |
| +} |
| + |
| +} // namespace chromeos |