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