blob: a157656e2c4677e3a192483e23cef461b45fb6a8 [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:
110 ~TestInterfaceProxyInterface() = default;
111};
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
Christopher Wiley824b5242014-12-03 11:58:01 -0800131 ~TestInterfaceProxy() {
132 }
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:
319 ~TestInterface2ProxyInterface() = default;
320};
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
342 ~TestInterface2Proxy() {
343 }
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:
425 ~TestInterfaceProxyInterface() = default;
426};
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
Christopher Wiley824b5242014-12-03 11:58:01 -0800443 ~TestInterfaceProxy() {
444 }
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:
486 ~TestInterface2ProxyInterface() = default;
487};
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
507 ~TestInterface2Proxy() {
508 }
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:
571 ~Itf1ProxyInterface() = default;
572};
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
Christopher Wiley824b5242014-12-03 11:58:01 -0800610 ~Itf1Proxy() {
611 }
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:
673 ~Itf2ProxyInterface() = default;
674};
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
710 ~Itf2Proxy() {
711 }
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},
743 dbus_object_manager_{bus->GetObjectManager(
744 service_name,
745 dbus::ObjectPath{"/org/chromium/Test"})} {
746 dbus_object_manager_->RegisterInterface("org.chromium.Itf1", this);
747 dbus_object_manager_->RegisterInterface("org.chromium.Itf2", this);
748 }
749
Christopher Wileye11e81c2014-12-19 13:10:52 -0800750 ~ObjectManagerProxy() override {
751 dbus_object_manager_->UnregisterInterface("org.chromium.Itf1");
752 dbus_object_manager_->UnregisterInterface("org.chromium.Itf2");
753 }
754
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800755 dbus::ObjectManager* GetObjectManagerProxy() const {
756 return dbus_object_manager_;
757 }
758
Alex Vakulenkoc4e01f12015-02-23 15:55:21 -0800759 org::chromium::Itf1Proxy* GetItf1Proxy() {
760 if (itf1_instances_.empty())
761 return nullptr;
762 return itf1_instances_.begin()->second.get();
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800763 }
764 std::vector<org::chromium::Itf1Proxy*> GetItf1Instances() const {
765 std::vector<org::chromium::Itf1Proxy*> values;
766 values.reserve(itf1_instances_.size());
767 for (const auto& pair : itf1_instances_)
768 values.push_back(pair.second.get());
769 return values;
770 }
771 void SetItf1AddedCallback(
772 const base::Callback<void(org::chromium::Itf1Proxy*)>& callback) {
773 on_itf1_added_ = callback;
774 }
775 void SetItf1RemovedCallback(
776 const base::Callback<void(const dbus::ObjectPath&)>& callback) {
777 on_itf1_removed_ = callback;
778 }
779
780 org::chromium::Itf2Proxy* GetItf2Proxy(
781 const dbus::ObjectPath& object_path) {
782 auto p = itf2_instances_.find(object_path);
783 if (p != itf2_instances_.end())
784 return p->second.get();
785 return nullptr;
786 }
787 std::vector<org::chromium::Itf2Proxy*> GetItf2Instances() const {
788 std::vector<org::chromium::Itf2Proxy*> values;
789 values.reserve(itf2_instances_.size());
790 for (const auto& pair : itf2_instances_)
791 values.push_back(pair.second.get());
792 return values;
793 }
794 void SetItf2AddedCallback(
795 const base::Callback<void(org::chromium::Itf2Proxy*)>& callback) {
796 on_itf2_added_ = callback;
797 }
798 void SetItf2RemovedCallback(
799 const base::Callback<void(const dbus::ObjectPath&)>& callback) {
800 on_itf2_removed_ = callback;
801 }
802
803 private:
804 void OnPropertyChanged(const dbus::ObjectPath& object_path,
805 const std::string& interface_name,
806 const std::string& property_name) {
807 if (interface_name == "org.chromium.Itf1") {
808 auto p = itf1_instances_.find(object_path);
809 if (p == itf1_instances_.end())
810 return;
811 p->second->OnPropertyChanged(property_name);
812 return;
813 }
814 }
815
816 void ObjectAdded(
817 const dbus::ObjectPath& object_path,
818 const std::string& interface_name) override {
819 if (interface_name == "org.chromium.Itf1") {
820 auto property_set =
821 static_cast<org::chromium::Itf1Proxy::PropertySet*>(
822 dbus_object_manager_->GetProperties(object_path, interface_name));
823 std::unique_ptr<org::chromium::Itf1Proxy> itf1_proxy{
824 new org::chromium::Itf1Proxy{bus_, property_set}
825 };
826 auto p = itf1_instances_.emplace(object_path, std::move(itf1_proxy));
827 if (!on_itf1_added_.is_null())
828 on_itf1_added_.Run(p.first->second.get());
829 return;
830 }
831 if (interface_name == "org.chromium.Itf2") {
832 std::unique_ptr<org::chromium::Itf2Proxy> itf2_proxy{
833 new org::chromium::Itf2Proxy{bus_, object_path}
834 };
835 auto p = itf2_instances_.emplace(object_path, std::move(itf2_proxy));
836 if (!on_itf2_added_.is_null())
837 on_itf2_added_.Run(p.first->second.get());
838 return;
839 }
840 }
841
842 void ObjectRemoved(
843 const dbus::ObjectPath& object_path,
844 const std::string& interface_name) override {
845 if (interface_name == "org.chromium.Itf1") {
846 auto p = itf1_instances_.find(object_path);
847 if (p != itf1_instances_.end()) {
848 if (!on_itf1_removed_.is_null())
849 on_itf1_removed_.Run(object_path);
850 itf1_instances_.erase(p);
851 }
852 return;
853 }
854 if (interface_name == "org.chromium.Itf2") {
855 auto p = itf2_instances_.find(object_path);
856 if (p != itf2_instances_.end()) {
857 if (!on_itf2_removed_.is_null())
858 on_itf2_removed_.Run(object_path);
859 itf2_instances_.erase(p);
860 }
861 return;
862 }
863 }
864
865 dbus::PropertySet* CreateProperties(
866 dbus::ObjectProxy* object_proxy,
867 const dbus::ObjectPath& object_path,
868 const std::string& interface_name) override {
869 if (interface_name == "org.chromium.Itf1") {
870 return new org::chromium::Itf1Proxy::PropertySet{
871 object_proxy,
872 base::Bind(&ObjectManagerProxy::OnPropertyChanged,
873 weak_ptr_factory_.GetWeakPtr(),
874 object_path,
875 interface_name)
876 };
877 }
878 if (interface_name == "org.chromium.Itf2") {
879 return new org::chromium::Itf2Proxy::PropertySet{
880 object_proxy,
881 base::Bind(&ObjectManagerProxy::OnPropertyChanged,
882 weak_ptr_factory_.GetWeakPtr(),
883 object_path,
884 interface_name)
885 };
886 }
887 LOG(FATAL) << "Creating properties for unsupported interface "
888 << interface_name;
889 return nullptr;
890 }
891
892 scoped_refptr<dbus::Bus> bus_;
893 dbus::ObjectManager* dbus_object_manager_;
894 std::map<dbus::ObjectPath,
895 std::unique_ptr<org::chromium::Itf1Proxy>> itf1_instances_;
896 base::Callback<void(org::chromium::Itf1Proxy*)> on_itf1_added_;
897 base::Callback<void(const dbus::ObjectPath&)> on_itf1_removed_;
898 std::map<dbus::ObjectPath,
899 std::unique_ptr<org::chromium::Itf2Proxy>> itf2_instances_;
900 base::Callback<void(org::chromium::Itf2Proxy*)> on_itf2_added_;
901 base::Callback<void(const dbus::ObjectPath&)> on_itf2_removed_;
902 base::WeakPtrFactory<ObjectManagerProxy> weak_ptr_factory_{this};
903
904 DISALLOW_COPY_AND_ASSIGN(ObjectManagerProxy);
905};
906
907} // namespace chromium
908} // namespace org
909)literal_string";
910
911const char kExpectedContentWithObjectManagerAndServiceName[] = R"literal_string(
912#include <memory>
913#include <string>
914#include <vector>
915
916#include <base/bind.h>
917#include <base/callback.h>
918#include <base/logging.h>
919#include <base/macros.h>
920#include <base/memory/ref_counted.h>
921#include <chromeos/any.h>
922#include <chromeos/dbus/dbus_method_invoker.h>
923#include <chromeos/dbus/dbus_property.h>
924#include <chromeos/dbus/dbus_signal_handler.h>
925#include <chromeos/errors/error.h>
926#include <chromeos/variant_dictionary.h>
927#include <dbus/bus.h>
928#include <dbus/message.h>
929#include <dbus/object_manager.h>
930#include <dbus/object_path.h>
931#include <dbus/object_proxy.h>
932
933namespace org {
934namespace chromium {
935class ObjectManagerProxy;
936} // namespace chromium
937} // namespace org
938
939namespace org {
940namespace chromium {
941
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800942// Abstract interface proxy for org::chromium::Itf1.
943class Itf1ProxyInterface {
944 public:
945 protected:
946 ~Itf1ProxyInterface() = default;
947};
948
949} // namespace chromium
950} // namespace org
951
952namespace org {
953namespace chromium {
954
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800955// Interface proxy for org::chromium::Itf1.
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800956class Itf1Proxy final : public Itf1ProxyInterface {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800957 public:
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800958 class PropertySet : public dbus::PropertySet {
959 public:
960 PropertySet(dbus::ObjectProxy* object_proxy,
961 const PropertyChangedCallback& callback)
962 : dbus::PropertySet{object_proxy,
963 "org.chromium.Itf1",
964 callback} {
965 }
966
967
968 private:
969 DISALLOW_COPY_AND_ASSIGN(PropertySet);
970 };
971
972 Itf1Proxy(const scoped_refptr<dbus::Bus>& bus) :
973 bus_{bus},
974 dbus_object_proxy_{
975 bus_->GetObjectProxy(service_name_, object_path_)} {
976 }
977
Christopher Wiley824b5242014-12-03 11:58:01 -0800978 ~Itf1Proxy() {
979 }
980
981 void RegisterCloserSignalHandler(
982 const base::Closure& signal_callback,
983 dbus::ObjectProxy::OnConnectedCallback on_connected_callback) {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800984 chromeos::dbus_utils::ConnectToSignal(
985 dbus_object_proxy_,
986 "org.chromium.Itf1",
987 "Closer",
Christopher Wiley824b5242014-12-03 11:58:01 -0800988 signal_callback,
989 on_connected_callback);
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800990 }
991
992 void ReleaseObjectProxy(const base::Closure& callback) {
993 bus_->RemoveObjectProxy(service_name_, object_path_, callback);
994 }
995
996 const dbus::ObjectPath& GetObjectPath() const {
997 return object_path_;
998 }
999
1000 dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; }
1001
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001002 private:
1003 scoped_refptr<dbus::Bus> bus_;
1004 const std::string service_name_{"org.chromium.Test"};
1005 const dbus::ObjectPath object_path_{"/org/chromium/Test/Object"};
1006 dbus::ObjectProxy* dbus_object_proxy_;
1007
1008 DISALLOW_COPY_AND_ASSIGN(Itf1Proxy);
1009};
1010
1011} // namespace chromium
1012} // namespace org
1013
1014namespace org {
1015namespace chromium {
1016
Alex Vakulenko008e19d2015-01-21 10:37:00 -08001017// Abstract interface proxy for org::chromium::Itf2.
1018class Itf2ProxyInterface {
1019 public:
1020 protected:
1021 ~Itf2ProxyInterface() = default;
1022};
1023
1024} // namespace chromium
1025} // namespace org
1026
1027namespace org {
1028namespace chromium {
1029
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001030// Interface proxy for org::chromium::Itf2.
Alex Vakulenko008e19d2015-01-21 10:37:00 -08001031class Itf2Proxy final : public Itf2ProxyInterface {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001032 public:
1033 class PropertySet : public dbus::PropertySet {
1034 public:
1035 PropertySet(dbus::ObjectProxy* object_proxy,
1036 const PropertyChangedCallback& callback)
1037 : dbus::PropertySet{object_proxy,
1038 "org.chromium.Itf2",
1039 callback} {
1040 }
1041
1042
1043 private:
1044 DISALLOW_COPY_AND_ASSIGN(PropertySet);
1045 };
1046
1047 Itf2Proxy(
1048 const scoped_refptr<dbus::Bus>& bus,
1049 const dbus::ObjectPath& object_path) :
1050 bus_{bus},
1051 object_path_{object_path},
1052 dbus_object_proxy_{
1053 bus_->GetObjectProxy(service_name_, object_path_)} {
1054 }
1055
1056 ~Itf2Proxy() {
1057 }
1058
1059 void ReleaseObjectProxy(const base::Closure& callback) {
1060 bus_->RemoveObjectProxy(service_name_, object_path_, callback);
1061 }
1062
1063 const dbus::ObjectPath& GetObjectPath() const {
1064 return object_path_;
1065 }
1066
1067 dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; }
1068
1069 private:
1070 scoped_refptr<dbus::Bus> bus_;
1071 const std::string service_name_{"org.chromium.Test"};
1072 dbus::ObjectPath object_path_;
1073 dbus::ObjectProxy* dbus_object_proxy_;
1074
1075 DISALLOW_COPY_AND_ASSIGN(Itf2Proxy);
1076};
1077
1078} // namespace chromium
1079} // namespace org
1080
1081namespace org {
1082namespace chromium {
1083
1084class ObjectManagerProxy : public dbus::ObjectManager::Interface {
1085 public:
1086 ObjectManagerProxy(const scoped_refptr<dbus::Bus>& bus)
1087 : bus_{bus},
1088 dbus_object_manager_{bus->GetObjectManager(
1089 "org.chromium.Test",
1090 dbus::ObjectPath{"/org/chromium/Test"})} {
1091 dbus_object_manager_->RegisterInterface("org.chromium.Itf1", this);
1092 dbus_object_manager_->RegisterInterface("org.chromium.Itf2", this);
1093 }
1094
Christopher Wileye11e81c2014-12-19 13:10:52 -08001095 ~ObjectManagerProxy() override {
1096 dbus_object_manager_->UnregisterInterface("org.chromium.Itf1");
1097 dbus_object_manager_->UnregisterInterface("org.chromium.Itf2");
1098 }
1099
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001100 dbus::ObjectManager* GetObjectManagerProxy() const {
1101 return dbus_object_manager_;
1102 }
1103
Alex Vakulenkoc4e01f12015-02-23 15:55:21 -08001104 org::chromium::Itf1Proxy* GetItf1Proxy() {
1105 if (itf1_instances_.empty())
1106 return nullptr;
1107 return itf1_instances_.begin()->second.get();
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001108 }
1109 std::vector<org::chromium::Itf1Proxy*> GetItf1Instances() const {
1110 std::vector<org::chromium::Itf1Proxy*> values;
1111 values.reserve(itf1_instances_.size());
1112 for (const auto& pair : itf1_instances_)
1113 values.push_back(pair.second.get());
1114 return values;
1115 }
1116 void SetItf1AddedCallback(
1117 const base::Callback<void(org::chromium::Itf1Proxy*)>& callback) {
1118 on_itf1_added_ = callback;
1119 }
1120 void SetItf1RemovedCallback(
1121 const base::Callback<void(const dbus::ObjectPath&)>& callback) {
1122 on_itf1_removed_ = callback;
1123 }
1124
1125 org::chromium::Itf2Proxy* GetItf2Proxy(
1126 const dbus::ObjectPath& object_path) {
1127 auto p = itf2_instances_.find(object_path);
1128 if (p != itf2_instances_.end())
1129 return p->second.get();
1130 return nullptr;
1131 }
1132 std::vector<org::chromium::Itf2Proxy*> GetItf2Instances() const {
1133 std::vector<org::chromium::Itf2Proxy*> values;
1134 values.reserve(itf2_instances_.size());
1135 for (const auto& pair : itf2_instances_)
1136 values.push_back(pair.second.get());
1137 return values;
1138 }
1139 void SetItf2AddedCallback(
1140 const base::Callback<void(org::chromium::Itf2Proxy*)>& callback) {
1141 on_itf2_added_ = callback;
1142 }
1143 void SetItf2RemovedCallback(
1144 const base::Callback<void(const dbus::ObjectPath&)>& callback) {
1145 on_itf2_removed_ = callback;
1146 }
1147
1148 private:
1149 void OnPropertyChanged(const dbus::ObjectPath& object_path,
1150 const std::string& interface_name,
1151 const std::string& property_name) {
1152 }
1153
1154 void ObjectAdded(
1155 const dbus::ObjectPath& object_path,
1156 const std::string& interface_name) override {
1157 if (interface_name == "org.chromium.Itf1") {
1158 std::unique_ptr<org::chromium::Itf1Proxy> itf1_proxy{
1159 new org::chromium::Itf1Proxy{bus_}
1160 };
1161 auto p = itf1_instances_.emplace(object_path, std::move(itf1_proxy));
1162 if (!on_itf1_added_.is_null())
1163 on_itf1_added_.Run(p.first->second.get());
1164 return;
1165 }
1166 if (interface_name == "org.chromium.Itf2") {
1167 std::unique_ptr<org::chromium::Itf2Proxy> itf2_proxy{
1168 new org::chromium::Itf2Proxy{bus_, object_path}
1169 };
1170 auto p = itf2_instances_.emplace(object_path, std::move(itf2_proxy));
1171 if (!on_itf2_added_.is_null())
1172 on_itf2_added_.Run(p.first->second.get());
1173 return;
1174 }
1175 }
1176
1177 void ObjectRemoved(
1178 const dbus::ObjectPath& object_path,
1179 const std::string& interface_name) override {
1180 if (interface_name == "org.chromium.Itf1") {
1181 auto p = itf1_instances_.find(object_path);
1182 if (p != itf1_instances_.end()) {
1183 if (!on_itf1_removed_.is_null())
1184 on_itf1_removed_.Run(object_path);
1185 itf1_instances_.erase(p);
1186 }
1187 return;
1188 }
1189 if (interface_name == "org.chromium.Itf2") {
1190 auto p = itf2_instances_.find(object_path);
1191 if (p != itf2_instances_.end()) {
1192 if (!on_itf2_removed_.is_null())
1193 on_itf2_removed_.Run(object_path);
1194 itf2_instances_.erase(p);
1195 }
1196 return;
1197 }
1198 }
1199
1200 dbus::PropertySet* CreateProperties(
1201 dbus::ObjectProxy* object_proxy,
1202 const dbus::ObjectPath& object_path,
1203 const std::string& interface_name) override {
1204 if (interface_name == "org.chromium.Itf1") {
1205 return new org::chromium::Itf1Proxy::PropertySet{
1206 object_proxy,
1207 base::Bind(&ObjectManagerProxy::OnPropertyChanged,
1208 weak_ptr_factory_.GetWeakPtr(),
1209 object_path,
1210 interface_name)
1211 };
1212 }
1213 if (interface_name == "org.chromium.Itf2") {
1214 return new org::chromium::Itf2Proxy::PropertySet{
1215 object_proxy,
1216 base::Bind(&ObjectManagerProxy::OnPropertyChanged,
1217 weak_ptr_factory_.GetWeakPtr(),
1218 object_path,
1219 interface_name)
1220 };
1221 }
1222 LOG(FATAL) << "Creating properties for unsupported interface "
1223 << interface_name;
1224 return nullptr;
1225 }
1226
1227 scoped_refptr<dbus::Bus> bus_;
1228 dbus::ObjectManager* dbus_object_manager_;
1229 std::map<dbus::ObjectPath,
1230 std::unique_ptr<org::chromium::Itf1Proxy>> itf1_instances_;
1231 base::Callback<void(org::chromium::Itf1Proxy*)> on_itf1_added_;
1232 base::Callback<void(const dbus::ObjectPath&)> on_itf1_removed_;
1233 std::map<dbus::ObjectPath,
1234 std::unique_ptr<org::chromium::Itf2Proxy>> itf2_instances_;
1235 base::Callback<void(org::chromium::Itf2Proxy*)> on_itf2_added_;
1236 base::Callback<void(const dbus::ObjectPath&)> on_itf2_removed_;
1237 base::WeakPtrFactory<ObjectManagerProxy> weak_ptr_factory_{this};
1238
1239 DISALLOW_COPY_AND_ASSIGN(ObjectManagerProxy);
1240};
1241
1242} // namespace chromium
1243} // namespace org
1244)literal_string";
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001245} // namespace
1246
1247class ProxyGeneratorTest : public Test {
1248 public:
1249 void SetUp() override {
1250 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
1251 }
1252
1253 protected:
1254 base::FilePath CreateInputFile(const string& contents) {
1255 base::FilePath path;
1256 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &path));
1257 EXPECT_EQ(contents.size(),
1258 base::WriteFile(path, contents.c_str(), contents.size()));
1259 return path;
1260 }
1261
1262 base::ScopedTempDir temp_dir_;
1263};
1264
1265TEST_F(ProxyGeneratorTest, GenerateAdaptors) {
1266 Interface interface;
Alex Vakulenkob69be602015-02-23 14:40:42 -08001267 interface.name = "org.chromium.TestInterface";
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001268 interface.path = "/org/chromium/Test";
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001269 interface.methods.emplace_back(
Alex Vakulenkob69be602015-02-23 14:40:42 -08001270 "Elements",
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001271 vector<Interface::Argument>{
Alex Vakulenkob69be602015-02-23 14:40:42 -08001272 {"space_walk", kDBusTypeString},
1273 {"ramblin_man", kDBusTypeArryOfObjects}},
1274 vector<Interface::Argument>{{"", kDBusTypeString}});
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001275 interface.methods.emplace_back(
Alex Vakulenkob69be602015-02-23 14:40:42 -08001276 "ReturnToPatagonia",
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001277 vector<Interface::Argument>{},
Alex Vakulenkob69be602015-02-23 14:40:42 -08001278 vector<Interface::Argument>{{"", kDBusTypeInt64}});
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001279 interface.methods.emplace_back(
Alex Vakulenkob69be602015-02-23 14:40:42 -08001280 "NiceWeatherForDucks",
1281 vector<Interface::Argument>{{"", kDBusTypeBool}},
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001282 vector<Interface::Argument>{});
Alex Vakulenkob69be602015-02-23 14:40:42 -08001283 interface.methods.emplace_back("ExperimentNumberSix");
1284 interface.signals.emplace_back("Closer");
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001285 interface.signals.emplace_back(
Alex Vakulenkob69be602015-02-23 14:40:42 -08001286 "TheCurseOfKaZar",
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001287 vector<Interface::Argument>{
Alex Vakulenkob69be602015-02-23 14:40:42 -08001288 {"", kDBusTypeArryOfStrings},
1289 {"", kDBusTypeByte}});
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001290 interface.methods.back().doc_string = "Comment line1\nline2";
Alex Vakulenkofafef132014-11-03 14:52:09 -08001291 Interface interface2;
Alex Vakulenkob69be602015-02-23 14:40:42 -08001292 interface2.name = "org.chromium.TestInterface2";
Alex Vakulenkofafef132014-11-03 14:52:09 -08001293 interface2.methods.emplace_back(
Alex Vakulenkob69be602015-02-23 14:40:42 -08001294 "GetPersonInfo",
Alex Vakulenkofafef132014-11-03 14:52:09 -08001295 vector<Interface::Argument>{},
1296 vector<Interface::Argument>{
Alex Vakulenkob69be602015-02-23 14:40:42 -08001297 {"name", kDBusTypeString},
1298 {"age", kDBusTypeInt32}});
Alex Vakulenkofafef132014-11-03 14:52:09 -08001299 vector<Interface> interfaces{interface, interface2};
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001300 base::FilePath output_path = temp_dir_.path().Append("output.h");
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001301 ServiceConfig config;
1302 EXPECT_TRUE(ProxyGenerator::GenerateProxies(config, interfaces, output_path));
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001303 string contents;
1304 EXPECT_TRUE(base::ReadFileToString(output_path, &contents));
1305 // The header guards contain the (temporary) filename, so we search for
1306 // the content we need within the string.
1307 EXPECT_NE(string::npos, contents.find(kExpectedContent))
1308 << "Expected to find the following content...\n"
1309 << kExpectedContent << "...within content...\n" << contents;
1310}
1311
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001312TEST_F(ProxyGeneratorTest, GenerateAdaptorsWithServiceName) {
1313 Interface interface;
Alex Vakulenkob69be602015-02-23 14:40:42 -08001314 interface.name = "org.chromium.TestInterface";
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001315 interface.path = "/org/chromium/Test";
Alex Vakulenkob69be602015-02-23 14:40:42 -08001316 interface.signals.emplace_back("Closer");
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001317 Interface interface2;
Alex Vakulenkob69be602015-02-23 14:40:42 -08001318 interface2.name = "org.chromium.TestInterface2";
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001319 vector<Interface> interfaces{interface, interface2};
1320 base::FilePath output_path = temp_dir_.path().Append("output2.h");
1321 ServiceConfig config;
1322 config.service_name = "org.chromium.Test";
1323 EXPECT_TRUE(ProxyGenerator::GenerateProxies(config, interfaces, output_path));
1324 string contents;
1325 EXPECT_TRUE(base::ReadFileToString(output_path, &contents));
1326 // The header guards contain the (temporary) filename, so we search for
1327 // the content we need within the string.
1328 EXPECT_NE(string::npos, contents.find(kExpectedContentWithService))
1329 << "Expected to find the following content...\n"
1330 << kExpectedContentWithService << "...within content...\n" << contents;
1331}
1332
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001333TEST_F(ProxyGeneratorTest, GenerateAdaptorsWithObjectManager) {
1334 Interface interface;
1335 interface.name = "org.chromium.Itf1";
1336 interface.path = "/org/chromium/Test/Object";
Alex Vakulenkob69be602015-02-23 14:40:42 -08001337 interface.signals.emplace_back("Closer");
Alex Vakulenko59dd2e92015-02-23 15:14:28 -08001338 interface.properties.emplace_back("Data", "s", "read");
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001339 Interface interface2;
1340 interface2.name = "org.chromium.Itf2";
1341 vector<Interface> interfaces{interface, interface2};
1342 base::FilePath output_path = temp_dir_.path().Append("output3.h");
1343 ServiceConfig config;
1344 config.object_manager.name = "org.chromium.ObjectManager";
1345 config.object_manager.object_path = "/org/chromium/Test";
1346 EXPECT_TRUE(ProxyGenerator::GenerateProxies(config, interfaces, output_path));
1347 string contents;
1348 EXPECT_TRUE(base::ReadFileToString(output_path, &contents));
1349 // The header guards contain the (temporary) filename, so we search for
1350 // the content we need within the string.
1351 EXPECT_NE(string::npos, contents.find(kExpectedContentWithObjectManager))
1352 << "Expected to find the following content...\n"
1353 << kExpectedContentWithObjectManager << "...within content...\n"
1354 << contents;
1355}
1356
1357TEST_F(ProxyGeneratorTest, GenerateAdaptorsWithObjectManagerAndServiceName) {
1358 Interface interface;
1359 interface.name = "org.chromium.Itf1";
1360 interface.path = "/org/chromium/Test/Object";
Alex Vakulenkob69be602015-02-23 14:40:42 -08001361 interface.signals.emplace_back("Closer");
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001362 Interface interface2;
1363 interface2.name = "org.chromium.Itf2";
1364 vector<Interface> interfaces{interface, interface2};
1365 base::FilePath output_path = temp_dir_.path().Append("output4.h");
1366 ServiceConfig config;
1367 config.service_name = "org.chromium.Test";
1368 config.object_manager.name = "org.chromium.ObjectManager";
1369 config.object_manager.object_path = "/org/chromium/Test";
1370 EXPECT_TRUE(ProxyGenerator::GenerateProxies(config, interfaces, output_path));
1371 string contents;
1372 EXPECT_TRUE(base::ReadFileToString(output_path, &contents));
1373 // The header guards contain the (temporary) filename, so we search for
1374 // the content we need within the string.
1375 EXPECT_NE(string::npos,
1376 contents.find(kExpectedContentWithObjectManagerAndServiceName))
1377 << "Expected to find the following content...\n"
1378 << kExpectedContentWithObjectManagerAndServiceName
1379 << "...within content...\n" << contents;
1380}
1381
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001382} // namespace chromeos_dbus_bindings