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