www.fgks.org   »   [go: up one dir, main page]

Skip to content

Commit

Permalink
[base] Use the term "process handle" in PortProvider.
Browse files Browse the repository at this point in the history
base::PortProvider maps a base::ProcessHandle to a port.
However, the terms "process" and "pid" are used to refer to the
base::ProcessHandle in comments and variable names. With this CL,
the term "process handle" is used consistently.

Change-Id: I2bf2b148ae2a84294aa7e84ebea8191cef1191a3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5259103
Auto-Submit: Francois Pierre Doray <fdoray@chromium.org>
Reviewed-by: Mark Mentovai <mark@chromium.org>
Commit-Queue: Ted Choc <tedchoc@chromium.org>
Reviewed-by: Ted Choc <tedchoc@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1256277}
  • Loading branch information
fdoray authored and Chromium LUCI CQ committed Feb 5, 2024
1 parent 035e7ab commit 7898890
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 74 deletions.
10 changes: 6 additions & 4 deletions base/process/port_provider_mac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ void PortProvider::RemoveObserver(Observer* observer) {
observer_list_->RemoveObserver(observer);
}

void PortProvider::NotifyObservers(ProcessHandle process) {
observer_list_->Notify(FROM_HERE, &Observer::OnReceivedTaskPort, process);
void PortProvider::NotifyObservers(ProcessHandle process_handle) {
observer_list_->Notify(FROM_HERE, &Observer::OnReceivedTaskPort,
process_handle);
}

mach_port_t SelfPortProvider::TaskForPid(base::ProcessHandle process) const {
DCHECK(base::Process(process).is_current());
mach_port_t SelfPortProvider::TaskForHandle(
base::ProcessHandle process_handle) const {
DCHECK(base::Process(process_handle).is_current());
return mach_task_self();
}

Expand Down
12 changes: 6 additions & 6 deletions base/process/port_provider_mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ class BASE_EXPORT PortProvider {
// received for a given process.
// This notification is guaranteed to be sent on the same task runner where
// the observer was added.
virtual void OnReceivedTaskPort(ProcessHandle process) = 0;
virtual void OnReceivedTaskPort(ProcessHandle process_handle) = 0;
};

// Returns the mach task port for |process| if possible, or else
// |MACH_PORT_NULL|.
virtual mach_port_t TaskForPid(ProcessHandle process) const = 0;
// Returns the mach task port for `process_handle` if possible, or else
// `MACH_PORT_NULL`.
virtual mach_port_t TaskForHandle(ProcessHandle process_handle) const = 0;

// Observer interface.
void AddObserver(Observer* observer);
void RemoveObserver(Observer* observer);

protected:
// Called by subclasses to send a notification to observers.
void NotifyObservers(ProcessHandle process);
void NotifyObservers(ProcessHandle process_handle);

private:
scoped_refptr<base::ObserverListThreadSafe<Observer>> observer_list_;
Expand All @@ -55,7 +55,7 @@ class BASE_EXPORT PortProvider {
// Port provider that returns the calling process's task port, ignoring its
// argument.
class BASE_EXPORT SelfPortProvider : public base::PortProvider {
mach_port_t TaskForPid(base::ProcessHandle process) const override;
mach_port_t TaskForHandle(base::ProcessHandle process_handle) const override;
};

} // namespace base
Expand Down
4 changes: 2 additions & 2 deletions base/process/process_mac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ Process::Priority Process::GetPriority(PortProvider* port_provider) const {
CHECK(IsValid());
CHECK(port_provider);

mach_port_t task_port = port_provider->TaskForPid(Pid());
mach_port_t task_port = port_provider->TaskForHandle(Handle());
if (task_port == TASK_NULL) {
// Upon failure, return the default value.
return Priority::kUserBlocking;
Expand Down Expand Up @@ -201,7 +201,7 @@ bool Process::SetPriority(PortProvider* port_provider, Priority priority) {
return false;
}

mach_port_t task_port = port_provider->TaskForPid(Pid());
mach_port_t task_port = port_provider->TaskForHandle(Handle());
if (task_port == TASK_NULL) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion base/process/process_metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ class BASE_EXPORT ProcessMetrics {
uint64_t absolute_package_idle_wakeups);

// Queries the port provider if it's set.
mach_port_t TaskForPid(ProcessHandle process) const;
mach_port_t TaskForHandle(ProcessHandle process_handle) const;
#endif

#if BUILDFLAG(IS_WIN)
Expand Down
12 changes: 6 additions & 6 deletions base/process/process_metrics_apple.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,21 @@ bool GetPowerInfo(mach_port_t task, task_power_info* power_info_data) {
} // namespace

// Implementations of ProcessMetrics class shared by Mac and iOS.
mach_port_t ProcessMetrics::TaskForPid(ProcessHandle process) const {
mach_port_t ProcessMetrics::TaskForHandle(ProcessHandle process_handle) const {
mach_port_t task = MACH_PORT_NULL;
#if BUILDFLAG(IS_MAC)
if (port_provider_) {
task = port_provider_->TaskForPid(process_);
task = port_provider_->TaskForHandle(process_);
}
#endif
if (task == MACH_PORT_NULL && process_ == getpid()) {
if (task == MACH_PORT_NULL && process_handle == getpid()) {
task = mach_task_self();
}
return task;
}

TimeDelta ProcessMetrics::GetCumulativeCPUUsage() {
mach_port_t task = TaskForPid(process_);
mach_port_t task = TaskForHandle(process_);
if (task == MACH_PORT_NULL) {
return TimeDelta();
}
Expand Down Expand Up @@ -144,7 +144,7 @@ TimeDelta ProcessMetrics::GetCumulativeCPUUsage() {
}

int ProcessMetrics::GetPackageIdleWakeupsPerSecond() {
mach_port_t task = TaskForPid(process_);
mach_port_t task = TaskForHandle(process_);
task_power_info power_info_data;

GetPowerInfo(task, &power_info_data);
Expand All @@ -164,7 +164,7 @@ int ProcessMetrics::GetPackageIdleWakeupsPerSecond() {
}

int ProcessMetrics::GetIdleWakeupsPerSecond() {
mach_port_t task = TaskForPid(process_);
mach_port_t task = TaskForHandle(process_);
task_power_info power_info_data;

GetPowerInfo(task, &power_info_data);
Expand Down
4 changes: 2 additions & 2 deletions base/process/process_metrics_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ class TestChildLauncher::TestChildPortProvider final : public PortProvider {
TestChildPortProvider(const TestChildPortProvider&) = delete;
TestChildPortProvider& operator=(const TestChildPortProvider&) = delete;

mach_port_t TaskForPid(ProcessHandle process) const final {
return process == handle_ ? port_.get() : MACH_PORT_NULL;
mach_port_t TaskForHandle(ProcessHandle process_handle) const final {
return process_handle == handle_ ? port_.get() : MACH_PORT_NULL;
}

private:
Expand Down
2 changes: 1 addition & 1 deletion base/process/process_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ constexpr int kDummyExitCode = 42;
// Fake port provider that returns the calling process's
// task port, ignoring its argument.
class FakePortProvider : public base::PortProvider {
mach_port_t TaskForPid(base::ProcessHandle process) const override {
mach_port_t TaskForHandle(base::ProcessHandle process_handle) const override {
return mach_task_self();
}
};
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/hang_monitor/hang_crash_dump_mac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
void CrashDumpHungChildProcess(base::ProcessHandle handle) {
base::PortProvider* provider =
content::BrowserChildProcessHost::GetPortProvider();
mach_port_t task_port = provider->TaskForPid(handle);
mach_port_t task_port = provider->TaskForHandle(handle);
if (task_port != MACH_PORT_NULL) {
crash_reporter::DumpProcessWithoutCrashing(task_port);
}
Expand Down
32 changes: 17 additions & 15 deletions content/browser/child_process_task_port_provider_mac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,23 @@ ChildProcessTaskPortProvider* ChildProcessTaskPortProvider::GetInstance() {
}

void ChildProcessTaskPortProvider::OnChildProcessLaunched(
base::ProcessHandle pid,
base::ProcessHandle process_handle,
mojom::ChildProcess* child_process) {
if (!ShouldRequestTaskPorts())
return;

child_process->GetTaskPort(
base::BindOnce(&ChildProcessTaskPortProvider::OnTaskPortReceived,
base::Unretained(this), pid));
base::Unretained(this), process_handle));
}

mach_port_t ChildProcessTaskPortProvider::TaskForPid(
base::ProcessHandle pid) const {
mach_port_t ChildProcessTaskPortProvider::TaskForHandle(
base::ProcessHandle process_handle) const {
base::AutoLock lock(lock_);
PidToTaskPortMap::const_iterator it = pid_to_task_port_.find(pid);
if (it == pid_to_task_port_.end())
auto it = handle_to_task_port_.find(process_handle);
if (it == handle_to_task_port_.end()) {
return MACH_PORT_NULL;
}
return it->second.get();
}

Expand Down Expand Up @@ -92,11 +93,12 @@ bool ChildProcessTaskPortProvider::ShouldRequestTaskPorts() const {
}

void ChildProcessTaskPortProvider::OnTaskPortReceived(
base::ProcessHandle pid,
base::ProcessHandle process_handle,
mojo::PlatformHandle task_port) {
DCHECK(ShouldRequestTaskPorts());
if (!task_port.is_mach_send()) {
DLOG(ERROR) << "Invalid handle received as task port for pid " << pid;
DLOG(ERROR) << "Invalid handle received as task port for pid "
<< base::GetProcId(process_handle);
return;
}
base::apple::ScopedMachSendRight port = task_port.TakeMachSendRight();
Expand All @@ -114,14 +116,14 @@ void ChildProcessTaskPortProvider::OnTaskPortReceived(
return;
}

DVLOG(1) << "Received task port for PID=" << pid
DVLOG(1) << "Received task port for PID=" << base::GetProcId(process_handle)
<< ", port name=" << port.get();

{
base::AutoLock lock(lock_);
auto it = pid_to_task_port_.find(pid);
if (it == pid_to_task_port_.end()) {
pid_to_task_port_.emplace(pid, std::move(port));
auto it = handle_to_task_port_.find(process_handle);
if (it == handle_to_task_port_.end()) {
handle_to_task_port_.emplace(process_handle, std::move(port));
} else {
// If a task port already exists for the PID, then reset it if the port
// is of a different name. The port name may be the same when running in
Expand All @@ -133,7 +135,7 @@ void ChildProcessTaskPortProvider::OnTaskPortReceived(
}
}

NotifyObservers(pid);
NotifyObservers(process_handle);
}

void ChildProcessTaskPortProvider::OnTaskPortDied() {
Expand All @@ -158,9 +160,9 @@ void ChildProcessTaskPortProvider::OnTaskPortDied() {
base::apple::ScopedMachSendRight dead_port(notification.not_port);

base::AutoLock lock(lock_);
std::erase_if(pid_to_task_port_, [&dead_port](const auto& pair) {
std::erase_if(handle_to_task_port_, [&dead_port](const auto& pair) {
if (pair.second.get() == dead_port.get()) {
DVLOG(1) << "Task died, PID=" << pair.first
DVLOG(1) << "Task died, PID=" << base::GetProcId(pair.first)
<< ", task port name=" << dead_port.get();
return true;
}
Expand Down
32 changes: 16 additions & 16 deletions content/browser/child_process_task_port_provider_mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@
#include "base/process/port_provider_mac.h"
#include "base/process/process_handle.h"
#include "base/synchronization/lock.h"
#include "base/thread_annotations.h"
#include "content/common/child_process.mojom-forward.h"
#include "content/common/content_export.h"
#include "mojo/public/cpp/platform/platform_handle.h"

namespace content {

// The ChildProcessTaskPortProvider keeps an association between a PID and the
// process's task port. This association is needed for the browser to manipulate
// certain aspects of its child processes.
// The ChildProcessTaskPortProvider keeps an association between the handle and
// the task port of a process. This association is needed for the browser to
// manipulate certain aspects of its child processes.
class CONTENT_EXPORT ChildProcessTaskPortProvider : public base::PortProvider {
public:
// Returns the singleton instance.
Expand All @@ -31,19 +32,18 @@ class CONTENT_EXPORT ChildProcessTaskPortProvider : public base::PortProvider {
ChildProcessTaskPortProvider& operator=(const ChildProcessTaskPortProvider&) =
delete;

// Called by BrowserChildProcessHostImpl and RenderProcessHostImpl when
// a new child has been created. This will invoke the GetTaskPort() method
// on |child_control| and will store the returned port as being associated to
// |pid|.
// Called by BrowserChildProcessHostImpl and RenderProcessHostImpl when a new
// child is launched. Invokes `GetTaskPort()` on `child_process` and stores
// the returned port as being associated to `process_handle`.
//
// When the kernel sends a notification that the port has become a dead name,
// indicating that the child process has died, the association will be
// removed.
void OnChildProcessLaunched(base::ProcessHandle pid,
void OnChildProcessLaunched(base::ProcessHandle process_handle,
mojom::ChildProcess* child_process);

// base::PortProvider:
mach_port_t TaskForPid(base::ProcessHandle process) const override;
mach_port_t TaskForHandle(base::ProcessHandle process_handle) const override;

private:
friend class ChildProcessTaskPortProviderTest;
Expand All @@ -61,7 +61,7 @@ class CONTENT_EXPORT ChildProcessTaskPortProvider : public base::PortProvider {
bool ShouldRequestTaskPorts() const;

// Callback for mojom::ChildProcess::GetTaskPort reply.
void OnTaskPortReceived(base::ProcessHandle pid,
void OnTaskPortReceived(base::ProcessHandle process_handle,
mojo::PlatformHandle task_port);

// Event handler for |notification_source_|, invoked for
Expand All @@ -71,14 +71,14 @@ class CONTENT_EXPORT ChildProcessTaskPortProvider : public base::PortProvider {
// Lock that protects the map below.
mutable base::Lock lock_;

// Maps a PID to the corresponding task port.
using PidToTaskPortMap =
// Maps a process handle to the corresponding task port.
using HandleToTaskPortMap =
std::map<base::ProcessHandle, base::apple::ScopedMachSendRight>;
PidToTaskPortMap pid_to_task_port_;
HandleToTaskPortMap handle_to_task_port_ GUARDED_BY(lock_);

// A Mach port that is used to register for dead name notifications from
// the kernel. All the ports in |pid_to_task_port_| have a notification set
// up to send to this port.
// A Mach port that is used to register for dead name notifications from the
// kernel. All the ports in `handle_to_task_port_` have a notification set up
// to send to this port.
base::apple::ScopedMachReceiveRight notification_port_;

// Dispatch source for |notification_port_|.
Expand Down
Loading

0 comments on commit 7898890

Please sign in to comment.