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"
8 #include "base/memory/weak_ptr.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::kCheckPathAccess
;
15 using permission_broker::kPermissionBrokerInterface
;
16 using permission_broker::kPermissionBrokerServiceName
;
17 using permission_broker::kPermissionBrokerServicePath
;
18 using permission_broker::kReleaseTcpPort
;
19 using permission_broker::kReleaseUdpPort
;
20 using permission_broker::kRequestPathAccess
;
21 using permission_broker::kRequestTcpPortAccess
;
22 using permission_broker::kRequestUdpPortAccess
;
26 class PermissionBrokerClientImpl
: public PermissionBrokerClient
{
28 PermissionBrokerClientImpl() : proxy_(NULL
), weak_ptr_factory_(this) {}
30 void CheckPathAccess(const std::string
& path
,
31 const ResultCallback
& callback
) override
{
32 dbus::MethodCall
method_call(kPermissionBrokerInterface
, kCheckPathAccess
);
33 dbus::MessageWriter
writer(&method_call
);
34 writer
.AppendString(path
);
35 proxy_
->CallMethod(&method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
36 base::Bind(&PermissionBrokerClientImpl::OnResponse
,
37 weak_ptr_factory_
.GetWeakPtr(), callback
));
40 void RequestPathAccess(const std::string
& path
,
41 const int interface_id
,
42 const ResultCallback
& callback
) override
{
43 dbus::MethodCall
method_call(kPermissionBrokerInterface
,
45 dbus::MessageWriter
writer(&method_call
);
46 writer
.AppendString(path
);
47 writer
.AppendInt32(interface_id
);
48 proxy_
->CallMethod(&method_call
,
49 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
50 base::Bind(&PermissionBrokerClientImpl::OnResponse
,
51 weak_ptr_factory_
.GetWeakPtr(), callback
));
54 void RequestTcpPortAccess(uint16 port
,
55 const std::string
& interface
,
56 const dbus::FileDescriptor
& lifeline_fd
,
57 const ResultCallback
& callback
) override
{
58 dbus::MethodCall
method_call(kPermissionBrokerInterface
,
59 kRequestTcpPortAccess
);
60 dbus::MessageWriter
writer(&method_call
);
61 writer
.AppendUint16(port
);
62 writer
.AppendString(interface
);
63 writer
.AppendFileDescriptor(lifeline_fd
);
64 proxy_
->CallMethod(&method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
65 base::Bind(&PermissionBrokerClientImpl::OnResponse
,
66 weak_ptr_factory_
.GetWeakPtr(), callback
));
69 void RequestUdpPortAccess(uint16 port
,
70 const std::string
& interface
,
71 const dbus::FileDescriptor
& lifeline_fd
,
72 const ResultCallback
& callback
) override
{
73 dbus::MethodCall
method_call(kPermissionBrokerInterface
,
74 kRequestUdpPortAccess
);
75 dbus::MessageWriter
writer(&method_call
);
76 writer
.AppendUint16(port
);
77 writer
.AppendString(interface
);
78 writer
.AppendFileDescriptor(lifeline_fd
);
79 proxy_
->CallMethod(&method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
80 base::Bind(&PermissionBrokerClientImpl::OnResponse
,
81 weak_ptr_factory_
.GetWeakPtr(), callback
));
84 void ReleaseTcpPort(uint16 port
,
85 const std::string
& interface
,
86 const ResultCallback
& callback
) override
{
87 dbus::MethodCall
method_call(kPermissionBrokerInterface
, kReleaseTcpPort
);
88 dbus::MessageWriter
writer(&method_call
);
89 writer
.AppendUint16(port
);
90 writer
.AppendString(interface
);
91 proxy_
->CallMethod(&method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
92 base::Bind(&PermissionBrokerClientImpl::OnResponse
,
93 weak_ptr_factory_
.GetWeakPtr(), callback
));
96 void ReleaseUdpPort(uint16 port
,
97 const std::string
& interface
,
98 const ResultCallback
& callback
) override
{
99 dbus::MethodCall
method_call(kPermissionBrokerInterface
, kReleaseUdpPort
);
100 dbus::MessageWriter
writer(&method_call
);
101 writer
.AppendUint16(port
);
102 writer
.AppendString(interface
);
103 proxy_
->CallMethod(&method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
104 base::Bind(&PermissionBrokerClientImpl::OnResponse
,
105 weak_ptr_factory_
.GetWeakPtr(), callback
));
109 void Init(dbus::Bus
* bus
) override
{
111 bus
->GetObjectProxy(kPermissionBrokerServiceName
,
112 dbus::ObjectPath(kPermissionBrokerServicePath
));
116 // Handle a DBus response from the permission broker, invoking the callback
117 // that the method was originally called with with the success response.
118 void OnResponse(const ResultCallback
& callback
, dbus::Response
* response
) {
120 LOG(WARNING
) << "Access request method call failed.";
126 dbus::MessageReader
reader(response
);
127 if (!reader
.PopBool(&result
))
128 LOG(WARNING
) << "Could not parse response: " << response
->ToString();
129 callback
.Run(result
);
132 dbus::ObjectProxy
* proxy_
;
134 // Note: This should remain the last member so that it will be destroyed
135 // first, invalidating its weak pointers, before the other members are
137 base::WeakPtrFactory
<PermissionBrokerClientImpl
> weak_ptr_factory_
;
139 DISALLOW_COPY_AND_ASSIGN(PermissionBrokerClientImpl
);
142 PermissionBrokerClient::PermissionBrokerClient() {}
144 PermissionBrokerClient::~PermissionBrokerClient() {}
146 PermissionBrokerClient
* PermissionBrokerClient::Create() {
147 return new PermissionBrokerClientImpl();
150 } // namespace chromeos