OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 The Chromium 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/bluetooth_advertisement_manager_client.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/logging.h" | |
9 #include "dbus/bus.h" | |
10 #include "dbus/message.h" | |
11 #include "dbus/object_proxy.h" | |
12 #include "third_party/cros_system_api/dbus/service_constants.h" | |
13 | |
14 namespace chromeos { | |
15 | |
16 // TODO(rkc): Remove these once the service constants header is updated. | |
17 const char BluetoothAdvertisementManagerClient::kNoResponseError[] = ""; | |
18 const char kBluetoothAdvertisementManagerInterface[] = ""; | |
19 const char kRegisterAdvertisement[] = ""; | |
20 const char kUnregisterAdvertisement[] = ""; | |
21 const char kBluetoothAdvertisementManagerServiceName[] = ""; | |
22 const char kBluetoothAdvertisementManagerServicePath[] = ""; | |
23 | |
24 // The BluetoothAdvertisementManagerClient implementation used in production. | |
25 class BluetoothAdvertisementManagerClientImpl | |
26 : public BluetoothAdvertisementManagerClient { | |
27 public: | |
28 BluetoothAdvertisementManagerClientImpl() | |
29 : object_proxy_(NULL), weak_ptr_factory_(this) {} | |
30 | |
31 ~BluetoothAdvertisementManagerClientImpl() override {} | |
32 | |
33 // BluetoothAdvertisementManagerClient override. | |
34 void RegisterAdvertisement(const dbus::ObjectPath& obj_path, | |
35 const base::Closure& callback, | |
36 const ErrorCallback& error_callback) override { | |
37 dbus::MethodCall method_call(kBluetoothAdvertisementManagerInterface, | |
38 kRegisterAdvertisement); | |
39 | |
40 dbus::MessageWriter writer(&method_call); | |
41 writer.AppendObjectPath(obj_path); | |
42 | |
43 object_proxy_->CallMethodWithErrorCallback( | |
44 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
45 base::Bind(&BluetoothAdvertisementManagerClientImpl::OnSuccess, | |
46 weak_ptr_factory_.GetWeakPtr(), callback), | |
47 base::Bind(&BluetoothAdvertisementManagerClientImpl::OnError, | |
48 weak_ptr_factory_.GetWeakPtr(), error_callback)); | |
49 } | |
50 | |
51 // BluetoothAdvertisementManagerClient override. | |
52 void UnregisterAdvertisement(const dbus::ObjectPath& profile_path, | |
53 const base::Closure& callback, | |
54 const ErrorCallback& error_callback) override { | |
55 dbus::MethodCall method_call(kBluetoothAdvertisementManagerInterface, | |
56 kUnregisterAdvertisement); | |
57 | |
58 dbus::MessageWriter writer(&method_call); | |
59 writer.AppendObjectPath(profile_path); | |
60 | |
61 object_proxy_->CallMethodWithErrorCallback( | |
62 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
63 base::Bind(&BluetoothAdvertisementManagerClientImpl::OnSuccess, | |
64 weak_ptr_factory_.GetWeakPtr(), callback), | |
65 base::Bind(&BluetoothAdvertisementManagerClientImpl::OnError, | |
66 weak_ptr_factory_.GetWeakPtr(), error_callback)); | |
67 } | |
68 | |
69 protected: | |
70 void Init(dbus::Bus* bus) override { | |
71 DCHECK(bus); | |
72 object_proxy_ = bus->GetObjectProxy( | |
73 kBluetoothAdvertisementManagerServiceName, | |
74 dbus::ObjectPath(kBluetoothAdvertisementManagerServicePath)); | |
Marie Janssen
2015/04/09 22:36:27
The path is not constant here, it will be differen
rkc
2015/04/13 19:47:45
Done.
| |
75 } | |
76 | |
77 private: | |
78 // Called when a response for successful method call is received. | |
79 void OnSuccess(const base::Closure& callback, dbus::Response* response) { | |
80 DCHECK(response); | |
81 callback.Run(); | |
82 } | |
83 | |
84 // Called when a response for a failed method call is received. | |
85 void OnError(const ErrorCallback& error_callback, | |
86 dbus::ErrorResponse* response) { | |
87 // Error response has optional error message argument. | |
88 std::string error_name; | |
89 std::string error_message; | |
90 if (response) { | |
91 dbus::MessageReader reader(response); | |
92 error_name = response->GetErrorName(); | |
93 reader.PopString(&error_message); | |
94 } else { | |
95 error_name = kNoResponseError; | |
96 error_message = ""; | |
97 } | |
98 error_callback.Run(error_name, error_message); | |
99 } | |
100 | |
101 dbus::ObjectProxy* object_proxy_; | |
102 | |
103 // Weak pointer factory for generating 'this' pointers that might live longer | |
104 // than we do. | |
105 // Note: This should remain the last member so it'll be destroyed and | |
106 // invalidate its weak pointers before any other members are destroyed. | |
107 base::WeakPtrFactory<BluetoothAdvertisementManagerClientImpl> | |
108 weak_ptr_factory_; | |
109 | |
110 DISALLOW_COPY_AND_ASSIGN(BluetoothAdvertisementManagerClientImpl); | |
111 }; | |
112 | |
113 BluetoothAdvertisementManagerClient::BluetoothAdvertisementManagerClient() { | |
114 } | |
115 | |
116 BluetoothAdvertisementManagerClient::~BluetoothAdvertisementManagerClient() { | |
117 } | |
118 | |
119 BluetoothAdvertisementManagerClient* | |
120 BluetoothAdvertisementManagerClient::Create() { | |
121 return new BluetoothAdvertisementManagerClientImpl(); | |
122 } | |
123 | |
124 } // namespace chromeos | |
OLD | NEW |