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 | |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 10 | #include <base/files/file_path.h> |
Alex Vakulenko | f3c6897 | 2014-12-10 12:52:31 -0800 | [diff] [blame] | 11 | #include <base/files/file_util.h> |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 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 | |
Christopher Wiley | e11e81c | 2014-12-19 13:10:52 -0800 | [diff] [blame^] | 624 | ~ObjectManagerProxy() override { |
| 625 | dbus_object_manager_->UnregisterInterface("org.chromium.Itf1"); |
| 626 | dbus_object_manager_->UnregisterInterface("org.chromium.Itf2"); |
| 627 | } |
| 628 | |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame] | 629 | dbus::ObjectManager* GetObjectManagerProxy() const { |
| 630 | return dbus_object_manager_; |
| 631 | } |
| 632 | |
| 633 | org::chromium::Itf1Proxy* GetItf1Proxy( |
| 634 | const dbus::ObjectPath& object_path) { |
| 635 | auto p = itf1_instances_.find(object_path); |
| 636 | if (p != itf1_instances_.end()) |
| 637 | return p->second.get(); |
| 638 | return nullptr; |
| 639 | } |
| 640 | std::vector<org::chromium::Itf1Proxy*> GetItf1Instances() const { |
| 641 | std::vector<org::chromium::Itf1Proxy*> values; |
| 642 | values.reserve(itf1_instances_.size()); |
| 643 | for (const auto& pair : itf1_instances_) |
| 644 | values.push_back(pair.second.get()); |
| 645 | return values; |
| 646 | } |
| 647 | void SetItf1AddedCallback( |
| 648 | const base::Callback<void(org::chromium::Itf1Proxy*)>& callback) { |
| 649 | on_itf1_added_ = callback; |
| 650 | } |
| 651 | void SetItf1RemovedCallback( |
| 652 | const base::Callback<void(const dbus::ObjectPath&)>& callback) { |
| 653 | on_itf1_removed_ = callback; |
| 654 | } |
| 655 | |
| 656 | org::chromium::Itf2Proxy* GetItf2Proxy( |
| 657 | const dbus::ObjectPath& object_path) { |
| 658 | auto p = itf2_instances_.find(object_path); |
| 659 | if (p != itf2_instances_.end()) |
| 660 | return p->second.get(); |
| 661 | return nullptr; |
| 662 | } |
| 663 | std::vector<org::chromium::Itf2Proxy*> GetItf2Instances() const { |
| 664 | std::vector<org::chromium::Itf2Proxy*> values; |
| 665 | values.reserve(itf2_instances_.size()); |
| 666 | for (const auto& pair : itf2_instances_) |
| 667 | values.push_back(pair.second.get()); |
| 668 | return values; |
| 669 | } |
| 670 | void SetItf2AddedCallback( |
| 671 | const base::Callback<void(org::chromium::Itf2Proxy*)>& callback) { |
| 672 | on_itf2_added_ = callback; |
| 673 | } |
| 674 | void SetItf2RemovedCallback( |
| 675 | const base::Callback<void(const dbus::ObjectPath&)>& callback) { |
| 676 | on_itf2_removed_ = callback; |
| 677 | } |
| 678 | |
| 679 | private: |
| 680 | void OnPropertyChanged(const dbus::ObjectPath& object_path, |
| 681 | const std::string& interface_name, |
| 682 | const std::string& property_name) { |
| 683 | if (interface_name == "org.chromium.Itf1") { |
| 684 | auto p = itf1_instances_.find(object_path); |
| 685 | if (p == itf1_instances_.end()) |
| 686 | return; |
| 687 | p->second->OnPropertyChanged(property_name); |
| 688 | return; |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | void ObjectAdded( |
| 693 | const dbus::ObjectPath& object_path, |
| 694 | const std::string& interface_name) override { |
| 695 | if (interface_name == "org.chromium.Itf1") { |
| 696 | auto property_set = |
| 697 | static_cast<org::chromium::Itf1Proxy::PropertySet*>( |
| 698 | dbus_object_manager_->GetProperties(object_path, interface_name)); |
| 699 | std::unique_ptr<org::chromium::Itf1Proxy> itf1_proxy{ |
| 700 | new org::chromium::Itf1Proxy{bus_, property_set} |
| 701 | }; |
| 702 | auto p = itf1_instances_.emplace(object_path, std::move(itf1_proxy)); |
| 703 | if (!on_itf1_added_.is_null()) |
| 704 | on_itf1_added_.Run(p.first->second.get()); |
| 705 | return; |
| 706 | } |
| 707 | if (interface_name == "org.chromium.Itf2") { |
| 708 | std::unique_ptr<org::chromium::Itf2Proxy> itf2_proxy{ |
| 709 | new org::chromium::Itf2Proxy{bus_, object_path} |
| 710 | }; |
| 711 | auto p = itf2_instances_.emplace(object_path, std::move(itf2_proxy)); |
| 712 | if (!on_itf2_added_.is_null()) |
| 713 | on_itf2_added_.Run(p.first->second.get()); |
| 714 | return; |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | void ObjectRemoved( |
| 719 | const dbus::ObjectPath& object_path, |
| 720 | const std::string& interface_name) override { |
| 721 | if (interface_name == "org.chromium.Itf1") { |
| 722 | auto p = itf1_instances_.find(object_path); |
| 723 | if (p != itf1_instances_.end()) { |
| 724 | if (!on_itf1_removed_.is_null()) |
| 725 | on_itf1_removed_.Run(object_path); |
| 726 | itf1_instances_.erase(p); |
| 727 | } |
| 728 | return; |
| 729 | } |
| 730 | if (interface_name == "org.chromium.Itf2") { |
| 731 | auto p = itf2_instances_.find(object_path); |
| 732 | if (p != itf2_instances_.end()) { |
| 733 | if (!on_itf2_removed_.is_null()) |
| 734 | on_itf2_removed_.Run(object_path); |
| 735 | itf2_instances_.erase(p); |
| 736 | } |
| 737 | return; |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | dbus::PropertySet* CreateProperties( |
| 742 | dbus::ObjectProxy* object_proxy, |
| 743 | const dbus::ObjectPath& object_path, |
| 744 | const std::string& interface_name) override { |
| 745 | if (interface_name == "org.chromium.Itf1") { |
| 746 | return new org::chromium::Itf1Proxy::PropertySet{ |
| 747 | object_proxy, |
| 748 | base::Bind(&ObjectManagerProxy::OnPropertyChanged, |
| 749 | weak_ptr_factory_.GetWeakPtr(), |
| 750 | object_path, |
| 751 | interface_name) |
| 752 | }; |
| 753 | } |
| 754 | if (interface_name == "org.chromium.Itf2") { |
| 755 | return new org::chromium::Itf2Proxy::PropertySet{ |
| 756 | object_proxy, |
| 757 | base::Bind(&ObjectManagerProxy::OnPropertyChanged, |
| 758 | weak_ptr_factory_.GetWeakPtr(), |
| 759 | object_path, |
| 760 | interface_name) |
| 761 | }; |
| 762 | } |
| 763 | LOG(FATAL) << "Creating properties for unsupported interface " |
| 764 | << interface_name; |
| 765 | return nullptr; |
| 766 | } |
| 767 | |
| 768 | scoped_refptr<dbus::Bus> bus_; |
| 769 | dbus::ObjectManager* dbus_object_manager_; |
| 770 | std::map<dbus::ObjectPath, |
| 771 | std::unique_ptr<org::chromium::Itf1Proxy>> itf1_instances_; |
| 772 | base::Callback<void(org::chromium::Itf1Proxy*)> on_itf1_added_; |
| 773 | base::Callback<void(const dbus::ObjectPath&)> on_itf1_removed_; |
| 774 | std::map<dbus::ObjectPath, |
| 775 | std::unique_ptr<org::chromium::Itf2Proxy>> itf2_instances_; |
| 776 | base::Callback<void(org::chromium::Itf2Proxy*)> on_itf2_added_; |
| 777 | base::Callback<void(const dbus::ObjectPath&)> on_itf2_removed_; |
| 778 | base::WeakPtrFactory<ObjectManagerProxy> weak_ptr_factory_{this}; |
| 779 | |
| 780 | DISALLOW_COPY_AND_ASSIGN(ObjectManagerProxy); |
| 781 | }; |
| 782 | |
| 783 | } // namespace chromium |
| 784 | } // namespace org |
| 785 | )literal_string"; |
| 786 | |
| 787 | const char kExpectedContentWithObjectManagerAndServiceName[] = R"literal_string( |
| 788 | #include <memory> |
| 789 | #include <string> |
| 790 | #include <vector> |
| 791 | |
| 792 | #include <base/bind.h> |
| 793 | #include <base/callback.h> |
| 794 | #include <base/logging.h> |
| 795 | #include <base/macros.h> |
| 796 | #include <base/memory/ref_counted.h> |
| 797 | #include <chromeos/any.h> |
| 798 | #include <chromeos/dbus/dbus_method_invoker.h> |
| 799 | #include <chromeos/dbus/dbus_property.h> |
| 800 | #include <chromeos/dbus/dbus_signal_handler.h> |
| 801 | #include <chromeos/errors/error.h> |
| 802 | #include <chromeos/variant_dictionary.h> |
| 803 | #include <dbus/bus.h> |
| 804 | #include <dbus/message.h> |
| 805 | #include <dbus/object_manager.h> |
| 806 | #include <dbus/object_path.h> |
| 807 | #include <dbus/object_proxy.h> |
| 808 | |
| 809 | namespace org { |
| 810 | namespace chromium { |
| 811 | class ObjectManagerProxy; |
| 812 | } // namespace chromium |
| 813 | } // namespace org |
| 814 | |
| 815 | namespace org { |
| 816 | namespace chromium { |
| 817 | |
| 818 | // Interface proxy for org::chromium::Itf1. |
| 819 | class Itf1Proxy final { |
| 820 | public: |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame] | 821 | class PropertySet : public dbus::PropertySet { |
| 822 | public: |
| 823 | PropertySet(dbus::ObjectProxy* object_proxy, |
| 824 | const PropertyChangedCallback& callback) |
| 825 | : dbus::PropertySet{object_proxy, |
| 826 | "org.chromium.Itf1", |
| 827 | callback} { |
| 828 | } |
| 829 | |
| 830 | |
| 831 | private: |
| 832 | DISALLOW_COPY_AND_ASSIGN(PropertySet); |
| 833 | }; |
| 834 | |
| 835 | Itf1Proxy(const scoped_refptr<dbus::Bus>& bus) : |
| 836 | bus_{bus}, |
| 837 | dbus_object_proxy_{ |
| 838 | bus_->GetObjectProxy(service_name_, object_path_)} { |
| 839 | } |
| 840 | |
Christopher Wiley | 824b524 | 2014-12-03 11:58:01 -0800 | [diff] [blame] | 841 | ~Itf1Proxy() { |
| 842 | } |
| 843 | |
| 844 | void RegisterCloserSignalHandler( |
| 845 | const base::Closure& signal_callback, |
| 846 | dbus::ObjectProxy::OnConnectedCallback on_connected_callback) { |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame] | 847 | chromeos::dbus_utils::ConnectToSignal( |
| 848 | dbus_object_proxy_, |
| 849 | "org.chromium.Itf1", |
| 850 | "Closer", |
Christopher Wiley | 824b524 | 2014-12-03 11:58:01 -0800 | [diff] [blame] | 851 | signal_callback, |
| 852 | on_connected_callback); |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | void ReleaseObjectProxy(const base::Closure& callback) { |
| 856 | bus_->RemoveObjectProxy(service_name_, object_path_, callback); |
| 857 | } |
| 858 | |
| 859 | const dbus::ObjectPath& GetObjectPath() const { |
| 860 | return object_path_; |
| 861 | } |
| 862 | |
| 863 | dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; } |
| 864 | |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame] | 865 | private: |
| 866 | scoped_refptr<dbus::Bus> bus_; |
| 867 | const std::string service_name_{"org.chromium.Test"}; |
| 868 | const dbus::ObjectPath object_path_{"/org/chromium/Test/Object"}; |
| 869 | dbus::ObjectProxy* dbus_object_proxy_; |
| 870 | |
| 871 | DISALLOW_COPY_AND_ASSIGN(Itf1Proxy); |
| 872 | }; |
| 873 | |
| 874 | } // namespace chromium |
| 875 | } // namespace org |
| 876 | |
| 877 | namespace org { |
| 878 | namespace chromium { |
| 879 | |
| 880 | // Interface proxy for org::chromium::Itf2. |
| 881 | class Itf2Proxy final { |
| 882 | public: |
| 883 | class PropertySet : public dbus::PropertySet { |
| 884 | public: |
| 885 | PropertySet(dbus::ObjectProxy* object_proxy, |
| 886 | const PropertyChangedCallback& callback) |
| 887 | : dbus::PropertySet{object_proxy, |
| 888 | "org.chromium.Itf2", |
| 889 | callback} { |
| 890 | } |
| 891 | |
| 892 | |
| 893 | private: |
| 894 | DISALLOW_COPY_AND_ASSIGN(PropertySet); |
| 895 | }; |
| 896 | |
| 897 | Itf2Proxy( |
| 898 | const scoped_refptr<dbus::Bus>& bus, |
| 899 | const dbus::ObjectPath& object_path) : |
| 900 | bus_{bus}, |
| 901 | object_path_{object_path}, |
| 902 | dbus_object_proxy_{ |
| 903 | bus_->GetObjectProxy(service_name_, object_path_)} { |
| 904 | } |
| 905 | |
| 906 | ~Itf2Proxy() { |
| 907 | } |
| 908 | |
| 909 | void ReleaseObjectProxy(const base::Closure& callback) { |
| 910 | bus_->RemoveObjectProxy(service_name_, object_path_, callback); |
| 911 | } |
| 912 | |
| 913 | const dbus::ObjectPath& GetObjectPath() const { |
| 914 | return object_path_; |
| 915 | } |
| 916 | |
| 917 | dbus::ObjectProxy* GetObjectProxy() const { return dbus_object_proxy_; } |
| 918 | |
| 919 | private: |
| 920 | scoped_refptr<dbus::Bus> bus_; |
| 921 | const std::string service_name_{"org.chromium.Test"}; |
| 922 | dbus::ObjectPath object_path_; |
| 923 | dbus::ObjectProxy* dbus_object_proxy_; |
| 924 | |
| 925 | DISALLOW_COPY_AND_ASSIGN(Itf2Proxy); |
| 926 | }; |
| 927 | |
| 928 | } // namespace chromium |
| 929 | } // namespace org |
| 930 | |
| 931 | namespace org { |
| 932 | namespace chromium { |
| 933 | |
| 934 | class ObjectManagerProxy : public dbus::ObjectManager::Interface { |
| 935 | public: |
| 936 | ObjectManagerProxy(const scoped_refptr<dbus::Bus>& bus) |
| 937 | : bus_{bus}, |
| 938 | dbus_object_manager_{bus->GetObjectManager( |
| 939 | "org.chromium.Test", |
| 940 | dbus::ObjectPath{"/org/chromium/Test"})} { |
| 941 | dbus_object_manager_->RegisterInterface("org.chromium.Itf1", this); |
| 942 | dbus_object_manager_->RegisterInterface("org.chromium.Itf2", this); |
| 943 | } |
| 944 | |
Christopher Wiley | e11e81c | 2014-12-19 13:10:52 -0800 | [diff] [blame^] | 945 | ~ObjectManagerProxy() override { |
| 946 | dbus_object_manager_->UnregisterInterface("org.chromium.Itf1"); |
| 947 | dbus_object_manager_->UnregisterInterface("org.chromium.Itf2"); |
| 948 | } |
| 949 | |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame] | 950 | dbus::ObjectManager* GetObjectManagerProxy() const { |
| 951 | return dbus_object_manager_; |
| 952 | } |
| 953 | |
| 954 | org::chromium::Itf1Proxy* GetItf1Proxy( |
| 955 | const dbus::ObjectPath& object_path) { |
| 956 | auto p = itf1_instances_.find(object_path); |
| 957 | if (p != itf1_instances_.end()) |
| 958 | return p->second.get(); |
| 959 | return nullptr; |
| 960 | } |
| 961 | std::vector<org::chromium::Itf1Proxy*> GetItf1Instances() const { |
| 962 | std::vector<org::chromium::Itf1Proxy*> values; |
| 963 | values.reserve(itf1_instances_.size()); |
| 964 | for (const auto& pair : itf1_instances_) |
| 965 | values.push_back(pair.second.get()); |
| 966 | return values; |
| 967 | } |
| 968 | void SetItf1AddedCallback( |
| 969 | const base::Callback<void(org::chromium::Itf1Proxy*)>& callback) { |
| 970 | on_itf1_added_ = callback; |
| 971 | } |
| 972 | void SetItf1RemovedCallback( |
| 973 | const base::Callback<void(const dbus::ObjectPath&)>& callback) { |
| 974 | on_itf1_removed_ = callback; |
| 975 | } |
| 976 | |
| 977 | org::chromium::Itf2Proxy* GetItf2Proxy( |
| 978 | const dbus::ObjectPath& object_path) { |
| 979 | auto p = itf2_instances_.find(object_path); |
| 980 | if (p != itf2_instances_.end()) |
| 981 | return p->second.get(); |
| 982 | return nullptr; |
| 983 | } |
| 984 | std::vector<org::chromium::Itf2Proxy*> GetItf2Instances() const { |
| 985 | std::vector<org::chromium::Itf2Proxy*> values; |
| 986 | values.reserve(itf2_instances_.size()); |
| 987 | for (const auto& pair : itf2_instances_) |
| 988 | values.push_back(pair.second.get()); |
| 989 | return values; |
| 990 | } |
| 991 | void SetItf2AddedCallback( |
| 992 | const base::Callback<void(org::chromium::Itf2Proxy*)>& callback) { |
| 993 | on_itf2_added_ = callback; |
| 994 | } |
| 995 | void SetItf2RemovedCallback( |
| 996 | const base::Callback<void(const dbus::ObjectPath&)>& callback) { |
| 997 | on_itf2_removed_ = callback; |
| 998 | } |
| 999 | |
| 1000 | private: |
| 1001 | void OnPropertyChanged(const dbus::ObjectPath& object_path, |
| 1002 | const std::string& interface_name, |
| 1003 | const std::string& property_name) { |
| 1004 | } |
| 1005 | |
| 1006 | void ObjectAdded( |
| 1007 | const dbus::ObjectPath& object_path, |
| 1008 | const std::string& interface_name) override { |
| 1009 | if (interface_name == "org.chromium.Itf1") { |
| 1010 | std::unique_ptr<org::chromium::Itf1Proxy> itf1_proxy{ |
| 1011 | new org::chromium::Itf1Proxy{bus_} |
| 1012 | }; |
| 1013 | auto p = itf1_instances_.emplace(object_path, std::move(itf1_proxy)); |
| 1014 | if (!on_itf1_added_.is_null()) |
| 1015 | on_itf1_added_.Run(p.first->second.get()); |
| 1016 | return; |
| 1017 | } |
| 1018 | if (interface_name == "org.chromium.Itf2") { |
| 1019 | std::unique_ptr<org::chromium::Itf2Proxy> itf2_proxy{ |
| 1020 | new org::chromium::Itf2Proxy{bus_, object_path} |
| 1021 | }; |
| 1022 | auto p = itf2_instances_.emplace(object_path, std::move(itf2_proxy)); |
| 1023 | if (!on_itf2_added_.is_null()) |
| 1024 | on_itf2_added_.Run(p.first->second.get()); |
| 1025 | return; |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | void ObjectRemoved( |
| 1030 | const dbus::ObjectPath& object_path, |
| 1031 | const std::string& interface_name) override { |
| 1032 | if (interface_name == "org.chromium.Itf1") { |
| 1033 | auto p = itf1_instances_.find(object_path); |
| 1034 | if (p != itf1_instances_.end()) { |
| 1035 | if (!on_itf1_removed_.is_null()) |
| 1036 | on_itf1_removed_.Run(object_path); |
| 1037 | itf1_instances_.erase(p); |
| 1038 | } |
| 1039 | return; |
| 1040 | } |
| 1041 | if (interface_name == "org.chromium.Itf2") { |
| 1042 | auto p = itf2_instances_.find(object_path); |
| 1043 | if (p != itf2_instances_.end()) { |
| 1044 | if (!on_itf2_removed_.is_null()) |
| 1045 | on_itf2_removed_.Run(object_path); |
| 1046 | itf2_instances_.erase(p); |
| 1047 | } |
| 1048 | return; |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | dbus::PropertySet* CreateProperties( |
| 1053 | dbus::ObjectProxy* object_proxy, |
| 1054 | const dbus::ObjectPath& object_path, |
| 1055 | const std::string& interface_name) override { |
| 1056 | if (interface_name == "org.chromium.Itf1") { |
| 1057 | return new org::chromium::Itf1Proxy::PropertySet{ |
| 1058 | object_proxy, |
| 1059 | base::Bind(&ObjectManagerProxy::OnPropertyChanged, |
| 1060 | weak_ptr_factory_.GetWeakPtr(), |
| 1061 | object_path, |
| 1062 | interface_name) |
| 1063 | }; |
| 1064 | } |
| 1065 | if (interface_name == "org.chromium.Itf2") { |
| 1066 | return new org::chromium::Itf2Proxy::PropertySet{ |
| 1067 | object_proxy, |
| 1068 | base::Bind(&ObjectManagerProxy::OnPropertyChanged, |
| 1069 | weak_ptr_factory_.GetWeakPtr(), |
| 1070 | object_path, |
| 1071 | interface_name) |
| 1072 | }; |
| 1073 | } |
| 1074 | LOG(FATAL) << "Creating properties for unsupported interface " |
| 1075 | << interface_name; |
| 1076 | return nullptr; |
| 1077 | } |
| 1078 | |
| 1079 | scoped_refptr<dbus::Bus> bus_; |
| 1080 | dbus::ObjectManager* dbus_object_manager_; |
| 1081 | std::map<dbus::ObjectPath, |
| 1082 | std::unique_ptr<org::chromium::Itf1Proxy>> itf1_instances_; |
| 1083 | base::Callback<void(org::chromium::Itf1Proxy*)> on_itf1_added_; |
| 1084 | base::Callback<void(const dbus::ObjectPath&)> on_itf1_removed_; |
| 1085 | std::map<dbus::ObjectPath, |
| 1086 | std::unique_ptr<org::chromium::Itf2Proxy>> itf2_instances_; |
| 1087 | base::Callback<void(org::chromium::Itf2Proxy*)> on_itf2_added_; |
| 1088 | base::Callback<void(const dbus::ObjectPath&)> on_itf2_removed_; |
| 1089 | base::WeakPtrFactory<ObjectManagerProxy> weak_ptr_factory_{this}; |
| 1090 | |
| 1091 | DISALLOW_COPY_AND_ASSIGN(ObjectManagerProxy); |
| 1092 | }; |
| 1093 | |
| 1094 | } // namespace chromium |
| 1095 | } // namespace org |
| 1096 | )literal_string"; |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 1097 | } // namespace |
| 1098 | |
| 1099 | class ProxyGeneratorTest : public Test { |
| 1100 | public: |
| 1101 | void SetUp() override { |
| 1102 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 1103 | } |
| 1104 | |
| 1105 | protected: |
| 1106 | base::FilePath CreateInputFile(const string& contents) { |
| 1107 | base::FilePath path; |
| 1108 | EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &path)); |
| 1109 | EXPECT_EQ(contents.size(), |
| 1110 | base::WriteFile(path, contents.c_str(), contents.size())); |
| 1111 | return path; |
| 1112 | } |
| 1113 | |
| 1114 | base::ScopedTempDir temp_dir_; |
| 1115 | }; |
| 1116 | |
| 1117 | TEST_F(ProxyGeneratorTest, GenerateAdaptors) { |
| 1118 | Interface interface; |
| 1119 | interface.name = kInterfaceName; |
Alex Vakulenko | 6ef2d73 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 1120 | interface.path = "/org/chromium/Test"; |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 1121 | interface.methods.emplace_back( |
| 1122 | kMethod1Name, |
| 1123 | vector<Interface::Argument>{ |
| 1124 | {kMethod1ArgumentName1, kMethod1Argument1}, |
| 1125 | {kMethod1ArgumentName2, kMethod1Argument2}}, |
| 1126 | vector<Interface::Argument>{{"", kMethod1Return}}); |
| 1127 | interface.methods.emplace_back( |
| 1128 | kMethod2Name, |
| 1129 | vector<Interface::Argument>{}, |
| 1130 | vector<Interface::Argument>{{"", kMethod2Return}}); |
| 1131 | interface.methods.emplace_back( |
| 1132 | kMethod3Name, |
| 1133 | vector<Interface::Argument>{{"", kMethod3Argument1}}, |
| 1134 | vector<Interface::Argument>{}); |
| 1135 | interface.methods.emplace_back(kMethod4Name); |
| 1136 | interface.signals.emplace_back(kSignal1Name); |
| 1137 | interface.signals.emplace_back( |
| 1138 | kSignal2Name, |
| 1139 | vector<Interface::Argument>{ |
| 1140 | {"", kSignal2Argument1}, |
| 1141 | {"", kSignal2Argument2}}); |
Alex Vakulenko | 6ef2d73 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 1142 | interface.methods.back().doc_string = "Comment line1\nline2"; |
Alex Vakulenko | fafef13 | 2014-11-03 14:52:09 -0800 | [diff] [blame] | 1143 | Interface interface2; |
| 1144 | interface2.name = kInterfaceName2; |
| 1145 | interface2.methods.emplace_back( |
| 1146 | kMethod5Name, |
| 1147 | vector<Interface::Argument>{}, |
| 1148 | vector<Interface::Argument>{ |
| 1149 | {kMethod5ArgumentName1, kMethod5Argument1}, |
| 1150 | {kMethod5ArgumentName2, kMethod5Argument2}}); |
| 1151 | vector<Interface> interfaces{interface, interface2}; |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 1152 | base::FilePath output_path = temp_dir_.path().Append("output.h"); |
Alex Vakulenko | 6ef2d73 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 1153 | ServiceConfig config; |
| 1154 | EXPECT_TRUE(ProxyGenerator::GenerateProxies(config, interfaces, output_path)); |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 1155 | string contents; |
| 1156 | EXPECT_TRUE(base::ReadFileToString(output_path, &contents)); |
| 1157 | // The header guards contain the (temporary) filename, so we search for |
| 1158 | // the content we need within the string. |
| 1159 | EXPECT_NE(string::npos, contents.find(kExpectedContent)) |
| 1160 | << "Expected to find the following content...\n" |
| 1161 | << kExpectedContent << "...within content...\n" << contents; |
| 1162 | } |
| 1163 | |
Alex Vakulenko | 6ef2d73 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 1164 | TEST_F(ProxyGeneratorTest, GenerateAdaptorsWithServiceName) { |
| 1165 | Interface interface; |
| 1166 | interface.name = kInterfaceName; |
| 1167 | interface.path = "/org/chromium/Test"; |
| 1168 | interface.signals.emplace_back(kSignal1Name); |
| 1169 | Interface interface2; |
| 1170 | interface2.name = kInterfaceName2; |
| 1171 | vector<Interface> interfaces{interface, interface2}; |
| 1172 | base::FilePath output_path = temp_dir_.path().Append("output2.h"); |
| 1173 | ServiceConfig config; |
| 1174 | config.service_name = "org.chromium.Test"; |
| 1175 | EXPECT_TRUE(ProxyGenerator::GenerateProxies(config, interfaces, output_path)); |
| 1176 | string contents; |
| 1177 | EXPECT_TRUE(base::ReadFileToString(output_path, &contents)); |
| 1178 | // The header guards contain the (temporary) filename, so we search for |
| 1179 | // the content we need within the string. |
| 1180 | EXPECT_NE(string::npos, contents.find(kExpectedContentWithService)) |
| 1181 | << "Expected to find the following content...\n" |
| 1182 | << kExpectedContentWithService << "...within content...\n" << contents; |
| 1183 | } |
| 1184 | |
Alex Vakulenko | 99e8fb0 | 2014-12-01 17:22:03 -0800 | [diff] [blame] | 1185 | TEST_F(ProxyGeneratorTest, GenerateAdaptorsWithObjectManager) { |
| 1186 | Interface interface; |
| 1187 | interface.name = "org.chromium.Itf1"; |
| 1188 | interface.path = "/org/chromium/Test/Object"; |
| 1189 | interface.signals.emplace_back(kSignal1Name); |
| 1190 | interface.properties.emplace_back("data", "s", "read"); |
| 1191 | Interface interface2; |
| 1192 | interface2.name = "org.chromium.Itf2"; |
| 1193 | vector<Interface> interfaces{interface, interface2}; |
| 1194 | base::FilePath output_path = temp_dir_.path().Append("output3.h"); |
| 1195 | ServiceConfig config; |
| 1196 | config.object_manager.name = "org.chromium.ObjectManager"; |
| 1197 | config.object_manager.object_path = "/org/chromium/Test"; |
| 1198 | EXPECT_TRUE(ProxyGenerator::GenerateProxies(config, interfaces, output_path)); |
| 1199 | string contents; |
| 1200 | EXPECT_TRUE(base::ReadFileToString(output_path, &contents)); |
| 1201 | // The header guards contain the (temporary) filename, so we search for |
| 1202 | // the content we need within the string. |
| 1203 | EXPECT_NE(string::npos, contents.find(kExpectedContentWithObjectManager)) |
| 1204 | << "Expected to find the following content...\n" |
| 1205 | << kExpectedContentWithObjectManager << "...within content...\n" |
| 1206 | << contents; |
| 1207 | } |
| 1208 | |
| 1209 | TEST_F(ProxyGeneratorTest, GenerateAdaptorsWithObjectManagerAndServiceName) { |
| 1210 | Interface interface; |
| 1211 | interface.name = "org.chromium.Itf1"; |
| 1212 | interface.path = "/org/chromium/Test/Object"; |
| 1213 | interface.signals.emplace_back(kSignal1Name); |
| 1214 | Interface interface2; |
| 1215 | interface2.name = "org.chromium.Itf2"; |
| 1216 | vector<Interface> interfaces{interface, interface2}; |
| 1217 | base::FilePath output_path = temp_dir_.path().Append("output4.h"); |
| 1218 | ServiceConfig config; |
| 1219 | config.service_name = "org.chromium.Test"; |
| 1220 | config.object_manager.name = "org.chromium.ObjectManager"; |
| 1221 | config.object_manager.object_path = "/org/chromium/Test"; |
| 1222 | EXPECT_TRUE(ProxyGenerator::GenerateProxies(config, interfaces, output_path)); |
| 1223 | string contents; |
| 1224 | EXPECT_TRUE(base::ReadFileToString(output_path, &contents)); |
| 1225 | // The header guards contain the (temporary) filename, so we search for |
| 1226 | // the content we need within the string. |
| 1227 | EXPECT_NE(string::npos, |
| 1228 | contents.find(kExpectedContentWithObjectManagerAndServiceName)) |
| 1229 | << "Expected to find the following content...\n" |
| 1230 | << kExpectedContentWithObjectManagerAndServiceName |
| 1231 | << "...within content...\n" << contents; |
| 1232 | } |
| 1233 | |
Paul Stewart | 1dce1ae | 2014-10-01 05:30:18 -0700 | [diff] [blame] | 1234 | } // namespace chromeos_dbus_bindings |