Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chromeos-dbus-bindings/proxy_generator.h" |
| 6 | |
| 7 | #include <string> |
| 8 | #include <vector> |
| 9 | |
| 10 | #include <base/file_util.h> |
| 11 | #include <base/files/file_path.h> |
| 12 | #include <base/files/scoped_temp_dir.h> |
| 13 | #include <gtest/gtest.h> |
| 14 | |
| 15 | #include "chromeos-dbus-bindings/interface.h" |
| 16 | |
| 17 | using std::string; |
| 18 | using std::vector; |
| 19 | using testing::Test; |
| 20 | |
| 21 | namespace chromeos_dbus_bindings { |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | const char kInterfaceName[] = "org.chromium.TestInterface"; |
Alex Vakulenko | fafef13 | 2014-11-03 14:52:09 -0800 | [diff] [blame] | 26 | const char kInterfaceName2[] = "org.chromium.TestInterface2"; |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 27 | const char kMethod1Name[] = "Elements"; |
| 28 | const char kMethod1Return[] = "s"; |
| 29 | const char kMethod1Argument1[] = "s"; |
| 30 | const char kMethod1ArgumentName1[] = "space_walk"; |
| 31 | const char kMethod1Argument2[] = "ao"; |
| 32 | const char kMethod1ArgumentName2[] = "ramblin_man"; |
| 33 | const char kMethod2Name[] = "ReturnToPatagonia"; |
| 34 | const char kMethod2Return[] = "x"; |
| 35 | const char kMethod3Name[] = "NiceWeatherForDucks"; |
| 36 | const char kMethod3Argument1[] = "b"; |
| 37 | const char kMethod4Name[] = "ExperimentNumberSix"; |
Alex Vakulenko | fafef13 | 2014-11-03 14:52:09 -0800 | [diff] [blame] | 38 | const char kMethod5Name[] = "GetPersonInfo"; |
| 39 | const char kMethod5Argument1[] = "s"; |
| 40 | const char kMethod5ArgumentName1[] = "name"; |
| 41 | const char kMethod5Argument2[] = "i"; |
| 42 | const char kMethod5ArgumentName2[] = "age"; |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 43 | const char kSignal1Name[] = "Closer"; |
| 44 | const char kSignal2Name[] = "TheCurseOfKaZar"; |
| 45 | const char kSignal2Argument1[] = "as"; |
| 46 | const char kSignal2Argument2[] = "y"; |
| 47 | const char kExpectedContent[] = R"literal_string( |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame^] | 48 | #include <memory> |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 49 | #include <string> |
| 50 | #include <vector> |
| 51 | |
| 52 | #include <base/bind.h> |
| 53 | #include <base/callback.h> |
| 54 | #include <base/logging.h> |
| 55 | #include <base/macros.h> |
| 56 | #include <base/memory/ref_counted.h> |
| 57 | #include <chromeos/any.h> |
| 58 | #include <chromeos/dbus/dbus_method_invoker.h> |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame^] | 59 | #include <chromeos/dbus/dbus_property.h> |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 60 | #include <chromeos/dbus/dbus_signal_handler.h> |
| 61 | #include <chromeos/errors/error.h> |
Alex Vakulenko | 4f3c05e | 2014-11-12 15:01:46 -0800 | [diff] [blame] | 62 | #include <chromeos/variant_dictionary.h> |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 63 | #include <dbus/bus.h> |
| 64 | #include <dbus/message.h> |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame^] | 65 | #include <dbus/object_manager.h> |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 66 | #include <dbus/object_path.h> |
| 67 | #include <dbus/object_proxy.h> |
| 68 | |
| 69 | namespace org { |
| 70 | namespace chromium { |
| 71 | |
Alex Vakulenko | c95f06b | 2014-11-20 15:06:09 -0800 | [diff] [blame] | 72 | // Interface proxy for org::chromium::TestInterface. |
Alex Vakulenko | 4f3c05e | 2014-11-12 15:01:46 -0800 | [diff] [blame] | 73 | class TestInterfaceProxy final { |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 74 | public: |
| 75 | class SignalReceiver { |
| 76 | public: |
| 77 | virtual void OnCloserSignal() {} |
| 78 | virtual void OnTheCurseOfKaZarSignal( |
| 79 | const std::vector<std::string>&, |
| 80 | uint8_t) {} |
| 81 | }; |
Alex Vakulenko | 4f3c05e | 2014-11-12 15:01:46 -0800 | [diff] [blame] | 82 | |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 83 | TestInterfaceProxy( |
| 84 | const scoped_refptr<dbus::Bus>& bus, |
Alex Vakulenko | 6ef2d73 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 85 | const std::string& service_name) : |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame^] | 86 | bus_{bus}, |
| 87 | service_name_{service_name}, |
| 88 | dbus_object_proxy_{ |
| 89 | bus_->GetObjectProxy(service_name_, object_path_)} { |
Alex Vakulenko | c95f06b | 2014-11-20 15:06:09 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | TestInterfaceProxy( |
| 93 | const scoped_refptr<dbus::Bus>& bus, |
| 94 | const std::string& service_name, |
Alex Vakulenko | 6ef2d73 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 95 | SignalReceiver* signal_receiver) : |
| 96 | TestInterfaceProxy(bus, service_name) { |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 97 | chromeos::dbus_utils::ConnectToSignal( |
| 98 | dbus_object_proxy_, |
| 99 | "org.chromium.TestInterface", |
| 100 | "Closer", |
| 101 | base::Bind( |
| 102 | &SignalReceiver::OnCloserSignal, |
| 103 | base::Unretained(signal_receiver)), |
| 104 | base::Bind( |
| 105 | &TestInterfaceProxy::OnDBusSignalConnected, |
| 106 | base::Unretained(this))); |
| 107 | chromeos::dbus_utils::ConnectToSignal( |
| 108 | dbus_object_proxy_, |
| 109 | "org.chromium.TestInterface", |
| 110 | "TheCurseOfKaZar", |
| 111 | base::Bind( |
| 112 | &SignalReceiver::OnTheCurseOfKaZarSignal, |
| 113 | base::Unretained(signal_receiver)), |
| 114 | base::Bind( |
| 115 | &TestInterfaceProxy::OnDBusSignalConnected, |
| 116 | base::Unretained(this))); |
| 117 | } |
Alex Vakulenko | 4f3c05e | 2014-11-12 15:01:46 -0800 | [diff] [blame] | 118 | |
| 119 | ~TestInterfaceProxy() { |
Alex Vakulenko | c95f06b | 2014-11-20 15:06:09 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void ReleaseObjectProxy(const base::Closure& callback) { |
| 123 | bus_->RemoveObjectProxy(service_name_, object_path_, callback); |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 124 | } |
Alex Vakulenko | 4f3c05e | 2014-11-12 15:01:46 -0800 | [diff] [blame] | 125 | |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame^] | 126 | const dbus::ObjectPath& GetObjectPath() const { |
| 127 | return object_path_; |
| 128 | } |
| 129 | |
| 130 | dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; } |
| 131 | |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 132 | void OnDBusSignalConnected( |
| 133 | const std::string& interface, |
| 134 | const std::string& signal, |
| 135 | bool success) { |
| 136 | if (!success) { |
| 137 | LOG(ERROR) |
| 138 | << "Failed to connect to " << interface << "." << signal |
| 139 | << " for " << service_name_ << " at " |
| 140 | << object_path_.value(); |
| 141 | } |
| 142 | } |
Alex Vakulenko | 4f3c05e | 2014-11-12 15:01:46 -0800 | [diff] [blame] | 143 | |
| 144 | bool Elements( |
Alex Vakulenko | fafef13 | 2014-11-03 14:52:09 -0800 | [diff] [blame] | 145 | const std::string& in_space_walk, |
| 146 | const std::vector<dbus::ObjectPath>& in_ramblin_man, |
| 147 | std::string* out_3, |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 148 | chromeos::ErrorPtr* error) { |
| 149 | auto response = chromeos::dbus_utils::CallMethodAndBlock( |
| 150 | dbus_object_proxy_, |
| 151 | "org.chromium.TestInterface", |
| 152 | "Elements", |
| 153 | error, |
Alex Vakulenko | fafef13 | 2014-11-03 14:52:09 -0800 | [diff] [blame] | 154 | in_space_walk, |
| 155 | in_ramblin_man); |
| 156 | return response && chromeos::dbus_utils::ExtractMethodCallResults( |
| 157 | response.get(), error, out_3); |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 158 | } |
Alex Vakulenko | 4f3c05e | 2014-11-12 15:01:46 -0800 | [diff] [blame] | 159 | |
| 160 | bool ReturnToPatagonia( |
Alex Vakulenko | fafef13 | 2014-11-03 14:52:09 -0800 | [diff] [blame] | 161 | int64_t* out_1, |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 162 | chromeos::ErrorPtr* error) { |
| 163 | auto response = chromeos::dbus_utils::CallMethodAndBlock( |
| 164 | dbus_object_proxy_, |
| 165 | "org.chromium.TestInterface", |
| 166 | "ReturnToPatagonia", |
| 167 | error); |
Alex Vakulenko | fafef13 | 2014-11-03 14:52:09 -0800 | [diff] [blame] | 168 | return response && chromeos::dbus_utils::ExtractMethodCallResults( |
| 169 | response.get(), error, out_1); |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 170 | } |
Alex Vakulenko | 4f3c05e | 2014-11-12 15:01:46 -0800 | [diff] [blame] | 171 | |
| 172 | bool NiceWeatherForDucks( |
Alex Vakulenko | fafef13 | 2014-11-03 14:52:09 -0800 | [diff] [blame] | 173 | bool in_1, |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 174 | chromeos::ErrorPtr* error) { |
| 175 | auto response = chromeos::dbus_utils::CallMethodAndBlock( |
| 176 | dbus_object_proxy_, |
| 177 | "org.chromium.TestInterface", |
| 178 | "NiceWeatherForDucks", |
| 179 | error, |
Alex Vakulenko | fafef13 | 2014-11-03 14:52:09 -0800 | [diff] [blame] | 180 | in_1); |
| 181 | return response && chromeos::dbus_utils::ExtractMethodCallResults( |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 182 | response.get(), error); |
| 183 | } |
Alex Vakulenko | 4f3c05e | 2014-11-12 15:01:46 -0800 | [diff] [blame] | 184 | |
| 185 | // Comment line1 |
| 186 | // line2 |
| 187 | bool ExperimentNumberSix( |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 188 | chromeos::ErrorPtr* error) { |
| 189 | auto response = chromeos::dbus_utils::CallMethodAndBlock( |
| 190 | dbus_object_proxy_, |
| 191 | "org.chromium.TestInterface", |
| 192 | "ExperimentNumberSix", |
| 193 | error); |
Alex Vakulenko | fafef13 | 2014-11-03 14:52:09 -0800 | [diff] [blame] | 194 | return response && chromeos::dbus_utils::ExtractMethodCallResults( |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 195 | response.get(), error); |
| 196 | } |
Alex Vakulenko | 4f3c05e | 2014-11-12 15:01:46 -0800 | [diff] [blame] | 197 | |
Alex Vakulenko | c95f06b | 2014-11-20 15:06:09 -0800 | [diff] [blame] | 198 | private: |
| 199 | scoped_refptr<dbus::Bus> bus_; |
| 200 | std::string service_name_; |
Alex Vakulenko | 6ef2d73 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 201 | const dbus::ObjectPath object_path_{"/org/chromium/Test"}; |
Alex Vakulenko | c95f06b | 2014-11-20 15:06:09 -0800 | [diff] [blame] | 202 | dbus::ObjectProxy* dbus_object_proxy_; |
| 203 | |
| 204 | DISALLOW_COPY_AND_ASSIGN(TestInterfaceProxy); |
| 205 | }; |
| 206 | |
| 207 | } // namespace chromium |
| 208 | } // namespace org |
| 209 | |
| 210 | namespace org { |
| 211 | namespace chromium { |
| 212 | |
| 213 | // Interface proxy for org::chromium::TestInterface2. |
| 214 | class TestInterface2Proxy final { |
| 215 | public: |
| 216 | TestInterface2Proxy( |
| 217 | const scoped_refptr<dbus::Bus>& bus, |
| 218 | const std::string& service_name, |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame^] | 219 | const dbus::ObjectPath& object_path) : |
| 220 | bus_{bus}, |
| 221 | service_name_{service_name}, |
| 222 | object_path_{object_path}, |
| 223 | dbus_object_proxy_{ |
| 224 | bus_->GetObjectProxy(service_name_, object_path_)} { |
Alex Vakulenko | c95f06b | 2014-11-20 15:06:09 -0800 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | ~TestInterface2Proxy() { |
| 228 | } |
| 229 | |
| 230 | void ReleaseObjectProxy(const base::Closure& callback) { |
| 231 | bus_->RemoveObjectProxy(service_name_, object_path_, callback); |
| 232 | } |
| 233 | |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame^] | 234 | const dbus::ObjectPath& GetObjectPath() const { |
| 235 | return object_path_; |
| 236 | } |
| 237 | |
| 238 | dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; } |
| 239 | |
Alex Vakulenko | 4f3c05e | 2014-11-12 15:01:46 -0800 | [diff] [blame] | 240 | bool GetPersonInfo( |
Alex Vakulenko | fafef13 | 2014-11-03 14:52:09 -0800 | [diff] [blame] | 241 | std::string* out_name, |
| 242 | int32_t* out_age, |
| 243 | chromeos::ErrorPtr* error) { |
| 244 | auto response = chromeos::dbus_utils::CallMethodAndBlock( |
| 245 | dbus_object_proxy_, |
| 246 | "org.chromium.TestInterface2", |
| 247 | "GetPersonInfo", |
| 248 | error); |
| 249 | return response && chromeos::dbus_utils::ExtractMethodCallResults( |
| 250 | response.get(), error, out_name, out_age); |
| 251 | } |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 252 | |
| 253 | private: |
| 254 | scoped_refptr<dbus::Bus> bus_; |
| 255 | std::string service_name_; |
| 256 | dbus::ObjectPath object_path_; |
| 257 | dbus::ObjectProxy* dbus_object_proxy_; |
| 258 | |
Alex Vakulenko | c95f06b | 2014-11-20 15:06:09 -0800 | [diff] [blame] | 259 | DISALLOW_COPY_AND_ASSIGN(TestInterface2Proxy); |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 260 | }; |
| 261 | |
| 262 | } // namespace chromium |
| 263 | } // namespace org |
| 264 | )literal_string"; |
| 265 | |
Alex Vakulenko | 6ef2d73 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 266 | const char kExpectedContentWithService[] = R"literal_string( |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame^] | 267 | #include <memory> |
Alex Vakulenko | 6ef2d73 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 268 | #include <string> |
| 269 | #include <vector> |
| 270 | |
| 271 | #include <base/bind.h> |
| 272 | #include <base/callback.h> |
| 273 | #include <base/logging.h> |
| 274 | #include <base/macros.h> |
| 275 | #include <base/memory/ref_counted.h> |
| 276 | #include <chromeos/any.h> |
| 277 | #include <chromeos/dbus/dbus_method_invoker.h> |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame^] | 278 | #include <chromeos/dbus/dbus_property.h> |
Alex Vakulenko | 6ef2d73 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 279 | #include <chromeos/dbus/dbus_signal_handler.h> |
| 280 | #include <chromeos/errors/error.h> |
| 281 | #include <chromeos/variant_dictionary.h> |
| 282 | #include <dbus/bus.h> |
| 283 | #include <dbus/message.h> |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame^] | 284 | #include <dbus/object_manager.h> |
Alex Vakulenko | 6ef2d73 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 285 | #include <dbus/object_path.h> |
| 286 | #include <dbus/object_proxy.h> |
| 287 | |
| 288 | namespace org { |
| 289 | namespace chromium { |
| 290 | |
| 291 | // Interface proxy for org::chromium::TestInterface. |
| 292 | class TestInterfaceProxy final { |
| 293 | public: |
| 294 | class SignalReceiver { |
| 295 | public: |
| 296 | virtual void OnCloserSignal() {} |
| 297 | }; |
| 298 | |
| 299 | TestInterfaceProxy(const scoped_refptr<dbus::Bus>& bus) : |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame^] | 300 | bus_{bus}, |
| 301 | dbus_object_proxy_{ |
| 302 | bus_->GetObjectProxy(service_name_, object_path_)} { |
Alex Vakulenko | 6ef2d73 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | TestInterfaceProxy( |
| 306 | const scoped_refptr<dbus::Bus>& bus, |
| 307 | SignalReceiver* signal_receiver) : |
| 308 | TestInterfaceProxy(bus) { |
| 309 | chromeos::dbus_utils::ConnectToSignal( |
| 310 | dbus_object_proxy_, |
| 311 | "org.chromium.TestInterface", |
| 312 | "Closer", |
| 313 | base::Bind( |
| 314 | &SignalReceiver::OnCloserSignal, |
| 315 | base::Unretained(signal_receiver)), |
| 316 | base::Bind( |
| 317 | &TestInterfaceProxy::OnDBusSignalConnected, |
| 318 | base::Unretained(this))); |
| 319 | } |
| 320 | |
| 321 | ~TestInterfaceProxy() { |
| 322 | } |
| 323 | |
| 324 | void ReleaseObjectProxy(const base::Closure& callback) { |
| 325 | bus_->RemoveObjectProxy(service_name_, object_path_, callback); |
| 326 | } |
| 327 | |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame^] | 328 | const dbus::ObjectPath& GetObjectPath() const { |
| 329 | return object_path_; |
| 330 | } |
| 331 | |
| 332 | dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; } |
| 333 | |
Alex Vakulenko | 6ef2d73 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 334 | void OnDBusSignalConnected( |
| 335 | const std::string& interface, |
| 336 | const std::string& signal, |
| 337 | bool success) { |
| 338 | if (!success) { |
| 339 | LOG(ERROR) |
| 340 | << "Failed to connect to " << interface << "." << signal |
| 341 | << " for " << service_name_ << " at " |
| 342 | << object_path_.value(); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | private: |
| 347 | scoped_refptr<dbus::Bus> bus_; |
| 348 | const std::string service_name_{"org.chromium.Test"}; |
| 349 | const dbus::ObjectPath object_path_{"/org/chromium/Test"}; |
| 350 | dbus::ObjectProxy* dbus_object_proxy_; |
| 351 | |
| 352 | DISALLOW_COPY_AND_ASSIGN(TestInterfaceProxy); |
| 353 | }; |
| 354 | |
| 355 | } // namespace chromium |
| 356 | } // namespace org |
| 357 | |
| 358 | namespace org { |
| 359 | namespace chromium { |
| 360 | |
| 361 | // Interface proxy for org::chromium::TestInterface2. |
| 362 | class TestInterface2Proxy final { |
| 363 | public: |
| 364 | TestInterface2Proxy( |
| 365 | const scoped_refptr<dbus::Bus>& bus, |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame^] | 366 | const dbus::ObjectPath& object_path) : |
| 367 | bus_{bus}, |
| 368 | object_path_{object_path}, |
| 369 | dbus_object_proxy_{ |
| 370 | bus_->GetObjectProxy(service_name_, object_path_)} { |
Alex Vakulenko | 6ef2d73 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | ~TestInterface2Proxy() { |
| 374 | } |
| 375 | |
| 376 | void ReleaseObjectProxy(const base::Closure& callback) { |
| 377 | bus_->RemoveObjectProxy(service_name_, object_path_, callback); |
| 378 | } |
| 379 | |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame^] | 380 | const dbus::ObjectPath& GetObjectPath() const { |
| 381 | return object_path_; |
| 382 | } |
| 383 | |
| 384 | dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; } |
| 385 | |
Alex Vakulenko | 6ef2d73 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 386 | private: |
| 387 | scoped_refptr<dbus::Bus> bus_; |
| 388 | const std::string service_name_{"org.chromium.Test"}; |
| 389 | dbus::ObjectPath object_path_; |
| 390 | dbus::ObjectProxy* dbus_object_proxy_; |
| 391 | |
| 392 | DISALLOW_COPY_AND_ASSIGN(TestInterface2Proxy); |
| 393 | }; |
| 394 | |
| 395 | } // namespace chromium |
| 396 | } // namespace org |
Alex Vakulenko | 6ef2d73 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 397 | )literal_string"; |
| 398 | |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame^] | 399 | const char kExpectedContentWithObjectManager[] = R"literal_string( |
| 400 | #include <memory> |
| 401 | #include <string> |
| 402 | #include <vector> |
| 403 | |
| 404 | #include <base/bind.h> |
| 405 | #include <base/callback.h> |
| 406 | #include <base/logging.h> |
| 407 | #include <base/macros.h> |
| 408 | #include <base/memory/ref_counted.h> |
| 409 | #include <chromeos/any.h> |
| 410 | #include <chromeos/dbus/dbus_method_invoker.h> |
| 411 | #include <chromeos/dbus/dbus_property.h> |
| 412 | #include <chromeos/dbus/dbus_signal_handler.h> |
| 413 | #include <chromeos/errors/error.h> |
| 414 | #include <chromeos/variant_dictionary.h> |
| 415 | #include <dbus/bus.h> |
| 416 | #include <dbus/message.h> |
| 417 | #include <dbus/object_manager.h> |
| 418 | #include <dbus/object_path.h> |
| 419 | #include <dbus/object_proxy.h> |
| 420 | |
| 421 | namespace org { |
| 422 | namespace chromium { |
| 423 | class ObjectManagerProxy; |
| 424 | } // namespace chromium |
| 425 | } // namespace org |
| 426 | |
| 427 | namespace org { |
| 428 | namespace chromium { |
| 429 | |
| 430 | // Interface proxy for org::chromium::Itf1. |
| 431 | class Itf1Proxy final { |
| 432 | public: |
| 433 | class SignalReceiver { |
| 434 | public: |
| 435 | virtual void OnCloserSignal() {} |
| 436 | }; |
| 437 | |
| 438 | class PropertySet : public dbus::PropertySet { |
| 439 | public: |
| 440 | PropertySet(dbus::ObjectProxy* object_proxy, |
| 441 | const PropertyChangedCallback& callback) |
| 442 | : dbus::PropertySet{object_proxy, |
| 443 | "org.chromium.Itf1", |
| 444 | callback} { |
| 445 | RegisterProperty("data", &data); |
| 446 | } |
| 447 | |
| 448 | chromeos::dbus_utils::Property<std::string> data; |
| 449 | |
| 450 | private: |
| 451 | DISALLOW_COPY_AND_ASSIGN(PropertySet); |
| 452 | }; |
| 453 | |
| 454 | Itf1Proxy( |
| 455 | const scoped_refptr<dbus::Bus>& bus, |
| 456 | const std::string& service_name, |
| 457 | PropertySet* property_set) : |
| 458 | bus_{bus}, |
| 459 | service_name_{service_name}, |
| 460 | property_set_{property_set}, |
| 461 | dbus_object_proxy_{ |
| 462 | bus_->GetObjectProxy(service_name_, object_path_)} { |
| 463 | } |
| 464 | |
| 465 | Itf1Proxy( |
| 466 | const scoped_refptr<dbus::Bus>& bus, |
| 467 | const std::string& service_name, |
| 468 | PropertySet* property_set, |
| 469 | SignalReceiver* signal_receiver) : |
| 470 | Itf1Proxy(bus, service_name, property_set) { |
| 471 | chromeos::dbus_utils::ConnectToSignal( |
| 472 | dbus_object_proxy_, |
| 473 | "org.chromium.Itf1", |
| 474 | "Closer", |
| 475 | base::Bind( |
| 476 | &SignalReceiver::OnCloserSignal, |
| 477 | base::Unretained(signal_receiver)), |
| 478 | base::Bind( |
| 479 | &Itf1Proxy::OnDBusSignalConnected, |
| 480 | base::Unretained(this))); |
| 481 | } |
| 482 | |
| 483 | ~Itf1Proxy() { |
| 484 | } |
| 485 | |
| 486 | void ReleaseObjectProxy(const base::Closure& callback) { |
| 487 | bus_->RemoveObjectProxy(service_name_, object_path_, callback); |
| 488 | } |
| 489 | |
| 490 | const dbus::ObjectPath& GetObjectPath() const { |
| 491 | return object_path_; |
| 492 | } |
| 493 | |
| 494 | dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; } |
| 495 | |
| 496 | void SetPropertyChangedCallback( |
| 497 | const base::Callback<void(Itf1Proxy*, const std::string&)>& callback) { |
| 498 | on_property_changed_ = callback; |
| 499 | } |
| 500 | |
| 501 | const PropertySet* GetProperties() const { return property_set_; } |
| 502 | PropertySet* GetProperties() { return property_set_; } |
| 503 | |
| 504 | void OnDBusSignalConnected( |
| 505 | const std::string& interface, |
| 506 | const std::string& signal, |
| 507 | bool success) { |
| 508 | if (!success) { |
| 509 | LOG(ERROR) |
| 510 | << "Failed to connect to " << interface << "." << signal |
| 511 | << " for " << service_name_ << " at " |
| 512 | << object_path_.value(); |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | const std::string& data() const { |
| 517 | return property_set_->data.value(); |
| 518 | } |
| 519 | |
| 520 | private: |
| 521 | void OnPropertyChanged(const std::string& property_name) { |
| 522 | if (!on_property_changed_.is_null()) |
| 523 | on_property_changed_.Run(this, property_name); |
| 524 | } |
| 525 | |
| 526 | scoped_refptr<dbus::Bus> bus_; |
| 527 | std::string service_name_; |
| 528 | const dbus::ObjectPath object_path_{"/org/chromium/Test/Object"}; |
| 529 | PropertySet* property_set_; |
| 530 | base::Callback<void(Itf1Proxy*, const std::string&)> on_property_changed_; |
| 531 | dbus::ObjectProxy* dbus_object_proxy_; |
| 532 | |
| 533 | friend class org::chromium::ObjectManagerProxy; |
| 534 | DISALLOW_COPY_AND_ASSIGN(Itf1Proxy); |
| 535 | }; |
| 536 | |
| 537 | } // namespace chromium |
| 538 | } // namespace org |
| 539 | |
| 540 | namespace org { |
| 541 | namespace chromium { |
| 542 | |
| 543 | // Interface proxy for org::chromium::Itf2. |
| 544 | class Itf2Proxy final { |
| 545 | public: |
| 546 | class PropertySet : public dbus::PropertySet { |
| 547 | public: |
| 548 | PropertySet(dbus::ObjectProxy* object_proxy, |
| 549 | const PropertyChangedCallback& callback) |
| 550 | : dbus::PropertySet{object_proxy, |
| 551 | "org.chromium.Itf2", |
| 552 | callback} { |
| 553 | } |
| 554 | |
| 555 | |
| 556 | private: |
| 557 | DISALLOW_COPY_AND_ASSIGN(PropertySet); |
| 558 | }; |
| 559 | |
| 560 | Itf2Proxy( |
| 561 | const scoped_refptr<dbus::Bus>& bus, |
| 562 | const std::string& service_name, |
| 563 | const dbus::ObjectPath& object_path) : |
| 564 | bus_{bus}, |
| 565 | service_name_{service_name}, |
| 566 | object_path_{object_path}, |
| 567 | dbus_object_proxy_{ |
| 568 | bus_->GetObjectProxy(service_name_, object_path_)} { |
| 569 | } |
| 570 | |
| 571 | ~Itf2Proxy() { |
| 572 | } |
| 573 | |
| 574 | void ReleaseObjectProxy(const base::Closure& callback) { |
| 575 | bus_->RemoveObjectProxy(service_name_, object_path_, callback); |
| 576 | } |
| 577 | |
| 578 | const dbus::ObjectPath& GetObjectPath() const { |
| 579 | return object_path_; |
| 580 | } |
| 581 | |
| 582 | dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; } |
| 583 | |
| 584 | private: |
| 585 | scoped_refptr<dbus::Bus> bus_; |
| 586 | std::string service_name_; |
| 587 | dbus::ObjectPath object_path_; |
| 588 | dbus::ObjectProxy* dbus_object_proxy_; |
| 589 | |
| 590 | DISALLOW_COPY_AND_ASSIGN(Itf2Proxy); |
| 591 | }; |
| 592 | |
| 593 | } // namespace chromium |
| 594 | } // namespace org |
| 595 | |
| 596 | namespace org { |
| 597 | namespace chromium { |
| 598 | |
| 599 | class ObjectManagerProxy : public dbus::ObjectManager::Interface { |
| 600 | public: |
| 601 | ObjectManagerProxy(const scoped_refptr<dbus::Bus>& bus, |
| 602 | const std::string& service_name) |
| 603 | : bus_{bus}, |
| 604 | dbus_object_manager_{bus->GetObjectManager( |
| 605 | service_name, |
| 606 | dbus::ObjectPath{"/org/chromium/Test"})} { |
| 607 | dbus_object_manager_->RegisterInterface("org.chromium.Itf1", this); |
| 608 | dbus_object_manager_->RegisterInterface("org.chromium.Itf2", this); |
| 609 | } |
| 610 | |
| 611 | dbus::ObjectManager* GetObjectManagerProxy() const { |
| 612 | return dbus_object_manager_; |
| 613 | } |
| 614 | |
| 615 | org::chromium::Itf1Proxy* GetItf1Proxy( |
| 616 | const dbus::ObjectPath& object_path) { |
| 617 | auto p = itf1_instances_.find(object_path); |
| 618 | if (p != itf1_instances_.end()) |
| 619 | return p->second.get(); |
| 620 | return nullptr; |
| 621 | } |
| 622 | std::vector<org::chromium::Itf1Proxy*> GetItf1Instances() const { |
| 623 | std::vector<org::chromium::Itf1Proxy*> values; |
| 624 | values.reserve(itf1_instances_.size()); |
| 625 | for (const auto& pair : itf1_instances_) |
| 626 | values.push_back(pair.second.get()); |
| 627 | return values; |
| 628 | } |
| 629 | void SetItf1AddedCallback( |
| 630 | const base::Callback<void(org::chromium::Itf1Proxy*)>& callback) { |
| 631 | on_itf1_added_ = callback; |
| 632 | } |
| 633 | void SetItf1RemovedCallback( |
| 634 | const base::Callback<void(const dbus::ObjectPath&)>& callback) { |
| 635 | on_itf1_removed_ = callback; |
| 636 | } |
| 637 | |
| 638 | org::chromium::Itf2Proxy* GetItf2Proxy( |
| 639 | const dbus::ObjectPath& object_path) { |
| 640 | auto p = itf2_instances_.find(object_path); |
| 641 | if (p != itf2_instances_.end()) |
| 642 | return p->second.get(); |
| 643 | return nullptr; |
| 644 | } |
| 645 | std::vector<org::chromium::Itf2Proxy*> GetItf2Instances() const { |
| 646 | std::vector<org::chromium::Itf2Proxy*> values; |
| 647 | values.reserve(itf2_instances_.size()); |
| 648 | for (const auto& pair : itf2_instances_) |
| 649 | values.push_back(pair.second.get()); |
| 650 | return values; |
| 651 | } |
| 652 | void SetItf2AddedCallback( |
| 653 | const base::Callback<void(org::chromium::Itf2Proxy*)>& callback) { |
| 654 | on_itf2_added_ = callback; |
| 655 | } |
| 656 | void SetItf2RemovedCallback( |
| 657 | const base::Callback<void(const dbus::ObjectPath&)>& callback) { |
| 658 | on_itf2_removed_ = callback; |
| 659 | } |
| 660 | |
| 661 | private: |
| 662 | void OnPropertyChanged(const dbus::ObjectPath& object_path, |
| 663 | const std::string& interface_name, |
| 664 | const std::string& property_name) { |
| 665 | if (interface_name == "org.chromium.Itf1") { |
| 666 | auto p = itf1_instances_.find(object_path); |
| 667 | if (p == itf1_instances_.end()) |
| 668 | return; |
| 669 | p->second->OnPropertyChanged(property_name); |
| 670 | return; |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | void ObjectAdded( |
| 675 | const dbus::ObjectPath& object_path, |
| 676 | const std::string& interface_name) override { |
| 677 | if (interface_name == "org.chromium.Itf1") { |
| 678 | auto property_set = |
| 679 | static_cast<org::chromium::Itf1Proxy::PropertySet*>( |
| 680 | dbus_object_manager_->GetProperties(object_path, interface_name)); |
| 681 | std::unique_ptr<org::chromium::Itf1Proxy> itf1_proxy{ |
| 682 | new org::chromium::Itf1Proxy{bus_, property_set} |
| 683 | }; |
| 684 | auto p = itf1_instances_.emplace(object_path, std::move(itf1_proxy)); |
| 685 | if (!on_itf1_added_.is_null()) |
| 686 | on_itf1_added_.Run(p.first->second.get()); |
| 687 | return; |
| 688 | } |
| 689 | if (interface_name == "org.chromium.Itf2") { |
| 690 | std::unique_ptr<org::chromium::Itf2Proxy> itf2_proxy{ |
| 691 | new org::chromium::Itf2Proxy{bus_, object_path} |
| 692 | }; |
| 693 | auto p = itf2_instances_.emplace(object_path, std::move(itf2_proxy)); |
| 694 | if (!on_itf2_added_.is_null()) |
| 695 | on_itf2_added_.Run(p.first->second.get()); |
| 696 | return; |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | void ObjectRemoved( |
| 701 | const dbus::ObjectPath& object_path, |
| 702 | const std::string& interface_name) override { |
| 703 | if (interface_name == "org.chromium.Itf1") { |
| 704 | auto p = itf1_instances_.find(object_path); |
| 705 | if (p != itf1_instances_.end()) { |
| 706 | if (!on_itf1_removed_.is_null()) |
| 707 | on_itf1_removed_.Run(object_path); |
| 708 | itf1_instances_.erase(p); |
| 709 | } |
| 710 | return; |
| 711 | } |
| 712 | if (interface_name == "org.chromium.Itf2") { |
| 713 | auto p = itf2_instances_.find(object_path); |
| 714 | if (p != itf2_instances_.end()) { |
| 715 | if (!on_itf2_removed_.is_null()) |
| 716 | on_itf2_removed_.Run(object_path); |
| 717 | itf2_instances_.erase(p); |
| 718 | } |
| 719 | return; |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | dbus::PropertySet* CreateProperties( |
| 724 | dbus::ObjectProxy* object_proxy, |
| 725 | const dbus::ObjectPath& object_path, |
| 726 | const std::string& interface_name) override { |
| 727 | if (interface_name == "org.chromium.Itf1") { |
| 728 | return new org::chromium::Itf1Proxy::PropertySet{ |
| 729 | object_proxy, |
| 730 | base::Bind(&ObjectManagerProxy::OnPropertyChanged, |
| 731 | weak_ptr_factory_.GetWeakPtr(), |
| 732 | object_path, |
| 733 | interface_name) |
| 734 | }; |
| 735 | } |
| 736 | if (interface_name == "org.chromium.Itf2") { |
| 737 | return new org::chromium::Itf2Proxy::PropertySet{ |
| 738 | object_proxy, |
| 739 | base::Bind(&ObjectManagerProxy::OnPropertyChanged, |
| 740 | weak_ptr_factory_.GetWeakPtr(), |
| 741 | object_path, |
| 742 | interface_name) |
| 743 | }; |
| 744 | } |
| 745 | LOG(FATAL) << "Creating properties for unsupported interface " |
| 746 | << interface_name; |
| 747 | return nullptr; |
| 748 | } |
| 749 | |
| 750 | scoped_refptr<dbus::Bus> bus_; |
| 751 | dbus::ObjectManager* dbus_object_manager_; |
| 752 | std::map<dbus::ObjectPath, |
| 753 | std::unique_ptr<org::chromium::Itf1Proxy>> itf1_instances_; |
| 754 | base::Callback<void(org::chromium::Itf1Proxy*)> on_itf1_added_; |
| 755 | base::Callback<void(const dbus::ObjectPath&)> on_itf1_removed_; |
| 756 | std::map<dbus::ObjectPath, |
| 757 | std::unique_ptr<org::chromium::Itf2Proxy>> itf2_instances_; |
| 758 | base::Callback<void(org::chromium::Itf2Proxy*)> on_itf2_added_; |
| 759 | base::Callback<void(const dbus::ObjectPath&)> on_itf2_removed_; |
| 760 | base::WeakPtrFactory<ObjectManagerProxy> weak_ptr_factory_{this}; |
| 761 | |
| 762 | DISALLOW_COPY_AND_ASSIGN(ObjectManagerProxy); |
| 763 | }; |
| 764 | |
| 765 | } // namespace chromium |
| 766 | } // namespace org |
| 767 | )literal_string"; |
| 768 | |
| 769 | const char kExpectedContentWithObjectManagerAndServiceName[] = R"literal_string( |
| 770 | #include <memory> |
| 771 | #include <string> |
| 772 | #include <vector> |
| 773 | |
| 774 | #include <base/bind.h> |
| 775 | #include <base/callback.h> |
| 776 | #include <base/logging.h> |
| 777 | #include <base/macros.h> |
| 778 | #include <base/memory/ref_counted.h> |
| 779 | #include <chromeos/any.h> |
| 780 | #include <chromeos/dbus/dbus_method_invoker.h> |
| 781 | #include <chromeos/dbus/dbus_property.h> |
| 782 | #include <chromeos/dbus/dbus_signal_handler.h> |
| 783 | #include <chromeos/errors/error.h> |
| 784 | #include <chromeos/variant_dictionary.h> |
| 785 | #include <dbus/bus.h> |
| 786 | #include <dbus/message.h> |
| 787 | #include <dbus/object_manager.h> |
| 788 | #include <dbus/object_path.h> |
| 789 | #include <dbus/object_proxy.h> |
| 790 | |
| 791 | namespace org { |
| 792 | namespace chromium { |
| 793 | class ObjectManagerProxy; |
| 794 | } // namespace chromium |
| 795 | } // namespace org |
| 796 | |
| 797 | namespace org { |
| 798 | namespace chromium { |
| 799 | |
| 800 | // Interface proxy for org::chromium::Itf1. |
| 801 | class Itf1Proxy final { |
| 802 | public: |
| 803 | class SignalReceiver { |
| 804 | public: |
| 805 | virtual void OnCloserSignal() {} |
| 806 | }; |
| 807 | |
| 808 | class PropertySet : public dbus::PropertySet { |
| 809 | public: |
| 810 | PropertySet(dbus::ObjectProxy* object_proxy, |
| 811 | const PropertyChangedCallback& callback) |
| 812 | : dbus::PropertySet{object_proxy, |
| 813 | "org.chromium.Itf1", |
| 814 | callback} { |
| 815 | } |
| 816 | |
| 817 | |
| 818 | private: |
| 819 | DISALLOW_COPY_AND_ASSIGN(PropertySet); |
| 820 | }; |
| 821 | |
| 822 | Itf1Proxy(const scoped_refptr<dbus::Bus>& bus) : |
| 823 | bus_{bus}, |
| 824 | dbus_object_proxy_{ |
| 825 | bus_->GetObjectProxy(service_name_, object_path_)} { |
| 826 | } |
| 827 | |
| 828 | Itf1Proxy( |
| 829 | const scoped_refptr<dbus::Bus>& bus, |
| 830 | SignalReceiver* signal_receiver) : |
| 831 | Itf1Proxy(bus) { |
| 832 | chromeos::dbus_utils::ConnectToSignal( |
| 833 | dbus_object_proxy_, |
| 834 | "org.chromium.Itf1", |
| 835 | "Closer", |
| 836 | base::Bind( |
| 837 | &SignalReceiver::OnCloserSignal, |
| 838 | base::Unretained(signal_receiver)), |
| 839 | base::Bind( |
| 840 | &Itf1Proxy::OnDBusSignalConnected, |
| 841 | base::Unretained(this))); |
| 842 | } |
| 843 | |
| 844 | ~Itf1Proxy() { |
| 845 | } |
| 846 | |
| 847 | void ReleaseObjectProxy(const base::Closure& callback) { |
| 848 | bus_->RemoveObjectProxy(service_name_, object_path_, callback); |
| 849 | } |
| 850 | |
| 851 | const dbus::ObjectPath& GetObjectPath() const { |
| 852 | return object_path_; |
| 853 | } |
| 854 | |
| 855 | dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; } |
| 856 | |
| 857 | void OnDBusSignalConnected( |
| 858 | const std::string& interface, |
| 859 | const std::string& signal, |
| 860 | bool success) { |
| 861 | if (!success) { |
| 862 | LOG(ERROR) |
| 863 | << "Failed to connect to " << interface << "." << signal |
| 864 | << " for " << service_name_ << " at " |
| 865 | << object_path_.value(); |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | private: |
| 870 | scoped_refptr<dbus::Bus> bus_; |
| 871 | const std::string service_name_{"org.chromium.Test"}; |
| 872 | const dbus::ObjectPath object_path_{"/org/chromium/Test/Object"}; |
| 873 | dbus::ObjectProxy* dbus_object_proxy_; |
| 874 | |
| 875 | DISALLOW_COPY_AND_ASSIGN(Itf1Proxy); |
| 876 | }; |
| 877 | |
| 878 | } // namespace chromium |
| 879 | } // namespace org |
| 880 | |
| 881 | namespace org { |
| 882 | namespace chromium { |
| 883 | |
| 884 | // Interface proxy for org::chromium::Itf2. |
| 885 | class Itf2Proxy final { |
| 886 | public: |
| 887 | class PropertySet : public dbus::PropertySet { |
| 888 | public: |
| 889 | PropertySet(dbus::ObjectProxy* object_proxy, |
| 890 | const PropertyChangedCallback& callback) |
| 891 | : dbus::PropertySet{object_proxy, |
| 892 | "org.chromium.Itf2", |
| 893 | callback} { |
| 894 | } |
| 895 | |
| 896 | |
| 897 | private: |
| 898 | DISALLOW_COPY_AND_ASSIGN(PropertySet); |
| 899 | }; |
| 900 | |
| 901 | Itf2Proxy( |
| 902 | const scoped_refptr<dbus::Bus>& bus, |
| 903 | const dbus::ObjectPath& object_path) : |
| 904 | bus_{bus}, |
| 905 | object_path_{object_path}, |
| 906 | dbus_object_proxy_{ |
| 907 | bus_->GetObjectProxy(service_name_, object_path_)} { |
| 908 | } |
| 909 | |
| 910 | ~Itf2Proxy() { |
| 911 | } |
| 912 | |
| 913 | void ReleaseObjectProxy(const base::Closure& callback) { |
| 914 | bus_->RemoveObjectProxy(service_name_, object_path_, callback); |
| 915 | } |
| 916 | |
| 917 | const dbus::ObjectPath& GetObjectPath() const { |
| 918 | return object_path_; |
| 919 | } |
| 920 | |
| 921 | dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; } |
| 922 | |
| 923 | private: |
| 924 | scoped_refptr<dbus::Bus> bus_; |
| 925 | const std::string service_name_{"org.chromium.Test"}; |
| 926 | dbus::ObjectPath object_path_; |
| 927 | dbus::ObjectProxy* dbus_object_proxy_; |
| 928 | |
| 929 | DISALLOW_COPY_AND_ASSIGN(Itf2Proxy); |
| 930 | }; |
| 931 | |
| 932 | } // namespace chromium |
| 933 | } // namespace org |
| 934 | |
| 935 | namespace org { |
| 936 | namespace chromium { |
| 937 | |
| 938 | class ObjectManagerProxy : public dbus::ObjectManager::Interface { |
| 939 | public: |
| 940 | ObjectManagerProxy(const scoped_refptr<dbus::Bus>& bus) |
| 941 | : bus_{bus}, |
| 942 | dbus_object_manager_{bus->GetObjectManager( |
| 943 | "org.chromium.Test", |
| 944 | dbus::ObjectPath{"/org/chromium/Test"})} { |
| 945 | dbus_object_manager_->RegisterInterface("org.chromium.Itf1", this); |
| 946 | dbus_object_manager_->RegisterInterface("org.chromium.Itf2", this); |
| 947 | } |
| 948 | |
| 949 | dbus::ObjectManager* GetObjectManagerProxy() const { |
| 950 | return dbus_object_manager_; |
| 951 | } |
| 952 | |
| 953 | org::chromium::Itf1Proxy* GetItf1Proxy( |
| 954 | const dbus::ObjectPath& object_path) { |
| 955 | auto p = itf1_instances_.find(object_path); |
| 956 | if (p != itf1_instances_.end()) |
| 957 | return p->second.get(); |
| 958 | return nullptr; |
| 959 | } |
| 960 | std::vector<org::chromium::Itf1Proxy*> GetItf1Instances() const { |
| 961 | std::vector<org::chromium::Itf1Proxy*> values; |
| 962 | values.reserve(itf1_instances_.size()); |
| 963 | for (const auto& pair : itf1_instances_) |
| 964 | values.push_back(pair.second.get()); |
| 965 | return values; |
| 966 | } |
| 967 | void SetItf1AddedCallback( |
| 968 | const base::Callback<void(org::chromium::Itf1Proxy*)>& callback) { |
| 969 | on_itf1_added_ = callback; |
| 970 | } |
| 971 | void SetItf1RemovedCallback( |
| 972 | const base::Callback<void(const dbus::ObjectPath&)>& callback) { |
| 973 | on_itf1_removed_ = callback; |
| 974 | } |
| 975 | |
| 976 | org::chromium::Itf2Proxy* GetItf2Proxy( |
| 977 | const dbus::ObjectPath& object_path) { |
| 978 | auto p = itf2_instances_.find(object_path); |
| 979 | if (p != itf2_instances_.end()) |
| 980 | return p->second.get(); |
| 981 | return nullptr; |
| 982 | } |
| 983 | std::vector<org::chromium::Itf2Proxy*> GetItf2Instances() const { |
| 984 | std::vector<org::chromium::Itf2Proxy*> values; |
| 985 | values.reserve(itf2_instances_.size()); |
| 986 | for (const auto& pair : itf2_instances_) |
| 987 | values.push_back(pair.second.get()); |
| 988 | return values; |
| 989 | } |
| 990 | void SetItf2AddedCallback( |
| 991 | const base::Callback<void(org::chromium::Itf2Proxy*)>& callback) { |
| 992 | on_itf2_added_ = callback; |
| 993 | } |
| 994 | void SetItf2RemovedCallback( |
| 995 | const base::Callback<void(const dbus::ObjectPath&)>& callback) { |
| 996 | on_itf2_removed_ = callback; |
| 997 | } |
| 998 | |
| 999 | private: |
| 1000 | void OnPropertyChanged(const dbus::ObjectPath& object_path, |
| 1001 | const std::string& interface_name, |
| 1002 | const std::string& property_name) { |
| 1003 | } |
| 1004 | |
| 1005 | void ObjectAdded( |
| 1006 | const dbus::ObjectPath& object_path, |
| 1007 | const std::string& interface_name) override { |
| 1008 | if (interface_name == "org.chromium.Itf1") { |
| 1009 | std::unique_ptr<org::chromium::Itf1Proxy> itf1_proxy{ |
| 1010 | new org::chromium::Itf1Proxy{bus_} |
| 1011 | }; |
| 1012 | auto p = itf1_instances_.emplace(object_path, std::move(itf1_proxy)); |
| 1013 | if (!on_itf1_added_.is_null()) |
| 1014 | on_itf1_added_.Run(p.first->second.get()); |
| 1015 | return; |
| 1016 | } |
| 1017 | if (interface_name == "org.chromium.Itf2") { |
| 1018 | std::unique_ptr<org::chromium::Itf2Proxy> itf2_proxy{ |
| 1019 | new org::chromium::Itf2Proxy{bus_, object_path} |
| 1020 | }; |
| 1021 | auto p = itf2_instances_.emplace(object_path, std::move(itf2_proxy)); |
| 1022 | if (!on_itf2_added_.is_null()) |
| 1023 | on_itf2_added_.Run(p.first->second.get()); |
| 1024 | return; |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | void ObjectRemoved( |
| 1029 | const dbus::ObjectPath& object_path, |
| 1030 | const std::string& interface_name) override { |
| 1031 | if (interface_name == "org.chromium.Itf1") { |
| 1032 | auto p = itf1_instances_.find(object_path); |
| 1033 | if (p != itf1_instances_.end()) { |
| 1034 | if (!on_itf1_removed_.is_null()) |
| 1035 | on_itf1_removed_.Run(object_path); |
| 1036 | itf1_instances_.erase(p); |
| 1037 | } |
| 1038 | return; |
| 1039 | } |
| 1040 | if (interface_name == "org.chromium.Itf2") { |
| 1041 | auto p = itf2_instances_.find(object_path); |
| 1042 | if (p != itf2_instances_.end()) { |
| 1043 | if (!on_itf2_removed_.is_null()) |
| 1044 | on_itf2_removed_.Run(object_path); |
| 1045 | itf2_instances_.erase(p); |
| 1046 | } |
| 1047 | return; |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | dbus::PropertySet* CreateProperties( |
| 1052 | dbus::ObjectProxy* object_proxy, |
| 1053 | const dbus::ObjectPath& object_path, |
| 1054 | const std::string& interface_name) override { |
| 1055 | if (interface_name == "org.chromium.Itf1") { |
| 1056 | return new org::chromium::Itf1Proxy::PropertySet{ |
| 1057 | object_proxy, |
| 1058 | base::Bind(&ObjectManagerProxy::OnPropertyChanged, |
| 1059 | weak_ptr_factory_.GetWeakPtr(), |
| 1060 | object_path, |
| 1061 | interface_name) |
| 1062 | }; |
| 1063 | } |
| 1064 | if (interface_name == "org.chromium.Itf2") { |
| 1065 | return new org::chromium::Itf2Proxy::PropertySet{ |
| 1066 | object_proxy, |
| 1067 | base::Bind(&ObjectManagerProxy::OnPropertyChanged, |
| 1068 | weak_ptr_factory_.GetWeakPtr(), |
| 1069 | object_path, |
| 1070 | interface_name) |
| 1071 | }; |
| 1072 | } |
| 1073 | LOG(FATAL) << "Creating properties for unsupported interface " |
| 1074 | << interface_name; |
| 1075 | return nullptr; |
| 1076 | } |
| 1077 | |
| 1078 | scoped_refptr<dbus::Bus> bus_; |
| 1079 | dbus::ObjectManager* dbus_object_manager_; |
| 1080 | std::map<dbus::ObjectPath, |
| 1081 | std::unique_ptr<org::chromium::Itf1Proxy>> itf1_instances_; |
| 1082 | base::Callback<void(org::chromium::Itf1Proxy*)> on_itf1_added_; |
| 1083 | base::Callback<void(const dbus::ObjectPath&)> on_itf1_removed_; |
| 1084 | std::map<dbus::ObjectPath, |
| 1085 | std::unique_ptr<org::chromium::Itf2Proxy>> itf2_instances_; |
| 1086 | base::Callback<void(org::chromium::Itf2Proxy*)> on_itf2_added_; |
| 1087 | base::Callback<void(const dbus::ObjectPath&)> on_itf2_removed_; |
| 1088 | base::WeakPtrFactory<ObjectManagerProxy> weak_ptr_factory_{this}; |
| 1089 | |
| 1090 | DISALLOW_COPY_AND_ASSIGN(ObjectManagerProxy); |
| 1091 | }; |
| 1092 | |
| 1093 | } // namespace chromium |
| 1094 | } // namespace org |
| 1095 | )literal_string"; |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 1096 | } // namespace |
| 1097 | |
| 1098 | class ProxyGeneratorTest : public Test { |
| 1099 | public: |
| 1100 | void SetUp() override { |
| 1101 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 1102 | } |
| 1103 | |
| 1104 | protected: |
| 1105 | base::FilePath CreateInputFile(const string& contents) { |
| 1106 | base::FilePath path; |
| 1107 | EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &path)); |
| 1108 | EXPECT_EQ(contents.size(), |
| 1109 | base::WriteFile(path, contents.c_str(), contents.size())); |
| 1110 | return path; |
| 1111 | } |
| 1112 | |
| 1113 | base::ScopedTempDir temp_dir_; |
| 1114 | }; |
| 1115 | |
| 1116 | TEST_F(ProxyGeneratorTest, GenerateAdaptors) { |
| 1117 | Interface interface; |
| 1118 | interface.name = kInterfaceName; |
Alex Vakulenko | 6ef2d73 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 1119 | interface.path = "/org/chromium/Test"; |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 1120 | interface.methods.emplace_back( |
| 1121 | kMethod1Name, |
| 1122 | vector<Interface::Argument>{ |
| 1123 | {kMethod1ArgumentName1, kMethod1Argument1}, |
| 1124 | {kMethod1ArgumentName2, kMethod1Argument2}}, |
| 1125 | vector<Interface::Argument>{{"", kMethod1Return}}); |
| 1126 | interface.methods.emplace_back( |
| 1127 | kMethod2Name, |
| 1128 | vector<Interface::Argument>{}, |
| 1129 | vector<Interface::Argument>{{"", kMethod2Return}}); |
| 1130 | interface.methods.emplace_back( |
| 1131 | kMethod3Name, |
| 1132 | vector<Interface::Argument>{{"", kMethod3Argument1}}, |
| 1133 | vector<Interface::Argument>{}); |
| 1134 | interface.methods.emplace_back(kMethod4Name); |
| 1135 | interface.signals.emplace_back(kSignal1Name); |
| 1136 | interface.signals.emplace_back( |
| 1137 | kSignal2Name, |
| 1138 | vector<Interface::Argument>{ |
| 1139 | {"", kSignal2Argument1}, |
| 1140 | {"", kSignal2Argument2}}); |
Alex Vakulenko | 6ef2d73 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 1141 | interface.methods.back().doc_string = "Comment line1\nline2"; |
Alex Vakulenko | fafef13 | 2014-11-03 14:52:09 -0800 | [diff] [blame] | 1142 | Interface interface2; |
| 1143 | interface2.name = kInterfaceName2; |
| 1144 | interface2.methods.emplace_back( |
| 1145 | kMethod5Name, |
| 1146 | vector<Interface::Argument>{}, |
| 1147 | vector<Interface::Argument>{ |
| 1148 | {kMethod5ArgumentName1, kMethod5Argument1}, |
| 1149 | {kMethod5ArgumentName2, kMethod5Argument2}}); |
| 1150 | vector<Interface> interfaces{interface, interface2}; |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 1151 | base::FilePath output_path = temp_dir_.path().Append("output.h"); |
Alex Vakulenko | 6ef2d73 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 1152 | ServiceConfig config; |
| 1153 | EXPECT_TRUE(ProxyGenerator::GenerateProxies(config, interfaces, output_path)); |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 1154 | string contents; |
| 1155 | EXPECT_TRUE(base::ReadFileToString(output_path, &contents)); |
| 1156 | // The header guards contain the (temporary) filename, so we search for |
| 1157 | // the content we need within the string. |
| 1158 | EXPECT_NE(string::npos, contents.find(kExpectedContent)) |
| 1159 | << "Expected to find the following content...\n" |
| 1160 | << kExpectedContent << "...within content...\n" << contents; |
| 1161 | } |
| 1162 | |
Alex Vakulenko | 6ef2d73 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 1163 | TEST_F(ProxyGeneratorTest, GenerateAdaptorsWithServiceName) { |
| 1164 | Interface interface; |
| 1165 | interface.name = kInterfaceName; |
| 1166 | interface.path = "/org/chromium/Test"; |
| 1167 | interface.signals.emplace_back(kSignal1Name); |
| 1168 | Interface interface2; |
| 1169 | interface2.name = kInterfaceName2; |
| 1170 | vector<Interface> interfaces{interface, interface2}; |
| 1171 | base::FilePath output_path = temp_dir_.path().Append("output2.h"); |
| 1172 | ServiceConfig config; |
| 1173 | config.service_name = "org.chromium.Test"; |
| 1174 | EXPECT_TRUE(ProxyGenerator::GenerateProxies(config, interfaces, output_path)); |
| 1175 | string contents; |
| 1176 | EXPECT_TRUE(base::ReadFileToString(output_path, &contents)); |
| 1177 | // The header guards contain the (temporary) filename, so we search for |
| 1178 | // the content we need within the string. |
| 1179 | EXPECT_NE(string::npos, contents.find(kExpectedContentWithService)) |
| 1180 | << "Expected to find the following content...\n" |
| 1181 | << kExpectedContentWithService << "...within content...\n" << contents; |
| 1182 | } |
| 1183 | |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame^] | 1184 | TEST_F(ProxyGeneratorTest, GenerateAdaptorsWithObjectManager) { |
| 1185 | Interface interface; |
| 1186 | interface.name = "org.chromium.Itf1"; |
| 1187 | interface.path = "/org/chromium/Test/Object"; |
| 1188 | interface.signals.emplace_back(kSignal1Name); |
| 1189 | interface.properties.emplace_back("data", "s", "read"); |
| 1190 | Interface interface2; |
| 1191 | interface2.name = "org.chromium.Itf2"; |
| 1192 | vector<Interface> interfaces{interface, interface2}; |
| 1193 | base::FilePath output_path = temp_dir_.path().Append("output3.h"); |
| 1194 | ServiceConfig config; |
| 1195 | config.object_manager.name = "org.chromium.ObjectManager"; |
| 1196 | config.object_manager.object_path = "/org/chromium/Test"; |
| 1197 | EXPECT_TRUE(ProxyGenerator::GenerateProxies(config, interfaces, output_path)); |
| 1198 | string contents; |
| 1199 | EXPECT_TRUE(base::ReadFileToString(output_path, &contents)); |
| 1200 | // The header guards contain the (temporary) filename, so we search for |
| 1201 | // the content we need within the string. |
| 1202 | EXPECT_NE(string::npos, contents.find(kExpectedContentWithObjectManager)) |
| 1203 | << "Expected to find the following content...\n" |
| 1204 | << kExpectedContentWithObjectManager << "...within content...\n" |
| 1205 | << contents; |
| 1206 | } |
| 1207 | |
| 1208 | TEST_F(ProxyGeneratorTest, GenerateAdaptorsWithObjectManagerAndServiceName) { |
| 1209 | Interface interface; |
| 1210 | interface.name = "org.chromium.Itf1"; |
| 1211 | interface.path = "/org/chromium/Test/Object"; |
| 1212 | interface.signals.emplace_back(kSignal1Name); |
| 1213 | Interface interface2; |
| 1214 | interface2.name = "org.chromium.Itf2"; |
| 1215 | vector<Interface> interfaces{interface, interface2}; |
| 1216 | base::FilePath output_path = temp_dir_.path().Append("output4.h"); |
| 1217 | ServiceConfig config; |
| 1218 | config.service_name = "org.chromium.Test"; |
| 1219 | config.object_manager.name = "org.chromium.ObjectManager"; |
| 1220 | config.object_manager.object_path = "/org/chromium/Test"; |
| 1221 | EXPECT_TRUE(ProxyGenerator::GenerateProxies(config, interfaces, output_path)); |
| 1222 | string contents; |
| 1223 | EXPECT_TRUE(base::ReadFileToString(output_path, &contents)); |
| 1224 | // The header guards contain the (temporary) filename, so we search for |
| 1225 | // the content we need within the string. |
| 1226 | EXPECT_NE(string::npos, |
| 1227 | contents.find(kExpectedContentWithObjectManagerAndServiceName)) |
| 1228 | << "Expected to find the following content...\n" |
| 1229 | << kExpectedContentWithObjectManagerAndServiceName |
| 1230 | << "...within content...\n" << contents; |
| 1231 | } |
| 1232 | |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 1233 | } // namespace chromeos_dbus_bindings |