Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "dbus/test_service.h" | 5 #include "dbus/test_service.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/test/test_timeouts.h" | 8 #include "base/test/test_timeouts.h" |
| 9 #include "base/threading/platform_thread.h" | 9 #include "base/threading/platform_thread.h" |
| 10 #include "dbus/bus.h" | 10 #include "dbus/bus.h" |
| 11 #include "dbus/exported_object.h" | 11 #include "dbus/exported_object.h" |
| 12 #include "dbus/message.h" | 12 #include "dbus/message.h" |
| 13 #include "dbus/object_path.h" | 13 #include "dbus/object_path.h" |
| 14 #include "dbus/property.h" | 14 #include "dbus/property.h" |
| 15 | 15 |
| 16 namespace { | |
| 17 | |
| 18 void EmptyCallback(bool b) { | |
|
satorux1
2012/11/07 01:42:25
b -> /* success */
Haruki Sato
2012/11/12 04:59:43
Done. Thanks.
| |
| 19 } | |
| 20 | |
| 21 } // namespace | |
| 22 | |
| 16 namespace dbus { | 23 namespace dbus { |
| 17 | 24 |
| 18 // Echo, SlowEcho, AsyncEcho, BrokenMethod, GetAll, Get, Set. | 25 // Echo, SlowEcho, AsyncEcho, BrokenMethod, GetAll, Get, Set. |
| 19 const int TestService::kNumMethodsToExport = 7; | 26 const int TestService::kNumMethodsToExport = 7; |
| 20 | 27 |
| 21 TestService::Options::Options() { | 28 TestService::Options::Options() { |
| 22 } | 29 } |
| 23 | 30 |
| 24 TestService::Options::~Options() { | 31 TestService::Options::~Options() { |
| 25 } | 32 } |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 88 exported_object_->SendSignal(&signal); | 95 exported_object_->SendSignal(&signal); |
| 89 } | 96 } |
| 90 | 97 |
| 91 void TestService::SendTestSignalFromRootInternal(const std::string& message) { | 98 void TestService::SendTestSignalFromRootInternal(const std::string& message) { |
| 92 dbus::Signal signal("org.chromium.TestInterface", "Test"); | 99 dbus::Signal signal("org.chromium.TestInterface", "Test"); |
| 93 dbus::MessageWriter writer(&signal); | 100 dbus::MessageWriter writer(&signal); |
| 94 writer.AppendString(message); | 101 writer.AppendString(message); |
| 95 | 102 |
| 96 bus_->RequestOwnership("org.chromium.TestService", | 103 bus_->RequestOwnership("org.chromium.TestService", |
| 97 base::Bind(&TestService::OnOwnership, | 104 base::Bind(&TestService::OnOwnership, |
| 98 base::Unretained(this))); | 105 base::Unretained(this), |
| 106 base::Bind(EmptyCallback))); | |
|
satorux1
2012/11/07 01:42:25
base::Bind(&EmptyCallback)
Haruki Sato
2012/11/12 04:59:43
Done. Thanks.
| |
| 99 | 107 |
| 100 // Use "/" just like dbus-send does. | 108 // Use "/" just like dbus-send does. |
| 101 ExportedObject* root_object = | 109 ExportedObject* root_object = |
| 102 bus_->GetExportedObject(dbus::ObjectPath("/")); | 110 bus_->GetExportedObject(dbus::ObjectPath("/")); |
| 103 root_object->SendSignal(&signal); | 111 root_object->SendSignal(&signal); |
| 104 } | 112 } |
| 105 | 113 |
| 106 void TestService::RequestOwnership() { | 114 void TestService::RequestOwnership(base::Callback<void(bool)> callback) { |
| 107 message_loop()->PostTask( | 115 message_loop()->PostTask( |
| 108 FROM_HERE, | 116 FROM_HERE, |
| 109 base::Bind(&TestService::RequestOwnershipInternal, | 117 base::Bind(&TestService::RequestOwnershipInternal, |
| 110 base::Unretained(this))); | 118 base::Unretained(this), |
| 119 callback)); | |
| 111 } | 120 } |
| 112 | 121 |
| 113 void TestService::RequestOwnershipInternal() { | 122 void TestService::RequestOwnershipInternal( |
| 123 base::Callback<void(bool)> callback) { | |
| 124 requesting_ownership = true; | |
| 114 bus_->RequestOwnership("org.chromium.TestService", | 125 bus_->RequestOwnership("org.chromium.TestService", |
| 115 base::Bind(&TestService::OnOwnership, | 126 base::Bind(&TestService::OnOwnership, |
| 116 base::Unretained(this))); | 127 base::Unretained(this), |
| 128 callback)); | |
| 117 } | 129 } |
| 118 | 130 |
| 119 void TestService::OnOwnership(const std::string& service_name, | 131 void TestService::OnOwnership(base::Callback<void(bool)> callback, |
| 132 const std::string& service_name, | |
| 120 bool success) { | 133 bool success) { |
| 134 has_name_ownership = success; | |
| 121 LOG_IF(ERROR, !success) << "Failed to own: " << service_name; | 135 LOG_IF(ERROR, !success) << "Failed to own: " << service_name; |
| 136 callback.Run(success); | |
| 137 } | |
| 138 | |
| 139 bool TestService::HasOwnership() { | |
| 140 return has_name_ownership; | |
| 122 } | 141 } |
| 123 | 142 |
| 124 void TestService::OnExported(const std::string& interface_name, | 143 void TestService::OnExported(const std::string& interface_name, |
| 125 const std::string& method_name, | 144 const std::string& method_name, |
| 126 bool success) { | 145 bool success) { |
| 127 if (!success) { | 146 if (!success) { |
| 128 LOG(ERROR) << "Failed to export: " << interface_name << "." | 147 LOG(ERROR) << "Failed to export: " << interface_name << "." |
| 129 << method_name; | 148 << method_name; |
| 130 // Returning here will make WaitUntilServiceIsStarted() to time out | 149 // Returning here will make WaitUntilServiceIsStarted() to time out |
| 131 // and return false. | 150 // and return false. |
| 132 return; | 151 return; |
| 133 } | 152 } |
| 134 | 153 |
| 135 ++num_exported_methods_; | 154 ++num_exported_methods_; |
| 136 if (num_exported_methods_ == kNumMethodsToExport) | 155 if (num_exported_methods_ == kNumMethodsToExport) |
| 137 on_all_methods_exported_.Signal(); | 156 on_all_methods_exported_.Signal(); |
| 138 } | 157 } |
| 139 | 158 |
| 140 void TestService::Run(MessageLoop* message_loop) { | 159 void TestService::Run(MessageLoop* message_loop) { |
| 141 Bus::Options bus_options; | 160 Bus::Options bus_options; |
| 142 bus_options.bus_type = Bus::SESSION; | 161 bus_options.bus_type = Bus::SESSION; |
| 143 bus_options.connection_type = Bus::PRIVATE; | 162 bus_options.connection_type = Bus::PRIVATE; |
| 144 bus_options.dbus_thread_message_loop_proxy = dbus_thread_message_loop_proxy_; | 163 bus_options.dbus_thread_message_loop_proxy = dbus_thread_message_loop_proxy_; |
| 145 bus_ = new Bus(bus_options); | 164 bus_ = new Bus(bus_options); |
| 146 | 165 |
| 147 bus_->RequestOwnership("org.chromium.TestService", | 166 bus_->RequestOwnership("org.chromium.TestService", |
| 148 base::Bind(&TestService::OnOwnership, | 167 base::Bind(&TestService::OnOwnership, |
| 149 base::Unretained(this))); | 168 base::Unretained(this), |
| 169 base::Bind(EmptyCallback))); | |
| 150 | 170 |
| 151 exported_object_ = bus_->GetExportedObject( | 171 exported_object_ = bus_->GetExportedObject( |
| 152 dbus::ObjectPath("/org/chromium/TestObject")); | 172 dbus::ObjectPath("/org/chromium/TestObject")); |
| 153 | 173 |
| 154 int num_methods = 0; | 174 int num_methods = 0; |
| 155 exported_object_->ExportMethod( | 175 exported_object_->ExportMethod( |
| 156 "org.chromium.TestInterface", | 176 "org.chromium.TestInterface", |
| 157 "Echo", | 177 "Echo", |
| 158 base::Bind(&TestService::Echo, | 178 base::Bind(&TestService::Echo, |
| 159 base::Unretained(this)), | 179 base::Unretained(this)), |
| (...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 458 array_writer.OpenDictEntry(&dict_entry_writer); | 478 array_writer.OpenDictEntry(&dict_entry_writer); |
| 459 dict_entry_writer.AppendString("Name"); | 479 dict_entry_writer.AppendString("Name"); |
| 460 dict_entry_writer.AppendVariantOfString(name); | 480 dict_entry_writer.AppendVariantOfString(name); |
| 461 array_writer.CloseContainer(&dict_entry_writer); | 481 array_writer.CloseContainer(&dict_entry_writer); |
| 462 writer.CloseContainer(&array_writer); | 482 writer.CloseContainer(&array_writer); |
| 463 | 483 |
| 464 exported_object_->SendSignal(&signal); | 484 exported_object_->SendSignal(&signal); |
| 465 } | 485 } |
| 466 | 486 |
| 467 } // namespace dbus | 487 } // namespace dbus |
| OLD | NEW |