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 <stdint.h> | 7 #include <stdint.h> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/guid.h" |
13 #include "base/test/test_timeouts.h" | 14 #include "base/test/test_timeouts.h" |
14 #include "base/threading/platform_thread.h" | 15 #include "base/threading/platform_thread.h" |
15 #include "dbus/bus.h" | 16 #include "dbus/bus.h" |
16 #include "dbus/exported_object.h" | 17 #include "dbus/exported_object.h" |
17 #include "dbus/message.h" | 18 #include "dbus/message.h" |
18 #include "dbus/object_manager.h" | 19 #include "dbus/object_manager.h" |
19 #include "dbus/object_path.h" | 20 #include "dbus/object_path.h" |
20 #include "dbus/property.h" | 21 #include "dbus/property.h" |
21 | 22 |
22 namespace { | 23 namespace { |
(...skipping 11 matching lines...) Expand all Loading... |
34 | 35 |
35 TestService::Options::Options() | 36 TestService::Options::Options() |
36 : request_ownership_options(Bus::REQUIRE_PRIMARY) { | 37 : request_ownership_options(Bus::REQUIRE_PRIMARY) { |
37 } | 38 } |
38 | 39 |
39 TestService::Options::~Options() { | 40 TestService::Options::~Options() { |
40 } | 41 } |
41 | 42 |
42 TestService::TestService(const Options& options) | 43 TestService::TestService(const Options& options) |
43 : base::Thread("TestService"), | 44 : base::Thread("TestService"), |
| 45 service_name_(options.service_name), |
44 request_ownership_options_(options.request_ownership_options), | 46 request_ownership_options_(options.request_ownership_options), |
45 dbus_task_runner_(options.dbus_task_runner), | 47 dbus_task_runner_(options.dbus_task_runner), |
46 on_name_obtained_(false, false), | 48 on_name_obtained_(false, false), |
47 num_exported_methods_(0), | 49 num_exported_methods_(0), |
48 send_immediate_properties_changed_(false), | 50 send_immediate_properties_changed_(false), |
49 has_ownership_(false), | 51 has_ownership_(false), |
50 exported_object_(NULL), | 52 exported_object_(NULL), |
51 exported_object_manager_(NULL) { | 53 exported_object_manager_(NULL) { |
| 54 if (service_name_.empty()) { |
| 55 service_name_ = "org.chromium.TestService-" + base::GenerateGUID(); |
| 56 } |
52 } | 57 } |
53 | 58 |
54 TestService::~TestService() { | 59 TestService::~TestService() { |
55 Stop(); | 60 Stop(); |
56 } | 61 } |
57 | 62 |
58 bool TestService::StartService() { | 63 bool TestService::StartService() { |
59 base::Thread::Options thread_options; | 64 base::Thread::Options thread_options; |
60 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; | 65 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
61 return StartWithOptions(thread_options); | 66 return StartWithOptions(thread_options); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 MessageWriter writer(&signal); | 111 MessageWriter writer(&signal); |
107 writer.AppendString(message); | 112 writer.AppendString(message); |
108 exported_object_->SendSignal(&signal); | 113 exported_object_->SendSignal(&signal); |
109 } | 114 } |
110 | 115 |
111 void TestService::SendTestSignalFromRootInternal(const std::string& message) { | 116 void TestService::SendTestSignalFromRootInternal(const std::string& message) { |
112 Signal signal("org.chromium.TestInterface", "Test"); | 117 Signal signal("org.chromium.TestInterface", "Test"); |
113 MessageWriter writer(&signal); | 118 MessageWriter writer(&signal); |
114 writer.AppendString(message); | 119 writer.AppendString(message); |
115 | 120 |
116 bus_->RequestOwnership("org.chromium.TestService", | 121 bus_->RequestOwnership(service_name_, |
117 request_ownership_options_, | 122 request_ownership_options_, |
118 base::Bind(&TestService::OnOwnership, | 123 base::Bind(&TestService::OnOwnership, |
119 base::Unretained(this), | 124 base::Unretained(this), |
120 base::Bind(&EmptyCallback))); | 125 base::Bind(&EmptyCallback))); |
121 | 126 |
122 // Use "/" just like dbus-send does. | 127 // Use "/" just like dbus-send does. |
123 ExportedObject* root_object = bus_->GetExportedObject(ObjectPath("/")); | 128 ExportedObject* root_object = bus_->GetExportedObject(ObjectPath("/")); |
124 root_object->SendSignal(&signal); | 129 root_object->SendSignal(&signal); |
125 } | 130 } |
126 | 131 |
127 void TestService::RequestOwnership(base::Callback<void(bool)> callback) { | 132 void TestService::RequestOwnership(base::Callback<void(bool)> callback) { |
128 message_loop()->PostTask( | 133 message_loop()->PostTask( |
129 FROM_HERE, | 134 FROM_HERE, |
130 base::Bind(&TestService::RequestOwnershipInternal, | 135 base::Bind(&TestService::RequestOwnershipInternal, |
131 base::Unretained(this), | 136 base::Unretained(this), |
132 callback)); | 137 callback)); |
133 } | 138 } |
134 | 139 |
135 void TestService::RequestOwnershipInternal( | 140 void TestService::RequestOwnershipInternal( |
136 base::Callback<void(bool)> callback) { | 141 base::Callback<void(bool)> callback) { |
137 bus_->RequestOwnership("org.chromium.TestService", | 142 bus_->RequestOwnership(service_name_, |
138 request_ownership_options_, | 143 request_ownership_options_, |
139 base::Bind(&TestService::OnOwnership, | 144 base::Bind(&TestService::OnOwnership, |
140 base::Unretained(this), | 145 base::Unretained(this), |
141 callback)); | 146 callback)); |
142 } | 147 } |
143 | 148 |
144 void TestService::OnOwnership(base::Callback<void(bool)> callback, | 149 void TestService::OnOwnership(base::Callback<void(bool)> callback, |
145 const std::string& service_name, | 150 const std::string& service_name, |
146 bool success) { | 151 bool success) { |
147 has_ownership_ = success; | 152 has_ownership_ = success; |
148 LOG_IF(ERROR, !success) << "Failed to own: " << service_name; | 153 LOG_IF(ERROR, !success) << "Failed to own: " << service_name; |
149 callback.Run(success); | 154 callback.Run(success); |
150 | 155 |
151 on_name_obtained_.Signal(); | 156 on_name_obtained_.Signal(); |
152 } | 157 } |
153 | 158 |
154 void TestService::ReleaseOwnership(base::Closure callback) { | 159 void TestService::ReleaseOwnership(base::Closure callback) { |
155 bus_->GetDBusTaskRunner()->PostTask( | 160 bus_->GetDBusTaskRunner()->PostTask( |
156 FROM_HERE, | 161 FROM_HERE, |
157 base::Bind(&TestService::ReleaseOwnershipInternal, | 162 base::Bind(&TestService::ReleaseOwnershipInternal, |
158 base::Unretained(this), | 163 base::Unretained(this), |
159 callback)); | 164 callback)); |
160 } | 165 } |
161 | 166 |
162 void TestService::ReleaseOwnershipInternal( | 167 void TestService::ReleaseOwnershipInternal( |
163 base::Closure callback) { | 168 base::Closure callback) { |
164 bus_->ReleaseOwnership("org.chromium.TestService"); | 169 bus_->ReleaseOwnership(service_name_); |
165 has_ownership_ = false; | 170 has_ownership_ = false; |
166 | 171 |
167 bus_->GetOriginTaskRunner()->PostTask( | 172 bus_->GetOriginTaskRunner()->PostTask( |
168 FROM_HERE, | 173 FROM_HERE, |
169 callback); | 174 callback); |
170 } | 175 } |
171 | 176 |
172 void TestService::SetSendImmediatePropertiesChanged() { | 177 void TestService::SetSendImmediatePropertiesChanged() { |
173 send_immediate_properties_changed_ = true; | 178 send_immediate_properties_changed_ = true; |
174 } | 179 } |
175 | 180 |
176 void TestService::OnExported(const std::string& interface_name, | 181 void TestService::OnExported(const std::string& interface_name, |
177 const std::string& method_name, | 182 const std::string& method_name, |
178 bool success) { | 183 bool success) { |
179 if (!success) { | 184 if (!success) { |
180 LOG(ERROR) << "Failed to export: " << interface_name << "." | 185 LOG(ERROR) << "Failed to export: " << interface_name << "." |
181 << method_name; | 186 << method_name; |
182 // Returning here will make WaitUntilServiceIsStarted() to time out | 187 // Returning here will make WaitUntilServiceIsStarted() to time out |
183 // and return false. | 188 // and return false. |
184 return; | 189 return; |
185 } | 190 } |
186 | 191 |
187 ++num_exported_methods_; | 192 ++num_exported_methods_; |
188 if (num_exported_methods_ == kNumMethodsToExport) { | 193 if (num_exported_methods_ == kNumMethodsToExport) { |
189 // As documented in exported_object.h, the service name should be | 194 // As documented in exported_object.h, the service name should be |
190 // requested after all methods are exposed. | 195 // requested after all methods are exposed. |
191 bus_->RequestOwnership("org.chromium.TestService", | 196 bus_->RequestOwnership(service_name_, |
192 request_ownership_options_, | 197 request_ownership_options_, |
193 base::Bind(&TestService::OnOwnership, | 198 base::Bind(&TestService::OnOwnership, |
194 base::Unretained(this), | 199 base::Unretained(this), |
195 base::Bind(&EmptyCallback))); | 200 base::Bind(&EmptyCallback))); |
196 } | 201 } |
197 } | 202 } |
198 | 203 |
199 void TestService::Run(base::MessageLoop* message_loop) { | 204 void TestService::Run(base::MessageLoop* message_loop) { |
200 Bus::Options bus_options; | 205 Bus::Options bus_options; |
201 bus_options.bus_type = Bus::SESSION; | 206 bus_options.bus_type = Bus::SESSION; |
(...skipping 535 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
737 MessageWriter invalidated_array_writer(NULL); | 742 MessageWriter invalidated_array_writer(NULL); |
738 | 743 |
739 writer.OpenArray("s", &invalidated_array_writer); | 744 writer.OpenArray("s", &invalidated_array_writer); |
740 invalidated_array_writer.AppendString("Name"); | 745 invalidated_array_writer.AppendString("Name"); |
741 writer.CloseContainer(&invalidated_array_writer); | 746 writer.CloseContainer(&invalidated_array_writer); |
742 | 747 |
743 exported_object_->SendSignal(&signal); | 748 exported_object_->SendSignal(&signal); |
744 } | 749 } |
745 | 750 |
746 } // namespace dbus | 751 } // namespace dbus |
OLD | NEW |