• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "chromeos/dbus/permission_broker_client.h"
6 
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/memory/weak_ptr.h"
10 #include "dbus/bus.h"
11 #include "dbus/message.h"
12 #include "dbus/object_proxy.h"
13 #include "third_party/cros_system_api/dbus/service_constants.h"
14 
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;
20 
21 namespace chromeos {
22 
23 class PermissionBrokerClientImpl : public PermissionBrokerClient {
24  public:
PermissionBrokerClientImpl()25   PermissionBrokerClientImpl() : proxy_(NULL), weak_ptr_factory_(this) {}
26 
RequestPathAccess(const std::string & path,const int interface_id,const ResultCallback & callback)27   virtual void RequestPathAccess(const std::string& path,
28                                  const int interface_id,
29                                  const ResultCallback& callback) OVERRIDE {
30     dbus::MethodCall method_call(kPermissionBrokerInterface,
31                                  kRequestPathAccess);
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));
39   }
40 
RequestUsbAccess(const uint16_t vendor_id,const uint16_t product_id,const int interface_id,const ResultCallback & 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));
54   }
55 
56  protected:
Init(dbus::Bus * bus)57   virtual void Init(dbus::Bus* bus) OVERRIDE {
58     proxy_ =
59         bus->GetObjectProxy(kPermissionBrokerServiceName,
60                             dbus::ObjectPath(kPermissionBrokerServicePath));
61   }
62 
63  private:
64   // Handle a DBus response from the permission broker, invoking the callback
65   // that the method was originally called with with the success response.
OnResponse(const ResultCallback & callback,dbus::Response * response)66   void OnResponse(const ResultCallback& callback, dbus::Response* response) {
67     if (!response) {
68       LOG(WARNING) << "Access request method call failed.";
69       callback.Run(false);
70       return;
71     }
72 
73     bool result = false;
74     dbus::MessageReader reader(response);
75     if (!reader.PopBool(&result))
76       LOG(WARNING) << "Could not parse response: " << response->ToString();
77     callback.Run(result);
78   }
79 
80   dbus::ObjectProxy* proxy_;
81 
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
84   // destroyed.
85   base::WeakPtrFactory<PermissionBrokerClientImpl> weak_ptr_factory_;
86 
87   DISALLOW_COPY_AND_ASSIGN(PermissionBrokerClientImpl);
88 };
89 
PermissionBrokerClient()90 PermissionBrokerClient::PermissionBrokerClient() {}
91 
~PermissionBrokerClient()92 PermissionBrokerClient::~PermissionBrokerClient() {}
93 
Create()94 PermissionBrokerClient* PermissionBrokerClient::Create() {
95   return new PermissionBrokerClientImpl();
96 }
97 
98 }  // namespace chromeos
99