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.
5 #include "chromeos/network/network_configuration_handler.h"
10 #include "base/bind.h"
11 #include "base/logging.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/values.h"
15 #include "chromeos/dbus/dbus_method_call_status.h"
16 #include "chromeos/dbus/dbus_thread_manager.h"
17 #include "chromeos/dbus/shill_manager_client.h"
18 #include "chromeos/dbus/shill_service_client.h"
19 #include "dbus/object_path.h"
23 static NetworkConfigurationHandler
* g_network_configuration_handler
= NULL
;
27 const char kLogModule
[] = "NetworkConfigurationHandler";
29 // None of these error messages are user-facing: they should only appear in
31 const char kErrorsListTag
[] = "errors";
32 const char kClearPropertiesFailedError
[] = "Error.ClearPropertiesFailed";
33 const char kClearPropertiesFailedErrorMessage
[] = "Clear properties failed";
34 const char kDBusFailedError
[] = "Error.DBusFailed";
35 const char kDBusFailedErrorMessage
[] = "DBus call failed.";
37 void ClearPropertiesCallback(
38 const std::vector
<std::string
>& names
,
39 const std::string
& service_path
,
40 const base::Closure
& callback
,
41 const network_handler::ErrorCallback
& error_callback
,
42 const base::ListValue
& result
) {
43 bool some_failed
= false;
44 for (size_t i
= 0; i
< result
.GetSize(); ++i
) {
46 if (result
.GetBoolean(i
, &success
)) {
52 NOTREACHED() << "Result garbled from ClearProperties";
57 DCHECK(names
.size() == result
.GetSize())
58 << "Result wrong size from ClearProperties.";
59 scoped_ptr
<base::DictionaryValue
> error_data(
60 network_handler::CreateErrorData(service_path
,
61 kClearPropertiesFailedError
,
62 kClearPropertiesFailedErrorMessage
));
63 LOG(ERROR
) << "ClearPropertiesCallback Failed for service path: "
65 error_data
->Set("errors", result
.DeepCopy());
66 scoped_ptr
<base::ListValue
> name_list(new base::ListValue
);
67 name_list
->AppendStrings(names
);
68 error_data
->Set("names", name_list
.release());
69 error_callback
.Run(kClearPropertiesFailedError
, error_data
.Pass());
75 // Used to translate the dbus dictionary callback into one that calls
76 // the error callback if we have a failure.
77 void RunCallbackWithDictionaryValue(
78 const network_handler::DictionaryResultCallback
& callback
,
79 const network_handler::ErrorCallback
& error_callback
,
80 const std::string
& service_path
,
81 DBusMethodCallStatus call_status
,
82 const base::DictionaryValue
& value
) {
83 if (call_status
!= DBUS_METHOD_CALL_SUCCESS
) {
84 scoped_ptr
<base::DictionaryValue
> error_data(
85 network_handler::CreateErrorData(service_path
,
87 kDBusFailedErrorMessage
));
88 LOG(ERROR
) << "CallbackWithDictionaryValue Failed for service path: "
90 error_callback
.Run(kDBusFailedError
, error_data
.Pass());
92 callback
.Run(service_path
, value
);
96 void RunCreateNetworkCallback(
97 const network_handler::StringResultCallback
& callback
,
98 const dbus::ObjectPath
& service_path
) {
99 callback
.Run(service_path
.value());
104 NetworkConfigurationHandler::NetworkConfigurationHandler() {
107 NetworkConfigurationHandler::~NetworkConfigurationHandler() {
111 void NetworkConfigurationHandler::Initialize() {
112 CHECK(!g_network_configuration_handler
);
113 g_network_configuration_handler
= new NetworkConfigurationHandler();
117 void NetworkConfigurationHandler::Shutdown() {
118 CHECK(g_network_configuration_handler
);
119 delete g_network_configuration_handler
;
120 g_network_configuration_handler
= NULL
;
124 NetworkConfigurationHandler
* NetworkConfigurationHandler::Get() {
125 CHECK(g_network_configuration_handler
)
126 << "NetworkConfigurationHandler::Get() called before Initialize()";
127 return g_network_configuration_handler
;
130 void NetworkConfigurationHandler::GetProperties(
131 const std::string
& service_path
,
132 const network_handler::DictionaryResultCallback
& callback
,
133 const network_handler::ErrorCallback
& error_callback
) const {
134 DBusThreadManager::Get()->GetShillServiceClient()->GetProperties(
135 dbus::ObjectPath(service_path
),
136 base::Bind(&RunCallbackWithDictionaryValue
,
142 void NetworkConfigurationHandler::SetProperties(
143 const std::string
& service_path
,
144 const base::DictionaryValue
& properties
,
145 const base::Closure
& callback
,
146 const network_handler::ErrorCallback
& error_callback
) const {
147 DBusThreadManager::Get()->GetShillManagerClient()->ConfigureService(
150 base::Bind(&network_handler::ShillErrorCallbackFunction
,
151 kLogModule
, service_path
, error_callback
));
154 void NetworkConfigurationHandler::ClearProperties(
155 const std::string
& service_path
,
156 const std::vector
<std::string
>& names
,
157 const base::Closure
& callback
,
158 const network_handler::ErrorCallback
& error_callback
) {
159 DBusThreadManager::Get()->GetShillServiceClient()->ClearProperties(
160 dbus::ObjectPath(service_path
),
162 base::Bind(&ClearPropertiesCallback
,
167 base::Bind(&network_handler::ShillErrorCallbackFunction
,
168 kLogModule
, service_path
, error_callback
));
171 void NetworkConfigurationHandler::Connect(
172 const std::string
& service_path
,
173 const base::Closure
& callback
,
174 const network_handler::ErrorCallback
& error_callback
) const {
175 DBusThreadManager::Get()->GetShillServiceClient()->Connect(
176 dbus::ObjectPath(service_path
),
178 base::Bind(&network_handler::ShillErrorCallbackFunction
,
179 kLogModule
, service_path
, error_callback
));
182 void NetworkConfigurationHandler::Disconnect(
183 const std::string
& service_path
,
184 const base::Closure
& callback
,
185 const network_handler::ErrorCallback
& error_callback
) const {
186 DBusThreadManager::Get()->GetShillServiceClient()->Disconnect(
187 dbus::ObjectPath(service_path
),
189 base::Bind(&network_handler::ShillErrorCallbackFunction
,
190 kLogModule
, service_path
, error_callback
));
193 void NetworkConfigurationHandler::CreateConfiguration(
194 const base::DictionaryValue
& properties
,
195 const network_handler::StringResultCallback
& callback
,
196 const network_handler::ErrorCallback
& error_callback
) const {
197 DBusThreadManager::Get()->GetShillManagerClient()->GetService(
199 base::Bind(&RunCreateNetworkCallback
, callback
),
200 base::Bind(&network_handler::ShillErrorCallbackFunction
,
201 kLogModule
, "", error_callback
));
204 void NetworkConfigurationHandler::RemoveConfiguration(
205 const std::string
& service_path
,
206 const base::Closure
& callback
,
207 const network_handler::ErrorCallback
& error_callback
) const {
208 DBusThreadManager::Get()->GetShillServiceClient()->Remove(
209 dbus::ObjectPath(service_path
),
211 base::Bind(&network_handler::ShillErrorCallbackFunction
,
212 kLogModule
, service_path
, error_callback
));
215 } // namespace chromeos