diff options
author | amistry <amistry@chromium.org> | 2016-03-10 15:45:40 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-03-10 23:47:08 +0000 |
commit | db227315c9a1706155a3c66d9a41a509df73323a (patch) | |
tree | 0646f8a44e55c6a5dd1a7b504d9867efe3c648aa | |
parent | e907a01ba6c3980e0ff877a0a6ac638bec6ff4ea (diff) | |
download | chromium_src-db227315c9a1706155a3c66d9a41a509df73323a.zip chromium_src-db227315c9a1706155a3c66d9a41a509df73323a.tar.gz chromium_src-db227315c9a1706155a3c66d9a41a509df73323a.tar.bz2 |
Notify observers when a task port is received.
BUG=582468
Review URL: https://codereview.chromium.org/1781943003
Cr-Commit-Position: refs/heads/master@{#380493}
-rw-r--r-- | base/mac/mach_port_broker.mm | 7 | ||||
-rw-r--r-- | base/mac/mach_port_broker_unittest.cc | 29 | ||||
-rw-r--r-- | content/browser/mach_broker_mac.h | 6 | ||||
-rw-r--r-- | content/browser/mach_broker_mac.mm | 12 | ||||
-rw-r--r-- | content/browser/mach_broker_mac_unittest.cc | 26 |
5 files changed, 72 insertions, 8 deletions
diff --git a/base/mac/mach_port_broker.mm b/base/mac/mach_port_broker.mm index 3d563ba..bd47017 100644 --- a/base/mac/mach_port_broker.mm +++ b/base/mac/mach_port_broker.mm @@ -163,8 +163,11 @@ void MachPortBroker::HandleRequest() { mach_port_t child_task_port = msg.child_task_port.name; // Take the lock and update the broker information. - base::AutoLock lock(lock_); - FinalizePid(child_pid, child_task_port); + { + base::AutoLock lock(lock_); + FinalizePid(child_pid, child_task_port); + } + NotifyObservers(child_pid); } void MachPortBroker::FinalizePid(base::ProcessHandle pid, diff --git a/base/mac/mach_port_broker_unittest.cc b/base/mac/mach_port_broker_unittest.cc index 2188ea3..c15afb6 100644 --- a/base/mac/mach_port_broker_unittest.cc +++ b/base/mac/mach_port_broker_unittest.cc @@ -6,6 +6,7 @@ #include "base/command_line.h" #include "base/synchronization/lock.h" +#include "base/synchronization/waitable_event.h" #include "base/test/multiprocess_test.h" #include "base/test/test_timeouts.h" #include "testing/gtest/include/gtest/gtest.h" @@ -17,9 +18,18 @@ namespace { const char kBootstrapPortName[] = "thisisatest"; } -class MachPortBrokerTest : public testing::Test { +class MachPortBrokerTest : public testing::Test, + public base::PortProvider::Observer { public: - MachPortBrokerTest() : broker_(kBootstrapPortName) {} + MachPortBrokerTest() + : broker_(kBootstrapPortName), + event_(true, false), + received_process_(kNullProcessHandle) { + broker_.AddObserver(this); + } + ~MachPortBrokerTest() override { + broker_.RemoveObserver(this); + } // Helper function to acquire/release locks and call |PlaceholderForPid()|. void AddPlaceholderForPid(base::ProcessHandle pid) { @@ -34,8 +44,20 @@ class MachPortBrokerTest : public testing::Test { broker_.FinalizePid(pid, task_port); } + void WaitForTaskPort() { + event_.Wait(); + } + + // base::PortProvider::Observer: + void OnReceivedTaskPort(ProcessHandle process) override { + received_process_ = process; + event_.Signal(); + } + protected: MachPortBroker broker_; + WaitableEvent event_; + ProcessHandle received_process_; }; TEST_F(MachPortBrokerTest, Locks) { @@ -77,6 +99,9 @@ TEST_F(MachPortBrokerTest, ReceivePortFromChild) { broker_.AddPlaceholderForPid(test_child_process.Handle()); broker_.GetLock().Release(); + WaitForTaskPort(); + EXPECT_EQ(test_child_process.Handle(), received_process_); + int rv = -1; ASSERT_TRUE(test_child_process.WaitForExitWithTimeout( TestTimeouts::action_timeout(), &rv)); diff --git a/content/browser/mach_broker_mac.h b/content/browser/mach_broker_mac.h index 32199c6..32ff5d6 100644 --- a/content/browser/mach_broker_mac.h +++ b/content/browser/mach_broker_mac.h @@ -24,7 +24,8 @@ namespace content { // access to mach task ports for content child processes. class CONTENT_EXPORT MachBroker : public base::PortProvider, public BrowserChildProcessObserver, - public NotificationObserver { + public NotificationObserver, + public base::PortProvider::Observer { public: // For use in child processes. This will send the task port of the current // process over Mach IPC to the port registered by name (via this class) in @@ -74,6 +75,9 @@ class CONTENT_EXPORT MachBroker : public base::PortProvider, MachBroker(); ~MachBroker() override; + // Implement |base::PortProvider::Observer|. + void OnReceivedTaskPort(base::ProcessHandle process) override; + // Removes all mappings belonging to |child_process_id| from the broker. void InvalidateChildProcessId(int child_process_id); diff --git a/content/browser/mach_broker_mac.mm b/content/browser/mach_broker_mac.mm index 57237aa..dfbf71f 100644 --- a/content/browser/mach_broker_mac.mm +++ b/content/browser/mach_broker_mac.mm @@ -99,9 +99,17 @@ std::string MachBroker::GetMachPortName() { return base::MachPortBroker::GetMachPortName(kBootstrapName, is_child); } -MachBroker::MachBroker() : initialized_(false), broker_(kBootstrapName) {} +MachBroker::MachBroker() : initialized_(false), broker_(kBootstrapName) { + broker_.AddObserver(this); +} + +MachBroker::~MachBroker() { + broker_.RemoveObserver(this); +} -MachBroker::~MachBroker() {} +void MachBroker::OnReceivedTaskPort(base::ProcessHandle process) { + NotifyObservers(process); +} void MachBroker::InvalidateChildProcessId(int child_process_id) { base::AutoLock lock(GetLock()); diff --git a/content/browser/mach_broker_mac_unittest.cc b/content/browser/mach_broker_mac_unittest.cc index 21a4c4d..65eaa91 100644 --- a/content/browser/mach_broker_mac_unittest.cc +++ b/content/browser/mach_broker_mac_unittest.cc @@ -6,6 +6,7 @@ #include "base/command_line.h" #include "base/synchronization/lock.h" +#include "base/synchronization/waitable_event.h" #include "base/test/multiprocess_test.h" #include "base/test/test_timeouts.h" #include "content/public/test/test_browser_thread_bundle.h" @@ -14,8 +15,17 @@ namespace content { -class MachBrokerTest : public testing::Test { +class MachBrokerTest : public testing::Test, + public base::PortProvider::Observer { public: + MachBrokerTest() + : event_(true, false), received_process_(base::kNullProcessHandle) { + broker_.AddObserver(this); + } + ~MachBrokerTest() override { + broker_.RemoveObserver(this); + } + // Helper function to acquire/release locks and call |PlaceholderForPid()|. void AddPlaceholderForPid(base::ProcessHandle pid, int child_process_id) { base::AutoLock lock(broker_.GetLock()); @@ -47,8 +57,20 @@ class MachBrokerTest : public testing::Test { EXPECT_EQ(0, rv); } + void WaitForTaskPort() { + event_.Wait(); + } + + // base::PortProvider::Observer: + void OnReceivedTaskPort(base::ProcessHandle process) override { + received_process_ = process; + event_.Signal(); + } + protected: MachBroker broker_; + base::WaitableEvent event_; + base::ProcessHandle received_process_; TestBrowserThreadBundle thread_bundle_; }; @@ -68,6 +90,8 @@ TEST_F(MachBrokerTest, AddChildProcess) { broker_.EnsureRunning(); } base::Process child_process = LaunchTestChild("MachBrokerTestChild", 7); + WaitForTaskPort(); + EXPECT_EQ(child_process.Handle(), received_process_); WaitForChildExit(child_process); EXPECT_NE(static_cast<mach_port_t>(MACH_PORT_NULL), |