diff options
-rw-r--r-- | chromeos/chromeos.gyp | 14 | ||||
-rw-r--r-- | chromeos/dbus/fake_shill_manager_client.cc | 628 | ||||
-rw-r--r-- | chromeos/dbus/fake_shill_manager_client.h | 93 | ||||
-rw-r--r-- | chromeos/dbus/fake_shill_profile_client.cc (renamed from chromeos/dbus/shill_profile_client_stub.cc) | 34 | ||||
-rw-r--r-- | chromeos/dbus/fake_shill_profile_client.h (renamed from chromeos/dbus/shill_profile_client_stub.h) | 20 | ||||
-rw-r--r-- | chromeos/dbus/fake_shill_service_client.cc (renamed from chromeos/dbus/shill_service_client_stub.cc) | 80 | ||||
-rw-r--r-- | chromeos/dbus/fake_shill_service_client.h (renamed from chromeos/dbus/shill_service_client_stub.h) | 25 | ||||
-rw-r--r-- | chromeos/dbus/shill_manager_client.cc | 4 | ||||
-rw-r--r-- | chromeos/dbus/shill_manager_client_stub.cc | 636 | ||||
-rw-r--r-- | chromeos/dbus/shill_manager_client_stub.h | 132 | ||||
-rw-r--r-- | chromeos/dbus/shill_profile_client.cc | 4 | ||||
-rw-r--r-- | chromeos/dbus/shill_service_client.cc | 4 | ||||
-rw-r--r-- | chromeos/dbus/shill_stub_helper.cc | 1 | ||||
-rw-r--r-- | chromeos/dbus/shill_stub_helper.h | 2 |
14 files changed, 749 insertions, 928 deletions
diff --git a/chromeos/chromeos.gyp b/chromeos/chromeos.gyp index a54532e..03b6cf4 100644 --- a/chromeos/chromeos.gyp +++ b/chromeos/chromeos.gyp @@ -119,6 +119,12 @@ 'dbus/fake_shill_device_client.h', 'dbus/fake_shill_ipconfig_client.cc', 'dbus/fake_shill_ipconfig_client.h', + 'dbus/fake_shill_manager_client.cc', + 'dbus/fake_shill_manager_client.h', + 'dbus/fake_shill_profile_client.cc', + 'dbus/fake_shill_profile_client.h', + 'dbus/fake_shill_service_client.cc', + 'dbus/fake_shill_service_client.h', 'dbus/fake_system_clock_client.cc', 'dbus/fake_system_clock_client.h', 'dbus/gsm_sms_client.cc', @@ -131,16 +137,10 @@ 'dbus/shill_ipconfig_client.h', 'dbus/shill_manager_client.cc', 'dbus/shill_manager_client.h', - 'dbus/shill_manager_client_stub.cc', - 'dbus/shill_manager_client_stub.h', 'dbus/shill_profile_client.cc', 'dbus/shill_profile_client.h', - 'dbus/shill_profile_client_stub.cc', - 'dbus/shill_profile_client_stub.h', 'dbus/shill_service_client.cc', 'dbus/shill_service_client.h', - 'dbus/shill_service_client_stub.cc', - 'dbus/shill_service_client_stub.h', 'dbus/shill_stub_helper.cc', 'dbus/shill_stub_helper.h', 'dbus/ibus/ibus_client.cc', @@ -377,8 +377,6 @@ 'dbus/mock_shill_profile_client.h', 'dbus/mock_shill_service_client.cc', 'dbus/mock_shill_service_client.h', - 'dbus/shill_profile_client_stub.cc', - 'dbus/shill_profile_client_stub.h', 'disks/mock_disk_mount_manager.cc', 'disks/mock_disk_mount_manager.h', 'ime/mock_component_extension_ime_manager_delegate.cc', diff --git a/chromeos/dbus/fake_shill_manager_client.cc b/chromeos/dbus/fake_shill_manager_client.cc index faab3d5..95486b5 100644 --- a/chromeos/dbus/fake_shill_manager_client.cc +++ b/chromeos/dbus/fake_shill_manager_client.cc @@ -1,104 +1,636 @@ -// Copyright (c) 2013 The Chromium Authors. All rights reserved. +// Copyright 2013 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "chromeos/dbus/fake_shill_manager_client.h" +#include "base/bind.h" +#include "base/command_line.h" +#include "base/message_loop/message_loop.h" +#include "base/values.h" +#include "chromeos/chromeos_switches.h" +#include "chromeos/dbus/dbus_thread_manager.h" +#include "chromeos/dbus/shill_device_client.h" +#include "chromeos/dbus/shill_profile_client.h" +#include "chromeos/dbus/shill_property_changed_observer.h" +#include "chromeos/dbus/shill_service_client.h" +#include "dbus/bus.h" +#include "dbus/message.h" +#include "dbus/object_path.h" +#include "dbus/values_util.h" +#include "third_party/cros_system_api/dbus/service_constants.h" + namespace chromeos { -FakeShillManagerClient::FakeShillManagerClient() { -} +namespace { -FakeShillManagerClient::~FakeShillManagerClient() { -} +// Used to compare values for finding entries to erase in a ListValue. +// (ListValue only implements a const_iterator version of Find). +struct ValueEquals { + explicit ValueEquals(const base::Value* first) : first_(first) {} + bool operator()(const base::Value* second) const { + return first_->Equals(second); + } + const base::Value* first_; +}; -void FakeShillManagerClient::Init(dbus::Bus* bus) { +// Appends string entries from |service_list_in| whose entries in ServiceClient +// have Type |match_type| to either an active list or an inactive list +// based on the entry's State. +void AppendServicesForType( + const base::ListValue* service_list_in, + const char* match_type, + std::vector<std::string>* active_service_list_out, + std::vector<std::string>* inactive_service_list_out) { + ShillServiceClient::TestInterface* service_client = + DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface(); + for (base::ListValue::const_iterator iter = service_list_in->begin(); + iter != service_list_in->end(); ++iter) { + std::string service_path; + if (!(*iter)->GetAsString(&service_path)) + continue; + const base::DictionaryValue* properties = + service_client->GetServiceProperties(service_path); + if (!properties) { + LOG(ERROR) << "Properties not found for service: " << service_path; + continue; + } + std::string type; + properties->GetString(shill::kTypeProperty, &type); + if (type != match_type) + continue; + std::string state; + properties->GetString(shill::kStateProperty, &state); + if (state == shill::kStateOnline || + state == shill::kStateAssociation || + state == shill::kStateConfiguration || + state == shill::kStatePortal || + state == shill::kStateReady) { + active_service_list_out->push_back(service_path); + } else { + inactive_service_list_out->push_back(service_path); + } + } } -void FakeShillManagerClient::RequestScan(const std::string& type, - const base::Closure& callback, - const ErrorCallback& error_callback) { -} +} // namespace -ShillManagerClient::TestInterface* FakeShillManagerClient::GetTestInterface() { - return NULL; +FakeShillManagerClient::FakeShillManagerClient() + : weak_ptr_factory_(this) { } -void FakeShillManagerClient::GetNetworksForGeolocation( - const DictionaryValueCallback& callback) { -} +FakeShillManagerClient::~FakeShillManagerClient() {} -void FakeShillManagerClient::ConnectToBestServices( - const base::Closure& callback, - const ErrorCallback& error_callback) { -} +// ShillManagerClient overrides. -void FakeShillManagerClient::RemovePropertyChangedObserver( - ShillPropertyChangedObserver* observer) { -} +void FakeShillManagerClient::Init(dbus::Bus* bus) {} -void FakeShillManagerClient::VerifyAndEncryptData( - const VerificationProperties& properties, - const std::string& data, - const StringCallback& callback, - const ErrorCallback& error_callback) { +void FakeShillManagerClient::AddPropertyChangedObserver( + ShillPropertyChangedObserver* observer) { + observer_list_.AddObserver(observer); } -void FakeShillManagerClient::GetService(const base::DictionaryValue& properties, - const ObjectPathCallback& callback, - const ErrorCallback& error_callback) { +void FakeShillManagerClient::RemovePropertyChangedObserver( + ShillPropertyChangedObserver* observer) { + observer_list_.RemoveObserver(observer); } -void FakeShillManagerClient::AddPropertyChangedObserver( - ShillPropertyChangedObserver* observer) { +void FakeShillManagerClient::GetProperties( + const DictionaryValueCallback& callback) { + base::MessageLoop::current()->PostTask( + FROM_HERE, base::Bind( + &FakeShillManagerClient::PassStubProperties, + weak_ptr_factory_.GetWeakPtr(), + callback)); } -void FakeShillManagerClient::DisableTechnology( - const std::string& type, - const base::Closure& callback, - const ErrorCallback& error_callback) { +void FakeShillManagerClient::GetNetworksForGeolocation( + const DictionaryValueCallback& callback) { + base::MessageLoop::current()->PostTask( + FROM_HERE, base::Bind( + &FakeShillManagerClient::PassStubGeoNetworks, + weak_ptr_factory_.GetWeakPtr(), + callback)); } void FakeShillManagerClient::SetProperty(const std::string& name, const base::Value& value, const base::Closure& callback, const ErrorCallback& error_callback) { + stub_properties_.SetWithoutPathExpansion(name, value.DeepCopy()); + CallNotifyObserversPropertyChanged(name, 0); + base::MessageLoop::current()->PostTask(FROM_HERE, callback); } -void FakeShillManagerClient::GetProperties( - const DictionaryValueCallback& callback) { +void FakeShillManagerClient::RequestScan(const std::string& type, + const base::Closure& callback, + const ErrorCallback& error_callback) { + // For Stub purposes, default to a Wifi scan. + std::string device_type = shill::kTypeWifi; + if (!type.empty()) + device_type = type; + ShillDeviceClient::TestInterface* device_client = + DBusThreadManager::Get()->GetShillDeviceClient()->GetTestInterface(); + std::string device_path = device_client->GetDevicePathForType(device_type); + if (!device_path.empty()) { + device_client->SetDeviceProperty(device_path, + shill::kScanningProperty, + base::FundamentalValue(true)); + } + const int kScanDurationSeconds = 3; + int scan_duration_seconds = kScanDurationSeconds; + if (!CommandLine::ForCurrentProcess()->HasSwitch( + chromeos::switches::kEnableStubInteractive)) { + scan_duration_seconds = 0; + } + base::MessageLoop::current()->PostDelayedTask( + FROM_HERE, + base::Bind(&FakeShillManagerClient::ScanCompleted, + weak_ptr_factory_.GetWeakPtr(), device_path, callback), + base::TimeDelta::FromSeconds(scan_duration_seconds)); } -void FakeShillManagerClient::VerifyAndEncryptCredentials( - const VerificationProperties& properties, - const std::string& service_path, - const StringCallback& callback, +void FakeShillManagerClient::EnableTechnology( + const std::string& type, + const base::Closure& callback, const ErrorCallback& error_callback) { + base::ListValue* enabled_list = NULL; + if (!stub_properties_.GetListWithoutPathExpansion( + shill::kEnabledTechnologiesProperty, &enabled_list)) { + base::MessageLoop::current()->PostTask(FROM_HERE, callback); + base::MessageLoop::current()->PostTask( + FROM_HERE, + base::Bind(error_callback, "StubError", "Property not found")); + return; + } + if (CommandLine::ForCurrentProcess()->HasSwitch( + chromeos::switches::kEnableStubInteractive)) { + const int kEnableTechnologyDelaySeconds = 3; + base::MessageLoop::current()->PostDelayedTask( + FROM_HERE, + base::Bind(&FakeShillManagerClient::SetTechnologyEnabled, + weak_ptr_factory_.GetWeakPtr(), type, callback, true), + base::TimeDelta::FromSeconds(kEnableTechnologyDelaySeconds)); + } else { + SetTechnologyEnabled(type, callback, true); + } } -void FakeShillManagerClient::EnableTechnology( +void FakeShillManagerClient::DisableTechnology( const std::string& type, const base::Closure& callback, const ErrorCallback& error_callback) { + base::ListValue* enabled_list = NULL; + if (!stub_properties_.GetListWithoutPathExpansion( + shill::kEnabledTechnologiesProperty, &enabled_list)) { + base::MessageLoop::current()->PostTask( + FROM_HERE, + base::Bind(error_callback, "StubError", "Property not found")); + return; + } + if (CommandLine::ForCurrentProcess()->HasSwitch( + chromeos::switches::kEnableStubInteractive)) { + const int kDisableTechnologyDelaySeconds = 3; + base::MessageLoop::current()->PostDelayedTask( + FROM_HERE, + base::Bind(&FakeShillManagerClient::SetTechnologyEnabled, + weak_ptr_factory_.GetWeakPtr(), type, callback, false), + base::TimeDelta::FromSeconds(kDisableTechnologyDelaySeconds)); + } else { + SetTechnologyEnabled(type, callback, false); + } } void FakeShillManagerClient::ConfigureService( const base::DictionaryValue& properties, const ObjectPathCallback& callback, const ErrorCallback& error_callback) { + ShillServiceClient::TestInterface* service_client = + DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface(); + + std::string guid; + std::string type; + if (!properties.GetString(shill::kGuidProperty, &guid) || + !properties.GetString(shill::kTypeProperty, &type)) { + LOG(ERROR) << "ConfigureService requies GUID and Type to be defined"; + // If the properties aren't filled out completely, then just return an empty + // object path. + base::MessageLoop::current()->PostTask( + FROM_HERE, base::Bind(callback, dbus::ObjectPath())); + return; + } + + // For the purposes of this stub, we're going to assume that the GUID property + // is set to the service path because we don't want to re-implement Shill's + // property matching magic here. + std::string service_path = guid; + + std::string ipconfig_path; + properties.GetString(shill::kIPConfigProperty, &ipconfig_path); + + // Merge the new properties with existing properties, if any. + const base::DictionaryValue* existing_properties = + service_client->GetServiceProperties(service_path); + if (!existing_properties) { + // Add a new service to the service client stub because none exists, yet. + // This calls AddManagerService. + service_client->AddServiceWithIPConfig(service_path, guid, type, + shill::kStateIdle, ipconfig_path, + true /* visible */, + true /* watch */); + existing_properties = service_client->GetServiceProperties(service_path); + } + + scoped_ptr<base::DictionaryValue> merged_properties( + existing_properties->DeepCopy()); + merged_properties->MergeDictionary(&properties); + + // Now set all the properties. + for (base::DictionaryValue::Iterator iter(*merged_properties); + !iter.IsAtEnd(); iter.Advance()) { + service_client->SetServiceProperty(service_path, iter.key(), iter.value()); + } + + // If the Profile property is set, add it to ProfileClient. + std::string profile_path; + merged_properties->GetStringWithoutPathExpansion(shill::kProfileProperty, + &profile_path); + if (!profile_path.empty()) { + DBusThreadManager::Get()->GetShillProfileClient()->GetTestInterface()-> + AddService(profile_path, service_path); + } + + base::MessageLoop::current()->PostTask( + FROM_HERE, base::Bind(callback, dbus::ObjectPath(service_path))); +} + +void FakeShillManagerClient::ConfigureServiceForProfile( + const dbus::ObjectPath& profile_path, + const base::DictionaryValue& properties, + const ObjectPathCallback& callback, + const ErrorCallback& error_callback) { + std::string profile_property; + properties.GetStringWithoutPathExpansion(shill::kProfileProperty, + &profile_property); + CHECK(profile_property == profile_path.value()); + ConfigureService(properties, callback, error_callback); +} + + +void FakeShillManagerClient::GetService( + const base::DictionaryValue& properties, + const ObjectPathCallback& callback, + const ErrorCallback& error_callback) { + base::MessageLoop::current()->PostTask( + FROM_HERE, base::Bind(callback, dbus::ObjectPath())); } void FakeShillManagerClient::VerifyDestination( const VerificationProperties& properties, const BooleanCallback& callback, const ErrorCallback& error_callback) { + base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback, true)); } -void FakeShillManagerClient::ConfigureServiceForProfile( - const dbus::ObjectPath& profile_path, - const base::DictionaryValue& properties, - const ObjectPathCallback& callback, +void FakeShillManagerClient::VerifyAndEncryptCredentials( + const VerificationProperties& properties, + const std::string& service_path, + const StringCallback& callback, + const ErrorCallback& error_callback) { + base::MessageLoop::current()->PostTask( + FROM_HERE, base::Bind(callback, "encrypted_credentials")); +} + +void FakeShillManagerClient::VerifyAndEncryptData( + const VerificationProperties& properties, + const std::string& data, + const StringCallback& callback, + const ErrorCallback& error_callback) { + base::MessageLoop::current()->PostTask(FROM_HERE, + base::Bind(callback, "encrypted_data")); +} + +void FakeShillManagerClient::ConnectToBestServices( + const base::Closure& callback, const ErrorCallback& error_callback) { } -} // namespace chromeos +ShillManagerClient::TestInterface* FakeShillManagerClient::GetTestInterface() { + return this; +} + +// ShillManagerClient::TestInterface overrides. + +void FakeShillManagerClient::AddDevice(const std::string& device_path) { + if (GetListProperty(shill::kDevicesProperty)->AppendIfNotPresent( + base::Value::CreateStringValue(device_path))) { + CallNotifyObserversPropertyChanged(shill::kDevicesProperty, 0); + } +} + +void FakeShillManagerClient::RemoveDevice(const std::string& device_path) { + base::StringValue device_path_value(device_path); + if (GetListProperty(shill::kDevicesProperty)->Remove( + device_path_value, NULL)) { + CallNotifyObserversPropertyChanged(shill::kDevicesProperty, 0); + } +} + +void FakeShillManagerClient::ClearDevices() { + GetListProperty(shill::kDevicesProperty)->Clear(); + CallNotifyObserversPropertyChanged(shill::kDevicesProperty, 0); +} + +void FakeShillManagerClient::AddTechnology(const std::string& type, + bool enabled) { + if (GetListProperty(shill::kAvailableTechnologiesProperty)-> + AppendIfNotPresent(base::Value::CreateStringValue(type))) { + CallNotifyObserversPropertyChanged( + shill::kAvailableTechnologiesProperty, 0); + } + if (enabled && + GetListProperty(shill::kEnabledTechnologiesProperty)-> + AppendIfNotPresent(base::Value::CreateStringValue(type))) { + CallNotifyObserversPropertyChanged( + shill::kEnabledTechnologiesProperty, 0); + } +} + +void FakeShillManagerClient::RemoveTechnology(const std::string& type) { + base::StringValue type_value(type); + if (GetListProperty(shill::kAvailableTechnologiesProperty)->Remove( + type_value, NULL)) { + CallNotifyObserversPropertyChanged( + shill::kAvailableTechnologiesProperty, 0); + } + if (GetListProperty(shill::kEnabledTechnologiesProperty)->Remove( + type_value, NULL)) { + CallNotifyObserversPropertyChanged( + shill::kEnabledTechnologiesProperty, 0); + } +} + +void FakeShillManagerClient::SetTechnologyInitializing(const std::string& type, + bool initializing) { + if (initializing) { + if (GetListProperty(shill::kUninitializedTechnologiesProperty)-> + AppendIfNotPresent(base::Value::CreateStringValue(type))) { + CallNotifyObserversPropertyChanged( + shill::kUninitializedTechnologiesProperty, 0); + } + } else { + if (GetListProperty(shill::kUninitializedTechnologiesProperty)->Remove( + base::StringValue(type), NULL)) { + CallNotifyObserversPropertyChanged( + shill::kUninitializedTechnologiesProperty, 0); + } + } +} + +void FakeShillManagerClient::ClearProperties() { + stub_properties_.Clear(); +} + +void FakeShillManagerClient::AddManagerService(const std::string& service_path, + bool add_to_visible_list, + bool add_to_watch_list) { + // Always add to ServiceCompleteListProperty. + GetListProperty(shill::kServiceCompleteListProperty)->AppendIfNotPresent( + base::Value::CreateStringValue(service_path)); + // If visible, add to Services and notify if new. + if (add_to_visible_list && + GetListProperty(shill::kServicesProperty)->AppendIfNotPresent( + base::Value::CreateStringValue(service_path))) { + CallNotifyObserversPropertyChanged(shill::kServicesProperty, 0); + } + if (add_to_watch_list) + AddServiceToWatchList(service_path); +} + +void FakeShillManagerClient::RemoveManagerService( + const std::string& service_path) { + base::StringValue service_path_value(service_path); + if (GetListProperty(shill::kServicesProperty)->Remove( + service_path_value, NULL)) { + CallNotifyObserversPropertyChanged(shill::kServicesProperty, 0); + } + GetListProperty(shill::kServiceCompleteListProperty)->Remove( + service_path_value, NULL); + if (GetListProperty(shill::kServiceWatchListProperty)->Remove( + service_path_value, NULL)) { + CallNotifyObserversPropertyChanged( + shill::kServiceWatchListProperty, 0); + } +} + +void FakeShillManagerClient::ClearManagerServices() { + GetListProperty(shill::kServicesProperty)->Clear(); + GetListProperty(shill::kServiceCompleteListProperty)->Clear(); + GetListProperty(shill::kServiceWatchListProperty)->Clear(); + CallNotifyObserversPropertyChanged(shill::kServicesProperty, 0); + CallNotifyObserversPropertyChanged(shill::kServiceWatchListProperty, 0); +} + +void FakeShillManagerClient::SortManagerServices() { + static const char* ordered_types[] = { + shill::kTypeEthernet, + shill::kTypeWifi, + shill::kTypeCellular, + shill::kTypeWimax, + shill::kTypeVPN + }; + base::ListValue* service_list = GetListProperty(shill::kServicesProperty); + if (!service_list || service_list->empty()) + return; + std::vector<std::string> active_services; + std::vector<std::string> inactive_services; + for (size_t i = 0; i < arraysize(ordered_types); ++i) { + AppendServicesForType(service_list, ordered_types[i], + &active_services, &inactive_services); + } + service_list->Clear(); + for (size_t i = 0; i < active_services.size(); ++i) + service_list->AppendString(active_services[i]); + for (size_t i = 0; i < inactive_services.size(); ++i) + service_list->AppendString(inactive_services[i]); + + CallNotifyObserversPropertyChanged(shill::kServicesProperty, 0); +} + +void FakeShillManagerClient::AddGeoNetwork( + const std::string& technology, + const base::DictionaryValue& network) { + base::ListValue* list_value = NULL; + if (!stub_geo_networks_.GetListWithoutPathExpansion( + technology, &list_value)) { + list_value = new base::ListValue; + stub_geo_networks_.SetWithoutPathExpansion(technology, list_value); + } + list_value->Append(network.DeepCopy()); +} + +void FakeShillManagerClient::AddProfile(const std::string& profile_path) { + const char* key = shill::kProfilesProperty; + if (GetListProperty(key)->AppendIfNotPresent( + new base::StringValue(profile_path))) { + CallNotifyObserversPropertyChanged(key, 0); + } +} + +void FakeShillManagerClient::AddServiceToWatchList( + const std::string& service_path) { + // Remove and insert the service, moving it to the front of the watch list. + GetListProperty(shill::kServiceWatchListProperty)->Remove( + base::StringValue(service_path), NULL); + GetListProperty(shill::kServiceWatchListProperty)->Insert( + 0, base::Value::CreateStringValue(service_path)); + CallNotifyObserversPropertyChanged( + shill::kServiceWatchListProperty, 0); +} + +void FakeShillManagerClient::PassStubProperties( + const DictionaryValueCallback& callback) const { + scoped_ptr<base::DictionaryValue> stub_properties( + stub_properties_.DeepCopy()); + // Remove disabled services from the list. + stub_properties->SetWithoutPathExpansion( + shill::kServicesProperty, + GetEnabledServiceList(shill::kServicesProperty)); + stub_properties->SetWithoutPathExpansion( + shill::kServiceWatchListProperty, + GetEnabledServiceList(shill::kServiceWatchListProperty)); + callback.Run(DBUS_METHOD_CALL_SUCCESS, *stub_properties); +} + +void FakeShillManagerClient::PassStubGeoNetworks( + const DictionaryValueCallback& callback) const { + callback.Run(DBUS_METHOD_CALL_SUCCESS, stub_geo_networks_); +} + +void FakeShillManagerClient::CallNotifyObserversPropertyChanged( + const std::string& property, + int delay_ms) { + // Avoid unnecessary delayed task if we have no observers (e.g. during + // initial setup). + if (!observer_list_.might_have_observers()) + return; + if (!CommandLine::ForCurrentProcess()->HasSwitch( + chromeos::switches::kEnableStubInteractive)) { + delay_ms = 0; + } + base::MessageLoop::current()->PostDelayedTask( + FROM_HERE, + base::Bind(&FakeShillManagerClient::NotifyObserversPropertyChanged, + weak_ptr_factory_.GetWeakPtr(), + property), + base::TimeDelta::FromMilliseconds(delay_ms)); +} + +void FakeShillManagerClient::NotifyObserversPropertyChanged( + const std::string& property) { + if (property == shill::kServicesProperty || + property == shill::kServiceWatchListProperty) { + scoped_ptr<base::ListValue> services(GetEnabledServiceList(property)); + FOR_EACH_OBSERVER(ShillPropertyChangedObserver, + observer_list_, + OnPropertyChanged(property, *(services.get()))); + return; + } + base::Value* value = NULL; + if (!stub_properties_.GetWithoutPathExpansion(property, &value)) { + LOG(ERROR) << "Notify for unknown property: " << property; + return; + } + FOR_EACH_OBSERVER(ShillPropertyChangedObserver, + observer_list_, + OnPropertyChanged(property, *value)); +} + +base::ListValue* FakeShillManagerClient::GetListProperty( + const std::string& property) { + base::ListValue* list_property = NULL; + if (!stub_properties_.GetListWithoutPathExpansion( + property, &list_property)) { + list_property = new base::ListValue; + stub_properties_.SetWithoutPathExpansion(property, list_property); + } + return list_property; +} + +bool FakeShillManagerClient::TechnologyEnabled(const std::string& type) const { + if (type == shill::kTypeVPN) + return true; // VPN is always "enabled" since there is no associated device + bool enabled = false; + const base::ListValue* technologies; + if (stub_properties_.GetListWithoutPathExpansion( + shill::kEnabledTechnologiesProperty, &technologies)) { + base::StringValue type_value(type); + if (technologies->Find(type_value) != technologies->end()) + enabled = true; + } + return enabled; +} + +void FakeShillManagerClient::SetTechnologyEnabled( + const std::string& type, + const base::Closure& callback, + bool enabled) { + base::ListValue* enabled_list = NULL; + stub_properties_.GetListWithoutPathExpansion( + shill::kEnabledTechnologiesProperty, &enabled_list); + DCHECK(enabled_list); + if (enabled) + enabled_list->AppendIfNotPresent(new base::StringValue(type)); + else + enabled_list->Remove(base::StringValue(type), NULL); + CallNotifyObserversPropertyChanged( + shill::kEnabledTechnologiesProperty, 0 /* already delayed */); + base::MessageLoop::current()->PostTask(FROM_HERE, callback); + // May affect available services + CallNotifyObserversPropertyChanged(shill::kServicesProperty, 0); + CallNotifyObserversPropertyChanged(shill::kServiceWatchListProperty, 0); +} + +base::ListValue* FakeShillManagerClient::GetEnabledServiceList( + const std::string& property) const { + base::ListValue* new_service_list = new base::ListValue; + const base::ListValue* service_list; + if (stub_properties_.GetListWithoutPathExpansion(property, &service_list)) { + ShillServiceClient::TestInterface* service_client = + DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface(); + for (base::ListValue::const_iterator iter = service_list->begin(); + iter != service_list->end(); ++iter) { + std::string service_path; + if (!(*iter)->GetAsString(&service_path)) + continue; + const base::DictionaryValue* properties = + service_client->GetServiceProperties(service_path); + if (!properties) { + LOG(ERROR) << "Properties not found for service: " << service_path; + continue; + } + std::string name; + properties->GetString(shill::kNameProperty, &name); + std::string type; + properties->GetString(shill::kTypeProperty, &type); + if (TechnologyEnabled(type)) + new_service_list->Append((*iter)->DeepCopy()); + } + } + return new_service_list; +} + +void FakeShillManagerClient::ScanCompleted(const std::string& device_path, + const base::Closure& callback) { + if (!device_path.empty()) { + DBusThreadManager::Get()->GetShillDeviceClient()->GetTestInterface()-> + SetDeviceProperty(device_path, + shill::kScanningProperty, + base::FundamentalValue(false)); + } + CallNotifyObserversPropertyChanged(shill::kServicesProperty, 0); + CallNotifyObserversPropertyChanged(shill::kServiceWatchListProperty, 0); + base::MessageLoop::current()->PostTask(FROM_HERE, callback); +} + +} // namespace chromeos diff --git a/chromeos/dbus/fake_shill_manager_client.h b/chromeos/dbus/fake_shill_manager_client.h index 1d1ba1a..65f449e 100644 --- a/chromeos/dbus/fake_shill_manager_client.h +++ b/chromeos/dbus/fake_shill_manager_client.h @@ -1,18 +1,25 @@ -// Copyright (c) 2013 The Chromium Authors. All rights reserved. +// Copyright 2013 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef CHROMEOS_DBUS_FAKE_SHILL_MANAGER_CLIENT_H_ #define CHROMEOS_DBUS_FAKE_SHILL_MANAGER_CLIENT_H_ -#include "base/values.h" +#include <string> + +#include "base/basictypes.h" +#include "base/callback.h" +#include "chromeos/chromeos_export.h" #include "chromeos/dbus/shill_manager_client.h" -#include "chromeos/dbus/shill_property_changed_observer.h" namespace chromeos { -// A fake implementation of ShillManagerClient. This class does nothing. -class FakeShillManagerClient : public ShillManagerClient { +// A fake implementation of ShillManagerClient. This works in close coordination +// with FakeShillServiceClient. FakeShillDeviceClient, and +// FakeShillProfileClient, and is not intended to be used independently. +class CHROMEOS_EXPORT FakeShillManagerClient : + public ShillManagerClient, + public ShillManagerClient::TestInterface { public: FakeShillManagerClient(); virtual ~FakeShillManagerClient(); @@ -33,23 +40,27 @@ class FakeShillManagerClient : public ShillManagerClient { virtual void RequestScan(const std::string& type, const base::Closure& callback, const ErrorCallback& error_callback) OVERRIDE; - virtual void EnableTechnology(const std::string& type, - const base::Closure& callback, - const ErrorCallback& error_callback) OVERRIDE; - virtual void DisableTechnology(const std::string& type, - const base::Closure& callback, - const ErrorCallback& error_callback) OVERRIDE; - virtual void ConfigureService(const base::DictionaryValue& properties, - const ObjectPathCallback& callback, - const ErrorCallback& error_callback) OVERRIDE; + virtual void EnableTechnology( + const std::string& type, + const base::Closure& callback, + const ErrorCallback& error_callback) OVERRIDE; + virtual void DisableTechnology( + const std::string& type, + const base::Closure& callback, + const ErrorCallback& error_callback) OVERRIDE; + virtual void ConfigureService( + const base::DictionaryValue& properties, + const ObjectPathCallback& callback, + const ErrorCallback& error_callback) OVERRIDE; virtual void ConfigureServiceForProfile( const dbus::ObjectPath& profile_path, const base::DictionaryValue& properties, const ObjectPathCallback& callback, const ErrorCallback& error_callback) OVERRIDE; - virtual void GetService(const base::DictionaryValue& properties, - const ObjectPathCallback& callback, - const ErrorCallback& error_callback) OVERRIDE; + virtual void GetService( + const base::DictionaryValue& properties, + const ObjectPathCallback& callback, + const ErrorCallback& error_callback) OVERRIDE; virtual void VerifyDestination(const VerificationProperties& properties, const BooleanCallback& callback, const ErrorCallback& error_callback) OVERRIDE; @@ -66,9 +77,55 @@ class FakeShillManagerClient : public ShillManagerClient { virtual void ConnectToBestServices( const base::Closure& callback, const ErrorCallback& error_callback) OVERRIDE; - virtual TestInterface* GetTestInterface() OVERRIDE; + virtual ShillManagerClient::TestInterface* GetTestInterface() OVERRIDE; + + // ShillManagerClient::TestInterface overrides. + virtual void AddDevice(const std::string& device_path) OVERRIDE; + virtual void RemoveDevice(const std::string& device_path) OVERRIDE; + virtual void ClearDevices() OVERRIDE; + virtual void AddTechnology(const std::string& type, bool enabled) OVERRIDE; + virtual void RemoveTechnology(const std::string& type) OVERRIDE; + virtual void SetTechnologyInitializing(const std::string& type, + bool initializing) OVERRIDE; + virtual void AddGeoNetwork(const std::string& technology, + const base::DictionaryValue& network) OVERRIDE; + virtual void AddProfile(const std::string& profile_path) OVERRIDE; + virtual void ClearProperties() OVERRIDE; + virtual void AddManagerService(const std::string& service_path, + bool add_to_visible_list, + bool add_to_watch_list) OVERRIDE; + virtual void RemoveManagerService(const std::string& service_path) OVERRIDE; + virtual void ClearManagerServices() OVERRIDE; + virtual void SortManagerServices() OVERRIDE; private: + void AddServiceToWatchList(const std::string& service_path); + void SetDefaultProperties(); + void PassStubProperties(const DictionaryValueCallback& callback) const; + void PassStubGeoNetworks(const DictionaryValueCallback& callback) const; + void CallNotifyObserversPropertyChanged(const std::string& property, + int delay_ms); + void NotifyObserversPropertyChanged(const std::string& property); + base::ListValue* GetListProperty(const std::string& property); + bool TechnologyEnabled(const std::string& type) const; + void SetTechnologyEnabled(const std::string& type, + const base::Closure& callback, + bool enabled); + base::ListValue* GetEnabledServiceList(const std::string& property) const; + void ScanCompleted(const std::string& device_path, + const base::Closure& callback); + + // Dictionary of property name -> property value + base::DictionaryValue stub_properties_; + // Dictionary of technology -> list of property dictionaries + base::DictionaryValue stub_geo_networks_; + + ObserverList<ShillPropertyChangedObserver> observer_list_; + + // Note: This should remain the last member so it'll be destroyed and + // invalidate its weak pointers before any other members are destroyed. + base::WeakPtrFactory<FakeShillManagerClient> weak_ptr_factory_; + DISALLOW_COPY_AND_ASSIGN(FakeShillManagerClient); }; diff --git a/chromeos/dbus/shill_profile_client_stub.cc b/chromeos/dbus/fake_shill_profile_client.cc index 05cdbb0..d0ff0f6 100644 --- a/chromeos/dbus/shill_profile_client_stub.cc +++ b/chromeos/dbus/fake_shill_profile_client.cc @@ -1,8 +1,8 @@ -// Copyright (c) 2013 The Chromium Authors. All rights reserved. +// Copyright 2013 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "chromeos/dbus/shill_profile_client_stub.h" +#include "chromeos/dbus/fake_shill_profile_client.h" #include "base/bind.h" #include "base/bind_helpers.h" @@ -21,7 +21,7 @@ namespace chromeos { -struct ShillProfileClientStub::ProfileProperties { +struct FakeShillProfileClient::ProfileProperties { base::DictionaryValue entries; base::DictionaryValue properties; }; @@ -36,27 +36,27 @@ void PassDictionary( } // namespace -ShillProfileClientStub::ShillProfileClientStub() { +FakeShillProfileClient::FakeShillProfileClient() { } -ShillProfileClientStub::~ShillProfileClientStub() { +FakeShillProfileClient::~FakeShillProfileClient() { STLDeleteValues(&profiles_); } -void ShillProfileClientStub::Init(dbus::Bus* bus) { +void FakeShillProfileClient::Init(dbus::Bus* bus) { } -void ShillProfileClientStub::AddPropertyChangedObserver( +void FakeShillProfileClient::AddPropertyChangedObserver( const dbus::ObjectPath& profile_path, ShillPropertyChangedObserver* observer) { } -void ShillProfileClientStub::RemovePropertyChangedObserver( +void FakeShillProfileClient::RemovePropertyChangedObserver( const dbus::ObjectPath& profile_path, ShillPropertyChangedObserver* observer) { } -void ShillProfileClientStub::GetProperties( +void FakeShillProfileClient::GetProperties( const dbus::ObjectPath& profile_path, const DictionaryValueCallbackWithoutStatus& callback, const ErrorCallback& error_callback) { @@ -77,7 +77,7 @@ void ShillProfileClientStub::GetProperties( base::Bind(&PassDictionary, callback, base::Owned(properties.release()))); } -void ShillProfileClientStub::GetEntry( +void FakeShillProfileClient::GetEntry( const dbus::ObjectPath& profile_path, const std::string& entry_path, const DictionaryValueCallbackWithoutStatus& callback, @@ -98,7 +98,7 @@ void ShillProfileClientStub::GetEntry( base::Bind(&PassDictionary, callback, base::Owned(entry->DeepCopy()))); } -void ShillProfileClientStub::DeleteEntry(const dbus::ObjectPath& profile_path, +void FakeShillProfileClient::DeleteEntry(const dbus::ObjectPath& profile_path, const std::string& entry_path, const base::Closure& callback, const ErrorCallback& error_callback) { @@ -120,11 +120,11 @@ void ShillProfileClientStub::DeleteEntry(const dbus::ObjectPath& profile_path, base::MessageLoop::current()->PostTask(FROM_HERE, callback); } -ShillProfileClient::TestInterface* ShillProfileClientStub::GetTestInterface() { +ShillProfileClient::TestInterface* FakeShillProfileClient::GetTestInterface() { return this; } -void ShillProfileClientStub::AddProfile(const std::string& profile_path, +void FakeShillProfileClient::AddProfile(const std::string& profile_path, const std::string& userhash) { if (GetProfile(dbus::ObjectPath(profile_path), ErrorCallback())) return; @@ -137,7 +137,7 @@ void ShillProfileClientStub::AddProfile(const std::string& profile_path, AddProfile(profile_path); } -void ShillProfileClientStub::AddEntry(const std::string& profile_path, +void FakeShillProfileClient::AddEntry(const std::string& profile_path, const std::string& entry_path, const base::DictionaryValue& properties) { ProfileProperties* profile = GetProfile(dbus::ObjectPath(profile_path), @@ -149,7 +149,7 @@ void ShillProfileClientStub::AddEntry(const std::string& profile_path, AddManagerService(entry_path, false /* visible */, false /* watch */); } -bool ShillProfileClientStub::AddService(const std::string& profile_path, +bool FakeShillProfileClient::AddService(const std::string& profile_path, const std::string& service_path) { ProfileProperties* profile = GetProfile(dbus::ObjectPath(profile_path), ErrorCallback()); @@ -184,7 +184,7 @@ bool ShillProfileClientStub::AddService(const std::string& profile_path, return true; } -void ShillProfileClientStub::GetProfilePaths( +void FakeShillProfileClient::GetProfilePaths( std::vector<std::string>* profiles) { for (ProfileMap::iterator iter = profiles_.begin(); iter != profiles_.end(); ++iter) { @@ -192,7 +192,7 @@ void ShillProfileClientStub::GetProfilePaths( } } -ShillProfileClientStub::ProfileProperties* ShillProfileClientStub::GetProfile( +FakeShillProfileClient::ProfileProperties* FakeShillProfileClient::GetProfile( const dbus::ObjectPath& profile_path, const ErrorCallback& error_callback) { ProfileMap::const_iterator found = profiles_.find(profile_path.value()); diff --git a/chromeos/dbus/shill_profile_client_stub.h b/chromeos/dbus/fake_shill_profile_client.h index 9230491..9353f50 100644 --- a/chromeos/dbus/shill_profile_client_stub.h +++ b/chromeos/dbus/fake_shill_profile_client.h @@ -1,25 +1,27 @@ -// Copyright (c) 2013 The Chromium Authors. All rights reserved. +// Copyright 2013 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef CHROMEOS_DBUS_SHILL_PROFILE_CLIENT_STUB_H_ -#define CHROMEOS_DBUS_SHILL_PROFILE_CLIENT_STUB_H_ +#ifndef CHROMEOS_DBUS_FAKE_SHILL_PROFILE_CLIENT_H_ +#define CHROMEOS_DBUS_FAKE_SHILL_PROFILE_CLIENT_H_ #include <map> #include <string> #include "base/basictypes.h" +#include "chromeos/chromeos_export.h" #include "chromeos/dbus/shill_manager_client.h" #include "chromeos/dbus/shill_profile_client.h" namespace chromeos { // A stub implementation of ShillProfileClient. -class ShillProfileClientStub : public ShillProfileClient, - public ShillProfileClient::TestInterface { +class CHROMEOS_EXPORT FakeShillProfileClient : + public ShillProfileClient, + public ShillProfileClient::TestInterface { public: - ShillProfileClientStub(); - virtual ~ShillProfileClientStub(); + FakeShillProfileClient(); + virtual ~FakeShillProfileClient(); // ShillProfileClient overrides virtual void Init(dbus::Bus* bus) OVERRIDE; @@ -64,9 +66,9 @@ class ShillProfileClientStub : public ShillProfileClient, // necessary. ProfileMap profiles_; - DISALLOW_COPY_AND_ASSIGN(ShillProfileClientStub); + DISALLOW_COPY_AND_ASSIGN(FakeShillProfileClient); }; } // namespace chromeos -#endif // CHROMEOS_DBUS_SHILL_PROFILE_CLIENT_STUB_H_ +#endif // CHROMEOS_DBUS_FAKE_SHILL_PROFILE_CLIENT_H_ diff --git a/chromeos/dbus/shill_service_client_stub.cc b/chromeos/dbus/fake_shill_service_client.cc index c03fc48..19ab3d6 100644 --- a/chromeos/dbus/shill_service_client_stub.cc +++ b/chromeos/dbus/fake_shill_service_client.cc @@ -1,8 +1,8 @@ -// Copyright (c) 2013 The Chromium Authors. All rights reserved. +// Copyright 2013 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "chromeos/dbus/shill_service_client_stub.h" +#include "chromeos/dbus/fake_shill_service_client.h" #include "base/bind.h" #include "base/bind_helpers.h" @@ -45,10 +45,10 @@ void PassStubServiceProperties( } // namespace -ShillServiceClientStub::ShillServiceClientStub() : weak_ptr_factory_(this) { +FakeShillServiceClient::FakeShillServiceClient() : weak_ptr_factory_(this) { } -ShillServiceClientStub::~ShillServiceClientStub() { +FakeShillServiceClient::~FakeShillServiceClient() { STLDeleteContainerPairSecondPointers( observer_list_.begin(), observer_list_.end()); } @@ -56,22 +56,22 @@ ShillServiceClientStub::~ShillServiceClientStub() { // ShillServiceClient overrides. -void ShillServiceClientStub::Init(dbus::Bus* bus) { +void FakeShillServiceClient::Init(dbus::Bus* bus) { } -void ShillServiceClientStub::AddPropertyChangedObserver( +void FakeShillServiceClient::AddPropertyChangedObserver( const dbus::ObjectPath& service_path, ShillPropertyChangedObserver* observer) { GetObserverList(service_path).AddObserver(observer); } -void ShillServiceClientStub::RemovePropertyChangedObserver( +void FakeShillServiceClient::RemovePropertyChangedObserver( const dbus::ObjectPath& service_path, ShillPropertyChangedObserver* observer) { GetObserverList(service_path).RemoveObserver(observer); } -void ShillServiceClientStub::GetProperties( +void FakeShillServiceClient::GetProperties( const dbus::ObjectPath& service_path, const DictionaryValueCallback& callback) { base::DictionaryValue* nested_dict = NULL; @@ -99,7 +99,7 @@ void ShillServiceClientStub::GetProperties( base::Owned(result_properties.release()))); } -void ShillServiceClientStub::SetProperty(const dbus::ObjectPath& service_path, +void FakeShillServiceClient::SetProperty(const dbus::ObjectPath& service_path, const std::string& name, const base::Value& value, const base::Closure& callback, @@ -112,7 +112,7 @@ void ShillServiceClientStub::SetProperty(const dbus::ObjectPath& service_path, base::MessageLoop::current()->PostTask(FROM_HERE, callback); } -void ShillServiceClientStub::SetProperties( +void FakeShillServiceClient::SetProperties( const dbus::ObjectPath& service_path, const base::DictionaryValue& properties, const base::Closure& callback, @@ -128,7 +128,7 @@ void ShillServiceClientStub::SetProperties( base::MessageLoop::current()->PostTask(FROM_HERE, callback); } -void ShillServiceClientStub::ClearProperty( +void FakeShillServiceClient::ClearProperty( const dbus::ObjectPath& service_path, const std::string& name, const base::Closure& callback, @@ -144,7 +144,7 @@ void ShillServiceClientStub::ClearProperty( base::MessageLoop::current()->PostTask(FROM_HERE, callback); } -void ShillServiceClientStub::ClearProperties( +void FakeShillServiceClient::ClearProperties( const dbus::ObjectPath& service_path, const std::vector<std::string>& names, const ListValueCallback& callback, @@ -168,10 +168,10 @@ void ShillServiceClientStub::ClearProperties( callback, base::Owned(results.release()))); } -void ShillServiceClientStub::Connect(const dbus::ObjectPath& service_path, +void FakeShillServiceClient::Connect(const dbus::ObjectPath& service_path, const base::Closure& callback, const ErrorCallback& error_callback) { - VLOG(1) << "ShillServiceClientStub::Connect: " << service_path.value(); + VLOG(1) << "FakeShillServiceClient::Connect: " << service_path.value(); base::DictionaryValue* service_properties = NULL; if (!stub_services_.GetDictionary( service_path.value(), &service_properties)) { @@ -200,7 +200,7 @@ void ShillServiceClientStub::Connect(const dbus::ObjectPath& service_path, } base::MessageLoop::current()->PostDelayedTask( FROM_HERE, - base::Bind(&ShillServiceClientStub::ContinueConnect, + base::Bind(&FakeShillServiceClient::ContinueConnect, weak_ptr_factory_.GetWeakPtr(), service_path.value()), delay); @@ -208,7 +208,7 @@ void ShillServiceClientStub::Connect(const dbus::ObjectPath& service_path, callback.Run(); } -void ShillServiceClientStub::Disconnect(const dbus::ObjectPath& service_path, +void FakeShillServiceClient::Disconnect(const dbus::ObjectPath& service_path, const base::Closure& callback, const ErrorCallback& error_callback) { base::Value* service; @@ -226,7 +226,7 @@ void ShillServiceClientStub::Disconnect(const dbus::ObjectPath& service_path, base::StringValue idle_value(shill::kStateIdle); base::MessageLoop::current()->PostDelayedTask( FROM_HERE, - base::Bind(&ShillServiceClientStub::SetProperty, + base::Bind(&FakeShillServiceClient::SetProperty, weak_ptr_factory_.GetWeakPtr(), service_path, shill::kStateProperty, @@ -237,13 +237,13 @@ void ShillServiceClientStub::Disconnect(const dbus::ObjectPath& service_path, callback.Run(); } -void ShillServiceClientStub::Remove(const dbus::ObjectPath& service_path, +void FakeShillServiceClient::Remove(const dbus::ObjectPath& service_path, const base::Closure& callback, const ErrorCallback& error_callback) { base::MessageLoop::current()->PostTask(FROM_HERE, callback); } -void ShillServiceClientStub::ActivateCellularModem( +void FakeShillServiceClient::ActivateCellularModem( const dbus::ObjectPath& service_path, const std::string& carrier, const base::Closure& callback, @@ -266,7 +266,7 @@ void ShillServiceClientStub::ActivateCellularModem( // Set Activated after a delay base::MessageLoop::current()->PostDelayedTask( FROM_HERE, - base::Bind(&ShillServiceClientStub::SetCellularActivated, + base::Bind(&FakeShillServiceClient::SetCellularActivated, weak_ptr_factory_.GetWeakPtr(), service_path, error_callback), @@ -275,14 +275,14 @@ void ShillServiceClientStub::ActivateCellularModem( base::MessageLoop::current()->PostTask(FROM_HERE, callback); } -void ShillServiceClientStub::CompleteCellularActivation( +void FakeShillServiceClient::CompleteCellularActivation( const dbus::ObjectPath& service_path, const base::Closure& callback, const ErrorCallback& error_callback) { base::MessageLoop::current()->PostTask(FROM_HERE, callback); } -void ShillServiceClientStub::GetLoadableProfileEntries( +void FakeShillServiceClient::GetLoadableProfileEntries( const dbus::ObjectPath& service_path, const DictionaryValueCallback& callback) { // Provide a dictionary with a single { profile_path, service_path } entry @@ -311,13 +311,13 @@ void ShillServiceClientStub::GetLoadableProfileEntries( base::Owned(result_properties.release()))); } -ShillServiceClient::TestInterface* ShillServiceClientStub::GetTestInterface() { +ShillServiceClient::TestInterface* FakeShillServiceClient::GetTestInterface() { return this; } // ShillServiceClient::TestInterface overrides. -void ShillServiceClientStub::AddService(const std::string& service_path, +void FakeShillServiceClient::AddService(const std::string& service_path, const std::string& name, const std::string& type, const std::string& state, @@ -332,7 +332,7 @@ void ShillServiceClientStub::AddService(const std::string& service_path, add_to_visible_list, add_to_watch_list); } -void ShillServiceClientStub::AddServiceWithIPConfig( +void FakeShillServiceClient::AddServiceWithIPConfig( const std::string& service_path, const std::string& name, const std::string& type, @@ -366,7 +366,7 @@ void ShillServiceClientStub::AddServiceWithIPConfig( base::Value::CreateStringValue(ipconfig_path)); } -void ShillServiceClientStub::RemoveService(const std::string& service_path) { +void FakeShillServiceClient::RemoveService(const std::string& service_path) { DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()-> RemoveManagerService(service_path); @@ -374,7 +374,7 @@ void ShillServiceClientStub::RemoveService(const std::string& service_path) { connect_behavior_.erase(service_path); } -bool ShillServiceClientStub::SetServiceProperty(const std::string& service_path, +bool FakeShillServiceClient::SetServiceProperty(const std::string& service_path, const std::string& property, const base::Value& value) { base::DictionaryValue* dict = NULL; @@ -411,20 +411,20 @@ bool ShillServiceClientStub::SetServiceProperty(const std::string& service_path, base::MessageLoop::current()->PostTask( FROM_HERE, - base::Bind(&ShillServiceClientStub::NotifyObserversPropertyChanged, + base::Bind(&FakeShillServiceClient::NotifyObserversPropertyChanged, weak_ptr_factory_.GetWeakPtr(), dbus::ObjectPath(service_path), changed_property)); return true; } -const base::DictionaryValue* ShillServiceClientStub::GetServiceProperties( +const base::DictionaryValue* FakeShillServiceClient::GetServiceProperties( const std::string& service_path) const { const base::DictionaryValue* properties = NULL; stub_services_.GetDictionaryWithoutPathExpansion(service_path, &properties); return properties; } -void ShillServiceClientStub::ClearServices() { +void FakeShillServiceClient::ClearServices() { DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()-> ClearManagerServices(); @@ -432,12 +432,12 @@ void ShillServiceClientStub::ClearServices() { connect_behavior_.clear(); } -void ShillServiceClientStub::SetConnectBehavior(const std::string& service_path, +void FakeShillServiceClient::SetConnectBehavior(const std::string& service_path, const base::Closure& behavior) { connect_behavior_[service_path] = behavior; } -void ShillServiceClientStub::NotifyObserversPropertyChanged( +void FakeShillServiceClient::NotifyObserversPropertyChanged( const dbus::ObjectPath& service_path, const std::string& property) { base::DictionaryValue* dict = NULL; @@ -457,7 +457,7 @@ void ShillServiceClientStub::NotifyObserversPropertyChanged( OnPropertyChanged(property, *value)); } -base::DictionaryValue* ShillServiceClientStub::GetModifiableServiceProperties( +base::DictionaryValue* FakeShillServiceClient::GetModifiableServiceProperties( const std::string& service_path, bool create_if_missing) { base::DictionaryValue* properties = NULL; if (!stub_services_.GetDictionaryWithoutPathExpansion(service_path, @@ -469,8 +469,8 @@ base::DictionaryValue* ShillServiceClientStub::GetModifiableServiceProperties( return properties; } -ShillServiceClientStub::PropertyObserverList& -ShillServiceClientStub::GetObserverList(const dbus::ObjectPath& device_path) { +FakeShillServiceClient::PropertyObserverList& +FakeShillServiceClient::GetObserverList(const dbus::ObjectPath& device_path) { std::map<dbus::ObjectPath, PropertyObserverList*>::iterator iter = observer_list_.find(device_path); if (iter != observer_list_.end()) @@ -480,7 +480,7 @@ ShillServiceClientStub::GetObserverList(const dbus::ObjectPath& device_path) { return *observer_list; } -void ShillServiceClientStub::SetOtherServicesOffline( +void FakeShillServiceClient::SetOtherServicesOffline( const std::string& service_path) { const base::DictionaryValue* service_properties = GetServiceProperties( service_path); @@ -510,7 +510,7 @@ void ShillServiceClientStub::SetOtherServicesOffline( } } -void ShillServiceClientStub::SetCellularActivated( +void FakeShillServiceClient::SetCellularActivated( const dbus::ObjectPath& service_path, const ErrorCallback& error_callback) { SetProperty(service_path, @@ -525,9 +525,9 @@ void ShillServiceClientStub::SetCellularActivated( error_callback); } -void ShillServiceClientStub::ContinueConnect( +void FakeShillServiceClient::ContinueConnect( const std::string& service_path) { - VLOG(1) << "ShillServiceClientStub::ContinueConnect: " << service_path; + VLOG(1) << "FakeShillServiceClient::ContinueConnect: " << service_path; base::DictionaryValue* service_properties = NULL; if (!stub_services_.GetDictionary(service_path, &service_properties)) { LOG(ERROR) << "Service not found: " << service_path; @@ -553,7 +553,7 @@ void ShillServiceClientStub::ContinueConnect( base::MessageLoop::current()->PostTask( FROM_HERE, base::Bind( - base::IgnoreResult(&ShillServiceClientStub::SetServiceProperty), + base::IgnoreResult(&FakeShillServiceClient::SetServiceProperty), weak_ptr_factory_.GetWeakPtr(), service_path, shill::kErrorProperty, diff --git a/chromeos/dbus/shill_service_client_stub.h b/chromeos/dbus/fake_shill_service_client.h index 810a8ea..91000e4 100644 --- a/chromeos/dbus/shill_service_client_stub.h +++ b/chromeos/dbus/fake_shill_service_client.h @@ -1,9 +1,9 @@ -// Copyright (c) 2013 The Chromium Authors. All rights reserved. +// Copyright 2013 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef CHROMEOS_DBUS_SHILL_SERVICE_CLIENT_STUB_H_ -#define CHROMEOS_DBUS_SHILL_SERVICE_CLIENT_STUB_H_ +#ifndef CHROMEOS_DBUS_FAKE_SHILL_SERVICE_CLIENT_H_ +#define CHROMEOS_DBUS_FAKE_SHILL_SERVICE_CLIENT_H_ #include <map> #include <string> @@ -16,13 +16,14 @@ namespace chromeos { -// A stub implementation of ShillServiceClient. This works in close coordination -// with ShillManagerClientStub and is not intended to be used independently. -class ShillServiceClientStub : public ShillServiceClient, - public ShillServiceClient::TestInterface { +// A fake implementation of ShillServiceClient. This works in close coordination +// with FakeShillManagerClient and is not intended to be used independently. +class CHROMEOS_EXPORT FakeShillServiceClient : + public ShillServiceClient, + public ShillServiceClient::TestInterface { public: - ShillServiceClientStub(); - virtual ~ShillServiceClientStub(); + FakeShillServiceClient(); + virtual ~FakeShillServiceClient(); // ShillServiceClient overrides virtual void Init(dbus::Bus* bus) OVERRIDE; @@ -124,11 +125,11 @@ class ShillServiceClientStub : public ShillServiceClient, // Note: This should remain the last member so it'll be destroyed and // invalidate its weak pointers before any other members are destroyed. - base::WeakPtrFactory<ShillServiceClientStub> weak_ptr_factory_; + base::WeakPtrFactory<FakeShillServiceClient> weak_ptr_factory_; - DISALLOW_COPY_AND_ASSIGN(ShillServiceClientStub); + DISALLOW_COPY_AND_ASSIGN(FakeShillServiceClient); }; } // namespace chromeos -#endif // CHROMEOS_DBUS_SHILL_SERVICE_CLIENT_STUB_H_ +#endif // CHROMEOS_DBUS_FAKE_SHILL_SERVICE_CLIENT_H_ diff --git a/chromeos/dbus/shill_manager_client.cc b/chromeos/dbus/shill_manager_client.cc index cae9b68..f790e4e 100644 --- a/chromeos/dbus/shill_manager_client.cc +++ b/chromeos/dbus/shill_manager_client.cc @@ -8,7 +8,7 @@ #include "base/logging.h" #include "base/message_loop/message_loop.h" #include "base/values.h" -#include "chromeos/dbus/shill_manager_client_stub.h" +#include "chromeos/dbus/fake_shill_manager_client.h" #include "chromeos/dbus/shill_property_changed_observer.h" #include "dbus/bus.h" #include "dbus/message.h" @@ -242,7 +242,7 @@ ShillManagerClient* ShillManagerClient::Create( if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) return new ShillManagerClientImpl(); DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); - return new ShillManagerClientStub(); + return new FakeShillManagerClient(); } // ShillManagerClient::VerificationProperties implementation. diff --git a/chromeos/dbus/shill_manager_client_stub.cc b/chromeos/dbus/shill_manager_client_stub.cc deleted file mode 100644 index 946c553..0000000 --- a/chromeos/dbus/shill_manager_client_stub.cc +++ /dev/null @@ -1,636 +0,0 @@ -// Copyright (c) 2013 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chromeos/dbus/shill_manager_client_stub.h" - -#include "base/bind.h" -#include "base/command_line.h" -#include "base/message_loop/message_loop.h" -#include "base/values.h" -#include "chromeos/chromeos_switches.h" -#include "chromeos/dbus/dbus_thread_manager.h" -#include "chromeos/dbus/shill_device_client.h" -#include "chromeos/dbus/shill_profile_client.h" -#include "chromeos/dbus/shill_property_changed_observer.h" -#include "chromeos/dbus/shill_service_client.h" -#include "dbus/bus.h" -#include "dbus/message.h" -#include "dbus/object_path.h" -#include "dbus/values_util.h" -#include "third_party/cros_system_api/dbus/service_constants.h" - -namespace chromeos { - -namespace { - -// Used to compare values for finding entries to erase in a ListValue. -// (ListValue only implements a const_iterator version of Find). -struct ValueEquals { - explicit ValueEquals(const base::Value* first) : first_(first) {} - bool operator()(const base::Value* second) const { - return first_->Equals(second); - } - const base::Value* first_; -}; - -// Appends string entries from |service_list_in| whose entries in ServiceClient -// have Type |match_type| to either an active list or an inactive list -// based on the entry's State. -void AppendServicesForType( - const base::ListValue* service_list_in, - const char* match_type, - std::vector<std::string>* active_service_list_out, - std::vector<std::string>* inactive_service_list_out) { - ShillServiceClient::TestInterface* service_client = - DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface(); - for (base::ListValue::const_iterator iter = service_list_in->begin(); - iter != service_list_in->end(); ++iter) { - std::string service_path; - if (!(*iter)->GetAsString(&service_path)) - continue; - const base::DictionaryValue* properties = - service_client->GetServiceProperties(service_path); - if (!properties) { - LOG(ERROR) << "Properties not found for service: " << service_path; - continue; - } - std::string type; - properties->GetString(shill::kTypeProperty, &type); - if (type != match_type) - continue; - std::string state; - properties->GetString(shill::kStateProperty, &state); - if (state == shill::kStateOnline || - state == shill::kStateAssociation || - state == shill::kStateConfiguration || - state == shill::kStatePortal || - state == shill::kStateReady) { - active_service_list_out->push_back(service_path); - } else { - inactive_service_list_out->push_back(service_path); - } - } -} - -} // namespace - -ShillManagerClientStub::ShillManagerClientStub() - : weak_ptr_factory_(this) { -} - -ShillManagerClientStub::~ShillManagerClientStub() {} - -// ShillManagerClient overrides. - -void ShillManagerClientStub::Init(dbus::Bus* bus) {} - -void ShillManagerClientStub::AddPropertyChangedObserver( - ShillPropertyChangedObserver* observer) { - observer_list_.AddObserver(observer); -} - -void ShillManagerClientStub::RemovePropertyChangedObserver( - ShillPropertyChangedObserver* observer) { - observer_list_.RemoveObserver(observer); -} - -void ShillManagerClientStub::GetProperties( - const DictionaryValueCallback& callback) { - base::MessageLoop::current()->PostTask( - FROM_HERE, base::Bind( - &ShillManagerClientStub::PassStubProperties, - weak_ptr_factory_.GetWeakPtr(), - callback)); -} - -void ShillManagerClientStub::GetNetworksForGeolocation( - const DictionaryValueCallback& callback) { - base::MessageLoop::current()->PostTask( - FROM_HERE, base::Bind( - &ShillManagerClientStub::PassStubGeoNetworks, - weak_ptr_factory_.GetWeakPtr(), - callback)); -} - -void ShillManagerClientStub::SetProperty(const std::string& name, - const base::Value& value, - const base::Closure& callback, - const ErrorCallback& error_callback) { - stub_properties_.SetWithoutPathExpansion(name, value.DeepCopy()); - CallNotifyObserversPropertyChanged(name, 0); - base::MessageLoop::current()->PostTask(FROM_HERE, callback); -} - -void ShillManagerClientStub::RequestScan(const std::string& type, - const base::Closure& callback, - const ErrorCallback& error_callback) { - // For Stub purposes, default to a Wifi scan. - std::string device_type = shill::kTypeWifi; - if (!type.empty()) - device_type = type; - ShillDeviceClient::TestInterface* device_client = - DBusThreadManager::Get()->GetShillDeviceClient()->GetTestInterface(); - std::string device_path = device_client->GetDevicePathForType(device_type); - if (!device_path.empty()) { - device_client->SetDeviceProperty(device_path, - shill::kScanningProperty, - base::FundamentalValue(true)); - } - const int kScanDurationSeconds = 3; - int scan_duration_seconds = kScanDurationSeconds; - if (!CommandLine::ForCurrentProcess()->HasSwitch( - chromeos::switches::kEnableStubInteractive)) { - scan_duration_seconds = 0; - } - base::MessageLoop::current()->PostDelayedTask( - FROM_HERE, - base::Bind(&ShillManagerClientStub::ScanCompleted, - weak_ptr_factory_.GetWeakPtr(), device_path, callback), - base::TimeDelta::FromSeconds(scan_duration_seconds)); -} - -void ShillManagerClientStub::EnableTechnology( - const std::string& type, - const base::Closure& callback, - const ErrorCallback& error_callback) { - base::ListValue* enabled_list = NULL; - if (!stub_properties_.GetListWithoutPathExpansion( - shill::kEnabledTechnologiesProperty, &enabled_list)) { - base::MessageLoop::current()->PostTask(FROM_HERE, callback); - base::MessageLoop::current()->PostTask( - FROM_HERE, - base::Bind(error_callback, "StubError", "Property not found")); - return; - } - if (CommandLine::ForCurrentProcess()->HasSwitch( - chromeos::switches::kEnableStubInteractive)) { - const int kEnableTechnologyDelaySeconds = 3; - base::MessageLoop::current()->PostDelayedTask( - FROM_HERE, - base::Bind(&ShillManagerClientStub::SetTechnologyEnabled, - weak_ptr_factory_.GetWeakPtr(), type, callback, true), - base::TimeDelta::FromSeconds(kEnableTechnologyDelaySeconds)); - } else { - SetTechnologyEnabled(type, callback, true); - } -} - -void ShillManagerClientStub::DisableTechnology( - const std::string& type, - const base::Closure& callback, - const ErrorCallback& error_callback) { - base::ListValue* enabled_list = NULL; - if (!stub_properties_.GetListWithoutPathExpansion( - shill::kEnabledTechnologiesProperty, &enabled_list)) { - base::MessageLoop::current()->PostTask( - FROM_HERE, - base::Bind(error_callback, "StubError", "Property not found")); - return; - } - if (CommandLine::ForCurrentProcess()->HasSwitch( - chromeos::switches::kEnableStubInteractive)) { - const int kDisableTechnologyDelaySeconds = 3; - base::MessageLoop::current()->PostDelayedTask( - FROM_HERE, - base::Bind(&ShillManagerClientStub::SetTechnologyEnabled, - weak_ptr_factory_.GetWeakPtr(), type, callback, false), - base::TimeDelta::FromSeconds(kDisableTechnologyDelaySeconds)); - } else { - SetTechnologyEnabled(type, callback, false); - } -} - -void ShillManagerClientStub::ConfigureService( - const base::DictionaryValue& properties, - const ObjectPathCallback& callback, - const ErrorCallback& error_callback) { - ShillServiceClient::TestInterface* service_client = - DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface(); - - std::string guid; - std::string type; - if (!properties.GetString(shill::kGuidProperty, &guid) || - !properties.GetString(shill::kTypeProperty, &type)) { - LOG(ERROR) << "ConfigureService requies GUID and Type to be defined"; - // If the properties aren't filled out completely, then just return an empty - // object path. - base::MessageLoop::current()->PostTask( - FROM_HERE, base::Bind(callback, dbus::ObjectPath())); - return; - } - - // For the purposes of this stub, we're going to assume that the GUID property - // is set to the service path because we don't want to re-implement Shill's - // property matching magic here. - std::string service_path = guid; - - std::string ipconfig_path; - properties.GetString(shill::kIPConfigProperty, &ipconfig_path); - - // Merge the new properties with existing properties, if any. - const base::DictionaryValue* existing_properties = - service_client->GetServiceProperties(service_path); - if (!existing_properties) { - // Add a new service to the service client stub because none exists, yet. - // This calls AddManagerService. - service_client->AddServiceWithIPConfig(service_path, guid, type, - shill::kStateIdle, ipconfig_path, - true /* visible */, - true /* watch */); - existing_properties = service_client->GetServiceProperties(service_path); - } - - scoped_ptr<base::DictionaryValue> merged_properties( - existing_properties->DeepCopy()); - merged_properties->MergeDictionary(&properties); - - // Now set all the properties. - for (base::DictionaryValue::Iterator iter(*merged_properties); - !iter.IsAtEnd(); iter.Advance()) { - service_client->SetServiceProperty(service_path, iter.key(), iter.value()); - } - - // If the Profile property is set, add it to ProfileClient. - std::string profile_path; - merged_properties->GetStringWithoutPathExpansion(shill::kProfileProperty, - &profile_path); - if (!profile_path.empty()) { - DBusThreadManager::Get()->GetShillProfileClient()->GetTestInterface()-> - AddService(profile_path, service_path); - } - - base::MessageLoop::current()->PostTask( - FROM_HERE, base::Bind(callback, dbus::ObjectPath(service_path))); -} - -void ShillManagerClientStub::ConfigureServiceForProfile( - const dbus::ObjectPath& profile_path, - const base::DictionaryValue& properties, - const ObjectPathCallback& callback, - const ErrorCallback& error_callback) { - std::string profile_property; - properties.GetStringWithoutPathExpansion(shill::kProfileProperty, - &profile_property); - CHECK(profile_property == profile_path.value()); - ConfigureService(properties, callback, error_callback); -} - - -void ShillManagerClientStub::GetService( - const base::DictionaryValue& properties, - const ObjectPathCallback& callback, - const ErrorCallback& error_callback) { - base::MessageLoop::current()->PostTask( - FROM_HERE, base::Bind(callback, dbus::ObjectPath())); -} - -void ShillManagerClientStub::VerifyDestination( - const VerificationProperties& properties, - const BooleanCallback& callback, - const ErrorCallback& error_callback) { - base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(callback, true)); -} - -void ShillManagerClientStub::VerifyAndEncryptCredentials( - const VerificationProperties& properties, - const std::string& service_path, - const StringCallback& callback, - const ErrorCallback& error_callback) { - base::MessageLoop::current()->PostTask( - FROM_HERE, base::Bind(callback, "encrypted_credentials")); -} - -void ShillManagerClientStub::VerifyAndEncryptData( - const VerificationProperties& properties, - const std::string& data, - const StringCallback& callback, - const ErrorCallback& error_callback) { - base::MessageLoop::current()->PostTask(FROM_HERE, - base::Bind(callback, "encrypted_data")); -} - -void ShillManagerClientStub::ConnectToBestServices( - const base::Closure& callback, - const ErrorCallback& error_callback) { -} - -ShillManagerClient::TestInterface* ShillManagerClientStub::GetTestInterface() { - return this; -} - -// ShillManagerClient::TestInterface overrides. - -void ShillManagerClientStub::AddDevice(const std::string& device_path) { - if (GetListProperty(shill::kDevicesProperty)->AppendIfNotPresent( - base::Value::CreateStringValue(device_path))) { - CallNotifyObserversPropertyChanged(shill::kDevicesProperty, 0); - } -} - -void ShillManagerClientStub::RemoveDevice(const std::string& device_path) { - base::StringValue device_path_value(device_path); - if (GetListProperty(shill::kDevicesProperty)->Remove( - device_path_value, NULL)) { - CallNotifyObserversPropertyChanged(shill::kDevicesProperty, 0); - } -} - -void ShillManagerClientStub::ClearDevices() { - GetListProperty(shill::kDevicesProperty)->Clear(); - CallNotifyObserversPropertyChanged(shill::kDevicesProperty, 0); -} - -void ShillManagerClientStub::AddTechnology(const std::string& type, - bool enabled) { - if (GetListProperty(shill::kAvailableTechnologiesProperty)-> - AppendIfNotPresent(base::Value::CreateStringValue(type))) { - CallNotifyObserversPropertyChanged( - shill::kAvailableTechnologiesProperty, 0); - } - if (enabled && - GetListProperty(shill::kEnabledTechnologiesProperty)-> - AppendIfNotPresent(base::Value::CreateStringValue(type))) { - CallNotifyObserversPropertyChanged( - shill::kEnabledTechnologiesProperty, 0); - } -} - -void ShillManagerClientStub::RemoveTechnology(const std::string& type) { - base::StringValue type_value(type); - if (GetListProperty(shill::kAvailableTechnologiesProperty)->Remove( - type_value, NULL)) { - CallNotifyObserversPropertyChanged( - shill::kAvailableTechnologiesProperty, 0); - } - if (GetListProperty(shill::kEnabledTechnologiesProperty)->Remove( - type_value, NULL)) { - CallNotifyObserversPropertyChanged( - shill::kEnabledTechnologiesProperty, 0); - } -} - -void ShillManagerClientStub::SetTechnologyInitializing(const std::string& type, - bool initializing) { - if (initializing) { - if (GetListProperty(shill::kUninitializedTechnologiesProperty)-> - AppendIfNotPresent(base::Value::CreateStringValue(type))) { - CallNotifyObserversPropertyChanged( - shill::kUninitializedTechnologiesProperty, 0); - } - } else { - if (GetListProperty(shill::kUninitializedTechnologiesProperty)->Remove( - base::StringValue(type), NULL)) { - CallNotifyObserversPropertyChanged( - shill::kUninitializedTechnologiesProperty, 0); - } - } -} - -void ShillManagerClientStub::ClearProperties() { - stub_properties_.Clear(); -} - -void ShillManagerClientStub::AddManagerService(const std::string& service_path, - bool add_to_visible_list, - bool add_to_watch_list) { - // Always add to ServiceCompleteListProperty. - GetListProperty(shill::kServiceCompleteListProperty)->AppendIfNotPresent( - base::Value::CreateStringValue(service_path)); - // If visible, add to Services and notify if new. - if (add_to_visible_list && - GetListProperty(shill::kServicesProperty)->AppendIfNotPresent( - base::Value::CreateStringValue(service_path))) { - CallNotifyObserversPropertyChanged(shill::kServicesProperty, 0); - } - if (add_to_watch_list) - AddServiceToWatchList(service_path); -} - -void ShillManagerClientStub::RemoveManagerService( - const std::string& service_path) { - base::StringValue service_path_value(service_path); - if (GetListProperty(shill::kServicesProperty)->Remove( - service_path_value, NULL)) { - CallNotifyObserversPropertyChanged(shill::kServicesProperty, 0); - } - GetListProperty(shill::kServiceCompleteListProperty)->Remove( - service_path_value, NULL); - if (GetListProperty(shill::kServiceWatchListProperty)->Remove( - service_path_value, NULL)) { - CallNotifyObserversPropertyChanged( - shill::kServiceWatchListProperty, 0); - } -} - -void ShillManagerClientStub::ClearManagerServices() { - GetListProperty(shill::kServicesProperty)->Clear(); - GetListProperty(shill::kServiceCompleteListProperty)->Clear(); - GetListProperty(shill::kServiceWatchListProperty)->Clear(); - CallNotifyObserversPropertyChanged(shill::kServicesProperty, 0); - CallNotifyObserversPropertyChanged(shill::kServiceWatchListProperty, 0); -} - -void ShillManagerClientStub::SortManagerServices() { - static const char* ordered_types[] = { - shill::kTypeEthernet, - shill::kTypeWifi, - shill::kTypeCellular, - shill::kTypeWimax, - shill::kTypeVPN - }; - base::ListValue* service_list = GetListProperty(shill::kServicesProperty); - if (!service_list || service_list->empty()) - return; - std::vector<std::string> active_services; - std::vector<std::string> inactive_services; - for (size_t i = 0; i < arraysize(ordered_types); ++i) { - AppendServicesForType(service_list, ordered_types[i], - &active_services, &inactive_services); - } - service_list->Clear(); - for (size_t i = 0; i < active_services.size(); ++i) - service_list->AppendString(active_services[i]); - for (size_t i = 0; i < inactive_services.size(); ++i) - service_list->AppendString(inactive_services[i]); - - CallNotifyObserversPropertyChanged(shill::kServicesProperty, 0); -} - -void ShillManagerClientStub::AddGeoNetwork( - const std::string& technology, - const base::DictionaryValue& network) { - base::ListValue* list_value = NULL; - if (!stub_geo_networks_.GetListWithoutPathExpansion( - technology, &list_value)) { - list_value = new base::ListValue; - stub_geo_networks_.SetWithoutPathExpansion(technology, list_value); - } - list_value->Append(network.DeepCopy()); -} - -void ShillManagerClientStub::AddProfile(const std::string& profile_path) { - const char* key = shill::kProfilesProperty; - if (GetListProperty(key)->AppendIfNotPresent( - new base::StringValue(profile_path))) { - CallNotifyObserversPropertyChanged(key, 0); - } -} - -void ShillManagerClientStub::AddServiceToWatchList( - const std::string& service_path) { - // Remove and insert the service, moving it to the front of the watch list. - GetListProperty(shill::kServiceWatchListProperty)->Remove( - base::StringValue(service_path), NULL); - GetListProperty(shill::kServiceWatchListProperty)->Insert( - 0, base::Value::CreateStringValue(service_path)); - CallNotifyObserversPropertyChanged( - shill::kServiceWatchListProperty, 0); -} - -void ShillManagerClientStub::PassStubProperties( - const DictionaryValueCallback& callback) const { - scoped_ptr<base::DictionaryValue> stub_properties( - stub_properties_.DeepCopy()); - // Remove disabled services from the list. - stub_properties->SetWithoutPathExpansion( - shill::kServicesProperty, - GetEnabledServiceList(shill::kServicesProperty)); - stub_properties->SetWithoutPathExpansion( - shill::kServiceWatchListProperty, - GetEnabledServiceList(shill::kServiceWatchListProperty)); - callback.Run(DBUS_METHOD_CALL_SUCCESS, *stub_properties); -} - -void ShillManagerClientStub::PassStubGeoNetworks( - const DictionaryValueCallback& callback) const { - callback.Run(DBUS_METHOD_CALL_SUCCESS, stub_geo_networks_); -} - -void ShillManagerClientStub::CallNotifyObserversPropertyChanged( - const std::string& property, - int delay_ms) { - // Avoid unnecessary delayed task if we have no observers (e.g. during - // initial setup). - if (!observer_list_.might_have_observers()) - return; - if (!CommandLine::ForCurrentProcess()->HasSwitch( - chromeos::switches::kEnableStubInteractive)) { - delay_ms = 0; - } - base::MessageLoop::current()->PostDelayedTask( - FROM_HERE, - base::Bind(&ShillManagerClientStub::NotifyObserversPropertyChanged, - weak_ptr_factory_.GetWeakPtr(), - property), - base::TimeDelta::FromMilliseconds(delay_ms)); -} - -void ShillManagerClientStub::NotifyObserversPropertyChanged( - const std::string& property) { - if (property == shill::kServicesProperty || - property == shill::kServiceWatchListProperty) { - scoped_ptr<base::ListValue> services(GetEnabledServiceList(property)); - FOR_EACH_OBSERVER(ShillPropertyChangedObserver, - observer_list_, - OnPropertyChanged(property, *(services.get()))); - return; - } - base::Value* value = NULL; - if (!stub_properties_.GetWithoutPathExpansion(property, &value)) { - LOG(ERROR) << "Notify for unknown property: " << property; - return; - } - FOR_EACH_OBSERVER(ShillPropertyChangedObserver, - observer_list_, - OnPropertyChanged(property, *value)); -} - -base::ListValue* ShillManagerClientStub::GetListProperty( - const std::string& property) { - base::ListValue* list_property = NULL; - if (!stub_properties_.GetListWithoutPathExpansion( - property, &list_property)) { - list_property = new base::ListValue; - stub_properties_.SetWithoutPathExpansion(property, list_property); - } - return list_property; -} - -bool ShillManagerClientStub::TechnologyEnabled(const std::string& type) const { - if (type == shill::kTypeVPN) - return true; // VPN is always "enabled" since there is no associated device - bool enabled = false; - const base::ListValue* technologies; - if (stub_properties_.GetListWithoutPathExpansion( - shill::kEnabledTechnologiesProperty, &technologies)) { - base::StringValue type_value(type); - if (technologies->Find(type_value) != technologies->end()) - enabled = true; - } - return enabled; -} - -void ShillManagerClientStub::SetTechnologyEnabled( - const std::string& type, - const base::Closure& callback, - bool enabled) { - base::ListValue* enabled_list = NULL; - stub_properties_.GetListWithoutPathExpansion( - shill::kEnabledTechnologiesProperty, &enabled_list); - DCHECK(enabled_list); - if (enabled) - enabled_list->AppendIfNotPresent(new base::StringValue(type)); - else - enabled_list->Remove(base::StringValue(type), NULL); - CallNotifyObserversPropertyChanged( - shill::kEnabledTechnologiesProperty, 0 /* already delayed */); - base::MessageLoop::current()->PostTask(FROM_HERE, callback); - // May affect available services - CallNotifyObserversPropertyChanged(shill::kServicesProperty, 0); - CallNotifyObserversPropertyChanged(shill::kServiceWatchListProperty, 0); -} - -base::ListValue* ShillManagerClientStub::GetEnabledServiceList( - const std::string& property) const { - base::ListValue* new_service_list = new base::ListValue; - const base::ListValue* service_list; - if (stub_properties_.GetListWithoutPathExpansion(property, &service_list)) { - ShillServiceClient::TestInterface* service_client = - DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface(); - for (base::ListValue::const_iterator iter = service_list->begin(); - iter != service_list->end(); ++iter) { - std::string service_path; - if (!(*iter)->GetAsString(&service_path)) - continue; - const base::DictionaryValue* properties = - service_client->GetServiceProperties(service_path); - if (!properties) { - LOG(ERROR) << "Properties not found for service: " << service_path; - continue; - } - std::string name; - properties->GetString(shill::kNameProperty, &name); - std::string type; - properties->GetString(shill::kTypeProperty, &type); - if (TechnologyEnabled(type)) - new_service_list->Append((*iter)->DeepCopy()); - } - } - return new_service_list; -} - -void ShillManagerClientStub::ScanCompleted(const std::string& device_path, - const base::Closure& callback) { - if (!device_path.empty()) { - DBusThreadManager::Get()->GetShillDeviceClient()->GetTestInterface()-> - SetDeviceProperty(device_path, - shill::kScanningProperty, - base::FundamentalValue(false)); - } - CallNotifyObserversPropertyChanged(shill::kServicesProperty, 0); - CallNotifyObserversPropertyChanged(shill::kServiceWatchListProperty, 0); - base::MessageLoop::current()->PostTask(FROM_HERE, callback); -} - -} // namespace chromeos diff --git a/chromeos/dbus/shill_manager_client_stub.h b/chromeos/dbus/shill_manager_client_stub.h deleted file mode 100644 index f6edc15..0000000 --- a/chromeos/dbus/shill_manager_client_stub.h +++ /dev/null @@ -1,132 +0,0 @@ -// Copyright (c) 2013 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROMEOS_DBUS_SHILL_MANAGER_CLIENT_STUB_H_ -#define CHROMEOS_DBUS_SHILL_MANAGER_CLIENT_STUB_H_ - -#include <string> - -#include "base/basictypes.h" -#include "base/callback.h" -#include "chromeos/dbus/shill_manager_client.h" - -namespace chromeos { - -// A stub implementation of ShillManagerClient. This works in close coordination -// with ShillServiceClientStub. ShillDeviceClientStub, and -// ShillProfileClientStub, and is not intended to be used independently. -class ShillManagerClientStub : public ShillManagerClient, - public ShillManagerClient::TestInterface { - public: - ShillManagerClientStub(); - virtual ~ShillManagerClientStub(); - - // ShillManagerClient overrides - virtual void Init(dbus::Bus* bus) OVERRIDE; - virtual void AddPropertyChangedObserver( - ShillPropertyChangedObserver* observer) OVERRIDE; - virtual void RemovePropertyChangedObserver( - ShillPropertyChangedObserver* observer) OVERRIDE; - virtual void GetProperties(const DictionaryValueCallback& callback) OVERRIDE; - virtual void GetNetworksForGeolocation( - const DictionaryValueCallback& callback) OVERRIDE; - virtual void SetProperty(const std::string& name, - const base::Value& value, - const base::Closure& callback, - const ErrorCallback& error_callback) OVERRIDE; - virtual void RequestScan(const std::string& type, - const base::Closure& callback, - const ErrorCallback& error_callback) OVERRIDE; - virtual void EnableTechnology( - const std::string& type, - const base::Closure& callback, - const ErrorCallback& error_callback) OVERRIDE; - virtual void DisableTechnology( - const std::string& type, - const base::Closure& callback, - const ErrorCallback& error_callback) OVERRIDE; - virtual void ConfigureService( - const base::DictionaryValue& properties, - const ObjectPathCallback& callback, - const ErrorCallback& error_callback) OVERRIDE; - virtual void ConfigureServiceForProfile( - const dbus::ObjectPath& profile_path, - const base::DictionaryValue& properties, - const ObjectPathCallback& callback, - const ErrorCallback& error_callback) OVERRIDE; - virtual void GetService( - const base::DictionaryValue& properties, - const ObjectPathCallback& callback, - const ErrorCallback& error_callback) OVERRIDE; - virtual void VerifyDestination(const VerificationProperties& properties, - const BooleanCallback& callback, - const ErrorCallback& error_callback) OVERRIDE; - virtual void VerifyAndEncryptCredentials( - const VerificationProperties& properties, - const std::string& service_path, - const StringCallback& callback, - const ErrorCallback& error_callback) OVERRIDE; - virtual void VerifyAndEncryptData( - const VerificationProperties& properties, - const std::string& data, - const StringCallback& callback, - const ErrorCallback& error_callback) OVERRIDE; - virtual void ConnectToBestServices( - const base::Closure& callback, - const ErrorCallback& error_callback) OVERRIDE; - virtual ShillManagerClient::TestInterface* GetTestInterface() OVERRIDE; - - // ShillManagerClient::TestInterface overrides. - virtual void AddDevice(const std::string& device_path) OVERRIDE; - virtual void RemoveDevice(const std::string& device_path) OVERRIDE; - virtual void ClearDevices() OVERRIDE; - virtual void AddTechnology(const std::string& type, bool enabled) OVERRIDE; - virtual void RemoveTechnology(const std::string& type) OVERRIDE; - virtual void SetTechnologyInitializing(const std::string& type, - bool initializing) OVERRIDE; - virtual void AddGeoNetwork(const std::string& technology, - const base::DictionaryValue& network) OVERRIDE; - virtual void AddProfile(const std::string& profile_path) OVERRIDE; - virtual void ClearProperties() OVERRIDE; - virtual void AddManagerService(const std::string& service_path, - bool add_to_visible_list, - bool add_to_watch_list) OVERRIDE; - virtual void RemoveManagerService(const std::string& service_path) OVERRIDE; - virtual void ClearManagerServices() OVERRIDE; - virtual void SortManagerServices() OVERRIDE; - - private: - void AddServiceToWatchList(const std::string& service_path); - void SetDefaultProperties(); - void PassStubProperties(const DictionaryValueCallback& callback) const; - void PassStubGeoNetworks(const DictionaryValueCallback& callback) const; - void CallNotifyObserversPropertyChanged(const std::string& property, - int delay_ms); - void NotifyObserversPropertyChanged(const std::string& property); - base::ListValue* GetListProperty(const std::string& property); - bool TechnologyEnabled(const std::string& type) const; - void SetTechnologyEnabled(const std::string& type, - const base::Closure& callback, - bool enabled); - base::ListValue* GetEnabledServiceList(const std::string& property) const; - void ScanCompleted(const std::string& device_path, - const base::Closure& callback); - - // Dictionary of property name -> property value - base::DictionaryValue stub_properties_; - // Dictionary of technology -> list of property dictionaries - base::DictionaryValue stub_geo_networks_; - - ObserverList<ShillPropertyChangedObserver> observer_list_; - - // Note: This should remain the last member so it'll be destroyed and - // invalidate its weak pointers before any other members are destroyed. - base::WeakPtrFactory<ShillManagerClientStub> weak_ptr_factory_; - - DISALLOW_COPY_AND_ASSIGN(ShillManagerClientStub); -}; - -} // namespace chromeos - -#endif // CHROMEOS_DBUS_SHILL_MANAGER_CLIENT_STUB_H_ diff --git a/chromeos/dbus/shill_profile_client.cc b/chromeos/dbus/shill_profile_client.cc index 0e63354..05bfa04 100644 --- a/chromeos/dbus/shill_profile_client.cc +++ b/chromeos/dbus/shill_profile_client.cc @@ -9,7 +9,7 @@ #include "base/stl_util.h" #include "base/values.h" #include "chromeos/dbus/dbus_thread_manager.h" -#include "chromeos/dbus/shill_profile_client_stub.h" +#include "chromeos/dbus/fake_shill_profile_client.h" #include "chromeos/dbus/shill_property_changed_observer.h" #include "dbus/bus.h" #include "dbus/message.h" @@ -140,7 +140,7 @@ ShillProfileClient* ShillProfileClient::Create( if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) return new ShillProfileClientImpl(); DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); - return new ShillProfileClientStub(); + return new FakeShillProfileClient(); } } // namespace chromeos diff --git a/chromeos/dbus/shill_service_client.cc b/chromeos/dbus/shill_service_client.cc index 696fb63..063d1e3 100644 --- a/chromeos/dbus/shill_service_client.cc +++ b/chromeos/dbus/shill_service_client.cc @@ -9,8 +9,8 @@ #include "base/message_loop/message_loop.h" #include "base/stl_util.h" #include "base/values.h" +#include "chromeos/dbus/fake_shill_service_client.h" #include "chromeos/dbus/shill_property_changed_observer.h" -#include "chromeos/dbus/shill_service_client_stub.h" #include "chromeos/network/network_event_log.h" #include "dbus/bus.h" #include "dbus/message.h" @@ -278,7 +278,7 @@ ShillServiceClient* ShillServiceClient::Create( if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) return new ShillServiceClientImpl(); DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); - return new ShillServiceClientStub(); + return new FakeShillServiceClient(); } } // namespace chromeos diff --git a/chromeos/dbus/shill_stub_helper.cc b/chromeos/dbus/shill_stub_helper.cc index 68dfa68..08f0ef8 100644 --- a/chromeos/dbus/shill_stub_helper.cc +++ b/chromeos/dbus/shill_stub_helper.cc @@ -11,7 +11,6 @@ #include "chromeos/dbus/shill_device_client.h" #include "chromeos/dbus/shill_manager_client.h" #include "chromeos/dbus/shill_profile_client.h" -#include "chromeos/dbus/shill_profile_client_stub.h" #include "chromeos/dbus/shill_service_client.h" #include "third_party/cros_system_api/dbus/service_constants.h" diff --git a/chromeos/dbus/shill_stub_helper.h b/chromeos/dbus/shill_stub_helper.h index e5b999b..b599014 100644 --- a/chromeos/dbus/shill_stub_helper.h +++ b/chromeos/dbus/shill_stub_helper.h @@ -15,7 +15,7 @@ namespace shill_stub_helper { CHROMEOS_EXPORT extern const char kSharedProfilePath[]; // Add default devices, services and profiles. This works only if -// DBusThreadManager was initialized with the Shill*ClientStubs. +// DBusThreadManager was initialized with the FakeShill*Clients. void SetupDefaultEnvironment(); std::string DevicePathForType(const std::string& type); |