Index: chrome/browser/extensions/api/diagnostics_private/send_ping_packet_chromeos.cc |
diff --git a/chrome/browser/extensions/api/diagnostics_private/send_ping_packet_chromeos.cc b/chrome/browser/extensions/api/diagnostics_private/send_ping_packet_chromeos.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c59570c17525873dcd96f3e31d71f73dd4e6603f |
--- /dev/null |
+++ b/chrome/browser/extensions/api/diagnostics_private/send_ping_packet_chromeos.cc |
@@ -0,0 +1,100 @@ |
+// 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 "send_ping_packet.h" |
+ |
+#include "base/bind.h" |
+#include "base/json/json_reader.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" |
+ |
+namespace extensions { |
+ |
+namespace { |
+ |
+const char kCount[] = "count"; |
+const char kDefaultCount[] = "1"; |
+const char kTTL[] = "ttl"; |
+const char kTimeout[] = "timeout"; |
+const char kSize[] = "size"; |
+ |
+void ParseResult(const SendPingPacketCallback& callback, |
+ bool succeeded, |
+ const std::string& status) { |
+ if (!succeeded) { |
+ callback.Run(SEND_PING_PACKET_FAILED, "", 0.0); |
+ return; |
+ } |
+ // Parses the result and returns IP and latency. |
+ scoped_ptr<base::Value> parsed_value(base::JSONReader::Read(status)); |
+ base::DictionaryValue* result; |
+ if (!parsed_value.get() || !parsed_value->GetAsDictionary(&result)) { |
+ callback.Run(SEND_PING_PACKET_FAILED, "", 0.0); |
+ return; |
+ } |
+ |
+ // Returns the first item. |
+ base::DictionaryValue::Iterator iterator(*result); |
+ if (iterator.IsAtEnd()) { |
+ callback.Run(SEND_PING_PACKET_FAILED, "", 0.0); |
+ return; |
+ } |
+ |
+ const std::string& ip = iterator.key(); |
+ const base::DictionaryValue* info; |
+ if (!iterator.value().GetAsDictionary(&info)) { |
+ callback.Run(SEND_PING_PACKET_FAILED, "", 0.0); |
+ return; |
+ } |
+ |
+ double latency; |
+ if (info->GetDouble("avg", &latency)) { |
+ callback.Run(SEND_PING_PACKET_FAILED, "", 0.0); |
+ return; |
+ } |
+ |
+ callback.Run(SEND_PING_PACKET_OK, ip, latency); |
+} |
+ |
+} // namespace |
+ |
+void SendPingPacket( |
+ const std::string& ip, |
+ const int* ttl, |
+ const int* timeout, |
+ const int* size, |
+ const SendPingPacketCallback& callback) { |
+ chromeos::DBusThreadManager* dbus_thread_manager |
+ = chromeos::DBusThreadManager::Get(); |
stevenjb
2013/06/18 22:14:51
nit: = on previous line
Bei Zhang
2013/06/18 23:48:40
Done. Sorry for a nits like this...
|
+ if (!dbus_thread_manager) { |
+ callback.Run(extensions::SEND_PING_PACKET_FAILED, "", 0.0); |
+ return; |
+ } |
+ |
+ chromeos::DebugDaemonClient* debugd_client = |
+ dbus_thread_manager->GetDebugDaemonClient(); |
+ if (!debugd_client) { |
+ callback.Run(extensions::SEND_PING_PACKET_FAILED, "", 0.0); |
+ return; |
+ } |
+ |
+ std::map<std::string, std::string> config; |
+ config[kCount] = kDefaultCount; |
+ if (ttl) |
+ config[kTTL] = base::IntToString(*ttl); |
+ if (timeout) |
+ config[kTimeout] = base::IntToString(*timeout); |
+ if (size) |
+ config[kSize] = base::IntToString(*size); |
+ |
+ debugd_client->TestICMPWithOptions( |
+ ip, |
+ config, |
+ base::Bind(ParseResult, callback) |
+ ); |
+} |
+ |
+} // namespace extensions |