blob: 165bd61ed9d46767addb6d3e30ac1b38efda76cb [file] [log] [blame]
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001// 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
Paul Stewart1dce1ae2014-10-01 05:30:18 -070010#include <base/files/file_path.h>
Alex Vakulenkof3c68972014-12-10 12:52:31 -080011#include <base/files/file_util.h>
Paul Stewart1dce1ae2014-10-01 05:30:18 -070012#include <base/files/scoped_temp_dir.h>
13#include <gtest/gtest.h>
14
15#include "chromeos-dbus-bindings/interface.h"
16
17using std::string;
18using std::vector;
19using testing::Test;
20
21namespace chromeos_dbus_bindings {
22
23namespace {
24
Alex Vakulenkob69be602015-02-23 14:40:42 -080025const char kDBusTypeArryOfObjects[] = "ao";
26const char kDBusTypeArryOfStrings[] = "as";
27const char kDBusTypeBool[] = "b";
28const char kDBusTypeByte[] = "y";
29const char kDBusTypeInt32[] = "i";
30const char kDBusTypeInt64[] = "x";
31const char kDBusTypeString[] = "s";
32
Paul Stewart1dce1ae2014-10-01 05:30:18 -070033const char kExpectedContent[] = R"literal_string(
Alex Vakulenko99e8fb02014-12-01 17:22:03 -080034#include <memory>
Paul Stewart1dce1ae2014-10-01 05:30:18 -070035#include <string>
36#include <vector>
37
38#include <base/bind.h>
39#include <base/callback.h>
40#include <base/logging.h>
41#include <base/macros.h>
42#include <base/memory/ref_counted.h>
43#include <chromeos/any.h>
44#include <chromeos/dbus/dbus_method_invoker.h>
Alex Vakulenko99e8fb02014-12-01 17:22:03 -080045#include <chromeos/dbus/dbus_property.h>
Paul Stewart1dce1ae2014-10-01 05:30:18 -070046#include <chromeos/dbus/dbus_signal_handler.h>
47#include <chromeos/errors/error.h>
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -080048#include <chromeos/variant_dictionary.h>
Paul Stewart1dce1ae2014-10-01 05:30:18 -070049#include <dbus/bus.h>
50#include <dbus/message.h>
Alex Vakulenko99e8fb02014-12-01 17:22:03 -080051#include <dbus/object_manager.h>
Paul Stewart1dce1ae2014-10-01 05:30:18 -070052#include <dbus/object_path.h>
53#include <dbus/object_proxy.h>
54
55namespace org {
56namespace chromium {
57
Alex Vakulenko008e19d2015-01-21 10:37:00 -080058// Abstract interface proxy for org::chromium::TestInterface.
59class TestInterfaceProxyInterface {
60 public:
61 virtual bool Elements(
62 const std::string& in_space_walk,
63 const std::vector<dbus::ObjectPath>& in_ramblin_man,
64 std::string* out_3,
65 chromeos::ErrorPtr* error,
66 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
67
68 virtual void ElementsAsync(
69 const std::string& in_space_walk,
70 const std::vector<dbus::ObjectPath>& in_ramblin_man,
71 const base::Callback<void(const std::string&)>& success_callback,
72 const base::Callback<void(chromeos::Error*)>& error_callback,
73 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
74
75 virtual bool ReturnToPatagonia(
76 int64_t* out_1,
77 chromeos::ErrorPtr* error,
78 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
79
80 virtual void ReturnToPatagoniaAsync(
81 const base::Callback<void(int64_t)>& success_callback,
82 const base::Callback<void(chromeos::Error*)>& error_callback,
83 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
84
85 virtual bool NiceWeatherForDucks(
86 bool in_1,
87 chromeos::ErrorPtr* error,
88 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
89
90 virtual void NiceWeatherForDucksAsync(
91 bool in_1,
92 const base::Callback<void()>& success_callback,
93 const base::Callback<void(chromeos::Error*)>& error_callback,
94 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
95
96 // Comment line1
97 // line2
98 virtual bool ExperimentNumberSix(
99 chromeos::ErrorPtr* error,
100 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
101
102 // Comment line1
103 // line2
104 virtual void ExperimentNumberSixAsync(
105 const base::Callback<void()>& success_callback,
106 const base::Callback<void(chromeos::Error*)>& error_callback,
107 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
108
109 protected:
Alex Deymo16856102015-06-19 20:55:14 -0700110 virtual ~TestInterfaceProxyInterface() = default;
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800111};
112
113} // namespace chromium
114} // namespace org
115
116namespace org {
117namespace chromium {
118
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800119// Interface proxy for org::chromium::TestInterface.
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800120class TestInterfaceProxy final : public TestInterfaceProxyInterface {
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700121 public:
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700122 TestInterfaceProxy(
123 const scoped_refptr<dbus::Bus>& bus,
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800124 const std::string& service_name) :
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800125 bus_{bus},
126 service_name_{service_name},
127 dbus_object_proxy_{
128 bus_->GetObjectProxy(service_name_, object_path_)} {
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800129 }
130
Alex Deymo16856102015-06-19 20:55:14 -0700131 ~TestInterfaceProxy() override {
Christopher Wiley824b5242014-12-03 11:58:01 -0800132 }
133
134 void RegisterCloserSignalHandler(
135 const base::Closure& signal_callback,
136 dbus::ObjectProxy::OnConnectedCallback on_connected_callback) {
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700137 chromeos::dbus_utils::ConnectToSignal(
138 dbus_object_proxy_,
139 "org.chromium.TestInterface",
140 "Closer",
Christopher Wiley824b5242014-12-03 11:58:01 -0800141 signal_callback,
142 on_connected_callback);
143 }
144
145 void RegisterTheCurseOfKaZarSignalHandler(
146 const base::Callback<void(const std::vector<std::string>&,
147 uint8_t)>& signal_callback,
148 dbus::ObjectProxy::OnConnectedCallback on_connected_callback) {
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700149 chromeos::dbus_utils::ConnectToSignal(
150 dbus_object_proxy_,
151 "org.chromium.TestInterface",
152 "TheCurseOfKaZar",
Christopher Wiley824b5242014-12-03 11:58:01 -0800153 signal_callback,
154 on_connected_callback);
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800155 }
156
157 void ReleaseObjectProxy(const base::Closure& callback) {
158 bus_->RemoveObjectProxy(service_name_, object_path_, callback);
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700159 }
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800160
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800161 const dbus::ObjectPath& GetObjectPath() const {
162 return object_path_;
163 }
164
165 dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; }
166
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800167 bool Elements(
Alex Vakulenkofafef132014-11-03 14:52:09 -0800168 const std::string& in_space_walk,
169 const std::vector<dbus::ObjectPath>& in_ramblin_man,
170 std::string* out_3,
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800171 chromeos::ErrorPtr* error,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800172 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800173 auto response = chromeos::dbus_utils::CallMethodAndBlockWithTimeout(
174 timeout_ms,
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700175 dbus_object_proxy_,
176 "org.chromium.TestInterface",
177 "Elements",
178 error,
Alex Vakulenkofafef132014-11-03 14:52:09 -0800179 in_space_walk,
180 in_ramblin_man);
181 return response && chromeos::dbus_utils::ExtractMethodCallResults(
182 response.get(), error, out_3);
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700183 }
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800184
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800185 void ElementsAsync(
186 const std::string& in_space_walk,
187 const std::vector<dbus::ObjectPath>& in_ramblin_man,
188 const base::Callback<void(const std::string&)>& success_callback,
189 const base::Callback<void(chromeos::Error*)>& error_callback,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800190 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800191 chromeos::dbus_utils::CallMethodWithTimeout(
192 timeout_ms,
193 dbus_object_proxy_,
194 "org.chromium.TestInterface",
195 "Elements",
196 success_callback,
197 error_callback,
198 in_space_walk,
199 in_ramblin_man);
200 }
201
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800202 bool ReturnToPatagonia(
Alex Vakulenkofafef132014-11-03 14:52:09 -0800203 int64_t* out_1,
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800204 chromeos::ErrorPtr* error,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800205 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800206 auto response = chromeos::dbus_utils::CallMethodAndBlockWithTimeout(
207 timeout_ms,
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700208 dbus_object_proxy_,
209 "org.chromium.TestInterface",
210 "ReturnToPatagonia",
211 error);
Alex Vakulenkofafef132014-11-03 14:52:09 -0800212 return response && chromeos::dbus_utils::ExtractMethodCallResults(
213 response.get(), error, out_1);
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700214 }
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800215
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800216 void ReturnToPatagoniaAsync(
217 const base::Callback<void(int64_t)>& success_callback,
218 const base::Callback<void(chromeos::Error*)>& error_callback,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800219 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800220 chromeos::dbus_utils::CallMethodWithTimeout(
221 timeout_ms,
222 dbus_object_proxy_,
223 "org.chromium.TestInterface",
224 "ReturnToPatagonia",
225 success_callback,
226 error_callback);
227 }
228
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800229 bool NiceWeatherForDucks(
Alex Vakulenkofafef132014-11-03 14:52:09 -0800230 bool in_1,
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800231 chromeos::ErrorPtr* error,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800232 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800233 auto response = chromeos::dbus_utils::CallMethodAndBlockWithTimeout(
234 timeout_ms,
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700235 dbus_object_proxy_,
236 "org.chromium.TestInterface",
237 "NiceWeatherForDucks",
238 error,
Alex Vakulenkofafef132014-11-03 14:52:09 -0800239 in_1);
240 return response && chromeos::dbus_utils::ExtractMethodCallResults(
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700241 response.get(), error);
242 }
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800243
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800244 void NiceWeatherForDucksAsync(
245 bool in_1,
246 const base::Callback<void()>& success_callback,
247 const base::Callback<void(chromeos::Error*)>& error_callback,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800248 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800249 chromeos::dbus_utils::CallMethodWithTimeout(
250 timeout_ms,
251 dbus_object_proxy_,
252 "org.chromium.TestInterface",
253 "NiceWeatherForDucks",
254 success_callback,
255 error_callback,
256 in_1);
257 }
258
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800259 // Comment line1
260 // line2
261 bool ExperimentNumberSix(
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800262 chromeos::ErrorPtr* error,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800263 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800264 auto response = chromeos::dbus_utils::CallMethodAndBlockWithTimeout(
265 timeout_ms,
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700266 dbus_object_proxy_,
267 "org.chromium.TestInterface",
268 "ExperimentNumberSix",
269 error);
Alex Vakulenkofafef132014-11-03 14:52:09 -0800270 return response && chromeos::dbus_utils::ExtractMethodCallResults(
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700271 response.get(), error);
272 }
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800273
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800274 // Comment line1
275 // line2
276 void ExperimentNumberSixAsync(
277 const base::Callback<void()>& success_callback,
278 const base::Callback<void(chromeos::Error*)>& error_callback,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800279 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800280 chromeos::dbus_utils::CallMethodWithTimeout(
281 timeout_ms,
282 dbus_object_proxy_,
283 "org.chromium.TestInterface",
284 "ExperimentNumberSix",
285 success_callback,
286 error_callback);
287 }
288
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800289 private:
290 scoped_refptr<dbus::Bus> bus_;
291 std::string service_name_;
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800292 const dbus::ObjectPath object_path_{"/org/chromium/Test"};
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800293 dbus::ObjectProxy* dbus_object_proxy_;
294
295 DISALLOW_COPY_AND_ASSIGN(TestInterfaceProxy);
296};
297
298} // namespace chromium
299} // namespace org
300
301namespace org {
302namespace chromium {
303
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800304// Abstract interface proxy for org::chromium::TestInterface2.
305class TestInterface2ProxyInterface {
306 public:
307 virtual bool GetPersonInfo(
308 std::string* out_name,
309 int32_t* out_age,
310 chromeos::ErrorPtr* error,
311 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
312
313 virtual void GetPersonInfoAsync(
314 const base::Callback<void(const std::string& /*name*/, int32_t /*age*/)>& success_callback,
315 const base::Callback<void(chromeos::Error*)>& error_callback,
316 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
317
318 protected:
Alex Deymo16856102015-06-19 20:55:14 -0700319 virtual ~TestInterface2ProxyInterface() = default;
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800320};
321
322} // namespace chromium
323} // namespace org
324
325namespace org {
326namespace chromium {
327
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800328// Interface proxy for org::chromium::TestInterface2.
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800329class TestInterface2Proxy final : public TestInterface2ProxyInterface {
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800330 public:
331 TestInterface2Proxy(
332 const scoped_refptr<dbus::Bus>& bus,
333 const std::string& service_name,
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800334 const dbus::ObjectPath& object_path) :
335 bus_{bus},
336 service_name_{service_name},
337 object_path_{object_path},
338 dbus_object_proxy_{
339 bus_->GetObjectProxy(service_name_, object_path_)} {
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800340 }
341
Alex Deymo16856102015-06-19 20:55:14 -0700342 ~TestInterface2Proxy() override {
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800343 }
344
345 void ReleaseObjectProxy(const base::Closure& callback) {
346 bus_->RemoveObjectProxy(service_name_, object_path_, callback);
347 }
348
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800349 const dbus::ObjectPath& GetObjectPath() const {
350 return object_path_;
351 }
352
353 dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; }
354
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800355 bool GetPersonInfo(
Alex Vakulenkofafef132014-11-03 14:52:09 -0800356 std::string* out_name,
357 int32_t* out_age,
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800358 chromeos::ErrorPtr* error,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800359 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800360 auto response = chromeos::dbus_utils::CallMethodAndBlockWithTimeout(
361 timeout_ms,
Alex Vakulenkofafef132014-11-03 14:52:09 -0800362 dbus_object_proxy_,
363 "org.chromium.TestInterface2",
364 "GetPersonInfo",
365 error);
366 return response && chromeos::dbus_utils::ExtractMethodCallResults(
367 response.get(), error, out_name, out_age);
368 }
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700369
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800370 void GetPersonInfoAsync(
371 const base::Callback<void(const std::string& /*name*/, int32_t /*age*/)>& success_callback,
372 const base::Callback<void(chromeos::Error*)>& error_callback,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800373 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800374 chromeos::dbus_utils::CallMethodWithTimeout(
375 timeout_ms,
376 dbus_object_proxy_,
377 "org.chromium.TestInterface2",
378 "GetPersonInfo",
379 success_callback,
380 error_callback);
381 }
382
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700383 private:
384 scoped_refptr<dbus::Bus> bus_;
385 std::string service_name_;
386 dbus::ObjectPath object_path_;
387 dbus::ObjectProxy* dbus_object_proxy_;
388
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800389 DISALLOW_COPY_AND_ASSIGN(TestInterface2Proxy);
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700390};
391
392} // namespace chromium
393} // namespace org
394)literal_string";
395
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800396const char kExpectedContentWithService[] = R"literal_string(
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800397#include <memory>
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800398#include <string>
399#include <vector>
400
401#include <base/bind.h>
402#include <base/callback.h>
403#include <base/logging.h>
404#include <base/macros.h>
405#include <base/memory/ref_counted.h>
406#include <chromeos/any.h>
407#include <chromeos/dbus/dbus_method_invoker.h>
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800408#include <chromeos/dbus/dbus_property.h>
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800409#include <chromeos/dbus/dbus_signal_handler.h>
410#include <chromeos/errors/error.h>
411#include <chromeos/variant_dictionary.h>
412#include <dbus/bus.h>
413#include <dbus/message.h>
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800414#include <dbus/object_manager.h>
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800415#include <dbus/object_path.h>
416#include <dbus/object_proxy.h>
417
418namespace org {
419namespace chromium {
420
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800421// Abstract interface proxy for org::chromium::TestInterface.
422class TestInterfaceProxyInterface {
423 public:
424 protected:
Alex Deymo16856102015-06-19 20:55:14 -0700425 virtual ~TestInterfaceProxyInterface() = default;
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800426};
427
428} // namespace chromium
429} // namespace org
430
431namespace org {
432namespace chromium {
433
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800434// Interface proxy for org::chromium::TestInterface.
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800435class TestInterfaceProxy final : public TestInterfaceProxyInterface {
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800436 public:
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800437 TestInterfaceProxy(const scoped_refptr<dbus::Bus>& bus) :
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800438 bus_{bus},
439 dbus_object_proxy_{
440 bus_->GetObjectProxy(service_name_, object_path_)} {
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800441 }
442
Alex Deymo16856102015-06-19 20:55:14 -0700443 ~TestInterfaceProxy() override {
Christopher Wiley824b5242014-12-03 11:58:01 -0800444 }
445
446 void RegisterCloserSignalHandler(
447 const base::Closure& signal_callback,
448 dbus::ObjectProxy::OnConnectedCallback on_connected_callback) {
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800449 chromeos::dbus_utils::ConnectToSignal(
450 dbus_object_proxy_,
451 "org.chromium.TestInterface",
452 "Closer",
Christopher Wiley824b5242014-12-03 11:58:01 -0800453 signal_callback,
454 on_connected_callback);
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800455 }
456
457 void ReleaseObjectProxy(const base::Closure& callback) {
458 bus_->RemoveObjectProxy(service_name_, object_path_, callback);
459 }
460
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800461 const dbus::ObjectPath& GetObjectPath() const {
462 return object_path_;
463 }
464
465 dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; }
466
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800467 private:
468 scoped_refptr<dbus::Bus> bus_;
469 const std::string service_name_{"org.chromium.Test"};
470 const dbus::ObjectPath object_path_{"/org/chromium/Test"};
471 dbus::ObjectProxy* dbus_object_proxy_;
472
473 DISALLOW_COPY_AND_ASSIGN(TestInterfaceProxy);
474};
475
476} // namespace chromium
477} // namespace org
478
479namespace org {
480namespace chromium {
481
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800482// Abstract interface proxy for org::chromium::TestInterface2.
483class TestInterface2ProxyInterface {
484 public:
485 protected:
Alex Deymo16856102015-06-19 20:55:14 -0700486 virtual ~TestInterface2ProxyInterface() = default;
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800487};
488
489} // namespace chromium
490} // namespace org
491
492namespace org {
493namespace chromium {
494
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800495// Interface proxy for org::chromium::TestInterface2.
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800496class TestInterface2Proxy final : public TestInterface2ProxyInterface {
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800497 public:
498 TestInterface2Proxy(
499 const scoped_refptr<dbus::Bus>& bus,
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800500 const dbus::ObjectPath& object_path) :
501 bus_{bus},
502 object_path_{object_path},
503 dbus_object_proxy_{
504 bus_->GetObjectProxy(service_name_, object_path_)} {
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800505 }
506
Alex Deymo16856102015-06-19 20:55:14 -0700507 ~TestInterface2Proxy() override {
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800508 }
509
510 void ReleaseObjectProxy(const base::Closure& callback) {
511 bus_->RemoveObjectProxy(service_name_, object_path_, callback);
512 }
513
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800514 const dbus::ObjectPath& GetObjectPath() const {
515 return object_path_;
516 }
517
518 dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; }
519
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800520 private:
521 scoped_refptr<dbus::Bus> bus_;
522 const std::string service_name_{"org.chromium.Test"};
523 dbus::ObjectPath object_path_;
524 dbus::ObjectProxy* dbus_object_proxy_;
525
526 DISALLOW_COPY_AND_ASSIGN(TestInterface2Proxy);
527};
528
529} // namespace chromium
530} // namespace org
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800531)literal_string";
532
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800533const char kExpectedContentWithObjectManager[] = R"literal_string(
534#include <memory>
535#include <string>
536#include <vector>
537
538#include <base/bind.h>
539#include <base/callback.h>
540#include <base/logging.h>
541#include <base/macros.h>
542#include <base/memory/ref_counted.h>
543#include <chromeos/any.h>
544#include <chromeos/dbus/dbus_method_invoker.h>
545#include <chromeos/dbus/dbus_property.h>
546#include <chromeos/dbus/dbus_signal_handler.h>
547#include <chromeos/errors/error.h>
548#include <chromeos/variant_dictionary.h>
549#include <dbus/bus.h>
550#include <dbus/message.h>
551#include <dbus/object_manager.h>
552#include <dbus/object_path.h>
553#include <dbus/object_proxy.h>
554
555namespace org {
556namespace chromium {
557class ObjectManagerProxy;
558} // namespace chromium
559} // namespace org
560
561namespace org {
562namespace chromium {
563
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800564// Abstract interface proxy for org::chromium::Itf1.
565class Itf1ProxyInterface {
566 public:
Alex Vakulenko59dd2e92015-02-23 15:14:28 -0800567 static const char* DataName() { return "Data"; }
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800568 virtual const std::string& data() const = 0;
569
570 protected:
Alex Deymo16856102015-06-19 20:55:14 -0700571 virtual ~Itf1ProxyInterface() = default;
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800572};
573
574} // namespace chromium
575} // namespace org
576
577namespace org {
578namespace chromium {
579
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800580// Interface proxy for org::chromium::Itf1.
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800581class Itf1Proxy final : public Itf1ProxyInterface {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800582 public:
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800583 class PropertySet : public dbus::PropertySet {
584 public:
585 PropertySet(dbus::ObjectProxy* object_proxy,
586 const PropertyChangedCallback& callback)
587 : dbus::PropertySet{object_proxy,
588 "org.chromium.Itf1",
589 callback} {
Alex Vakulenko59dd2e92015-02-23 15:14:28 -0800590 RegisterProperty(DataName(), &data);
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800591 }
592
593 chromeos::dbus_utils::Property<std::string> data;
594
595 private:
596 DISALLOW_COPY_AND_ASSIGN(PropertySet);
597 };
598
599 Itf1Proxy(
600 const scoped_refptr<dbus::Bus>& bus,
601 const std::string& service_name,
602 PropertySet* property_set) :
603 bus_{bus},
604 service_name_{service_name},
605 property_set_{property_set},
606 dbus_object_proxy_{
607 bus_->GetObjectProxy(service_name_, object_path_)} {
608 }
609
Alex Deymo16856102015-06-19 20:55:14 -0700610 ~Itf1Proxy() override {
Christopher Wiley824b5242014-12-03 11:58:01 -0800611 }
612
613 void RegisterCloserSignalHandler(
614 const base::Closure& signal_callback,
615 dbus::ObjectProxy::OnConnectedCallback on_connected_callback) {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800616 chromeos::dbus_utils::ConnectToSignal(
617 dbus_object_proxy_,
618 "org.chromium.Itf1",
619 "Closer",
Christopher Wiley824b5242014-12-03 11:58:01 -0800620 signal_callback,
621 on_connected_callback);
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800622 }
623
624 void ReleaseObjectProxy(const base::Closure& callback) {
625 bus_->RemoveObjectProxy(service_name_, object_path_, callback);
626 }
627
628 const dbus::ObjectPath& GetObjectPath() const {
629 return object_path_;
630 }
631
632 dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; }
633
634 void SetPropertyChangedCallback(
635 const base::Callback<void(Itf1Proxy*, const std::string&)>& callback) {
636 on_property_changed_ = callback;
637 }
638
639 const PropertySet* GetProperties() const { return property_set_; }
640 PropertySet* GetProperties() { return property_set_; }
641
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800642 const std::string& data() const override {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800643 return property_set_->data.value();
644 }
645
646 private:
647 void OnPropertyChanged(const std::string& property_name) {
648 if (!on_property_changed_.is_null())
649 on_property_changed_.Run(this, property_name);
650 }
651
652 scoped_refptr<dbus::Bus> bus_;
653 std::string service_name_;
654 const dbus::ObjectPath object_path_{"/org/chromium/Test/Object"};
655 PropertySet* property_set_;
656 base::Callback<void(Itf1Proxy*, const std::string&)> on_property_changed_;
657 dbus::ObjectProxy* dbus_object_proxy_;
658
659 friend class org::chromium::ObjectManagerProxy;
660 DISALLOW_COPY_AND_ASSIGN(Itf1Proxy);
661};
662
663} // namespace chromium
664} // namespace org
665
666namespace org {
667namespace chromium {
668
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800669// Abstract interface proxy for org::chromium::Itf2.
670class Itf2ProxyInterface {
671 public:
672 protected:
Alex Deymo16856102015-06-19 20:55:14 -0700673 virtual ~Itf2ProxyInterface() = default;
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800674};
675
676} // namespace chromium
677} // namespace org
678
679namespace org {
680namespace chromium {
681
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800682// Interface proxy for org::chromium::Itf2.
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800683class Itf2Proxy final : public Itf2ProxyInterface {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800684 public:
685 class PropertySet : public dbus::PropertySet {
686 public:
687 PropertySet(dbus::ObjectProxy* object_proxy,
688 const PropertyChangedCallback& callback)
689 : dbus::PropertySet{object_proxy,
690 "org.chromium.Itf2",
691 callback} {
692 }
693
694
695 private:
696 DISALLOW_COPY_AND_ASSIGN(PropertySet);
697 };
698
699 Itf2Proxy(
700 const scoped_refptr<dbus::Bus>& bus,
701 const std::string& service_name,
702 const dbus::ObjectPath& object_path) :
703 bus_{bus},
704 service_name_{service_name},
705 object_path_{object_path},
706 dbus_object_proxy_{
707 bus_->GetObjectProxy(service_name_, object_path_)} {
708 }
709
Alex Deymo16856102015-06-19 20:55:14 -0700710 ~Itf2Proxy() override {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800711 }
712
713 void ReleaseObjectProxy(const base::Closure& callback) {
714 bus_->RemoveObjectProxy(service_name_, object_path_, callback);
715 }
716
717 const dbus::ObjectPath& GetObjectPath() const {
718 return object_path_;
719 }
720
721 dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; }
722
723 private:
724 scoped_refptr<dbus::Bus> bus_;
725 std::string service_name_;
726 dbus::ObjectPath object_path_;
727 dbus::ObjectProxy* dbus_object_proxy_;
728
729 DISALLOW_COPY_AND_ASSIGN(Itf2Proxy);
730};
731
732} // namespace chromium
733} // namespace org
734
735namespace org {
736namespace chromium {
737
738class ObjectManagerProxy : public dbus::ObjectManager::Interface {
739 public:
740 ObjectManagerProxy(const scoped_refptr<dbus::Bus>& bus,
741 const std::string& service_name)
742 : bus_{bus},
Christopher Wileyefe9df52015-03-12 16:02:22 -0700743 service_name_{service_name},
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800744 dbus_object_manager_{bus->GetObjectManager(
745 service_name,
746 dbus::ObjectPath{"/org/chromium/Test"})} {
747 dbus_object_manager_->RegisterInterface("org.chromium.Itf1", this);
748 dbus_object_manager_->RegisterInterface("org.chromium.Itf2", this);
749 }
750
Christopher Wileye11e81c2014-12-19 13:10:52 -0800751 ~ObjectManagerProxy() override {
752 dbus_object_manager_->UnregisterInterface("org.chromium.Itf1");
753 dbus_object_manager_->UnregisterInterface("org.chromium.Itf2");
754 }
755
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800756 dbus::ObjectManager* GetObjectManagerProxy() const {
757 return dbus_object_manager_;
758 }
759
Alex Vakulenkoc4e01f12015-02-23 15:55:21 -0800760 org::chromium::Itf1Proxy* GetItf1Proxy() {
761 if (itf1_instances_.empty())
762 return nullptr;
763 return itf1_instances_.begin()->second.get();
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800764 }
765 std::vector<org::chromium::Itf1Proxy*> GetItf1Instances() const {
766 std::vector<org::chromium::Itf1Proxy*> values;
767 values.reserve(itf1_instances_.size());
768 for (const auto& pair : itf1_instances_)
769 values.push_back(pair.second.get());
770 return values;
771 }
772 void SetItf1AddedCallback(
773 const base::Callback<void(org::chromium::Itf1Proxy*)>& callback) {
774 on_itf1_added_ = callback;
775 }
776 void SetItf1RemovedCallback(
777 const base::Callback<void(const dbus::ObjectPath&)>& callback) {
778 on_itf1_removed_ = callback;
779 }
780
781 org::chromium::Itf2Proxy* GetItf2Proxy(
782 const dbus::ObjectPath& object_path) {
783 auto p = itf2_instances_.find(object_path);
784 if (p != itf2_instances_.end())
785 return p->second.get();
786 return nullptr;
787 }
788 std::vector<org::chromium::Itf2Proxy*> GetItf2Instances() const {
789 std::vector<org::chromium::Itf2Proxy*> values;
790 values.reserve(itf2_instances_.size());
791 for (const auto& pair : itf2_instances_)
792 values.push_back(pair.second.get());
793 return values;
794 }
795 void SetItf2AddedCallback(
796 const base::Callback<void(org::chromium::Itf2Proxy*)>& callback) {
797 on_itf2_added_ = callback;
798 }
799 void SetItf2RemovedCallback(
800 const base::Callback<void(const dbus::ObjectPath&)>& callback) {
801 on_itf2_removed_ = callback;
802 }
803
804 private:
805 void OnPropertyChanged(const dbus::ObjectPath& object_path,
806 const std::string& interface_name,
807 const std::string& property_name) {
808 if (interface_name == "org.chromium.Itf1") {
809 auto p = itf1_instances_.find(object_path);
810 if (p == itf1_instances_.end())
811 return;
812 p->second->OnPropertyChanged(property_name);
813 return;
814 }
815 }
816
817 void ObjectAdded(
818 const dbus::ObjectPath& object_path,
819 const std::string& interface_name) override {
820 if (interface_name == "org.chromium.Itf1") {
821 auto property_set =
822 static_cast<org::chromium::Itf1Proxy::PropertySet*>(
823 dbus_object_manager_->GetProperties(object_path, interface_name));
824 std::unique_ptr<org::chromium::Itf1Proxy> itf1_proxy{
Christopher Wileyefe9df52015-03-12 16:02:22 -0700825 new org::chromium::Itf1Proxy{bus_, service_name_, property_set}
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800826 };
827 auto p = itf1_instances_.emplace(object_path, std::move(itf1_proxy));
828 if (!on_itf1_added_.is_null())
829 on_itf1_added_.Run(p.first->second.get());
830 return;
831 }
832 if (interface_name == "org.chromium.Itf2") {
833 std::unique_ptr<org::chromium::Itf2Proxy> itf2_proxy{
Christopher Wileyefe9df52015-03-12 16:02:22 -0700834 new org::chromium::Itf2Proxy{bus_, service_name_, object_path}
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800835 };
836 auto p = itf2_instances_.emplace(object_path, std::move(itf2_proxy));
837 if (!on_itf2_added_.is_null())
838 on_itf2_added_.Run(p.first->second.get());
839 return;
840 }
841 }
842
843 void ObjectRemoved(
844 const dbus::ObjectPath& object_path,
845 const std::string& interface_name) override {
846 if (interface_name == "org.chromium.Itf1") {
847 auto p = itf1_instances_.find(object_path);
848 if (p != itf1_instances_.end()) {
849 if (!on_itf1_removed_.is_null())
850 on_itf1_removed_.Run(object_path);
851 itf1_instances_.erase(p);
852 }
853 return;
854 }
855 if (interface_name == "org.chromium.Itf2") {
856 auto p = itf2_instances_.find(object_path);
857 if (p != itf2_instances_.end()) {
858 if (!on_itf2_removed_.is_null())
859 on_itf2_removed_.Run(object_path);
860 itf2_instances_.erase(p);
861 }
862 return;
863 }
864 }
865
866 dbus::PropertySet* CreateProperties(
867 dbus::ObjectProxy* object_proxy,
868 const dbus::ObjectPath& object_path,
869 const std::string& interface_name) override {
870 if (interface_name == "org.chromium.Itf1") {
871 return new org::chromium::Itf1Proxy::PropertySet{
872 object_proxy,
873 base::Bind(&ObjectManagerProxy::OnPropertyChanged,
874 weak_ptr_factory_.GetWeakPtr(),
875 object_path,
876 interface_name)
877 };
878 }
879 if (interface_name == "org.chromium.Itf2") {
880 return new org::chromium::Itf2Proxy::PropertySet{
881 object_proxy,
882 base::Bind(&ObjectManagerProxy::OnPropertyChanged,
883 weak_ptr_factory_.GetWeakPtr(),
884 object_path,
885 interface_name)
886 };
887 }
888 LOG(FATAL) << "Creating properties for unsupported interface "
889 << interface_name;
890 return nullptr;
891 }
892
893 scoped_refptr<dbus::Bus> bus_;
Christopher Wileyefe9df52015-03-12 16:02:22 -0700894 std::string service_name_;
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800895 dbus::ObjectManager* dbus_object_manager_;
896 std::map<dbus::ObjectPath,
897 std::unique_ptr<org::chromium::Itf1Proxy>> itf1_instances_;
898 base::Callback<void(org::chromium::Itf1Proxy*)> on_itf1_added_;
899 base::Callback<void(const dbus::ObjectPath&)> on_itf1_removed_;
900 std::map<dbus::ObjectPath,
901 std::unique_ptr<org::chromium::Itf2Proxy>> itf2_instances_;
902 base::Callback<void(org::chromium::Itf2Proxy*)> on_itf2_added_;
903 base::Callback<void(const dbus::ObjectPath&)> on_itf2_removed_;
904 base::WeakPtrFactory<ObjectManagerProxy> weak_ptr_factory_{this};
905
906 DISALLOW_COPY_AND_ASSIGN(ObjectManagerProxy);
907};
908
909} // namespace chromium
910} // namespace org
911)literal_string";
912
913const char kExpectedContentWithObjectManagerAndServiceName[] = R"literal_string(
914#include <memory>
915#include <string>
916#include <vector>
917
918#include <base/bind.h>
919#include <base/callback.h>
920#include <base/logging.h>
921#include <base/macros.h>
922#include <base/memory/ref_counted.h>
923#include <chromeos/any.h>
924#include <chromeos/dbus/dbus_method_invoker.h>
925#include <chromeos/dbus/dbus_property.h>
926#include <chromeos/dbus/dbus_signal_handler.h>
927#include <chromeos/errors/error.h>
928#include <chromeos/variant_dictionary.h>
929#include <dbus/bus.h>
930#include <dbus/message.h>
931#include <dbus/object_manager.h>
932#include <dbus/object_path.h>
933#include <dbus/object_proxy.h>
934
935namespace org {
936namespace chromium {
937class ObjectManagerProxy;
938} // namespace chromium
939} // namespace org
940
941namespace org {
942namespace chromium {
943
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800944// Abstract interface proxy for org::chromium::Itf1.
945class Itf1ProxyInterface {
946 public:
947 protected:
Alex Deymo16856102015-06-19 20:55:14 -0700948 virtual ~Itf1ProxyInterface() = default;
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800949};
950
951} // namespace chromium
952} // namespace org
953
954namespace org {
955namespace chromium {
956
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800957// Interface proxy for org::chromium::Itf1.
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800958class Itf1Proxy final : public Itf1ProxyInterface {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800959 public:
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800960 class PropertySet : public dbus::PropertySet {
961 public:
962 PropertySet(dbus::ObjectProxy* object_proxy,
963 const PropertyChangedCallback& callback)
964 : dbus::PropertySet{object_proxy,
965 "org.chromium.Itf1",
966 callback} {
967 }
968
969
970 private:
971 DISALLOW_COPY_AND_ASSIGN(PropertySet);
972 };
973
974 Itf1Proxy(const scoped_refptr<dbus::Bus>& bus) :
975 bus_{bus},
976 dbus_object_proxy_{
977 bus_->GetObjectProxy(service_name_, object_path_)} {
978 }
979
Alex Deymo16856102015-06-19 20:55:14 -0700980 ~Itf1Proxy() override {
Christopher Wiley824b5242014-12-03 11:58:01 -0800981 }
982
983 void RegisterCloserSignalHandler(
984 const base::Closure& signal_callback,
985 dbus::ObjectProxy::OnConnectedCallback on_connected_callback) {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800986 chromeos::dbus_utils::ConnectToSignal(
987 dbus_object_proxy_,
988 "org.chromium.Itf1",
989 "Closer",
Christopher Wiley824b5242014-12-03 11:58:01 -0800990 signal_callback,
991 on_connected_callback);
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800992 }
993
994 void ReleaseObjectProxy(const base::Closure& callback) {
995 bus_->RemoveObjectProxy(service_name_, object_path_, callback);
996 }
997
998 const dbus::ObjectPath& GetObjectPath() const {
999 return object_path_;
1000 }
1001
1002 dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; }
1003
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001004 private:
1005 scoped_refptr<dbus::Bus> bus_;
1006 const std::string service_name_{"org.chromium.Test"};
1007 const dbus::ObjectPath object_path_{"/org/chromium/Test/Object"};
1008 dbus::ObjectProxy* dbus_object_proxy_;
1009
1010 DISALLOW_COPY_AND_ASSIGN(Itf1Proxy);
1011};
1012
1013} // namespace chromium
1014} // namespace org
1015
1016namespace org {
1017namespace chromium {
1018
Alex Vakulenko008e19d2015-01-21 10:37:00 -08001019// Abstract interface proxy for org::chromium::Itf2.
1020class Itf2ProxyInterface {
1021 public:
1022 protected:
Alex Deymo16856102015-06-19 20:55:14 -07001023 virtual ~Itf2ProxyInterface() = default;
Alex Vakulenko008e19d2015-01-21 10:37:00 -08001024};
1025
1026} // namespace chromium
1027} // namespace org
1028
1029namespace org {
1030namespace chromium {
1031
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001032// Interface proxy for org::chromium::Itf2.
Alex Vakulenko008e19d2015-01-21 10:37:00 -08001033class Itf2Proxy final : public Itf2ProxyInterface {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001034 public:
1035 class PropertySet : public dbus::PropertySet {
1036 public:
1037 PropertySet(dbus::ObjectProxy* object_proxy,
1038 const PropertyChangedCallback& callback)
1039 : dbus::PropertySet{object_proxy,
1040 "org.chromium.Itf2",
1041 callback} {
1042 }
1043
1044
1045 private:
1046 DISALLOW_COPY_AND_ASSIGN(PropertySet);
1047 };
1048
1049 Itf2Proxy(
1050 const scoped_refptr<dbus::Bus>& bus,
1051 const dbus::ObjectPath& object_path) :
1052 bus_{bus},
1053 object_path_{object_path},
1054 dbus_object_proxy_{
1055 bus_->GetObjectProxy(service_name_, object_path_)} {
1056 }
1057
Alex Deymo16856102015-06-19 20:55:14 -07001058 ~Itf2Proxy() override {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001059 }
1060
1061 void ReleaseObjectProxy(const base::Closure& callback) {
1062 bus_->RemoveObjectProxy(service_name_, object_path_, callback);
1063 }
1064
1065 const dbus::ObjectPath& GetObjectPath() const {
1066 return object_path_;
1067 }
1068
1069 dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; }
1070
1071 private:
1072 scoped_refptr<dbus::Bus> bus_;
1073 const std::string service_name_{"org.chromium.Test"};
1074 dbus::ObjectPath object_path_;
1075 dbus::ObjectProxy* dbus_object_proxy_;
1076
1077 DISALLOW_COPY_AND_ASSIGN(Itf2Proxy);
1078};
1079
1080} // namespace chromium
1081} // namespace org
1082
1083namespace org {
1084namespace chromium {
1085
1086class ObjectManagerProxy : public dbus::ObjectManager::Interface {
1087 public:
1088 ObjectManagerProxy(const scoped_refptr<dbus::Bus>& bus)
1089 : bus_{bus},
1090 dbus_object_manager_{bus->GetObjectManager(
1091 "org.chromium.Test",
1092 dbus::ObjectPath{"/org/chromium/Test"})} {
1093 dbus_object_manager_->RegisterInterface("org.chromium.Itf1", this);
1094 dbus_object_manager_->RegisterInterface("org.chromium.Itf2", this);
1095 }
1096
Christopher Wileye11e81c2014-12-19 13:10:52 -08001097 ~ObjectManagerProxy() override {
1098 dbus_object_manager_->UnregisterInterface("org.chromium.Itf1");
1099 dbus_object_manager_->UnregisterInterface("org.chromium.Itf2");
1100 }
1101
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001102 dbus::ObjectManager* GetObjectManagerProxy() const {
1103 return dbus_object_manager_;
1104 }
1105
Alex Vakulenkoc4e01f12015-02-23 15:55:21 -08001106 org::chromium::Itf1Proxy* GetItf1Proxy() {
1107 if (itf1_instances_.empty())
1108 return nullptr;
1109 return itf1_instances_.begin()->second.get();
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001110 }
1111 std::vector<org::chromium::Itf1Proxy*> GetItf1Instances() const {
1112 std::vector<org::chromium::Itf1Proxy*> values;
1113 values.reserve(itf1_instances_.size());
1114 for (const auto& pair : itf1_instances_)
1115 values.push_back(pair.second.get());
1116 return values;
1117 }
1118 void SetItf1AddedCallback(
1119 const base::Callback<void(org::chromium::Itf1Proxy*)>& callback) {
1120 on_itf1_added_ = callback;
1121 }
1122 void SetItf1RemovedCallback(
1123 const base::Callback<void(const dbus::ObjectPath&)>& callback) {
1124 on_itf1_removed_ = callback;
1125 }
1126
1127 org::chromium::Itf2Proxy* GetItf2Proxy(
1128 const dbus::ObjectPath& object_path) {
1129 auto p = itf2_instances_.find(object_path);
1130 if (p != itf2_instances_.end())
1131 return p->second.get();
1132 return nullptr;
1133 }
1134 std::vector<org::chromium::Itf2Proxy*> GetItf2Instances() const {
1135 std::vector<org::chromium::Itf2Proxy*> values;
1136 values.reserve(itf2_instances_.size());
1137 for (const auto& pair : itf2_instances_)
1138 values.push_back(pair.second.get());
1139 return values;
1140 }
1141 void SetItf2AddedCallback(
1142 const base::Callback<void(org::chromium::Itf2Proxy*)>& callback) {
1143 on_itf2_added_ = callback;
1144 }
1145 void SetItf2RemovedCallback(
1146 const base::Callback<void(const dbus::ObjectPath&)>& callback) {
1147 on_itf2_removed_ = callback;
1148 }
1149
1150 private:
1151 void OnPropertyChanged(const dbus::ObjectPath& object_path,
1152 const std::string& interface_name,
1153 const std::string& property_name) {
1154 }
1155
1156 void ObjectAdded(
1157 const dbus::ObjectPath& object_path,
1158 const std::string& interface_name) override {
1159 if (interface_name == "org.chromium.Itf1") {
1160 std::unique_ptr<org::chromium::Itf1Proxy> itf1_proxy{
1161 new org::chromium::Itf1Proxy{bus_}
1162 };
1163 auto p = itf1_instances_.emplace(object_path, std::move(itf1_proxy));
1164 if (!on_itf1_added_.is_null())
1165 on_itf1_added_.Run(p.first->second.get());
1166 return;
1167 }
1168 if (interface_name == "org.chromium.Itf2") {
1169 std::unique_ptr<org::chromium::Itf2Proxy> itf2_proxy{
1170 new org::chromium::Itf2Proxy{bus_, object_path}
1171 };
1172 auto p = itf2_instances_.emplace(object_path, std::move(itf2_proxy));
1173 if (!on_itf2_added_.is_null())
1174 on_itf2_added_.Run(p.first->second.get());
1175 return;
1176 }
1177 }
1178
1179 void ObjectRemoved(
1180 const dbus::ObjectPath& object_path,
1181 const std::string& interface_name) override {
1182 if (interface_name == "org.chromium.Itf1") {
1183 auto p = itf1_instances_.find(object_path);
1184 if (p != itf1_instances_.end()) {
1185 if (!on_itf1_removed_.is_null())
1186 on_itf1_removed_.Run(object_path);
1187 itf1_instances_.erase(p);
1188 }
1189 return;
1190 }
1191 if (interface_name == "org.chromium.Itf2") {
1192 auto p = itf2_instances_.find(object_path);
1193 if (p != itf2_instances_.end()) {
1194 if (!on_itf2_removed_.is_null())
1195 on_itf2_removed_.Run(object_path);
1196 itf2_instances_.erase(p);
1197 }
1198 return;
1199 }
1200 }
1201
1202 dbus::PropertySet* CreateProperties(
1203 dbus::ObjectProxy* object_proxy,
1204 const dbus::ObjectPath& object_path,
1205 const std::string& interface_name) override {
1206 if (interface_name == "org.chromium.Itf1") {
1207 return new org::chromium::Itf1Proxy::PropertySet{
1208 object_proxy,
1209 base::Bind(&ObjectManagerProxy::OnPropertyChanged,
1210 weak_ptr_factory_.GetWeakPtr(),
1211 object_path,
1212 interface_name)
1213 };
1214 }
1215 if (interface_name == "org.chromium.Itf2") {
1216 return new org::chromium::Itf2Proxy::PropertySet{
1217 object_proxy,
1218 base::Bind(&ObjectManagerProxy::OnPropertyChanged,
1219 weak_ptr_factory_.GetWeakPtr(),
1220 object_path,
1221 interface_name)
1222 };
1223 }
1224 LOG(FATAL) << "Creating properties for unsupported interface "
1225 << interface_name;
1226 return nullptr;
1227 }
1228
1229 scoped_refptr<dbus::Bus> bus_;
1230 dbus::ObjectManager* dbus_object_manager_;
1231 std::map<dbus::ObjectPath,
1232 std::unique_ptr<org::chromium::Itf1Proxy>> itf1_instances_;
1233 base::Callback<void(org::chromium::Itf1Proxy*)> on_itf1_added_;
1234 base::Callback<void(const dbus::ObjectPath&)> on_itf1_removed_;
1235 std::map<dbus::ObjectPath,
1236 std::unique_ptr<org::chromium::Itf2Proxy>> itf2_instances_;
1237 base::Callback<void(org::chromium::Itf2Proxy*)> on_itf2_added_;
1238 base::Callback<void(const dbus::ObjectPath&)> on_itf2_removed_;
1239 base::WeakPtrFactory<ObjectManagerProxy> weak_ptr_factory_{this};
1240
1241 DISALLOW_COPY_AND_ASSIGN(ObjectManagerProxy);
1242};
1243
1244} // namespace chromium
1245} // namespace org
1246)literal_string";
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001247} // namespace
1248
1249class ProxyGeneratorTest : public Test {
1250 public:
1251 void SetUp() override {
1252 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
1253 }
1254
1255 protected:
1256 base::FilePath CreateInputFile(const string& contents) {
1257 base::FilePath path;
1258 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &path));
Christopher Wileyd0cb3fe2015-07-27 17:32:58 -07001259 int written = base::WriteFile(path, contents.c_str(), contents.size());
1260 EXPECT_EQ(contents.size(), static_cast<size_t>(written));
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001261 return path;
1262 }
1263
1264 base::ScopedTempDir temp_dir_;
1265};
1266
1267TEST_F(ProxyGeneratorTest, GenerateAdaptors) {
1268 Interface interface;
Alex Vakulenkob69be602015-02-23 14:40:42 -08001269 interface.name = "org.chromium.TestInterface";
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001270 interface.path = "/org/chromium/Test";
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001271 interface.methods.emplace_back(
Alex Vakulenkob69be602015-02-23 14:40:42 -08001272 "Elements",
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001273 vector<Interface::Argument>{
Alex Vakulenkob69be602015-02-23 14:40:42 -08001274 {"space_walk", kDBusTypeString},
1275 {"ramblin_man", kDBusTypeArryOfObjects}},
1276 vector<Interface::Argument>{{"", kDBusTypeString}});
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001277 interface.methods.emplace_back(
Alex Vakulenkob69be602015-02-23 14:40:42 -08001278 "ReturnToPatagonia",
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001279 vector<Interface::Argument>{},
Alex Vakulenkob69be602015-02-23 14:40:42 -08001280 vector<Interface::Argument>{{"", kDBusTypeInt64}});
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001281 interface.methods.emplace_back(
Alex Vakulenkob69be602015-02-23 14:40:42 -08001282 "NiceWeatherForDucks",
1283 vector<Interface::Argument>{{"", kDBusTypeBool}},
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001284 vector<Interface::Argument>{});
Alex Vakulenkob69be602015-02-23 14:40:42 -08001285 interface.methods.emplace_back("ExperimentNumberSix");
1286 interface.signals.emplace_back("Closer");
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001287 interface.signals.emplace_back(
Alex Vakulenkob69be602015-02-23 14:40:42 -08001288 "TheCurseOfKaZar",
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001289 vector<Interface::Argument>{
Alex Vakulenkob69be602015-02-23 14:40:42 -08001290 {"", kDBusTypeArryOfStrings},
1291 {"", kDBusTypeByte}});
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001292 interface.methods.back().doc_string = "Comment line1\nline2";
Alex Vakulenkofafef132014-11-03 14:52:09 -08001293 Interface interface2;
Alex Vakulenkob69be602015-02-23 14:40:42 -08001294 interface2.name = "org.chromium.TestInterface2";
Alex Vakulenkofafef132014-11-03 14:52:09 -08001295 interface2.methods.emplace_back(
Alex Vakulenkob69be602015-02-23 14:40:42 -08001296 "GetPersonInfo",
Alex Vakulenkofafef132014-11-03 14:52:09 -08001297 vector<Interface::Argument>{},
1298 vector<Interface::Argument>{
Alex Vakulenkob69be602015-02-23 14:40:42 -08001299 {"name", kDBusTypeString},
1300 {"age", kDBusTypeInt32}});
Alex Vakulenkofafef132014-11-03 14:52:09 -08001301 vector<Interface> interfaces{interface, interface2};
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001302 base::FilePath output_path = temp_dir_.path().Append("output.h");
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001303 ServiceConfig config;
1304 EXPECT_TRUE(ProxyGenerator::GenerateProxies(config, interfaces, output_path));
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001305 string contents;
1306 EXPECT_TRUE(base::ReadFileToString(output_path, &contents));
1307 // The header guards contain the (temporary) filename, so we search for
1308 // the content we need within the string.
1309 EXPECT_NE(string::npos, contents.find(kExpectedContent))
1310 << "Expected to find the following content...\n"
1311 << kExpectedContent << "...within content...\n" << contents;
1312}
1313
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001314TEST_F(ProxyGeneratorTest, GenerateAdaptorsWithServiceName) {
1315 Interface interface;
Alex Vakulenkob69be602015-02-23 14:40:42 -08001316 interface.name = "org.chromium.TestInterface";
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001317 interface.path = "/org/chromium/Test";
Alex Vakulenkob69be602015-02-23 14:40:42 -08001318 interface.signals.emplace_back("Closer");
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001319 Interface interface2;
Alex Vakulenkob69be602015-02-23 14:40:42 -08001320 interface2.name = "org.chromium.TestInterface2";
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001321 vector<Interface> interfaces{interface, interface2};
1322 base::FilePath output_path = temp_dir_.path().Append("output2.h");
1323 ServiceConfig config;
1324 config.service_name = "org.chromium.Test";
1325 EXPECT_TRUE(ProxyGenerator::GenerateProxies(config, interfaces, output_path));
1326 string contents;
1327 EXPECT_TRUE(base::ReadFileToString(output_path, &contents));
1328 // The header guards contain the (temporary) filename, so we search for
1329 // the content we need within the string.
1330 EXPECT_NE(string::npos, contents.find(kExpectedContentWithService))
1331 << "Expected to find the following content...\n"
1332 << kExpectedContentWithService << "...within content...\n" << contents;
1333}
1334
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001335TEST_F(ProxyGeneratorTest, GenerateAdaptorsWithObjectManager) {
1336 Interface interface;
1337 interface.name = "org.chromium.Itf1";
1338 interface.path = "/org/chromium/Test/Object";
Alex Vakulenkob69be602015-02-23 14:40:42 -08001339 interface.signals.emplace_back("Closer");
Alex Vakulenko59dd2e92015-02-23 15:14:28 -08001340 interface.properties.emplace_back("Data", "s", "read");
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001341 Interface interface2;
1342 interface2.name = "org.chromium.Itf2";
1343 vector<Interface> interfaces{interface, interface2};
1344 base::FilePath output_path = temp_dir_.path().Append("output3.h");
1345 ServiceConfig config;
1346 config.object_manager.name = "org.chromium.ObjectManager";
1347 config.object_manager.object_path = "/org/chromium/Test";
1348 EXPECT_TRUE(ProxyGenerator::GenerateProxies(config, interfaces, output_path));
1349 string contents;
1350 EXPECT_TRUE(base::ReadFileToString(output_path, &contents));
1351 // The header guards contain the (temporary) filename, so we search for
1352 // the content we need within the string.
1353 EXPECT_NE(string::npos, contents.find(kExpectedContentWithObjectManager))
1354 << "Expected to find the following content...\n"
1355 << kExpectedContentWithObjectManager << "...within content...\n"
1356 << contents;
1357}
1358
1359TEST_F(ProxyGeneratorTest, GenerateAdaptorsWithObjectManagerAndServiceName) {
1360 Interface interface;
1361 interface.name = "org.chromium.Itf1";
1362 interface.path = "/org/chromium/Test/Object";
Alex Vakulenkob69be602015-02-23 14:40:42 -08001363 interface.signals.emplace_back("Closer");
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001364 Interface interface2;
1365 interface2.name = "org.chromium.Itf2";
1366 vector<Interface> interfaces{interface, interface2};
1367 base::FilePath output_path = temp_dir_.path().Append("output4.h");
1368 ServiceConfig config;
1369 config.service_name = "org.chromium.Test";
1370 config.object_manager.name = "org.chromium.ObjectManager";
1371 config.object_manager.object_path = "/org/chromium/Test";
1372 EXPECT_TRUE(ProxyGenerator::GenerateProxies(config, interfaces, output_path));
1373 string contents;
1374 EXPECT_TRUE(base::ReadFileToString(output_path, &contents));
1375 // The header guards contain the (temporary) filename, so we search for
1376 // the content we need within the string.
1377 EXPECT_NE(string::npos,
1378 contents.find(kExpectedContentWithObjectManagerAndServiceName))
1379 << "Expected to find the following content...\n"
1380 << kExpectedContentWithObjectManagerAndServiceName
1381 << "...within content...\n" << contents;
1382}
1383
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001384} // namespace chromeos_dbus_bindings