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 /* success */) { |
| 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))); |
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) { |
114 bus_->RequestOwnership("org.chromium.TestService", | 124 bus_->RequestOwnership("org.chromium.TestService", |
115 base::Bind(&TestService::OnOwnership, | 125 base::Bind(&TestService::OnOwnership, |
116 base::Unretained(this))); | 126 base::Unretained(this), |
| 127 callback)); |
117 } | 128 } |
118 | 129 |
119 void TestService::OnOwnership(const std::string& service_name, | 130 void TestService::OnOwnership(base::Callback<void(bool)> callback, |
| 131 const std::string& service_name, |
120 bool success) { | 132 bool success) { |
| 133 has_ownership_ = success; |
121 LOG_IF(ERROR, !success) << "Failed to own: " << service_name; | 134 LOG_IF(ERROR, !success) << "Failed to own: " << service_name; |
| 135 callback.Run(success); |
122 } | 136 } |
123 | 137 |
124 void TestService::OnExported(const std::string& interface_name, | 138 void TestService::OnExported(const std::string& interface_name, |
125 const std::string& method_name, | 139 const std::string& method_name, |
126 bool success) { | 140 bool success) { |
127 if (!success) { | 141 if (!success) { |
128 LOG(ERROR) << "Failed to export: " << interface_name << "." | 142 LOG(ERROR) << "Failed to export: " << interface_name << "." |
129 << method_name; | 143 << method_name; |
130 // Returning here will make WaitUntilServiceIsStarted() to time out | 144 // Returning here will make WaitUntilServiceIsStarted() to time out |
131 // and return false. | 145 // and return false. |
132 return; | 146 return; |
133 } | 147 } |
134 | 148 |
135 ++num_exported_methods_; | 149 ++num_exported_methods_; |
136 if (num_exported_methods_ == kNumMethodsToExport) | 150 if (num_exported_methods_ == kNumMethodsToExport) |
137 on_all_methods_exported_.Signal(); | 151 on_all_methods_exported_.Signal(); |
138 } | 152 } |
139 | 153 |
140 void TestService::Run(MessageLoop* message_loop) { | 154 void TestService::Run(MessageLoop* message_loop) { |
141 Bus::Options bus_options; | 155 Bus::Options bus_options; |
142 bus_options.bus_type = Bus::SESSION; | 156 bus_options.bus_type = Bus::SESSION; |
143 bus_options.connection_type = Bus::PRIVATE; | 157 bus_options.connection_type = Bus::PRIVATE; |
144 bus_options.dbus_thread_message_loop_proxy = dbus_thread_message_loop_proxy_; | 158 bus_options.dbus_thread_message_loop_proxy = dbus_thread_message_loop_proxy_; |
145 bus_ = new Bus(bus_options); | 159 bus_ = new Bus(bus_options); |
146 | 160 |
147 bus_->RequestOwnership("org.chromium.TestService", | 161 bus_->RequestOwnership("org.chromium.TestService", |
148 base::Bind(&TestService::OnOwnership, | 162 base::Bind(&TestService::OnOwnership, |
149 base::Unretained(this))); | 163 base::Unretained(this), |
| 164 base::Bind(&EmptyCallback))); |
150 | 165 |
151 exported_object_ = bus_->GetExportedObject( | 166 exported_object_ = bus_->GetExportedObject( |
152 dbus::ObjectPath("/org/chromium/TestObject")); | 167 dbus::ObjectPath("/org/chromium/TestObject")); |
153 | 168 |
154 int num_methods = 0; | 169 int num_methods = 0; |
155 exported_object_->ExportMethod( | 170 exported_object_->ExportMethod( |
156 "org.chromium.TestInterface", | 171 "org.chromium.TestInterface", |
157 "Echo", | 172 "Echo", |
158 base::Bind(&TestService::Echo, | 173 base::Bind(&TestService::Echo, |
159 base::Unretained(this)), | 174 base::Unretained(this)), |
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
458 array_writer.OpenDictEntry(&dict_entry_writer); | 473 array_writer.OpenDictEntry(&dict_entry_writer); |
459 dict_entry_writer.AppendString("Name"); | 474 dict_entry_writer.AppendString("Name"); |
460 dict_entry_writer.AppendVariantOfString(name); | 475 dict_entry_writer.AppendVariantOfString(name); |
461 array_writer.CloseContainer(&dict_entry_writer); | 476 array_writer.CloseContainer(&dict_entry_writer); |
462 writer.CloseContainer(&array_writer); | 477 writer.CloseContainer(&array_writer); |
463 | 478 |
464 exported_object_->SendSignal(&signal); | 479 exported_object_->SendSignal(&signal); |
465 } | 480 } |
466 | 481 |
467 } // namespace dbus | 482 } // namespace dbus |
OLD | NEW |