blob: 1c04fbe083e4351f7ec57cdee0abd9c3bf9d01cf [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:
567 virtual const std::string& data() const = 0;
568
569 protected:
570 ~Itf1ProxyInterface() = default;
571};
572
573} // namespace chromium
574} // namespace org
575
576namespace org {
577namespace chromium {
578
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800579// Interface proxy for org::chromium::Itf1.
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800580class Itf1Proxy final : public Itf1ProxyInterface {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800581 public:
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800582 class PropertySet : public dbus::PropertySet {
583 public:
584 PropertySet(dbus::ObjectProxy* object_proxy,
585 const PropertyChangedCallback& callback)
586 : dbus::PropertySet{object_proxy,
587 "org.chromium.Itf1",
588 callback} {
589 RegisterProperty("data", &data);
590 }
591
592 chromeos::dbus_utils::Property<std::string> data;
593
594 private:
595 DISALLOW_COPY_AND_ASSIGN(PropertySet);
596 };
597
598 Itf1Proxy(
599 const scoped_refptr<dbus::Bus>& bus,
600 const std::string& service_name,
601 PropertySet* property_set) :
602 bus_{bus},
603 service_name_{service_name},
604 property_set_{property_set},
605 dbus_object_proxy_{
606 bus_->GetObjectProxy(service_name_, object_path_)} {
607 }
608
Christopher Wiley824b5242014-12-03 11:58:01 -0800609 ~Itf1Proxy() {
610 }
611
612 void RegisterCloserSignalHandler(
613 const base::Closure& signal_callback,
614 dbus::ObjectProxy::OnConnectedCallback on_connected_callback) {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800615 chromeos::dbus_utils::ConnectToSignal(
616 dbus_object_proxy_,
617 "org.chromium.Itf1",
618 "Closer",
Christopher Wiley824b5242014-12-03 11:58:01 -0800619 signal_callback,
620 on_connected_callback);
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800621 }
622
623 void ReleaseObjectProxy(const base::Closure& callback) {
624 bus_->RemoveObjectProxy(service_name_, object_path_, callback);
625 }
626
627 const dbus::ObjectPath& GetObjectPath() const {
628 return object_path_;
629 }
630
631 dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; }
632
633 void SetPropertyChangedCallback(
634 const base::Callback<void(Itf1Proxy*, const std::string&)>& callback) {
635 on_property_changed_ = callback;
636 }
637
638 const PropertySet* GetProperties() const { return property_set_; }
639 PropertySet* GetProperties() { return property_set_; }
640
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800641 const std::string& data() const override {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800642 return property_set_->data.value();
643 }
644
645 private:
646 void OnPropertyChanged(const std::string& property_name) {
647 if (!on_property_changed_.is_null())
648 on_property_changed_.Run(this, property_name);
649 }
650
651 scoped_refptr<dbus::Bus> bus_;
652 std::string service_name_;
653 const dbus::ObjectPath object_path_{"/org/chromium/Test/Object"};
654 PropertySet* property_set_;
655 base::Callback<void(Itf1Proxy*, const std::string&)> on_property_changed_;
656 dbus::ObjectProxy* dbus_object_proxy_;
657
658 friend class org::chromium::ObjectManagerProxy;
659 DISALLOW_COPY_AND_ASSIGN(Itf1Proxy);
660};
661
662} // namespace chromium
663} // namespace org
664
665namespace org {
666namespace chromium {
667
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800668// Abstract interface proxy for org::chromium::Itf2.
669class Itf2ProxyInterface {
670 public:
671 protected:
672 ~Itf2ProxyInterface() = default;
673};
674
675} // namespace chromium
676} // namespace org
677
678namespace org {
679namespace chromium {
680
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800681// Interface proxy for org::chromium::Itf2.
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800682class Itf2Proxy final : public Itf2ProxyInterface {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800683 public:
684 class PropertySet : public dbus::PropertySet {
685 public:
686 PropertySet(dbus::ObjectProxy* object_proxy,
687 const PropertyChangedCallback& callback)
688 : dbus::PropertySet{object_proxy,
689 "org.chromium.Itf2",
690 callback} {
691 }
692
693
694 private:
695 DISALLOW_COPY_AND_ASSIGN(PropertySet);
696 };
697
698 Itf2Proxy(
699 const scoped_refptr<dbus::Bus>& bus,
700 const std::string& service_name,
701 const dbus::ObjectPath& object_path) :
702 bus_{bus},
703 service_name_{service_name},
704 object_path_{object_path},
705 dbus_object_proxy_{
706 bus_->GetObjectProxy(service_name_, object_path_)} {
707 }
708
709 ~Itf2Proxy() {
710 }
711
712 void ReleaseObjectProxy(const base::Closure& callback) {
713 bus_->RemoveObjectProxy(service_name_, object_path_, callback);
714 }
715
716 const dbus::ObjectPath& GetObjectPath() const {
717 return object_path_;
718 }
719
720 dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; }
721
722 private:
723 scoped_refptr<dbus::Bus> bus_;
724 std::string service_name_;
725 dbus::ObjectPath object_path_;
726 dbus::ObjectProxy* dbus_object_proxy_;
727
728 DISALLOW_COPY_AND_ASSIGN(Itf2Proxy);
729};
730
731} // namespace chromium
732} // namespace org
733
734namespace org {
735namespace chromium {
736
737class ObjectManagerProxy : public dbus::ObjectManager::Interface {
738 public:
739 ObjectManagerProxy(const scoped_refptr<dbus::Bus>& bus,
740 const std::string& service_name)
741 : bus_{bus},
742 dbus_object_manager_{bus->GetObjectManager(
743 service_name,
744 dbus::ObjectPath{"/org/chromium/Test"})} {
745 dbus_object_manager_->RegisterInterface("org.chromium.Itf1", this);
746 dbus_object_manager_->RegisterInterface("org.chromium.Itf2", this);
747 }
748
Christopher Wileye11e81c2014-12-19 13:10:52 -0800749 ~ObjectManagerProxy() override {
750 dbus_object_manager_->UnregisterInterface("org.chromium.Itf1");
751 dbus_object_manager_->UnregisterInterface("org.chromium.Itf2");
752 }
753
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800754 dbus::ObjectManager* GetObjectManagerProxy() const {
755 return dbus_object_manager_;
756 }
757
758 org::chromium::Itf1Proxy* GetItf1Proxy(
759 const dbus::ObjectPath& object_path) {
760 auto p = itf1_instances_.find(object_path);
761 if (p != itf1_instances_.end())
762 return p->second.get();
763 return nullptr;
764 }
765 std::vector<org::chromium::Itf1Proxy*> GetItf1Instances() const {
766 std::vector<org::chromium::Itf1Proxy*> values;
767 values.reserve(itf1_instances_.size());
768 for (const auto& pair : itf1_instances_)
769 values.push_back(pair.second.get());
770 return values;
771 }
772 void SetItf1AddedCallback(
773 const base::Callback<void(org::chromium::Itf1Proxy*)>& callback) {
774 on_itf1_added_ = callback;
775 }
776 void SetItf1RemovedCallback(
777 const base::Callback<void(const dbus::ObjectPath&)>& callback) {
778 on_itf1_removed_ = callback;
779 }
780
781 org::chromium::Itf2Proxy* GetItf2Proxy(
782 const dbus::ObjectPath& object_path) {
783 auto p = itf2_instances_.find(object_path);
784 if (p != itf2_instances_.end())
785 return p->second.get();
786 return nullptr;
787 }
788 std::vector<org::chromium::Itf2Proxy*> GetItf2Instances() const {
789 std::vector<org::chromium::Itf2Proxy*> values;
790 values.reserve(itf2_instances_.size());
791 for (const auto& pair : itf2_instances_)
792 values.push_back(pair.second.get());
793 return values;
794 }
795 void SetItf2AddedCallback(
796 const base::Callback<void(org::chromium::Itf2Proxy*)>& callback) {
797 on_itf2_added_ = callback;
798 }
799 void SetItf2RemovedCallback(
800 const base::Callback<void(const dbus::ObjectPath&)>& callback) {
801 on_itf2_removed_ = callback;
802 }
803
804 private:
805 void OnPropertyChanged(const dbus::ObjectPath& object_path,
806 const std::string& interface_name,
807 const std::string& property_name) {
808 if (interface_name == "org.chromium.Itf1") {
809 auto p = itf1_instances_.find(object_path);
810 if (p == itf1_instances_.end())
811 return;
812 p->second->OnPropertyChanged(property_name);
813 return;
814 }
815 }
816
817 void ObjectAdded(
818 const dbus::ObjectPath& object_path,
819 const std::string& interface_name) override {
820 if (interface_name == "org.chromium.Itf1") {
821 auto property_set =
822 static_cast<org::chromium::Itf1Proxy::PropertySet*>(
823 dbus_object_manager_->GetProperties(object_path, interface_name));
824 std::unique_ptr<org::chromium::Itf1Proxy> itf1_proxy{
825 new org::chromium::Itf1Proxy{bus_, property_set}
826 };
827 auto p = itf1_instances_.emplace(object_path, std::move(itf1_proxy));
828 if (!on_itf1_added_.is_null())
829 on_itf1_added_.Run(p.first->second.get());
830 return;
831 }
832 if (interface_name == "org.chromium.Itf2") {
833 std::unique_ptr<org::chromium::Itf2Proxy> itf2_proxy{
834 new org::chromium::Itf2Proxy{bus_, object_path}
835 };
836 auto p = itf2_instances_.emplace(object_path, std::move(itf2_proxy));
837 if (!on_itf2_added_.is_null())
838 on_itf2_added_.Run(p.first->second.get());
839 return;
840 }
841 }
842
843 void ObjectRemoved(
844 const dbus::ObjectPath& object_path,
845 const std::string& interface_name) override {
846 if (interface_name == "org.chromium.Itf1") {
847 auto p = itf1_instances_.find(object_path);
848 if (p != itf1_instances_.end()) {
849 if (!on_itf1_removed_.is_null())
850 on_itf1_removed_.Run(object_path);
851 itf1_instances_.erase(p);
852 }
853 return;
854 }
855 if (interface_name == "org.chromium.Itf2") {
856 auto p = itf2_instances_.find(object_path);
857 if (p != itf2_instances_.end()) {
858 if (!on_itf2_removed_.is_null())
859 on_itf2_removed_.Run(object_path);
860 itf2_instances_.erase(p);
861 }
862 return;
863 }
864 }
865
866 dbus::PropertySet* CreateProperties(
867 dbus::ObjectProxy* object_proxy,
868 const dbus::ObjectPath& object_path,
869 const std::string& interface_name) override {
870 if (interface_name == "org.chromium.Itf1") {
871 return new org::chromium::Itf1Proxy::PropertySet{
872 object_proxy,
873 base::Bind(&ObjectManagerProxy::OnPropertyChanged,
874 weak_ptr_factory_.GetWeakPtr(),
875 object_path,
876 interface_name)
877 };
878 }
879 if (interface_name == "org.chromium.Itf2") {
880 return new org::chromium::Itf2Proxy::PropertySet{
881 object_proxy,
882 base::Bind(&ObjectManagerProxy::OnPropertyChanged,
883 weak_ptr_factory_.GetWeakPtr(),
884 object_path,
885 interface_name)
886 };
887 }
888 LOG(FATAL) << "Creating properties for unsupported interface "
889 << interface_name;
890 return nullptr;
891 }
892
893 scoped_refptr<dbus::Bus> bus_;
894 dbus::ObjectManager* dbus_object_manager_;
895 std::map<dbus::ObjectPath,
896 std::unique_ptr<org::chromium::Itf1Proxy>> itf1_instances_;
897 base::Callback<void(org::chromium::Itf1Proxy*)> on_itf1_added_;
898 base::Callback<void(const dbus::ObjectPath&)> on_itf1_removed_;
899 std::map<dbus::ObjectPath,
900 std::unique_ptr<org::chromium::Itf2Proxy>> itf2_instances_;
901 base::Callback<void(org::chromium::Itf2Proxy*)> on_itf2_added_;
902 base::Callback<void(const dbus::ObjectPath&)> on_itf2_removed_;
903 base::WeakPtrFactory<ObjectManagerProxy> weak_ptr_factory_{this};
904
905 DISALLOW_COPY_AND_ASSIGN(ObjectManagerProxy);
906};
907
908} // namespace chromium
909} // namespace org
910)literal_string";
911
912const char kExpectedContentWithObjectManagerAndServiceName[] = R"literal_string(
913#include <memory>
914#include <string>
915#include <vector>
916
917#include <base/bind.h>
918#include <base/callback.h>
919#include <base/logging.h>
920#include <base/macros.h>
921#include <base/memory/ref_counted.h>
922#include <chromeos/any.h>
923#include <chromeos/dbus/dbus_method_invoker.h>
924#include <chromeos/dbus/dbus_property.h>
925#include <chromeos/dbus/dbus_signal_handler.h>
926#include <chromeos/errors/error.h>
927#include <chromeos/variant_dictionary.h>
928#include <dbus/bus.h>
929#include <dbus/message.h>
930#include <dbus/object_manager.h>
931#include <dbus/object_path.h>
932#include <dbus/object_proxy.h>
933
934namespace org {
935namespace chromium {
936class ObjectManagerProxy;
937} // namespace chromium
938} // namespace org
939
940namespace org {
941namespace chromium {
942
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800943// Abstract interface proxy for org::chromium::Itf1.
944class Itf1ProxyInterface {
945 public:
946 protected:
947 ~Itf1ProxyInterface() = default;
948};
949
950} // namespace chromium
951} // namespace org
952
953namespace org {
954namespace chromium {
955
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800956// Interface proxy for org::chromium::Itf1.
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800957class Itf1Proxy final : public Itf1ProxyInterface {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800958 public:
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800959 class PropertySet : public dbus::PropertySet {
960 public:
961 PropertySet(dbus::ObjectProxy* object_proxy,
962 const PropertyChangedCallback& callback)
963 : dbus::PropertySet{object_proxy,
964 "org.chromium.Itf1",
965 callback} {
966 }
967
968
969 private:
970 DISALLOW_COPY_AND_ASSIGN(PropertySet);
971 };
972
973 Itf1Proxy(const scoped_refptr<dbus::Bus>& bus) :
974 bus_{bus},
975 dbus_object_proxy_{
976 bus_->GetObjectProxy(service_name_, object_path_)} {
977 }
978
Christopher Wiley824b5242014-12-03 11:58:01 -0800979 ~Itf1Proxy() {
980 }
981
982 void RegisterCloserSignalHandler(
983 const base::Closure& signal_callback,
984 dbus::ObjectProxy::OnConnectedCallback on_connected_callback) {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800985 chromeos::dbus_utils::ConnectToSignal(
986 dbus_object_proxy_,
987 "org.chromium.Itf1",
988 "Closer",
Christopher Wiley824b5242014-12-03 11:58:01 -0800989 signal_callback,
990 on_connected_callback);
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800991 }
992
993 void ReleaseObjectProxy(const base::Closure& callback) {
994 bus_->RemoveObjectProxy(service_name_, object_path_, callback);
995 }
996
997 const dbus::ObjectPath& GetObjectPath() const {
998 return object_path_;
999 }
1000
1001 dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; }
1002
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001003 private:
1004 scoped_refptr<dbus::Bus> bus_;
1005 const std::string service_name_{"org.chromium.Test"};
1006 const dbus::ObjectPath object_path_{"/org/chromium/Test/Object"};
1007 dbus::ObjectProxy* dbus_object_proxy_;
1008
1009 DISALLOW_COPY_AND_ASSIGN(Itf1Proxy);
1010};
1011
1012} // namespace chromium
1013} // namespace org
1014
1015namespace org {
1016namespace chromium {
1017
Alex Vakulenko008e19d2015-01-21 10:37:00 -08001018// Abstract interface proxy for org::chromium::Itf2.
1019class Itf2ProxyInterface {
1020 public:
1021 protected:
1022 ~Itf2ProxyInterface() = default;
1023};
1024
1025} // namespace chromium
1026} // namespace org
1027
1028namespace org {
1029namespace chromium {
1030
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001031// Interface proxy for org::chromium::Itf2.
Alex Vakulenko008e19d2015-01-21 10:37:00 -08001032class Itf2Proxy final : public Itf2ProxyInterface {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001033 public:
1034 class PropertySet : public dbus::PropertySet {
1035 public:
1036 PropertySet(dbus::ObjectProxy* object_proxy,
1037 const PropertyChangedCallback& callback)
1038 : dbus::PropertySet{object_proxy,
1039 "org.chromium.Itf2",
1040 callback} {
1041 }
1042
1043
1044 private:
1045 DISALLOW_COPY_AND_ASSIGN(PropertySet);
1046 };
1047
1048 Itf2Proxy(
1049 const scoped_refptr<dbus::Bus>& bus,
1050 const dbus::ObjectPath& object_path) :
1051 bus_{bus},
1052 object_path_{object_path},
1053 dbus_object_proxy_{
1054 bus_->GetObjectProxy(service_name_, object_path_)} {
1055 }
1056
1057 ~Itf2Proxy() {
1058 }
1059
1060 void ReleaseObjectProxy(const base::Closure& callback) {
1061 bus_->RemoveObjectProxy(service_name_, object_path_, callback);
1062 }
1063
1064 const dbus::ObjectPath& GetObjectPath() const {
1065 return object_path_;
1066 }
1067
1068 dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; }
1069
1070 private:
1071 scoped_refptr<dbus::Bus> bus_;
1072 const std::string service_name_{"org.chromium.Test"};
1073 dbus::ObjectPath object_path_;
1074 dbus::ObjectProxy* dbus_object_proxy_;
1075
1076 DISALLOW_COPY_AND_ASSIGN(Itf2Proxy);
1077};
1078
1079} // namespace chromium
1080} // namespace org
1081
1082namespace org {
1083namespace chromium {
1084
1085class ObjectManagerProxy : public dbus::ObjectManager::Interface {
1086 public:
1087 ObjectManagerProxy(const scoped_refptr<dbus::Bus>& bus)
1088 : bus_{bus},
1089 dbus_object_manager_{bus->GetObjectManager(
1090 "org.chromium.Test",
1091 dbus::ObjectPath{"/org/chromium/Test"})} {
1092 dbus_object_manager_->RegisterInterface("org.chromium.Itf1", this);
1093 dbus_object_manager_->RegisterInterface("org.chromium.Itf2", this);
1094 }
1095
Christopher Wileye11e81c2014-12-19 13:10:52 -08001096 ~ObjectManagerProxy() override {
1097 dbus_object_manager_->UnregisterInterface("org.chromium.Itf1");
1098 dbus_object_manager_->UnregisterInterface("org.chromium.Itf2");
1099 }
1100
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001101 dbus::ObjectManager* GetObjectManagerProxy() const {
1102 return dbus_object_manager_;
1103 }
1104
1105 org::chromium::Itf1Proxy* GetItf1Proxy(
1106 const dbus::ObjectPath& object_path) {
1107 auto p = itf1_instances_.find(object_path);
1108 if (p != itf1_instances_.end())
1109 return p->second.get();
1110 return nullptr;
1111 }
1112 std::vector<org::chromium::Itf1Proxy*> GetItf1Instances() const {
1113 std::vector<org::chromium::Itf1Proxy*> values;
1114 values.reserve(itf1_instances_.size());
1115 for (const auto& pair : itf1_instances_)
1116 values.push_back(pair.second.get());
1117 return values;
1118 }
1119 void SetItf1AddedCallback(
1120 const base::Callback<void(org::chromium::Itf1Proxy*)>& callback) {
1121 on_itf1_added_ = callback;
1122 }
1123 void SetItf1RemovedCallback(
1124 const base::Callback<void(const dbus::ObjectPath&)>& callback) {
1125 on_itf1_removed_ = callback;
1126 }
1127
1128 org::chromium::Itf2Proxy* GetItf2Proxy(
1129 const dbus::ObjectPath& object_path) {
1130 auto p = itf2_instances_.find(object_path);
1131 if (p != itf2_instances_.end())
1132 return p->second.get();
1133 return nullptr;
1134 }
1135 std::vector<org::chromium::Itf2Proxy*> GetItf2Instances() const {
1136 std::vector<org::chromium::Itf2Proxy*> values;
1137 values.reserve(itf2_instances_.size());
1138 for (const auto& pair : itf2_instances_)
1139 values.push_back(pair.second.get());
1140 return values;
1141 }
1142 void SetItf2AddedCallback(
1143 const base::Callback<void(org::chromium::Itf2Proxy*)>& callback) {
1144 on_itf2_added_ = callback;
1145 }
1146 void SetItf2RemovedCallback(
1147 const base::Callback<void(const dbus::ObjectPath&)>& callback) {
1148 on_itf2_removed_ = callback;
1149 }
1150
1151 private:
1152 void OnPropertyChanged(const dbus::ObjectPath& object_path,
1153 const std::string& interface_name,
1154 const std::string& property_name) {
1155 }
1156
1157 void ObjectAdded(
1158 const dbus::ObjectPath& object_path,
1159 const std::string& interface_name) override {
1160 if (interface_name == "org.chromium.Itf1") {
1161 std::unique_ptr<org::chromium::Itf1Proxy> itf1_proxy{
1162 new org::chromium::Itf1Proxy{bus_}
1163 };
1164 auto p = itf1_instances_.emplace(object_path, std::move(itf1_proxy));
1165 if (!on_itf1_added_.is_null())
1166 on_itf1_added_.Run(p.first->second.get());
1167 return;
1168 }
1169 if (interface_name == "org.chromium.Itf2") {
1170 std::unique_ptr<org::chromium::Itf2Proxy> itf2_proxy{
1171 new org::chromium::Itf2Proxy{bus_, object_path}
1172 };
1173 auto p = itf2_instances_.emplace(object_path, std::move(itf2_proxy));
1174 if (!on_itf2_added_.is_null())
1175 on_itf2_added_.Run(p.first->second.get());
1176 return;
1177 }
1178 }
1179
1180 void ObjectRemoved(
1181 const dbus::ObjectPath& object_path,
1182 const std::string& interface_name) override {
1183 if (interface_name == "org.chromium.Itf1") {
1184 auto p = itf1_instances_.find(object_path);
1185 if (p != itf1_instances_.end()) {
1186 if (!on_itf1_removed_.is_null())
1187 on_itf1_removed_.Run(object_path);
1188 itf1_instances_.erase(p);
1189 }
1190 return;
1191 }
1192 if (interface_name == "org.chromium.Itf2") {
1193 auto p = itf2_instances_.find(object_path);
1194 if (p != itf2_instances_.end()) {
1195 if (!on_itf2_removed_.is_null())
1196 on_itf2_removed_.Run(object_path);
1197 itf2_instances_.erase(p);
1198 }
1199 return;
1200 }
1201 }
1202
1203 dbus::PropertySet* CreateProperties(
1204 dbus::ObjectProxy* object_proxy,
1205 const dbus::ObjectPath& object_path,
1206 const std::string& interface_name) override {
1207 if (interface_name == "org.chromium.Itf1") {
1208 return new org::chromium::Itf1Proxy::PropertySet{
1209 object_proxy,
1210 base::Bind(&ObjectManagerProxy::OnPropertyChanged,
1211 weak_ptr_factory_.GetWeakPtr(),
1212 object_path,
1213 interface_name)
1214 };
1215 }
1216 if (interface_name == "org.chromium.Itf2") {
1217 return new org::chromium::Itf2Proxy::PropertySet{
1218 object_proxy,
1219 base::Bind(&ObjectManagerProxy::OnPropertyChanged,
1220 weak_ptr_factory_.GetWeakPtr(),
1221 object_path,
1222 interface_name)
1223 };
1224 }
1225 LOG(FATAL) << "Creating properties for unsupported interface "
1226 << interface_name;
1227 return nullptr;
1228 }
1229
1230 scoped_refptr<dbus::Bus> bus_;
1231 dbus::ObjectManager* dbus_object_manager_;
1232 std::map<dbus::ObjectPath,
1233 std::unique_ptr<org::chromium::Itf1Proxy>> itf1_instances_;
1234 base::Callback<void(org::chromium::Itf1Proxy*)> on_itf1_added_;
1235 base::Callback<void(const dbus::ObjectPath&)> on_itf1_removed_;
1236 std::map<dbus::ObjectPath,
1237 std::unique_ptr<org::chromium::Itf2Proxy>> itf2_instances_;
1238 base::Callback<void(org::chromium::Itf2Proxy*)> on_itf2_added_;
1239 base::Callback<void(const dbus::ObjectPath&)> on_itf2_removed_;
1240 base::WeakPtrFactory<ObjectManagerProxy> weak_ptr_factory_{this};
1241
1242 DISALLOW_COPY_AND_ASSIGN(ObjectManagerProxy);
1243};
1244
1245} // namespace chromium
1246} // namespace org
1247)literal_string";
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001248} // namespace
1249
1250class ProxyGeneratorTest : public Test {
1251 public:
1252 void SetUp() override {
1253 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
1254 }
1255
1256 protected:
1257 base::FilePath CreateInputFile(const string& contents) {
1258 base::FilePath path;
1259 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &path));
1260 EXPECT_EQ(contents.size(),
1261 base::WriteFile(path, contents.c_str(), contents.size()));
1262 return path;
1263 }
1264
1265 base::ScopedTempDir temp_dir_;
1266};
1267
1268TEST_F(ProxyGeneratorTest, GenerateAdaptors) {
1269 Interface interface;
Alex Vakulenkob69be602015-02-23 14:40:42 -08001270 interface.name = "org.chromium.TestInterface";
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001271 interface.path = "/org/chromium/Test";
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001272 interface.methods.emplace_back(
Alex Vakulenkob69be602015-02-23 14:40:42 -08001273 "Elements",
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001274 vector<Interface::Argument>{
Alex Vakulenkob69be602015-02-23 14:40:42 -08001275 {"space_walk", kDBusTypeString},
1276 {"ramblin_man", kDBusTypeArryOfObjects}},
1277 vector<Interface::Argument>{{"", kDBusTypeString}});
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001278 interface.methods.emplace_back(
Alex Vakulenkob69be602015-02-23 14:40:42 -08001279 "ReturnToPatagonia",
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001280 vector<Interface::Argument>{},
Alex Vakulenkob69be602015-02-23 14:40:42 -08001281 vector<Interface::Argument>{{"", kDBusTypeInt64}});
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001282 interface.methods.emplace_back(
Alex Vakulenkob69be602015-02-23 14:40:42 -08001283 "NiceWeatherForDucks",
1284 vector<Interface::Argument>{{"", kDBusTypeBool}},
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001285 vector<Interface::Argument>{});
Alex Vakulenkob69be602015-02-23 14:40:42 -08001286 interface.methods.emplace_back("ExperimentNumberSix");
1287 interface.signals.emplace_back("Closer");
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001288 interface.signals.emplace_back(
Alex Vakulenkob69be602015-02-23 14:40:42 -08001289 "TheCurseOfKaZar",
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001290 vector<Interface::Argument>{
Alex Vakulenkob69be602015-02-23 14:40:42 -08001291 {"", kDBusTypeArryOfStrings},
1292 {"", kDBusTypeByte}});
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001293 interface.methods.back().doc_string = "Comment line1\nline2";
Alex Vakulenkofafef132014-11-03 14:52:09 -08001294 Interface interface2;
Alex Vakulenkob69be602015-02-23 14:40:42 -08001295 interface2.name = "org.chromium.TestInterface2";
Alex Vakulenkofafef132014-11-03 14:52:09 -08001296 interface2.methods.emplace_back(
Alex Vakulenkob69be602015-02-23 14:40:42 -08001297 "GetPersonInfo",
Alex Vakulenkofafef132014-11-03 14:52:09 -08001298 vector<Interface::Argument>{},
1299 vector<Interface::Argument>{
Alex Vakulenkob69be602015-02-23 14:40:42 -08001300 {"name", kDBusTypeString},
1301 {"age", kDBusTypeInt32}});
Alex Vakulenkofafef132014-11-03 14:52:09 -08001302 vector<Interface> interfaces{interface, interface2};
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001303 base::FilePath output_path = temp_dir_.path().Append("output.h");
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001304 ServiceConfig config;
1305 EXPECT_TRUE(ProxyGenerator::GenerateProxies(config, interfaces, output_path));
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001306 string contents;
1307 EXPECT_TRUE(base::ReadFileToString(output_path, &contents));
1308 // The header guards contain the (temporary) filename, so we search for
1309 // the content we need within the string.
1310 EXPECT_NE(string::npos, contents.find(kExpectedContent))
1311 << "Expected to find the following content...\n"
1312 << kExpectedContent << "...within content...\n" << contents;
1313}
1314
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001315TEST_F(ProxyGeneratorTest, GenerateAdaptorsWithServiceName) {
1316 Interface interface;
Alex Vakulenkob69be602015-02-23 14:40:42 -08001317 interface.name = "org.chromium.TestInterface";
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001318 interface.path = "/org/chromium/Test";
Alex Vakulenkob69be602015-02-23 14:40:42 -08001319 interface.signals.emplace_back("Closer");
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001320 Interface interface2;
Alex Vakulenkob69be602015-02-23 14:40:42 -08001321 interface2.name = "org.chromium.TestInterface2";
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001322 vector<Interface> interfaces{interface, interface2};
1323 base::FilePath output_path = temp_dir_.path().Append("output2.h");
1324 ServiceConfig config;
1325 config.service_name = "org.chromium.Test";
1326 EXPECT_TRUE(ProxyGenerator::GenerateProxies(config, interfaces, output_path));
1327 string contents;
1328 EXPECT_TRUE(base::ReadFileToString(output_path, &contents));
1329 // The header guards contain the (temporary) filename, so we search for
1330 // the content we need within the string.
1331 EXPECT_NE(string::npos, contents.find(kExpectedContentWithService))
1332 << "Expected to find the following content...\n"
1333 << kExpectedContentWithService << "...within content...\n" << contents;
1334}
1335
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001336TEST_F(ProxyGeneratorTest, GenerateAdaptorsWithObjectManager) {
1337 Interface interface;
1338 interface.name = "org.chromium.Itf1";
1339 interface.path = "/org/chromium/Test/Object";
Alex Vakulenkob69be602015-02-23 14:40:42 -08001340 interface.signals.emplace_back("Closer");
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001341 interface.properties.emplace_back("data", "s", "read");
1342 Interface interface2;
1343 interface2.name = "org.chromium.Itf2";
1344 vector<Interface> interfaces{interface, interface2};
1345 base::FilePath output_path = temp_dir_.path().Append("output3.h");
1346 ServiceConfig config;
1347 config.object_manager.name = "org.chromium.ObjectManager";
1348 config.object_manager.object_path = "/org/chromium/Test";
1349 EXPECT_TRUE(ProxyGenerator::GenerateProxies(config, interfaces, output_path));
1350 string contents;
1351 EXPECT_TRUE(base::ReadFileToString(output_path, &contents));
1352 // The header guards contain the (temporary) filename, so we search for
1353 // the content we need within the string.
1354 EXPECT_NE(string::npos, contents.find(kExpectedContentWithObjectManager))
1355 << "Expected to find the following content...\n"
1356 << kExpectedContentWithObjectManager << "...within content...\n"
1357 << contents;
1358}
1359
1360TEST_F(ProxyGeneratorTest, GenerateAdaptorsWithObjectManagerAndServiceName) {
1361 Interface interface;
1362 interface.name = "org.chromium.Itf1";
1363 interface.path = "/org/chromium/Test/Object";
Alex Vakulenkob69be602015-02-23 14:40:42 -08001364 interface.signals.emplace_back("Closer");
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001365 Interface interface2;
1366 interface2.name = "org.chromium.Itf2";
1367 vector<Interface> interfaces{interface, interface2};
1368 base::FilePath output_path = temp_dir_.path().Append("output4.h");
1369 ServiceConfig config;
1370 config.service_name = "org.chromium.Test";
1371 config.object_manager.name = "org.chromium.ObjectManager";
1372 config.object_manager.object_path = "/org/chromium/Test";
1373 EXPECT_TRUE(ProxyGenerator::GenerateProxies(config, interfaces, output_path));
1374 string contents;
1375 EXPECT_TRUE(base::ReadFileToString(output_path, &contents));
1376 // The header guards contain the (temporary) filename, so we search for
1377 // the content we need within the string.
1378 EXPECT_NE(string::npos,
1379 contents.find(kExpectedContentWithObjectManagerAndServiceName))
1380 << "Expected to find the following content...\n"
1381 << kExpectedContentWithObjectManagerAndServiceName
1382 << "...within content...\n" << contents;
1383}
1384
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001385} // namespace chromeos_dbus_bindings