summaryrefslogtreecommitdiffstats
path: root/chromium/chrome/browser/ui/webui/discards/graph_dump_impl.h
blob: b973761978acac36030643bc69eb97613484d8e7 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright 2017 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.

#ifndef CHROME_BROWSER_UI_WEBUI_DISCARDS_GRAPH_DUMP_IMPL_H_
#define CHROME_BROWSER_UI_WEBUI_DISCARDS_GRAPH_DUMP_IMPL_H_

#include <memory>

#include "base/memory/ref_counted_memory.h"
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "chrome/browser/ui/webui/discards/discards.mojom.h"
#include "components/performance_manager/public/graph/frame_node.h"
#include "components/performance_manager/public/graph/graph.h"
#include "components/performance_manager/public/graph/page_node.h"
#include "components/performance_manager/public/graph/process_node.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"

// TODO(siggi): Add workers to the WebUI graph.
class DiscardsGraphDumpImpl : public discards::mojom::GraphDump,
                              public performance_manager::GraphOwned,
                              public performance_manager::FrameNodeObserver,
                              public performance_manager::PageNodeObserver,
                              public performance_manager::ProcessNodeObserver {
 public:
  DiscardsGraphDumpImpl();
  ~DiscardsGraphDumpImpl() override;

  // Creates a new DiscardsGraphDumpImpl to service |receiver| and passes its
  // ownership to |graph|.
  static void CreateAndBind(
      mojo::PendingReceiver<discards::mojom::GraphDump> receiver,
      performance_manager::Graph* graph);

  // Exposed for testing.
  void BindWithGraph(
      performance_manager::Graph* graph,
      mojo::PendingReceiver<discards::mojom::GraphDump> receiver);

 protected:
  // WebUIGraphDump implementation.
  void SubscribeToChanges(
      mojo::PendingRemote<discards::mojom::GraphChangeStream> change_subscriber)
      override;

  // GraphOwned implementation.
  void OnPassedToGraph(performance_manager::Graph* graph) override;
  void OnTakenFromGraph(performance_manager::Graph* graph) override;

  // FrameNodeObserver implementation:
  void OnFrameNodeAdded(
      const performance_manager::FrameNode* frame_node) override;
  void OnBeforeFrameNodeRemoved(
      const performance_manager::FrameNode* frame_node) override;
  // Ignored.
  void OnIsCurrentChanged(
      const performance_manager::FrameNode* frame_node) override {}
  // Ignored.
  void OnNetworkAlmostIdleChanged(
      const performance_manager::FrameNode* frame_node) override {}
  // Ignored.
  void OnFrameLifecycleStateChanged(
      const performance_manager::FrameNode* frame_node) override {}
  // Ignored.
  void OnOriginTrialFreezePolicyChanged(
      const performance_manager::FrameNode* frame_node,
      const InterventionPolicy& previous_value) override {}
  void OnURLChanged(const performance_manager::FrameNode* frame_node,
                    const GURL& previous_value) override;
  // Ignored.
  void OnIsAdFrameChanged(
      const performance_manager::FrameNode* frame_node) override {}
  // Ignored.
  void OnFrameIsHoldingWebLockChanged(
      const performance_manager::FrameNode* frame_node) override {}
  // Ignored.
  void OnFrameIsHoldingIndexedDBLockChanged(
      const performance_manager::FrameNode* frame_node) override {}
  // Ignored.
  void OnNonPersistentNotificationCreated(
      const performance_manager::FrameNode* frame_node) override {}
  // Ignored.
  void OnPriorityAndReasonChanged(
      const performance_manager::FrameNode* frame_node) override {}

  // PageNodeObserver implementation:
  void OnPageNodeAdded(const performance_manager::PageNode* page_node) override;
  void OnBeforePageNodeRemoved(
      const performance_manager::PageNode* page_node) override;
  void OnIsVisibleChanged(
      const performance_manager::PageNode* page_node) override {}  // Ignored.
  void OnIsAudibleChanged(
      const performance_manager::PageNode* page_node) override {}  // Ignored.
  void OnIsLoadingChanged(
      const performance_manager::PageNode* page_node) override {}  // Ignored.
  void OnUkmSourceIdChanged(
      const performance_manager::PageNode* page_node) override {}  // Ignored.
  // Ignored.
  void OnPageLifecycleStateChanged(
      const performance_manager::PageNode* page_node) override {}
  // Ignored.
  void OnPageOriginTrialFreezePolicyChanged(
      const performance_manager::PageNode* page_node) override {}
  // Ignored.
  void OnPageIsHoldingWebLockChanged(
      const performance_manager::PageNode* page_node) override {}
  // Ignored.
  void OnPageIsHoldingIndexedDBLockChanged(
      const performance_manager::PageNode* page_node) override {}
  void OnMainFrameUrlChanged(
      const performance_manager::PageNode* page_node) override;
  // Ignored.
  void OnPageAlmostIdleChanged(
      const performance_manager::PageNode* page_node) override {}
  void OnMainFrameDocumentChanged(
      const performance_manager::PageNode* page_node) override {}
  void OnTitleUpdated(const performance_manager::PageNode* page_node) override {
  }  // Ignored.
  void OnFaviconUpdated(
      const performance_manager::PageNode* page_node) override;

  // ProcessNodeObserver implementation:
  void OnProcessNodeAdded(
      const performance_manager::ProcessNode* process_node) override;
  void OnProcessLifetimeChange(
      const performance_manager::ProcessNode* process_node) override;
  void OnBeforeProcessNodeRemoved(
      const performance_manager::ProcessNode* process_node) override;
  void OnExpectedTaskQueueingDurationSample(
      const performance_manager::ProcessNode* process_node) override {
  }  // Ignored.
  // Ignored.
  void OnMainThreadTaskLoadIsLow(
      const performance_manager::ProcessNode* process_node) override {}
  // Ignored.
  void OnAllFramesInProcessFrozen(
      const performance_manager::ProcessNode* process_node) override {}

 private:
  // The favicon requests happen on the UI thread. This helper class
  // maintains the state required to do that.
  class FaviconRequestHelper;

  FaviconRequestHelper* EnsureFaviconRequestHelper();

  void StartPageFaviconRequest(const performance_manager::PageNode* page_node);
  void StartFrameFaviconRequest(
      const performance_manager::FrameNode* frame_node);

  void SendFrameNotification(const performance_manager::FrameNode* frame,
                             bool created);
  void SendPageNotification(const performance_manager::PageNode* page,
                            bool created);
  void SendProcessNotification(const performance_manager::ProcessNode* process,
                               bool created);
  void SendDeletionNotification(const performance_manager::Node* node);
  void SendFaviconNotification(
      int64_t serialization_id,
      scoped_refptr<base::RefCountedMemory> bitmap_data);

  static void BindOnPMSequence(
      mojo::PendingReceiver<discards::mojom::GraphDump> receiver,
      performance_manager::Graph* graph);
  static void OnConnectionError(DiscardsGraphDumpImpl* impl);

  performance_manager::Graph* graph_ = nullptr;

  std::unique_ptr<FaviconRequestHelper> favicon_request_helper_;

  // The current change subscriber to this dumper. This instance is subscribed
  // to every node in |graph_| save for the system node, so long as there is a
  // subscriber.
  mojo::Remote<discards::mojom::GraphChangeStream> change_subscriber_;
  mojo::Receiver<discards::mojom::GraphDump> receiver_{this};

  SEQUENCE_CHECKER(sequence_checker_);

  base::WeakPtrFactory<DiscardsGraphDumpImpl> weak_factory_{this};

  DISALLOW_COPY_AND_ASSIGN(DiscardsGraphDumpImpl);
};

#endif  // CHROME_BROWSER_UI_WEBUI_DISCARDS_GRAPH_DUMP_IMPL_H_