summaryrefslogtreecommitdiff
path: root/chromium/components/performance_manager/decorators/tab_properties_decorator.cc
blob: b6131de21c85c2b8368848080bf9098f61ba7565 (plain)
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
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/performance_manager/public/decorators/tab_properties_decorator.h"

#include "components/performance_manager/decorators/decorators_utils.h"
#include "components/performance_manager/graph/node_attached_data_impl.h"
#include "components/performance_manager/graph/page_node_impl.h"
#include "components/performance_manager/public/graph/node_data_describer_registry.h"
#include "components/performance_manager/public/performance_manager.h"
#include "content/public/browser/browser_thread.h"

namespace performance_manager {

namespace {

class TabPropertiesDataImpl
    : public TabPropertiesDecorator::Data,
      public NodeAttachedDataImpl<TabPropertiesDataImpl> {
 public:
  struct Traits : public NodeAttachedDataInMap<PageNodeImpl> {};
  ~TabPropertiesDataImpl() override = default;
  TabPropertiesDataImpl(const TabPropertiesDataImpl& other) = delete;
  TabPropertiesDataImpl& operator=(const TabPropertiesDataImpl&) = delete;

  // TabPropertiesDecorator::Data implementation.
  bool IsInTabStrip() const override { return is_tab_; }

  void set_is_tab(bool is_tab) { is_tab_ = is_tab; }

 private:
  // Make the impl our friend so it can access the constructor and any
  // storage providers.
  friend class ::performance_manager::NodeAttachedDataImpl<
      TabPropertiesDataImpl>;

  explicit TabPropertiesDataImpl(const PageNodeImpl* page_node) {}

  bool is_tab_ = false;
};

}  // namespace

void TabPropertiesDecorator::SetIsTab(content::WebContents* contents,
                                      bool is_tab) {
  SetPropertyForWebContentsPageNode(contents,
                                    &TabPropertiesDataImpl::set_is_tab, is_tab);
}

void TabPropertiesDecorator::SetIsTabForTesting(PageNode* page_node,
                                                bool is_tab) {
  auto* data =
      TabPropertiesDataImpl::GetOrCreate(PageNodeImpl::FromNode(page_node));
  DCHECK(data);
  data->set_is_tab(is_tab);
}

void TabPropertiesDecorator::OnPassedToGraph(Graph* graph) {
  graph->GetNodeDataDescriberRegistry()->RegisterDescriber(this,
                                                           "TabPropertiesDecorator");
}

void TabPropertiesDecorator::OnTakenFromGraph(Graph* graph) {
  graph->GetNodeDataDescriberRegistry()->UnregisterDescriber(this);
}

base::Value TabPropertiesDecorator::DescribePageNodeData(
    const PageNode* node) const {
  auto* data = TabPropertiesDecorator::Data::FromPageNode(node);
  if (!data)
    return base::Value();

  base::Value ret(base::Value::Type::DICTIONARY);
  ret.SetBoolKey("IsInTabStrip", data->IsInTabStrip());

  return ret;
}

TabPropertiesDecorator::Data::Data() = default;
TabPropertiesDecorator::Data::~Data() = default;

const TabPropertiesDecorator::Data* TabPropertiesDecorator::Data::FromPageNode(
    const PageNode* page_node) {
  return TabPropertiesDataImpl::Get(PageNodeImpl::FromNode(page_node));
}

TabPropertiesDecorator::Data*
TabPropertiesDecorator::Data::GetOrCreateForTesting(const PageNode* page_node) {
  return TabPropertiesDataImpl::GetOrCreate(PageNodeImpl::FromNode(page_node));
}

}  // namespace performance_manager