OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // TODO(satorux): | 5 // TODO(satorux): |
6 // - Handle "disconnected" signal. | 6 // - Handle "disconnected" signal. |
7 // - Collect metrics (ex. # of method calls, method call time, etc.) | 7 // - Collect metrics (ex. # of method calls, method call time, etc.) |
8 | 8 |
9 #include "dbus/bus.h" | 9 #include "dbus/bus.h" |
10 | 10 |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 DCHECK(filter_functions_added_.empty()); | 205 DCHECK(filter_functions_added_.empty()); |
206 DCHECK(registered_object_paths_.empty()); | 206 DCHECK(registered_object_paths_.empty()); |
207 DCHECK_EQ(0, num_pending_watches_); | 207 DCHECK_EQ(0, num_pending_watches_); |
208 DCHECK_EQ(0, num_pending_timeouts_); | 208 DCHECK_EQ(0, num_pending_timeouts_); |
209 } | 209 } |
210 | 210 |
211 ObjectProxy* Bus::GetObjectProxy(const std::string& service_name, | 211 ObjectProxy* Bus::GetObjectProxy(const std::string& service_name, |
212 const std::string& object_path) { | 212 const std::string& object_path) { |
213 AssertOnOriginThread(); | 213 AssertOnOriginThread(); |
214 | 214 |
| 215 // Check if we already have the requested object proxy. |
| 216 std::pair<std::string, std::string> key = |
| 217 std::make_pair(service_name, object_path); |
| 218 ObjectProxyTable::iterator iter = object_proxy_table_.find(key); |
| 219 if (iter != object_proxy_table_.end()) { |
| 220 return iter->second; |
| 221 } |
| 222 |
215 scoped_refptr<ObjectProxy> object_proxy = | 223 scoped_refptr<ObjectProxy> object_proxy = |
216 new ObjectProxy(this, service_name, object_path); | 224 new ObjectProxy(this, service_name, object_path); |
217 object_proxies_.push_back(object_proxy); | 225 object_proxy_table_[key] = object_proxy; |
218 | 226 |
219 return object_proxy; | 227 return object_proxy.get(); |
220 } | 228 } |
221 | 229 |
222 ExportedObject* Bus::GetExportedObject(const std::string& service_name, | 230 ExportedObject* Bus::GetExportedObject(const std::string& service_name, |
223 const std::string& object_path) { | 231 const std::string& object_path) { |
224 AssertOnOriginThread(); | 232 AssertOnOriginThread(); |
225 | 233 |
| 234 // Check if we already have the requested exported object. |
| 235 std::pair<std::string, std::string> key = |
| 236 std::make_pair(service_name, object_path); |
| 237 ExportedObjectTable::iterator iter = exported_object_table_.find(key); |
| 238 if (iter != exported_object_table_.end()) { |
| 239 return iter->second; |
| 240 } |
| 241 |
226 scoped_refptr<ExportedObject> exported_object = | 242 scoped_refptr<ExportedObject> exported_object = |
227 new ExportedObject(this, service_name, object_path); | 243 new ExportedObject(this, service_name, object_path); |
228 exported_objects_.push_back(exported_object); | 244 exported_object_table_[key] = exported_object; |
229 | 245 |
230 return exported_object; | 246 return exported_object.get(); |
231 } | 247 } |
232 | 248 |
233 bool Bus::Connect() { | 249 bool Bus::Connect() { |
234 // dbus_bus_get_private() and dbus_bus_get() are blocking calls. | 250 // dbus_bus_get_private() and dbus_bus_get() are blocking calls. |
235 AssertOnDBusThread(); | 251 AssertOnDBusThread(); |
236 | 252 |
237 // Check if it's already initialized. | 253 // Check if it's already initialized. |
238 if (connection_) | 254 if (connection_) |
239 return true; | 255 return true; |
240 | 256 |
(...skipping 12 matching lines...) Expand all Loading... |
253 // We shouldn't exit on the disconnected signal. | 269 // We shouldn't exit on the disconnected signal. |
254 dbus_connection_set_exit_on_disconnect(connection_, false); | 270 dbus_connection_set_exit_on_disconnect(connection_, false); |
255 | 271 |
256 return true; | 272 return true; |
257 } | 273 } |
258 | 274 |
259 void Bus::ShutdownAndBlock() { | 275 void Bus::ShutdownAndBlock() { |
260 AssertOnDBusThread(); | 276 AssertOnDBusThread(); |
261 | 277 |
262 // Unregister the exported objects. | 278 // Unregister the exported objects. |
263 for (size_t i = 0; i < exported_objects_.size(); ++i) { | 279 for (ExportedObjectTable::iterator iter = exported_object_table_.begin(); |
264 exported_objects_[i]->Unregister(); | 280 iter != exported_object_table_.end(); ++iter) { |
| 281 iter->second->Unregister(); |
265 } | 282 } |
266 | 283 |
267 // Release all service names. | 284 // Release all service names. |
268 for (std::set<std::string>::iterator iter = owned_service_names_.begin(); | 285 for (std::set<std::string>::iterator iter = owned_service_names_.begin(); |
269 iter != owned_service_names_.end();) { | 286 iter != owned_service_names_.end();) { |
270 // This is a bit tricky but we should increment the iter here as | 287 // This is a bit tricky but we should increment the iter here as |
271 // ReleaseOwnership() may remove |service_name| from the set. | 288 // ReleaseOwnership() may remove |service_name| from the set. |
272 const std::string& service_name = *iter++; | 289 const std::string& service_name = *iter++; |
273 ReleaseOwnership(service_name); | 290 ReleaseOwnership(service_name); |
274 } | 291 } |
275 if (!owned_service_names_.empty()) { | 292 if (!owned_service_names_.empty()) { |
276 LOG(ERROR) << "Failed to release all service names. # of services left: " | 293 LOG(ERROR) << "Failed to release all service names. # of services left: " |
277 << owned_service_names_.size(); | 294 << owned_service_names_.size(); |
278 } | 295 } |
279 | 296 |
280 // Detach from the remote objects. | 297 // Detach from the remote objects. |
281 for (size_t i = 0; i < object_proxies_.size(); ++i) { | 298 for (ObjectProxyTable::iterator iter = object_proxy_table_.begin(); |
282 object_proxies_[i]->Detach(); | 299 iter != object_proxy_table_.end(); ++iter) { |
| 300 iter->second->Detach(); |
283 } | 301 } |
284 | 302 |
285 // Private connection should be closed. | 303 // Private connection should be closed. |
286 if (connection_ && connection_type_ == PRIVATE) { | 304 if (connection_ && connection_type_ == PRIVATE) { |
287 dbus_connection_close(connection_); | 305 dbus_connection_close(connection_); |
288 } | 306 } |
289 // dbus_connection_close() won't unref. | 307 // dbus_connection_close() won't unref. |
290 dbus_connection_unref(connection_); | 308 dbus_connection_unref(connection_); |
291 | 309 |
292 connection_ = NULL; | 310 connection_ = NULL; |
(...skipping 387 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
680 } | 698 } |
681 | 699 |
682 void Bus::OnDispatchStatusChangedThunk(DBusConnection* connection, | 700 void Bus::OnDispatchStatusChangedThunk(DBusConnection* connection, |
683 DBusDispatchStatus status, | 701 DBusDispatchStatus status, |
684 void* data) { | 702 void* data) { |
685 Bus* self = static_cast<Bus*>(data); | 703 Bus* self = static_cast<Bus*>(data); |
686 return self->OnDispatchStatusChanged(connection, status); | 704 return self->OnDispatchStatusChanged(connection, status); |
687 } | 705 } |
688 | 706 |
689 } // namespace dbus | 707 } // namespace dbus |
OLD | NEW |