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
3 // found in the LICENSE file.
5 #include "extensions/browser/api/diagnostics/diagnostics_api.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"
15 namespace extensions
{
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";
25 typedef base::Callback
<void(
26 DiagnosticsSendPacketFunction::SendPacketResultCode result_code
,
27 const std::string
& ip
,
28 double latency
)> SendPacketCallback
;
30 bool ParseResult(const std::string
& status
, std::string
* ip
, double* latency
) {
31 // Parses the result and returns IP and latency.
32 scoped_ptr
<base::Value
> parsed_value(base::JSONReader::Read(status
));
36 base::DictionaryValue
* result
= NULL
;
37 if (!parsed_value
->GetAsDictionary(&result
) || result
->size() != 1)
40 // Returns the first item.
41 base::DictionaryValue::Iterator
iterator(*result
);
43 const base::DictionaryValue
* info
;
44 if (!iterator
.value().GetAsDictionary(&info
))
47 if (!info
->GetDouble("avg", latency
))
54 void OnTestICMPCompleted(const SendPacketCallback
& callback
,
56 const std::string
& status
) {
59 if (!succeeded
|| !ParseResult(status
, &ip
, &latency
)) {
60 callback
.Run(DiagnosticsSendPacketFunction::SEND_PACKET_FAILED
, "", 0.0);
62 callback
.Run(DiagnosticsSendPacketFunction::SEND_PACKET_OK
, ip
, latency
);
68 void DiagnosticsSendPacketFunction::AsyncWorkStart() {
69 std::map
<std::string
, std::string
> config
;
70 config
[kCount
] = kDefaultCount
;
71 if (parameters_
->options
.ttl
)
72 config
[kTTL
] = base::IntToString(*parameters_
->options
.ttl
);
73 if (parameters_
->options
.timeout
)
74 config
[kTimeout
] = base::IntToString(*parameters_
->options
.timeout
);
75 if (parameters_
->options
.size
)
76 config
[kSize
] = base::IntToString(*parameters_
->options
.size
);
78 chromeos::DBusThreadManager::Get()
79 ->GetDebugDaemonClient()
80 ->TestICMPWithOptions(
81 parameters_
->options
.ip
, config
,
84 base::Bind(&DiagnosticsSendPacketFunction::OnCompleted
, this)));
87 } // namespace extensions