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/diagnostics_api.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/callback.h" | |
9 #include "base/json/json_reader.h" | |
10 #include "base/strings/string_number_conversions.h" | |
11 #include "base/values.h" | |
12 #include "chromeos/dbus/dbus_thread_manager.h" | |
13 #include "chromeos/dbus/debug_daemon_client.h" | |
14 | |
15 namespace extensions { | |
16 | |
17 namespace { | |
18 | |
19 const char kCount[] = "count"; | |
20 const char kDefaultCount[] = "1"; | |
21 const char kTTL[] = "ttl"; | |
22 const char kTimeout[] = "timeout"; | |
23 const char kSize[] = "size"; | |
24 | |
25 typedef base::Callback<void( | |
26 DiagnosticsSendPacketFunction::SendPacketResultCode result_code, | |
27 const std::string& ip, | |
28 double latency)> | |
29 SendPacketCallback; | |
30 | |
31 bool ParseResult(const std::string& status, | |
32 std::string* ip, | |
33 double* latency) { | |
34 // Parses the result and returns IP and latency. | |
35 scoped_ptr<base::Value> parsed_value(base::JSONReader::Read(status)); | |
36 if (!parsed_value) | |
37 return false; | |
38 | |
39 base::DictionaryValue* result = NULL; | |
40 if (!parsed_value->GetAsDictionary(&result) || result->size() != 1) | |
41 return false; | |
42 | |
43 // Returns the first item. | |
44 base::DictionaryValue::Iterator iterator(*result); | |
45 | |
46 const base::DictionaryValue* info; | |
47 if (!iterator.value().GetAsDictionary(&info)) | |
48 return false; | |
49 | |
50 if (info->GetDouble("avg", latency)) | |
51 return false; | |
52 | |
53 *ip = iterator.key(); | |
54 return true; | |
55 } | |
56 | |
57 void OnTestICMPCompleted( | |
58 const SendPacketCallback& callback, | |
59 bool succeeded, | |
60 const std::string& status) { | |
61 std::string ip; | |
62 double latency; | |
63 if (!status || !ParseResult(status, &ip, &latency)) { | |
64 callback.Run(DiagnosticsSendPacketFunction::SEND_PACKET_FAILED, "", 0.0); | |
65 } else { | |
66 callback.Run(DiagnosticsSendPacketFunction::SEND_PACKET_OK, | |
67 ip, | |
68 latency); | |
not at google - send to devlin
2013/06/22 00:14:33
It would be simpler to let ParseResult send the OK
Bei Zhang
2013/06/22 00:35:07
I think it'll be weird if you pass in a callback a
| |
69 } | |
70 } | |
71 | |
72 } // namespace | |
73 | |
74 void DiagnosticsSendPacketFunction::AsyncWorkStart() { | |
75 std::map<std::string, std::string> config; | |
76 config[kCount] = kDefaultCount; | |
77 if (parameters_->options.ttl) | |
78 config[kTTL] = base::IntToString(*parameters_->options.ttl); | |
79 if (parameters_->options.timeout) | |
80 config[kTimeout] = base::IntToString(*parameters_->options.timeout); | |
81 if (parameters_->options.size) | |
82 config[kSize] = base::IntToString(*parameters_->options.size); | |
83 | |
84 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient()-> | |
85 TestICMPWithOptions( | |
86 parameters_->options.ip, config, | |
87 base::Bind( | |
88 OnTestICMPCompleted, | |
89 base::Bind(&DiagnosticsSendPacketFunction::OnCompleted, this))); | |
90 } | |
91 | |
92 } // namespace extensions | |
OLD | NEW |