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