| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/dbus/shill_profile_client_stub.h" | 5 #include "chromeos/dbus/fake_shill_profile_client.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chromeos/dbus/dbus_thread_manager.h" | 12 #include "chromeos/dbus/dbus_thread_manager.h" |
| 13 #include "chromeos/dbus/shill_manager_client.h" | 13 #include "chromeos/dbus/shill_manager_client.h" |
| 14 #include "chromeos/dbus/shill_property_changed_observer.h" | 14 #include "chromeos/dbus/shill_property_changed_observer.h" |
| 15 #include "chromeos/dbus/shill_service_client.h" | 15 #include "chromeos/dbus/shill_service_client.h" |
| 16 #include "dbus/bus.h" | 16 #include "dbus/bus.h" |
| 17 #include "dbus/message.h" | 17 #include "dbus/message.h" |
| 18 #include "dbus/object_path.h" | 18 #include "dbus/object_path.h" |
| 19 #include "dbus/values_util.h" | 19 #include "dbus/values_util.h" |
| 20 #include "third_party/cros_system_api/dbus/service_constants.h" | 20 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 21 | 21 |
| 22 namespace chromeos { | 22 namespace chromeos { |
| 23 | 23 |
| 24 struct ShillProfileClientStub::ProfileProperties { | 24 struct FakeShillProfileClient::ProfileProperties { |
| 25 base::DictionaryValue entries; | 25 base::DictionaryValue entries; |
| 26 base::DictionaryValue properties; | 26 base::DictionaryValue properties; |
| 27 }; | 27 }; |
| 28 | 28 |
| 29 namespace { | 29 namespace { |
| 30 | 30 |
| 31 void PassDictionary( | 31 void PassDictionary( |
| 32 const ShillProfileClient::DictionaryValueCallbackWithoutStatus& callback, | 32 const ShillProfileClient::DictionaryValueCallbackWithoutStatus& callback, |
| 33 const base::DictionaryValue* dictionary) { | 33 const base::DictionaryValue* dictionary) { |
| 34 callback.Run(*dictionary); | 34 callback.Run(*dictionary); |
| 35 } | 35 } |
| 36 | 36 |
| 37 } // namespace | 37 } // namespace |
| 38 | 38 |
| 39 ShillProfileClientStub::ShillProfileClientStub() { | 39 FakeShillProfileClient::FakeShillProfileClient() { |
| 40 } | 40 } |
| 41 | 41 |
| 42 ShillProfileClientStub::~ShillProfileClientStub() { | 42 FakeShillProfileClient::~FakeShillProfileClient() { |
| 43 STLDeleteValues(&profiles_); | 43 STLDeleteValues(&profiles_); |
| 44 } | 44 } |
| 45 | 45 |
| 46 void ShillProfileClientStub::Init(dbus::Bus* bus) { | 46 void FakeShillProfileClient::Init(dbus::Bus* bus) { |
| 47 } | 47 } |
| 48 | 48 |
| 49 void ShillProfileClientStub::AddPropertyChangedObserver( | 49 void FakeShillProfileClient::AddPropertyChangedObserver( |
| 50 const dbus::ObjectPath& profile_path, | 50 const dbus::ObjectPath& profile_path, |
| 51 ShillPropertyChangedObserver* observer) { | 51 ShillPropertyChangedObserver* observer) { |
| 52 } | 52 } |
| 53 | 53 |
| 54 void ShillProfileClientStub::RemovePropertyChangedObserver( | 54 void FakeShillProfileClient::RemovePropertyChangedObserver( |
| 55 const dbus::ObjectPath& profile_path, | 55 const dbus::ObjectPath& profile_path, |
| 56 ShillPropertyChangedObserver* observer) { | 56 ShillPropertyChangedObserver* observer) { |
| 57 } | 57 } |
| 58 | 58 |
| 59 void ShillProfileClientStub::GetProperties( | 59 void FakeShillProfileClient::GetProperties( |
| 60 const dbus::ObjectPath& profile_path, | 60 const dbus::ObjectPath& profile_path, |
| 61 const DictionaryValueCallbackWithoutStatus& callback, | 61 const DictionaryValueCallbackWithoutStatus& callback, |
| 62 const ErrorCallback& error_callback) { | 62 const ErrorCallback& error_callback) { |
| 63 ProfileProperties* profile = GetProfile(profile_path, error_callback); | 63 ProfileProperties* profile = GetProfile(profile_path, error_callback); |
| 64 if (!profile) | 64 if (!profile) |
| 65 return; | 65 return; |
| 66 | 66 |
| 67 scoped_ptr<base::DictionaryValue> properties(profile->properties.DeepCopy()); | 67 scoped_ptr<base::DictionaryValue> properties(profile->properties.DeepCopy()); |
| 68 base::ListValue* entry_paths = new base::ListValue; | 68 base::ListValue* entry_paths = new base::ListValue; |
| 69 properties->SetWithoutPathExpansion(shill::kEntriesProperty, entry_paths); | 69 properties->SetWithoutPathExpansion(shill::kEntriesProperty, entry_paths); |
| 70 for (base::DictionaryValue::Iterator it(profile->entries); !it.IsAtEnd(); | 70 for (base::DictionaryValue::Iterator it(profile->entries); !it.IsAtEnd(); |
| 71 it.Advance()) { | 71 it.Advance()) { |
| 72 entry_paths->AppendString(it.key()); | 72 entry_paths->AppendString(it.key()); |
| 73 } | 73 } |
| 74 | 74 |
| 75 base::MessageLoop::current()->PostTask( | 75 base::MessageLoop::current()->PostTask( |
| 76 FROM_HERE, | 76 FROM_HERE, |
| 77 base::Bind(&PassDictionary, callback, base::Owned(properties.release()))); | 77 base::Bind(&PassDictionary, callback, base::Owned(properties.release()))); |
| 78 } | 78 } |
| 79 | 79 |
| 80 void ShillProfileClientStub::GetEntry( | 80 void FakeShillProfileClient::GetEntry( |
| 81 const dbus::ObjectPath& profile_path, | 81 const dbus::ObjectPath& profile_path, |
| 82 const std::string& entry_path, | 82 const std::string& entry_path, |
| 83 const DictionaryValueCallbackWithoutStatus& callback, | 83 const DictionaryValueCallbackWithoutStatus& callback, |
| 84 const ErrorCallback& error_callback) { | 84 const ErrorCallback& error_callback) { |
| 85 ProfileProperties* profile = GetProfile(profile_path, error_callback); | 85 ProfileProperties* profile = GetProfile(profile_path, error_callback); |
| 86 if (!profile) | 86 if (!profile) |
| 87 return; | 87 return; |
| 88 | 88 |
| 89 base::DictionaryValue* entry = NULL; | 89 base::DictionaryValue* entry = NULL; |
| 90 profile->entries.GetDictionaryWithoutPathExpansion(entry_path, &entry); | 90 profile->entries.GetDictionaryWithoutPathExpansion(entry_path, &entry); |
| 91 if (!entry) { | 91 if (!entry) { |
| 92 error_callback.Run("Error.InvalidProfileEntry", "Invalid profile entry"); | 92 error_callback.Run("Error.InvalidProfileEntry", "Invalid profile entry"); |
| 93 return; | 93 return; |
| 94 } | 94 } |
| 95 | 95 |
| 96 base::MessageLoop::current()->PostTask( | 96 base::MessageLoop::current()->PostTask( |
| 97 FROM_HERE, | 97 FROM_HERE, |
| 98 base::Bind(&PassDictionary, callback, base::Owned(entry->DeepCopy()))); | 98 base::Bind(&PassDictionary, callback, base::Owned(entry->DeepCopy()))); |
| 99 } | 99 } |
| 100 | 100 |
| 101 void ShillProfileClientStub::DeleteEntry(const dbus::ObjectPath& profile_path, | 101 void FakeShillProfileClient::DeleteEntry(const dbus::ObjectPath& profile_path, |
| 102 const std::string& entry_path, | 102 const std::string& entry_path, |
| 103 const base::Closure& callback, | 103 const base::Closure& callback, |
| 104 const ErrorCallback& error_callback) { | 104 const ErrorCallback& error_callback) { |
| 105 ProfileProperties* profile = GetProfile(profile_path, error_callback); | 105 ProfileProperties* profile = GetProfile(profile_path, error_callback); |
| 106 if (!profile) | 106 if (!profile) |
| 107 return; | 107 return; |
| 108 | 108 |
| 109 if (!profile->entries.RemoveWithoutPathExpansion(entry_path, NULL)) { | 109 if (!profile->entries.RemoveWithoutPathExpansion(entry_path, NULL)) { |
| 110 error_callback.Run("Error.InvalidProfileEntry", entry_path); | 110 error_callback.Run("Error.InvalidProfileEntry", entry_path); |
| 111 return; | 111 return; |
| 112 } | 112 } |
| 113 | 113 |
| 114 base::StringValue profile_path_value(""); | 114 base::StringValue profile_path_value(""); |
| 115 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface()-> | 115 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface()-> |
| 116 SetServiceProperty(entry_path, | 116 SetServiceProperty(entry_path, |
| 117 shill::kProfileProperty, | 117 shill::kProfileProperty, |
| 118 profile_path_value); | 118 profile_path_value); |
| 119 | 119 |
| 120 base::MessageLoop::current()->PostTask(FROM_HERE, callback); | 120 base::MessageLoop::current()->PostTask(FROM_HERE, callback); |
| 121 } | 121 } |
| 122 | 122 |
| 123 ShillProfileClient::TestInterface* ShillProfileClientStub::GetTestInterface() { | 123 ShillProfileClient::TestInterface* FakeShillProfileClient::GetTestInterface() { |
| 124 return this; | 124 return this; |
| 125 } | 125 } |
| 126 | 126 |
| 127 void ShillProfileClientStub::AddProfile(const std::string& profile_path, | 127 void FakeShillProfileClient::AddProfile(const std::string& profile_path, |
| 128 const std::string& userhash) { | 128 const std::string& userhash) { |
| 129 if (GetProfile(dbus::ObjectPath(profile_path), ErrorCallback())) | 129 if (GetProfile(dbus::ObjectPath(profile_path), ErrorCallback())) |
| 130 return; | 130 return; |
| 131 | 131 |
| 132 ProfileProperties* profile = new ProfileProperties; | 132 ProfileProperties* profile = new ProfileProperties; |
| 133 profile->properties.SetStringWithoutPathExpansion(shill::kUserHashProperty, | 133 profile->properties.SetStringWithoutPathExpansion(shill::kUserHashProperty, |
| 134 userhash); | 134 userhash); |
| 135 profiles_[profile_path] = profile; | 135 profiles_[profile_path] = profile; |
| 136 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()-> | 136 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()-> |
| 137 AddProfile(profile_path); | 137 AddProfile(profile_path); |
| 138 } | 138 } |
| 139 | 139 |
| 140 void ShillProfileClientStub::AddEntry(const std::string& profile_path, | 140 void FakeShillProfileClient::AddEntry(const std::string& profile_path, |
| 141 const std::string& entry_path, | 141 const std::string& entry_path, |
| 142 const base::DictionaryValue& properties) { | 142 const base::DictionaryValue& properties) { |
| 143 ProfileProperties* profile = GetProfile(dbus::ObjectPath(profile_path), | 143 ProfileProperties* profile = GetProfile(dbus::ObjectPath(profile_path), |
| 144 ErrorCallback()); | 144 ErrorCallback()); |
| 145 DCHECK(profile); | 145 DCHECK(profile); |
| 146 profile->entries.SetWithoutPathExpansion(entry_path, | 146 profile->entries.SetWithoutPathExpansion(entry_path, |
| 147 properties.DeepCopy()); | 147 properties.DeepCopy()); |
| 148 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()-> | 148 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface()-> |
| 149 AddManagerService(entry_path, false /* visible */, false /* watch */); | 149 AddManagerService(entry_path, false /* visible */, false /* watch */); |
| 150 } | 150 } |
| 151 | 151 |
| 152 bool ShillProfileClientStub::AddService(const std::string& profile_path, | 152 bool FakeShillProfileClient::AddService(const std::string& profile_path, |
| 153 const std::string& service_path) { | 153 const std::string& service_path) { |
| 154 ProfileProperties* profile = GetProfile(dbus::ObjectPath(profile_path), | 154 ProfileProperties* profile = GetProfile(dbus::ObjectPath(profile_path), |
| 155 ErrorCallback()); | 155 ErrorCallback()); |
| 156 if (!profile) { | 156 if (!profile) { |
| 157 LOG(ERROR) << "No matching profile: " << profile_path; | 157 LOG(ERROR) << "No matching profile: " << profile_path; |
| 158 return false; | 158 return false; |
| 159 } | 159 } |
| 160 | 160 |
| 161 ShillServiceClient::TestInterface* service_test = | 161 ShillServiceClient::TestInterface* service_test = |
| 162 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface(); | 162 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 177 | 177 |
| 178 base::StringValue profile_path_value(profile_path); | 178 base::StringValue profile_path_value(profile_path); |
| 179 service_test->SetServiceProperty(service_path, | 179 service_test->SetServiceProperty(service_path, |
| 180 shill::kProfileProperty, | 180 shill::kProfileProperty, |
| 181 profile_path_value); | 181 profile_path_value); |
| 182 profile->entries.SetWithoutPathExpansion(service_path, | 182 profile->entries.SetWithoutPathExpansion(service_path, |
| 183 service_properties->DeepCopy()); | 183 service_properties->DeepCopy()); |
| 184 return true; | 184 return true; |
| 185 } | 185 } |
| 186 | 186 |
| 187 void ShillProfileClientStub::GetProfilePaths( | 187 void FakeShillProfileClient::GetProfilePaths( |
| 188 std::vector<std::string>* profiles) { | 188 std::vector<std::string>* profiles) { |
| 189 for (ProfileMap::iterator iter = profiles_.begin(); | 189 for (ProfileMap::iterator iter = profiles_.begin(); |
| 190 iter != profiles_.end(); ++iter) { | 190 iter != profiles_.end(); ++iter) { |
| 191 profiles->push_back(iter->first); | 191 profiles->push_back(iter->first); |
| 192 } | 192 } |
| 193 } | 193 } |
| 194 | 194 |
| 195 ShillProfileClientStub::ProfileProperties* ShillProfileClientStub::GetProfile( | 195 FakeShillProfileClient::ProfileProperties* FakeShillProfileClient::GetProfile( |
| 196 const dbus::ObjectPath& profile_path, | 196 const dbus::ObjectPath& profile_path, |
| 197 const ErrorCallback& error_callback) { | 197 const ErrorCallback& error_callback) { |
| 198 ProfileMap::const_iterator found = profiles_.find(profile_path.value()); | 198 ProfileMap::const_iterator found = profiles_.find(profile_path.value()); |
| 199 if (found == profiles_.end()) { | 199 if (found == profiles_.end()) { |
| 200 if (!error_callback.is_null()) | 200 if (!error_callback.is_null()) |
| 201 error_callback.Run("Error.InvalidProfile", "Invalid profile"); | 201 error_callback.Run("Error.InvalidProfile", "Invalid profile"); |
| 202 return NULL; | 202 return NULL; |
| 203 } | 203 } |
| 204 | 204 |
| 205 return found->second; | 205 return found->second; |
| 206 } | 206 } |
| 207 | 207 |
| 208 } // namespace chromeos | 208 } // namespace chromeos |
| OLD | NEW |