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
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_PERFORMANCE_MANAGER_PERFORMANCE_MANAGER_REGISTRY_IMPL_H_
#define COMPONENTS_PERFORMANCE_MANAGER_PERFORMANCE_MANAGER_REGISTRY_IMPL_H_
#include <memory>
#include "base/containers/flat_map.h"
#include "base/containers/flat_set.h"
#include "base/memory/raw_ptr.h"
#include "base/observer_list.h"
#include "base/sequence_checker.h"
#include "components/performance_manager/browser_child_process_watcher.h"
#include "components/performance_manager/embedder/binders.h"
#include "components/performance_manager/embedder/performance_manager_registry.h"
#include "components/performance_manager/performance_manager_tab_helper.h"
#include "components/performance_manager/public/browser_child_process_host_id.h"
#include "components/performance_manager/render_process_user_data.h"
#include "components/performance_manager/tab_helper_frame_node_source.h"
#include "content/public/browser/render_process_host_creation_observer.h"
#include "third_party/blink/public/common/tokens/tokens.h"
namespace content {
class BrowserContext;
class RenderProcessHost;
class WebContents;
} // namespace content
namespace performance_manager {
class PerformanceManagerObserver;
class ServiceWorkerContextAdapterImpl;
class WorkerNodeImpl;
class WorkerWatcher;
class PerformanceManagerRegistryImpl
: public content::RenderProcessHostCreationObserver,
public PerformanceManagerRegistry,
public PerformanceManagerTabHelper::DestructionObserver,
public RenderProcessUserData::DestructionObserver {
public:
PerformanceManagerRegistryImpl();
~PerformanceManagerRegistryImpl() override;
PerformanceManagerRegistryImpl(const PerformanceManagerRegistryImpl&) =
delete;
void operator=(const PerformanceManagerRegistryImpl&) = delete;
// Returns the only instance of PerformanceManagerRegistryImpl living in this
// process, or nullptr if there is none.
static PerformanceManagerRegistryImpl* GetInstance();
// Adds / removes an observer that is notified when a PageNode is created on
// the main thread. Forwarded to from the public PerformanceManager interface.
void AddObserver(PerformanceManagerObserver* observer);
void RemoveObserver(PerformanceManagerObserver* observer);
// PerformanceManagerRegistry:
Binders& GetBinders() override;
void CreatePageNodeForWebContents(
content::WebContents* web_contents) override;
void SetPageType(content::WebContents* web_contents, PageType type) override;
void NotifyBrowserContextAdded(
content::BrowserContext* browser_context) override;
void NotifyBrowserContextRemoved(
content::BrowserContext* browser_context) override;
void CreateProcessNode(
content::RenderProcessHost* render_process_host) override;
void TearDown() override;
// PerformanceManagerTabHelper::DestructionObserver:
void OnPerformanceManagerTabHelperDestroying(
content::WebContents* web_contents) override;
// RenderProcessUserData::DestructionObserver:
void OnRenderProcessUserDataDestroying(
content::RenderProcessHost* render_process_host) override;
// This is exposed so that the tab helper can call it as well, as in some
// testing configurations we otherwise miss RPH creation notifications that
// usually arrive when interfaces are exposed to the renderer.
void EnsureProcessNodeForRenderProcessHost(
content::RenderProcessHost* render_process_host);
// Gets the ProcessNode for the browser process from the
// BrowserChildProcessWatcher.
ProcessNodeImpl* GetBrowserProcessNode();
// Gets the ProcessNode for the non-renderer child process with the given `id`
// from the BrowserChildProcessWatcher.
ProcessNodeImpl* GetBrowserChildProcessNode(BrowserChildProcessHostId id);
// Searches all WorkerWatchers for a WorkerNode matching the given `token`.
// Exposed so that accessors in performance_manager.h can look up WorkerNodes
// on the UI thread.
WorkerNodeImpl* FindWorkerNodeForToken(const blink::WorkerToken& token);
// Returns the WorkerWatcher for `browser_context`, or nullptr if there is
// none. Tests can call methods on the WorkerWatcher to simulate workers.
WorkerWatcher* GetWorkerWatcherForTesting(
content::BrowserContext* browser_context);
private:
friend class TestBrowserChildProcess;
friend void DeleteBrowserProcessNodeForTesting();
// Allow unit tests to register simulated child processes with
// BrowserChildProcessWatcher.
BrowserChildProcessWatcher& GetBrowserChildProcessWatcherForTesting();
// content::RenderProcessHostCreationObserver:
void OnRenderProcessHostCreated(content::RenderProcessHost* host) override;
SEQUENCE_CHECKER(sequence_checker_);
// Helper object to bind Mojo interfaces. Can be accessed on any sequence.
Binders binders_;
// Tracks WebContents and RenderProcessHost for which we have created user
// data. Used to destroy all user data when the registry is destroyed.
base::flat_set<raw_ptr<content::WebContents, CtnExperimental>> web_contents_
GUARDED_BY_CONTEXT(sequence_checker_);
base::flat_set<raw_ptr<content::RenderProcessHost, CtnExperimental>>
render_process_hosts_ GUARDED_BY_CONTEXT(sequence_checker_);
// Maps each browser context to its ServiceWorkerContextAdapterImpl.
base::flat_map<content::BrowserContext*,
std::unique_ptr<ServiceWorkerContextAdapterImpl>>
service_worker_context_adapters_ GUARDED_BY_CONTEXT(sequence_checker_);
// Maps each browser context to its worker watcher.
base::flat_map<content::BrowserContext*, std::unique_ptr<WorkerWatcher>>
worker_watchers_ GUARDED_BY_CONTEXT(sequence_checker_);
BrowserChildProcessWatcher browser_child_process_watcher_
GUARDED_BY_CONTEXT(sequence_checker_);
// Used by WorkerWatchers to access existing frame nodes.
performance_manager::TabHelperFrameNodeSource frame_node_source_
GUARDED_BY_CONTEXT(sequence_checker_);
base::ObserverList<PerformanceManagerObserver> observers_
GUARDED_BY_CONTEXT(sequence_checker_);
};
} // namespace performance_manager
#endif // COMPONENTS_PERFORMANCE_MANAGER_PERFORMANCE_MANAGER_REGISTRY_IMPL_H_
|