frame_visibility_voter.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2020 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/frame_visibility_voter.h"
  5. #include <utility>
  6. #include "components/performance_manager/public/execution_context/execution_context_registry.h"
  7. #include "url/gurl.h"
  8. namespace performance_manager {
  9. namespace execution_context_priority {
  10. namespace {
  11. const execution_context::ExecutionContext* GetExecutionContext(
  12. const FrameNode* frame_node) {
  13. return execution_context::ExecutionContextRegistry::GetFromGraph(
  14. frame_node->GetGraph())
  15. ->GetExecutionContextForFrameNode(frame_node);
  16. }
  17. // Returns a vote with the appropriate priority depending on the frame's
  18. // |visibility|.
  19. Vote GetVote(FrameNode::Visibility visibility) {
  20. base::TaskPriority priority;
  21. switch (visibility) {
  22. case FrameNode::Visibility::kUnknown:
  23. priority = base::TaskPriority::USER_VISIBLE;
  24. break;
  25. case FrameNode::Visibility::kVisible:
  26. priority = base::TaskPriority::USER_VISIBLE;
  27. break;
  28. case FrameNode::Visibility::kNotVisible:
  29. priority = base::TaskPriority::LOWEST;
  30. break;
  31. }
  32. return Vote(priority, FrameVisibilityVoter::kFrameVisibilityReason);
  33. }
  34. } // namespace
  35. // static
  36. const char FrameVisibilityVoter::kFrameVisibilityReason[] = "Frame visibility.";
  37. FrameVisibilityVoter::FrameVisibilityVoter() = default;
  38. FrameVisibilityVoter::~FrameVisibilityVoter() = default;
  39. void FrameVisibilityVoter::SetVotingChannel(VotingChannel voting_channel) {
  40. voting_channel_ = std::move(voting_channel);
  41. }
  42. void FrameVisibilityVoter::OnFrameNodeAdded(const FrameNode* frame_node) {
  43. const Vote vote = GetVote(frame_node->GetVisibility());
  44. voting_channel_.SubmitVote(GetExecutionContext(frame_node), vote);
  45. }
  46. void FrameVisibilityVoter::OnBeforeFrameNodeRemoved(
  47. const FrameNode* frame_node) {
  48. voting_channel_.InvalidateVote(GetExecutionContext(frame_node));
  49. }
  50. void FrameVisibilityVoter::OnFrameVisibilityChanged(
  51. const FrameNode* frame_node,
  52. FrameNode::Visibility previous_value) {
  53. const Vote new_vote = GetVote(frame_node->GetVisibility());
  54. // Nothing to change if the new priority is the same as the old one.
  55. if (new_vote == GetVote(previous_value))
  56. return;
  57. voting_channel_.ChangeVote(GetExecutionContext(frame_node), new_vote);
  58. }
  59. } // namespace execution_context_priority
  60. } // namespace performance_manager