Commit 834d97c9 authored by Robert Sesek's avatar Robert Sesek Committed by Commit Bot

mac: Remove two unneeded ContentMainDelegate sandbox-related methods.

DelaySandboxInitialization() is not called anywhere and is dead code.

ProcessRegistersWithSystemProcess() was used under the V1 sandbox
architecture to warm-up access to various IOKit services. Under the V2
sandbox architecture, there is no warm-up phase and the sandbox profiles
simply allow access to those resources where needed. This allows
base::PowerMonitorDeviceSource to be simplified, since the IOServices
can be directly connected to rather than pre-allocated.

Change-Id: I56769c90bd7fd9d8a5c3b8d16dc8166a53b5ce03
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2039390
Commit-Queue: Robert Sesek <rsesek@chromium.org>
Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Reviewed-by: default avatarMark Mentovai <mark@chromium.org>
Cr-Commit-Position: refs/heads/master@{#738771}
parent e9559700
...@@ -15,6 +15,13 @@ ...@@ -15,6 +15,13 @@
#include <windows.h> #include <windows.h>
#endif // !OS_WIN #endif // !OS_WIN
#if defined(OS_MACOSX) && !defined(OS_IOS)
#include <IOKit/IOTypes.h>
#include "base/mac/scoped_cftyperef.h"
#include "base/mac/scoped_ionotificationportref.h"
#endif
#if defined(OS_IOS) #if defined(OS_IOS)
#include <objc/runtime.h> #include <objc/runtime.h>
#endif // OS_IOS #endif // OS_IOS
...@@ -28,18 +35,6 @@ class BASE_EXPORT PowerMonitorDeviceSource : public PowerMonitorSource { ...@@ -28,18 +35,6 @@ class BASE_EXPORT PowerMonitorDeviceSource : public PowerMonitorSource {
PowerMonitorDeviceSource(); PowerMonitorDeviceSource();
~PowerMonitorDeviceSource() override; ~PowerMonitorDeviceSource() override;
#if defined(OS_MACOSX)
// Allocate system resources needed by the PowerMonitor class.
//
// This function must be called before instantiating an instance of the class
// and before the Sandbox is initialized.
#if !defined(OS_IOS)
static void AllocateSystemIOPorts();
#else
static void AllocateSystemIOPorts() {}
#endif // OS_IOS
#endif // OS_MACOSX
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
// On Chrome OS, Chrome receives power-related events from powerd, the system // On Chrome OS, Chrome receives power-related events from powerd, the system
// power daemon, via D-Bus signals received on the UI thread. base can't // power daemon, via D-Bus signals received on the UI thread. base can't
...@@ -74,13 +69,35 @@ class BASE_EXPORT PowerMonitorDeviceSource : public PowerMonitorSource { ...@@ -74,13 +69,35 @@ class BASE_EXPORT PowerMonitorDeviceSource : public PowerMonitorSource {
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
void PlatformInit(); void PlatformInit();
void PlatformDestroy(); void PlatformDestroy();
#endif
#if !defined(OS_IOS)
// Callback from IORegisterForSystemPower(). |refcon| is the |this| pointer.
static void SystemPowerEventCallback(void* refcon,
io_service_t service,
natural_t message_type,
void* message_argument);
#endif // !OS_IOS
#endif // OS_MACOSX
// Platform-specific method to check whether the system is currently // Platform-specific method to check whether the system is currently
// running on battery power. Returns true if running on batteries, // running on battery power. Returns true if running on batteries,
// false otherwise. // false otherwise.
bool IsOnBatteryPowerImpl() override; bool IsOnBatteryPowerImpl() override;
#if defined(OS_MACOSX) && !defined(OS_IOS)
// Reference to the system IOPMrootDomain port.
io_connect_t power_manager_port_ = IO_OBJECT_NULL;
// Notification port that delivers power (sleep/wake) notifications.
mac::ScopedIONotificationPortRef notification_port_;
// Notifier reference for the |notification_port_|.
io_object_t notifier_ = IO_OBJECT_NULL;
// Run loop source to observe power-source-change events.
ScopedCFTypeRef<CFRunLoopSourceRef> power_source_run_loop_source_;
#endif
#if defined(OS_IOS) #if defined(OS_IOS)
// Holds pointers to system event notification observers. // Holds pointers to system event notification observers.
std::vector<id> notification_observers_; std::vector<id> notification_observers_;
......
...@@ -53,106 +53,79 @@ bool PowerMonitorDeviceSource::IsOnBatteryPowerImpl() { ...@@ -53,106 +53,79 @@ bool PowerMonitorDeviceSource::IsOnBatteryPowerImpl() {
namespace { namespace {
io_connect_t g_system_power_io_port = 0;
IONotificationPortRef g_notification_port_ref = 0;
io_object_t g_notifier_object = 0;
CFRunLoopSourceRef g_battery_status_ref = 0;
void BatteryEventCallback(void*) { void BatteryEventCallback(void*) {
ProcessPowerEventHelper(PowerMonitorSource::POWER_STATE_EVENT); ProcessPowerEventHelper(PowerMonitorSource::POWER_STATE_EVENT);
} }
void SystemPowerEventCallback(void*,
io_service_t service,
natural_t message_type,
void* message_argument) {
switch (message_type) {
// If this message is not handled the system may delay sleep for 30 seconds.
case kIOMessageCanSystemSleep:
IOAllowPowerChange(g_system_power_io_port,
reinterpret_cast<intptr_t>(message_argument));
break;
case kIOMessageSystemWillSleep:
ProcessPowerEventHelper(base::PowerMonitorSource::SUSPEND_EVENT);
IOAllowPowerChange(g_system_power_io_port,
reinterpret_cast<intptr_t>(message_argument));
break;
case kIOMessageSystemWillPowerOn:
ProcessPowerEventHelper(PowerMonitorSource::RESUME_EVENT);
break;
}
}
} // namespace } // namespace
// The reason we can't include this code in the constructor is because
// PlatformInit() requires an active runloop and the IO port needs to be
// allocated at sandbox initialization time, before there's a runloop.
// See crbug.com/83783 .
// static
void PowerMonitorDeviceSource::AllocateSystemIOPorts() {
DCHECK_EQ(g_system_power_io_port, 0u);
// Notification port allocated by IORegisterForSystemPower.
g_system_power_io_port = IORegisterForSystemPower(
NULL, &g_notification_port_ref, SystemPowerEventCallback,
&g_notifier_object);
DCHECK_NE(g_system_power_io_port, 0u);
}
void PowerMonitorDeviceSource::PlatformInit() { void PowerMonitorDeviceSource::PlatformInit() {
// Need to call AllocateSystemIOPorts() before creating a PowerMonitor power_manager_port_ = IORegisterForSystemPower(
// object. this,
DCHECK_NE(g_system_power_io_port, 0u); mac::ScopedIONotificationPortRef::Receiver(notification_port_).get(),
if (g_system_power_io_port == 0) &SystemPowerEventCallback, &notifier_);
return; DCHECK_NE(power_manager_port_, IO_OBJECT_NULL);
// Add the notification port and battery monitor to the application runloop // Add the sleep/wake notification event source to the runloop.
CFRunLoopAddSource( CFRunLoopAddSource(
CFRunLoopGetCurrent(), CFRunLoopGetCurrent(),
IONotificationPortGetRunLoopSource(g_notification_port_ref), IONotificationPortGetRunLoopSource(notification_port_.get()),
kCFRunLoopCommonModes); kCFRunLoopCommonModes);
base::ScopedCFTypeRef<CFRunLoopSourceRef> battery_status_ref( // Create and add the power-source-change event source to the runloop.
IOPSNotificationCreateRunLoopSource(BatteryEventCallback, nullptr)); power_source_run_loop_source_.reset(
DCHECK(battery_status_ref); // crbug.com/897557 IOPSNotificationCreateRunLoopSource(&BatteryEventCallback, nullptr));
// Verify that the source was created. This may fail if the sandbox does not
// permit the process to access the underlying system service. See
// https://crbug.com/897557 for an example of such a configuration bug.
DCHECK(power_source_run_loop_source_);
CFRunLoopAddSource(CFRunLoopGetCurrent(), battery_status_ref, CFRunLoopAddSource(CFRunLoopGetCurrent(), power_source_run_loop_source_,
kCFRunLoopDefaultMode); kCFRunLoopDefaultMode);
g_battery_status_ref = battery_status_ref.release();
} }
void PowerMonitorDeviceSource::PlatformDestroy() { void PowerMonitorDeviceSource::PlatformDestroy() {
DCHECK_NE(g_system_power_io_port, 0u);
if (g_system_power_io_port == 0)
return;
// Remove the sleep notification port from the application runloop
CFRunLoopRemoveSource( CFRunLoopRemoveSource(
CFRunLoopGetCurrent(), CFRunLoopGetCurrent(),
IONotificationPortGetRunLoopSource(g_notification_port_ref), IONotificationPortGetRunLoopSource(notification_port_.get()),
kCFRunLoopCommonModes); kCFRunLoopCommonModes);
base::ScopedCFTypeRef<CFRunLoopSourceRef> battery_status_ref( CFRunLoopRemoveSource(CFRunLoopGetCurrent(),
g_battery_status_ref); power_source_run_loop_source_.get(),
CFRunLoopRemoveSource(CFRunLoopGetCurrent(), g_battery_status_ref,
kCFRunLoopDefaultMode); kCFRunLoopDefaultMode);
g_battery_status_ref = 0;
// Deregister for system sleep notifications // Deregister for system power notifications.
IODeregisterForSystemPower(&g_notifier_object); IODeregisterForSystemPower(&notifier_);
// IORegisterForSystemPower implicitly opens the Root Power Domain IOService, // Close the connection to the IOPMrootDomain that was opened in
// so we close it here. // PlatformInit().
IOServiceClose(g_system_power_io_port); IOServiceClose(power_manager_port_);
power_manager_port_ = IO_OBJECT_NULL;
}
g_system_power_io_port = 0; void PowerMonitorDeviceSource::SystemPowerEventCallback(
void* refcon,
io_service_t service,
natural_t message_type,
void* message_argument) {
auto* thiz = static_cast<PowerMonitorDeviceSource*>(refcon);
// Destroy the notification port allocated by IORegisterForSystemPower. switch (message_type) {
IONotificationPortDestroy(g_notification_port_ref); // If this message is not handled the system may delay sleep for 30 seconds.
case kIOMessageCanSystemSleep:
IOAllowPowerChange(thiz->power_manager_port_,
reinterpret_cast<intptr_t>(message_argument));
break;
case kIOMessageSystemWillSleep:
ProcessPowerEventHelper(PowerMonitorSource::SUSPEND_EVENT);
IOAllowPowerChange(thiz->power_manager_port_,
reinterpret_cast<intptr_t>(message_argument));
break;
case kIOMessageSystemWillPowerOn:
ProcessPowerEventHelper(PowerMonitorSource::RESUME_EVENT);
break;
}
} }
} // namespace base } // namespace base
...@@ -1133,27 +1133,7 @@ void ChromeMainDelegate::ProcessExiting(const std::string& process_type) { ...@@ -1133,27 +1133,7 @@ void ChromeMainDelegate::ProcessExiting(const std::string& process_type) {
#endif // !defined(OS_ANDROID) #endif // !defined(OS_ANDROID)
} }
#if defined(OS_MACOSX) #if defined(OS_LINUX)
bool ChromeMainDelegate::ProcessRegistersWithSystemProcess(
const std::string& process_type) {
#if !BUILDFLAG(ENABLE_NACL)
return false;
#else
return process_type == switches::kNaClLoaderProcess;
#endif
}
bool ChromeMainDelegate::DelaySandboxInitialization(
const std::string& process_type) {
#if BUILDFLAG(ENABLE_NACL)
// NaClLoader does this in NaClMainPlatformDelegate::EnableSandbox().
// No sandbox needed for relauncher.
if (process_type == switches::kNaClLoaderProcess)
return true;
#endif
return process_type == switches::kRelauncherProcess;
}
#elif defined(OS_LINUX)
void ChromeMainDelegate::ZygoteStarting( void ChromeMainDelegate::ZygoteStarting(
std::vector<std::unique_ptr<service_manager::ZygoteForkDelegate>>* std::vector<std::unique_ptr<service_manager::ZygoteForkDelegate>>*
delegates) { delegates) {
......
...@@ -51,11 +51,7 @@ class ChromeMainDelegate : public content::ContentMainDelegate { ...@@ -51,11 +51,7 @@ class ChromeMainDelegate : public content::ContentMainDelegate {
const std::string& process_type, const std::string& process_type,
const content::MainFunctionParams& main_function_params) override; const content::MainFunctionParams& main_function_params) override;
void ProcessExiting(const std::string& process_type) override; void ProcessExiting(const std::string& process_type) override;
#if defined(OS_MACOSX) #if defined(OS_LINUX)
bool ProcessRegistersWithSystemProcess(
const std::string& process_type) override;
bool DelaySandboxInitialization(const std::string& process_type) override;
#elif defined(OS_LINUX)
void ZygoteStarting( void ZygoteStarting(
std::vector<std::unique_ptr<service_manager::ZygoteForkDelegate>>* std::vector<std::unique_ptr<service_manager::ZygoteForkDelegate>>*
delegates) override; delegates) override;
......
...@@ -89,9 +89,6 @@ class EventRouterForwarderTest : public testing::Test { ...@@ -89,9 +89,6 @@ class EventRouterForwarderTest : public testing::Test {
EventRouterForwarderTest() EventRouterForwarderTest()
: task_environment_(content::BrowserTaskEnvironment::REAL_IO_THREAD), : task_environment_(content::BrowserTaskEnvironment::REAL_IO_THREAD),
profile_manager_(TestingBrowserProcess::GetGlobal()) { profile_manager_(TestingBrowserProcess::GetGlobal()) {
#if defined(OS_MACOSX)
base::PowerMonitorDeviceSource::AllocateSystemIOPorts();
#endif
std::unique_ptr<base::PowerMonitorSource> power_monitor_source( std::unique_ptr<base::PowerMonitorSource> power_monitor_source(
new base::PowerMonitorDeviceSource()); new base::PowerMonitorDeviceSource());
base::PowerMonitor::Initialize(std::move(power_monitor_source)); base::PowerMonitor::Initialize(std::move(power_monitor_source));
......
...@@ -693,17 +693,6 @@ int ContentMainRunnerImpl::Initialize(const ContentMainParams& params) { ...@@ -693,17 +693,6 @@ int ContentMainRunnerImpl::Initialize(const ContentMainParams& params) {
TRACE_EVENT0("startup,benchmark,rail", "ContentMainRunnerImpl::Initialize"); TRACE_EVENT0("startup,benchmark,rail", "ContentMainRunnerImpl::Initialize");
#endif // !OS_ANDROID #endif // !OS_ANDROID
#if defined(OS_MACOSX)
// We need to allocate the IO Ports before the Sandbox is initialized or
// the first instance of PowerMonitor is created.
// It's important not to allocate the ports for processes which don't
// register with the power monitor - see https://crbug.com/88867.
if (process_type.empty() ||
delegate_->ProcessRegistersWithSystemProcess(process_type)) {
base::PowerMonitorDeviceSource::AllocateSystemIOPorts();
}
#endif
// If we are on a platform where the default allocator is overridden (shim // If we are on a platform where the default allocator is overridden (shim
// layer on windows, tcmalloc on Linux Desktop) smoke-tests that the // layer on windows, tcmalloc on Linux Desktop) smoke-tests that the
// overriding logic is working correctly. If not causes a hard crash, as its // overriding logic is working correctly. If not causes a hard crash, as its
......
...@@ -27,19 +27,7 @@ int ContentMainDelegate::RunProcess( ...@@ -27,19 +27,7 @@ int ContentMainDelegate::RunProcess(
return -1; return -1;
} }
#if defined(OS_MACOSX) #if defined(OS_LINUX)
bool ContentMainDelegate::ProcessRegistersWithSystemProcess(
const std::string& process_type) {
return false;
}
bool ContentMainDelegate::DelaySandboxInitialization(
const std::string& process_type) {
return false;
}
#elif defined(OS_LINUX)
void ContentMainDelegate::ZygoteStarting( void ContentMainDelegate::ZygoteStarting(
std::vector<std::unique_ptr<service_manager::ZygoteForkDelegate>>* std::vector<std::unique_ptr<service_manager::ZygoteForkDelegate>>*
......
...@@ -60,19 +60,7 @@ class CONTENT_EXPORT ContentMainDelegate { ...@@ -60,19 +60,7 @@ class CONTENT_EXPORT ContentMainDelegate {
// Called right before the process exits. // Called right before the process exits.
virtual void ProcessExiting(const std::string& process_type) {} virtual void ProcessExiting(const std::string& process_type) {}
#if defined(OS_MACOSX) #if defined(OS_LINUX)
// Returns true if the process registers with the system monitor, so that we
// can allocate an IO port for it before the sandbox is initialized. Embedders
// are called only for process types that content doesn't know about.
virtual bool ProcessRegistersWithSystemProcess(
const std::string& process_type);
// Allows the embedder to override initializing the sandbox. This is needed
// because some processes might not want to enable it right away or might not
// want it at all.
virtual bool DelaySandboxInitialization(const std::string& process_type);
#elif defined(OS_LINUX)
// Tells the embedder that the zygote process is starting, and allows it to // Tells the embedder that the zygote process is starting, and allows it to
// specify one or more zygote delegates if it wishes by storing them in // specify one or more zygote delegates if it wishes by storing them in
// |*delegates|. // |*delegates|.
......
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