blob: 7ce1c3e51f4a00792ad55cdbbac3932a368f13ba [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"
Alex Deymof211ae62015-07-28 13:09:29 -070016#include "chromeos-dbus-bindings/test_utils.h"
Paul Stewart1dce1ae2014-10-01 05:30:18 -070017
18using std::string;
19using std::vector;
20using testing::Test;
21
22namespace chromeos_dbus_bindings {
23
24namespace {
25
Alex Vakulenkob69be602015-02-23 14:40:42 -080026const char kDBusTypeArryOfObjects[] = "ao";
27const char kDBusTypeArryOfStrings[] = "as";
28const char kDBusTypeBool[] = "b";
29const char kDBusTypeByte[] = "y";
30const char kDBusTypeInt32[] = "i";
31const char kDBusTypeInt64[] = "x";
32const char kDBusTypeString[] = "s";
33
Paul Stewart1dce1ae2014-10-01 05:30:18 -070034const char kExpectedContent[] = R"literal_string(
Alex Vakulenko99e8fb02014-12-01 17:22:03 -080035#include <memory>
Paul Stewart1dce1ae2014-10-01 05:30:18 -070036#include <string>
37#include <vector>
38
39#include <base/bind.h>
40#include <base/callback.h>
41#include <base/logging.h>
42#include <base/macros.h>
43#include <base/memory/ref_counted.h>
44#include <chromeos/any.h>
45#include <chromeos/dbus/dbus_method_invoker.h>
Alex Vakulenko99e8fb02014-12-01 17:22:03 -080046#include <chromeos/dbus/dbus_property.h>
Paul Stewart1dce1ae2014-10-01 05:30:18 -070047#include <chromeos/dbus/dbus_signal_handler.h>
48#include <chromeos/errors/error.h>
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -080049#include <chromeos/variant_dictionary.h>
Paul Stewart1dce1ae2014-10-01 05:30:18 -070050#include <dbus/bus.h>
51#include <dbus/message.h>
Alex Vakulenko99e8fb02014-12-01 17:22:03 -080052#include <dbus/object_manager.h>
Paul Stewart1dce1ae2014-10-01 05:30:18 -070053#include <dbus/object_path.h>
54#include <dbus/object_proxy.h>
55
56namespace org {
57namespace chromium {
58
Alex Vakulenko008e19d2015-01-21 10:37:00 -080059// Abstract interface proxy for org::chromium::TestInterface.
60class TestInterfaceProxyInterface {
61 public:
62 virtual bool Elements(
63 const std::string& in_space_walk,
64 const std::vector<dbus::ObjectPath>& in_ramblin_man,
65 std::string* out_3,
66 chromeos::ErrorPtr* error,
67 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
68
69 virtual void ElementsAsync(
70 const std::string& in_space_walk,
71 const std::vector<dbus::ObjectPath>& in_ramblin_man,
72 const base::Callback<void(const std::string&)>& success_callback,
73 const base::Callback<void(chromeos::Error*)>& error_callback,
74 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
75
76 virtual bool ReturnToPatagonia(
77 int64_t* out_1,
78 chromeos::ErrorPtr* error,
79 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
80
81 virtual void ReturnToPatagoniaAsync(
82 const base::Callback<void(int64_t)>& success_callback,
83 const base::Callback<void(chromeos::Error*)>& error_callback,
84 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
85
86 virtual bool NiceWeatherForDucks(
87 bool in_1,
88 chromeos::ErrorPtr* error,
89 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
90
91 virtual void NiceWeatherForDucksAsync(
92 bool in_1,
93 const base::Callback<void()>& success_callback,
94 const base::Callback<void(chromeos::Error*)>& error_callback,
95 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
96
97 // Comment line1
98 // line2
99 virtual bool ExperimentNumberSix(
100 chromeos::ErrorPtr* error,
101 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
102
103 // Comment line1
104 // line2
105 virtual void ExperimentNumberSixAsync(
106 const base::Callback<void()>& success_callback,
107 const base::Callback<void(chromeos::Error*)>& error_callback,
108 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
109
Alex Deymoce286b92015-07-28 15:04:07 -0700110 virtual void RegisterCloserSignalHandler(
111 const base::Closure& signal_callback,
112 dbus::ObjectProxy::OnConnectedCallback on_connected_callback) = 0;
113
114 virtual void RegisterTheCurseOfKaZarSignalHandler(
115 const base::Callback<void(const std::vector<std::string>&,
116 uint8_t)>& signal_callback,
117 dbus::ObjectProxy::OnConnectedCallback on_connected_callback) = 0;
118
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800119 protected:
Alex Deymo16856102015-06-19 20:55:14 -0700120 virtual ~TestInterfaceProxyInterface() = default;
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800121};
122
123} // namespace chromium
124} // namespace org
125
126namespace org {
127namespace chromium {
128
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800129// Interface proxy for org::chromium::TestInterface.
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800130class TestInterfaceProxy final : public TestInterfaceProxyInterface {
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700131 public:
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700132 TestInterfaceProxy(
133 const scoped_refptr<dbus::Bus>& bus,
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800134 const std::string& service_name) :
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800135 bus_{bus},
136 service_name_{service_name},
137 dbus_object_proxy_{
138 bus_->GetObjectProxy(service_name_, object_path_)} {
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800139 }
140
Alex Deymo16856102015-06-19 20:55:14 -0700141 ~TestInterfaceProxy() override {
Christopher Wiley824b5242014-12-03 11:58:01 -0800142 }
143
144 void RegisterCloserSignalHandler(
145 const base::Closure& signal_callback,
Alex Deymoce286b92015-07-28 15:04:07 -0700146 dbus::ObjectProxy::OnConnectedCallback on_connected_callback) override {
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700147 chromeos::dbus_utils::ConnectToSignal(
148 dbus_object_proxy_,
149 "org.chromium.TestInterface",
150 "Closer",
Christopher Wiley824b5242014-12-03 11:58:01 -0800151 signal_callback,
152 on_connected_callback);
153 }
154
155 void RegisterTheCurseOfKaZarSignalHandler(
156 const base::Callback<void(const std::vector<std::string>&,
157 uint8_t)>& signal_callback,
Alex Deymoce286b92015-07-28 15:04:07 -0700158 dbus::ObjectProxy::OnConnectedCallback on_connected_callback) override {
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700159 chromeos::dbus_utils::ConnectToSignal(
160 dbus_object_proxy_,
161 "org.chromium.TestInterface",
162 "TheCurseOfKaZar",
Christopher Wiley824b5242014-12-03 11:58:01 -0800163 signal_callback,
164 on_connected_callback);
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800165 }
166
167 void ReleaseObjectProxy(const base::Closure& callback) {
168 bus_->RemoveObjectProxy(service_name_, object_path_, callback);
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700169 }
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800170
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800171 const dbus::ObjectPath& GetObjectPath() const {
172 return object_path_;
173 }
174
175 dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; }
176
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800177 bool Elements(
Alex Vakulenkofafef132014-11-03 14:52:09 -0800178 const std::string& in_space_walk,
179 const std::vector<dbus::ObjectPath>& in_ramblin_man,
180 std::string* out_3,
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800181 chromeos::ErrorPtr* error,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800182 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800183 auto response = chromeos::dbus_utils::CallMethodAndBlockWithTimeout(
184 timeout_ms,
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700185 dbus_object_proxy_,
186 "org.chromium.TestInterface",
187 "Elements",
188 error,
Alex Vakulenkofafef132014-11-03 14:52:09 -0800189 in_space_walk,
190 in_ramblin_man);
191 return response && chromeos::dbus_utils::ExtractMethodCallResults(
192 response.get(), error, out_3);
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700193 }
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800194
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800195 void ElementsAsync(
196 const std::string& in_space_walk,
197 const std::vector<dbus::ObjectPath>& in_ramblin_man,
198 const base::Callback<void(const std::string&)>& success_callback,
199 const base::Callback<void(chromeos::Error*)>& error_callback,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800200 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800201 chromeos::dbus_utils::CallMethodWithTimeout(
202 timeout_ms,
203 dbus_object_proxy_,
204 "org.chromium.TestInterface",
205 "Elements",
206 success_callback,
207 error_callback,
208 in_space_walk,
209 in_ramblin_man);
210 }
211
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800212 bool ReturnToPatagonia(
Alex Vakulenkofafef132014-11-03 14:52:09 -0800213 int64_t* out_1,
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800214 chromeos::ErrorPtr* error,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800215 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800216 auto response = chromeos::dbus_utils::CallMethodAndBlockWithTimeout(
217 timeout_ms,
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700218 dbus_object_proxy_,
219 "org.chromium.TestInterface",
220 "ReturnToPatagonia",
221 error);
Alex Vakulenkofafef132014-11-03 14:52:09 -0800222 return response && chromeos::dbus_utils::ExtractMethodCallResults(
223 response.get(), error, out_1);
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700224 }
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800225
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800226 void ReturnToPatagoniaAsync(
227 const base::Callback<void(int64_t)>& success_callback,
228 const base::Callback<void(chromeos::Error*)>& error_callback,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800229 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800230 chromeos::dbus_utils::CallMethodWithTimeout(
231 timeout_ms,
232 dbus_object_proxy_,
233 "org.chromium.TestInterface",
234 "ReturnToPatagonia",
235 success_callback,
236 error_callback);
237 }
238
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800239 bool NiceWeatherForDucks(
Alex Vakulenkofafef132014-11-03 14:52:09 -0800240 bool in_1,
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800241 chromeos::ErrorPtr* error,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800242 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800243 auto response = chromeos::dbus_utils::CallMethodAndBlockWithTimeout(
244 timeout_ms,
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700245 dbus_object_proxy_,
246 "org.chromium.TestInterface",
247 "NiceWeatherForDucks",
248 error,
Alex Vakulenkofafef132014-11-03 14:52:09 -0800249 in_1);
250 return response && chromeos::dbus_utils::ExtractMethodCallResults(
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700251 response.get(), error);
252 }
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800253
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800254 void NiceWeatherForDucksAsync(
255 bool in_1,
256 const base::Callback<void()>& success_callback,
257 const base::Callback<void(chromeos::Error*)>& error_callback,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800258 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800259 chromeos::dbus_utils::CallMethodWithTimeout(
260 timeout_ms,
261 dbus_object_proxy_,
262 "org.chromium.TestInterface",
263 "NiceWeatherForDucks",
264 success_callback,
265 error_callback,
266 in_1);
267 }
268
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800269 // Comment line1
270 // line2
271 bool ExperimentNumberSix(
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800272 chromeos::ErrorPtr* error,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800273 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800274 auto response = chromeos::dbus_utils::CallMethodAndBlockWithTimeout(
275 timeout_ms,
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700276 dbus_object_proxy_,
277 "org.chromium.TestInterface",
278 "ExperimentNumberSix",
279 error);
Alex Vakulenkofafef132014-11-03 14:52:09 -0800280 return response && chromeos::dbus_utils::ExtractMethodCallResults(
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700281 response.get(), error);
282 }
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800283
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800284 // Comment line1
285 // line2
286 void ExperimentNumberSixAsync(
287 const base::Callback<void()>& success_callback,
288 const base::Callback<void(chromeos::Error*)>& error_callback,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800289 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800290 chromeos::dbus_utils::CallMethodWithTimeout(
291 timeout_ms,
292 dbus_object_proxy_,
293 "org.chromium.TestInterface",
294 "ExperimentNumberSix",
295 success_callback,
296 error_callback);
297 }
298
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800299 private:
300 scoped_refptr<dbus::Bus> bus_;
301 std::string service_name_;
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800302 const dbus::ObjectPath object_path_{"/org/chromium/Test"};
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800303 dbus::ObjectProxy* dbus_object_proxy_;
304
305 DISALLOW_COPY_AND_ASSIGN(TestInterfaceProxy);
306};
307
308} // namespace chromium
309} // namespace org
310
311namespace org {
312namespace chromium {
313
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800314// Abstract interface proxy for org::chromium::TestInterface2.
315class TestInterface2ProxyInterface {
316 public:
317 virtual bool GetPersonInfo(
318 std::string* out_name,
319 int32_t* out_age,
320 chromeos::ErrorPtr* error,
321 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
322
323 virtual void GetPersonInfoAsync(
324 const base::Callback<void(const std::string& /*name*/, int32_t /*age*/)>& success_callback,
325 const base::Callback<void(chromeos::Error*)>& error_callback,
326 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
327
328 protected:
Alex Deymo16856102015-06-19 20:55:14 -0700329 virtual ~TestInterface2ProxyInterface() = default;
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800330};
331
332} // namespace chromium
333} // namespace org
334
335namespace org {
336namespace chromium {
337
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800338// Interface proxy for org::chromium::TestInterface2.
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800339class TestInterface2Proxy final : public TestInterface2ProxyInterface {
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800340 public:
341 TestInterface2Proxy(
342 const scoped_refptr<dbus::Bus>& bus,
343 const std::string& service_name,
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800344 const dbus::ObjectPath& object_path) :
345 bus_{bus},
346 service_name_{service_name},
347 object_path_{object_path},
348 dbus_object_proxy_{
349 bus_->GetObjectProxy(service_name_, object_path_)} {
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800350 }
351
Alex Deymo16856102015-06-19 20:55:14 -0700352 ~TestInterface2Proxy() override {
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800353 }
354
355 void ReleaseObjectProxy(const base::Closure& callback) {
356 bus_->RemoveObjectProxy(service_name_, object_path_, callback);
357 }
358
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800359 const dbus::ObjectPath& GetObjectPath() const {
360 return object_path_;
361 }
362
363 dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; }
364
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800365 bool GetPersonInfo(
Alex Vakulenkofafef132014-11-03 14:52:09 -0800366 std::string* out_name,
367 int32_t* out_age,
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800368 chromeos::ErrorPtr* error,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800369 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800370 auto response = chromeos::dbus_utils::CallMethodAndBlockWithTimeout(
371 timeout_ms,
Alex Vakulenkofafef132014-11-03 14:52:09 -0800372 dbus_object_proxy_,
373 "org.chromium.TestInterface2",
374 "GetPersonInfo",
375 error);
376 return response && chromeos::dbus_utils::ExtractMethodCallResults(
377 response.get(), error, out_name, out_age);
378 }
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700379
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800380 void GetPersonInfoAsync(
381 const base::Callback<void(const std::string& /*name*/, int32_t /*age*/)>& success_callback,
382 const base::Callback<void(chromeos::Error*)>& error_callback,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800383 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800384 chromeos::dbus_utils::CallMethodWithTimeout(
385 timeout_ms,
386 dbus_object_proxy_,
387 "org.chromium.TestInterface2",
388 "GetPersonInfo",
389 success_callback,
390 error_callback);
391 }
392
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700393 private:
394 scoped_refptr<dbus::Bus> bus_;
395 std::string service_name_;
396 dbus::ObjectPath object_path_;
397 dbus::ObjectProxy* dbus_object_proxy_;
398
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800399 DISALLOW_COPY_AND_ASSIGN(TestInterface2Proxy);
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700400};
401
402} // namespace chromium
403} // namespace org
404)literal_string";
405
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800406const char kExpectedContentWithService[] = R"literal_string(
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800407#include <memory>
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800408#include <string>
409#include <vector>
410
411#include <base/bind.h>
412#include <base/callback.h>
413#include <base/logging.h>
414#include <base/macros.h>
415#include <base/memory/ref_counted.h>
416#include <chromeos/any.h>
417#include <chromeos/dbus/dbus_method_invoker.h>
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800418#include <chromeos/dbus/dbus_property.h>
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800419#include <chromeos/dbus/dbus_signal_handler.h>
420#include <chromeos/errors/error.h>
421#include <chromeos/variant_dictionary.h>
422#include <dbus/bus.h>
423#include <dbus/message.h>
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800424#include <dbus/object_manager.h>
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800425#include <dbus/object_path.h>
426#include <dbus/object_proxy.h>
427
428namespace org {
429namespace chromium {
430
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800431// Abstract interface proxy for org::chromium::TestInterface.
432class TestInterfaceProxyInterface {
433 public:
Alex Deymoce286b92015-07-28 15:04:07 -0700434 virtual void RegisterCloserSignalHandler(
435 const base::Closure& signal_callback,
436 dbus::ObjectProxy::OnConnectedCallback on_connected_callback) = 0;
437
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800438 protected:
Alex Deymo16856102015-06-19 20:55:14 -0700439 virtual ~TestInterfaceProxyInterface() = default;
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800440};
441
442} // namespace chromium
443} // namespace org
444
445namespace org {
446namespace chromium {
447
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800448// Interface proxy for org::chromium::TestInterface.
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800449class TestInterfaceProxy final : public TestInterfaceProxyInterface {
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800450 public:
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800451 TestInterfaceProxy(const scoped_refptr<dbus::Bus>& bus) :
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800452 bus_{bus},
453 dbus_object_proxy_{
454 bus_->GetObjectProxy(service_name_, object_path_)} {
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800455 }
456
Alex Deymo16856102015-06-19 20:55:14 -0700457 ~TestInterfaceProxy() override {
Christopher Wiley824b5242014-12-03 11:58:01 -0800458 }
459
460 void RegisterCloserSignalHandler(
461 const base::Closure& signal_callback,
Alex Deymoce286b92015-07-28 15:04:07 -0700462 dbus::ObjectProxy::OnConnectedCallback on_connected_callback) override {
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800463 chromeos::dbus_utils::ConnectToSignal(
464 dbus_object_proxy_,
465 "org.chromium.TestInterface",
466 "Closer",
Christopher Wiley824b5242014-12-03 11:58:01 -0800467 signal_callback,
468 on_connected_callback);
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800469 }
470
471 void ReleaseObjectProxy(const base::Closure& callback) {
472 bus_->RemoveObjectProxy(service_name_, object_path_, callback);
473 }
474
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800475 const dbus::ObjectPath& GetObjectPath() const {
476 return object_path_;
477 }
478
479 dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; }
480
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800481 private:
482 scoped_refptr<dbus::Bus> bus_;
483 const std::string service_name_{"org.chromium.Test"};
484 const dbus::ObjectPath object_path_{"/org/chromium/Test"};
485 dbus::ObjectProxy* dbus_object_proxy_;
486
487 DISALLOW_COPY_AND_ASSIGN(TestInterfaceProxy);
488};
489
490} // namespace chromium
491} // namespace org
492
493namespace org {
494namespace chromium {
495
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800496// Abstract interface proxy for org::chromium::TestInterface2.
497class TestInterface2ProxyInterface {
498 public:
499 protected:
Alex Deymo16856102015-06-19 20:55:14 -0700500 virtual ~TestInterface2ProxyInterface() = default;
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800501};
502
503} // namespace chromium
504} // namespace org
505
506namespace org {
507namespace chromium {
508
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800509// Interface proxy for org::chromium::TestInterface2.
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800510class TestInterface2Proxy final : public TestInterface2ProxyInterface {
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800511 public:
512 TestInterface2Proxy(
513 const scoped_refptr<dbus::Bus>& bus,
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800514 const dbus::ObjectPath& object_path) :
515 bus_{bus},
516 object_path_{object_path},
517 dbus_object_proxy_{
518 bus_->GetObjectProxy(service_name_, object_path_)} {
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800519 }
520
Alex Deymo16856102015-06-19 20:55:14 -0700521 ~TestInterface2Proxy() override {
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800522 }
523
524 void ReleaseObjectProxy(const base::Closure& callback) {
525 bus_->RemoveObjectProxy(service_name_, object_path_, callback);
526 }
527
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800528 const dbus::ObjectPath& GetObjectPath() const {
529 return object_path_;
530 }
531
532 dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; }
533
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800534 private:
535 scoped_refptr<dbus::Bus> bus_;
536 const std::string service_name_{"org.chromium.Test"};
537 dbus::ObjectPath object_path_;
538 dbus::ObjectProxy* dbus_object_proxy_;
539
540 DISALLOW_COPY_AND_ASSIGN(TestInterface2Proxy);
541};
542
543} // namespace chromium
544} // namespace org
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800545)literal_string";
546
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800547const char kExpectedContentWithObjectManager[] = R"literal_string(
548#include <memory>
549#include <string>
550#include <vector>
551
552#include <base/bind.h>
553#include <base/callback.h>
554#include <base/logging.h>
555#include <base/macros.h>
556#include <base/memory/ref_counted.h>
557#include <chromeos/any.h>
558#include <chromeos/dbus/dbus_method_invoker.h>
559#include <chromeos/dbus/dbus_property.h>
560#include <chromeos/dbus/dbus_signal_handler.h>
561#include <chromeos/errors/error.h>
562#include <chromeos/variant_dictionary.h>
563#include <dbus/bus.h>
564#include <dbus/message.h>
565#include <dbus/object_manager.h>
566#include <dbus/object_path.h>
567#include <dbus/object_proxy.h>
568
569namespace org {
570namespace chromium {
571class ObjectManagerProxy;
572} // namespace chromium
573} // namespace org
574
575namespace org {
576namespace chromium {
577
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800578// Abstract interface proxy for org::chromium::Itf1.
579class Itf1ProxyInterface {
580 public:
Alex Deymoce286b92015-07-28 15:04:07 -0700581 virtual void RegisterCloserSignalHandler(
582 const base::Closure& signal_callback,
583 dbus::ObjectProxy::OnConnectedCallback on_connected_callback) = 0;
584
Alex Vakulenko59dd2e92015-02-23 15:14:28 -0800585 static const char* DataName() { return "Data"; }
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800586 virtual const std::string& data() const = 0;
587
588 protected:
Alex Deymo16856102015-06-19 20:55:14 -0700589 virtual ~Itf1ProxyInterface() = default;
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800590};
591
592} // namespace chromium
593} // namespace org
594
595namespace org {
596namespace chromium {
597
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800598// Interface proxy for org::chromium::Itf1.
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800599class Itf1Proxy final : public Itf1ProxyInterface {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800600 public:
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800601 class PropertySet : public dbus::PropertySet {
602 public:
603 PropertySet(dbus::ObjectProxy* object_proxy,
604 const PropertyChangedCallback& callback)
605 : dbus::PropertySet{object_proxy,
606 "org.chromium.Itf1",
607 callback} {
Alex Vakulenko59dd2e92015-02-23 15:14:28 -0800608 RegisterProperty(DataName(), &data);
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800609 }
610
611 chromeos::dbus_utils::Property<std::string> data;
612
613 private:
614 DISALLOW_COPY_AND_ASSIGN(PropertySet);
615 };
616
617 Itf1Proxy(
618 const scoped_refptr<dbus::Bus>& bus,
619 const std::string& service_name,
620 PropertySet* property_set) :
621 bus_{bus},
622 service_name_{service_name},
623 property_set_{property_set},
624 dbus_object_proxy_{
625 bus_->GetObjectProxy(service_name_, object_path_)} {
626 }
627
Alex Deymo16856102015-06-19 20:55:14 -0700628 ~Itf1Proxy() override {
Christopher Wiley824b5242014-12-03 11:58:01 -0800629 }
630
631 void RegisterCloserSignalHandler(
632 const base::Closure& signal_callback,
Alex Deymoce286b92015-07-28 15:04:07 -0700633 dbus::ObjectProxy::OnConnectedCallback on_connected_callback) override {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800634 chromeos::dbus_utils::ConnectToSignal(
635 dbus_object_proxy_,
636 "org.chromium.Itf1",
637 "Closer",
Christopher Wiley824b5242014-12-03 11:58:01 -0800638 signal_callback,
639 on_connected_callback);
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800640 }
641
642 void ReleaseObjectProxy(const base::Closure& callback) {
643 bus_->RemoveObjectProxy(service_name_, object_path_, callback);
644 }
645
646 const dbus::ObjectPath& GetObjectPath() const {
647 return object_path_;
648 }
649
650 dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; }
651
652 void SetPropertyChangedCallback(
653 const base::Callback<void(Itf1Proxy*, const std::string&)>& callback) {
654 on_property_changed_ = callback;
655 }
656
657 const PropertySet* GetProperties() const { return property_set_; }
658 PropertySet* GetProperties() { return property_set_; }
659
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800660 const std::string& data() const override {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800661 return property_set_->data.value();
662 }
663
664 private:
665 void OnPropertyChanged(const std::string& property_name) {
666 if (!on_property_changed_.is_null())
667 on_property_changed_.Run(this, property_name);
668 }
669
670 scoped_refptr<dbus::Bus> bus_;
671 std::string service_name_;
672 const dbus::ObjectPath object_path_{"/org/chromium/Test/Object"};
673 PropertySet* property_set_;
674 base::Callback<void(Itf1Proxy*, const std::string&)> on_property_changed_;
675 dbus::ObjectProxy* dbus_object_proxy_;
676
677 friend class org::chromium::ObjectManagerProxy;
678 DISALLOW_COPY_AND_ASSIGN(Itf1Proxy);
679};
680
681} // namespace chromium
682} // namespace org
683
684namespace org {
685namespace chromium {
686
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800687// Abstract interface proxy for org::chromium::Itf2.
688class Itf2ProxyInterface {
689 public:
690 protected:
Alex Deymo16856102015-06-19 20:55:14 -0700691 virtual ~Itf2ProxyInterface() = default;
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800692};
693
694} // namespace chromium
695} // namespace org
696
697namespace org {
698namespace chromium {
699
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800700// Interface proxy for org::chromium::Itf2.
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800701class Itf2Proxy final : public Itf2ProxyInterface {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800702 public:
703 class PropertySet : public dbus::PropertySet {
704 public:
705 PropertySet(dbus::ObjectProxy* object_proxy,
706 const PropertyChangedCallback& callback)
707 : dbus::PropertySet{object_proxy,
708 "org.chromium.Itf2",
709 callback} {
710 }
711
712
713 private:
714 DISALLOW_COPY_AND_ASSIGN(PropertySet);
715 };
716
717 Itf2Proxy(
718 const scoped_refptr<dbus::Bus>& bus,
719 const std::string& service_name,
720 const dbus::ObjectPath& object_path) :
721 bus_{bus},
722 service_name_{service_name},
723 object_path_{object_path},
724 dbus_object_proxy_{
725 bus_->GetObjectProxy(service_name_, object_path_)} {
726 }
727
Alex Deymo16856102015-06-19 20:55:14 -0700728 ~Itf2Proxy() override {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800729 }
730
731 void ReleaseObjectProxy(const base::Closure& callback) {
732 bus_->RemoveObjectProxy(service_name_, object_path_, callback);
733 }
734
735 const dbus::ObjectPath& GetObjectPath() const {
736 return object_path_;
737 }
738
739 dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; }
740
741 private:
742 scoped_refptr<dbus::Bus> bus_;
743 std::string service_name_;
744 dbus::ObjectPath object_path_;
745 dbus::ObjectProxy* dbus_object_proxy_;
746
747 DISALLOW_COPY_AND_ASSIGN(Itf2Proxy);
748};
749
750} // namespace chromium
751} // namespace org
752
753namespace org {
754namespace chromium {
755
756class ObjectManagerProxy : public dbus::ObjectManager::Interface {
757 public:
758 ObjectManagerProxy(const scoped_refptr<dbus::Bus>& bus,
759 const std::string& service_name)
760 : bus_{bus},
Christopher Wileyefe9df52015-03-12 16:02:22 -0700761 service_name_{service_name},
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800762 dbus_object_manager_{bus->GetObjectManager(
763 service_name,
764 dbus::ObjectPath{"/org/chromium/Test"})} {
765 dbus_object_manager_->RegisterInterface("org.chromium.Itf1", this);
766 dbus_object_manager_->RegisterInterface("org.chromium.Itf2", this);
767 }
768
Christopher Wileye11e81c2014-12-19 13:10:52 -0800769 ~ObjectManagerProxy() override {
770 dbus_object_manager_->UnregisterInterface("org.chromium.Itf1");
771 dbus_object_manager_->UnregisterInterface("org.chromium.Itf2");
772 }
773
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800774 dbus::ObjectManager* GetObjectManagerProxy() const {
775 return dbus_object_manager_;
776 }
777
Alex Vakulenkoc4e01f12015-02-23 15:55:21 -0800778 org::chromium::Itf1Proxy* GetItf1Proxy() {
779 if (itf1_instances_.empty())
780 return nullptr;
781 return itf1_instances_.begin()->second.get();
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800782 }
783 std::vector<org::chromium::Itf1Proxy*> GetItf1Instances() const {
784 std::vector<org::chromium::Itf1Proxy*> values;
785 values.reserve(itf1_instances_.size());
786 for (const auto& pair : itf1_instances_)
787 values.push_back(pair.second.get());
788 return values;
789 }
790 void SetItf1AddedCallback(
791 const base::Callback<void(org::chromium::Itf1Proxy*)>& callback) {
792 on_itf1_added_ = callback;
793 }
794 void SetItf1RemovedCallback(
795 const base::Callback<void(const dbus::ObjectPath&)>& callback) {
796 on_itf1_removed_ = callback;
797 }
798
799 org::chromium::Itf2Proxy* GetItf2Proxy(
800 const dbus::ObjectPath& object_path) {
801 auto p = itf2_instances_.find(object_path);
802 if (p != itf2_instances_.end())
803 return p->second.get();
804 return nullptr;
805 }
806 std::vector<org::chromium::Itf2Proxy*> GetItf2Instances() const {
807 std::vector<org::chromium::Itf2Proxy*> values;
808 values.reserve(itf2_instances_.size());
809 for (const auto& pair : itf2_instances_)
810 values.push_back(pair.second.get());
811 return values;
812 }
813 void SetItf2AddedCallback(
814 const base::Callback<void(org::chromium::Itf2Proxy*)>& callback) {
815 on_itf2_added_ = callback;
816 }
817 void SetItf2RemovedCallback(
818 const base::Callback<void(const dbus::ObjectPath&)>& callback) {
819 on_itf2_removed_ = callback;
820 }
821
822 private:
823 void OnPropertyChanged(const dbus::ObjectPath& object_path,
824 const std::string& interface_name,
825 const std::string& property_name) {
826 if (interface_name == "org.chromium.Itf1") {
827 auto p = itf1_instances_.find(object_path);
828 if (p == itf1_instances_.end())
829 return;
830 p->second->OnPropertyChanged(property_name);
831 return;
832 }
833 }
834
835 void ObjectAdded(
836 const dbus::ObjectPath& object_path,
837 const std::string& interface_name) override {
838 if (interface_name == "org.chromium.Itf1") {
839 auto property_set =
840 static_cast<org::chromium::Itf1Proxy::PropertySet*>(
841 dbus_object_manager_->GetProperties(object_path, interface_name));
842 std::unique_ptr<org::chromium::Itf1Proxy> itf1_proxy{
Christopher Wileyefe9df52015-03-12 16:02:22 -0700843 new org::chromium::Itf1Proxy{bus_, service_name_, property_set}
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800844 };
845 auto p = itf1_instances_.emplace(object_path, std::move(itf1_proxy));
846 if (!on_itf1_added_.is_null())
847 on_itf1_added_.Run(p.first->second.get());
848 return;
849 }
850 if (interface_name == "org.chromium.Itf2") {
851 std::unique_ptr<org::chromium::Itf2Proxy> itf2_proxy{
Christopher Wileyefe9df52015-03-12 16:02:22 -0700852 new org::chromium::Itf2Proxy{bus_, service_name_, object_path}
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800853 };
854 auto p = itf2_instances_.emplace(object_path, std::move(itf2_proxy));
855 if (!on_itf2_added_.is_null())
856 on_itf2_added_.Run(p.first->second.get());
857 return;
858 }
859 }
860
861 void ObjectRemoved(
862 const dbus::ObjectPath& object_path,
863 const std::string& interface_name) override {
864 if (interface_name == "org.chromium.Itf1") {
865 auto p = itf1_instances_.find(object_path);
866 if (p != itf1_instances_.end()) {
867 if (!on_itf1_removed_.is_null())
868 on_itf1_removed_.Run(object_path);
869 itf1_instances_.erase(p);
870 }
871 return;
872 }
873 if (interface_name == "org.chromium.Itf2") {
874 auto p = itf2_instances_.find(object_path);
875 if (p != itf2_instances_.end()) {
876 if (!on_itf2_removed_.is_null())
877 on_itf2_removed_.Run(object_path);
878 itf2_instances_.erase(p);
879 }
880 return;
881 }
882 }
883
884 dbus::PropertySet* CreateProperties(
885 dbus::ObjectProxy* object_proxy,
886 const dbus::ObjectPath& object_path,
887 const std::string& interface_name) override {
888 if (interface_name == "org.chromium.Itf1") {
889 return new org::chromium::Itf1Proxy::PropertySet{
890 object_proxy,
891 base::Bind(&ObjectManagerProxy::OnPropertyChanged,
892 weak_ptr_factory_.GetWeakPtr(),
893 object_path,
894 interface_name)
895 };
896 }
897 if (interface_name == "org.chromium.Itf2") {
898 return new org::chromium::Itf2Proxy::PropertySet{
899 object_proxy,
900 base::Bind(&ObjectManagerProxy::OnPropertyChanged,
901 weak_ptr_factory_.GetWeakPtr(),
902 object_path,
903 interface_name)
904 };
905 }
906 LOG(FATAL) << "Creating properties for unsupported interface "
907 << interface_name;
908 return nullptr;
909 }
910
911 scoped_refptr<dbus::Bus> bus_;
Christopher Wileyefe9df52015-03-12 16:02:22 -0700912 std::string service_name_;
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800913 dbus::ObjectManager* dbus_object_manager_;
914 std::map<dbus::ObjectPath,
915 std::unique_ptr<org::chromium::Itf1Proxy>> itf1_instances_;
916 base::Callback<void(org::chromium::Itf1Proxy*)> on_itf1_added_;
917 base::Callback<void(const dbus::ObjectPath&)> on_itf1_removed_;
918 std::map<dbus::ObjectPath,
919 std::unique_ptr<org::chromium::Itf2Proxy>> itf2_instances_;
920 base::Callback<void(org::chromium::Itf2Proxy*)> on_itf2_added_;
921 base::Callback<void(const dbus::ObjectPath&)> on_itf2_removed_;
922 base::WeakPtrFactory<ObjectManagerProxy> weak_ptr_factory_{this};
923
924 DISALLOW_COPY_AND_ASSIGN(ObjectManagerProxy);
925};
926
927} // namespace chromium
928} // namespace org
929)literal_string";
930
931const char kExpectedContentWithObjectManagerAndServiceName[] = R"literal_string(
932#include <memory>
933#include <string>
934#include <vector>
935
936#include <base/bind.h>
937#include <base/callback.h>
938#include <base/logging.h>
939#include <base/macros.h>
940#include <base/memory/ref_counted.h>
941#include <chromeos/any.h>
942#include <chromeos/dbus/dbus_method_invoker.h>
943#include <chromeos/dbus/dbus_property.h>
944#include <chromeos/dbus/dbus_signal_handler.h>
945#include <chromeos/errors/error.h>
946#include <chromeos/variant_dictionary.h>
947#include <dbus/bus.h>
948#include <dbus/message.h>
949#include <dbus/object_manager.h>
950#include <dbus/object_path.h>
951#include <dbus/object_proxy.h>
952
953namespace org {
954namespace chromium {
955class ObjectManagerProxy;
956} // namespace chromium
957} // namespace org
958
959namespace org {
960namespace chromium {
961
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800962// Abstract interface proxy for org::chromium::Itf1.
963class Itf1ProxyInterface {
964 public:
Alex Deymoce286b92015-07-28 15:04:07 -0700965 virtual void RegisterCloserSignalHandler(
966 const base::Closure& signal_callback,
967 dbus::ObjectProxy::OnConnectedCallback on_connected_callback) = 0;
968
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800969 protected:
Alex Deymo16856102015-06-19 20:55:14 -0700970 virtual ~Itf1ProxyInterface() = default;
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800971};
972
973} // namespace chromium
974} // namespace org
975
976namespace org {
977namespace chromium {
978
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800979// Interface proxy for org::chromium::Itf1.
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800980class Itf1Proxy final : public Itf1ProxyInterface {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800981 public:
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800982 class PropertySet : public dbus::PropertySet {
983 public:
984 PropertySet(dbus::ObjectProxy* object_proxy,
985 const PropertyChangedCallback& callback)
986 : dbus::PropertySet{object_proxy,
987 "org.chromium.Itf1",
988 callback} {
989 }
990
991
992 private:
993 DISALLOW_COPY_AND_ASSIGN(PropertySet);
994 };
995
996 Itf1Proxy(const scoped_refptr<dbus::Bus>& bus) :
997 bus_{bus},
998 dbus_object_proxy_{
999 bus_->GetObjectProxy(service_name_, object_path_)} {
1000 }
1001
Alex Deymo16856102015-06-19 20:55:14 -07001002 ~Itf1Proxy() override {
Christopher Wiley824b5242014-12-03 11:58:01 -08001003 }
1004
1005 void RegisterCloserSignalHandler(
1006 const base::Closure& signal_callback,
Alex Deymoce286b92015-07-28 15:04:07 -07001007 dbus::ObjectProxy::OnConnectedCallback on_connected_callback) override {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001008 chromeos::dbus_utils::ConnectToSignal(
1009 dbus_object_proxy_,
1010 "org.chromium.Itf1",
1011 "Closer",
Christopher Wiley824b5242014-12-03 11:58:01 -08001012 signal_callback,
1013 on_connected_callback);
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001014 }
1015
1016 void ReleaseObjectProxy(const base::Closure& callback) {
1017 bus_->RemoveObjectProxy(service_name_, object_path_, callback);
1018 }
1019
1020 const dbus::ObjectPath& GetObjectPath() const {
1021 return object_path_;
1022 }
1023
1024 dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; }
1025
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001026 private:
1027 scoped_refptr<dbus::Bus> bus_;
1028 const std::string service_name_{"org.chromium.Test"};
1029 const dbus::ObjectPath object_path_{"/org/chromium/Test/Object"};
1030 dbus::ObjectProxy* dbus_object_proxy_;
1031
1032 DISALLOW_COPY_AND_ASSIGN(Itf1Proxy);
1033};
1034
1035} // namespace chromium
1036} // namespace org
1037
1038namespace org {
1039namespace chromium {
1040
Alex Vakulenko008e19d2015-01-21 10:37:00 -08001041// Abstract interface proxy for org::chromium::Itf2.
1042class Itf2ProxyInterface {
1043 public:
1044 protected:
Alex Deymo16856102015-06-19 20:55:14 -07001045 virtual ~Itf2ProxyInterface() = default;
Alex Vakulenko008e19d2015-01-21 10:37:00 -08001046};
1047
1048} // namespace chromium
1049} // namespace org
1050
1051namespace org {
1052namespace chromium {
1053
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001054// Interface proxy for org::chromium::Itf2.
Alex Vakulenko008e19d2015-01-21 10:37:00 -08001055class Itf2Proxy final : public Itf2ProxyInterface {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001056 public:
1057 class PropertySet : public dbus::PropertySet {
1058 public:
1059 PropertySet(dbus::ObjectProxy* object_proxy,
1060 const PropertyChangedCallback& callback)
1061 : dbus::PropertySet{object_proxy,
1062 "org.chromium.Itf2",
1063 callback} {
1064 }
1065
1066
1067 private:
1068 DISALLOW_COPY_AND_ASSIGN(PropertySet);
1069 };
1070
1071 Itf2Proxy(
1072 const scoped_refptr<dbus::Bus>& bus,
1073 const dbus::ObjectPath& object_path) :
1074 bus_{bus},
1075 object_path_{object_path},
1076 dbus_object_proxy_{
1077 bus_->GetObjectProxy(service_name_, object_path_)} {
1078 }
1079
Alex Deymo16856102015-06-19 20:55:14 -07001080 ~Itf2Proxy() override {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001081 }
1082
1083 void ReleaseObjectProxy(const base::Closure& callback) {
1084 bus_->RemoveObjectProxy(service_name_, object_path_, callback);
1085 }
1086
1087 const dbus::ObjectPath& GetObjectPath() const {
1088 return object_path_;
1089 }
1090
1091 dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; }
1092
1093 private:
1094 scoped_refptr<dbus::Bus> bus_;
1095 const std::string service_name_{"org.chromium.Test"};
1096 dbus::ObjectPath object_path_;
1097 dbus::ObjectProxy* dbus_object_proxy_;
1098
1099 DISALLOW_COPY_AND_ASSIGN(Itf2Proxy);
1100};
1101
1102} // namespace chromium
1103} // namespace org
1104
1105namespace org {
1106namespace chromium {
1107
1108class ObjectManagerProxy : public dbus::ObjectManager::Interface {
1109 public:
1110 ObjectManagerProxy(const scoped_refptr<dbus::Bus>& bus)
1111 : bus_{bus},
1112 dbus_object_manager_{bus->GetObjectManager(
1113 "org.chromium.Test",
1114 dbus::ObjectPath{"/org/chromium/Test"})} {
1115 dbus_object_manager_->RegisterInterface("org.chromium.Itf1", this);
1116 dbus_object_manager_->RegisterInterface("org.chromium.Itf2", this);
1117 }
1118
Christopher Wileye11e81c2014-12-19 13:10:52 -08001119 ~ObjectManagerProxy() override {
1120 dbus_object_manager_->UnregisterInterface("org.chromium.Itf1");
1121 dbus_object_manager_->UnregisterInterface("org.chromium.Itf2");
1122 }
1123
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001124 dbus::ObjectManager* GetObjectManagerProxy() const {
1125 return dbus_object_manager_;
1126 }
1127
Alex Vakulenkoc4e01f12015-02-23 15:55:21 -08001128 org::chromium::Itf1Proxy* GetItf1Proxy() {
1129 if (itf1_instances_.empty())
1130 return nullptr;
1131 return itf1_instances_.begin()->second.get();
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001132 }
1133 std::vector<org::chromium::Itf1Proxy*> GetItf1Instances() const {
1134 std::vector<org::chromium::Itf1Proxy*> values;
1135 values.reserve(itf1_instances_.size());
1136 for (const auto& pair : itf1_instances_)
1137 values.push_back(pair.second.get());
1138 return values;
1139 }
1140 void SetItf1AddedCallback(
1141 const base::Callback<void(org::chromium::Itf1Proxy*)>& callback) {
1142 on_itf1_added_ = callback;
1143 }
1144 void SetItf1RemovedCallback(
1145 const base::Callback<void(const dbus::ObjectPath&)>& callback) {
1146 on_itf1_removed_ = callback;
1147 }
1148
1149 org::chromium::Itf2Proxy* GetItf2Proxy(
1150 const dbus::ObjectPath& object_path) {
1151 auto p = itf2_instances_.find(object_path);
1152 if (p != itf2_instances_.end())
1153 return p->second.get();
1154 return nullptr;
1155 }
1156 std::vector<org::chromium::Itf2Proxy*> GetItf2Instances() const {
1157 std::vector<org::chromium::Itf2Proxy*> values;
1158 values.reserve(itf2_instances_.size());
1159 for (const auto& pair : itf2_instances_)
1160 values.push_back(pair.second.get());
1161 return values;
1162 }
1163 void SetItf2AddedCallback(
1164 const base::Callback<void(org::chromium::Itf2Proxy*)>& callback) {
1165 on_itf2_added_ = callback;
1166 }
1167 void SetItf2RemovedCallback(
1168 const base::Callback<void(const dbus::ObjectPath&)>& callback) {
1169 on_itf2_removed_ = callback;
1170 }
1171
1172 private:
1173 void OnPropertyChanged(const dbus::ObjectPath& object_path,
1174 const std::string& interface_name,
1175 const std::string& property_name) {
1176 }
1177
1178 void ObjectAdded(
1179 const dbus::ObjectPath& object_path,
1180 const std::string& interface_name) override {
1181 if (interface_name == "org.chromium.Itf1") {
1182 std::unique_ptr<org::chromium::Itf1Proxy> itf1_proxy{
1183 new org::chromium::Itf1Proxy{bus_}
1184 };
1185 auto p = itf1_instances_.emplace(object_path, std::move(itf1_proxy));
1186 if (!on_itf1_added_.is_null())
1187 on_itf1_added_.Run(p.first->second.get());
1188 return;
1189 }
1190 if (interface_name == "org.chromium.Itf2") {
1191 std::unique_ptr<org::chromium::Itf2Proxy> itf2_proxy{
1192 new org::chromium::Itf2Proxy{bus_, object_path}
1193 };
1194 auto p = itf2_instances_.emplace(object_path, std::move(itf2_proxy));
1195 if (!on_itf2_added_.is_null())
1196 on_itf2_added_.Run(p.first->second.get());
1197 return;
1198 }
1199 }
1200
1201 void ObjectRemoved(
1202 const dbus::ObjectPath& object_path,
1203 const std::string& interface_name) override {
1204 if (interface_name == "org.chromium.Itf1") {
1205 auto p = itf1_instances_.find(object_path);
1206 if (p != itf1_instances_.end()) {
1207 if (!on_itf1_removed_.is_null())
1208 on_itf1_removed_.Run(object_path);
1209 itf1_instances_.erase(p);
1210 }
1211 return;
1212 }
1213 if (interface_name == "org.chromium.Itf2") {
1214 auto p = itf2_instances_.find(object_path);
1215 if (p != itf2_instances_.end()) {
1216 if (!on_itf2_removed_.is_null())
1217 on_itf2_removed_.Run(object_path);
1218 itf2_instances_.erase(p);
1219 }
1220 return;
1221 }
1222 }
1223
1224 dbus::PropertySet* CreateProperties(
1225 dbus::ObjectProxy* object_proxy,
1226 const dbus::ObjectPath& object_path,
1227 const std::string& interface_name) override {
1228 if (interface_name == "org.chromium.Itf1") {
1229 return new org::chromium::Itf1Proxy::PropertySet{
1230 object_proxy,
1231 base::Bind(&ObjectManagerProxy::OnPropertyChanged,
1232 weak_ptr_factory_.GetWeakPtr(),
1233 object_path,
1234 interface_name)
1235 };
1236 }
1237 if (interface_name == "org.chromium.Itf2") {
1238 return new org::chromium::Itf2Proxy::PropertySet{
1239 object_proxy,
1240 base::Bind(&ObjectManagerProxy::OnPropertyChanged,
1241 weak_ptr_factory_.GetWeakPtr(),
1242 object_path,
1243 interface_name)
1244 };
1245 }
1246 LOG(FATAL) << "Creating properties for unsupported interface "
1247 << interface_name;
1248 return nullptr;
1249 }
1250
1251 scoped_refptr<dbus::Bus> bus_;
1252 dbus::ObjectManager* dbus_object_manager_;
1253 std::map<dbus::ObjectPath,
1254 std::unique_ptr<org::chromium::Itf1Proxy>> itf1_instances_;
1255 base::Callback<void(org::chromium::Itf1Proxy*)> on_itf1_added_;
1256 base::Callback<void(const dbus::ObjectPath&)> on_itf1_removed_;
1257 std::map<dbus::ObjectPath,
1258 std::unique_ptr<org::chromium::Itf2Proxy>> itf2_instances_;
1259 base::Callback<void(org::chromium::Itf2Proxy*)> on_itf2_added_;
1260 base::Callback<void(const dbus::ObjectPath&)> on_itf2_removed_;
1261 base::WeakPtrFactory<ObjectManagerProxy> weak_ptr_factory_{this};
1262
1263 DISALLOW_COPY_AND_ASSIGN(ObjectManagerProxy);
1264};
1265
1266} // namespace chromium
1267} // namespace org
1268)literal_string";
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001269} // namespace
1270
1271class ProxyGeneratorTest : public Test {
1272 public:
1273 void SetUp() override {
1274 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
1275 }
1276
1277 protected:
1278 base::FilePath CreateInputFile(const string& contents) {
1279 base::FilePath path;
1280 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &path));
Christopher Wileyd0cb3fe2015-07-27 17:32:58 -07001281 int written = base::WriteFile(path, contents.c_str(), contents.size());
1282 EXPECT_EQ(contents.size(), static_cast<size_t>(written));
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001283 return path;
1284 }
1285
1286 base::ScopedTempDir temp_dir_;
1287};
1288
1289TEST_F(ProxyGeneratorTest, GenerateAdaptors) {
1290 Interface interface;
Alex Vakulenkob69be602015-02-23 14:40:42 -08001291 interface.name = "org.chromium.TestInterface";
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001292 interface.path = "/org/chromium/Test";
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001293 interface.methods.emplace_back(
Alex Vakulenkob69be602015-02-23 14:40:42 -08001294 "Elements",
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001295 vector<Interface::Argument>{
Alex Vakulenkob69be602015-02-23 14:40:42 -08001296 {"space_walk", kDBusTypeString},
1297 {"ramblin_man", kDBusTypeArryOfObjects}},
1298 vector<Interface::Argument>{{"", kDBusTypeString}});
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001299 interface.methods.emplace_back(
Alex Vakulenkob69be602015-02-23 14:40:42 -08001300 "ReturnToPatagonia",
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001301 vector<Interface::Argument>{},
Alex Vakulenkob69be602015-02-23 14:40:42 -08001302 vector<Interface::Argument>{{"", kDBusTypeInt64}});
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001303 interface.methods.emplace_back(
Alex Vakulenkob69be602015-02-23 14:40:42 -08001304 "NiceWeatherForDucks",
1305 vector<Interface::Argument>{{"", kDBusTypeBool}},
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001306 vector<Interface::Argument>{});
Alex Vakulenkob69be602015-02-23 14:40:42 -08001307 interface.methods.emplace_back("ExperimentNumberSix");
1308 interface.signals.emplace_back("Closer");
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001309 interface.signals.emplace_back(
Alex Vakulenkob69be602015-02-23 14:40:42 -08001310 "TheCurseOfKaZar",
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001311 vector<Interface::Argument>{
Alex Vakulenkob69be602015-02-23 14:40:42 -08001312 {"", kDBusTypeArryOfStrings},
1313 {"", kDBusTypeByte}});
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001314 interface.methods.back().doc_string = "Comment line1\nline2";
Alex Vakulenkofafef132014-11-03 14:52:09 -08001315 Interface interface2;
Alex Vakulenkob69be602015-02-23 14:40:42 -08001316 interface2.name = "org.chromium.TestInterface2";
Alex Vakulenkofafef132014-11-03 14:52:09 -08001317 interface2.methods.emplace_back(
Alex Vakulenkob69be602015-02-23 14:40:42 -08001318 "GetPersonInfo",
Alex Vakulenkofafef132014-11-03 14:52:09 -08001319 vector<Interface::Argument>{},
1320 vector<Interface::Argument>{
Alex Vakulenkob69be602015-02-23 14:40:42 -08001321 {"name", kDBusTypeString},
1322 {"age", kDBusTypeInt32}});
Alex Vakulenkofafef132014-11-03 14:52:09 -08001323 vector<Interface> interfaces{interface, interface2};
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001324 base::FilePath output_path = temp_dir_.path().Append("output.h");
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001325 ServiceConfig config;
1326 EXPECT_TRUE(ProxyGenerator::GenerateProxies(config, interfaces, output_path));
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001327 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.
Alex Deymof211ae62015-07-28 13:09:29 -07001331 test_utils::EXPECT_TEXT_CONTAINED(kExpectedContent, contents);
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001332}
1333
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001334TEST_F(ProxyGeneratorTest, GenerateAdaptorsWithServiceName) {
1335 Interface interface;
Alex Vakulenkob69be602015-02-23 14:40:42 -08001336 interface.name = "org.chromium.TestInterface";
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001337 interface.path = "/org/chromium/Test";
Alex Vakulenkob69be602015-02-23 14:40:42 -08001338 interface.signals.emplace_back("Closer");
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001339 Interface interface2;
Alex Vakulenkob69be602015-02-23 14:40:42 -08001340 interface2.name = "org.chromium.TestInterface2";
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001341 vector<Interface> interfaces{interface, interface2};
1342 base::FilePath output_path = temp_dir_.path().Append("output2.h");
1343 ServiceConfig config;
1344 config.service_name = "org.chromium.Test";
1345 EXPECT_TRUE(ProxyGenerator::GenerateProxies(config, interfaces, output_path));
1346 string contents;
1347 EXPECT_TRUE(base::ReadFileToString(output_path, &contents));
1348 // The header guards contain the (temporary) filename, so we search for
1349 // the content we need within the string.
Alex Deymof211ae62015-07-28 13:09:29 -07001350 test_utils::EXPECT_TEXT_CONTAINED(kExpectedContentWithService, contents);
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001351}
1352
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001353TEST_F(ProxyGeneratorTest, GenerateAdaptorsWithObjectManager) {
1354 Interface interface;
1355 interface.name = "org.chromium.Itf1";
1356 interface.path = "/org/chromium/Test/Object";
Alex Vakulenkob69be602015-02-23 14:40:42 -08001357 interface.signals.emplace_back("Closer");
Alex Vakulenko59dd2e92015-02-23 15:14:28 -08001358 interface.properties.emplace_back("Data", "s", "read");
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001359 Interface interface2;
1360 interface2.name = "org.chromium.Itf2";
1361 vector<Interface> interfaces{interface, interface2};
1362 base::FilePath output_path = temp_dir_.path().Append("output3.h");
1363 ServiceConfig config;
1364 config.object_manager.name = "org.chromium.ObjectManager";
1365 config.object_manager.object_path = "/org/chromium/Test";
1366 EXPECT_TRUE(ProxyGenerator::GenerateProxies(config, interfaces, output_path));
1367 string contents;
1368 EXPECT_TRUE(base::ReadFileToString(output_path, &contents));
1369 // The header guards contain the (temporary) filename, so we search for
1370 // the content we need within the string.
Alex Deymof211ae62015-07-28 13:09:29 -07001371 test_utils::EXPECT_TEXT_CONTAINED(
1372 kExpectedContentWithObjectManager, contents);
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001373}
1374
1375TEST_F(ProxyGeneratorTest, GenerateAdaptorsWithObjectManagerAndServiceName) {
1376 Interface interface;
1377 interface.name = "org.chromium.Itf1";
1378 interface.path = "/org/chromium/Test/Object";
Alex Vakulenkob69be602015-02-23 14:40:42 -08001379 interface.signals.emplace_back("Closer");
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001380 Interface interface2;
1381 interface2.name = "org.chromium.Itf2";
1382 vector<Interface> interfaces{interface, interface2};
1383 base::FilePath output_path = temp_dir_.path().Append("output4.h");
1384 ServiceConfig config;
1385 config.service_name = "org.chromium.Test";
1386 config.object_manager.name = "org.chromium.ObjectManager";
1387 config.object_manager.object_path = "/org/chromium/Test";
1388 EXPECT_TRUE(ProxyGenerator::GenerateProxies(config, interfaces, output_path));
1389 string contents;
1390 EXPECT_TRUE(base::ReadFileToString(output_path, &contents));
1391 // The header guards contain the (temporary) filename, so we search for
1392 // the content we need within the string.
Alex Deymof211ae62015-07-28 13:09:29 -07001393 test_utils::EXPECT_TEXT_CONTAINED(
1394 kExpectedContentWithObjectManagerAndServiceName, contents);
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001395}
1396
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001397} // namespace chromeos_dbus_bindings