Home | History | Annotate | Download | only in dbus
      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:
     25   explicit PermissionBrokerClientImpl(dbus::Bus* bus)
     26       : proxy_(bus->GetObjectProxy(kPermissionBrokerServiceName,
     27             dbus::ObjectPath(kPermissionBrokerServicePath))),
     28         weak_ptr_factory_(this) {}
     29 
     30   virtual void RequestPathAccess(const std::string& path,
     31                                  const int interface_id,
     32                                  const ResultCallback& callback) OVERRIDE {
     33     dbus::MethodCall method_call(kPermissionBrokerInterface,
     34                                  kRequestPathAccess);
     35     dbus::MessageWriter writer(&method_call);
     36     writer.AppendString(path);
     37     writer.AppendInt32(interface_id);
     38     proxy_->CallMethod(&method_call,
     39                        dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
     40                        base::Bind(&PermissionBrokerClientImpl::OnResponse,
     41                                   weak_ptr_factory_.GetWeakPtr(), callback));
     42   }
     43 
     44   virtual void RequestUsbAccess(const uint16_t vendor_id,
     45                                 const uint16_t product_id,
     46                                 const int interface_id,
     47                                 const ResultCallback& callback) OVERRIDE {
     48     dbus::MethodCall method_call(kPermissionBrokerInterface, kRequestUsbAccess);
     49     dbus::MessageWriter writer(&method_call);
     50     writer.AppendUint16(vendor_id);
     51     writer.AppendUint16(product_id);
     52     writer.AppendInt32(interface_id);
     53     proxy_->CallMethod(&method_call,
     54                        dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
     55                        base::Bind(&PermissionBrokerClientImpl::OnResponse,
     56                                   weak_ptr_factory_.GetWeakPtr(), callback));
     57   }
     58 
     59  private:
     60   // Handle a DBus response from the permission broker, invoking the callback
     61   // that the method was originally called with with the success response.
     62   void OnResponse(const ResultCallback& callback, dbus::Response* response) {
     63     if (!response) {
     64       LOG(WARNING) << "Access request method call failed.";
     65       callback.Run(false);
     66       return;
     67     }
     68 
     69     bool result = false;
     70     dbus::MessageReader reader(response);
     71     if (!reader.PopBool(&result))
     72       LOG(WARNING) << "Could not parse response: " << response->ToString();
     73     callback.Run(result);
     74   }
     75 
     76   dbus::ObjectProxy* proxy_;
     77 
     78   // Note: This should remain the last member so that it will be destroyed
     79   // first, invalidating its weak pointers, before the other members are
     80   // destroyed.
     81   base::WeakPtrFactory<PermissionBrokerClientImpl> weak_ptr_factory_;
     82 
     83   DISALLOW_COPY_AND_ASSIGN(PermissionBrokerClientImpl);
     84 };
     85 
     86 class PermissionBrokerClientStubImpl : public PermissionBrokerClient {
     87  public:
     88   PermissionBrokerClientStubImpl() {}
     89   virtual ~PermissionBrokerClientStubImpl() {}
     90 
     91   virtual void RequestPathAccess(const std::string& path,
     92                                  int interface_id,
     93                                  const ResultCallback& callback) OVERRIDE {
     94     callback.Run(false);
     95   }
     96 
     97   virtual void RequestUsbAccess(const uint16_t vendor_id,
     98                                 const uint16_t product_id,
     99                                 int interface_id,
    100                                 const ResultCallback& callback) OVERRIDE {
    101     callback.Run(false);
    102   }
    103 
    104  private:
    105   DISALLOW_COPY_AND_ASSIGN(PermissionBrokerClientStubImpl);
    106 };
    107 
    108 PermissionBrokerClient::PermissionBrokerClient() {}
    109 
    110 PermissionBrokerClient::~PermissionBrokerClient() {}
    111 
    112 PermissionBrokerClient* PermissionBrokerClient::Create(
    113     DBusClientImplementationType type, dbus::Bus* bus) {
    114   if (type == REAL_DBUS_CLIENT_IMPLEMENTATION)
    115     return new PermissionBrokerClientImpl(bus);
    116   return new PermissionBrokerClientStubImpl();
    117 }
    118 
    119 }  // namespace chromeos
    120