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 "chromeos/dbus/permission_broker_client.h" | 5 #include "chromeos/dbus/permission_broker_client.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
13 #include "dbus/bus.h" | 13 #include "dbus/bus.h" |
14 #include "dbus/message.h" | 14 #include "dbus/message.h" |
15 #include "dbus/object_proxy.h" | 15 #include "dbus/object_proxy.h" |
16 #include "third_party/cros_system_api/dbus/service_constants.h" | 16 #include "third_party/cros_system_api/dbus/service_constants.h" |
17 | 17 |
18 using permission_broker::kCheckPathAccess; | 18 using permission_broker::kCheckPathAccess; |
19 using permission_broker::kOpenPath; | 19 using permission_broker::kOpenPath; |
20 using permission_broker::kPermissionBrokerInterface; | 20 using permission_broker::kPermissionBrokerInterface; |
21 using permission_broker::kPermissionBrokerServiceName; | 21 using permission_broker::kPermissionBrokerServiceName; |
22 using permission_broker::kPermissionBrokerServicePath; | 22 using permission_broker::kPermissionBrokerServicePath; |
23 using permission_broker::kReleaseTcpPort; | 23 using permission_broker::kReleaseTcpPort; |
24 using permission_broker::kReleaseUdpPort; | 24 using permission_broker::kReleaseUdpPort; |
25 using permission_broker::kRequestTcpPortAccess; | 25 using permission_broker::kRequestTcpPortAccess; |
26 using permission_broker::kRequestUdpPortAccess; | 26 using permission_broker::kRequestUdpPortAccess; |
27 | 27 |
28 namespace chromeos { | 28 namespace chromeos { |
29 | 29 |
| 30 namespace { |
| 31 const char kNoResponseError[] = "org.chromium.Error.NoResponse"; |
| 32 } |
| 33 |
30 class PermissionBrokerClientImpl : public PermissionBrokerClient { | 34 class PermissionBrokerClientImpl : public PermissionBrokerClient { |
31 public: | 35 public: |
32 PermissionBrokerClientImpl() : proxy_(NULL), weak_ptr_factory_(this) {} | 36 PermissionBrokerClientImpl() : proxy_(NULL), weak_ptr_factory_(this) {} |
33 | 37 |
34 void CheckPathAccess(const std::string& path, | 38 void CheckPathAccess(const std::string& path, |
35 const ResultCallback& callback) override { | 39 const ResultCallback& callback) override { |
36 dbus::MethodCall method_call(kPermissionBrokerInterface, kCheckPathAccess); | 40 dbus::MethodCall method_call(kPermissionBrokerInterface, kCheckPathAccess); |
37 dbus::MessageWriter writer(&method_call); | 41 dbus::MessageWriter writer(&method_call); |
38 writer.AppendString(path); | 42 writer.AppendString(path); |
39 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 43 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
40 base::Bind(&PermissionBrokerClientImpl::OnResponse, | 44 base::Bind(&PermissionBrokerClientImpl::OnResponse, |
41 weak_ptr_factory_.GetWeakPtr(), callback)); | 45 weak_ptr_factory_.GetWeakPtr(), callback)); |
42 } | 46 } |
43 | 47 |
44 void OpenPath(const std::string& path, | 48 void OpenPath(const std::string& path, |
45 const OpenPathCallback& callback) override { | 49 const OpenPathCallback& callback, |
| 50 const ErrorCallback& error_callback) override { |
46 dbus::MethodCall method_call(kPermissionBrokerInterface, kOpenPath); | 51 dbus::MethodCall method_call(kPermissionBrokerInterface, kOpenPath); |
47 dbus::MessageWriter writer(&method_call); | 52 dbus::MessageWriter writer(&method_call); |
48 writer.AppendString(path); | 53 writer.AppendString(path); |
49 proxy_->CallMethod( | 54 proxy_->CallMethodWithErrorCallback( |
50 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 55 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
51 base::Bind(&PermissionBrokerClientImpl::OnOpenPathResponse, | 56 base::Bind(&PermissionBrokerClientImpl::OnOpenPathResponse, |
52 weak_ptr_factory_.GetWeakPtr(), callback)); | 57 weak_ptr_factory_.GetWeakPtr(), callback), |
| 58 base::Bind(&PermissionBrokerClientImpl::OnError, |
| 59 weak_ptr_factory_.GetWeakPtr(), error_callback)); |
53 } | 60 } |
54 | 61 |
55 void RequestTcpPortAccess(uint16_t port, | 62 void RequestTcpPortAccess(uint16_t port, |
56 const std::string& interface, | 63 const std::string& interface, |
57 const dbus::FileDescriptor& lifeline_fd, | 64 const dbus::FileDescriptor& lifeline_fd, |
58 const ResultCallback& callback) override { | 65 const ResultCallback& callback) override { |
59 dbus::MethodCall method_call(kPermissionBrokerInterface, | 66 dbus::MethodCall method_call(kPermissionBrokerInterface, |
60 kRequestTcpPortAccess); | 67 kRequestTcpPortAccess); |
61 dbus::MessageWriter writer(&method_call); | 68 dbus::MessageWriter writer(&method_call); |
62 writer.AppendUint16(port); | 69 writer.AppendUint16(port); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 bool result = false; | 133 bool result = false; |
127 dbus::MessageReader reader(response); | 134 dbus::MessageReader reader(response); |
128 if (!reader.PopBool(&result)) | 135 if (!reader.PopBool(&result)) |
129 LOG(WARNING) << "Could not parse response: " << response->ToString(); | 136 LOG(WARNING) << "Could not parse response: " << response->ToString(); |
130 callback.Run(result); | 137 callback.Run(result); |
131 } | 138 } |
132 | 139 |
133 void OnOpenPathResponse(const OpenPathCallback& callback, | 140 void OnOpenPathResponse(const OpenPathCallback& callback, |
134 dbus::Response* response) { | 141 dbus::Response* response) { |
135 dbus::FileDescriptor fd; | 142 dbus::FileDescriptor fd; |
136 if (response) { | 143 dbus::MessageReader reader(response); |
137 dbus::MessageReader reader(response); | 144 if (!reader.PopFileDescriptor(&fd)) |
138 if (!reader.PopFileDescriptor(&fd)) | 145 LOG(WARNING) << "Could not parse response: " << response->ToString(); |
139 LOG(WARNING) << "Could not parse response: " << response->ToString(); | |
140 } else { | |
141 LOG(WARNING) << "Access request method call failed."; | |
142 } | |
143 | |
144 callback.Run(std::move(fd)); | 146 callback.Run(std::move(fd)); |
145 } | 147 } |
146 | 148 |
| 149 void OnError(const ErrorCallback& callback, dbus::ErrorResponse* response) { |
| 150 std::string error_name; |
| 151 std::string error_message; |
| 152 if (response) { |
| 153 dbus::MessageReader reader(response); |
| 154 error_name = response->GetErrorName(); |
| 155 reader.PopString(&error_message); |
| 156 } else { |
| 157 error_name = kNoResponseError; |
| 158 } |
| 159 callback.Run(error_name, error_message); |
| 160 } |
| 161 |
147 dbus::ObjectProxy* proxy_; | 162 dbus::ObjectProxy* proxy_; |
148 | 163 |
149 // Note: This should remain the last member so that it will be destroyed | 164 // Note: This should remain the last member so that it will be destroyed |
150 // first, invalidating its weak pointers, before the other members are | 165 // first, invalidating its weak pointers, before the other members are |
151 // destroyed. | 166 // destroyed. |
152 base::WeakPtrFactory<PermissionBrokerClientImpl> weak_ptr_factory_; | 167 base::WeakPtrFactory<PermissionBrokerClientImpl> weak_ptr_factory_; |
153 | 168 |
154 DISALLOW_COPY_AND_ASSIGN(PermissionBrokerClientImpl); | 169 DISALLOW_COPY_AND_ASSIGN(PermissionBrokerClientImpl); |
155 }; | 170 }; |
156 | 171 |
157 PermissionBrokerClient::PermissionBrokerClient() {} | 172 PermissionBrokerClient::PermissionBrokerClient() {} |
158 | 173 |
159 PermissionBrokerClient::~PermissionBrokerClient() {} | 174 PermissionBrokerClient::~PermissionBrokerClient() {} |
160 | 175 |
161 PermissionBrokerClient* PermissionBrokerClient::Create() { | 176 PermissionBrokerClient* PermissionBrokerClient::Create() { |
162 return new PermissionBrokerClientImpl(); | 177 return new PermissionBrokerClientImpl(); |
163 } | 178 } |
164 | 179 |
165 } // namespace chromeos | 180 } // namespace chromeos |
OLD | NEW |