network_handler_callbacks.cc revision 3551c9c881056c480085172ff9840cab31610854
1// Copyright (c) 2012 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 "chromeos/network/network_handler_callbacks.h"
6
7#include "base/logging.h"
8#include "base/values.h"
9#include "chromeos/network/network_event_log.h"
10
11namespace chromeos {
12namespace network_handler {
13
14// None of these messages are user-facing, they should only appear in logs.
15const char kDBusFailedError[] = "Error.DBusFailed";
16const char kDBusFailedErrorMessage[] = "DBus call failed.";
17
18// These are names of fields in the error data dictionary for ErrorCallback.
19const char kErrorName[] = "errorName";
20const char kErrorDetail[] = "errorDetail";
21const char kDbusErrorName[] = "dbusErrorName";
22const char kDbusErrorMessage[] = "dbusErrorMessage";
23const char kPath[] = "path";
24
25base::DictionaryValue* CreateErrorData(const std::string& service_path,
26                                       const std::string& error_name,
27                                       const std::string& error_detail) {
28  return CreateDBusErrorData(service_path, error_name, error_detail, "", "");
29}
30
31base::DictionaryValue* CreateDBusErrorData(
32    const std::string& path,
33    const std::string& error_name,
34    const std::string& error_detail,
35    const std::string& dbus_error_name,
36    const std::string& dbus_error_message) {
37  base::DictionaryValue* error_data(new base::DictionaryValue);
38  error_data->SetString(kErrorName, error_name);
39  error_data->SetString(kErrorDetail, error_detail);
40  error_data->SetString(kDbusErrorName, dbus_error_name);
41  error_data->SetString(kDbusErrorMessage, dbus_error_message);
42  if (!path.empty())
43    error_data->SetString(kPath, path);
44  return error_data;
45}
46
47void ShillErrorCallbackFunction(const std::string& error_name,
48                                const std::string& path,
49                                const ErrorCallback& error_callback,
50                                const std::string& dbus_error_name,
51                                const std::string& dbus_error_message) {
52  std::string detail;
53  if (!path.empty())
54    detail += path + ": ";
55  detail += dbus_error_name;
56  if (!dbus_error_message.empty())
57    detail += ": " + dbus_error_message;
58  NET_LOG_ERROR(error_name, detail);
59
60  if (error_callback.is_null())
61    return;
62  scoped_ptr<base::DictionaryValue> error_data(
63      CreateDBusErrorData(path, error_name, detail,
64                          dbus_error_name, dbus_error_message));
65  error_callback.Run(error_name, error_data.Pass());
66}
67
68void GetPropertiesCallback(
69    const network_handler::DictionaryResultCallback& callback,
70    const network_handler::ErrorCallback& error_callback,
71    const std::string& path,
72    DBusMethodCallStatus call_status,
73    const base::DictionaryValue& value) {
74  if (call_status != DBUS_METHOD_CALL_SUCCESS) {
75    scoped_ptr<base::DictionaryValue> error_data(
76        network_handler::CreateErrorData(path,
77                                         kDBusFailedError,
78                                         kDBusFailedErrorMessage));
79    NET_LOG_ERROR(
80        base::StringPrintf("GetProperties failed. Status: %d", call_status),
81        path);
82    if (!error_callback.is_null())
83      error_callback.Run(kDBusFailedError, error_data.Pass());
84  } else if (!callback.is_null()) {
85    callback.Run(path, value);
86  }
87}
88
89}  // namespace network_handler
90}  // namespace chromeos
91