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
|
// 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.
#ifndef CHROMEOS_ASH_COMPONENTS_DBUS_SPACED_SPACED_CLIENT_H_
#define CHROMEOS_ASH_COMPONENTS_DBUS_SPACED_SPACED_CLIENT_H_
#include <map>
#include <vector>
#include "base/component_export.h"
#include "base/files/file_path.h"
#include "base/observer_list.h"
#include "chromeos/ash/components/dbus/spaced/spaced.pb.h"
#include "chromeos/dbus/common/dbus_callback.h"
#include "chromeos/dbus/common/dbus_client.h"
namespace dbus {
class Bus;
}
namespace ash {
// A class to make DBus calls for the org.chromium.Spaced service.
class COMPONENT_EXPORT(SPACED_CLIENT) SpacedClient {
public:
class Observer : public base::CheckedObserver {
public:
using SpaceEvent = spaced::StatefulDiskSpaceUpdate;
// Called when the space is queried periodically.
virtual void OnSpaceUpdate(const SpaceEvent& event) = 0;
};
struct 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);
~SpaceMaps();
SpaceMaps(const SpaceMaps& other);
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;
};
using GetSizeCallback = chromeos::DBusMethodCallback<int64_t>;
using BoolCallback = chromeos::DBusMethodCallback<bool>;
using GetSpacesForIdsCallback = chromeos::DBusMethodCallback<SpaceMaps>;
SpacedClient(const SpacedClient&) = delete;
SpacedClient& operator=(const SpacedClient&) = delete;
// Creates and initializes the global instance. |bus| must not be null.
static void Initialize(dbus::Bus* bus);
// Creates and initializes a fake global instance if not already created.
static void InitializeFake();
// Destroys the global instance.
static void Shutdown();
// Returns the global instance which may be null if not initialized.
static SpacedClient* Get();
// Gets free disk space available in bytes for the given file path.
virtual void GetFreeDiskSpace(const std::string& path,
GetSizeCallback callback) = 0;
// Gets total disk space available in bytes on the current partition for the
// given file path.
virtual void GetTotalDiskSpace(const std::string& path,
GetSizeCallback callback) = 0;
// Gets the total disk space available in bytes for usage on the device.
virtual void GetRootDeviceSize(GetSizeCallback callback) = 0;
// Gets whether the user's cryptohome is mounted with quota enabled.
virtual void IsQuotaSupported(const std::string& path,
BoolCallback callback) = 0;
// Gets the current disk space used by the given uid.
virtual void GetQuotaCurrentSpaceForUid(const std::string& path,
uint32_t uid,
GetSizeCallback callback) = 0;
// Gets the current disk space used by the given gid.
virtual void GetQuotaCurrentSpaceForGid(const std::string& path,
uint32_t gid,
GetSizeCallback callback) = 0;
// Gets the current disk space used by the given project_id.
virtual void GetQuotaCurrentSpaceForProjectId(const std::string& path,
uint32_t project_id,
GetSizeCallback callback) = 0;
// Gets the current disk spaces used by each of the given UIDs, GIDs, and
// project IDs, and returns maps from the IDs to their disk space usages.
virtual 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) = 0;
// Adds an observer.
void AddObserver(Observer* const observer) {
observers_.AddObserver(observer);
}
// Removes an observer.
void RemoveObserver(Observer* const observer) {
observers_.RemoveObserver(observer);
}
// Returns true if the `OnStatefulDiskSpaceUpdate` signal has been connected.
bool IsConnected() const { return connected_; }
protected:
// Initialize/Shutdown should be used instead.
SpacedClient();
virtual ~SpacedClient();
// List of observers.
base::ObserverList<Observer> observers_;
// Whether the `OnStatefulDiskSpaceUpdate` signal has been connected.
bool connected_ = false;
};
} // namespace ash
#endif // CHROMEOS_ASH_COMPONENTS_DBUS_SPACED_SPACED_CLIENT_H_
|