| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/shill_profile_client_stub.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.h" | 9 #include "base/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_property_changed_observer.h" | 14 #include "chromeos/dbus/shill_property_changed_observer.h" |
| 14 #include "chromeos/dbus/shill_service_client.h" | 15 #include "chromeos/dbus/shill_service_client.h" |
| 15 #include "dbus/bus.h" | 16 #include "dbus/bus.h" |
| 16 #include "dbus/message.h" | 17 #include "dbus/message.h" |
| 17 #include "dbus/object_path.h" | 18 #include "dbus/object_path.h" |
| 18 #include "dbus/values_util.h" | 19 #include "dbus/values_util.h" |
| 19 #include "third_party/cros_system_api/dbus/service_constants.h" | 20 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 20 | 21 |
| 22 // TODO(pneubeck): remove once cros_system_api DEP is rolled!!!!!!! |
| 23 // !!!!!!!!!!!! |
| 24 namespace shill { |
| 25 const char kUserHashProperty[] = "UserHash"; |
| 26 } |
| 27 |
| 21 namespace chromeos { | 28 namespace chromeos { |
| 22 | 29 |
| 30 struct ShillProfileClientStub::ProfileProperties { |
| 31 base::DictionaryValue entries; |
| 32 base::DictionaryValue properties; |
| 33 }; |
| 34 |
| 23 namespace { | 35 namespace { |
| 24 | 36 |
| 25 const char kSharedProfilePath[] = "/profile/default"; | 37 const char kSharedProfilePath[] = "/profile/default"; |
| 26 | 38 |
| 27 void PassEmptyDictionary( | 39 void PassEmptyDictionary( |
| 28 const ShillProfileClient::DictionaryValueCallbackWithoutStatus& callback) { | 40 const ShillProfileClient::DictionaryValueCallbackWithoutStatus& callback) { |
| 29 base::DictionaryValue dictionary; | 41 base::DictionaryValue dictionary; |
| 30 if (callback.is_null()) | 42 if (callback.is_null()) |
| 31 return; | 43 return; |
| 32 callback.Run(dictionary); | 44 callback.Run(dictionary); |
| 33 } | 45 } |
| 34 | 46 |
| 35 void PassDictionary( | 47 void PassDictionary( |
| 36 const ShillProfileClient::DictionaryValueCallbackWithoutStatus& callback, | 48 const ShillProfileClient::DictionaryValueCallbackWithoutStatus& callback, |
| 37 const base::DictionaryValue* dictionary) { | 49 const base::DictionaryValue* dictionary) { |
| 38 if (callback.is_null()) | 50 if (callback.is_null()) |
| 39 return; | 51 return; |
| 40 callback.Run(*dictionary); | 52 callback.Run(*dictionary); |
| 41 } | 53 } |
| 42 | 54 |
| 43 base::DictionaryValue* GetOrCreateDictionary(const std::string& key, | |
| 44 base::DictionaryValue* dict) { | |
| 45 base::DictionaryValue* nested_dict = NULL; | |
| 46 dict->GetDictionaryWithoutPathExpansion(key, &nested_dict); | |
| 47 if (!nested_dict) { | |
| 48 nested_dict = new base::DictionaryValue; | |
| 49 dict->SetWithoutPathExpansion(key, nested_dict); | |
| 50 } | |
| 51 return nested_dict; | |
| 52 } | |
| 53 | |
| 54 } // namespace | 55 } // namespace |
| 55 | 56 |
| 56 ShillProfileClientStub::ShillProfileClientStub() { | 57 ShillProfileClientStub::ShillProfileClientStub( |
| 57 AddProfile(kSharedProfilePath); | 58 ShillManagerClient::TestInterface* manager_test) |
| 59 : manager_test_(manager_test) { |
| 60 AddProfile(kSharedProfilePath, std::string()); |
| 58 } | 61 } |
| 59 | 62 |
| 60 ShillProfileClientStub::~ShillProfileClientStub() { | 63 ShillProfileClientStub::~ShillProfileClientStub() { |
| 64 STLDeleteValues(&profiles_); |
| 61 } | 65 } |
| 62 | 66 |
| 63 void ShillProfileClientStub::AddPropertyChangedObserver( | 67 void ShillProfileClientStub::AddPropertyChangedObserver( |
| 64 const dbus::ObjectPath& profile_path, | 68 const dbus::ObjectPath& profile_path, |
| 65 ShillPropertyChangedObserver* observer) { | 69 ShillPropertyChangedObserver* observer) { |
| 66 } | 70 } |
| 67 | 71 |
| 68 void ShillProfileClientStub::RemovePropertyChangedObserver( | 72 void ShillProfileClientStub::RemovePropertyChangedObserver( |
| 69 const dbus::ObjectPath& profile_path, | 73 const dbus::ObjectPath& profile_path, |
| 70 ShillPropertyChangedObserver* observer) { | 74 ShillPropertyChangedObserver* observer) { |
| 71 } | 75 } |
| 72 | 76 |
| 73 void ShillProfileClientStub::GetProperties( | 77 void ShillProfileClientStub::GetProperties( |
| 74 const dbus::ObjectPath& profile_path, | 78 const dbus::ObjectPath& profile_path, |
| 75 const DictionaryValueCallbackWithoutStatus& callback, | 79 const DictionaryValueCallbackWithoutStatus& callback, |
| 76 const ErrorCallback& error_callback) { | 80 const ErrorCallback& error_callback) { |
| 77 base::DictionaryValue* profile = GetProfile(profile_path, error_callback); | 81 ProfileProperties* profile = GetProfile(profile_path, error_callback); |
| 78 if (!profile) | 82 if (!profile) |
| 79 return; | 83 return; |
| 80 | 84 |
| 81 scoped_ptr<base::DictionaryValue> properties(new base::DictionaryValue); | 85 scoped_ptr<base::DictionaryValue> properties(profile->properties.DeepCopy()); |
| 82 base::ListValue* entry_paths = new base::ListValue; | 86 base::ListValue* entry_paths = new base::ListValue; |
| 83 properties->SetWithoutPathExpansion(flimflam::kEntriesProperty, entry_paths); | 87 properties->SetWithoutPathExpansion(flimflam::kEntriesProperty, entry_paths); |
| 84 for (base::DictionaryValue::Iterator it(*profile); !it.IsAtEnd(); | 88 for (base::DictionaryValue::Iterator it(profile->entries); !it.IsAtEnd(); |
| 85 it.Advance()) { | 89 it.Advance()) { |
| 86 entry_paths->AppendString(it.key()); | 90 entry_paths->AppendString(it.key()); |
| 87 } | 91 } |
| 88 | 92 |
| 89 MessageLoop::current()->PostTask( | 93 MessageLoop::current()->PostTask( |
| 90 FROM_HERE, | 94 FROM_HERE, |
| 91 base::Bind(&PassDictionary, callback, base::Owned(properties.release()))); | 95 base::Bind(&PassDictionary, callback, base::Owned(properties.release()))); |
| 92 } | 96 } |
| 93 | 97 |
| 94 void ShillProfileClientStub::GetEntry( | 98 void ShillProfileClientStub::GetEntry( |
| 95 const dbus::ObjectPath& profile_path, | 99 const dbus::ObjectPath& profile_path, |
| 96 const std::string& entry_path, | 100 const std::string& entry_path, |
| 97 const DictionaryValueCallbackWithoutStatus& callback, | 101 const DictionaryValueCallbackWithoutStatus& callback, |
| 98 const ErrorCallback& error_callback) { | 102 const ErrorCallback& error_callback) { |
| 99 base::DictionaryValue* profile = GetProfile(profile_path, error_callback); | 103 ProfileProperties* profile = GetProfile(profile_path, error_callback); |
| 100 if (!profile) | 104 if (!profile) |
| 101 return; | 105 return; |
| 102 | 106 |
| 103 base::DictionaryValue* entry = NULL; | 107 base::DictionaryValue* entry = NULL; |
| 104 profile->GetDictionaryWithoutPathExpansion(entry_path, &entry); | 108 profile->entries.GetDictionaryWithoutPathExpansion(entry_path, &entry); |
| 105 if (!entry) { | 109 if (!entry) { |
| 106 error_callback.Run("Error.InvalidProfileEntry", "Invalid profile entry"); | 110 error_callback.Run("Error.InvalidProfileEntry", "Invalid profile entry"); |
| 107 return; | 111 return; |
| 108 } | 112 } |
| 109 | 113 |
| 110 MessageLoop::current()->PostTask( | 114 MessageLoop::current()->PostTask( |
| 111 FROM_HERE, | 115 FROM_HERE, |
| 112 base::Bind(&PassDictionary, callback, base::Owned(entry->DeepCopy()))); | 116 base::Bind(&PassDictionary, callback, base::Owned(entry->DeepCopy()))); |
| 113 } | 117 } |
| 114 | 118 |
| 115 void ShillProfileClientStub::DeleteEntry(const dbus::ObjectPath& profile_path, | 119 void ShillProfileClientStub::DeleteEntry(const dbus::ObjectPath& profile_path, |
| 116 const std::string& entry_path, | 120 const std::string& entry_path, |
| 117 const base::Closure& callback, | 121 const base::Closure& callback, |
| 118 const ErrorCallback& error_callback) { | 122 const ErrorCallback& error_callback) { |
| 119 base::DictionaryValue* profile = GetProfile(profile_path, error_callback); | 123 ProfileProperties* profile = GetProfile(profile_path, error_callback); |
| 120 if (!profile) | 124 if (!profile) |
| 121 return; | 125 return; |
| 122 | 126 |
| 123 if (!profile->RemoveWithoutPathExpansion(entry_path, NULL)) { | 127 if (!profile->entries.RemoveWithoutPathExpansion(entry_path, NULL)) { |
| 124 error_callback.Run("Error.InvalidProfileEntry", "Invalid profile entry"); | 128 error_callback.Run("Error.InvalidProfileEntry", "Invalid profile entry"); |
| 125 return; | 129 return; |
| 126 } | 130 } |
| 127 | 131 |
| 128 MessageLoop::current()->PostTask(FROM_HERE, callback); | 132 MessageLoop::current()->PostTask(FROM_HERE, callback); |
| 129 } | 133 } |
| 130 | 134 |
| 131 ShillProfileClient::TestInterface* ShillProfileClientStub::GetTestInterface() { | 135 ShillProfileClient::TestInterface* ShillProfileClientStub::GetTestInterface() { |
| 132 return this; | 136 return this; |
| 133 } | 137 } |
| 134 | 138 |
| 135 void ShillProfileClientStub::AddProfile(const std::string& profile_path) { | 139 void ShillProfileClientStub::AddProfile(const std::string& profile_path, |
| 136 profile_entries_.SetWithoutPathExpansion(profile_path, | 140 const std::string& userhash) { |
| 137 new base::DictionaryValue); | 141 if (GetProfile(dbus::ObjectPath(profile_path), ErrorCallback())) |
| 142 return; |
| 143 |
| 144 ProfileProperties* profile = new ProfileProperties; |
| 145 profile->properties.SetStringWithoutPathExpansion(shill::kUserHashProperty, |
| 146 userhash); |
| 147 profiles_[profile_path] = profile; |
| 148 if (manager_test_) |
| 149 manager_test_->AddProfile(profile_path); |
| 138 } | 150 } |
| 139 | 151 |
| 140 void ShillProfileClientStub::AddEntry(const std::string& profile_path, | 152 void ShillProfileClientStub::AddEntry(const std::string& profile_path, |
| 141 const std::string& entry_path, | 153 const std::string& entry_path, |
| 142 const base::DictionaryValue& properties) { | 154 const base::DictionaryValue& properties) { |
| 143 base::DictionaryValue* profile = GetOrCreateDictionary(profile_path, | 155 ProfileProperties* profile = GetProfile(dbus::ObjectPath(profile_path), |
| 144 &profile_entries_); | 156 ErrorCallback()); |
| 145 profile->SetWithoutPathExpansion(entry_path, | 157 DCHECK(profile); |
| 146 properties.DeepCopy()); | 158 profile->entries.SetWithoutPathExpansion(entry_path, |
| 159 properties.DeepCopy()); |
| 147 } | 160 } |
| 148 | 161 |
| 149 bool ShillProfileClientStub::AddService(const std::string& service_path) { | 162 bool ShillProfileClientStub::AddService(const std::string& service_path) { |
| 150 ShillServiceClient::TestInterface* service_test = | 163 ShillServiceClient::TestInterface* service_test = |
| 151 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface(); | 164 DBusThreadManager::Get()->GetShillServiceClient()->GetTestInterface(); |
| 152 const base::DictionaryValue* properties = | 165 const base::DictionaryValue* properties = |
| 153 service_test->GetServiceProperties(service_path); | 166 service_test->GetServiceProperties(service_path); |
| 154 std::string profile_path; | 167 std::string profile_path; |
| 155 if (!properties) | 168 if (!properties) |
| 156 return false; | 169 return false; |
| 157 | 170 |
| 158 properties->GetStringWithoutPathExpansion(flimflam::kProfileProperty, | 171 properties->GetStringWithoutPathExpansion(flimflam::kProfileProperty, |
| 159 &profile_path); | 172 &profile_path); |
| 160 if (profile_path.empty()) | 173 if (profile_path.empty()) |
| 161 return false; | 174 return false; |
| 162 | 175 |
| 163 AddEntry(profile_path, service_path, *properties); | 176 AddEntry(profile_path, service_path, *properties); |
| 164 return true; | 177 return true; |
| 165 } | 178 } |
| 166 | 179 |
| 167 base::DictionaryValue* ShillProfileClientStub::GetProfile( | 180 ShillProfileClientStub::ProfileProperties* ShillProfileClientStub::GetProfile( |
| 168 const dbus::ObjectPath& profile_path, | 181 const dbus::ObjectPath& profile_path, |
| 169 const ErrorCallback& error_callback) { | 182 const ErrorCallback& error_callback) { |
| 170 base::DictionaryValue* profile = NULL; | 183 ProfileMap::const_iterator found = profiles_.find(profile_path.value()); |
| 171 profile_entries_.GetDictionaryWithoutPathExpansion(profile_path.value(), | 184 if (found == profiles_.end()) { |
| 172 &profile); | 185 if (!error_callback.is_null()) |
| 173 if (!profile) | 186 error_callback.Run("Error.InvalidProfile", "Invalid profile"); |
| 174 error_callback.Run("Error.InvalidProfile", "Invalid profile"); | 187 return NULL; |
| 175 return profile; | 188 } |
| 189 |
| 190 return found->second; |
| 176 } | 191 } |
| 177 | 192 |
| 178 } // namespace chromeos | 193 } // namespace chromeos |
| OLD | NEW |