blob: f0615a9c474b21c4a9adf8950b044680ec93379e [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
759 org::chromium::Itf1Proxy* GetItf1Proxy(
760 const dbus::ObjectPath& object_path) {
761 auto p = itf1_instances_.find(object_path);
762 if (p != itf1_instances_.end())
763 return p->second.get();
764 return nullptr;
765 }
766 std::vector<org::chromium::Itf1Proxy*> GetItf1Instances() const {
767 std::vector<org::chromium::Itf1Proxy*> values;
768 values.reserve(itf1_instances_.size());
769 for (const auto& pair : itf1_instances_)
770 values.push_back(pair.second.get());
771 return values;
772 }
773 void SetItf1AddedCallback(
774 const base::Callback<void(org::chromium::Itf1Proxy*)>& callback) {
775 on_itf1_added_ = callback;
776 }
777 void SetItf1RemovedCallback(
778 const base::Callback<void(const dbus::ObjectPath&)>& callback) {
779 on_itf1_removed_ = callback;
780 }
781
782 org::chromium::Itf2Proxy* GetItf2Proxy(
783 const dbus::ObjectPath& object_path) {
784 auto p = itf2_instances_.find(object_path);
785 if (p != itf2_instances_.end())
786 return p->second.get();
787 return nullptr;
788 }
789 std::vector<org::chromium::Itf2Proxy*> GetItf2Instances() const {
790 std::vector<org::chromium::Itf2Proxy*> values;
791 values.reserve(itf2_instances_.size());
792 for (const auto& pair : itf2_instances_)
793 values.push_back(pair.second.get());
794 return values;
795 }
796 void SetItf2AddedCallback(
797 const base::Callback<void(org::chromium::Itf2Proxy*)>& callback) {
798 on_itf2_added_ = callback;
799 }
800 void SetItf2RemovedCallback(
801 const base::Callback<void(const dbus::ObjectPath&)>& callback) {
802 on_itf2_removed_ = callback;
803 }
804
805 private:
806 void OnPropertyChanged(const dbus::ObjectPath& object_path,
807 const std::string& interface_name,
808 const std::string& property_name) {
809 if (interface_name == "org.chromium.Itf1") {
810 auto p = itf1_instances_.find(object_path);
811 if (p == itf1_instances_.end())
812 return;
813 p->second->OnPropertyChanged(property_name);
814 return;
815 }
816 }
817
818 void ObjectAdded(
819 const dbus::ObjectPath& object_path,
820 const std::string& interface_name) override {
821 if (interface_name == "org.chromium.Itf1") {
822 auto property_set =
823 static_cast<org::chromium::Itf1Proxy::PropertySet*>(
824 dbus_object_manager_->GetProperties(object_path, interface_name));
825 std::unique_ptr<org::chromium::Itf1Proxy> itf1_proxy{
826 new org::chromium::Itf1Proxy{bus_, property_set}
827 };
828 auto p = itf1_instances_.emplace(object_path, std::move(itf1_proxy));
829 if (!on_itf1_added_.is_null())
830 on_itf1_added_.Run(p.first->second.get());
831 return;
832 }
833 if (interface_name == "org.chromium.Itf2") {
834 std::unique_ptr<org::chromium::Itf2Proxy> itf2_proxy{
835 new org::chromium::Itf2Proxy{bus_, object_path}
836 };
837 auto p = itf2_instances_.emplace(object_path, std::move(itf2_proxy));
838 if (!on_itf2_added_.is_null())
839 on_itf2_added_.Run(p.first->second.get());
840 return;
841 }
842 }
843
844 void ObjectRemoved(
845 const dbus::ObjectPath& object_path,
846 const std::string& interface_name) override {
847 if (interface_name == "org.chromium.Itf1") {
848 auto p = itf1_instances_.find(object_path);
849 if (p != itf1_instances_.end()) {
850 if (!on_itf1_removed_.is_null())
851 on_itf1_removed_.Run(object_path);
852 itf1_instances_.erase(p);
853 }
854 return;
855 }
856 if (interface_name == "org.chromium.Itf2") {
857 auto p = itf2_instances_.find(object_path);
858 if (p != itf2_instances_.end()) {
859 if (!on_itf2_removed_.is_null())
860 on_itf2_removed_.Run(object_path);
861 itf2_instances_.erase(p);
862 }
863 return;
864 }
865 }
866
867 dbus::PropertySet* CreateProperties(
868 dbus::ObjectProxy* object_proxy,
869 const dbus::ObjectPath& object_path,
870 const std::string& interface_name) override {
871 if (interface_name == "org.chromium.Itf1") {
872 return new org::chromium::Itf1Proxy::PropertySet{
873 object_proxy,
874 base::Bind(&ObjectManagerProxy::OnPropertyChanged,
875 weak_ptr_factory_.GetWeakPtr(),
876 object_path,
877 interface_name)
878 };
879 }
880 if (interface_name == "org.chromium.Itf2") {
881 return new org::chromium::Itf2Proxy::PropertySet{
882 object_proxy,
883 base::Bind(&ObjectManagerProxy::OnPropertyChanged,
884 weak_ptr_factory_.GetWeakPtr(),
885 object_path,
886 interface_name)
887 };
888 }
889 LOG(FATAL) << "Creating properties for unsupported interface "
890 << interface_name;
891 return nullptr;
892 }
893
894 scoped_refptr<dbus::Bus> bus_;
895 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:
948 ~Itf1ProxyInterface() = default;
949};
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
Christopher Wiley824b5242014-12-03 11:58:01 -0800980 ~Itf1Proxy() {
981 }
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:
1023 ~Itf2ProxyInterface() = default;
1024};
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
1058 ~Itf2Proxy() {
1059 }
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
1106 org::chromium::Itf1Proxy* GetItf1Proxy(
1107 const dbus::ObjectPath& object_path) {
1108 auto p = itf1_instances_.find(object_path);
1109 if (p != itf1_instances_.end())
1110 return p->second.get();
1111 return nullptr;
1112 }
1113 std::vector<org::chromium::Itf1Proxy*> GetItf1Instances() const {
1114 std::vector<org::chromium::Itf1Proxy*> values;
1115 values.reserve(itf1_instances_.size());
1116 for (const auto& pair : itf1_instances_)
1117 values.push_back(pair.second.get());
1118 return values;
1119 }
1120 void SetItf1AddedCallback(
1121 const base::Callback<void(org::chromium::Itf1Proxy*)>& callback) {
1122 on_itf1_added_ = callback;
1123 }
1124 void SetItf1RemovedCallback(
1125 const base::Callback<void(const dbus::ObjectPath&)>& callback) {
1126 on_itf1_removed_ = callback;
1127 }
1128
1129 org::chromium::Itf2Proxy* GetItf2Proxy(
1130 const dbus::ObjectPath& object_path) {
1131 auto p = itf2_instances_.find(object_path);
1132 if (p != itf2_instances_.end())
1133 return p->second.get();
1134 return nullptr;
1135 }
1136 std::vector<org::chromium::Itf2Proxy*> GetItf2Instances() const {
1137 std::vector<org::chromium::Itf2Proxy*> values;
1138 values.reserve(itf2_instances_.size());
1139 for (const auto& pair : itf2_instances_)
1140 values.push_back(pair.second.get());
1141 return values;
1142 }
1143 void SetItf2AddedCallback(
1144 const base::Callback<void(org::chromium::Itf2Proxy*)>& callback) {
1145 on_itf2_added_ = callback;
1146 }
1147 void SetItf2RemovedCallback(
1148 const base::Callback<void(const dbus::ObjectPath&)>& callback) {
1149 on_itf2_removed_ = callback;
1150 }
1151
1152 private:
1153 void OnPropertyChanged(const dbus::ObjectPath& object_path,
1154 const std::string& interface_name,
1155 const std::string& property_name) {
1156 }
1157
1158 void ObjectAdded(
1159 const dbus::ObjectPath& object_path,
1160 const std::string& interface_name) override {
1161 if (interface_name == "org.chromium.Itf1") {
1162 std::unique_ptr<org::chromium::Itf1Proxy> itf1_proxy{
1163 new org::chromium::Itf1Proxy{bus_}
1164 };
1165 auto p = itf1_instances_.emplace(object_path, std::move(itf1_proxy));
1166 if (!on_itf1_added_.is_null())
1167 on_itf1_added_.Run(p.first->second.get());
1168 return;
1169 }
1170 if (interface_name == "org.chromium.Itf2") {
1171 std::unique_ptr<org::chromium::Itf2Proxy> itf2_proxy{
1172 new org::chromium::Itf2Proxy{bus_, object_path}
1173 };
1174 auto p = itf2_instances_.emplace(object_path, std::move(itf2_proxy));
1175 if (!on_itf2_added_.is_null())
1176 on_itf2_added_.Run(p.first->second.get());
1177 return;
1178 }
1179 }
1180
1181 void ObjectRemoved(
1182 const dbus::ObjectPath& object_path,
1183 const std::string& interface_name) override {
1184 if (interface_name == "org.chromium.Itf1") {
1185 auto p = itf1_instances_.find(object_path);
1186 if (p != itf1_instances_.end()) {
1187 if (!on_itf1_removed_.is_null())
1188 on_itf1_removed_.Run(object_path);
1189 itf1_instances_.erase(p);
1190 }
1191 return;
1192 }
1193 if (interface_name == "org.chromium.Itf2") {
1194 auto p = itf2_instances_.find(object_path);
1195 if (p != itf2_instances_.end()) {
1196 if (!on_itf2_removed_.is_null())
1197 on_itf2_removed_.Run(object_path);
1198 itf2_instances_.erase(p);
1199 }
1200 return;
1201 }
1202 }
1203
1204 dbus::PropertySet* CreateProperties(
1205 dbus::ObjectProxy* object_proxy,
1206 const dbus::ObjectPath& object_path,
1207 const std::string& interface_name) override {
1208 if (interface_name == "org.chromium.Itf1") {
1209 return new org::chromium::Itf1Proxy::PropertySet{
1210 object_proxy,
1211 base::Bind(&ObjectManagerProxy::OnPropertyChanged,
1212 weak_ptr_factory_.GetWeakPtr(),
1213 object_path,
1214 interface_name)
1215 };
1216 }
1217 if (interface_name == "org.chromium.Itf2") {
1218 return new org::chromium::Itf2Proxy::PropertySet{
1219 object_proxy,
1220 base::Bind(&ObjectManagerProxy::OnPropertyChanged,
1221 weak_ptr_factory_.GetWeakPtr(),
1222 object_path,
1223 interface_name)
1224 };
1225 }
1226 LOG(FATAL) << "Creating properties for unsupported interface "
1227 << interface_name;
1228 return nullptr;
1229 }
1230
1231 scoped_refptr<dbus::Bus> bus_;
1232 dbus::ObjectManager* dbus_object_manager_;
1233 std::map<dbus::ObjectPath,
1234 std::unique_ptr<org::chromium::Itf1Proxy>> itf1_instances_;
1235 base::Callback<void(org::chromium::Itf1Proxy*)> on_itf1_added_;
1236 base::Callback<void(const dbus::ObjectPath&)> on_itf1_removed_;
1237 std::map<dbus::ObjectPath,
1238 std::unique_ptr<org::chromium::Itf2Proxy>> itf2_instances_;
1239 base::Callback<void(org::chromium::Itf2Proxy*)> on_itf2_added_;
1240 base::Callback<void(const dbus::ObjectPath&)> on_itf2_removed_;
1241 base::WeakPtrFactory<ObjectManagerProxy> weak_ptr_factory_{this};
1242
1243 DISALLOW_COPY_AND_ASSIGN(ObjectManagerProxy);
1244};
1245
1246} // namespace chromium
1247} // namespace org
1248)literal_string";
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001249} // namespace
1250
1251class ProxyGeneratorTest : public Test {
1252 public:
1253 void SetUp() override {
1254 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
1255 }
1256
1257 protected:
1258 base::FilePath CreateInputFile(const string& contents) {
1259 base::FilePath path;
1260 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &path));
1261 EXPECT_EQ(contents.size(),
1262 base::WriteFile(path, contents.c_str(), contents.size()));
1263 return path;
1264 }
1265
1266 base::ScopedTempDir temp_dir_;
1267};
1268
1269TEST_F(ProxyGeneratorTest, GenerateAdaptors) {
1270 Interface interface;
Alex Vakulenkob69be602015-02-23 14:40:42 -08001271 interface.name = "org.chromium.TestInterface";
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001272 interface.path = "/org/chromium/Test";
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001273 interface.methods.emplace_back(
Alex Vakulenkob69be602015-02-23 14:40:42 -08001274 "Elements",
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001275 vector<Interface::Argument>{
Alex Vakulenkob69be602015-02-23 14:40:42 -08001276 {"space_walk", kDBusTypeString},
1277 {"ramblin_man", kDBusTypeArryOfObjects}},
1278 vector<Interface::Argument>{{"", kDBusTypeString}});
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001279 interface.methods.emplace_back(
Alex Vakulenkob69be602015-02-23 14:40:42 -08001280 "ReturnToPatagonia",
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001281 vector<Interface::Argument>{},
Alex Vakulenkob69be602015-02-23 14:40:42 -08001282 vector<Interface::Argument>{{"", kDBusTypeInt64}});
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001283 interface.methods.emplace_back(
Alex Vakulenkob69be602015-02-23 14:40:42 -08001284 "NiceWeatherForDucks",
1285 vector<Interface::Argument>{{"", kDBusTypeBool}},
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001286 vector<Interface::Argument>{});
Alex Vakulenkob69be602015-02-23 14:40:42 -08001287 interface.methods.emplace_back("ExperimentNumberSix");
1288 interface.signals.emplace_back("Closer");
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001289 interface.signals.emplace_back(
Alex Vakulenkob69be602015-02-23 14:40:42 -08001290 "TheCurseOfKaZar",
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001291 vector<Interface::Argument>{
Alex Vakulenkob69be602015-02-23 14:40:42 -08001292 {"", kDBusTypeArryOfStrings},
1293 {"", kDBusTypeByte}});
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001294 interface.methods.back().doc_string = "Comment line1\nline2";
Alex Vakulenkofafef132014-11-03 14:52:09 -08001295 Interface interface2;
Alex Vakulenkob69be602015-02-23 14:40:42 -08001296 interface2.name = "org.chromium.TestInterface2";
Alex Vakulenkofafef132014-11-03 14:52:09 -08001297 interface2.methods.emplace_back(
Alex Vakulenkob69be602015-02-23 14:40:42 -08001298 "GetPersonInfo",
Alex Vakulenkofafef132014-11-03 14:52:09 -08001299 vector<Interface::Argument>{},
1300 vector<Interface::Argument>{
Alex Vakulenkob69be602015-02-23 14:40:42 -08001301 {"name", kDBusTypeString},
1302 {"age", kDBusTypeInt32}});
Alex Vakulenkofafef132014-11-03 14:52:09 -08001303 vector<Interface> interfaces{interface, interface2};
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001304 base::FilePath output_path = temp_dir_.path().Append("output.h");
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001305 ServiceConfig config;
1306 EXPECT_TRUE(ProxyGenerator::GenerateProxies(config, interfaces, output_path));
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001307 string contents;
1308 EXPECT_TRUE(base::ReadFileToString(output_path, &contents));
1309 // The header guards contain the (temporary) filename, so we search for
1310 // the content we need within the string.
1311 EXPECT_NE(string::npos, contents.find(kExpectedContent))
1312 << "Expected to find the following content...\n"
1313 << kExpectedContent << "...within content...\n" << contents;
1314}
1315
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001316TEST_F(ProxyGeneratorTest, GenerateAdaptorsWithServiceName) {
1317 Interface interface;
Alex Vakulenkob69be602015-02-23 14:40:42 -08001318 interface.name = "org.chromium.TestInterface";
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001319 interface.path = "/org/chromium/Test";
Alex Vakulenkob69be602015-02-23 14:40:42 -08001320 interface.signals.emplace_back("Closer");
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001321 Interface interface2;
Alex Vakulenkob69be602015-02-23 14:40:42 -08001322 interface2.name = "org.chromium.TestInterface2";
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001323 vector<Interface> interfaces{interface, interface2};
1324 base::FilePath output_path = temp_dir_.path().Append("output2.h");
1325 ServiceConfig config;
1326 config.service_name = "org.chromium.Test";
1327 EXPECT_TRUE(ProxyGenerator::GenerateProxies(config, interfaces, output_path));
1328 string contents;
1329 EXPECT_TRUE(base::ReadFileToString(output_path, &contents));
1330 // The header guards contain the (temporary) filename, so we search for
1331 // the content we need within the string.
1332 EXPECT_NE(string::npos, contents.find(kExpectedContentWithService))
1333 << "Expected to find the following content...\n"
1334 << kExpectedContentWithService << "...within content...\n" << contents;
1335}
1336
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001337TEST_F(ProxyGeneratorTest, GenerateAdaptorsWithObjectManager) {
1338 Interface interface;
1339 interface.name = "org.chromium.Itf1";
1340 interface.path = "/org/chromium/Test/Object";
Alex Vakulenkob69be602015-02-23 14:40:42 -08001341 interface.signals.emplace_back("Closer");
Alex Vakulenko59dd2e92015-02-23 15:14:28 -08001342 interface.properties.emplace_back("Data", "s", "read");
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001343 Interface interface2;
1344 interface2.name = "org.chromium.Itf2";
1345 vector<Interface> interfaces{interface, interface2};
1346 base::FilePath output_path = temp_dir_.path().Append("output3.h");
1347 ServiceConfig config;
1348 config.object_manager.name = "org.chromium.ObjectManager";
1349 config.object_manager.object_path = "/org/chromium/Test";
1350 EXPECT_TRUE(ProxyGenerator::GenerateProxies(config, interfaces, output_path));
1351 string contents;
1352 EXPECT_TRUE(base::ReadFileToString(output_path, &contents));
1353 // The header guards contain the (temporary) filename, so we search for
1354 // the content we need within the string.
1355 EXPECT_NE(string::npos, contents.find(kExpectedContentWithObjectManager))
1356 << "Expected to find the following content...\n"
1357 << kExpectedContentWithObjectManager << "...within content...\n"
1358 << contents;
1359}
1360
1361TEST_F(ProxyGeneratorTest, GenerateAdaptorsWithObjectManagerAndServiceName) {
1362 Interface interface;
1363 interface.name = "org.chromium.Itf1";
1364 interface.path = "/org/chromium/Test/Object";
Alex Vakulenkob69be602015-02-23 14:40:42 -08001365 interface.signals.emplace_back("Closer");
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001366 Interface interface2;
1367 interface2.name = "org.chromium.Itf2";
1368 vector<Interface> interfaces{interface, interface2};
1369 base::FilePath output_path = temp_dir_.path().Append("output4.h");
1370 ServiceConfig config;
1371 config.service_name = "org.chromium.Test";
1372 config.object_manager.name = "org.chromium.ObjectManager";
1373 config.object_manager.object_path = "/org/chromium/Test";
1374 EXPECT_TRUE(ProxyGenerator::GenerateProxies(config, interfaces, output_path));
1375 string contents;
1376 EXPECT_TRUE(base::ReadFileToString(output_path, &contents));
1377 // The header guards contain the (temporary) filename, so we search for
1378 // the content we need within the string.
1379 EXPECT_NE(string::npos,
1380 contents.find(kExpectedContentWithObjectManagerAndServiceName))
1381 << "Expected to find the following content...\n"
1382 << kExpectedContentWithObjectManagerAndServiceName
1383 << "...within content...\n" << contents;
1384}
1385
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001386} // namespace chromeos_dbus_bindings