| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chromeos/network/network_configuration_handler.h" | 5 #include "chromeos/network/network_configuration_handler.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "chromeos/dbus/dbus_method_call_status.h" | 15 #include "chromeos/dbus/dbus_method_call_status.h" |
| 16 #include "chromeos/dbus/dbus_thread_manager.h" | 16 #include "chromeos/dbus/dbus_thread_manager.h" |
| 17 #include "chromeos/dbus/shill_manager_client.h" | 17 #include "chromeos/dbus/shill_manager_client.h" |
| 18 #include "chromeos/dbus/shill_service_client.h" | 18 #include "chromeos/dbus/shill_service_client.h" |
| 19 #include "chromeos/network/network_state_handler.h" | 19 #include "chromeos/network/network_state_handler.h" |
| 20 #include "dbus/object_path.h" | 20 #include "dbus/object_path.h" |
| 21 #include "third_party/cros_system_api/dbus/service_constants.h" | 21 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 22 | 22 |
| 23 namespace chromeos { | 23 namespace chromeos { |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 NetworkConfigurationHandler* g_configuration_handler_instance = NULL; | |
| 28 | |
| 29 // None of these error messages are user-facing: they should only appear in | 27 // None of these error messages are user-facing: they should only appear in |
| 30 // logs. | 28 // logs. |
| 31 const char kErrorsListTag[] = "errors"; | 29 const char kErrorsListTag[] = "errors"; |
| 32 const char kClearPropertiesFailedError[] = "Error.ClearPropertiesFailed"; | 30 const char kClearPropertiesFailedError[] = "Error.ClearPropertiesFailed"; |
| 33 const char kClearPropertiesFailedErrorMessage[] = "Clear properties failed"; | 31 const char kClearPropertiesFailedErrorMessage[] = "Clear properties failed"; |
| 34 const char kDBusFailedError[] = "Error.DBusFailed"; | 32 const char kDBusFailedError[] = "Error.DBusFailed"; |
| 35 const char kDBusFailedErrorMessage[] = "DBus call failed."; | 33 const char kDBusFailedErrorMessage[] = "DBus call failed."; |
| 36 | 34 |
| 37 void ClearPropertiesCallback( | 35 void ClearPropertiesCallback( |
| 38 const std::vector<std::string>& names, | 36 const std::vector<std::string>& names, |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 kDBusFailedError, | 84 kDBusFailedError, |
| 87 kDBusFailedErrorMessage)); | 85 kDBusFailedErrorMessage)); |
| 88 LOG(ERROR) << "CallbackWithDictionaryValue failed for service path: " | 86 LOG(ERROR) << "CallbackWithDictionaryValue failed for service path: " |
| 89 << service_path; | 87 << service_path; |
| 90 error_callback.Run(kDBusFailedError, error_data.Pass()); | 88 error_callback.Run(kDBusFailedError, error_data.Pass()); |
| 91 } else { | 89 } else { |
| 92 callback.Run(service_path, value); | 90 callback.Run(service_path, value); |
| 93 } | 91 } |
| 94 } | 92 } |
| 95 | 93 |
| 96 void RunCreateNetworkCallback( | |
| 97 const network_handler::StringResultCallback& callback, | |
| 98 const dbus::ObjectPath& service_path) { | |
| 99 callback.Run(service_path.value()); | |
| 100 // This may also get called when CreateConfiguration is used to update an | |
| 101 // existing configuration, so request a service update just in case. | |
| 102 // TODO(pneubeck): Separate 'Create' and 'Update' calls and only trigger | |
| 103 // this on an update. | |
| 104 NetworkStateHandler::Get()->RequestUpdateForNetwork(service_path.value()); | |
| 105 } | |
| 106 | |
| 107 void IgnoreObjectPathCallback(const base::Closure& callback, | 94 void IgnoreObjectPathCallback(const base::Closure& callback, |
| 108 const dbus::ObjectPath& object_path) { | 95 const dbus::ObjectPath& object_path) { |
| 109 callback.Run(); | 96 callback.Run(); |
| 110 } | 97 } |
| 111 | 98 |
| 112 } // namespace | 99 } // namespace |
| 113 | 100 |
| 114 // static | |
| 115 void NetworkConfigurationHandler::Initialize() { | |
| 116 CHECK(!g_configuration_handler_instance); | |
| 117 g_configuration_handler_instance = new NetworkConfigurationHandler; | |
| 118 } | |
| 119 | |
| 120 // static | |
| 121 void NetworkConfigurationHandler::Shutdown() { | |
| 122 CHECK(g_configuration_handler_instance); | |
| 123 delete g_configuration_handler_instance; | |
| 124 g_configuration_handler_instance = NULL; | |
| 125 } | |
| 126 | |
| 127 // static | |
| 128 NetworkConfigurationHandler* NetworkConfigurationHandler::Get() { | |
| 129 CHECK(g_configuration_handler_instance) | |
| 130 << "NetworkConfigurationHandler::Get() called before Initialize()"; | |
| 131 return g_configuration_handler_instance; | |
| 132 } | |
| 133 | |
| 134 void NetworkConfigurationHandler::GetProperties( | 101 void NetworkConfigurationHandler::GetProperties( |
| 135 const std::string& service_path, | 102 const std::string& service_path, |
| 136 const network_handler::DictionaryResultCallback& callback, | 103 const network_handler::DictionaryResultCallback& callback, |
| 137 const network_handler::ErrorCallback& error_callback) const { | 104 const network_handler::ErrorCallback& error_callback) const { |
| 138 DBusThreadManager::Get()->GetShillServiceClient()->GetProperties( | 105 DBusThreadManager::Get()->GetShillServiceClient()->GetProperties( |
| 139 dbus::ObjectPath(service_path), | 106 dbus::ObjectPath(service_path), |
| 140 base::Bind(&RunCallbackWithDictionaryValue, | 107 base::Bind(&RunCallbackWithDictionaryValue, |
| 141 callback, | 108 callback, |
| 142 error_callback, | 109 error_callback, |
| 143 service_path)); | 110 service_path)); |
| 144 } | 111 } |
| 145 | 112 |
| 146 void NetworkConfigurationHandler::SetProperties( | 113 void NetworkConfigurationHandler::SetProperties( |
| 147 const std::string& service_path, | 114 const std::string& service_path, |
| 148 const base::DictionaryValue& properties, | 115 const base::DictionaryValue& properties, |
| 149 const base::Closure& callback, | 116 const base::Closure& callback, |
| 150 const network_handler::ErrorCallback& error_callback) const { | 117 const network_handler::ErrorCallback& error_callback) const { |
| 151 DBusThreadManager::Get()->GetShillManagerClient()->ConfigureService( | 118 DBusThreadManager::Get()->GetShillManagerClient()->ConfigureService( |
| 152 properties, | 119 properties, |
| 153 base::Bind(&IgnoreObjectPathCallback, callback), | 120 base::Bind(&IgnoreObjectPathCallback, callback), |
| 154 base::Bind(&network_handler::ShillErrorCallbackFunction, | 121 base::Bind(&network_handler::ShillErrorCallbackFunction, |
| 155 service_path, error_callback)); | 122 service_path, error_callback)); |
| 156 NetworkStateHandler::Get()->RequestUpdateForNetwork(service_path); | 123 network_state_handler_->RequestUpdateForNetwork(service_path); |
| 157 } | 124 } |
| 158 | 125 |
| 159 void NetworkConfigurationHandler::ClearProperties( | 126 void NetworkConfigurationHandler::ClearProperties( |
| 160 const std::string& service_path, | 127 const std::string& service_path, |
| 161 const std::vector<std::string>& names, | 128 const std::vector<std::string>& names, |
| 162 const base::Closure& callback, | 129 const base::Closure& callback, |
| 163 const network_handler::ErrorCallback& error_callback) { | 130 const network_handler::ErrorCallback& error_callback) { |
| 164 DBusThreadManager::Get()->GetShillServiceClient()->ClearProperties( | 131 DBusThreadManager::Get()->GetShillServiceClient()->ClearProperties( |
| 165 dbus::ObjectPath(service_path), | 132 dbus::ObjectPath(service_path), |
| 166 names, | 133 names, |
| 167 base::Bind(&ClearPropertiesCallback, | 134 base::Bind(&ClearPropertiesCallback, |
| 168 names, | 135 names, |
| 169 service_path, | 136 service_path, |
| 170 callback, | 137 callback, |
| 171 error_callback), | 138 error_callback), |
| 172 base::Bind(&network_handler::ShillErrorCallbackFunction, | 139 base::Bind(&network_handler::ShillErrorCallbackFunction, |
| 173 service_path, error_callback)); | 140 service_path, error_callback)); |
| 174 } | 141 } |
| 175 | 142 |
| 176 void NetworkConfigurationHandler::CreateConfiguration( | 143 void NetworkConfigurationHandler::CreateConfiguration( |
| 177 const base::DictionaryValue& properties, | 144 const base::DictionaryValue& properties, |
| 178 const network_handler::StringResultCallback& callback, | 145 const network_handler::StringResultCallback& callback, |
| 179 const network_handler::ErrorCallback& error_callback) const { | 146 const network_handler::ErrorCallback& error_callback) { |
| 180 ShillManagerClient* manager = | 147 ShillManagerClient* manager = |
| 181 DBusThreadManager::Get()->GetShillManagerClient(); | 148 DBusThreadManager::Get()->GetShillManagerClient(); |
| 182 | 149 |
| 183 std::string type; | 150 std::string type; |
| 184 properties.GetStringWithoutPathExpansion(flimflam::kTypeProperty, &type); | 151 properties.GetStringWithoutPathExpansion(flimflam::kTypeProperty, &type); |
| 185 // Shill supports ConfigureServiceForProfile only for network type WiFi. In | 152 // Shill supports ConfigureServiceForProfile only for network type WiFi. In |
| 186 // all other cases, we have to rely on GetService for now. This is | 153 // all other cases, we have to rely on GetService for now. This is |
| 187 // unproblematic for VPN (user profile only), but will lead to inconsistencies | 154 // unproblematic for VPN (user profile only), but will lead to inconsistencies |
| 188 // with WiMax, for example. | 155 // with WiMax, for example. |
| 189 if (type == flimflam::kTypeWifi) { | 156 if (type == flimflam::kTypeWifi) { |
| 190 std::string profile; | 157 std::string profile; |
| 191 properties.GetStringWithoutPathExpansion(flimflam::kProfileProperty, | 158 properties.GetStringWithoutPathExpansion(flimflam::kProfileProperty, |
| 192 &profile); | 159 &profile); |
| 193 manager->ConfigureServiceForProfile( | 160 manager->ConfigureServiceForProfile( |
| 194 dbus::ObjectPath(profile), | 161 dbus::ObjectPath(profile), |
| 195 properties, | 162 properties, |
| 196 base::Bind(&RunCreateNetworkCallback, callback), | 163 base::Bind(&NetworkConfigurationHandler::RunCreateNetworkCallback, |
| 164 AsWeakPtr(), callback), |
| 197 base::Bind(&network_handler::ShillErrorCallbackFunction, | 165 base::Bind(&network_handler::ShillErrorCallbackFunction, |
| 198 "", error_callback)); | 166 "", error_callback)); |
| 199 } else { | 167 } else { |
| 200 manager->GetService( | 168 manager->GetService( |
| 201 properties, | 169 properties, |
| 202 base::Bind(&RunCreateNetworkCallback, callback), | 170 base::Bind(&NetworkConfigurationHandler::RunCreateNetworkCallback, |
| 171 AsWeakPtr(), callback), |
| 203 base::Bind(&network_handler::ShillErrorCallbackFunction, | 172 base::Bind(&network_handler::ShillErrorCallbackFunction, |
| 204 "", error_callback)); | 173 "", error_callback)); |
| 205 } | 174 } |
| 206 } | 175 } |
| 207 | 176 |
| 208 void NetworkConfigurationHandler::RemoveConfiguration( | 177 void NetworkConfigurationHandler::RemoveConfiguration( |
| 209 const std::string& service_path, | 178 const std::string& service_path, |
| 210 const base::Closure& callback, | 179 const base::Closure& callback, |
| 211 const network_handler::ErrorCallback& error_callback) const { | 180 const network_handler::ErrorCallback& error_callback) const { |
| 212 DBusThreadManager::Get()->GetShillServiceClient()->Remove( | 181 DBusThreadManager::Get()->GetShillServiceClient()->Remove( |
| 213 dbus::ObjectPath(service_path), | 182 dbus::ObjectPath(service_path), |
| 214 callback, | 183 callback, |
| 215 base::Bind(&network_handler::ShillErrorCallbackFunction, | 184 base::Bind(&network_handler::ShillErrorCallbackFunction, |
| 216 service_path, error_callback)); | 185 service_path, error_callback)); |
| 217 } | 186 } |
| 218 | 187 |
| 219 NetworkConfigurationHandler::NetworkConfigurationHandler() { | 188 NetworkConfigurationHandler::NetworkConfigurationHandler() |
| 189 : network_state_handler_(NULL) { |
| 220 } | 190 } |
| 221 | 191 |
| 222 NetworkConfigurationHandler::~NetworkConfigurationHandler() { | 192 NetworkConfigurationHandler::~NetworkConfigurationHandler() { |
| 223 } | 193 } |
| 224 | 194 |
| 195 void NetworkConfigurationHandler::Init( |
| 196 NetworkStateHandler* network_state_handler) { |
| 197 network_state_handler_ = network_state_handler; |
| 198 } |
| 199 |
| 200 void NetworkConfigurationHandler::RunCreateNetworkCallback( |
| 201 const network_handler::StringResultCallback& callback, |
| 202 const dbus::ObjectPath& service_path) { |
| 203 callback.Run(service_path.value()); |
| 204 // This may also get called when CreateConfiguration is used to update an |
| 205 // existing configuration, so request a service update just in case. |
| 206 // TODO(pneubeck): Separate 'Create' and 'Update' calls and only trigger |
| 207 // this on an update. |
| 208 network_state_handler_->RequestUpdateForNetwork(service_path.value()); |
| 209 } |
| 210 |
| 211 // static |
| 212 NetworkConfigurationHandler* NetworkConfigurationHandler::InitializeForTest( |
| 213 NetworkStateHandler* network_state_handler) { |
| 214 NetworkConfigurationHandler* handler = new NetworkConfigurationHandler(); |
| 215 handler->Init(network_state_handler); |
| 216 return handler; |
| 217 } |
| 218 |
| 225 } // namespace chromeos | 219 } // namespace chromeos |
| OLD | NEW |