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.
4
5#include "chrome/browser/extensions/api/diagnostics/diagnostics_api.h"
6
7namespace SendPacket = extensions::api::diagnostics::SendPacket;
8
9namespace {
10
11const char kErrorPingNotImplemented[] = "Not implemented";
12const char kErrorPingFailed[] = "Failed to send ping packet";
13
14}
15
16namespace extensions {
17
18DiagnosticsSendPacketFunction::DiagnosticsSendPacketFunction() {}
19
20DiagnosticsSendPacketFunction::~DiagnosticsSendPacketFunction() {}
21
22bool DiagnosticsSendPacketFunction::Prepare() {
23  parameters_ = SendPacket::Params::Create(*args_);
24  EXTENSION_FUNCTION_VALIDATE(parameters_.get());
25  return true;
26}
27
28bool DiagnosticsSendPacketFunction::Respond() {
29  return error_.empty();
30}
31
32void DiagnosticsSendPacketFunction::OnCompleted(
33    SendPacketResultCode result_code,
34    const std::string& ip,
35    double latency) {
36  switch (result_code) {
37    case SEND_PACKET_OK: {
38      extensions::api::diagnostics::SendPacketResult result;
39      result.ip = ip;
40      result.latency = latency;
41      results_ = SendPacket::Results::Create(result);
42      break;
43    }
44    case SEND_PACKET_NOT_IMPLEMENTED:
45      SetError(kErrorPingNotImplemented);
46      break;
47    case SEND_PACKET_FAILED:
48      SetError(kErrorPingFailed);
49      break;
50  }
51  AsyncWorkCompleted();
52}
53
54}  // namespace extensions
55