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 "chrome/browser/extensions/api/diagnostics/send_ping_packet.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/json/json_reader.h" |
| 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/values.h" |
| 11 #include "chromeos/dbus/dbus_thread_manager.h" |
| 12 #include "chromeos/dbus/debug_daemon_client.h" |
| 13 |
| 14 namespace extensions { |
| 15 |
| 16 namespace { |
| 17 |
| 18 const char kCount[] = "count"; |
| 19 const char kDefaultCount[] = "1"; |
| 20 const char kTTL[] = "ttl"; |
| 21 const char kTimeout[] = "timeout"; |
| 22 const char kSize[] = "size"; |
| 23 |
| 24 void ParseResult(const SendPingPacketCallback& callback, |
| 25 bool succeeded, |
| 26 const std::string& status) { |
| 27 if (!succeeded) { |
| 28 callback.Run(SEND_PING_PACKET_FAILED, "", 0.0); |
| 29 return; |
| 30 } |
| 31 // Parses the result and returns IP and latency. |
| 32 scoped_ptr<base::Value> parsed_value(base::JSONReader::Read(status)); |
| 33 base::DictionaryValue* result; |
| 34 if (!parsed_value.get() || !parsed_value->GetAsDictionary(&result)) { |
| 35 callback.Run(SEND_PING_PACKET_FAILED, "", 0.0); |
| 36 return; |
| 37 } |
| 38 |
| 39 // Returns the first item. |
| 40 base::DictionaryValue::Iterator iterator(*result); |
| 41 if (iterator.IsAtEnd()) { |
| 42 callback.Run(SEND_PING_PACKET_FAILED, "", 0.0); |
| 43 return; |
| 44 } |
| 45 |
| 46 const std::string& ip = iterator.key(); |
| 47 const base::DictionaryValue* info; |
| 48 if (!iterator.value().GetAsDictionary(&info)) { |
| 49 callback.Run(SEND_PING_PACKET_FAILED, "", 0.0); |
| 50 return; |
| 51 } |
| 52 |
| 53 double latency; |
| 54 if (info->GetDouble("avg", &latency)) { |
| 55 callback.Run(SEND_PING_PACKET_FAILED, "", 0.0); |
| 56 return; |
| 57 } |
| 58 |
| 59 callback.Run(SEND_PING_PACKET_OK, ip, latency); |
| 60 } |
| 61 |
| 62 } // namespace |
| 63 |
| 64 void SendPingPacket( |
| 65 const std::string& ip, |
| 66 const int* ttl, |
| 67 const int* timeout, |
| 68 const int* size, |
| 69 const SendPingPacketCallback& callback) { |
| 70 chromeos::DBusThreadManager* dbus_thread_manager = |
| 71 chromeos::DBusThreadManager::Get(); |
| 72 if (!dbus_thread_manager) { |
| 73 callback.Run(extensions::SEND_PING_PACKET_FAILED, "", 0.0); |
| 74 return; |
| 75 } |
| 76 |
| 77 chromeos::DebugDaemonClient* debugd_client = |
| 78 dbus_thread_manager->GetDebugDaemonClient(); |
| 79 if (!debugd_client) { |
| 80 callback.Run(extensions::SEND_PING_PACKET_FAILED, "", 0.0); |
| 81 return; |
| 82 } |
| 83 |
| 84 std::map<std::string, std::string> config; |
| 85 config[kCount] = kDefaultCount; |
| 86 if (ttl) |
| 87 config[kTTL] = base::IntToString(*ttl); |
| 88 if (timeout) |
| 89 config[kTimeout] = base::IntToString(*timeout); |
| 90 if (size) |
| 91 config[kSize] = base::IntToString(*size); |
| 92 |
| 93 debugd_client->TestICMPWithOptions( |
| 94 ip, |
| 95 config, |
| 96 base::Bind(ParseResult, callback)); |
| 97 } |
| 98 |
| 99 } // namespace extensions |
OLD | NEW |