root_vote_observer.cc 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. #include "components/performance_manager/execution_context_priority/root_vote_observer.h"
  5. #include "components/performance_manager/graph/frame_node_impl.h"
  6. #include "components/performance_manager/graph/worker_node_impl.h"
  7. #include "components/performance_manager/public/execution_context/execution_context.h"
  8. namespace performance_manager {
  9. namespace execution_context_priority {
  10. namespace {
  11. // Sets the priority of an execution context.
  12. void SetPriorityAndReason(
  13. const execution_context::ExecutionContext* execution_context,
  14. const PriorityAndReason& priority_and_reason) {
  15. switch (execution_context->GetType()) {
  16. case execution_context::ExecutionContextType::kFrameNode:
  17. FrameNodeImpl::FromNode(execution_context->GetFrameNode())
  18. ->SetPriorityAndReason(priority_and_reason);
  19. break;
  20. case execution_context::ExecutionContextType::kWorkerNode:
  21. WorkerNodeImpl::FromNode(execution_context->GetWorkerNode())
  22. ->SetPriorityAndReason(priority_and_reason);
  23. break;
  24. }
  25. }
  26. } // namespace
  27. RootVoteObserver::RootVoteObserver() = default;
  28. RootVoteObserver::~RootVoteObserver() = default;
  29. VotingChannel RootVoteObserver::GetVotingChannel() {
  30. DCHECK_EQ(0u, voting_channel_factory_.voting_channels_issued());
  31. auto channel = voting_channel_factory_.BuildVotingChannel();
  32. voter_id_ = channel.voter_id();
  33. return channel;
  34. }
  35. void RootVoteObserver::OnVoteSubmitted(
  36. VoterId voter_id,
  37. const ExecutionContext* execution_context,
  38. const Vote& vote) {
  39. DCHECK_EQ(voter_id_, voter_id);
  40. SetPriorityAndReason(execution_context,
  41. PriorityAndReason(vote.value(), vote.reason()));
  42. }
  43. void RootVoteObserver::OnVoteChanged(VoterId voter_id,
  44. const ExecutionContext* execution_context,
  45. const Vote& new_vote) {
  46. DCHECK_EQ(voter_id_, voter_id);
  47. SetPriorityAndReason(execution_context,
  48. PriorityAndReason(new_vote.value(), new_vote.reason()));
  49. }
  50. void RootVoteObserver::OnVoteInvalidated(
  51. VoterId voter_id,
  52. const ExecutionContext* execution_context) {
  53. DCHECK_EQ(voter_id_, voter_id);
  54. SetPriorityAndReason(
  55. execution_context,
  56. PriorityAndReason(base::TaskPriority::LOWEST,
  57. FrameNodeImpl::kDefaultPriorityReason));
  58. }
  59. } // namespace execution_context_priority
  60. } // namespace performance_manager