Commit 342162af authored by Wanchang Ryu's avatar Wanchang Ryu Committed by Commit Bot

Clear pending BackgroundTracingAgentClientImpl ctor

When renderer process is created, BackgroundTracingManagerImpl creates
closure for constructing of BackgroundTracingAgentClientImpl and creates
its instance lazily only when any agent observers are registered.
But it never releases if none of agent observer exists so it remained
as leaked memory.

To release pending constructor, BackgroundTracingAgentClientImpl
registers a disconnect handler to BackgroundTracingAgentProvider
then it releases when it is disconnected.
BackgroundTracingAgentProvider would be disconnected when the process
is gone.

Change-Id: I880c31caa7731dd3429c089ff523da5f846af00c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2065195
Commit-Queue: Darin Fisher <darin@chromium.org>
Reviewed-by: default avatarDarin Fisher <darin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#745142}
parent ddaaa979
...@@ -16,15 +16,12 @@ namespace content { ...@@ -16,15 +16,12 @@ namespace content {
// static // static
void BackgroundTracingAgentClientImpl::Create( void BackgroundTracingAgentClientImpl::Create(
int child_process_id, int child_process_id,
mojo::PendingRemote<tracing::mojom::BackgroundTracingAgentProvider> mojo::Remote<tracing::mojom::BackgroundTracingAgentProvider> provider) {
pending_provider) {
mojo::PendingRemote<tracing::mojom::BackgroundTracingAgentClient> client; mojo::PendingRemote<tracing::mojom::BackgroundTracingAgentClient> client;
auto client_receiver = client.InitWithNewPipeAndPassReceiver(); auto client_receiver = client.InitWithNewPipeAndPassReceiver();
mojo::Remote<tracing::mojom::BackgroundTracingAgent> agent; mojo::Remote<tracing::mojom::BackgroundTracingAgent> agent;
mojo::Remote<tracing::mojom::BackgroundTracingAgentProvider> provider(
std::move(pending_provider));
provider->Create(ChildProcessHostImpl::ChildProcessUniqueIdToTracingProcessId( provider->Create(ChildProcessHostImpl::ChildProcessUniqueIdToTracingProcessId(
child_process_id), child_process_id),
std::move(client), agent.BindNewPipeAndPassReceiver()); std::move(client), agent.BindNewPipeAndPassReceiver());
......
...@@ -19,8 +19,7 @@ class BackgroundTracingAgentClientImpl ...@@ -19,8 +19,7 @@ class BackgroundTracingAgentClientImpl
public: public:
static void Create( static void Create(
int child_process_id, int child_process_id,
mojo::PendingRemote<tracing::mojom::BackgroundTracingAgentProvider> mojo::Remote<tracing::mojom::BackgroundTracingAgentProvider> provider);
pending_provider);
~BackgroundTracingAgentClientImpl() override; ~BackgroundTracingAgentClientImpl() override;
......
...@@ -70,14 +70,9 @@ void BackgroundTracingManagerImpl::ActivateForProcess( ...@@ -70,14 +70,9 @@ void BackgroundTracingManagerImpl::ActivateForProcess(
child_process->GetBackgroundTracingAgentProvider( child_process->GetBackgroundTracingAgentProvider(
pending_provider.InitWithNewPipeAndPassReceiver()); pending_provider.InitWithNewPipeAndPassReceiver());
auto constructor = base::PostTask(FROM_HERE, {BrowserThread::UI},
base::BindOnce(&BackgroundTracingAgentClientImpl::Create, base::BindOnce(&BackgroundTracingManagerImpl::AddPendingAgent,
child_process_id, std::move(pending_provider)); child_process_id, std::move(pending_provider)));
base::PostTask(
FROM_HERE, {BrowserThread::UI},
base::BindOnce(&BackgroundTracingManagerImpl::AddPendingAgentConstructor,
std::move(constructor)));
} }
BackgroundTracingManagerImpl::BackgroundTracingManagerImpl() BackgroundTracingManagerImpl::BackgroundTracingManagerImpl()
...@@ -456,26 +451,42 @@ void BackgroundTracingManagerImpl::OnScenarioAborted() { ...@@ -456,26 +451,42 @@ void BackgroundTracingManagerImpl::OnScenarioAborted() {
} }
// static // static
void BackgroundTracingManagerImpl::AddPendingAgentConstructor( void BackgroundTracingManagerImpl::AddPendingAgent(
base::OnceClosure constructor) { int child_process_id,
mojo::PendingRemote<tracing::mojom::BackgroundTracingAgentProvider>
pending_provider) {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
// Stash away the parameters here, and delay agent initialization until we // Delay agent initialization until we have an interested AgentObserver.
// have an interested AgentObserver. // We set disconnect handler for cleanup when the tracing target is closed.
mojo::Remote<tracing::mojom::BackgroundTracingAgentProvider> provider(
std::move(pending_provider));
GetInstance()->pending_agent_constructors_.push_back(std::move(constructor)); provider.set_disconnect_handler(base::BindOnce(
&BackgroundTracingManagerImpl::ClearPendingAgent, child_process_id));
GetInstance()->pending_agents_[child_process_id] = std::move(provider);
GetInstance()->MaybeConstructPendingAgents(); GetInstance()->MaybeConstructPendingAgents();
} }
// static
void BackgroundTracingManagerImpl::ClearPendingAgent(int child_process_id) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
GetInstance()->pending_agents_.erase(child_process_id);
}
void BackgroundTracingManagerImpl::MaybeConstructPendingAgents() { void BackgroundTracingManagerImpl::MaybeConstructPendingAgents() {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (agent_observers_.empty()) if (agent_observers_.empty())
return; return;
for (auto& constructor : pending_agent_constructors_) for (auto& pending_agent : pending_agents_) {
std::move(constructor).Run(); pending_agent.second.set_disconnect_handler(base::OnceClosure());
pending_agent_constructors_.clear(); BackgroundTracingAgentClientImpl::Create(pending_agent.first,
std::move(pending_agent.second));
}
pending_agents_.clear();
} }
} // namespace content } // namespace content
...@@ -12,8 +12,10 @@ ...@@ -12,8 +12,10 @@
#include <vector> #include <vector>
#include "base/macros.h" #include "base/macros.h"
#include "components/tracing/common/background_tracing_agent.mojom.h"
#include "content/browser/tracing/background_tracing_config_impl.h" #include "content/browser/tracing/background_tracing_config_impl.h"
#include "content/public/browser/background_tracing_manager.h" #include "content/public/browser/background_tracing_manager.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/tracing/public/cpp/perfetto/trace_event_data_source.h" #include "services/tracing/public/cpp/perfetto/trace_event_data_source.h"
namespace base { namespace base {
...@@ -24,6 +26,7 @@ class NoDestructor; ...@@ -24,6 +26,7 @@ class NoDestructor;
namespace tracing { namespace tracing {
namespace mojom { namespace mojom {
class BackgroundTracingAgent; class BackgroundTracingAgent;
class BackgroundTracingAgentProvider;
} // namespace mojom } // namespace mojom
} // namespace tracing } // namespace tracing
...@@ -153,7 +156,11 @@ class BackgroundTracingManagerImpl : public BackgroundTracingManager { ...@@ -153,7 +156,11 @@ class BackgroundTracingManagerImpl : public BackgroundTracingManager {
bool privacy_filtering_enabled); bool privacy_filtering_enabled);
bool IsTriggerHandleValid(TriggerHandle handle) const; bool IsTriggerHandleValid(TriggerHandle handle) const;
void OnScenarioAborted(); void OnScenarioAborted();
static void AddPendingAgentConstructor(base::OnceClosure constructor); static void AddPendingAgent(
int child_process_id,
mojo::PendingRemote<tracing::mojom::BackgroundTracingAgentProvider>
provider);
static void ClearPendingAgent(int child_process_id);
void MaybeConstructPendingAgents(); void MaybeConstructPendingAgents();
std::unique_ptr<BackgroundTracingActiveScenario> active_scenario_; std::unique_ptr<BackgroundTracingActiveScenario> active_scenario_;
...@@ -168,7 +175,8 @@ class BackgroundTracingManagerImpl : public BackgroundTracingManager { ...@@ -168,7 +175,8 @@ class BackgroundTracingManagerImpl : public BackgroundTracingManager {
std::set<tracing::mojom::BackgroundTracingAgent*> agents_; std::set<tracing::mojom::BackgroundTracingAgent*> agents_;
std::set<AgentObserver*> agent_observers_; std::set<AgentObserver*> agent_observers_;
std::vector<base::OnceClosure> pending_agent_constructors_; std::map<int, mojo::Remote<tracing::mojom::BackgroundTracingAgentProvider>>
pending_agents_;
IdleCallback idle_callback_; IdleCallback idle_callback_;
base::RepeatingClosure tracing_enabled_callback_for_testing_; base::RepeatingClosure tracing_enabled_callback_for_testing_;
......
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