| 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/shill_property_handler.h" | 5 #include "chromeos/network/shill_property_handler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/format_macros.h" | 8 #include "base/format_macros.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "chromeos/dbus/dbus_thread_manager.h" | 13 #include "chromeos/dbus/dbus_thread_manager.h" |
| 14 #include "chromeos/dbus/shill_device_client.h" | 14 #include "chromeos/dbus/shill_device_client.h" |
| 15 #include "chromeos/dbus/shill_ipconfig_client.h" | 15 #include "chromeos/dbus/shill_ipconfig_client.h" |
| 16 #include "chromeos/dbus/shill_manager_client.h" | 16 #include "chromeos/dbus/shill_manager_client.h" |
| 17 #include "chromeos/dbus/shill_service_client.h" | 17 #include "chromeos/dbus/shill_service_client.h" |
| 18 #include "chromeos/network/network_event_log.h" | 18 #include "chromeos/network/network_event_log.h" |
| 19 #include "chromeos/network/network_state.h" | 19 #include "chromeos/network/network_state.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 { | 23 namespace { |
| 24 | 24 |
| 25 const char kLogModule[] = "ShillPropertyHandler"; | |
| 26 | |
| 27 // Limit the number of services or devices we observe. Since they are listed in | 25 // Limit the number of services or devices we observe. Since they are listed in |
| 28 // priority order, it should be reasonable to ignore services past this. | 26 // priority order, it should be reasonable to ignore services past this. |
| 29 const size_t kMaxObserved = 100; | 27 const size_t kMaxObserved = 100; |
| 30 | 28 |
| 31 const base::ListValue* GetListValue(const std::string& key, | 29 const base::ListValue* GetListValue(const std::string& key, |
| 32 const base::Value& value) { | 30 const base::Value& value) { |
| 33 const base::ListValue* vlist = NULL; | 31 const base::ListValue* vlist = NULL; |
| 34 if (!value.GetAsList(&vlist)) { | 32 if (!value.GetAsList(&vlist)) { |
| 35 LOG(ERROR) << "Error parsing key as list: " << key; | 33 LOG(ERROR) << "Error parsing key as list: " << key; |
| 36 return NULL; | 34 return NULL; |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 base::Bind(&ShillPropertyHandler::EnableTechnologyFailed, | 151 base::Bind(&ShillPropertyHandler::EnableTechnologyFailed, |
| 154 AsWeakPtr(), technology, error_callback)); | 152 AsWeakPtr(), technology, error_callback)); |
| 155 } else { | 153 } else { |
| 156 // Imediately clear locally from enabled and enabling lists. | 154 // Imediately clear locally from enabled and enabling lists. |
| 157 enabled_technologies_.erase(technology); | 155 enabled_technologies_.erase(technology); |
| 158 enabling_technologies_.erase(technology); | 156 enabling_technologies_.erase(technology); |
| 159 shill_manager_->DisableTechnology( | 157 shill_manager_->DisableTechnology( |
| 160 technology, | 158 technology, |
| 161 base::Bind(&base::DoNothing), | 159 base::Bind(&base::DoNothing), |
| 162 base::Bind(&network_handler::ShillErrorCallbackFunction, | 160 base::Bind(&network_handler::ShillErrorCallbackFunction, |
| 163 kLogModule, technology, error_callback)); | 161 technology, error_callback)); |
| 164 } | 162 } |
| 165 } | 163 } |
| 166 | 164 |
| 167 void ShillPropertyHandler::RequestScan() const { | 165 void ShillPropertyHandler::RequestScan() const { |
| 168 shill_manager_->RequestScan( | 166 shill_manager_->RequestScan( |
| 169 "", | 167 "", |
| 170 base::Bind(&base::DoNothing), | 168 base::Bind(&base::DoNothing), |
| 171 base::Bind(&network_handler::ShillErrorCallbackFunction, | 169 base::Bind(&network_handler::ShillErrorCallbackFunction, |
| 172 kLogModule, "", network_handler::ErrorCallback())); | 170 "", network_handler::ErrorCallback())); |
| 173 } | 171 } |
| 174 | 172 |
| 175 void ShillPropertyHandler::ConnectToBestServices() const { | 173 void ShillPropertyHandler::ConnectToBestServices() const { |
| 176 network_event_log::AddEntry(kLogModule, "ConnectToBestServices", ""); | 174 NET_LOG_EVENT("ConnectToBestServices", ""); |
| 177 shill_manager_->ConnectToBestServices( | 175 shill_manager_->ConnectToBestServices( |
| 178 base::Bind(&base::DoNothing), | 176 base::Bind(&base::DoNothing), |
| 179 base::Bind(&network_handler::ShillErrorCallbackFunction, | 177 base::Bind(&network_handler::ShillErrorCallbackFunction, |
| 180 kLogModule, "", network_handler::ErrorCallback())); | 178 "", network_handler::ErrorCallback())); |
| 181 } | 179 } |
| 182 | 180 |
| 183 void ShillPropertyHandler::RequestProperties(ManagedState::ManagedType type, | 181 void ShillPropertyHandler::RequestProperties(ManagedState::ManagedType type, |
| 184 const std::string& path) { | 182 const std::string& path) { |
| 185 if (pending_updates_[type].find(path) != pending_updates_[type].end()) | 183 if (pending_updates_[type].find(path) != pending_updates_[type].end()) |
| 186 return; // Update already requested. | 184 return; // Update already requested. |
| 187 | 185 |
| 188 pending_updates_[type].insert(path); | 186 pending_updates_[type].insert(path); |
| 189 if (type == ManagedState::MANAGED_TYPE_NETWORK) { | 187 if (type == ManagedState::MANAGED_TYPE_NETWORK) { |
| 190 DBusThreadManager::Get()->GetShillServiceClient()->GetProperties( | 188 DBusThreadManager::Get()->GetShillServiceClient()->GetProperties( |
| 191 dbus::ObjectPath(path), | 189 dbus::ObjectPath(path), |
| 192 base::Bind(&ShillPropertyHandler::GetPropertiesCallback, | 190 base::Bind(&ShillPropertyHandler::GetPropertiesCallback, |
| 193 AsWeakPtr(), type, path)); | 191 AsWeakPtr(), type, path)); |
| 194 } else if (type == ManagedState::MANAGED_TYPE_DEVICE) { | 192 } else if (type == ManagedState::MANAGED_TYPE_DEVICE) { |
| 195 DBusThreadManager::Get()->GetShillDeviceClient()->GetProperties( | 193 DBusThreadManager::Get()->GetShillDeviceClient()->GetProperties( |
| 196 dbus::ObjectPath(path), | 194 dbus::ObjectPath(path), |
| 197 base::Bind(&ShillPropertyHandler::GetPropertiesCallback, | 195 base::Bind(&ShillPropertyHandler::GetPropertiesCallback, |
| 198 AsWeakPtr(), type, path)); | 196 AsWeakPtr(), type, path)); |
| 199 } else { | 197 } else { |
| 200 NOTREACHED(); | 198 NOTREACHED(); |
| 201 } | 199 } |
| 202 } | 200 } |
| 203 | 201 |
| 204 void ShillPropertyHandler::OnPropertyChanged(const std::string& key, | 202 void ShillPropertyHandler::OnPropertyChanged(const std::string& key, |
| 205 const base::Value& value) { | 203 const base::Value& value) { |
| 206 if (ManagerPropertyChanged(key, value)) | 204 if (ManagerPropertyChanged(key, value)) { |
| 207 listener_->ManagerPropertyChanged(); | 205 std::string detail = key; |
| 206 detail += " = " + network_event_log::ValueAsString(value); |
| 207 NET_LOG_DEBUG("ManagerPropertyChanged", detail); |
| 208 listener_->NotifyManagerPropertyChanged(); |
| 209 } |
| 208 // If the service or device list changed and there are no pending | 210 // If the service or device list changed and there are no pending |
| 209 // updates, signal the state list changed callback. | 211 // updates, signal the state list changed callback. |
| 210 if ((key == flimflam::kServicesProperty) && | 212 if ((key == flimflam::kServicesProperty) && |
| 211 pending_updates_[ManagedState::MANAGED_TYPE_NETWORK].size() == 0) { | 213 pending_updates_[ManagedState::MANAGED_TYPE_NETWORK].size() == 0) { |
| 212 listener_->ManagedStateListChanged(ManagedState::MANAGED_TYPE_NETWORK); | 214 listener_->ManagedStateListChanged(ManagedState::MANAGED_TYPE_NETWORK); |
| 213 } | 215 } |
| 214 if (key == flimflam::kDevicesProperty && | 216 if (key == flimflam::kDevicesProperty && |
| 215 pending_updates_[ManagedState::MANAGED_TYPE_DEVICE].size() == 0) { | 217 pending_updates_[ManagedState::MANAGED_TYPE_DEVICE].size() == 0) { |
| 216 listener_->ManagedStateListChanged(ManagedState::MANAGED_TYPE_DEVICE); | 218 listener_->ManagedStateListChanged(ManagedState::MANAGED_TYPE_DEVICE); |
| 217 } | 219 } |
| 218 } | 220 } |
| 219 | 221 |
| 220 //------------------------------------------------------------------------------ | 222 //------------------------------------------------------------------------------ |
| 221 // Private methods | 223 // Private methods |
| 222 | 224 |
| 223 void ShillPropertyHandler::ManagerPropertiesCallback( | 225 void ShillPropertyHandler::ManagerPropertiesCallback( |
| 224 DBusMethodCallStatus call_status, | 226 DBusMethodCallStatus call_status, |
| 225 const base::DictionaryValue& properties) { | 227 const base::DictionaryValue& properties) { |
| 226 if (call_status != DBUS_METHOD_CALL_SUCCESS) { | 228 if (call_status != DBUS_METHOD_CALL_SUCCESS) { |
| 227 LOG(ERROR) << "Failed to get Manager properties:" << call_status; | 229 NET_LOG_ERROR("ManagerPropertiesCallback", |
| 230 base::StringPrintf("Failed: %d", call_status)); |
| 228 return; | 231 return; |
| 229 } | 232 } |
| 233 NET_LOG_EVENT("ManagerPropertiesCallback", "Success"); |
| 230 bool notify = false; | 234 bool notify = false; |
| 231 bool update_service_list = false; | 235 bool update_service_list = false; |
| 232 for (base::DictionaryValue::Iterator iter(properties); | 236 for (base::DictionaryValue::Iterator iter(properties); |
| 233 !iter.IsAtEnd(); iter.Advance()) { | 237 !iter.IsAtEnd(); iter.Advance()) { |
| 234 // Defer updating Services until all other properties have been updated. | 238 // Defer updating Services until all other properties have been updated. |
| 235 if (iter.key() == flimflam::kServicesProperty) | 239 if (iter.key() == flimflam::kServicesProperty) |
| 236 update_service_list = true; | 240 update_service_list = true; |
| 237 else | 241 else |
| 238 notify |= ManagerPropertyChanged(iter.key(), iter.value()); | 242 notify |= ManagerPropertyChanged(iter.key(), iter.value()); |
| 239 } | 243 } |
| 240 // Now update the service list which can safely assume other properties have | 244 // Now update the service list which can safely assume other properties have |
| 241 // been initially set. | 245 // been initially set. |
| 242 if (update_service_list) { | 246 if (update_service_list) { |
| 243 const base::Value* value = NULL; | 247 const base::Value* value = NULL; |
| 244 if (properties.GetWithoutPathExpansion(flimflam::kServicesProperty, &value)) | 248 if (properties.GetWithoutPathExpansion(flimflam::kServicesProperty, &value)) |
| 245 notify |= ManagerPropertyChanged(flimflam::kServicesProperty, *value); | 249 notify |= ManagerPropertyChanged(flimflam::kServicesProperty, *value); |
| 246 } | 250 } |
| 247 if (notify) | 251 if (notify) |
| 248 listener_->ManagerPropertyChanged(); | 252 listener_->NotifyManagerPropertyChanged(); |
| 249 // If there are no pending updates, signal the state list changed callbacks. | 253 // If there are no pending updates, signal the state list changed callbacks. |
| 250 if (pending_updates_[ManagedState::MANAGED_TYPE_NETWORK].size() == 0) | 254 if (pending_updates_[ManagedState::MANAGED_TYPE_NETWORK].size() == 0) |
| 251 listener_->ManagedStateListChanged(ManagedState::MANAGED_TYPE_NETWORK); | 255 listener_->ManagedStateListChanged(ManagedState::MANAGED_TYPE_NETWORK); |
| 252 if (pending_updates_[ManagedState::MANAGED_TYPE_DEVICE].size() == 0) | 256 if (pending_updates_[ManagedState::MANAGED_TYPE_DEVICE].size() == 0) |
| 253 listener_->ManagedStateListChanged(ManagedState::MANAGED_TYPE_DEVICE); | 257 listener_->ManagedStateListChanged(ManagedState::MANAGED_TYPE_DEVICE); |
| 254 } | 258 } |
| 255 | 259 |
| 256 bool ShillPropertyHandler::ManagerPropertyChanged(const std::string& key, | 260 bool ShillPropertyHandler::ManagerPropertyChanged(const std::string& key, |
| 257 const base::Value& value) { | 261 const base::Value& value) { |
| 258 bool notify_manager_changed = false; | 262 bool notify_manager_changed = false; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 ShillPropertyObserverMap::iterator iter2 = observer_map.find(path); | 315 ShillPropertyObserverMap::iterator iter2 = observer_map.find(path); |
| 312 if (iter2 != observer_map.end()) { | 316 if (iter2 != observer_map.end()) { |
| 313 new_observed[path] = iter2->second; | 317 new_observed[path] = iter2->second; |
| 314 } else { | 318 } else { |
| 315 // Request an update. | 319 // Request an update. |
| 316 RequestProperties(type, path); | 320 RequestProperties(type, path); |
| 317 // Create an observer for future updates. | 321 // Create an observer for future updates. |
| 318 new_observed[path] = new ShillPropertyObserver( | 322 new_observed[path] = new ShillPropertyObserver( |
| 319 type, path, base::Bind( | 323 type, path, base::Bind( |
| 320 &ShillPropertyHandler::PropertyChangedCallback, AsWeakPtr())); | 324 &ShillPropertyHandler::PropertyChangedCallback, AsWeakPtr())); |
| 321 network_event_log::AddEntry(kLogModule, "StartObserving", path); | 325 NET_LOG_DEBUG("StartObserving", path); |
| 322 } | 326 } |
| 323 observer_map.erase(path); | 327 observer_map.erase(path); |
| 324 // Limit the number of observed services. | 328 // Limit the number of observed services. |
| 325 if (new_observed.size() >= kMaxObserved) | 329 if (new_observed.size() >= kMaxObserved) |
| 326 break; | 330 break; |
| 327 } | 331 } |
| 328 // Delete network service observers still in observer_map. | 332 // Delete network service observers still in observer_map. |
| 329 for (ShillPropertyObserverMap::iterator iter = observer_map.begin(); | 333 for (ShillPropertyObserverMap::iterator iter = observer_map.begin(); |
| 330 iter != observer_map.end(); ++iter) { | 334 iter != observer_map.end(); ++iter) { |
| 331 network_event_log::AddEntry(kLogModule, "StopObserving", iter->first); | 335 NET_LOG_DEBUG("StopObserving", iter->first); |
| 332 delete iter->second; | 336 delete iter->second; |
| 333 } | 337 } |
| 334 observer_map.swap(new_observed); | 338 observer_map.swap(new_observed); |
| 335 } | 339 } |
| 336 | 340 |
| 337 void ShillPropertyHandler::UpdateAvailableTechnologies( | 341 void ShillPropertyHandler::UpdateAvailableTechnologies( |
| 338 const base::ListValue& technologies) { | 342 const base::ListValue& technologies) { |
| 339 available_technologies_.clear(); | 343 available_technologies_.clear(); |
| 340 network_event_log::AddEntry( | 344 NET_LOG_EVENT("AvailableTechnologiesChanged", |
| 341 kLogModule, "AvailableTechnologiesChanged", | 345 base::StringPrintf("Size: %"PRIuS, technologies.GetSize())); |
| 342 base::StringPrintf("Size: %"PRIuS, technologies.GetSize())); | |
| 343 for (base::ListValue::const_iterator iter = technologies.begin(); | 346 for (base::ListValue::const_iterator iter = technologies.begin(); |
| 344 iter != technologies.end(); ++iter) { | 347 iter != technologies.end(); ++iter) { |
| 345 std::string technology; | 348 std::string technology; |
| 346 (*iter)->GetAsString(&technology); | 349 (*iter)->GetAsString(&technology); |
| 347 DCHECK(!technology.empty()); | 350 DCHECK(!technology.empty()); |
| 348 available_technologies_.insert(technology); | 351 available_technologies_.insert(technology); |
| 349 } | 352 } |
| 350 } | 353 } |
| 351 | 354 |
| 352 void ShillPropertyHandler::UpdateEnabledTechnologies( | 355 void ShillPropertyHandler::UpdateEnabledTechnologies( |
| 353 const base::ListValue& technologies) { | 356 const base::ListValue& technologies) { |
| 354 enabled_technologies_.clear(); | 357 enabled_technologies_.clear(); |
| 355 network_event_log::AddEntry( | 358 NET_LOG_EVENT("EnabledTechnologiesChanged", |
| 356 kLogModule, "EnabledTechnologiesChanged", | 359 base::StringPrintf("Size: %"PRIuS, technologies.GetSize())); |
| 357 base::StringPrintf("Size: %"PRIuS, technologies.GetSize())); | |
| 358 for (base::ListValue::const_iterator iter = technologies.begin(); | 360 for (base::ListValue::const_iterator iter = technologies.begin(); |
| 359 iter != technologies.end(); ++iter) { | 361 iter != technologies.end(); ++iter) { |
| 360 std::string technology; | 362 std::string technology; |
| 361 (*iter)->GetAsString(&technology); | 363 (*iter)->GetAsString(&technology); |
| 362 DCHECK(!technology.empty()); | 364 DCHECK(!technology.empty()); |
| 363 enabled_technologies_.insert(technology); | 365 enabled_technologies_.insert(technology); |
| 364 enabling_technologies_.erase(technology); | 366 enabling_technologies_.erase(technology); |
| 365 } | 367 } |
| 366 } | 368 } |
| 367 | 369 |
| 368 void ShillPropertyHandler::UpdateUninitializedTechnologies( | 370 void ShillPropertyHandler::UpdateUninitializedTechnologies( |
| 369 const base::ListValue& technologies) { | 371 const base::ListValue& technologies) { |
| 370 uninitialized_technologies_.clear(); | 372 uninitialized_technologies_.clear(); |
| 371 network_event_log::AddEntry( | 373 NET_LOG_EVENT("UninitializedTechnologiesChanged", |
| 372 kLogModule, "UninitializedTechnologiesChanged", | 374 base::StringPrintf("Size: %"PRIuS, technologies.GetSize())); |
| 373 base::StringPrintf("Size: %"PRIuS, technologies.GetSize())); | |
| 374 for (base::ListValue::const_iterator iter = technologies.begin(); | 375 for (base::ListValue::const_iterator iter = technologies.begin(); |
| 375 iter != technologies.end(); ++iter) { | 376 iter != technologies.end(); ++iter) { |
| 376 std::string technology; | 377 std::string technology; |
| 377 (*iter)->GetAsString(&technology); | 378 (*iter)->GetAsString(&technology); |
| 378 DCHECK(!technology.empty()); | 379 DCHECK(!technology.empty()); |
| 379 uninitialized_technologies_.insert(technology); | 380 uninitialized_technologies_.insert(technology); |
| 380 } | 381 } |
| 381 } | 382 } |
| 382 | 383 |
| 383 void ShillPropertyHandler::EnableTechnologyFailed( | 384 void ShillPropertyHandler::EnableTechnologyFailed( |
| 384 const std::string& technology, | 385 const std::string& technology, |
| 385 const network_handler::ErrorCallback& error_callback, | 386 const network_handler::ErrorCallback& error_callback, |
| 386 const std::string& error_name, | 387 const std::string& error_name, |
| 387 const std::string& error_message) { | 388 const std::string& error_message) { |
| 388 enabling_technologies_.erase(technology); | 389 enabling_technologies_.erase(technology); |
| 389 network_handler::ShillErrorCallbackFunction( | 390 network_handler::ShillErrorCallbackFunction( |
| 390 kLogModule, technology, error_callback, error_name, error_message); | 391 technology, error_callback, error_name, error_message); |
| 391 } | 392 } |
| 392 | 393 |
| 393 void ShillPropertyHandler::GetPropertiesCallback( | 394 void ShillPropertyHandler::GetPropertiesCallback( |
| 394 ManagedState::ManagedType type, | 395 ManagedState::ManagedType type, |
| 395 const std::string& path, | 396 const std::string& path, |
| 396 DBusMethodCallStatus call_status, | 397 DBusMethodCallStatus call_status, |
| 397 const base::DictionaryValue& properties) { | 398 const base::DictionaryValue& properties) { |
| 398 VLOG(2) << "GetPropertiesCallback: " << type << " : " << path; | 399 VLOG(2) << "GetPropertiesCallback: " << type << " : " << path; |
| 399 pending_updates_[type].erase(path); | 400 pending_updates_[type].erase(path); |
| 400 if (call_status != DBUS_METHOD_CALL_SUCCESS) { | 401 if (call_status != DBUS_METHOD_CALL_SUCCESS) { |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 486 | 487 |
| 487 void ShillPropertyHandler::NetworkDevicePropertyChangedCallback( | 488 void ShillPropertyHandler::NetworkDevicePropertyChangedCallback( |
| 488 const std::string& path, | 489 const std::string& path, |
| 489 const std::string& key, | 490 const std::string& key, |
| 490 const base::Value& value) { | 491 const base::Value& value) { |
| 491 listener_->UpdateDeviceProperty(path, key, value); | 492 listener_->UpdateDeviceProperty(path, key, value); |
| 492 } | 493 } |
| 493 | 494 |
| 494 } // namespace internal | 495 } // namespace internal |
| 495 } // namespace chromeos | 496 } // namespace chromeos |
| OLD | NEW |