Commit b63393ac authored by Ryan Powell's avatar Ryan Powell Committed by Commit Bot

[PM] Add function CallOnGraphAndReplyWithResult to performance manager.

Adds a function, CallOnGraphAndReplyWithResult, to the performance
manager api. This function allows you to post a callback that will be
run on the PM sequence, and be provided a pointer to the Graph. The
return value of that callback is passed back as an argument to the reply
callback.

Bug: 980965
Change-Id: Icf1abc0d738bd1edd8ff2f50d5dba824fa861853
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1773961
Commit-Queue: Ryan Powell <ryanpow@google.com>
Reviewed-by: default avatarSébastien Marchand <sebmarchand@chromium.org>
Reviewed-by: default avatarFrançois Doray <fdoray@chromium.org>
Cr-Commit-Position: refs/heads/master@{#692088}
parent 568a975f
......@@ -11,9 +11,11 @@
#include <vector>
#include "base/callback.h"
#include "base/callback_helpers.h"
#include "base/location.h"
#include "base/sequence_checker.h"
#include "base/sequenced_task_runner.h"
#include "base/task_runner_util.h"
#include "chrome/browser/performance_manager/graph/graph_impl.h"
#include "chrome/browser/performance_manager/public/graph/worker_node.h"
#include "chrome/browser/performance_manager/public/render_process_host_proxy.h"
......@@ -57,6 +59,16 @@ class PerformanceManager {
static void CallOnGraph(const base::Location& from_here,
GraphCallback graph_callback);
// Posts a callback that will run on the PM sequence, and be provided a
// pointer to the Graph. Valid to be called from the main thread only, and
// only if "IsAvailable" returns true. The return value is returned as an
// argument to the reply callback.
template <typename TaskReturnType>
void CallOnGraphAndReplyWithResult(
const base::Location& from_here,
base::OnceCallback<TaskReturnType(GraphImpl*)> task,
base::OnceCallback<void(TaskReturnType)> reply);
// Passes a GraphOwned object into the Graph on the PM sequence. Should only
// be called from the main thread and only if "IsAvailable" returns true.
// TODO(chrisha): Move this to the public interface.
......@@ -150,6 +162,10 @@ class PerformanceManager {
void OnStartImpl(std::unique_ptr<service_manager::Connector> connector);
void CallOnGraphImpl(GraphCallback graph_callback);
template <typename TaskReturnType>
TaskReturnType CallOnGraphAndReplyWithResultImpl(
base::OnceCallback<TaskReturnType(GraphImpl*)> task);
// The performance task runner.
const scoped_refptr<base::SequencedTaskRunner> task_runner_;
GraphImpl graph_;
......@@ -158,7 +174,6 @@ class PerformanceManager {
// TODO(siggi): This no longer needs to go through mojo.
std::unique_ptr<ukm::MojoUkmRecorder> ukm_recorder_;
SEQUENCE_CHECKER(sequence_checker_);
DISALLOW_COPY_AND_ASSIGN(PerformanceManager);
......@@ -169,6 +184,26 @@ void PerformanceManager::DeleteNode(std::unique_ptr<NodeType> node) {
PostDeleteNode(std::move(node));
}
template <typename TaskReturnType>
void PerformanceManager::CallOnGraphAndReplyWithResult(
const base::Location& from_here,
base::OnceCallback<TaskReturnType(GraphImpl*)> task,
base::OnceCallback<void(TaskReturnType)> reply) {
auto* pm = GetInstance();
base::PostTaskAndReplyWithResult(
pm->task_runner_.get(), from_here,
base::BindOnce(&PerformanceManager::CallOnGraphAndReplyWithResultImpl<
TaskReturnType>,
base::Unretained(pm), std::move(task)),
std::move(reply));
}
template <typename TaskReturnType>
TaskReturnType PerformanceManager::CallOnGraphAndReplyWithResultImpl(
base::OnceCallback<TaskReturnType(GraphImpl*)> task) {
return std::move(task).Run(&graph_);
}
} // namespace performance_manager
#endif // CHROME_BROWSER_PERFORMANCE_MANAGER_PERFORMANCE_MANAGER_H_
......@@ -142,4 +142,36 @@ TEST_F(PerformanceManagerTest, CallOnGraph) {
performance_manager()->DeleteNode(std::move(page_node));
}
TEST_F(PerformanceManagerTest, CallOnGraphAndReplyWithResult) {
// Create a page node for something to target.
std::unique_ptr<PageNodeImpl> page_node =
performance_manager()->CreatePageNode(WebContentsProxy(), std::string(),
false, false);
base::RunLoop run_loop;
EXPECT_FALSE(performance_manager()->OnPMTaskRunnerForTesting());
base::OnceCallback<int(GraphImpl*)> task =
base::BindLambdaForTesting([&](GraphImpl* graph) {
EXPECT_TRUE(
PerformanceManager::GetInstance()->OnPMTaskRunnerForTesting());
EXPECT_EQ(page_node.get()->graph(), graph);
return 1;
});
bool reply_called = false;
base::OnceCallback<void(int)> reply = base::BindLambdaForTesting([&](int i) {
EXPECT_EQ(i, 1);
reply_called = true;
std::move(run_loop.QuitClosure()).Run();
});
performance_manager()->CallOnGraphAndReplyWithResult(
FROM_HERE, std::move(task), std::move(reply));
run_loop.Run();
performance_manager()->DeleteNode(std::move(page_node));
EXPECT_TRUE(reply_called);
}
} // namespace performance_manager
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