blob: 8840f4118514fa7fd7ac465994d3c9bb3d7badf1 [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
110 protected:
Alex Deymo16856102015-06-19 20:55:14 -0700111 virtual ~TestInterfaceProxyInterface() = default;
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800112};
113
114} // namespace chromium
115} // namespace org
116
117namespace org {
118namespace chromium {
119
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800120// Interface proxy for org::chromium::TestInterface.
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800121class TestInterfaceProxy final : public TestInterfaceProxyInterface {
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700122 public:
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700123 TestInterfaceProxy(
124 const scoped_refptr<dbus::Bus>& bus,
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800125 const std::string& service_name) :
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800126 bus_{bus},
127 service_name_{service_name},
128 dbus_object_proxy_{
129 bus_->GetObjectProxy(service_name_, object_path_)} {
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800130 }
131
Alex Deymo16856102015-06-19 20:55:14 -0700132 ~TestInterfaceProxy() override {
Christopher Wiley824b5242014-12-03 11:58:01 -0800133 }
134
135 void RegisterCloserSignalHandler(
136 const base::Closure& signal_callback,
137 dbus::ObjectProxy::OnConnectedCallback on_connected_callback) {
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700138 chromeos::dbus_utils::ConnectToSignal(
139 dbus_object_proxy_,
140 "org.chromium.TestInterface",
141 "Closer",
Christopher Wiley824b5242014-12-03 11:58:01 -0800142 signal_callback,
143 on_connected_callback);
144 }
145
146 void RegisterTheCurseOfKaZarSignalHandler(
147 const base::Callback<void(const std::vector<std::string>&,
148 uint8_t)>& signal_callback,
149 dbus::ObjectProxy::OnConnectedCallback on_connected_callback) {
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700150 chromeos::dbus_utils::ConnectToSignal(
151 dbus_object_proxy_,
152 "org.chromium.TestInterface",
153 "TheCurseOfKaZar",
Christopher Wiley824b5242014-12-03 11:58:01 -0800154 signal_callback,
155 on_connected_callback);
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800156 }
157
158 void ReleaseObjectProxy(const base::Closure& callback) {
159 bus_->RemoveObjectProxy(service_name_, object_path_, callback);
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700160 }
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800161
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800162 const dbus::ObjectPath& GetObjectPath() const {
163 return object_path_;
164 }
165
166 dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; }
167
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800168 bool Elements(
Alex Vakulenkofafef132014-11-03 14:52:09 -0800169 const std::string& in_space_walk,
170 const std::vector<dbus::ObjectPath>& in_ramblin_man,
171 std::string* out_3,
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800172 chromeos::ErrorPtr* error,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800173 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800174 auto response = chromeos::dbus_utils::CallMethodAndBlockWithTimeout(
175 timeout_ms,
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700176 dbus_object_proxy_,
177 "org.chromium.TestInterface",
178 "Elements",
179 error,
Alex Vakulenkofafef132014-11-03 14:52:09 -0800180 in_space_walk,
181 in_ramblin_man);
182 return response && chromeos::dbus_utils::ExtractMethodCallResults(
183 response.get(), error, out_3);
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700184 }
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800185
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800186 void ElementsAsync(
187 const std::string& in_space_walk,
188 const std::vector<dbus::ObjectPath>& in_ramblin_man,
189 const base::Callback<void(const std::string&)>& success_callback,
190 const base::Callback<void(chromeos::Error*)>& error_callback,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800191 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800192 chromeos::dbus_utils::CallMethodWithTimeout(
193 timeout_ms,
194 dbus_object_proxy_,
195 "org.chromium.TestInterface",
196 "Elements",
197 success_callback,
198 error_callback,
199 in_space_walk,
200 in_ramblin_man);
201 }
202
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800203 bool ReturnToPatagonia(
Alex Vakulenkofafef132014-11-03 14:52:09 -0800204 int64_t* out_1,
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800205 chromeos::ErrorPtr* error,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800206 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800207 auto response = chromeos::dbus_utils::CallMethodAndBlockWithTimeout(
208 timeout_ms,
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700209 dbus_object_proxy_,
210 "org.chromium.TestInterface",
211 "ReturnToPatagonia",
212 error);
Alex Vakulenkofafef132014-11-03 14:52:09 -0800213 return response && chromeos::dbus_utils::ExtractMethodCallResults(
214 response.get(), error, out_1);
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700215 }
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800216
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800217 void ReturnToPatagoniaAsync(
218 const base::Callback<void(int64_t)>& success_callback,
219 const base::Callback<void(chromeos::Error*)>& error_callback,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800220 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800221 chromeos::dbus_utils::CallMethodWithTimeout(
222 timeout_ms,
223 dbus_object_proxy_,
224 "org.chromium.TestInterface",
225 "ReturnToPatagonia",
226 success_callback,
227 error_callback);
228 }
229
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800230 bool NiceWeatherForDucks(
Alex Vakulenkofafef132014-11-03 14:52:09 -0800231 bool in_1,
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800232 chromeos::ErrorPtr* error,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800233 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800234 auto response = chromeos::dbus_utils::CallMethodAndBlockWithTimeout(
235 timeout_ms,
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700236 dbus_object_proxy_,
237 "org.chromium.TestInterface",
238 "NiceWeatherForDucks",
239 error,
Alex Vakulenkofafef132014-11-03 14:52:09 -0800240 in_1);
241 return response && chromeos::dbus_utils::ExtractMethodCallResults(
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700242 response.get(), error);
243 }
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800244
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800245 void NiceWeatherForDucksAsync(
246 bool in_1,
247 const base::Callback<void()>& success_callback,
248 const base::Callback<void(chromeos::Error*)>& error_callback,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800249 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800250 chromeos::dbus_utils::CallMethodWithTimeout(
251 timeout_ms,
252 dbus_object_proxy_,
253 "org.chromium.TestInterface",
254 "NiceWeatherForDucks",
255 success_callback,
256 error_callback,
257 in_1);
258 }
259
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800260 // Comment line1
261 // line2
262 bool ExperimentNumberSix(
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800263 chromeos::ErrorPtr* error,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800264 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800265 auto response = chromeos::dbus_utils::CallMethodAndBlockWithTimeout(
266 timeout_ms,
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700267 dbus_object_proxy_,
268 "org.chromium.TestInterface",
269 "ExperimentNumberSix",
270 error);
Alex Vakulenkofafef132014-11-03 14:52:09 -0800271 return response && chromeos::dbus_utils::ExtractMethodCallResults(
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700272 response.get(), error);
273 }
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800274
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800275 // Comment line1
276 // line2
277 void ExperimentNumberSixAsync(
278 const base::Callback<void()>& success_callback,
279 const base::Callback<void(chromeos::Error*)>& error_callback,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800280 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800281 chromeos::dbus_utils::CallMethodWithTimeout(
282 timeout_ms,
283 dbus_object_proxy_,
284 "org.chromium.TestInterface",
285 "ExperimentNumberSix",
286 success_callback,
287 error_callback);
288 }
289
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800290 private:
291 scoped_refptr<dbus::Bus> bus_;
292 std::string service_name_;
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800293 const dbus::ObjectPath object_path_{"/org/chromium/Test"};
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800294 dbus::ObjectProxy* dbus_object_proxy_;
295
296 DISALLOW_COPY_AND_ASSIGN(TestInterfaceProxy);
297};
298
299} // namespace chromium
300} // namespace org
301
302namespace org {
303namespace chromium {
304
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800305// Abstract interface proxy for org::chromium::TestInterface2.
306class TestInterface2ProxyInterface {
307 public:
308 virtual bool GetPersonInfo(
309 std::string* out_name,
310 int32_t* out_age,
311 chromeos::ErrorPtr* error,
312 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
313
314 virtual void GetPersonInfoAsync(
315 const base::Callback<void(const std::string& /*name*/, int32_t /*age*/)>& success_callback,
316 const base::Callback<void(chromeos::Error*)>& error_callback,
317 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) = 0;
318
319 protected:
Alex Deymo16856102015-06-19 20:55:14 -0700320 virtual ~TestInterface2ProxyInterface() = default;
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800321};
322
323} // namespace chromium
324} // namespace org
325
326namespace org {
327namespace chromium {
328
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800329// Interface proxy for org::chromium::TestInterface2.
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800330class TestInterface2Proxy final : public TestInterface2ProxyInterface {
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800331 public:
332 TestInterface2Proxy(
333 const scoped_refptr<dbus::Bus>& bus,
334 const std::string& service_name,
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800335 const dbus::ObjectPath& object_path) :
336 bus_{bus},
337 service_name_{service_name},
338 object_path_{object_path},
339 dbus_object_proxy_{
340 bus_->GetObjectProxy(service_name_, object_path_)} {
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800341 }
342
Alex Deymo16856102015-06-19 20:55:14 -0700343 ~TestInterface2Proxy() override {
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800344 }
345
346 void ReleaseObjectProxy(const base::Closure& callback) {
347 bus_->RemoveObjectProxy(service_name_, object_path_, callback);
348 }
349
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800350 const dbus::ObjectPath& GetObjectPath() const {
351 return object_path_;
352 }
353
354 dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; }
355
Alex Vakulenko4f3c05e2014-11-12 15:01:46 -0800356 bool GetPersonInfo(
Alex Vakulenkofafef132014-11-03 14:52:09 -0800357 std::string* out_name,
358 int32_t* out_age,
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800359 chromeos::ErrorPtr* error,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800360 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800361 auto response = chromeos::dbus_utils::CallMethodAndBlockWithTimeout(
362 timeout_ms,
Alex Vakulenkofafef132014-11-03 14:52:09 -0800363 dbus_object_proxy_,
364 "org.chromium.TestInterface2",
365 "GetPersonInfo",
366 error);
367 return response && chromeos::dbus_utils::ExtractMethodCallResults(
368 response.get(), error, out_name, out_age);
369 }
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700370
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800371 void GetPersonInfoAsync(
372 const base::Callback<void(const std::string& /*name*/, int32_t /*age*/)>& success_callback,
373 const base::Callback<void(chromeos::Error*)>& error_callback,
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800374 int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) override {
Alex Vakulenkoc4e32f52014-12-04 08:28:49 -0800375 chromeos::dbus_utils::CallMethodWithTimeout(
376 timeout_ms,
377 dbus_object_proxy_,
378 "org.chromium.TestInterface2",
379 "GetPersonInfo",
380 success_callback,
381 error_callback);
382 }
383
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700384 private:
385 scoped_refptr<dbus::Bus> bus_;
386 std::string service_name_;
387 dbus::ObjectPath object_path_;
388 dbus::ObjectProxy* dbus_object_proxy_;
389
Alex Vakulenkoc95f06b2014-11-20 15:06:09 -0800390 DISALLOW_COPY_AND_ASSIGN(TestInterface2Proxy);
Paul Stewart1dce1ae2014-10-01 05:30:18 -0700391};
392
393} // namespace chromium
394} // namespace org
395)literal_string";
396
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800397const char kExpectedContentWithService[] = R"literal_string(
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800398#include <memory>
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800399#include <string>
400#include <vector>
401
402#include <base/bind.h>
403#include <base/callback.h>
404#include <base/logging.h>
405#include <base/macros.h>
406#include <base/memory/ref_counted.h>
407#include <chromeos/any.h>
408#include <chromeos/dbus/dbus_method_invoker.h>
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800409#include <chromeos/dbus/dbus_property.h>
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800410#include <chromeos/dbus/dbus_signal_handler.h>
411#include <chromeos/errors/error.h>
412#include <chromeos/variant_dictionary.h>
413#include <dbus/bus.h>
414#include <dbus/message.h>
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800415#include <dbus/object_manager.h>
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800416#include <dbus/object_path.h>
417#include <dbus/object_proxy.h>
418
419namespace org {
420namespace chromium {
421
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800422// Abstract interface proxy for org::chromium::TestInterface.
423class TestInterfaceProxyInterface {
424 public:
425 protected:
Alex Deymo16856102015-06-19 20:55:14 -0700426 virtual ~TestInterfaceProxyInterface() = default;
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800427};
428
429} // namespace chromium
430} // namespace org
431
432namespace org {
433namespace chromium {
434
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800435// Interface proxy for org::chromium::TestInterface.
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800436class TestInterfaceProxy final : public TestInterfaceProxyInterface {
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800437 public:
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800438 TestInterfaceProxy(const scoped_refptr<dbus::Bus>& bus) :
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800439 bus_{bus},
440 dbus_object_proxy_{
441 bus_->GetObjectProxy(service_name_, object_path_)} {
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800442 }
443
Alex Deymo16856102015-06-19 20:55:14 -0700444 ~TestInterfaceProxy() override {
Christopher Wiley824b5242014-12-03 11:58:01 -0800445 }
446
447 void RegisterCloserSignalHandler(
448 const base::Closure& signal_callback,
449 dbus::ObjectProxy::OnConnectedCallback on_connected_callback) {
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800450 chromeos::dbus_utils::ConnectToSignal(
451 dbus_object_proxy_,
452 "org.chromium.TestInterface",
453 "Closer",
Christopher Wiley824b5242014-12-03 11:58:01 -0800454 signal_callback,
455 on_connected_callback);
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800456 }
457
458 void ReleaseObjectProxy(const base::Closure& callback) {
459 bus_->RemoveObjectProxy(service_name_, object_path_, callback);
460 }
461
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800462 const dbus::ObjectPath& GetObjectPath() const {
463 return object_path_;
464 }
465
466 dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; }
467
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800468 private:
469 scoped_refptr<dbus::Bus> bus_;
470 const std::string service_name_{"org.chromium.Test"};
471 const dbus::ObjectPath object_path_{"/org/chromium/Test"};
472 dbus::ObjectProxy* dbus_object_proxy_;
473
474 DISALLOW_COPY_AND_ASSIGN(TestInterfaceProxy);
475};
476
477} // namespace chromium
478} // namespace org
479
480namespace org {
481namespace chromium {
482
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800483// Abstract interface proxy for org::chromium::TestInterface2.
484class TestInterface2ProxyInterface {
485 public:
486 protected:
Alex Deymo16856102015-06-19 20:55:14 -0700487 virtual ~TestInterface2ProxyInterface() = default;
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800488};
489
490} // namespace chromium
491} // namespace org
492
493namespace org {
494namespace chromium {
495
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800496// Interface proxy for org::chromium::TestInterface2.
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800497class TestInterface2Proxy final : public TestInterface2ProxyInterface {
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800498 public:
499 TestInterface2Proxy(
500 const scoped_refptr<dbus::Bus>& bus,
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800501 const dbus::ObjectPath& object_path) :
502 bus_{bus},
503 object_path_{object_path},
504 dbus_object_proxy_{
505 bus_->GetObjectProxy(service_name_, object_path_)} {
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800506 }
507
Alex Deymo16856102015-06-19 20:55:14 -0700508 ~TestInterface2Proxy() override {
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800509 }
510
511 void ReleaseObjectProxy(const base::Closure& callback) {
512 bus_->RemoveObjectProxy(service_name_, object_path_, callback);
513 }
514
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800515 const dbus::ObjectPath& GetObjectPath() const {
516 return object_path_;
517 }
518
519 dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; }
520
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800521 private:
522 scoped_refptr<dbus::Bus> bus_;
523 const std::string service_name_{"org.chromium.Test"};
524 dbus::ObjectPath object_path_;
525 dbus::ObjectProxy* dbus_object_proxy_;
526
527 DISALLOW_COPY_AND_ASSIGN(TestInterface2Proxy);
528};
529
530} // namespace chromium
531} // namespace org
Alex Vakulenko6ef2d732014-11-25 14:04:27 -0800532)literal_string";
533
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800534const char kExpectedContentWithObjectManager[] = R"literal_string(
535#include <memory>
536#include <string>
537#include <vector>
538
539#include <base/bind.h>
540#include <base/callback.h>
541#include <base/logging.h>
542#include <base/macros.h>
543#include <base/memory/ref_counted.h>
544#include <chromeos/any.h>
545#include <chromeos/dbus/dbus_method_invoker.h>
546#include <chromeos/dbus/dbus_property.h>
547#include <chromeos/dbus/dbus_signal_handler.h>
548#include <chromeos/errors/error.h>
549#include <chromeos/variant_dictionary.h>
550#include <dbus/bus.h>
551#include <dbus/message.h>
552#include <dbus/object_manager.h>
553#include <dbus/object_path.h>
554#include <dbus/object_proxy.h>
555
556namespace org {
557namespace chromium {
558class ObjectManagerProxy;
559} // namespace chromium
560} // namespace org
561
562namespace org {
563namespace chromium {
564
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800565// Abstract interface proxy for org::chromium::Itf1.
566class Itf1ProxyInterface {
567 public:
Alex Vakulenko59dd2e92015-02-23 15:14:28 -0800568 static const char* DataName() { return "Data"; }
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800569 virtual const std::string& data() const = 0;
570
571 protected:
Alex Deymo16856102015-06-19 20:55:14 -0700572 virtual ~Itf1ProxyInterface() = default;
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800573};
574
575} // namespace chromium
576} // namespace org
577
578namespace org {
579namespace chromium {
580
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800581// Interface proxy for org::chromium::Itf1.
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800582class Itf1Proxy final : public Itf1ProxyInterface {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800583 public:
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800584 class PropertySet : public dbus::PropertySet {
585 public:
586 PropertySet(dbus::ObjectProxy* object_proxy,
587 const PropertyChangedCallback& callback)
588 : dbus::PropertySet{object_proxy,
589 "org.chromium.Itf1",
590 callback} {
Alex Vakulenko59dd2e92015-02-23 15:14:28 -0800591 RegisterProperty(DataName(), &data);
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800592 }
593
594 chromeos::dbus_utils::Property<std::string> data;
595
596 private:
597 DISALLOW_COPY_AND_ASSIGN(PropertySet);
598 };
599
600 Itf1Proxy(
601 const scoped_refptr<dbus::Bus>& bus,
602 const std::string& service_name,
603 PropertySet* property_set) :
604 bus_{bus},
605 service_name_{service_name},
606 property_set_{property_set},
607 dbus_object_proxy_{
608 bus_->GetObjectProxy(service_name_, object_path_)} {
609 }
610
Alex Deymo16856102015-06-19 20:55:14 -0700611 ~Itf1Proxy() override {
Christopher Wiley824b5242014-12-03 11:58:01 -0800612 }
613
614 void RegisterCloserSignalHandler(
615 const base::Closure& signal_callback,
616 dbus::ObjectProxy::OnConnectedCallback on_connected_callback) {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800617 chromeos::dbus_utils::ConnectToSignal(
618 dbus_object_proxy_,
619 "org.chromium.Itf1",
620 "Closer",
Christopher Wiley824b5242014-12-03 11:58:01 -0800621 signal_callback,
622 on_connected_callback);
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800623 }
624
625 void ReleaseObjectProxy(const base::Closure& callback) {
626 bus_->RemoveObjectProxy(service_name_, object_path_, callback);
627 }
628
629 const dbus::ObjectPath& GetObjectPath() const {
630 return object_path_;
631 }
632
633 dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; }
634
635 void SetPropertyChangedCallback(
636 const base::Callback<void(Itf1Proxy*, const std::string&)>& callback) {
637 on_property_changed_ = callback;
638 }
639
640 const PropertySet* GetProperties() const { return property_set_; }
641 PropertySet* GetProperties() { return property_set_; }
642
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800643 const std::string& data() const override {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800644 return property_set_->data.value();
645 }
646
647 private:
648 void OnPropertyChanged(const std::string& property_name) {
649 if (!on_property_changed_.is_null())
650 on_property_changed_.Run(this, property_name);
651 }
652
653 scoped_refptr<dbus::Bus> bus_;
654 std::string service_name_;
655 const dbus::ObjectPath object_path_{"/org/chromium/Test/Object"};
656 PropertySet* property_set_;
657 base::Callback<void(Itf1Proxy*, const std::string&)> on_property_changed_;
658 dbus::ObjectProxy* dbus_object_proxy_;
659
660 friend class org::chromium::ObjectManagerProxy;
661 DISALLOW_COPY_AND_ASSIGN(Itf1Proxy);
662};
663
664} // namespace chromium
665} // namespace org
666
667namespace org {
668namespace chromium {
669
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800670// Abstract interface proxy for org::chromium::Itf2.
671class Itf2ProxyInterface {
672 public:
673 protected:
Alex Deymo16856102015-06-19 20:55:14 -0700674 virtual ~Itf2ProxyInterface() = default;
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800675};
676
677} // namespace chromium
678} // namespace org
679
680namespace org {
681namespace chromium {
682
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800683// Interface proxy for org::chromium::Itf2.
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800684class Itf2Proxy final : public Itf2ProxyInterface {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800685 public:
686 class PropertySet : public dbus::PropertySet {
687 public:
688 PropertySet(dbus::ObjectProxy* object_proxy,
689 const PropertyChangedCallback& callback)
690 : dbus::PropertySet{object_proxy,
691 "org.chromium.Itf2",
692 callback} {
693 }
694
695
696 private:
697 DISALLOW_COPY_AND_ASSIGN(PropertySet);
698 };
699
700 Itf2Proxy(
701 const scoped_refptr<dbus::Bus>& bus,
702 const std::string& service_name,
703 const dbus::ObjectPath& object_path) :
704 bus_{bus},
705 service_name_{service_name},
706 object_path_{object_path},
707 dbus_object_proxy_{
708 bus_->GetObjectProxy(service_name_, object_path_)} {
709 }
710
Alex Deymo16856102015-06-19 20:55:14 -0700711 ~Itf2Proxy() override {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800712 }
713
714 void ReleaseObjectProxy(const base::Closure& callback) {
715 bus_->RemoveObjectProxy(service_name_, object_path_, callback);
716 }
717
718 const dbus::ObjectPath& GetObjectPath() const {
719 return object_path_;
720 }
721
722 dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; }
723
724 private:
725 scoped_refptr<dbus::Bus> bus_;
726 std::string service_name_;
727 dbus::ObjectPath object_path_;
728 dbus::ObjectProxy* dbus_object_proxy_;
729
730 DISALLOW_COPY_AND_ASSIGN(Itf2Proxy);
731};
732
733} // namespace chromium
734} // namespace org
735
736namespace org {
737namespace chromium {
738
739class ObjectManagerProxy : public dbus::ObjectManager::Interface {
740 public:
741 ObjectManagerProxy(const scoped_refptr<dbus::Bus>& bus,
742 const std::string& service_name)
743 : bus_{bus},
Christopher Wileyefe9df52015-03-12 16:02:22 -0700744 service_name_{service_name},
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800745 dbus_object_manager_{bus->GetObjectManager(
746 service_name,
747 dbus::ObjectPath{"/org/chromium/Test"})} {
748 dbus_object_manager_->RegisterInterface("org.chromium.Itf1", this);
749 dbus_object_manager_->RegisterInterface("org.chromium.Itf2", this);
750 }
751
Christopher Wileye11e81c2014-12-19 13:10:52 -0800752 ~ObjectManagerProxy() override {
753 dbus_object_manager_->UnregisterInterface("org.chromium.Itf1");
754 dbus_object_manager_->UnregisterInterface("org.chromium.Itf2");
755 }
756
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800757 dbus::ObjectManager* GetObjectManagerProxy() const {
758 return dbus_object_manager_;
759 }
760
Alex Vakulenkoc4e01f12015-02-23 15:55:21 -0800761 org::chromium::Itf1Proxy* GetItf1Proxy() {
762 if (itf1_instances_.empty())
763 return nullptr;
764 return itf1_instances_.begin()->second.get();
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800765 }
766 std::vector<org::chromium::Itf1Proxy*> GetItf1Instances() const {
767 std::vector<org::chromium::Itf1Proxy*> values;
768 values.reserve(itf1_instances_.size());
769 for (const auto& pair : itf1_instances_)
770 values.push_back(pair.second.get());
771 return values;
772 }
773 void SetItf1AddedCallback(
774 const base::Callback<void(org::chromium::Itf1Proxy*)>& callback) {
775 on_itf1_added_ = callback;
776 }
777 void SetItf1RemovedCallback(
778 const base::Callback<void(const dbus::ObjectPath&)>& callback) {
779 on_itf1_removed_ = callback;
780 }
781
782 org::chromium::Itf2Proxy* GetItf2Proxy(
783 const dbus::ObjectPath& object_path) {
784 auto p = itf2_instances_.find(object_path);
785 if (p != itf2_instances_.end())
786 return p->second.get();
787 return nullptr;
788 }
789 std::vector<org::chromium::Itf2Proxy*> GetItf2Instances() const {
790 std::vector<org::chromium::Itf2Proxy*> values;
791 values.reserve(itf2_instances_.size());
792 for (const auto& pair : itf2_instances_)
793 values.push_back(pair.second.get());
794 return values;
795 }
796 void SetItf2AddedCallback(
797 const base::Callback<void(org::chromium::Itf2Proxy*)>& callback) {
798 on_itf2_added_ = callback;
799 }
800 void SetItf2RemovedCallback(
801 const base::Callback<void(const dbus::ObjectPath&)>& callback) {
802 on_itf2_removed_ = callback;
803 }
804
805 private:
806 void OnPropertyChanged(const dbus::ObjectPath& object_path,
807 const std::string& interface_name,
808 const std::string& property_name) {
809 if (interface_name == "org.chromium.Itf1") {
810 auto p = itf1_instances_.find(object_path);
811 if (p == itf1_instances_.end())
812 return;
813 p->second->OnPropertyChanged(property_name);
814 return;
815 }
816 }
817
818 void ObjectAdded(
819 const dbus::ObjectPath& object_path,
820 const std::string& interface_name) override {
821 if (interface_name == "org.chromium.Itf1") {
822 auto property_set =
823 static_cast<org::chromium::Itf1Proxy::PropertySet*>(
824 dbus_object_manager_->GetProperties(object_path, interface_name));
825 std::unique_ptr<org::chromium::Itf1Proxy> itf1_proxy{
Christopher Wileyefe9df52015-03-12 16:02:22 -0700826 new org::chromium::Itf1Proxy{bus_, service_name_, property_set}
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800827 };
828 auto p = itf1_instances_.emplace(object_path, std::move(itf1_proxy));
829 if (!on_itf1_added_.is_null())
830 on_itf1_added_.Run(p.first->second.get());
831 return;
832 }
833 if (interface_name == "org.chromium.Itf2") {
834 std::unique_ptr<org::chromium::Itf2Proxy> itf2_proxy{
Christopher Wileyefe9df52015-03-12 16:02:22 -0700835 new org::chromium::Itf2Proxy{bus_, service_name_, object_path}
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800836 };
837 auto p = itf2_instances_.emplace(object_path, std::move(itf2_proxy));
838 if (!on_itf2_added_.is_null())
839 on_itf2_added_.Run(p.first->second.get());
840 return;
841 }
842 }
843
844 void ObjectRemoved(
845 const dbus::ObjectPath& object_path,
846 const std::string& interface_name) override {
847 if (interface_name == "org.chromium.Itf1") {
848 auto p = itf1_instances_.find(object_path);
849 if (p != itf1_instances_.end()) {
850 if (!on_itf1_removed_.is_null())
851 on_itf1_removed_.Run(object_path);
852 itf1_instances_.erase(p);
853 }
854 return;
855 }
856 if (interface_name == "org.chromium.Itf2") {
857 auto p = itf2_instances_.find(object_path);
858 if (p != itf2_instances_.end()) {
859 if (!on_itf2_removed_.is_null())
860 on_itf2_removed_.Run(object_path);
861 itf2_instances_.erase(p);
862 }
863 return;
864 }
865 }
866
867 dbus::PropertySet* CreateProperties(
868 dbus::ObjectProxy* object_proxy,
869 const dbus::ObjectPath& object_path,
870 const std::string& interface_name) override {
871 if (interface_name == "org.chromium.Itf1") {
872 return new org::chromium::Itf1Proxy::PropertySet{
873 object_proxy,
874 base::Bind(&ObjectManagerProxy::OnPropertyChanged,
875 weak_ptr_factory_.GetWeakPtr(),
876 object_path,
877 interface_name)
878 };
879 }
880 if (interface_name == "org.chromium.Itf2") {
881 return new org::chromium::Itf2Proxy::PropertySet{
882 object_proxy,
883 base::Bind(&ObjectManagerProxy::OnPropertyChanged,
884 weak_ptr_factory_.GetWeakPtr(),
885 object_path,
886 interface_name)
887 };
888 }
889 LOG(FATAL) << "Creating properties for unsupported interface "
890 << interface_name;
891 return nullptr;
892 }
893
894 scoped_refptr<dbus::Bus> bus_;
Christopher Wileyefe9df52015-03-12 16:02:22 -0700895 std::string service_name_;
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800896 dbus::ObjectManager* dbus_object_manager_;
897 std::map<dbus::ObjectPath,
898 std::unique_ptr<org::chromium::Itf1Proxy>> itf1_instances_;
899 base::Callback<void(org::chromium::Itf1Proxy*)> on_itf1_added_;
900 base::Callback<void(const dbus::ObjectPath&)> on_itf1_removed_;
901 std::map<dbus::ObjectPath,
902 std::unique_ptr<org::chromium::Itf2Proxy>> itf2_instances_;
903 base::Callback<void(org::chromium::Itf2Proxy*)> on_itf2_added_;
904 base::Callback<void(const dbus::ObjectPath&)> on_itf2_removed_;
905 base::WeakPtrFactory<ObjectManagerProxy> weak_ptr_factory_{this};
906
907 DISALLOW_COPY_AND_ASSIGN(ObjectManagerProxy);
908};
909
910} // namespace chromium
911} // namespace org
912)literal_string";
913
914const char kExpectedContentWithObjectManagerAndServiceName[] = R"literal_string(
915#include <memory>
916#include <string>
917#include <vector>
918
919#include <base/bind.h>
920#include <base/callback.h>
921#include <base/logging.h>
922#include <base/macros.h>
923#include <base/memory/ref_counted.h>
924#include <chromeos/any.h>
925#include <chromeos/dbus/dbus_method_invoker.h>
926#include <chromeos/dbus/dbus_property.h>
927#include <chromeos/dbus/dbus_signal_handler.h>
928#include <chromeos/errors/error.h>
929#include <chromeos/variant_dictionary.h>
930#include <dbus/bus.h>
931#include <dbus/message.h>
932#include <dbus/object_manager.h>
933#include <dbus/object_path.h>
934#include <dbus/object_proxy.h>
935
936namespace org {
937namespace chromium {
938class ObjectManagerProxy;
939} // namespace chromium
940} // namespace org
941
942namespace org {
943namespace chromium {
944
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800945// Abstract interface proxy for org::chromium::Itf1.
946class Itf1ProxyInterface {
947 public:
948 protected:
Alex Deymo16856102015-06-19 20:55:14 -0700949 virtual ~Itf1ProxyInterface() = default;
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800950};
951
952} // namespace chromium
953} // namespace org
954
955namespace org {
956namespace chromium {
957
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800958// Interface proxy for org::chromium::Itf1.
Alex Vakulenko008e19d2015-01-21 10:37:00 -0800959class Itf1Proxy final : public Itf1ProxyInterface {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800960 public:
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800961 class PropertySet : public dbus::PropertySet {
962 public:
963 PropertySet(dbus::ObjectProxy* object_proxy,
964 const PropertyChangedCallback& callback)
965 : dbus::PropertySet{object_proxy,
966 "org.chromium.Itf1",
967 callback} {
968 }
969
970
971 private:
972 DISALLOW_COPY_AND_ASSIGN(PropertySet);
973 };
974
975 Itf1Proxy(const scoped_refptr<dbus::Bus>& bus) :
976 bus_{bus},
977 dbus_object_proxy_{
978 bus_->GetObjectProxy(service_name_, object_path_)} {
979 }
980
Alex Deymo16856102015-06-19 20:55:14 -0700981 ~Itf1Proxy() override {
Christopher Wiley824b5242014-12-03 11:58:01 -0800982 }
983
984 void RegisterCloserSignalHandler(
985 const base::Closure& signal_callback,
986 dbus::ObjectProxy::OnConnectedCallback on_connected_callback) {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800987 chromeos::dbus_utils::ConnectToSignal(
988 dbus_object_proxy_,
989 "org.chromium.Itf1",
990 "Closer",
Christopher Wiley824b5242014-12-03 11:58:01 -0800991 signal_callback,
992 on_connected_callback);
Alex Vakulenko99e8fb02014-12-01 17:22:03 -0800993 }
994
995 void ReleaseObjectProxy(const base::Closure& callback) {
996 bus_->RemoveObjectProxy(service_name_, object_path_, callback);
997 }
998
999 const dbus::ObjectPath& GetObjectPath() const {
1000 return object_path_;
1001 }
1002
1003 dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; }
1004
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001005 private:
1006 scoped_refptr<dbus::Bus> bus_;
1007 const std::string service_name_{"org.chromium.Test"};
1008 const dbus::ObjectPath object_path_{"/org/chromium/Test/Object"};
1009 dbus::ObjectProxy* dbus_object_proxy_;
1010
1011 DISALLOW_COPY_AND_ASSIGN(Itf1Proxy);
1012};
1013
1014} // namespace chromium
1015} // namespace org
1016
1017namespace org {
1018namespace chromium {
1019
Alex Vakulenko008e19d2015-01-21 10:37:00 -08001020// Abstract interface proxy for org::chromium::Itf2.
1021class Itf2ProxyInterface {
1022 public:
1023 protected:
Alex Deymo16856102015-06-19 20:55:14 -07001024 virtual ~Itf2ProxyInterface() = default;
Alex Vakulenko008e19d2015-01-21 10:37:00 -08001025};
1026
1027} // namespace chromium
1028} // namespace org
1029
1030namespace org {
1031namespace chromium {
1032
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001033// Interface proxy for org::chromium::Itf2.
Alex Vakulenko008e19d2015-01-21 10:37:00 -08001034class Itf2Proxy final : public Itf2ProxyInterface {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001035 public:
1036 class PropertySet : public dbus::PropertySet {
1037 public:
1038 PropertySet(dbus::ObjectProxy* object_proxy,
1039 const PropertyChangedCallback& callback)
1040 : dbus::PropertySet{object_proxy,
1041 "org.chromium.Itf2",
1042 callback} {
1043 }
1044
1045
1046 private:
1047 DISALLOW_COPY_AND_ASSIGN(PropertySet);
1048 };
1049
1050 Itf2Proxy(
1051 const scoped_refptr<dbus::Bus>& bus,
1052 const dbus::ObjectPath& object_path) :
1053 bus_{bus},
1054 object_path_{object_path},
1055 dbus_object_proxy_{
1056 bus_->GetObjectProxy(service_name_, object_path_)} {
1057 }
1058
Alex Deymo16856102015-06-19 20:55:14 -07001059 ~Itf2Proxy() override {
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001060 }
1061
1062 void ReleaseObjectProxy(const base::Closure& callback) {
1063 bus_->RemoveObjectProxy(service_name_, object_path_, callback);
1064 }
1065
1066 const dbus::ObjectPath& GetObjectPath() const {
1067 return object_path_;
1068 }
1069
1070 dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; }
1071
1072 private:
1073 scoped_refptr<dbus::Bus> bus_;
1074 const std::string service_name_{"org.chromium.Test"};
1075 dbus::ObjectPath object_path_;
1076 dbus::ObjectProxy* dbus_object_proxy_;
1077
1078 DISALLOW_COPY_AND_ASSIGN(Itf2Proxy);
1079};
1080
1081} // namespace chromium
1082} // namespace org
1083
1084namespace org {
1085namespace chromium {
1086
1087class ObjectManagerProxy : public dbus::ObjectManager::Interface {
1088 public:
1089 ObjectManagerProxy(const scoped_refptr<dbus::Bus>& bus)
1090 : bus_{bus},
1091 dbus_object_manager_{bus->GetObjectManager(
1092 "org.chromium.Test",
1093 dbus::ObjectPath{"/org/chromium/Test"})} {
1094 dbus_object_manager_->RegisterInterface("org.chromium.Itf1", this);
1095 dbus_object_manager_->RegisterInterface("org.chromium.Itf2", this);
1096 }
1097
Christopher Wileye11e81c2014-12-19 13:10:52 -08001098 ~ObjectManagerProxy() override {
1099 dbus_object_manager_->UnregisterInterface("org.chromium.Itf1");
1100 dbus_object_manager_->UnregisterInterface("org.chromium.Itf2");
1101 }
1102
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001103 dbus::ObjectManager* GetObjectManagerProxy() const {
1104 return dbus_object_manager_;
1105 }
1106
Alex Vakulenkoc4e01f12015-02-23 15:55:21 -08001107 org::chromium::Itf1Proxy* GetItf1Proxy() {
1108 if (itf1_instances_.empty())
1109 return nullptr;
1110 return itf1_instances_.begin()->second.get();
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001111 }
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));
Christopher Wileyd0cb3fe2015-07-27 17:32:58 -07001260 int written = base::WriteFile(path, contents.c_str(), contents.size());
1261 EXPECT_EQ(contents.size(), static_cast<size_t>(written));
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001262 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.
Alex Deymof211ae62015-07-28 13:09:29 -07001310 test_utils::EXPECT_TEXT_CONTAINED(kExpectedContent, contents);
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001311}
1312
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001313TEST_F(ProxyGeneratorTest, GenerateAdaptorsWithServiceName) {
1314 Interface interface;
Alex Vakulenkob69be602015-02-23 14:40:42 -08001315 interface.name = "org.chromium.TestInterface";
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001316 interface.path = "/org/chromium/Test";
Alex Vakulenkob69be602015-02-23 14:40:42 -08001317 interface.signals.emplace_back("Closer");
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001318 Interface interface2;
Alex Vakulenkob69be602015-02-23 14:40:42 -08001319 interface2.name = "org.chromium.TestInterface2";
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001320 vector<Interface> interfaces{interface, interface2};
1321 base::FilePath output_path = temp_dir_.path().Append("output2.h");
1322 ServiceConfig config;
1323 config.service_name = "org.chromium.Test";
1324 EXPECT_TRUE(ProxyGenerator::GenerateProxies(config, interfaces, output_path));
1325 string contents;
1326 EXPECT_TRUE(base::ReadFileToString(output_path, &contents));
1327 // The header guards contain the (temporary) filename, so we search for
1328 // the content we need within the string.
Alex Deymof211ae62015-07-28 13:09:29 -07001329 test_utils::EXPECT_TEXT_CONTAINED(kExpectedContentWithService, contents);
Alex Vakulenko6ef2d732014-11-25 14:04:27 -08001330}
1331
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001332TEST_F(ProxyGeneratorTest, GenerateAdaptorsWithObjectManager) {
1333 Interface interface;
1334 interface.name = "org.chromium.Itf1";
1335 interface.path = "/org/chromium/Test/Object";
Alex Vakulenkob69be602015-02-23 14:40:42 -08001336 interface.signals.emplace_back("Closer");
Alex Vakulenko59dd2e92015-02-23 15:14:28 -08001337 interface.properties.emplace_back("Data", "s", "read");
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001338 Interface interface2;
1339 interface2.name = "org.chromium.Itf2";
1340 vector<Interface> interfaces{interface, interface2};
1341 base::FilePath output_path = temp_dir_.path().Append("output3.h");
1342 ServiceConfig config;
1343 config.object_manager.name = "org.chromium.ObjectManager";
1344 config.object_manager.object_path = "/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(
1351 kExpectedContentWithObjectManager, contents);
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001352}
1353
1354TEST_F(ProxyGeneratorTest, GenerateAdaptorsWithObjectManagerAndServiceName) {
1355 Interface interface;
1356 interface.name = "org.chromium.Itf1";
1357 interface.path = "/org/chromium/Test/Object";
Alex Vakulenkob69be602015-02-23 14:40:42 -08001358 interface.signals.emplace_back("Closer");
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("output4.h");
1363 ServiceConfig config;
1364 config.service_name = "org.chromium.Test";
1365 config.object_manager.name = "org.chromium.ObjectManager";
1366 config.object_manager.object_path = "/org/chromium/Test";
1367 EXPECT_TRUE(ProxyGenerator::GenerateProxies(config, interfaces, output_path));
1368 string contents;
1369 EXPECT_TRUE(base::ReadFileToString(output_path, &contents));
1370 // The header guards contain the (temporary) filename, so we search for
1371 // the content we need within the string.
Alex Deymof211ae62015-07-28 13:09:29 -07001372 test_utils::EXPECT_TEXT_CONTAINED(
1373 kExpectedContentWithObjectManagerAndServiceName, contents);
Alex Vakulenko99e8fb02014-12-01 17:22:03 -08001374}
1375
Paul Stewart1dce1ae2014-10-01 05:30:18 -07001376} // namespace chromeos_dbus_bindings