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