Commit 70cc5275 authored by Ulan Degenbaev's avatar Ulan Degenbaev Committed by Commit Bot

Add PerformanceManager to content_shell

A new web API called performance.measureMemory is implemented using
PerformanceManager. Since web platform tests run in a content_shell,
we need to add and enable PerformanceManager there.

This CL is based on a patch by Joe Mason.

Bug: 1085129
Change-Id: Iefeafbd7a12245d4ca246f3ee28dc3e5292d37e8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2426587
Commit-Queue: Ulan Degenbaev <ulan@chromium.org>
Reviewed-by: default avatarChris Hamilton <chrisha@chromium.org>
Reviewed-by: default avatarMike West <mkwst@chromium.org>
Cr-Commit-Position: refs/heads/master@{#812089}
parent 9e29191f
......@@ -8,6 +8,7 @@
#include <memory>
#include "base/callback_forward.h"
#include "components/performance_manager/embedder/performance_manager_registry.h"
#include "components/performance_manager/public/performance_manager.h"
namespace performance_manager {
......@@ -16,6 +17,20 @@ class Graph;
using GraphCreatedCallback = base::OnceCallback<void(Graph*)>;
enum class Decorators { kNone, kDefault };
// A helper class that manages the lifetime of PerformanceManager
// and PerformanceManagerRegistry.
class PerformanceManagerLifetime {
public:
PerformanceManagerLifetime(Decorators, GraphCreatedCallback);
~PerformanceManagerLifetime();
private:
std::unique_ptr<PerformanceManager> performance_manager_;
std::unique_ptr<PerformanceManagerRegistry> performance_manager_registry_;
};
// Creates a PerformanceManager with default decorators.
// |graph_created_callback| is invoked on the PM sequence once the Graph is
// created.
......
......@@ -5,6 +5,7 @@
#include "components/performance_manager/embedder/performance_manager_lifetime.h"
#include "base/bind.h"
#include "base/notreached.h"
#include "build/build_config.h"
#include "components/performance_manager/decorators/page_load_tracker_decorator.h"
#include "components/performance_manager/execution_context/execution_context_registry_impl.h"
......@@ -43,13 +44,49 @@ void DefaultGraphCreatedCallback(
std::move(external_graph_created_callback).Run(graph);
}
void NullGraphCreatedCallback(
GraphCreatedCallback external_graph_created_callback,
GraphImpl* graph) {
std::move(external_graph_created_callback).Run(graph);
}
base::OnceCallback<void(GraphImpl*)> AddDecorators(
Decorators decorators,
GraphCreatedCallback graph_created_callback) {
switch (decorators) {
case Decorators::kNone:
return base::BindOnce(&NullGraphCreatedCallback,
std::move(graph_created_callback));
case Decorators::kDefault:
return base::BindOnce(&DefaultGraphCreatedCallback,
std::move(graph_created_callback));
}
NOTREACHED();
return {};
}
} // namespace
PerformanceManagerLifetime::PerformanceManagerLifetime(
Decorators decorators,
GraphCreatedCallback graph_created_callback)
: performance_manager_(PerformanceManagerImpl::Create(
AddDecorators(decorators, std::move(graph_created_callback)))),
performance_manager_registry_(
performance_manager::PerformanceManagerRegistry::Create()) {}
PerformanceManagerLifetime::~PerformanceManagerLifetime() {
performance_manager_registry_->TearDown();
performance_manager_registry_.reset();
performance_manager::DestroyPerformanceManager(
std::move(performance_manager_));
}
std::unique_ptr<PerformanceManager>
CreatePerformanceManagerWithDefaultDecorators(
GraphCreatedCallback graph_created_callback) {
return PerformanceManagerImpl::Create(base::BindOnce(
&DefaultGraphCreatedCallback, std::move(graph_created_callback)));
return PerformanceManagerImpl::Create(
AddDecorators(Decorators::kDefault, std::move(graph_created_callback)));
}
void DestroyPerformanceManager(std::unique_ptr<PerformanceManager> instance) {
......
......@@ -376,9 +376,6 @@ void PerformanceManagerTabHelper::InnerWebContentsAttached(
// Determine the opened type.
auto opened_type = PageNode::OpenedType::kInvalid;
if (inner_web_contents->IsPortal()) {
// Portals don't have openers.
DCHECK(!inner_web_contents->HasOpener() &&
!inner_web_contents->HasOriginalOpener());
opened_type = PageNode::OpenedType::kPortal;
// In the case of portals there can be a temporary RFH that is created that
......@@ -398,7 +395,16 @@ void PerformanceManagerTabHelper::InnerWebContentsAttached(
// severed.
}
DCHECK_NE(PageNode::OpenedType::kInvalid, opened_type);
DCHECK(frame);
if (!frame) {
DCHECK(!render_frame_host->IsRenderFrameCreated());
DCHECK(!inner_web_contents->IsPortal());
// TODO(crbug.com/1133361):
// WebContentsImplBrowserTest.AttachNestedInnerWebContents calls
// WebContents::AttachInnerWebContents without creating RenderFrame.
// Removing this conditional once either the test is fixed or this function
// is adjusted to handle the case without the render frame.
return;
}
PerformanceManagerImpl::CallOnGraphImpl(
FROM_HERE, base::BindOnce(&PageNodeImpl::SetOpenerFrameNodeAndOpenedType,
......
......@@ -6,7 +6,6 @@
#include "base/bind_helpers.h"
#include "base/run_loop.h"
#include "components/performance_manager/embedder/performance_manager_registry.h"
#include "content/public/common/content_switches.h"
#include "content/shell/browser/shell.h"
#include "content/shell/browser/shell_content_browser_client.h"
......@@ -18,10 +17,6 @@
namespace performance_manager {
PerformanceManagerBrowserTestHarness::PerformanceManagerBrowserTestHarness() {
helper_ = std::make_unique<PerformanceManagerTestHarnessHelper>();
}
PerformanceManagerBrowserTestHarness::~PerformanceManagerBrowserTestHarness() =
default;
......@@ -35,11 +30,6 @@ void PerformanceManagerBrowserTestHarness::PreRunTestOnMainThread() {
ASSERT_TRUE(embedded_test_server()->Start());
}
void PerformanceManagerBrowserTestHarness::PostRunTestOnMainThread() {
helper_->TearDown();
Super::PostRunTestOnMainThread();
}
void PerformanceManagerBrowserTestHarness::SetUpCommandLine(
base::CommandLine* command_line) {
// Ensure the PM logic is enabled in renderers.
......@@ -47,43 +37,6 @@ void PerformanceManagerBrowserTestHarness::SetUpCommandLine(
"PerformanceManagerInstrumentation");
}
// We're a full embedder of the PM, so we have to wire up all of the embedder
// hooks. Note that this runs *before* PreRunTestOnMainThread.
void PerformanceManagerBrowserTestHarness::CreatedBrowserMainParts(
content::BrowserMainParts* browser_main_parts) {
helper_->SetUp();
content::ShellContentBrowserClient::Get()
->set_web_contents_view_delegate_callback(
base::BindRepeating([](content::WebContents* contents)
-> content::WebContentsViewDelegate* {
PerformanceManagerRegistry::GetInstance()
->MaybeCreatePageNodeForWebContents(contents);
return content::CreateShellWebContentsViewDelegate(contents);
}));
// Expose interfaces to RenderProcess.
content::ShellContentBrowserClient::Get()
->set_expose_interfaces_to_renderer_callback(base::BindRepeating(
[](service_manager::BinderRegistry* registry,
blink::AssociatedInterfaceRegistry* associated_registry_unused,
content::RenderProcessHost* render_process_host) {
PerformanceManagerRegistry::GetInstance()
->CreateProcessNodeAndExposeInterfacesToRendererProcess(
registry, render_process_host);
}));
// Expose interfaces to RenderFrame.
content::ShellContentBrowserClient::Get()
->set_register_browser_interface_binders_for_frame_callback(
base::BindRepeating(
[](content::RenderFrameHost* render_frame_host,
mojo::BinderMapWithContext<content::RenderFrameHost*>* map) {
PerformanceManagerRegistry::GetInstance()
->ExposeInterfacesToRenderFrame(map);
}));
}
content::Shell* PerformanceManagerBrowserTestHarness::CreateShell() {
content::Shell* shell = CreateBrowser();
return shell;
......
......@@ -5,7 +5,6 @@
#ifndef COMPONENTS_PERFORMANCE_MANAGER_TEST_SUPPORT_PERFORMANCE_MANAGER_BROWSERTEST_HARNESS_H_
#define COMPONENTS_PERFORMANCE_MANAGER_TEST_SUPPORT_PERFORMANCE_MANAGER_BROWSERTEST_HARNESS_H_
#include "components/performance_manager/test_support/test_harness_helper.h"
#include "content/public/test/content_browser_test.h"
namespace performance_manager {
......@@ -18,7 +17,7 @@ class PerformanceManagerBrowserTestHarness
using Super = content::ContentBrowserTest;
public:
PerformanceManagerBrowserTestHarness();
PerformanceManagerBrowserTestHarness() = default;
PerformanceManagerBrowserTestHarness(
const PerformanceManagerBrowserTestHarness&) = delete;
PerformanceManagerBrowserTestHarness& operator=(
......@@ -27,10 +26,7 @@ class PerformanceManagerBrowserTestHarness
// content::BrowserTestBase:
void PreRunTestOnMainThread() override;
void PostRunTestOnMainThread() override;
void SetUpCommandLine(base::CommandLine* command_line) override;
void CreatedBrowserMainParts(
content::BrowserMainParts* browser_main_parts) override;
// Creates a content shell with its own window, hosting a single tab that is
// navigated to about:blank. The WebContents will have the PM helpers
......@@ -44,9 +40,6 @@ class PerformanceManagerBrowserTestHarness
// Waits for an ongoing navigation to terminate on the given |contents|.
void WaitForLoad(content::WebContents* contents);
private:
std::unique_ptr<PerformanceManagerTestHarnessHelper> helper_;
};
} // namespace performance_manager
......
......@@ -217,6 +217,7 @@ static_library("content_shell_lib") {
"//components/cdm/renderer",
"//components/keyed_service/content",
"//components/network_session_configurator/common",
"//components/performance_manager",
"//components/services/storage/test_api",
"//components/url_formatter",
"//components/web_cache/renderer",
......
......@@ -33,6 +33,7 @@ include_rules = [
"+components/crash",
"+components/download",
"+components/keyed_service/core",
"+components/performance_manager",
"+components/url_formatter",
"+components/network_session_configurator/browser",
"+components/viz/common/resources",
......
......@@ -4,8 +4,11 @@
#include "content/shell/browser/shell_browser_main_parts.h"
#include <utility>
#include "base/base_switches.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
......@@ -14,6 +17,7 @@
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#include "cc/base/switches.h"
#include "components/performance_manager/embedder/performance_manager_lifetime.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/storage_partition.h"
......@@ -190,6 +194,12 @@ int ShellBrowserMainParts::PreCreateThreads() {
return 0;
}
void ShellBrowserMainParts::PostCreateThreads() {
performance_manager_lifetime_ =
std::make_unique<performance_manager::PerformanceManagerLifetime>(
performance_manager::Decorators::kNone, base::DoNothing());
}
void ShellBrowserMainParts::PreMainMessageLoopRun() {
InitializeBrowserContexts();
Shell::Initialize(CreateShellPlatformDelegate());
......@@ -215,6 +225,7 @@ void ShellBrowserMainParts::PostMainMessageLoopRun() {
#if BUILDFLAG(USE_GTK)
views::LinuxUI::SetInstance(nullptr);
#endif
performance_manager_lifetime_.reset();
}
void ShellBrowserMainParts::PreDefaultMainMessageLoopRun(
......
......@@ -15,6 +15,10 @@
#include "content/shell/browser/shell_browser_context.h"
#include "ui/base/buildflags.h"
namespace performance_manager {
class PerformanceManagerLifetime;
} // namespace performance_manager
#if BUILDFLAG(USE_GTK)
namespace ui {
class GtkUiDelegate;
......@@ -32,6 +36,7 @@ class ShellBrowserMainParts : public BrowserMainParts {
// BrowserMainParts overrides.
int PreEarlyInitialization() override;
int PreCreateThreads() override;
void PostCreateThreads() override;
void PreMainMessageLoopStart() override;
void PostMainMessageLoopStart() override;
void ToolkitInitialized() override;
......@@ -73,6 +78,9 @@ class ShellBrowserMainParts : public BrowserMainParts {
std::unique_ptr<ui::GtkUiDelegate> gtk_ui_delegate_;
#endif
std::unique_ptr<performance_manager::PerformanceManagerLifetime>
performance_manager_lifetime_;
DISALLOW_COPY_AND_ASSIGN(ShellBrowserMainParts);
};
......
......@@ -20,6 +20,7 @@
#include "base/strings/utf_string_conversions.h"
#include "base/threading/sequence_local_storage_slot.h"
#include "build/build_config.h"
#include "components/performance_manager/embedder/performance_manager_registry.h"
#include "content/public/browser/client_certificate_delegate.h"
#include "content/public/browser/login_delegate.h"
#include "content/public/browser/navigation_throttle.h"
......@@ -248,8 +249,8 @@ std::string ShellContentBrowserClient::GetDefaultDownloadName() {
WebContentsViewDelegate* ShellContentBrowserClient::GetWebContentsViewDelegate(
WebContents* web_contents) {
if (web_contents_view_delegate_callback_)
return web_contents_view_delegate_callback_.Run(web_contents);
performance_manager::PerformanceManagerRegistry::GetInstance()
->MaybeCreatePageNodeForWebContents(web_contents);
return CreateShellWebContentsViewDelegate(web_contents);
}
......@@ -310,10 +311,9 @@ void ShellContentBrowserClient::ExposeInterfacesToRenderer(
service_manager::BinderRegistry* registry,
blink::AssociatedInterfaceRegistry* associated_registry,
RenderProcessHost* render_process_host) {
if (expose_interfaces_to_renderer_callback_) {
expose_interfaces_to_renderer_callback_.Run(registry, associated_registry,
render_process_host);
}
performance_manager::PerformanceManagerRegistry::GetInstance()
->CreateProcessNodeAndExposeInterfacesToRendererProcess(
registry, render_process_host);
}
mojo::Remote<::media::mojom::MediaService>
......@@ -332,9 +332,8 @@ ShellContentBrowserClient::RunSecondaryMediaService() {
void ShellContentBrowserClient::RegisterBrowserInterfaceBindersForFrame(
RenderFrameHost* render_frame_host,
mojo::BinderMapWithContext<RenderFrameHost*>* map) {
if (register_browser_interface_binders_for_frame_callback_)
register_browser_interface_binders_for_frame_callback_.Run(
render_frame_host, map);
performance_manager::PerformanceManagerRegistry::GetInstance()
->ExposeInterfacesToRenderFrame(map);
}
void ShellContentBrowserClient::OpenURL(
......
......@@ -121,12 +121,6 @@ class ShellContentBrowserClient : public ContentBrowserClient {
}
// Used for content_browsertests.
void set_web_contents_view_delegate_callback(
base::RepeatingCallback<WebContentsViewDelegate*(WebContents*)>
web_contents_view_delegate_callback) {
web_contents_view_delegate_callback_ =
std::move(web_contents_view_delegate_callback);
}
void set_select_client_certificate_callback(
base::OnceClosure select_client_certificate_callback) {
select_client_certificate_callback_ =
......@@ -148,23 +142,6 @@ class ShellContentBrowserClient : public ContentBrowserClient {
url_loader_factory_params_callback_ =
std::move(url_loader_factory_params_callback);
}
void set_expose_interfaces_to_renderer_callback(
base::RepeatingCallback<
void(service_manager::BinderRegistry* registry,
blink::AssociatedInterfaceRegistry* associated_registry,
RenderProcessHost* render_process_host)>
expose_interfaces_to_renderer_callback) {
expose_interfaces_to_renderer_callback_ =
expose_interfaces_to_renderer_callback;
}
void set_register_browser_interface_binders_for_frame_callback(
base::RepeatingCallback<
void(RenderFrameHost* render_frame_host,
mojo::BinderMapWithContext<RenderFrameHost*>* map)>
register_browser_interface_binders_for_frame_callback) {
register_browser_interface_binders_for_frame_callback_ =
register_browser_interface_binders_for_frame_callback;
}
void set_create_throttles_for_navigation_callback(
base::RepeatingCallback<std::vector<std::unique_ptr<NavigationThrottle>>(
NavigationHandle*)> create_throttles_for_navigation_callback) {
......@@ -195,8 +172,6 @@ class ShellContentBrowserClient : public ContentBrowserClient {
private:
static bool allow_any_cors_exempt_header_for_browser_;
base::RepeatingCallback<WebContentsViewDelegate*(WebContents*)>
web_contents_view_delegate_callback_;
base::OnceClosure select_client_certificate_callback_;
base::OnceCallback<bool(const service_manager::Identity&)>
should_terminate_on_service_quit_callback_;
......@@ -205,15 +180,6 @@ class ShellContentBrowserClient : public ContentBrowserClient {
const url::Origin&,
bool is_for_isolated_world)>
url_loader_factory_params_callback_;
base::RepeatingCallback<void(
service_manager::BinderRegistry* registry,
blink::AssociatedInterfaceRegistry* associated_registry,
RenderProcessHost* render_process_host)>
expose_interfaces_to_renderer_callback_;
base::RepeatingCallback<void(
RenderFrameHost* render_frame_host,
mojo::BinderMapWithContext<RenderFrameHost*>* map)>
register_browser_interface_binders_for_frame_callback_;
base::RepeatingCallback<std::vector<std::unique_ptr<NavigationThrottle>>(
NavigationHandle*)>
create_throttles_for_navigation_callback_;
......
......@@ -387,6 +387,8 @@ bool WebTestContentBrowserClient::CanCreateWindow(
void WebTestContentBrowserClient::RegisterBrowserInterfaceBindersForFrame(
RenderFrameHost* render_frame_host,
mojo::BinderMapWithContext<content::RenderFrameHost*>* map) {
ShellContentBrowserClient::RegisterBrowserInterfaceBindersForFrame(
render_frame_host, map);
map->Add<mojom::MojoWebTestHelper>(base::BindRepeating(&BindWebTestHelper));
map->Add<blink::mojom::ClipboardHost>(base::BindRepeating(
&WebTestContentBrowserClient::BindClipboardHost, base::Unretained(this)));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment