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