Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4c99e96e authored by Bart Van Assche's avatar Bart Van Assche
Browse files

Introduce interface class IProfileAttribute



This patch does not change any functionality but makes it easier to add
unit tests in a later patch.

Bug: 213617178
Test: Compile-tested only.
Change-Id: I6fbbb3297795c9d7ece8fb3263b3a9b0e5115b18
Signed-off-by: default avatarBart Van Assche <bvanassche@google.com>
parent ee36ba39
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -85,7 +85,7 @@ bool CgroupGetControllerFromPath(const std::string& path, std::string* cgroup_na

bool CgroupGetAttributePath(const std::string& attr_name, std::string* path) {
    const TaskProfiles& tp = TaskProfiles::GetInstance();
    const ProfileAttribute* attr = tp.GetAttribute(attr_name);
    const IProfileAttribute* attr = tp.GetAttribute(attr_name);

    if (attr == nullptr) {
        return false;
@@ -100,7 +100,7 @@ bool CgroupGetAttributePath(const std::string& attr_name, std::string* path) {

bool CgroupGetAttributePathForTask(const std::string& attr_name, int tid, std::string* path) {
    const TaskProfiles& tp = TaskProfiles::GetInstance();
    const ProfileAttribute* attr = tp.GetAttribute(attr_name);
    const IProfileAttribute* attr = tp.GetAttribute(attr_name);

    if (attr == nullptr) {
        return false;
+3 −1
Original line number Diff line number Diff line
@@ -112,6 +112,8 @@ bool FdCacheHelper::IsAppDependentPath(const std::string& path) {
    return path.find("<uid>", 0) != std::string::npos || path.find("<pid>", 0) != std::string::npos;
}

IProfileAttribute::~IProfileAttribute() = default;

void ProfileAttribute::Reset(const CgroupController& controller, const std::string& file_name) {
    controller_ = controller;
    file_name_ = file_name;
@@ -726,7 +728,7 @@ TaskProfile* TaskProfiles::GetProfile(const std::string& name) const {
    return nullptr;
}

const ProfileAttribute* TaskProfiles::GetAttribute(const std::string& name) const {
const IProfileAttribute* TaskProfiles::GetAttribute(const std::string& name) const {
    auto iter = attributes_.find(name);

    if (iter != attributes_.end()) {
+19 −9
Original line number Diff line number Diff line
@@ -26,16 +26,26 @@
#include <android-base/unique_fd.h>
#include <cgroup_map.h>

class ProfileAttribute {
class IProfileAttribute {
  public:
    virtual ~IProfileAttribute() = 0;
    virtual void Reset(const CgroupController& controller, const std::string& file_name) = 0;
    virtual const CgroupController* controller() const = 0;
    virtual const std::string& file_name() const = 0;
    virtual bool GetPathForTask(int tid, std::string* path) const = 0;
};

class ProfileAttribute : public IProfileAttribute {
  public:
    ProfileAttribute(const CgroupController& controller, const std::string& file_name)
        : controller_(controller), file_name_(file_name) {}
    ~ProfileAttribute() = default;

    const CgroupController* controller() const { return &controller_; }
    const std::string& file_name() const { return file_name_; }
    void Reset(const CgroupController& controller, const std::string& file_name);
    const CgroupController* controller() const override { return &controller_; }
    const std::string& file_name() const override { return file_name_; }
    void Reset(const CgroupController& controller, const std::string& file_name) override;

    bool GetPathForTask(int tid, std::string* path) const;
    bool GetPathForTask(int tid, std::string* path) const override;

  private:
    CgroupController controller_;
@@ -88,14 +98,14 @@ class SetTimerSlackAction : public ProfileAction {
// Set attribute profile element
class SetAttributeAction : public ProfileAction {
  public:
    SetAttributeAction(const ProfileAttribute* attribute, const std::string& value)
    SetAttributeAction(const IProfileAttribute* attribute, const std::string& value)
        : attribute_(attribute), value_(value) {}

    bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
    bool ExecuteForTask(int tid) const override;

  private:
    const ProfileAttribute* attribute_;
    const IProfileAttribute* attribute_;
    std::string value_;
};

@@ -180,7 +190,7 @@ class TaskProfiles {
    static TaskProfiles& GetInstance();

    TaskProfile* GetProfile(const std::string& name) const;
    const ProfileAttribute* GetAttribute(const std::string& name) const;
    const IProfileAttribute* GetAttribute(const std::string& name) const;
    void DropResourceCaching(ProfileAction::ResourceCacheType cache_type) const;
    bool SetProcessProfiles(uid_t uid, pid_t pid, const std::vector<std::string>& profiles,
                            bool use_fd_cache);
@@ -188,7 +198,7 @@ class TaskProfiles {

  private:
    std::map<std::string, std::shared_ptr<TaskProfile>> profiles_;
    std::map<std::string, std::unique_ptr<ProfileAttribute>> attributes_;
    std::map<std::string, std::unique_ptr<IProfileAttribute>> attributes_;

    TaskProfiles();