OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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 "icmp_packet_sender.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/chromeos/chromeos_version.h" |
| 9 #include "base/json/json_reader.h" |
| 10 #include "base/memory/singleton.h" |
| 11 #include "base/strings/string_number_conversions.h" |
| 12 |
| 13 #include "chromeos/dbus/dbus_thread_manager.h" |
| 14 #include "chromeos/dbus/debug_daemon_client.h" |
| 15 #include "chromeos/dbus/permission_broker_client.h" |
| 16 |
| 17 using base::Value; |
| 18 |
| 19 namespace chromeos { |
| 20 |
| 21 ICMPPacketSender::ICMPPacketSender() {} |
| 22 |
| 23 ICMPPacketSender::~ICMPPacketSender() {} |
| 24 |
| 25 ICMPPacketSender* ICMPPacketSender::GetInstance() { |
| 26 return Singleton<ICMPPacketSender>::get(); |
| 27 } |
| 28 |
| 29 void ICMPPacketSender::Send(const std::string& ip, |
| 30 int* ttl, int* timeout, int* size, |
| 31 const SendCallback& callback) { |
| 32 chromeos::DebugDaemonClient* debugd_client = |
| 33 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient(); |
| 34 std::map<std::string, std::string> config; |
| 35 if (ttl) |
| 36 config["ttl"] = base::IntToString(*ttl); |
| 37 if (timeout) |
| 38 config["timeout"] = base::IntToString(*timeout); |
| 39 if (size) |
| 40 config["size"] = base::IntToString(*size); |
| 41 debugd_client->TestICMPWithOptions(ip, config, |
| 42 base::Bind(&ICMPPacketSender::OnSend, |
| 43 base::Unretained(this), |
| 44 callback)); |
| 45 } |
| 46 |
| 47 void ICMPPacketSender::OnSend(const SendCallback& callback, |
| 48 bool succeeded, const std::string& status) { |
| 49 if (succeeded) { |
| 50 // Parses the result and returns IP and latency. |
| 51 scoped_ptr<Value> parsed_value(base::JSONReader::Read(status)); |
| 52 base::DictionaryValue* result; |
| 53 if (parsed_value.get() && parsed_value->GetAsDictionary(&result)) { |
| 54 base::DictionaryValue::Iterator iterator(*result); |
| 55 // Returns the first item. |
| 56 if (!iterator.IsAtEnd()) { |
| 57 std::string ip = iterator.key(); |
| 58 const base::DictionaryValue* info; |
| 59 if (iterator.value().GetAsDictionary(&info)) { |
| 60 int latency; |
| 61 if (info->GetInteger("time", &latency)) { |
| 62 callback.Run(true, ip, latency); |
| 63 return; |
| 64 } |
| 65 } |
| 66 } |
| 67 } |
| 68 } |
| 69 |
| 70 callback.Run(false, "", 0); |
| 71 } |
| 72 |
| 73 } // namespace chromeos |
OLD | NEW |