Simple Cache: transactional index in subdirectory
[chromium-blink-merge.git] / chromeos / dbus / permission_broker_client.cc
blob4bf2699e2f7c88db79d1c80d74e96a5e47a54569
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/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"
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;
21 namespace chromeos {
23 class PermissionBrokerClientImpl : public PermissionBrokerClient {
24 public:
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,
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));
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));
56 protected:
57 virtual void Init(dbus::Bus* bus) OVERRIDE {
58 proxy_ =
59 bus->GetObjectProxy(kPermissionBrokerServiceName,
60 dbus::ObjectPath(kPermissionBrokerServicePath));
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.
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;
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);
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
84 // destroyed.
85 base::WeakPtrFactory<PermissionBrokerClientImpl> weak_ptr_factory_;
87 DISALLOW_COPY_AND_ASSIGN(PermissionBrokerClientImpl);
90 class PermissionBrokerClientStubImpl : public PermissionBrokerClient {
91 public:
92 PermissionBrokerClientStubImpl() {}
93 virtual ~PermissionBrokerClientStubImpl() {}
95 virtual void Init(dbus::Bus* bus) OVERRIDE {}
96 virtual void RequestPathAccess(const std::string& path,
97 int interface_id,
98 const ResultCallback& callback) OVERRIDE {
99 callback.Run(false);
102 virtual void RequestUsbAccess(const uint16_t vendor_id,
103 const uint16_t product_id,
104 int interface_id,
105 const ResultCallback& callback) OVERRIDE {
106 callback.Run(false);
109 private:
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