1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/ash/components/dbus/spaced/spaced_client.h"
#include <map>
#include <vector>
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "chromeos/ash/components/dbus/spaced/fake_spaced_client.h"
#include "dbus/bus.h"
#include "dbus/message.h"
#include "dbus/object_proxy.h"
#include "third_party/cros_system_api/dbus/spaced/dbus-constants.h"
namespace ash {
namespace {
using spaced::kSpacedInterface;
SpacedClient* g_instance = nullptr;
// Implementation of SpacedClient talking to the Spaced interface on the Chrome
// OS side.
class SpacedClientImpl : public SpacedClient {
public:
SpacedClientImpl() = default;
~SpacedClientImpl() override = default;
// Not copyable or movable.
SpacedClientImpl(const SpacedClientImpl&) = delete;
SpacedClientImpl& operator=(const SpacedClientImpl&) = delete;
void GetFreeDiskSpace(const std::string& path,
GetSizeCallback callback) override {
dbus::MethodCall method_call(kSpacedInterface,
spaced::kGetFreeDiskSpaceMethod);
dbus::MessageWriter writer(&method_call);
writer.AppendString(path);
proxy_->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&SpacedClientImpl::HandleGetSizeResponse,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
void GetTotalDiskSpace(const std::string& path,
GetSizeCallback callback) override {
dbus::MethodCall method_call(kSpacedInterface,
spaced::kGetTotalDiskSpaceMethod);
dbus::MessageWriter writer(&method_call);
writer.AppendString(path);
proxy_->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&SpacedClientImpl::HandleGetSizeResponse,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
void GetRootDeviceSize(GetSizeCallback callback) override {
dbus::MethodCall method_call(kSpacedInterface,
spaced::kGetRootDeviceSizeMethod);
dbus::MessageWriter writer(&method_call);
proxy_->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&SpacedClientImpl::HandleGetSizeResponse,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
void IsQuotaSupported(const std::string& path,
BoolCallback callback) override {
dbus::MethodCall method_call(kSpacedInterface,
spaced::kIsQuotaSupportedMethod);
dbus::MessageWriter writer(&method_call);
writer.AppendString(path);
proxy_->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&SpacedClientImpl::HandleBoolResponse,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
void GetQuotaCurrentSpaceForUid(const std::string& path,
uint32_t uid,
GetSizeCallback callback) override {
dbus::MethodCall method_call(kSpacedInterface,
spaced::kGetQuotaCurrentSpaceForUidMethod);
dbus::MessageWriter writer(&method_call);
writer.AppendString(path);
writer.AppendUint32(uid);
proxy_->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&SpacedClientImpl::HandleGetSizeResponse,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
void GetQuotaCurrentSpaceForGid(const std::string& path,
uint32_t gid,
GetSizeCallback callback) override {
dbus::MethodCall method_call(kSpacedInterface,
spaced::kGetQuotaCurrentSpaceForGidMethod);
dbus::MessageWriter writer(&method_call);
writer.AppendString(path);
writer.AppendUint32(gid);
proxy_->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&SpacedClientImpl::HandleGetSizeResponse,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
void GetQuotaCurrentSpaceForProjectId(const std::string& path,
uint32_t project_id,
GetSizeCallback callback) override {
dbus::MethodCall method_call(
kSpacedInterface, spaced::kGetQuotaCurrentSpaceForProjectIdMethod);
dbus::MessageWriter writer(&method_call);
writer.AppendString(path);
writer.AppendUint32(project_id);
proxy_->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&SpacedClientImpl::HandleGetSizeResponse,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
void GetQuotaCurrentSpacesForIds(const std::string& path,
const std::vector<uint32_t>& uids,
const std::vector<uint32_t>& gids,
const std::vector<uint32_t>& project_ids,
GetSpacesForIdsCallback callback) override {
dbus::MethodCall method_call(kSpacedInterface,
spaced::kGetQuotaCurrentSpacesForIdsMethod);
spaced::GetQuotaCurrentSpacesForIdsRequest request;
request.set_path(path);
request.mutable_uids()->Add(uids.begin(), uids.end());
request.mutable_gids()->Add(gids.begin(), gids.end());
request.mutable_project_ids()->Add(project_ids.begin(), project_ids.end());
dbus::MessageWriter writer(&method_call);
writer.AppendProtoAsArrayOfBytes(request);
proxy_->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(
[](GetSpacesForIdsCallback callback, dbus::Response* response) {
if (!response) {
LOG(ERROR) << "Spaced D-Bus method "
<< spaced::kGetQuotaCurrentSpacesForIdsMethod
<< " returned a null response";
std::move(callback).Run(std::nullopt);
return;
}
dbus::MessageReader reader(response);
spaced::GetQuotaCurrentSpacesForIdsReply reply;
if (!reader.PopArrayOfBytesAsProto(&reply)) {
LOG(ERROR) << "Spaced D-Bus method "
<< spaced::kGetQuotaCurrentSpacesForIdsMethod
<< " returned an invalid response: "
<< response->ToString();
std::move(callback).Run(std::nullopt);
return;
}
std::move(callback).Run(
SpaceMaps(std::map<uint32_t, int64_t>(
reply.curspaces_for_uids().begin(),
reply.curspaces_for_uids().end()),
std::map<uint32_t, int64_t>(
reply.curspaces_for_gids().begin(),
reply.curspaces_for_gids().end()),
std::map<uint32_t, int64_t>(
reply.curspaces_for_project_ids().begin(),
reply.curspaces_for_project_ids().end())));
},
std::move(callback)));
}
void Init(dbus::Bus* bus) {
proxy_ = bus->GetObjectProxy(spaced::kSpacedServiceName,
dbus::ObjectPath(spaced::kSpacedServicePath));
DCHECK(proxy_);
proxy_->ConnectToSignal(
kSpacedInterface, spaced::kStatefulDiskSpaceUpdate,
base::BindRepeating(&SpacedClientImpl::OnSpaceUpdate,
weak_ptr_factory_.GetWeakPtr()),
base::BindOnce(&SpacedClientImpl::OnSignalConnected,
weak_ptr_factory_.GetWeakPtr()));
}
private:
void HandleBoolResponse(BoolCallback callback, dbus::Response* response) {
if (!response) {
std::move(callback).Run(std::nullopt);
return;
}
dbus::MessageReader reader(response);
bool result = 0;
if (!reader.PopBool(&result)) {
LOG(ERROR) << "Spaced D-Bus method " << response->GetMember()
<< ": Invalid response. " + response->ToString();
std::move(callback).Run(std::nullopt);
return;
}
std::move(callback).Run(result);
}
// Handles the result of methods that get size and calls |callback|.
void HandleGetSizeResponse(GetSizeCallback callback,
dbus::Response* response) {
if (!response) {
std::move(callback).Run(std::nullopt);
return;
}
dbus::MessageReader reader(response);
int64_t size = 0;
if (!reader.PopInt64(&size)) {
LOG(ERROR) << "Spaced D-Bus method " << response->GetMember()
<< ": Invalid response. " + response->ToString();
std::move(callback).Run(std::nullopt);
return;
}
std::move(callback).Run(size);
}
void OnSpaceUpdate(dbus::Signal* const signal) const {
Observer::SpaceEvent event;
if (!dbus::MessageReader(signal).PopArrayOfBytesAsProto(&event)) {
LOG(ERROR) << "Cannot parse StatefulDiskSpaceUpdate proto";
return;
}
for (Observer& observer : observers_) {
observer.OnSpaceUpdate(event);
}
}
void OnSignalConnected(const std::string& interface_name,
const std::string& signal_name,
const bool connected) {
connected_ = connected;
LOG_IF(ERROR, !connected)
<< "Cannot connect to StatefulDiskSpaceUpdate signal";
DCHECK_EQ(interface_name, kSpacedInterface);
DCHECK_EQ(signal_name, spaced::kStatefulDiskSpaceUpdate);
}
raw_ptr<dbus::ObjectProxy> proxy_ = nullptr;
// Note: This should remain the last member so it'll be destroyed and
// invalidate its weak pointers before any other members are destroyed.
base::WeakPtrFactory<SpacedClientImpl> weak_ptr_factory_{this};
};
} // namespace
SpacedClient::SpaceMaps::SpaceMaps(
std::map<uint32_t, int64_t>&& curspaces_for_uids,
std::map<uint32_t, int64_t>&& curspaces_for_gids,
std::map<uint32_t, int64_t>&& curspaces_for_project_ids)
: curspaces_for_uids(std::move(curspaces_for_uids)),
curspaces_for_gids(std::move(curspaces_for_gids)),
curspaces_for_project_ids(std::move(curspaces_for_project_ids)) {}
SpacedClient::SpaceMaps::~SpaceMaps() = default;
SpacedClient::SpaceMaps::SpaceMaps(const SpacedClient::SpaceMaps& other) =
default;
SpacedClient::SpacedClient() {
CHECK(!g_instance);
g_instance = this;
}
SpacedClient::~SpacedClient() {
CHECK_EQ(this, g_instance);
g_instance = nullptr;
}
// static
void SpacedClient::Initialize(dbus::Bus* bus) {
CHECK(bus);
(new SpacedClientImpl())->Init(bus);
}
// static
void SpacedClient::InitializeFake() {
new FakeSpacedClient();
}
// static
void SpacedClient::Shutdown() {
CHECK(g_instance);
delete g_instance;
// The destructor resets |g_instance|.
DCHECK(!g_instance);
}
// static
SpacedClient* SpacedClient::Get() {
return g_instance;
}
} // namespace ash
|