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

ash / public / cpp / test / test_desk_profiles_delegate.cc [blame]

// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/public/cpp/test/test_desk_profiles_delegate.h"

#include <vector>

#include "base/ranges/algorithm.h"

namespace ash {

TestDeskProfilesDelegate::TestDeskProfilesDelegate() = default;

TestDeskProfilesDelegate::~TestDeskProfilesDelegate() = default;

void TestDeskProfilesDelegate::UpdateTestProfile(LacrosProfileSummary profile) {
  auto* summary = const_cast<LacrosProfileSummary*>(
      GetProfilesSnapshotByProfileId(profile.profile_id));
  if (!summary) {
    summary = &profiles_.emplace_back(std::move(profile));
  }

  for (auto& observer : observers_) {
    observer.OnProfileUpsert(*summary);
  }
}

bool TestDeskProfilesDelegate::RemoveTestProfile(uint64_t profile_id) {
  CHECK(profile_id != primary_user_profile_id_);

  if (std::erase_if(profiles_, [&](const auto& profile) {
        return profile.profile_id == profile_id;
      })) {
    for (auto& observer : observers_) {
      observer.OnProfileRemoved(profile_id);
    }
    return true;
  }

  return false;
}

bool TestDeskProfilesDelegate::SetPrimaryProfileByProfileId(
    uint64_t profile_id) {
  if (GetProfilesSnapshotByProfileId(profile_id)) {
    primary_user_profile_id_ = profile_id;
    return true;
  }
  return false;
}

const std::vector<LacrosProfileSummary>&
TestDeskProfilesDelegate::GetProfilesSnapshot() const {
  return profiles_;
}

const LacrosProfileSummary*
TestDeskProfilesDelegate::GetProfilesSnapshotByProfileId(
    uint64_t profile_id) const {
  if (profile_id == 0) {
    profile_id = primary_user_profile_id_;
  }

  const auto iter = base::ranges::find(profiles_, profile_id,
                                       &LacrosProfileSummary::profile_id);
  return iter == profiles_.end() ? nullptr : &(*iter);
}

uint64_t TestDeskProfilesDelegate::GetPrimaryProfileId() const {
  return primary_user_profile_id_;
}

void TestDeskProfilesDelegate::AddObserver(Observer* observer) {
  observers_.AddObserver(observer);
}

void TestDeskProfilesDelegate::RemoveObserver(Observer* observer) {
  observers_.RemoveObserver(observer);
}

}  // namespace ash