Chromium Code Reviews| Index: chrome/browser/extensions/api/diagnostics/send_ping_packet_chromeos.cc |
| diff --git a/chrome/browser/extensions/api/diagnostics/send_ping_packet_chromeos.cc b/chrome/browser/extensions/api/diagnostics/send_ping_packet_chromeos.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3014b89c5b9f9bce3ffec5fd335cba61e3f510dd |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/diagnostics/send_ping_packet_chromeos.cc |
| @@ -0,0 +1,99 @@ |
| +// 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 "chrome/browser/extensions/api/diagnostics/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; |
|
not at google - send to devlin
2013/06/20 23:32:12
initialize pointers to NULL
Bei Zhang
2013/06/21 08:31:07
Done.
|
| + if (!parsed_value.get() || !parsed_value->GetAsDictionary(&result)) { |
|
not at google - send to devlin
2013/06/20 23:32:12
just check parsed_value, get() not necessary.
Bei Zhang
2013/06/21 08:31:07
Done.
|
| + 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; |
|
not at google - send to devlin
2013/06/20 23:32:12
this check should really be that size() == 1 since
Bei Zhang
2013/06/21 08:31:07
Done.
|
| + } |
| + |
| + 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); |
|
not at google - send to devlin
2013/06/20 23:32:12
For all of these failures, is there something more
Bei Zhang
2013/06/21 08:31:07
Our chromeos backend currently does not distinguis
|
| + 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(); |
| + if (!dbus_thread_manager) { |
|
not at google - send to devlin
2013/06/20 23:32:12
when can this happen?
Bei Zhang
2013/06/21 08:31:07
For the current code, it is protected by a CHECK.
|
| + callback.Run(extensions::SEND_PING_PACKET_FAILED, "", 0.0); |
| + return; |
| + } |
| + |
| + chromeos::DebugDaemonClient* debugd_client = |
| + dbus_thread_manager->GetDebugDaemonClient(); |
| + if (!debugd_client) { |
|
not at google - send to devlin
2013/06/20 23:32:12
when can this happen?
Bei Zhang
2013/06/21 08:31:07
It never happens for the current code.
On 2013/06/
|
| + 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 |