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 "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 void ParseResult(const SendPingPacketCallback& callback, | |
19 bool succeeded, | |
20 const std::string& status) { | |
21 if (!succeeded) { | |
22 callback.Run("Failed sending packet", "", 0.0); | |
23 return; | |
24 } | |
25 // Parses the result and returns IP and latency. | |
26 scoped_ptr<base::Value> parsed_value(base::JSONReader::Read(status)); | |
27 base::DictionaryValue* result; | |
28 if (!parsed_value.get() || !parsed_value->GetAsDictionary(&result)) { | |
29 callback.Run("Malformed reply from the backend.", "", 0.0); | |
30 return; | |
31 } | |
32 | |
33 // Returns the first item. | |
34 base::DictionaryValue::Iterator iterator(*result); | |
35 if (iterator.IsAtEnd()) { | |
36 callback.Run("Empty result from the backend.", "", 0.0); | |
37 return; | |
38 } | |
39 | |
40 std::string ip = iterator.key(); | |
xiaowenx
2013/06/18 19:07:37
const std::string& ip = iterator.key()?
Bei Zhang
2013/06/18 22:04:46
Done.
| |
41 const base::DictionaryValue* info; | |
42 if (iterator.value().GetAsDictionary(&info)) { | |
43 double latency; | |
44 if (info->GetDouble("avg", &latency)) { | |
45 callback.Run("", ip, latency); | |
46 return; | |
47 } | |
48 } | |
49 | |
50 callback.Run("Malformed reply from the backend.", "", 0.0); | |
51 return; | |
xiaowenx
2013/06/18 19:07:37
Unnecessary return? The other way to do this is t
stevenjb
2013/06/18 19:19:28
+1. I'd suggest:
const base::DictionaryValue* i
Bei Zhang
2013/06/18 22:04:46
Done.
| |
52 } | |
53 | |
54 } // namespace | |
55 | |
56 void SendPingPacket( | |
57 const std::string& ip, | |
58 const int* ttl, | |
59 const int* timeout, | |
60 const int* size, | |
61 const SendPingPacketCallback& callback) { | |
62 chromeos::DebugDaemonClient* debugd_client = | |
63 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient(); | |
stevenjb
2013/06/18 19:19:28
no need for local used just once further down
Bei Zhang
2013/06/18 22:04:46
Actually, I should test whether they are available
| |
64 std::map<std::string, std::string> config; | |
65 config["count"] = "1"; | |
stevenjb
2013/06/18 19:19:28
"count" and other keys should be consts defined at
Bei Zhang
2013/06/18 22:04:46
Done.
| |
66 if (ttl) config["ttl"] = base::IntToString(*ttl); | |
stevenjb
2013/06/18 19:19:28
2 lines here and below
Bei Zhang
2013/06/18 22:04:46
Done.
| |
67 if (timeout) config["timeout"] = base::IntToString(*timeout); | |
68 if (size) config["size"] = base::IntToString(*size); | |
69 debugd_client->TestICMPWithOptions( | |
70 ip, | |
71 config, | |
72 base::Bind(ParseResult, callback) | |
73 ); | |
74 } | |
75 | |
76 } // namespace extensions | |
OLD | NEW |