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"
20 #include "third_party/cros_system_api/dbus/service_constants.h"
26 const char kLogModule
[] = "NetworkConfigurationHandler";
28 NetworkConfigurationHandler
* g_configuration_handler_instance
= NULL
;
30 // None of these error messages are user-facing: they should only appear in
32 const char kErrorsListTag
[] = "errors";
33 const char kClearPropertiesFailedError
[] = "Error.ClearPropertiesFailed";
34 const char kClearPropertiesFailedErrorMessage
[] = "Clear properties failed";
35 const char kDBusFailedError
[] = "Error.DBusFailed";
36 const char kDBusFailedErrorMessage
[] = "DBus call failed.";
38 void ClearPropertiesCallback(
39 const std::vector
<std::string
>& names
,
40 const std::string
& service_path
,
41 const base::Closure
& callback
,
42 const network_handler::ErrorCallback
& error_callback
,
43 const base::ListValue
& result
) {
44 bool some_failed
= false;
45 for (size_t i
= 0; i
< result
.GetSize(); ++i
) {
47 if (result
.GetBoolean(i
, &success
)) {
53 NOTREACHED() << "Result garbled from ClearProperties";
58 DCHECK(names
.size() == result
.GetSize())
59 << "Result wrong size from ClearProperties.";
60 scoped_ptr
<base::DictionaryValue
> error_data(
61 network_handler::CreateErrorData(service_path
,
62 kClearPropertiesFailedError
,
63 kClearPropertiesFailedErrorMessage
));
64 LOG(ERROR
) << "ClearPropertiesCallback failed for service path: "
66 error_data
->Set("errors", result
.DeepCopy());
67 scoped_ptr
<base::ListValue
> name_list(new base::ListValue
);
68 name_list
->AppendStrings(names
);
69 error_data
->Set("names", name_list
.release());
70 error_callback
.Run(kClearPropertiesFailedError
, error_data
.Pass());
76 // Used to translate the dbus dictionary callback into one that calls
77 // the error callback if we have a failure.
78 void RunCallbackWithDictionaryValue(
79 const network_handler::DictionaryResultCallback
& callback
,
80 const network_handler::ErrorCallback
& error_callback
,
81 const std::string
& service_path
,
82 DBusMethodCallStatus call_status
,
83 const base::DictionaryValue
& value
) {
84 if (call_status
!= DBUS_METHOD_CALL_SUCCESS
) {
85 scoped_ptr
<base::DictionaryValue
> error_data(
86 network_handler::CreateErrorData(service_path
,
88 kDBusFailedErrorMessage
));
89 LOG(ERROR
) << "CallbackWithDictionaryValue failed for service path: "
91 error_callback
.Run(kDBusFailedError
, error_data
.Pass());
93 callback
.Run(service_path
, value
);
97 void RunCreateNetworkCallback(
98 const network_handler::StringResultCallback
& callback
,
99 const dbus::ObjectPath
& service_path
) {
100 callback
.Run(service_path
.value());
103 void IgnoreObjectPathCallback(const base::Closure
& callback
,
104 const dbus::ObjectPath
& object_path
) {
111 void NetworkConfigurationHandler::Initialize() {
112 CHECK(!g_configuration_handler_instance
);
113 g_configuration_handler_instance
= new NetworkConfigurationHandler
;
117 void NetworkConfigurationHandler::Shutdown() {
118 CHECK(g_configuration_handler_instance
);
119 delete g_configuration_handler_instance
;
120 g_configuration_handler_instance
= NULL
;
124 NetworkConfigurationHandler
* NetworkConfigurationHandler::Get() {
125 CHECK(g_configuration_handler_instance
)
126 << "NetworkConfigurationHandler::Get() called before Initialize()";
127 return g_configuration_handler_instance
;
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(
149 base::Bind(&IgnoreObjectPathCallback
, callback
),
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 ShillManagerClient
* manager
=
198 DBusThreadManager::Get()->GetShillManagerClient();
201 properties
.GetStringWithoutPathExpansion(flimflam::kTypeProperty
, &type
);
202 // Shill supports ConfigureServiceForProfile only for network type WiFi. In
203 // all other cases, we have to rely on GetService for now. This is
204 // unproblematic for VPN (user profile only), but will lead to inconsistencies
205 // with WiMax, for example.
206 if (type
== flimflam::kTypeWifi
) {
208 properties
.GetStringWithoutPathExpansion(flimflam::kProfileProperty
,
210 manager
->ConfigureServiceForProfile(
211 dbus::ObjectPath(profile
),
213 base::Bind(&RunCreateNetworkCallback
, callback
),
214 base::Bind(&network_handler::ShillErrorCallbackFunction
,
215 kLogModule
, "", error_callback
));
219 base::Bind(&RunCreateNetworkCallback
, callback
),
220 base::Bind(&network_handler::ShillErrorCallbackFunction
,
221 kLogModule
, "", error_callback
));
225 void NetworkConfigurationHandler::RemoveConfiguration(
226 const std::string
& service_path
,
227 const base::Closure
& callback
,
228 const network_handler::ErrorCallback
& error_callback
) const {
229 DBusThreadManager::Get()->GetShillServiceClient()->Remove(
230 dbus::ObjectPath(service_path
),
232 base::Bind(&network_handler::ShillErrorCallbackFunction
,
233 kLogModule
, service_path
, error_callback
));
236 NetworkConfigurationHandler::NetworkConfigurationHandler() {
239 NetworkConfigurationHandler::~NetworkConfigurationHandler() {
242 } // namespace chromeos