override_vote_aggregator.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2019 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.
  4. #ifndef COMPONENTS_PERFORMANCE_MANAGER_EXECUTION_CONTEXT_PRIORITY_OVERRIDE_VOTE_AGGREGATOR_H_
  5. #define COMPONENTS_PERFORMANCE_MANAGER_EXECUTION_CONTEXT_PRIORITY_OVERRIDE_VOTE_AGGREGATOR_H_
  6. #include <map>
  7. #include "components/performance_manager/public/execution_context_priority/execution_context_priority.h"
  8. #include "third_party/abseil-cpp/absl/types/optional.h"
  9. namespace performance_manager {
  10. namespace execution_context_priority {
  11. // Aggregator that allows votes from 2 different Voters, where one of the voters
  12. // is allowed to override the votes of another. This aggregator should be
  13. // completely setup before any votes are submitted to it.
  14. class OverrideVoteAggregator : public VoteObserver {
  15. public:
  16. OverrideVoteAggregator();
  17. ~OverrideVoteAggregator() override;
  18. OverrideVoteAggregator(const OverrideVoteAggregator&) = delete;
  19. OverrideVoteAggregator& operator=(const OverrideVoteAggregator&) = delete;
  20. // All 3 of these must have been called in order for the aggregator to be
  21. // fully setup.
  22. VotingChannel GetOverrideVotingChannel();
  23. VotingChannel GetDefaultVotingChannel();
  24. void SetUpstreamVotingChannel(VotingChannel channel);
  25. bool IsSetup() const;
  26. size_t GetSizeForTesting() const { return vote_data_map_.size(); }
  27. protected:
  28. // VoteObserver implementation:
  29. void OnVoteSubmitted(VoterId voter_id,
  30. const ExecutionContext* execution_context,
  31. const Vote& vote) override;
  32. void OnVoteChanged(VoterId voter_id,
  33. const ExecutionContext* execution_context,
  34. const Vote& new_vote) override;
  35. void OnVoteInvalidated(VoterId voter_id,
  36. const ExecutionContext* execution_context) override;
  37. private:
  38. // This is move-only because all of its members are move-only.
  39. class VoteData {
  40. public:
  41. enum class VoterType {
  42. kDefault,
  43. kOverride,
  44. };
  45. VoteData();
  46. VoteData(const VoteData& rhs) = delete;
  47. VoteData(VoteData&& rhs);
  48. VoteData& operator=(const VoteData& rhs) = delete;
  49. VoteData& operator=(VoteData&& rhs) = default;
  50. ~VoteData();
  51. void AddVote(VoterType voter_type, const Vote& vote);
  52. void ChangeVote(VoterType voter_type, const Vote& new_vote);
  53. void RemoveVote(VoterType voter_type);
  54. bool HasChosenVote() const;
  55. const Vote& GetChosenVote() const;
  56. private:
  57. // At least one of these is not null if a vote has been emitted for this
  58. // execution context.
  59. absl::optional<Vote> default_vote_;
  60. absl::optional<Vote> override_vote_;
  61. };
  62. using VoteDataMap = std::map<const ExecutionContext*, VoteData>;
  63. // Looks up the VoteData associated with the provided |vote|. The data is
  64. // expected to already exist (enforced by a DCHECK).
  65. VoteDataMap::iterator GetVoteData(const ExecutionContext* execution_context);
  66. // Returns the VoterType associated with |voter_id|.
  67. VoteData::VoterType GetVoterType(VoterId voter_id) const;
  68. // Our two input voters. We'll only accept votes from these voters otherwise
  69. // we'll DCHECK.
  70. VoterId override_voter_id_;
  71. VoterId default_voter_id_;
  72. // Our channel for upstreaming our votes.
  73. VotingChannel channel_;
  74. // Provides VotingChannels to our input voters.
  75. VotingChannelFactory voting_channel_factory_{this};
  76. // The votes we've upstreamed to our consumer.
  77. VoteDataMap vote_data_map_;
  78. };
  79. } // namespace execution_context_priority
  80. } // namespace performance_manager
  81. #endif // COMPONENTS_PERFORMANCE_MANAGER_EXECUTION_CONTEXT_PRIORITY_OVERRIDE_VOTE_AGGREGATOR_H_