OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/diagnostics/diagnostics_api.h" | 5 #include "extensions/browser/api/diagnostics/diagnostics_api.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "chromeos/dbus/dbus_thread_manager.h" | 12 #include "chromeos/dbus/dbus_thread_manager.h" |
13 #include "chromeos/dbus/debug_daemon_client.h" | 13 #include "chromeos/dbus/debug_daemon_client.h" |
14 | 14 |
15 namespace extensions { | 15 namespace extensions { |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 const char kCount[] = "count"; | 19 const char kCount[] = "count"; |
20 const char kDefaultCount[] = "1"; | 20 const char kDefaultCount[] = "1"; |
21 const char kTTL[] = "ttl"; | 21 const char kTTL[] = "ttl"; |
22 const char kTimeout[] = "timeout"; | 22 const char kTimeout[] = "timeout"; |
23 const char kSize[] = "size"; | 23 const char kSize[] = "size"; |
24 | 24 |
25 typedef base::Callback<void( | 25 typedef base::Callback<void( |
26 DiagnosticsSendPacketFunction::SendPacketResultCode result_code, | 26 DiagnosticsSendPacketFunction::SendPacketResultCode result_code, |
27 const std::string& ip, | 27 const std::string& ip, |
28 double latency)> | 28 double latency)> SendPacketCallback; |
29 SendPacketCallback; | |
30 | 29 |
31 bool ParseResult(const std::string& status, | 30 bool ParseResult(const std::string& status, std::string* ip, double* latency) { |
32 std::string* ip, | |
33 double* latency) { | |
34 // Parses the result and returns IP and latency. | 31 // Parses the result and returns IP and latency. |
35 scoped_ptr<base::Value> parsed_value(base::JSONReader::Read(status)); | 32 scoped_ptr<base::Value> parsed_value(base::JSONReader::Read(status)); |
36 if (!parsed_value) | 33 if (!parsed_value) |
37 return false; | 34 return false; |
38 | 35 |
39 base::DictionaryValue* result = NULL; | 36 base::DictionaryValue* result = NULL; |
40 if (!parsed_value->GetAsDictionary(&result) || result->size() != 1) | 37 if (!parsed_value->GetAsDictionary(&result) || result->size() != 1) |
41 return false; | 38 return false; |
42 | 39 |
43 // Returns the first item. | 40 // Returns the first item. |
44 base::DictionaryValue::Iterator iterator(*result); | 41 base::DictionaryValue::Iterator iterator(*result); |
45 | 42 |
46 const base::DictionaryValue* info; | 43 const base::DictionaryValue* info; |
47 if (!iterator.value().GetAsDictionary(&info)) | 44 if (!iterator.value().GetAsDictionary(&info)) |
48 return false; | 45 return false; |
49 | 46 |
50 if (!info->GetDouble("avg", latency)) | 47 if (!info->GetDouble("avg", latency)) |
51 return false; | 48 return false; |
52 | 49 |
53 *ip = iterator.key(); | 50 *ip = iterator.key(); |
54 return true; | 51 return true; |
55 } | 52 } |
56 | 53 |
57 void OnTestICMPCompleted( | 54 void OnTestICMPCompleted(const SendPacketCallback& callback, |
58 const SendPacketCallback& callback, | 55 bool succeeded, |
59 bool succeeded, | 56 const std::string& status) { |
60 const std::string& status) { | |
61 std::string ip; | 57 std::string ip; |
62 double latency; | 58 double latency; |
63 if (!succeeded || !ParseResult(status, &ip, &latency)) { | 59 if (!succeeded || !ParseResult(status, &ip, &latency)) { |
64 callback.Run(DiagnosticsSendPacketFunction::SEND_PACKET_FAILED, "", 0.0); | 60 callback.Run(DiagnosticsSendPacketFunction::SEND_PACKET_FAILED, "", 0.0); |
65 } else { | 61 } else { |
66 callback.Run(DiagnosticsSendPacketFunction::SEND_PACKET_OK, | 62 callback.Run(DiagnosticsSendPacketFunction::SEND_PACKET_OK, ip, latency); |
67 ip, | |
68 latency); | |
69 } | 63 } |
70 } | 64 } |
71 | 65 |
72 } // namespace | 66 } // namespace |
73 | 67 |
74 void DiagnosticsSendPacketFunction::AsyncWorkStart() { | 68 void DiagnosticsSendPacketFunction::AsyncWorkStart() { |
75 std::map<std::string, std::string> config; | 69 std::map<std::string, std::string> config; |
76 config[kCount] = kDefaultCount; | 70 config[kCount] = kDefaultCount; |
77 if (parameters_->options.ttl) | 71 if (parameters_->options.ttl) |
78 config[kTTL] = base::IntToString(*parameters_->options.ttl); | 72 config[kTTL] = base::IntToString(*parameters_->options.ttl); |
79 if (parameters_->options.timeout) | 73 if (parameters_->options.timeout) |
80 config[kTimeout] = base::IntToString(*parameters_->options.timeout); | 74 config[kTimeout] = base::IntToString(*parameters_->options.timeout); |
81 if (parameters_->options.size) | 75 if (parameters_->options.size) |
82 config[kSize] = base::IntToString(*parameters_->options.size); | 76 config[kSize] = base::IntToString(*parameters_->options.size); |
83 | 77 |
84 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient()-> | 78 chromeos::DBusThreadManager::Get() |
85 TestICMPWithOptions( | 79 ->GetDebugDaemonClient() |
| 80 ->TestICMPWithOptions( |
86 parameters_->options.ip, config, | 81 parameters_->options.ip, config, |
87 base::Bind( | 82 base::Bind( |
88 OnTestICMPCompleted, | 83 OnTestICMPCompleted, |
89 base::Bind(&DiagnosticsSendPacketFunction::OnCompleted, this))); | 84 base::Bind(&DiagnosticsSendPacketFunction::OnCompleted, this))); |
90 } | 85 } |
91 | 86 |
92 } // namespace extensions | 87 } // namespace extensions |
OLD | NEW |