Adding Peter Thatcher to the owners file.
[chromium-blink-merge.git] / chromeos / dbus / permission_broker_client.cc
blobc26d53c9acdbfff91d529b293f95bc779f4419c4
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
3 // found in the LICENSE file.
5 #include "chromeos/dbus/permission_broker_client.h"
7 #include "base/bind.h"
8 #include "base/memory/weak_ptr.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"
14 using permission_broker::kPermissionBrokerInterface;
15 using permission_broker::kPermissionBrokerServiceName;
16 using permission_broker::kPermissionBrokerServicePath;
17 using permission_broker::kReleaseTcpPort;
18 using permission_broker::kReleaseUdpPort;
19 using permission_broker::kRequestPathAccess;
20 using permission_broker::kRequestTcpPortAccess;
21 using permission_broker::kRequestUdpPortAccess;
23 namespace chromeos {
25 class PermissionBrokerClientImpl : public PermissionBrokerClient {
26 public:
27 PermissionBrokerClientImpl() : proxy_(NULL), weak_ptr_factory_(this) {}
29 void RequestPathAccess(const std::string& path,
30 const int interface_id,
31 const ResultCallback& callback) override {
32 dbus::MethodCall method_call(kPermissionBrokerInterface,
33 kRequestPathAccess);
34 dbus::MessageWriter writer(&method_call);
35 writer.AppendString(path);
36 writer.AppendInt32(interface_id);
37 proxy_->CallMethod(&method_call,
38 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
39 base::Bind(&PermissionBrokerClientImpl::OnResponse,
40 weak_ptr_factory_.GetWeakPtr(), callback));
43 void RequestTcpPortAccess(uint16 port,
44 const std::string& interface,
45 const dbus::FileDescriptor& lifeline_fd,
46 const ResultCallback& callback) override {
47 dbus::MethodCall method_call(kPermissionBrokerInterface,
48 kRequestTcpPortAccess);
49 dbus::MessageWriter writer(&method_call);
50 writer.AppendUint16(port);
51 writer.AppendString(interface);
52 writer.AppendFileDescriptor(lifeline_fd);
53 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
54 base::Bind(&PermissionBrokerClientImpl::OnResponse,
55 weak_ptr_factory_.GetWeakPtr(), callback));
58 void RequestUdpPortAccess(uint16 port,
59 const std::string& interface,
60 const dbus::FileDescriptor& lifeline_fd,
61 const ResultCallback& callback) override {
62 dbus::MethodCall method_call(kPermissionBrokerInterface,
63 kRequestUdpPortAccess);
64 dbus::MessageWriter writer(&method_call);
65 writer.AppendUint16(port);
66 writer.AppendString(interface);
67 writer.AppendFileDescriptor(lifeline_fd);
68 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
69 base::Bind(&PermissionBrokerClientImpl::OnResponse,
70 weak_ptr_factory_.GetWeakPtr(), callback));
73 void ReleaseTcpPort(uint16 port,
74 const std::string& interface,
75 const ResultCallback& callback) override {
76 dbus::MethodCall method_call(kPermissionBrokerInterface, kReleaseTcpPort);
77 dbus::MessageWriter writer(&method_call);
78 writer.AppendUint16(port);
79 writer.AppendString(interface);
80 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
81 base::Bind(&PermissionBrokerClientImpl::OnResponse,
82 weak_ptr_factory_.GetWeakPtr(), callback));
85 void ReleaseUdpPort(uint16 port,
86 const std::string& interface,
87 const ResultCallback& callback) override {
88 dbus::MethodCall method_call(kPermissionBrokerInterface, kReleaseUdpPort);
89 dbus::MessageWriter writer(&method_call);
90 writer.AppendUint16(port);
91 writer.AppendString(interface);
92 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
93 base::Bind(&PermissionBrokerClientImpl::OnResponse,
94 weak_ptr_factory_.GetWeakPtr(), callback));
97 protected:
98 void Init(dbus::Bus* bus) override {
99 proxy_ =
100 bus->GetObjectProxy(kPermissionBrokerServiceName,
101 dbus::ObjectPath(kPermissionBrokerServicePath));
104 private:
105 // Handle a DBus response from the permission broker, invoking the callback
106 // that the method was originally called with with the success response.
107 void OnResponse(const ResultCallback& callback, dbus::Response* response) {
108 if (!response) {
109 LOG(WARNING) << "Access request method call failed.";
110 callback.Run(false);
111 return;
114 bool result = false;
115 dbus::MessageReader reader(response);
116 if (!reader.PopBool(&result))
117 LOG(WARNING) << "Could not parse response: " << response->ToString();
118 callback.Run(result);
121 dbus::ObjectProxy* proxy_;
123 // Note: This should remain the last member so that it will be destroyed
124 // first, invalidating its weak pointers, before the other members are
125 // destroyed.
126 base::WeakPtrFactory<PermissionBrokerClientImpl> weak_ptr_factory_;
128 DISALLOW_COPY_AND_ASSIGN(PermissionBrokerClientImpl);
131 PermissionBrokerClient::PermissionBrokerClient() {}
133 PermissionBrokerClient::~PermissionBrokerClient() {}
135 PermissionBrokerClient* PermissionBrokerClient::Create() {
136 return new PermissionBrokerClientImpl();
139 } // namespace chromeos