Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 1 | // 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 | |
| 10 | #include <base/file_util.h> |
| 11 | #include <base/files/file_path.h> |
| 12 | #include <base/files/scoped_temp_dir.h> |
| 13 | #include <gtest/gtest.h> |
| 14 | |
| 15 | #include "chromeos-dbus-bindings/interface.h" |
| 16 | |
| 17 | using std::string; |
| 18 | using std::vector; |
| 19 | using testing::Test; |
| 20 | |
| 21 | namespace chromeos_dbus_bindings { |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | const char kInterfaceName[] = "org.chromium.TestInterface"; |
Alex Vakulenko | fafef13 | 2014-11-03 14:52:09 -0800 | [diff] [blame] | 26 | const char kInterfaceName2[] = "org.chromium.TestInterface2"; |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 27 | const char kMethod1Name[] = "Elements"; |
| 28 | const char kMethod1Return[] = "s"; |
| 29 | const char kMethod1Argument1[] = "s"; |
| 30 | const char kMethod1ArgumentName1[] = "space_walk"; |
| 31 | const char kMethod1Argument2[] = "ao"; |
| 32 | const char kMethod1ArgumentName2[] = "ramblin_man"; |
| 33 | const char kMethod2Name[] = "ReturnToPatagonia"; |
| 34 | const char kMethod2Return[] = "x"; |
| 35 | const char kMethod3Name[] = "NiceWeatherForDucks"; |
| 36 | const char kMethod3Argument1[] = "b"; |
| 37 | const char kMethod4Name[] = "ExperimentNumberSix"; |
Alex Vakulenko | fafef13 | 2014-11-03 14:52:09 -0800 | [diff] [blame] | 38 | const char kMethod5Name[] = "GetPersonInfo"; |
| 39 | const char kMethod5Argument1[] = "s"; |
| 40 | const char kMethod5ArgumentName1[] = "name"; |
| 41 | const char kMethod5Argument2[] = "i"; |
| 42 | const char kMethod5ArgumentName2[] = "age"; |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 43 | const char kSignal1Name[] = "Closer"; |
| 44 | const char kSignal2Name[] = "TheCurseOfKaZar"; |
| 45 | const char kSignal2Argument1[] = "as"; |
| 46 | const char kSignal2Argument2[] = "y"; |
| 47 | const char kExpectedContent[] = R"literal_string( |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame] | 48 | #include <memory> |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 49 | #include <string> |
| 50 | #include <vector> |
| 51 | |
| 52 | #include <base/bind.h> |
| 53 | #include <base/callback.h> |
| 54 | #include <base/logging.h> |
| 55 | #include <base/macros.h> |
| 56 | #include <base/memory/ref_counted.h> |
| 57 | #include <chromeos/any.h> |
| 58 | #include <chromeos/dbus/dbus_method_invoker.h> |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame] | 59 | #include <chromeos/dbus/dbus_property.h> |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 60 | #include <chromeos/dbus/dbus_signal_handler.h> |
| 61 | #include <chromeos/errors/error.h> |
Alex Vakulenko | 4f3c05e | 2014-11-12 15:01:46 -0800 | [diff] [blame] | 62 | #include <chromeos/variant_dictionary.h> |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 63 | #include <dbus/bus.h> |
| 64 | #include <dbus/message.h> |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame] | 65 | #include <dbus/object_manager.h> |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 66 | #include <dbus/object_path.h> |
| 67 | #include <dbus/object_proxy.h> |
| 68 | |
| 69 | namespace org { |
| 70 | namespace chromium { |
| 71 | |
Alex Vakulenko | c95f06b | 2014-11-20 15:06:09 -0800 | [diff] [blame] | 72 | // Interface proxy for org::chromium::TestInterface. |
Alex Vakulenko | 4f3c05e | 2014-11-12 15:01:46 -0800 | [diff] [blame] | 73 | class TestInterfaceProxy final { |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 74 | public: |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 75 | TestInterfaceProxy( |
| 76 | const scoped_refptr<dbus::Bus>& bus, |
Alex Vakulenko | 6ef2d73 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 77 | const std::string& service_name) : |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame] | 78 | bus_{bus}, |
| 79 | service_name_{service_name}, |
| 80 | dbus_object_proxy_{ |
| 81 | bus_->GetObjectProxy(service_name_, object_path_)} { |
Alex Vakulenko | c95f06b | 2014-11-20 15:06:09 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Christopher Wiley | 824b524 | 2014-12-03 11:58:01 -0800 | [diff] [blame] | 84 | ~TestInterfaceProxy() { |
| 85 | } |
| 86 | |
| 87 | void RegisterCloserSignalHandler( |
| 88 | const base::Closure& signal_callback, |
| 89 | dbus::ObjectProxy::OnConnectedCallback on_connected_callback) { |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 90 | chromeos::dbus_utils::ConnectToSignal( |
| 91 | dbus_object_proxy_, |
| 92 | "org.chromium.TestInterface", |
| 93 | "Closer", |
Christopher Wiley | 824b524 | 2014-12-03 11:58:01 -0800 | [diff] [blame] | 94 | signal_callback, |
| 95 | on_connected_callback); |
| 96 | } |
| 97 | |
| 98 | void RegisterTheCurseOfKaZarSignalHandler( |
| 99 | const base::Callback<void(const std::vector<std::string>&, |
| 100 | uint8_t)>& signal_callback, |
| 101 | dbus::ObjectProxy::OnConnectedCallback on_connected_callback) { |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 102 | chromeos::dbus_utils::ConnectToSignal( |
| 103 | dbus_object_proxy_, |
| 104 | "org.chromium.TestInterface", |
| 105 | "TheCurseOfKaZar", |
Christopher Wiley | 824b524 | 2014-12-03 11:58:01 -0800 | [diff] [blame] | 106 | signal_callback, |
| 107 | on_connected_callback); |
Alex Vakulenko | c95f06b | 2014-11-20 15:06:09 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | void ReleaseObjectProxy(const base::Closure& callback) { |
| 111 | bus_->RemoveObjectProxy(service_name_, object_path_, callback); |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 112 | } |
Alex Vakulenko | 4f3c05e | 2014-11-12 15:01:46 -0800 | [diff] [blame] | 113 | |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame] | 114 | const dbus::ObjectPath& GetObjectPath() const { |
| 115 | return object_path_; |
| 116 | } |
| 117 | |
| 118 | dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; } |
| 119 | |
Alex Vakulenko | 4f3c05e | 2014-11-12 15:01:46 -0800 | [diff] [blame] | 120 | bool Elements( |
Alex Vakulenko | fafef13 | 2014-11-03 14:52:09 -0800 | [diff] [blame] | 121 | const std::string& in_space_walk, |
| 122 | const std::vector<dbus::ObjectPath>& in_ramblin_man, |
| 123 | std::string* out_3, |
Alex Vakulenko | c4e32f5 | 2014-12-04 08:28:49 -0800 | [diff] [blame^] | 124 | chromeos::ErrorPtr* error, |
| 125 | int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) { |
| 126 | auto response = chromeos::dbus_utils::CallMethodAndBlockWithTimeout( |
| 127 | timeout_ms, |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 128 | dbus_object_proxy_, |
| 129 | "org.chromium.TestInterface", |
| 130 | "Elements", |
| 131 | error, |
Alex Vakulenko | fafef13 | 2014-11-03 14:52:09 -0800 | [diff] [blame] | 132 | in_space_walk, |
| 133 | in_ramblin_man); |
| 134 | return response && chromeos::dbus_utils::ExtractMethodCallResults( |
| 135 | response.get(), error, out_3); |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 136 | } |
Alex Vakulenko | 4f3c05e | 2014-11-12 15:01:46 -0800 | [diff] [blame] | 137 | |
Alex Vakulenko | c4e32f5 | 2014-12-04 08:28:49 -0800 | [diff] [blame^] | 138 | void ElementsAsync( |
| 139 | const std::string& in_space_walk, |
| 140 | const std::vector<dbus::ObjectPath>& in_ramblin_man, |
| 141 | const base::Callback<void(const std::string&)>& success_callback, |
| 142 | const base::Callback<void(chromeos::Error*)>& error_callback, |
| 143 | int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) { |
| 144 | chromeos::dbus_utils::CallMethodWithTimeout( |
| 145 | timeout_ms, |
| 146 | dbus_object_proxy_, |
| 147 | "org.chromium.TestInterface", |
| 148 | "Elements", |
| 149 | success_callback, |
| 150 | error_callback, |
| 151 | in_space_walk, |
| 152 | in_ramblin_man); |
| 153 | } |
| 154 | |
Alex Vakulenko | 4f3c05e | 2014-11-12 15:01:46 -0800 | [diff] [blame] | 155 | bool ReturnToPatagonia( |
Alex Vakulenko | fafef13 | 2014-11-03 14:52:09 -0800 | [diff] [blame] | 156 | int64_t* out_1, |
Alex Vakulenko | c4e32f5 | 2014-12-04 08:28:49 -0800 | [diff] [blame^] | 157 | chromeos::ErrorPtr* error, |
| 158 | int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) { |
| 159 | auto response = chromeos::dbus_utils::CallMethodAndBlockWithTimeout( |
| 160 | timeout_ms, |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 161 | dbus_object_proxy_, |
| 162 | "org.chromium.TestInterface", |
| 163 | "ReturnToPatagonia", |
| 164 | error); |
Alex Vakulenko | fafef13 | 2014-11-03 14:52:09 -0800 | [diff] [blame] | 165 | return response && chromeos::dbus_utils::ExtractMethodCallResults( |
| 166 | response.get(), error, out_1); |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 167 | } |
Alex Vakulenko | 4f3c05e | 2014-11-12 15:01:46 -0800 | [diff] [blame] | 168 | |
Alex Vakulenko | c4e32f5 | 2014-12-04 08:28:49 -0800 | [diff] [blame^] | 169 | void ReturnToPatagoniaAsync( |
| 170 | const base::Callback<void(int64_t)>& success_callback, |
| 171 | const base::Callback<void(chromeos::Error*)>& error_callback, |
| 172 | int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) { |
| 173 | chromeos::dbus_utils::CallMethodWithTimeout( |
| 174 | timeout_ms, |
| 175 | dbus_object_proxy_, |
| 176 | "org.chromium.TestInterface", |
| 177 | "ReturnToPatagonia", |
| 178 | success_callback, |
| 179 | error_callback); |
| 180 | } |
| 181 | |
Alex Vakulenko | 4f3c05e | 2014-11-12 15:01:46 -0800 | [diff] [blame] | 182 | bool NiceWeatherForDucks( |
Alex Vakulenko | fafef13 | 2014-11-03 14:52:09 -0800 | [diff] [blame] | 183 | bool in_1, |
Alex Vakulenko | c4e32f5 | 2014-12-04 08:28:49 -0800 | [diff] [blame^] | 184 | chromeos::ErrorPtr* error, |
| 185 | int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) { |
| 186 | auto response = chromeos::dbus_utils::CallMethodAndBlockWithTimeout( |
| 187 | timeout_ms, |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 188 | dbus_object_proxy_, |
| 189 | "org.chromium.TestInterface", |
| 190 | "NiceWeatherForDucks", |
| 191 | error, |
Alex Vakulenko | fafef13 | 2014-11-03 14:52:09 -0800 | [diff] [blame] | 192 | in_1); |
| 193 | return response && chromeos::dbus_utils::ExtractMethodCallResults( |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 194 | response.get(), error); |
| 195 | } |
Alex Vakulenko | 4f3c05e | 2014-11-12 15:01:46 -0800 | [diff] [blame] | 196 | |
Alex Vakulenko | c4e32f5 | 2014-12-04 08:28:49 -0800 | [diff] [blame^] | 197 | void NiceWeatherForDucksAsync( |
| 198 | bool in_1, |
| 199 | const base::Callback<void()>& success_callback, |
| 200 | const base::Callback<void(chromeos::Error*)>& error_callback, |
| 201 | int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) { |
| 202 | chromeos::dbus_utils::CallMethodWithTimeout( |
| 203 | timeout_ms, |
| 204 | dbus_object_proxy_, |
| 205 | "org.chromium.TestInterface", |
| 206 | "NiceWeatherForDucks", |
| 207 | success_callback, |
| 208 | error_callback, |
| 209 | in_1); |
| 210 | } |
| 211 | |
Alex Vakulenko | 4f3c05e | 2014-11-12 15:01:46 -0800 | [diff] [blame] | 212 | // Comment line1 |
| 213 | // line2 |
| 214 | bool ExperimentNumberSix( |
Alex Vakulenko | c4e32f5 | 2014-12-04 08:28:49 -0800 | [diff] [blame^] | 215 | chromeos::ErrorPtr* error, |
| 216 | int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) { |
| 217 | auto response = chromeos::dbus_utils::CallMethodAndBlockWithTimeout( |
| 218 | timeout_ms, |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 219 | dbus_object_proxy_, |
| 220 | "org.chromium.TestInterface", |
| 221 | "ExperimentNumberSix", |
| 222 | error); |
Alex Vakulenko | fafef13 | 2014-11-03 14:52:09 -0800 | [diff] [blame] | 223 | return response && chromeos::dbus_utils::ExtractMethodCallResults( |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 224 | response.get(), error); |
| 225 | } |
Alex Vakulenko | 4f3c05e | 2014-11-12 15:01:46 -0800 | [diff] [blame] | 226 | |
Alex Vakulenko | c4e32f5 | 2014-12-04 08:28:49 -0800 | [diff] [blame^] | 227 | // Comment line1 |
| 228 | // line2 |
| 229 | void ExperimentNumberSixAsync( |
| 230 | const base::Callback<void()>& success_callback, |
| 231 | const base::Callback<void(chromeos::Error*)>& error_callback, |
| 232 | int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) { |
| 233 | chromeos::dbus_utils::CallMethodWithTimeout( |
| 234 | timeout_ms, |
| 235 | dbus_object_proxy_, |
| 236 | "org.chromium.TestInterface", |
| 237 | "ExperimentNumberSix", |
| 238 | success_callback, |
| 239 | error_callback); |
| 240 | } |
| 241 | |
Alex Vakulenko | c95f06b | 2014-11-20 15:06:09 -0800 | [diff] [blame] | 242 | private: |
| 243 | scoped_refptr<dbus::Bus> bus_; |
| 244 | std::string service_name_; |
Alex Vakulenko | 6ef2d73 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 245 | const dbus::ObjectPath object_path_{"/org/chromium/Test"}; |
Alex Vakulenko | c95f06b | 2014-11-20 15:06:09 -0800 | [diff] [blame] | 246 | dbus::ObjectProxy* dbus_object_proxy_; |
| 247 | |
| 248 | DISALLOW_COPY_AND_ASSIGN(TestInterfaceProxy); |
| 249 | }; |
| 250 | |
| 251 | } // namespace chromium |
| 252 | } // namespace org |
| 253 | |
| 254 | namespace org { |
| 255 | namespace chromium { |
| 256 | |
| 257 | // Interface proxy for org::chromium::TestInterface2. |
| 258 | class TestInterface2Proxy final { |
| 259 | public: |
| 260 | TestInterface2Proxy( |
| 261 | const scoped_refptr<dbus::Bus>& bus, |
| 262 | const std::string& service_name, |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame] | 263 | const dbus::ObjectPath& object_path) : |
| 264 | bus_{bus}, |
| 265 | service_name_{service_name}, |
| 266 | object_path_{object_path}, |
| 267 | dbus_object_proxy_{ |
| 268 | bus_->GetObjectProxy(service_name_, object_path_)} { |
Alex Vakulenko | c95f06b | 2014-11-20 15:06:09 -0800 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | ~TestInterface2Proxy() { |
| 272 | } |
| 273 | |
| 274 | void ReleaseObjectProxy(const base::Closure& callback) { |
| 275 | bus_->RemoveObjectProxy(service_name_, object_path_, callback); |
| 276 | } |
| 277 | |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame] | 278 | const dbus::ObjectPath& GetObjectPath() const { |
| 279 | return object_path_; |
| 280 | } |
| 281 | |
| 282 | dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; } |
| 283 | |
Alex Vakulenko | 4f3c05e | 2014-11-12 15:01:46 -0800 | [diff] [blame] | 284 | bool GetPersonInfo( |
Alex Vakulenko | fafef13 | 2014-11-03 14:52:09 -0800 | [diff] [blame] | 285 | std::string* out_name, |
| 286 | int32_t* out_age, |
Alex Vakulenko | c4e32f5 | 2014-12-04 08:28:49 -0800 | [diff] [blame^] | 287 | chromeos::ErrorPtr* error, |
| 288 | int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) { |
| 289 | auto response = chromeos::dbus_utils::CallMethodAndBlockWithTimeout( |
| 290 | timeout_ms, |
Alex Vakulenko | fafef13 | 2014-11-03 14:52:09 -0800 | [diff] [blame] | 291 | dbus_object_proxy_, |
| 292 | "org.chromium.TestInterface2", |
| 293 | "GetPersonInfo", |
| 294 | error); |
| 295 | return response && chromeos::dbus_utils::ExtractMethodCallResults( |
| 296 | response.get(), error, out_name, out_age); |
| 297 | } |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 298 | |
Alex Vakulenko | c4e32f5 | 2014-12-04 08:28:49 -0800 | [diff] [blame^] | 299 | void GetPersonInfoAsync( |
| 300 | const base::Callback<void(const std::string& /*name*/, int32_t /*age*/)>& success_callback, |
| 301 | const base::Callback<void(chromeos::Error*)>& error_callback, |
| 302 | int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT) { |
| 303 | chromeos::dbus_utils::CallMethodWithTimeout( |
| 304 | timeout_ms, |
| 305 | dbus_object_proxy_, |
| 306 | "org.chromium.TestInterface2", |
| 307 | "GetPersonInfo", |
| 308 | success_callback, |
| 309 | error_callback); |
| 310 | } |
| 311 | |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 312 | private: |
| 313 | scoped_refptr<dbus::Bus> bus_; |
| 314 | std::string service_name_; |
| 315 | dbus::ObjectPath object_path_; |
| 316 | dbus::ObjectProxy* dbus_object_proxy_; |
| 317 | |
Alex Vakulenko | c95f06b | 2014-11-20 15:06:09 -0800 | [diff] [blame] | 318 | DISALLOW_COPY_AND_ASSIGN(TestInterface2Proxy); |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 319 | }; |
| 320 | |
| 321 | } // namespace chromium |
| 322 | } // namespace org |
| 323 | )literal_string"; |
| 324 | |
Alex Vakulenko | 6ef2d73 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 325 | const char kExpectedContentWithService[] = R"literal_string( |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame] | 326 | #include <memory> |
Alex Vakulenko | 6ef2d73 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 327 | #include <string> |
| 328 | #include <vector> |
| 329 | |
| 330 | #include <base/bind.h> |
| 331 | #include <base/callback.h> |
| 332 | #include <base/logging.h> |
| 333 | #include <base/macros.h> |
| 334 | #include <base/memory/ref_counted.h> |
| 335 | #include <chromeos/any.h> |
| 336 | #include <chromeos/dbus/dbus_method_invoker.h> |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame] | 337 | #include <chromeos/dbus/dbus_property.h> |
Alex Vakulenko | 6ef2d73 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 338 | #include <chromeos/dbus/dbus_signal_handler.h> |
| 339 | #include <chromeos/errors/error.h> |
| 340 | #include <chromeos/variant_dictionary.h> |
| 341 | #include <dbus/bus.h> |
| 342 | #include <dbus/message.h> |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame] | 343 | #include <dbus/object_manager.h> |
Alex Vakulenko | 6ef2d73 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 344 | #include <dbus/object_path.h> |
| 345 | #include <dbus/object_proxy.h> |
| 346 | |
| 347 | namespace org { |
| 348 | namespace chromium { |
| 349 | |
| 350 | // Interface proxy for org::chromium::TestInterface. |
| 351 | class TestInterfaceProxy final { |
| 352 | public: |
Alex Vakulenko | 6ef2d73 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 353 | TestInterfaceProxy(const scoped_refptr<dbus::Bus>& bus) : |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame] | 354 | bus_{bus}, |
| 355 | dbus_object_proxy_{ |
| 356 | bus_->GetObjectProxy(service_name_, object_path_)} { |
Alex Vakulenko | 6ef2d73 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 357 | } |
| 358 | |
Christopher Wiley | 824b524 | 2014-12-03 11:58:01 -0800 | [diff] [blame] | 359 | ~TestInterfaceProxy() { |
| 360 | } |
| 361 | |
| 362 | void RegisterCloserSignalHandler( |
| 363 | const base::Closure& signal_callback, |
| 364 | dbus::ObjectProxy::OnConnectedCallback on_connected_callback) { |
Alex Vakulenko | 6ef2d73 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 365 | chromeos::dbus_utils::ConnectToSignal( |
| 366 | dbus_object_proxy_, |
| 367 | "org.chromium.TestInterface", |
| 368 | "Closer", |
Christopher Wiley | 824b524 | 2014-12-03 11:58:01 -0800 | [diff] [blame] | 369 | signal_callback, |
| 370 | on_connected_callback); |
Alex Vakulenko | 6ef2d73 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | void ReleaseObjectProxy(const base::Closure& callback) { |
| 374 | bus_->RemoveObjectProxy(service_name_, object_path_, callback); |
| 375 | } |
| 376 | |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame] | 377 | const dbus::ObjectPath& GetObjectPath() const { |
| 378 | return object_path_; |
| 379 | } |
| 380 | |
| 381 | dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; } |
| 382 | |
Alex Vakulenko | 6ef2d73 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 383 | private: |
| 384 | scoped_refptr<dbus::Bus> bus_; |
| 385 | const std::string service_name_{"org.chromium.Test"}; |
| 386 | const dbus::ObjectPath object_path_{"/org/chromium/Test"}; |
| 387 | dbus::ObjectProxy* dbus_object_proxy_; |
| 388 | |
| 389 | DISALLOW_COPY_AND_ASSIGN(TestInterfaceProxy); |
| 390 | }; |
| 391 | |
| 392 | } // namespace chromium |
| 393 | } // namespace org |
| 394 | |
| 395 | namespace org { |
| 396 | namespace chromium { |
| 397 | |
| 398 | // Interface proxy for org::chromium::TestInterface2. |
| 399 | class TestInterface2Proxy final { |
| 400 | public: |
| 401 | TestInterface2Proxy( |
| 402 | const scoped_refptr<dbus::Bus>& bus, |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame] | 403 | const dbus::ObjectPath& object_path) : |
| 404 | bus_{bus}, |
| 405 | object_path_{object_path}, |
| 406 | dbus_object_proxy_{ |
| 407 | bus_->GetObjectProxy(service_name_, object_path_)} { |
Alex Vakulenko | 6ef2d73 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | ~TestInterface2Proxy() { |
| 411 | } |
| 412 | |
| 413 | void ReleaseObjectProxy(const base::Closure& callback) { |
| 414 | bus_->RemoveObjectProxy(service_name_, object_path_, callback); |
| 415 | } |
| 416 | |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame] | 417 | const dbus::ObjectPath& GetObjectPath() const { |
| 418 | return object_path_; |
| 419 | } |
| 420 | |
| 421 | dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; } |
| 422 | |
Alex Vakulenko | 6ef2d73 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 423 | private: |
| 424 | scoped_refptr<dbus::Bus> bus_; |
| 425 | const std::string service_name_{"org.chromium.Test"}; |
| 426 | dbus::ObjectPath object_path_; |
| 427 | dbus::ObjectProxy* dbus_object_proxy_; |
| 428 | |
| 429 | DISALLOW_COPY_AND_ASSIGN(TestInterface2Proxy); |
| 430 | }; |
| 431 | |
| 432 | } // namespace chromium |
| 433 | } // namespace org |
Alex Vakulenko | 6ef2d73 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 434 | )literal_string"; |
| 435 | |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame] | 436 | const char kExpectedContentWithObjectManager[] = R"literal_string( |
| 437 | #include <memory> |
| 438 | #include <string> |
| 439 | #include <vector> |
| 440 | |
| 441 | #include <base/bind.h> |
| 442 | #include <base/callback.h> |
| 443 | #include <base/logging.h> |
| 444 | #include <base/macros.h> |
| 445 | #include <base/memory/ref_counted.h> |
| 446 | #include <chromeos/any.h> |
| 447 | #include <chromeos/dbus/dbus_method_invoker.h> |
| 448 | #include <chromeos/dbus/dbus_property.h> |
| 449 | #include <chromeos/dbus/dbus_signal_handler.h> |
| 450 | #include <chromeos/errors/error.h> |
| 451 | #include <chromeos/variant_dictionary.h> |
| 452 | #include <dbus/bus.h> |
| 453 | #include <dbus/message.h> |
| 454 | #include <dbus/object_manager.h> |
| 455 | #include <dbus/object_path.h> |
| 456 | #include <dbus/object_proxy.h> |
| 457 | |
| 458 | namespace org { |
| 459 | namespace chromium { |
| 460 | class ObjectManagerProxy; |
| 461 | } // namespace chromium |
| 462 | } // namespace org |
| 463 | |
| 464 | namespace org { |
| 465 | namespace chromium { |
| 466 | |
| 467 | // Interface proxy for org::chromium::Itf1. |
| 468 | class Itf1Proxy final { |
| 469 | public: |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame] | 470 | class PropertySet : public dbus::PropertySet { |
| 471 | public: |
| 472 | PropertySet(dbus::ObjectProxy* object_proxy, |
| 473 | const PropertyChangedCallback& callback) |
| 474 | : dbus::PropertySet{object_proxy, |
| 475 | "org.chromium.Itf1", |
| 476 | callback} { |
| 477 | RegisterProperty("data", &data); |
| 478 | } |
| 479 | |
| 480 | chromeos::dbus_utils::Property<std::string> data; |
| 481 | |
| 482 | private: |
| 483 | DISALLOW_COPY_AND_ASSIGN(PropertySet); |
| 484 | }; |
| 485 | |
| 486 | Itf1Proxy( |
| 487 | const scoped_refptr<dbus::Bus>& bus, |
| 488 | const std::string& service_name, |
| 489 | PropertySet* property_set) : |
| 490 | bus_{bus}, |
| 491 | service_name_{service_name}, |
| 492 | property_set_{property_set}, |
| 493 | dbus_object_proxy_{ |
| 494 | bus_->GetObjectProxy(service_name_, object_path_)} { |
| 495 | } |
| 496 | |
Christopher Wiley | 824b524 | 2014-12-03 11:58:01 -0800 | [diff] [blame] | 497 | ~Itf1Proxy() { |
| 498 | } |
| 499 | |
| 500 | void RegisterCloserSignalHandler( |
| 501 | const base::Closure& signal_callback, |
| 502 | dbus::ObjectProxy::OnConnectedCallback on_connected_callback) { |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame] | 503 | chromeos::dbus_utils::ConnectToSignal( |
| 504 | dbus_object_proxy_, |
| 505 | "org.chromium.Itf1", |
| 506 | "Closer", |
Christopher Wiley | 824b524 | 2014-12-03 11:58:01 -0800 | [diff] [blame] | 507 | signal_callback, |
| 508 | on_connected_callback); |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | void ReleaseObjectProxy(const base::Closure& callback) { |
| 512 | bus_->RemoveObjectProxy(service_name_, object_path_, callback); |
| 513 | } |
| 514 | |
| 515 | const dbus::ObjectPath& GetObjectPath() const { |
| 516 | return object_path_; |
| 517 | } |
| 518 | |
| 519 | dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; } |
| 520 | |
| 521 | void SetPropertyChangedCallback( |
| 522 | const base::Callback<void(Itf1Proxy*, const std::string&)>& callback) { |
| 523 | on_property_changed_ = callback; |
| 524 | } |
| 525 | |
| 526 | const PropertySet* GetProperties() const { return property_set_; } |
| 527 | PropertySet* GetProperties() { return property_set_; } |
| 528 | |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame] | 529 | const std::string& data() const { |
| 530 | return property_set_->data.value(); |
| 531 | } |
| 532 | |
| 533 | private: |
| 534 | void OnPropertyChanged(const std::string& property_name) { |
| 535 | if (!on_property_changed_.is_null()) |
| 536 | on_property_changed_.Run(this, property_name); |
| 537 | } |
| 538 | |
| 539 | scoped_refptr<dbus::Bus> bus_; |
| 540 | std::string service_name_; |
| 541 | const dbus::ObjectPath object_path_{"/org/chromium/Test/Object"}; |
| 542 | PropertySet* property_set_; |
| 543 | base::Callback<void(Itf1Proxy*, const std::string&)> on_property_changed_; |
| 544 | dbus::ObjectProxy* dbus_object_proxy_; |
| 545 | |
| 546 | friend class org::chromium::ObjectManagerProxy; |
| 547 | DISALLOW_COPY_AND_ASSIGN(Itf1Proxy); |
| 548 | }; |
| 549 | |
| 550 | } // namespace chromium |
| 551 | } // namespace org |
| 552 | |
| 553 | namespace org { |
| 554 | namespace chromium { |
| 555 | |
| 556 | // Interface proxy for org::chromium::Itf2. |
| 557 | class Itf2Proxy final { |
| 558 | public: |
| 559 | class PropertySet : public dbus::PropertySet { |
| 560 | public: |
| 561 | PropertySet(dbus::ObjectProxy* object_proxy, |
| 562 | const PropertyChangedCallback& callback) |
| 563 | : dbus::PropertySet{object_proxy, |
| 564 | "org.chromium.Itf2", |
| 565 | callback} { |
| 566 | } |
| 567 | |
| 568 | |
| 569 | private: |
| 570 | DISALLOW_COPY_AND_ASSIGN(PropertySet); |
| 571 | }; |
| 572 | |
| 573 | Itf2Proxy( |
| 574 | const scoped_refptr<dbus::Bus>& bus, |
| 575 | const std::string& service_name, |
| 576 | const dbus::ObjectPath& object_path) : |
| 577 | bus_{bus}, |
| 578 | service_name_{service_name}, |
| 579 | object_path_{object_path}, |
| 580 | dbus_object_proxy_{ |
| 581 | bus_->GetObjectProxy(service_name_, object_path_)} { |
| 582 | } |
| 583 | |
| 584 | ~Itf2Proxy() { |
| 585 | } |
| 586 | |
| 587 | void ReleaseObjectProxy(const base::Closure& callback) { |
| 588 | bus_->RemoveObjectProxy(service_name_, object_path_, callback); |
| 589 | } |
| 590 | |
| 591 | const dbus::ObjectPath& GetObjectPath() const { |
| 592 | return object_path_; |
| 593 | } |
| 594 | |
| 595 | dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; } |
| 596 | |
| 597 | private: |
| 598 | scoped_refptr<dbus::Bus> bus_; |
| 599 | std::string service_name_; |
| 600 | dbus::ObjectPath object_path_; |
| 601 | dbus::ObjectProxy* dbus_object_proxy_; |
| 602 | |
| 603 | DISALLOW_COPY_AND_ASSIGN(Itf2Proxy); |
| 604 | }; |
| 605 | |
| 606 | } // namespace chromium |
| 607 | } // namespace org |
| 608 | |
| 609 | namespace org { |
| 610 | namespace chromium { |
| 611 | |
| 612 | class ObjectManagerProxy : public dbus::ObjectManager::Interface { |
| 613 | public: |
| 614 | ObjectManagerProxy(const scoped_refptr<dbus::Bus>& bus, |
| 615 | const std::string& service_name) |
| 616 | : bus_{bus}, |
| 617 | dbus_object_manager_{bus->GetObjectManager( |
| 618 | service_name, |
| 619 | dbus::ObjectPath{"/org/chromium/Test"})} { |
| 620 | dbus_object_manager_->RegisterInterface("org.chromium.Itf1", this); |
| 621 | dbus_object_manager_->RegisterInterface("org.chromium.Itf2", this); |
| 622 | } |
| 623 | |
| 624 | dbus::ObjectManager* GetObjectManagerProxy() const { |
| 625 | return dbus_object_manager_; |
| 626 | } |
| 627 | |
| 628 | org::chromium::Itf1Proxy* GetItf1Proxy( |
| 629 | const dbus::ObjectPath& object_path) { |
| 630 | auto p = itf1_instances_.find(object_path); |
| 631 | if (p != itf1_instances_.end()) |
| 632 | return p->second.get(); |
| 633 | return nullptr; |
| 634 | } |
| 635 | std::vector<org::chromium::Itf1Proxy*> GetItf1Instances() const { |
| 636 | std::vector<org::chromium::Itf1Proxy*> values; |
| 637 | values.reserve(itf1_instances_.size()); |
| 638 | for (const auto& pair : itf1_instances_) |
| 639 | values.push_back(pair.second.get()); |
| 640 | return values; |
| 641 | } |
| 642 | void SetItf1AddedCallback( |
| 643 | const base::Callback<void(org::chromium::Itf1Proxy*)>& callback) { |
| 644 | on_itf1_added_ = callback; |
| 645 | } |
| 646 | void SetItf1RemovedCallback( |
| 647 | const base::Callback<void(const dbus::ObjectPath&)>& callback) { |
| 648 | on_itf1_removed_ = callback; |
| 649 | } |
| 650 | |
| 651 | org::chromium::Itf2Proxy* GetItf2Proxy( |
| 652 | const dbus::ObjectPath& object_path) { |
| 653 | auto p = itf2_instances_.find(object_path); |
| 654 | if (p != itf2_instances_.end()) |
| 655 | return p->second.get(); |
| 656 | return nullptr; |
| 657 | } |
| 658 | std::vector<org::chromium::Itf2Proxy*> GetItf2Instances() const { |
| 659 | std::vector<org::chromium::Itf2Proxy*> values; |
| 660 | values.reserve(itf2_instances_.size()); |
| 661 | for (const auto& pair : itf2_instances_) |
| 662 | values.push_back(pair.second.get()); |
| 663 | return values; |
| 664 | } |
| 665 | void SetItf2AddedCallback( |
| 666 | const base::Callback<void(org::chromium::Itf2Proxy*)>& callback) { |
| 667 | on_itf2_added_ = callback; |
| 668 | } |
| 669 | void SetItf2RemovedCallback( |
| 670 | const base::Callback<void(const dbus::ObjectPath&)>& callback) { |
| 671 | on_itf2_removed_ = callback; |
| 672 | } |
| 673 | |
| 674 | private: |
| 675 | void OnPropertyChanged(const dbus::ObjectPath& object_path, |
| 676 | const std::string& interface_name, |
| 677 | const std::string& property_name) { |
| 678 | if (interface_name == "org.chromium.Itf1") { |
| 679 | auto p = itf1_instances_.find(object_path); |
| 680 | if (p == itf1_instances_.end()) |
| 681 | return; |
| 682 | p->second->OnPropertyChanged(property_name); |
| 683 | return; |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | void ObjectAdded( |
| 688 | const dbus::ObjectPath& object_path, |
| 689 | const std::string& interface_name) override { |
| 690 | if (interface_name == "org.chromium.Itf1") { |
| 691 | auto property_set = |
| 692 | static_cast<org::chromium::Itf1Proxy::PropertySet*>( |
| 693 | dbus_object_manager_->GetProperties(object_path, interface_name)); |
| 694 | std::unique_ptr<org::chromium::Itf1Proxy> itf1_proxy{ |
| 695 | new org::chromium::Itf1Proxy{bus_, property_set} |
| 696 | }; |
| 697 | auto p = itf1_instances_.emplace(object_path, std::move(itf1_proxy)); |
| 698 | if (!on_itf1_added_.is_null()) |
| 699 | on_itf1_added_.Run(p.first->second.get()); |
| 700 | return; |
| 701 | } |
| 702 | if (interface_name == "org.chromium.Itf2") { |
| 703 | std::unique_ptr<org::chromium::Itf2Proxy> itf2_proxy{ |
| 704 | new org::chromium::Itf2Proxy{bus_, object_path} |
| 705 | }; |
| 706 | auto p = itf2_instances_.emplace(object_path, std::move(itf2_proxy)); |
| 707 | if (!on_itf2_added_.is_null()) |
| 708 | on_itf2_added_.Run(p.first->second.get()); |
| 709 | return; |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | void ObjectRemoved( |
| 714 | const dbus::ObjectPath& object_path, |
| 715 | const std::string& interface_name) override { |
| 716 | if (interface_name == "org.chromium.Itf1") { |
| 717 | auto p = itf1_instances_.find(object_path); |
| 718 | if (p != itf1_instances_.end()) { |
| 719 | if (!on_itf1_removed_.is_null()) |
| 720 | on_itf1_removed_.Run(object_path); |
| 721 | itf1_instances_.erase(p); |
| 722 | } |
| 723 | return; |
| 724 | } |
| 725 | if (interface_name == "org.chromium.Itf2") { |
| 726 | auto p = itf2_instances_.find(object_path); |
| 727 | if (p != itf2_instances_.end()) { |
| 728 | if (!on_itf2_removed_.is_null()) |
| 729 | on_itf2_removed_.Run(object_path); |
| 730 | itf2_instances_.erase(p); |
| 731 | } |
| 732 | return; |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | dbus::PropertySet* CreateProperties( |
| 737 | dbus::ObjectProxy* object_proxy, |
| 738 | const dbus::ObjectPath& object_path, |
| 739 | const std::string& interface_name) override { |
| 740 | if (interface_name == "org.chromium.Itf1") { |
| 741 | return new org::chromium::Itf1Proxy::PropertySet{ |
| 742 | object_proxy, |
| 743 | base::Bind(&ObjectManagerProxy::OnPropertyChanged, |
| 744 | weak_ptr_factory_.GetWeakPtr(), |
| 745 | object_path, |
| 746 | interface_name) |
| 747 | }; |
| 748 | } |
| 749 | if (interface_name == "org.chromium.Itf2") { |
| 750 | return new org::chromium::Itf2Proxy::PropertySet{ |
| 751 | object_proxy, |
| 752 | base::Bind(&ObjectManagerProxy::OnPropertyChanged, |
| 753 | weak_ptr_factory_.GetWeakPtr(), |
| 754 | object_path, |
| 755 | interface_name) |
| 756 | }; |
| 757 | } |
| 758 | LOG(FATAL) << "Creating properties for unsupported interface " |
| 759 | << interface_name; |
| 760 | return nullptr; |
| 761 | } |
| 762 | |
| 763 | scoped_refptr<dbus::Bus> bus_; |
| 764 | dbus::ObjectManager* dbus_object_manager_; |
| 765 | std::map<dbus::ObjectPath, |
| 766 | std::unique_ptr<org::chromium::Itf1Proxy>> itf1_instances_; |
| 767 | base::Callback<void(org::chromium::Itf1Proxy*)> on_itf1_added_; |
| 768 | base::Callback<void(const dbus::ObjectPath&)> on_itf1_removed_; |
| 769 | std::map<dbus::ObjectPath, |
| 770 | std::unique_ptr<org::chromium::Itf2Proxy>> itf2_instances_; |
| 771 | base::Callback<void(org::chromium::Itf2Proxy*)> on_itf2_added_; |
| 772 | base::Callback<void(const dbus::ObjectPath&)> on_itf2_removed_; |
| 773 | base::WeakPtrFactory<ObjectManagerProxy> weak_ptr_factory_{this}; |
| 774 | |
| 775 | DISALLOW_COPY_AND_ASSIGN(ObjectManagerProxy); |
| 776 | }; |
| 777 | |
| 778 | } // namespace chromium |
| 779 | } // namespace org |
| 780 | )literal_string"; |
| 781 | |
| 782 | const char kExpectedContentWithObjectManagerAndServiceName[] = R"literal_string( |
| 783 | #include <memory> |
| 784 | #include <string> |
| 785 | #include <vector> |
| 786 | |
| 787 | #include <base/bind.h> |
| 788 | #include <base/callback.h> |
| 789 | #include <base/logging.h> |
| 790 | #include <base/macros.h> |
| 791 | #include <base/memory/ref_counted.h> |
| 792 | #include <chromeos/any.h> |
| 793 | #include <chromeos/dbus/dbus_method_invoker.h> |
| 794 | #include <chromeos/dbus/dbus_property.h> |
| 795 | #include <chromeos/dbus/dbus_signal_handler.h> |
| 796 | #include <chromeos/errors/error.h> |
| 797 | #include <chromeos/variant_dictionary.h> |
| 798 | #include <dbus/bus.h> |
| 799 | #include <dbus/message.h> |
| 800 | #include <dbus/object_manager.h> |
| 801 | #include <dbus/object_path.h> |
| 802 | #include <dbus/object_proxy.h> |
| 803 | |
| 804 | namespace org { |
| 805 | namespace chromium { |
| 806 | class ObjectManagerProxy; |
| 807 | } // namespace chromium |
| 808 | } // namespace org |
| 809 | |
| 810 | namespace org { |
| 811 | namespace chromium { |
| 812 | |
| 813 | // Interface proxy for org::chromium::Itf1. |
| 814 | class Itf1Proxy final { |
| 815 | public: |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame] | 816 | class PropertySet : public dbus::PropertySet { |
| 817 | public: |
| 818 | PropertySet(dbus::ObjectProxy* object_proxy, |
| 819 | const PropertyChangedCallback& callback) |
| 820 | : dbus::PropertySet{object_proxy, |
| 821 | "org.chromium.Itf1", |
| 822 | callback} { |
| 823 | } |
| 824 | |
| 825 | |
| 826 | private: |
| 827 | DISALLOW_COPY_AND_ASSIGN(PropertySet); |
| 828 | }; |
| 829 | |
| 830 | Itf1Proxy(const scoped_refptr<dbus::Bus>& bus) : |
| 831 | bus_{bus}, |
| 832 | dbus_object_proxy_{ |
| 833 | bus_->GetObjectProxy(service_name_, object_path_)} { |
| 834 | } |
| 835 | |
Christopher Wiley | 824b524 | 2014-12-03 11:58:01 -0800 | [diff] [blame] | 836 | ~Itf1Proxy() { |
| 837 | } |
| 838 | |
| 839 | void RegisterCloserSignalHandler( |
| 840 | const base::Closure& signal_callback, |
| 841 | dbus::ObjectProxy::OnConnectedCallback on_connected_callback) { |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame] | 842 | chromeos::dbus_utils::ConnectToSignal( |
| 843 | dbus_object_proxy_, |
| 844 | "org.chromium.Itf1", |
| 845 | "Closer", |
Christopher Wiley | 824b524 | 2014-12-03 11:58:01 -0800 | [diff] [blame] | 846 | signal_callback, |
| 847 | on_connected_callback); |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | void ReleaseObjectProxy(const base::Closure& callback) { |
| 851 | bus_->RemoveObjectProxy(service_name_, object_path_, callback); |
| 852 | } |
| 853 | |
| 854 | const dbus::ObjectPath& GetObjectPath() const { |
| 855 | return object_path_; |
| 856 | } |
| 857 | |
| 858 | dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; } |
| 859 | |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame] | 860 | private: |
| 861 | scoped_refptr<dbus::Bus> bus_; |
| 862 | const std::string service_name_{"org.chromium.Test"}; |
| 863 | const dbus::ObjectPath object_path_{"/org/chromium/Test/Object"}; |
| 864 | dbus::ObjectProxy* dbus_object_proxy_; |
| 865 | |
| 866 | DISALLOW_COPY_AND_ASSIGN(Itf1Proxy); |
| 867 | }; |
| 868 | |
| 869 | } // namespace chromium |
| 870 | } // namespace org |
| 871 | |
| 872 | namespace org { |
| 873 | namespace chromium { |
| 874 | |
| 875 | // Interface proxy for org::chromium::Itf2. |
| 876 | class Itf2Proxy final { |
| 877 | public: |
| 878 | class PropertySet : public dbus::PropertySet { |
| 879 | public: |
| 880 | PropertySet(dbus::ObjectProxy* object_proxy, |
| 881 | const PropertyChangedCallback& callback) |
| 882 | : dbus::PropertySet{object_proxy, |
| 883 | "org.chromium.Itf2", |
| 884 | callback} { |
| 885 | } |
| 886 | |
| 887 | |
| 888 | private: |
| 889 | DISALLOW_COPY_AND_ASSIGN(PropertySet); |
| 890 | }; |
| 891 | |
| 892 | Itf2Proxy( |
| 893 | const scoped_refptr<dbus::Bus>& bus, |
| 894 | const dbus::ObjectPath& object_path) : |
| 895 | bus_{bus}, |
| 896 | object_path_{object_path}, |
| 897 | dbus_object_proxy_{ |
| 898 | bus_->GetObjectProxy(service_name_, object_path_)} { |
| 899 | } |
| 900 | |
| 901 | ~Itf2Proxy() { |
| 902 | } |
| 903 | |
| 904 | void ReleaseObjectProxy(const base::Closure& callback) { |
| 905 | bus_->RemoveObjectProxy(service_name_, object_path_, callback); |
| 906 | } |
| 907 | |
| 908 | const dbus::ObjectPath& GetObjectPath() const { |
| 909 | return object_path_; |
| 910 | } |
| 911 | |
| 912 | dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; } |
| 913 | |
| 914 | private: |
| 915 | scoped_refptr<dbus::Bus> bus_; |
| 916 | const std::string service_name_{"org.chromium.Test"}; |
| 917 | dbus::ObjectPath object_path_; |
| 918 | dbus::ObjectProxy* dbus_object_proxy_; |
| 919 | |
| 920 | DISALLOW_COPY_AND_ASSIGN(Itf2Proxy); |
| 921 | }; |
| 922 | |
| 923 | } // namespace chromium |
| 924 | } // namespace org |
| 925 | |
| 926 | namespace org { |
| 927 | namespace chromium { |
| 928 | |
| 929 | class ObjectManagerProxy : public dbus::ObjectManager::Interface { |
| 930 | public: |
| 931 | ObjectManagerProxy(const scoped_refptr<dbus::Bus>& bus) |
| 932 | : bus_{bus}, |
| 933 | dbus_object_manager_{bus->GetObjectManager( |
| 934 | "org.chromium.Test", |
| 935 | dbus::ObjectPath{"/org/chromium/Test"})} { |
| 936 | dbus_object_manager_->RegisterInterface("org.chromium.Itf1", this); |
| 937 | dbus_object_manager_->RegisterInterface("org.chromium.Itf2", this); |
| 938 | } |
| 939 | |
| 940 | dbus::ObjectManager* GetObjectManagerProxy() const { |
| 941 | return dbus_object_manager_; |
| 942 | } |
| 943 | |
| 944 | org::chromium::Itf1Proxy* GetItf1Proxy( |
| 945 | const dbus::ObjectPath& object_path) { |
| 946 | auto p = itf1_instances_.find(object_path); |
| 947 | if (p != itf1_instances_.end()) |
| 948 | return p->second.get(); |
| 949 | return nullptr; |
| 950 | } |
| 951 | std::vector<org::chromium::Itf1Proxy*> GetItf1Instances() const { |
| 952 | std::vector<org::chromium::Itf1Proxy*> values; |
| 953 | values.reserve(itf1_instances_.size()); |
| 954 | for (const auto& pair : itf1_instances_) |
| 955 | values.push_back(pair.second.get()); |
| 956 | return values; |
| 957 | } |
| 958 | void SetItf1AddedCallback( |
| 959 | const base::Callback<void(org::chromium::Itf1Proxy*)>& callback) { |
| 960 | on_itf1_added_ = callback; |
| 961 | } |
| 962 | void SetItf1RemovedCallback( |
| 963 | const base::Callback<void(const dbus::ObjectPath&)>& callback) { |
| 964 | on_itf1_removed_ = callback; |
| 965 | } |
| 966 | |
| 967 | org::chromium::Itf2Proxy* GetItf2Proxy( |
| 968 | const dbus::ObjectPath& object_path) { |
| 969 | auto p = itf2_instances_.find(object_path); |
| 970 | if (p != itf2_instances_.end()) |
| 971 | return p->second.get(); |
| 972 | return nullptr; |
| 973 | } |
| 974 | std::vector<org::chromium::Itf2Proxy*> GetItf2Instances() const { |
| 975 | std::vector<org::chromium::Itf2Proxy*> values; |
| 976 | values.reserve(itf2_instances_.size()); |
| 977 | for (const auto& pair : itf2_instances_) |
| 978 | values.push_back(pair.second.get()); |
| 979 | return values; |
| 980 | } |
| 981 | void SetItf2AddedCallback( |
| 982 | const base::Callback<void(org::chromium::Itf2Proxy*)>& callback) { |
| 983 | on_itf2_added_ = callback; |
| 984 | } |
| 985 | void SetItf2RemovedCallback( |
| 986 | const base::Callback<void(const dbus::ObjectPath&)>& callback) { |
| 987 | on_itf2_removed_ = callback; |
| 988 | } |
| 989 | |
| 990 | private: |
| 991 | void OnPropertyChanged(const dbus::ObjectPath& object_path, |
| 992 | const std::string& interface_name, |
| 993 | const std::string& property_name) { |
| 994 | } |
| 995 | |
| 996 | void ObjectAdded( |
| 997 | const dbus::ObjectPath& object_path, |
| 998 | const std::string& interface_name) override { |
| 999 | if (interface_name == "org.chromium.Itf1") { |
| 1000 | std::unique_ptr<org::chromium::Itf1Proxy> itf1_proxy{ |
| 1001 | new org::chromium::Itf1Proxy{bus_} |
| 1002 | }; |
| 1003 | auto p = itf1_instances_.emplace(object_path, std::move(itf1_proxy)); |
| 1004 | if (!on_itf1_added_.is_null()) |
| 1005 | on_itf1_added_.Run(p.first->second.get()); |
| 1006 | return; |
| 1007 | } |
| 1008 | if (interface_name == "org.chromium.Itf2") { |
| 1009 | std::unique_ptr<org::chromium::Itf2Proxy> itf2_proxy{ |
| 1010 | new org::chromium::Itf2Proxy{bus_, object_path} |
| 1011 | }; |
| 1012 | auto p = itf2_instances_.emplace(object_path, std::move(itf2_proxy)); |
| 1013 | if (!on_itf2_added_.is_null()) |
| 1014 | on_itf2_added_.Run(p.first->second.get()); |
| 1015 | return; |
| 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | void ObjectRemoved( |
| 1020 | const dbus::ObjectPath& object_path, |
| 1021 | const std::string& interface_name) override { |
| 1022 | if (interface_name == "org.chromium.Itf1") { |
| 1023 | auto p = itf1_instances_.find(object_path); |
| 1024 | if (p != itf1_instances_.end()) { |
| 1025 | if (!on_itf1_removed_.is_null()) |
| 1026 | on_itf1_removed_.Run(object_path); |
| 1027 | itf1_instances_.erase(p); |
| 1028 | } |
| 1029 | return; |
| 1030 | } |
| 1031 | if (interface_name == "org.chromium.Itf2") { |
| 1032 | auto p = itf2_instances_.find(object_path); |
| 1033 | if (p != itf2_instances_.end()) { |
| 1034 | if (!on_itf2_removed_.is_null()) |
| 1035 | on_itf2_removed_.Run(object_path); |
| 1036 | itf2_instances_.erase(p); |
| 1037 | } |
| 1038 | return; |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | dbus::PropertySet* CreateProperties( |
| 1043 | dbus::ObjectProxy* object_proxy, |
| 1044 | const dbus::ObjectPath& object_path, |
| 1045 | const std::string& interface_name) override { |
| 1046 | if (interface_name == "org.chromium.Itf1") { |
| 1047 | return new org::chromium::Itf1Proxy::PropertySet{ |
| 1048 | object_proxy, |
| 1049 | base::Bind(&ObjectManagerProxy::OnPropertyChanged, |
| 1050 | weak_ptr_factory_.GetWeakPtr(), |
| 1051 | object_path, |
| 1052 | interface_name) |
| 1053 | }; |
| 1054 | } |
| 1055 | if (interface_name == "org.chromium.Itf2") { |
| 1056 | return new org::chromium::Itf2Proxy::PropertySet{ |
| 1057 | object_proxy, |
| 1058 | base::Bind(&ObjectManagerProxy::OnPropertyChanged, |
| 1059 | weak_ptr_factory_.GetWeakPtr(), |
| 1060 | object_path, |
| 1061 | interface_name) |
| 1062 | }; |
| 1063 | } |
| 1064 | LOG(FATAL) << "Creating properties for unsupported interface " |
| 1065 | << interface_name; |
| 1066 | return nullptr; |
| 1067 | } |
| 1068 | |
| 1069 | scoped_refptr<dbus::Bus> bus_; |
| 1070 | dbus::ObjectManager* dbus_object_manager_; |
| 1071 | std::map<dbus::ObjectPath, |
| 1072 | std::unique_ptr<org::chromium::Itf1Proxy>> itf1_instances_; |
| 1073 | base::Callback<void(org::chromium::Itf1Proxy*)> on_itf1_added_; |
| 1074 | base::Callback<void(const dbus::ObjectPath&)> on_itf1_removed_; |
| 1075 | std::map<dbus::ObjectPath, |
| 1076 | std::unique_ptr<org::chromium::Itf2Proxy>> itf2_instances_; |
| 1077 | base::Callback<void(org::chromium::Itf2Proxy*)> on_itf2_added_; |
| 1078 | base::Callback<void(const dbus::ObjectPath&)> on_itf2_removed_; |
| 1079 | base::WeakPtrFactory<ObjectManagerProxy> weak_ptr_factory_{this}; |
| 1080 | |
| 1081 | DISALLOW_COPY_AND_ASSIGN(ObjectManagerProxy); |
| 1082 | }; |
| 1083 | |
| 1084 | } // namespace chromium |
| 1085 | } // namespace org |
| 1086 | )literal_string"; |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 1087 | } // namespace |
| 1088 | |
| 1089 | class ProxyGeneratorTest : public Test { |
| 1090 | public: |
| 1091 | void SetUp() override { |
| 1092 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 1093 | } |
| 1094 | |
| 1095 | protected: |
| 1096 | base::FilePath CreateInputFile(const string& contents) { |
| 1097 | base::FilePath path; |
| 1098 | EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &path)); |
| 1099 | EXPECT_EQ(contents.size(), |
| 1100 | base::WriteFile(path, contents.c_str(), contents.size())); |
| 1101 | return path; |
| 1102 | } |
| 1103 | |
| 1104 | base::ScopedTempDir temp_dir_; |
| 1105 | }; |
| 1106 | |
| 1107 | TEST_F(ProxyGeneratorTest, GenerateAdaptors) { |
| 1108 | Interface interface; |
| 1109 | interface.name = kInterfaceName; |
Alex Vakulenko | 6ef2d73 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 1110 | interface.path = "/org/chromium/Test"; |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 1111 | interface.methods.emplace_back( |
| 1112 | kMethod1Name, |
| 1113 | vector<Interface::Argument>{ |
| 1114 | {kMethod1ArgumentName1, kMethod1Argument1}, |
| 1115 | {kMethod1ArgumentName2, kMethod1Argument2}}, |
| 1116 | vector<Interface::Argument>{{"", kMethod1Return}}); |
| 1117 | interface.methods.emplace_back( |
| 1118 | kMethod2Name, |
| 1119 | vector<Interface::Argument>{}, |
| 1120 | vector<Interface::Argument>{{"", kMethod2Return}}); |
| 1121 | interface.methods.emplace_back( |
| 1122 | kMethod3Name, |
| 1123 | vector<Interface::Argument>{{"", kMethod3Argument1}}, |
| 1124 | vector<Interface::Argument>{}); |
| 1125 | interface.methods.emplace_back(kMethod4Name); |
| 1126 | interface.signals.emplace_back(kSignal1Name); |
| 1127 | interface.signals.emplace_back( |
| 1128 | kSignal2Name, |
| 1129 | vector<Interface::Argument>{ |
| 1130 | {"", kSignal2Argument1}, |
| 1131 | {"", kSignal2Argument2}}); |
Alex Vakulenko | 6ef2d73 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 1132 | interface.methods.back().doc_string = "Comment line1\nline2"; |
Alex Vakulenko | fafef13 | 2014-11-03 14:52:09 -0800 | [diff] [blame] | 1133 | Interface interface2; |
| 1134 | interface2.name = kInterfaceName2; |
| 1135 | interface2.methods.emplace_back( |
| 1136 | kMethod5Name, |
| 1137 | vector<Interface::Argument>{}, |
| 1138 | vector<Interface::Argument>{ |
| 1139 | {kMethod5ArgumentName1, kMethod5Argument1}, |
| 1140 | {kMethod5ArgumentName2, kMethod5Argument2}}); |
| 1141 | vector<Interface> interfaces{interface, interface2}; |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 1142 | base::FilePath output_path = temp_dir_.path().Append("output.h"); |
Alex Vakulenko | 6ef2d73 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 1143 | ServiceConfig config; |
| 1144 | EXPECT_TRUE(ProxyGenerator::GenerateProxies(config, interfaces, output_path)); |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 1145 | string contents; |
| 1146 | EXPECT_TRUE(base::ReadFileToString(output_path, &contents)); |
| 1147 | // The header guards contain the (temporary) filename, so we search for |
| 1148 | // the content we need within the string. |
| 1149 | EXPECT_NE(string::npos, contents.find(kExpectedContent)) |
| 1150 | << "Expected to find the following content...\n" |
| 1151 | << kExpectedContent << "...within content...\n" << contents; |
| 1152 | } |
| 1153 | |
Alex Vakulenko | 6ef2d73 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 1154 | TEST_F(ProxyGeneratorTest, GenerateAdaptorsWithServiceName) { |
| 1155 | Interface interface; |
| 1156 | interface.name = kInterfaceName; |
| 1157 | interface.path = "/org/chromium/Test"; |
| 1158 | interface.signals.emplace_back(kSignal1Name); |
| 1159 | Interface interface2; |
| 1160 | interface2.name = kInterfaceName2; |
| 1161 | vector<Interface> interfaces{interface, interface2}; |
| 1162 | base::FilePath output_path = temp_dir_.path().Append("output2.h"); |
| 1163 | ServiceConfig config; |
| 1164 | config.service_name = "org.chromium.Test"; |
| 1165 | EXPECT_TRUE(ProxyGenerator::GenerateProxies(config, interfaces, output_path)); |
| 1166 | string contents; |
| 1167 | EXPECT_TRUE(base::ReadFileToString(output_path, &contents)); |
| 1168 | // The header guards contain the (temporary) filename, so we search for |
| 1169 | // the content we need within the string. |
| 1170 | EXPECT_NE(string::npos, contents.find(kExpectedContentWithService)) |
| 1171 | << "Expected to find the following content...\n" |
| 1172 | << kExpectedContentWithService << "...within content...\n" << contents; |
| 1173 | } |
| 1174 | |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame] | 1175 | TEST_F(ProxyGeneratorTest, GenerateAdaptorsWithObjectManager) { |
| 1176 | Interface interface; |
| 1177 | interface.name = "org.chromium.Itf1"; |
| 1178 | interface.path = "/org/chromium/Test/Object"; |
| 1179 | interface.signals.emplace_back(kSignal1Name); |
| 1180 | interface.properties.emplace_back("data", "s", "read"); |
| 1181 | Interface interface2; |
| 1182 | interface2.name = "org.chromium.Itf2"; |
| 1183 | vector<Interface> interfaces{interface, interface2}; |
| 1184 | base::FilePath output_path = temp_dir_.path().Append("output3.h"); |
| 1185 | ServiceConfig config; |
| 1186 | config.object_manager.name = "org.chromium.ObjectManager"; |
| 1187 | config.object_manager.object_path = "/org/chromium/Test"; |
| 1188 | EXPECT_TRUE(ProxyGenerator::GenerateProxies(config, interfaces, output_path)); |
| 1189 | string contents; |
| 1190 | EXPECT_TRUE(base::ReadFileToString(output_path, &contents)); |
| 1191 | // The header guards contain the (temporary) filename, so we search for |
| 1192 | // the content we need within the string. |
| 1193 | EXPECT_NE(string::npos, contents.find(kExpectedContentWithObjectManager)) |
| 1194 | << "Expected to find the following content...\n" |
| 1195 | << kExpectedContentWithObjectManager << "...within content...\n" |
| 1196 | << contents; |
| 1197 | } |
| 1198 | |
| 1199 | TEST_F(ProxyGeneratorTest, GenerateAdaptorsWithObjectManagerAndServiceName) { |
| 1200 | Interface interface; |
| 1201 | interface.name = "org.chromium.Itf1"; |
| 1202 | interface.path = "/org/chromium/Test/Object"; |
| 1203 | interface.signals.emplace_back(kSignal1Name); |
| 1204 | Interface interface2; |
| 1205 | interface2.name = "org.chromium.Itf2"; |
| 1206 | vector<Interface> interfaces{interface, interface2}; |
| 1207 | base::FilePath output_path = temp_dir_.path().Append("output4.h"); |
| 1208 | ServiceConfig config; |
| 1209 | config.service_name = "org.chromium.Test"; |
| 1210 | config.object_manager.name = "org.chromium.ObjectManager"; |
| 1211 | config.object_manager.object_path = "/org/chromium/Test"; |
| 1212 | EXPECT_TRUE(ProxyGenerator::GenerateProxies(config, interfaces, output_path)); |
| 1213 | string contents; |
| 1214 | EXPECT_TRUE(base::ReadFileToString(output_path, &contents)); |
| 1215 | // The header guards contain the (temporary) filename, so we search for |
| 1216 | // the content we need within the string. |
| 1217 | EXPECT_NE(string::npos, |
| 1218 | contents.find(kExpectedContentWithObjectManagerAndServiceName)) |
| 1219 | << "Expected to find the following content...\n" |
| 1220 | << kExpectedContentWithObjectManagerAndServiceName |
| 1221 | << "...within content...\n" << contents; |
| 1222 | } |
| 1223 | |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 1224 | } // namespace chromeos_dbus_bindings |