Chromium Code Reviews| 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 void ParseResult( | |
| 26 const base::Callback< | |
| 27 void(DiagnosticsSendPacketFunction::SendPacketResultCode result_code, | |
| 28 const std::string& ip, double latency)>& callback, | |
|
not at google - send to devlin
2013/06/21 17:13:24
TestICMPCallback?
Bei Zhang
2013/06/21 19:31:44
Done. I prefer SendPacketCallback because it's not
not at google - send to devlin
2013/06/22 00:14:32
Well there was already a TestICMPCallback in that
| |
| 29 bool succeeded, const std::string& status) { | |
|
stevenjb
2013/06/21 18:35:21
Each parameter on its own line
Bei Zhang
2013/06/21 19:31:44
Done.
| |
| 30 do { | |
| 31 if (!succeeded) | |
| 32 break; | |
| 33 | |
| 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 break; | |
| 38 | |
| 39 base::DictionaryValue* result = NULL; | |
| 40 if (!parsed_value->GetAsDictionary(&result) || result->size() != 1) | |
| 41 break; | |
| 42 | |
| 43 // Returns the first item. | |
| 44 base::DictionaryValue::Iterator iterator(*result); | |
| 45 const std::string& ip = iterator.key(); | |
| 46 const base::DictionaryValue* info; | |
| 47 if (!iterator.value().GetAsDictionary(&info)) | |
| 48 break; | |
| 49 | |
| 50 double latency; | |
| 51 if (info->GetDouble("avg", &latency)) | |
| 52 break; | |
| 53 | |
| 54 callback.Run(DiagnosticsSendPacketFunction::SEND_PACKET_OK, ip, latency); | |
| 55 return; | |
| 56 } while (false); | |
|
not at google - send to devlin
2013/06/21 17:13:24
I hope there is a way to factor this without emula
Jeffrey Yasskin
2013/06/21 17:33:50
I suppose another way is to just write the goto in
not at google - send to devlin
2013/06/21 17:40:44
I would prefer to factor it into a function that r
stevenjb
2013/06/21 18:35:21
+1, this should be broken up into a subfunction th
Bei Zhang
2013/06/21 19:31:44
This idea is great! We are actually using the do-w
| |
| 57 | |
| 58 callback.Run(DiagnosticsSendPacketFunction::SEND_PACKET_FAILED, "", 0.0); | |
| 59 } | |
| 60 | |
| 61 } // namespace | |
| 62 | |
| 63 void DiagnosticsSendPacketFunction::AsyncWorkStart() { | |
| 64 std::map<std::string, std::string> config; | |
| 65 config[kCount] = kDefaultCount; | |
| 66 if (parameters_->options.ttl) | |
| 67 config[kTTL] = base::IntToString(*parameters_->options.ttl); | |
| 68 if (parameters_->options.timeout) | |
| 69 config[kTimeout] = base::IntToString(*parameters_->options.timeout); | |
| 70 if (parameters_->options.size) | |
| 71 config[kSize] = base::IntToString(*parameters_->options.size); | |
| 72 | |
| 73 chromeos::DBusThreadManager::Get()->GetDebugDaemonClient()-> | |
| 74 TestICMPWithOptions( | |
| 75 parameters_->options.ip, config, | |
| 76 base::Bind( | |
| 77 ParseResult, | |
| 78 base::Bind(&DiagnosticsSendPacketFunction::OnCompleted, this))); | |
| 79 } | |
| 80 | |
| 81 } // namespace extensions | |
| OLD | NEW |