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/callback.h"
9 #include "base/memory/weak_ptr.h"
11 #include "dbus/message.h"
12 #include "dbus/object_proxy.h"
13 #include "third_party/cros_system_api/dbus/service_constants.h"
15 using permission_broker::kPermissionBrokerInterface
;
16 using permission_broker::kPermissionBrokerServiceName
;
17 using permission_broker::kPermissionBrokerServicePath
;
18 using permission_broker::kRequestPathAccess
;
19 using permission_broker::kRequestUsbAccess
;
23 class PermissionBrokerClientImpl
: public PermissionBrokerClient
{
25 PermissionBrokerClientImpl() : proxy_(NULL
), weak_ptr_factory_(this) {}
27 virtual void RequestPathAccess(const std::string
& path
,
28 const int interface_id
,
29 const ResultCallback
& callback
) OVERRIDE
{
30 dbus::MethodCall
method_call(kPermissionBrokerInterface
,
32 dbus::MessageWriter
writer(&method_call
);
33 writer
.AppendString(path
);
34 writer
.AppendInt32(interface_id
);
35 proxy_
->CallMethod(&method_call
,
36 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
37 base::Bind(&PermissionBrokerClientImpl::OnResponse
,
38 weak_ptr_factory_
.GetWeakPtr(), callback
));
41 virtual void RequestUsbAccess(const uint16_t vendor_id
,
42 const uint16_t product_id
,
43 const int interface_id
,
44 const ResultCallback
& callback
) OVERRIDE
{
45 dbus::MethodCall
method_call(kPermissionBrokerInterface
, kRequestUsbAccess
);
46 dbus::MessageWriter
writer(&method_call
);
47 writer
.AppendUint16(vendor_id
);
48 writer
.AppendUint16(product_id
);
49 writer
.AppendInt32(interface_id
);
50 proxy_
->CallMethod(&method_call
,
51 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
52 base::Bind(&PermissionBrokerClientImpl::OnResponse
,
53 weak_ptr_factory_
.GetWeakPtr(), callback
));
57 virtual void Init(dbus::Bus
* bus
) OVERRIDE
{
59 bus
->GetObjectProxy(kPermissionBrokerServiceName
,
60 dbus::ObjectPath(kPermissionBrokerServicePath
));
64 // Handle a DBus response from the permission broker, invoking the callback
65 // that the method was originally called with with the success response.
66 void OnResponse(const ResultCallback
& callback
, dbus::Response
* response
) {
68 LOG(WARNING
) << "Access request method call failed.";
74 dbus::MessageReader
reader(response
);
75 if (!reader
.PopBool(&result
))
76 LOG(WARNING
) << "Could not parse response: " << response
->ToString();
80 dbus::ObjectProxy
* proxy_
;
82 // Note: This should remain the last member so that it will be destroyed
83 // first, invalidating its weak pointers, before the other members are
85 base::WeakPtrFactory
<PermissionBrokerClientImpl
> weak_ptr_factory_
;
87 DISALLOW_COPY_AND_ASSIGN(PermissionBrokerClientImpl
);
90 class PermissionBrokerClientStubImpl
: public PermissionBrokerClient
{
92 PermissionBrokerClientStubImpl() {}
93 virtual ~PermissionBrokerClientStubImpl() {}
95 virtual void Init(dbus::Bus
* bus
) OVERRIDE
{}
96 virtual void RequestPathAccess(const std::string
& path
,
98 const ResultCallback
& callback
) OVERRIDE
{
102 virtual void RequestUsbAccess(const uint16_t vendor_id
,
103 const uint16_t product_id
,
105 const ResultCallback
& callback
) OVERRIDE
{
110 DISALLOW_COPY_AND_ASSIGN(PermissionBrokerClientStubImpl
);
113 PermissionBrokerClient::PermissionBrokerClient() {}
115 PermissionBrokerClient::~PermissionBrokerClient() {}
117 PermissionBrokerClient
* PermissionBrokerClient::Create(
118 DBusClientImplementationType type
) {
119 if (type
== REAL_DBUS_CLIENT_IMPLEMENTATION
)
120 return new PermissionBrokerClientImpl();
121 return new PermissionBrokerClientStubImpl();
124 } // namespace chromeos