Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chromeos / dbus / permission_broker_client.cc
blob4ba4d31aa53a82342fd896242c88f3b06d5c9988
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/memory/weak_ptr.h"
9 #include "dbus/bus.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::kOpenPath;
16 using permission_broker::kPermissionBrokerInterface;
17 using permission_broker::kPermissionBrokerServiceName;
18 using permission_broker::kPermissionBrokerServicePath;
19 using permission_broker::kReleaseTcpPort;
20 using permission_broker::kReleaseUdpPort;
21 using permission_broker::kRequestPathAccess;
22 using permission_broker::kRequestTcpPortAccess;
23 using permission_broker::kRequestUdpPortAccess;
25 namespace chromeos {
27 class PermissionBrokerClientImpl : public PermissionBrokerClient {
28 public:
29 PermissionBrokerClientImpl() : proxy_(NULL), weak_ptr_factory_(this) {}
31 void CheckPathAccess(const std::string& path,
32 const ResultCallback& callback) override {
33 dbus::MethodCall method_call(kPermissionBrokerInterface, kCheckPathAccess);
34 dbus::MessageWriter writer(&method_call);
35 writer.AppendString(path);
36 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
37 base::Bind(&PermissionBrokerClientImpl::OnResponse,
38 weak_ptr_factory_.GetWeakPtr(), callback));
41 void RequestPathAccess(const std::string& path,
42 const int interface_id,
43 const ResultCallback& callback) override {
44 dbus::MethodCall method_call(kPermissionBrokerInterface,
45 kRequestPathAccess);
46 dbus::MessageWriter writer(&method_call);
47 writer.AppendString(path);
48 writer.AppendInt32(interface_id);
49 proxy_->CallMethod(&method_call,
50 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
51 base::Bind(&PermissionBrokerClientImpl::OnResponse,
52 weak_ptr_factory_.GetWeakPtr(), callback));
55 void OpenPath(const std::string& path,
56 const OpenPathCallback& callback) override {
57 dbus::MethodCall method_call(kPermissionBrokerInterface, kOpenPath);
58 dbus::MessageWriter writer(&method_call);
59 writer.AppendString(path);
60 proxy_->CallMethod(
61 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
62 base::Bind(&PermissionBrokerClientImpl::OnOpenPathResponse,
63 weak_ptr_factory_.GetWeakPtr(), callback));
66 void RequestTcpPortAccess(uint16 port,
67 const std::string& interface,
68 const dbus::FileDescriptor& lifeline_fd,
69 const ResultCallback& callback) override {
70 dbus::MethodCall method_call(kPermissionBrokerInterface,
71 kRequestTcpPortAccess);
72 dbus::MessageWriter writer(&method_call);
73 writer.AppendUint16(port);
74 writer.AppendString(interface);
75 writer.AppendFileDescriptor(lifeline_fd);
76 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
77 base::Bind(&PermissionBrokerClientImpl::OnResponse,
78 weak_ptr_factory_.GetWeakPtr(), callback));
81 void RequestUdpPortAccess(uint16 port,
82 const std::string& interface,
83 const dbus::FileDescriptor& lifeline_fd,
84 const ResultCallback& callback) override {
85 dbus::MethodCall method_call(kPermissionBrokerInterface,
86 kRequestUdpPortAccess);
87 dbus::MessageWriter writer(&method_call);
88 writer.AppendUint16(port);
89 writer.AppendString(interface);
90 writer.AppendFileDescriptor(lifeline_fd);
91 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
92 base::Bind(&PermissionBrokerClientImpl::OnResponse,
93 weak_ptr_factory_.GetWeakPtr(), callback));
96 void ReleaseTcpPort(uint16 port,
97 const std::string& interface,
98 const ResultCallback& callback) override {
99 dbus::MethodCall method_call(kPermissionBrokerInterface, kReleaseTcpPort);
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));
108 void ReleaseUdpPort(uint16 port,
109 const std::string& interface,
110 const ResultCallback& callback) override {
111 dbus::MethodCall method_call(kPermissionBrokerInterface, kReleaseUdpPort);
112 dbus::MessageWriter writer(&method_call);
113 writer.AppendUint16(port);
114 writer.AppendString(interface);
115 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
116 base::Bind(&PermissionBrokerClientImpl::OnResponse,
117 weak_ptr_factory_.GetWeakPtr(), callback));
120 protected:
121 void Init(dbus::Bus* bus) override {
122 proxy_ =
123 bus->GetObjectProxy(kPermissionBrokerServiceName,
124 dbus::ObjectPath(kPermissionBrokerServicePath));
127 private:
128 // Handle a DBus response from the permission broker, invoking the callback
129 // that the method was originally called with with the success response.
130 void OnResponse(const ResultCallback& callback, dbus::Response* response) {
131 if (!response) {
132 LOG(WARNING) << "Access request method call failed.";
133 callback.Run(false);
134 return;
137 bool result = false;
138 dbus::MessageReader reader(response);
139 if (!reader.PopBool(&result))
140 LOG(WARNING) << "Could not parse response: " << response->ToString();
141 callback.Run(result);
144 void OnOpenPathResponse(const OpenPathCallback& callback,
145 dbus::Response* response) {
146 dbus::FileDescriptor fd;
147 if (response) {
148 dbus::MessageReader reader(response);
149 if (!reader.PopFileDescriptor(&fd))
150 LOG(WARNING) << "Could not parse response: " << response->ToString();
151 } else {
152 LOG(WARNING) << "Access request method call failed.";
155 callback.Run(fd.Pass());
158 dbus::ObjectProxy* proxy_;
160 // Note: This should remain the last member so that it will be destroyed
161 // first, invalidating its weak pointers, before the other members are
162 // destroyed.
163 base::WeakPtrFactory<PermissionBrokerClientImpl> weak_ptr_factory_;
165 DISALLOW_COPY_AND_ASSIGN(PermissionBrokerClientImpl);
168 PermissionBrokerClient::PermissionBrokerClient() {}
170 PermissionBrokerClient::~PermissionBrokerClient() {}
172 PermissionBrokerClient* PermissionBrokerClient::Create() {
173 return new PermissionBrokerClientImpl();
176 } // namespace chromeos