| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/mach_broker_mac.h" | 5 #include "content/browser/mach_broker_mac.h" |
| 6 | 6 |
| 7 #include "base/synchronization/lock.h" | 7 #include "base/synchronization/lock.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 | 9 |
| 10 namespace content { | 10 namespace content { |
| 11 | 11 |
| 12 class MachBrokerTest : public testing::Test { | 12 class MachBrokerTest : public testing::Test { |
| 13 public: | 13 public: |
| 14 // Helper function to acquire/release locks and call |PlaceholderForPid()|. | 14 // Helper function to acquire/release locks and call |PlaceholderForPid()|. |
| 15 void AddPlaceholderForPid(base::ProcessHandle pid, int child_process_id) { | 15 void AddPlaceholderForPid(base::ProcessHandle pid, |
| 16 int child_process_id, |
| 17 bool is_gpu_process) { |
| 16 base::AutoLock lock(broker_.GetLock()); | 18 base::AutoLock lock(broker_.GetLock()); |
| 17 broker_.AddPlaceholderForPid(pid, child_process_id); | 19 broker_.AddPlaceholderForPid(pid, child_process_id, is_gpu_process); |
| 18 } | 20 } |
| 19 | 21 |
| 20 void InvalidateChildProcessId(int child_process_id) { | 22 void InvalidateChildProcessId(int child_process_id) { |
| 21 broker_.InvalidateChildProcessId(child_process_id); | 23 broker_.InvalidateChildProcessId(child_process_id); |
| 22 } | 24 } |
| 23 | 25 |
| 24 int GetChildProcessCount(int child_process_id) { | 26 int GetChildProcessCount(int child_process_id) { |
| 25 return broker_.child_process_id_map_.count(child_process_id); | 27 return broker_.child_process_id_map_.count(child_process_id); |
| 26 } | 28 } |
| 27 | 29 |
| 28 // Helper function to acquire/release locks and call |FinalizePid()|. | 30 // Helper function to acquire/release locks and call |FinalizePid()|. |
| 29 void FinalizePid(base::ProcessHandle pid, | 31 void FinalizePid(base::ProcessHandle pid, |
| 30 mach_port_t task_port) { | 32 mach_port_t task_port) { |
| 31 base::AutoLock lock(broker_.GetLock()); | 33 base::AutoLock lock(broker_.GetLock()); |
| 32 broker_.FinalizePid(pid, task_port); | 34 broker_.FinalizePid(pid, task_port); |
| 33 } | 35 } |
| 34 | 36 |
| 37 // Helper function to acquire/release locks and call |RegisterIOSurface()|. |
| 38 bool RegisterIOSurface(base::ProcessHandle pid, |
| 39 int io_surface_id, |
| 40 int client_id, |
| 41 mach_port_t io_surface_port) { |
| 42 base::AutoLock lock(broker_.GetLock()); |
| 43 return broker_.RegisterIOSurface(pid, io_surface_id, client_id, |
| 44 io_surface_port); |
| 45 } |
| 46 |
| 47 // Helper function to acquire/release locks and call |UnregisterIOSurface()|. |
| 48 void UnregisterIOSurface(base::ProcessHandle pid, |
| 49 int io_surface_id, |
| 50 int client_id) { |
| 51 base::AutoLock lock(broker_.GetLock()); |
| 52 broker_.UnregisterIOSurface(pid, io_surface_id, client_id); |
| 53 } |
| 54 |
| 55 // Helper function to acquire/release locks and call |AcquireIOSurface()|. |
| 56 mach_port_t AcquireIOSurface(base::ProcessHandle pid, int io_surface_id) { |
| 57 base::AutoLock lock(broker_.GetLock()); |
| 58 return broker_.AcquireIOSurface(pid, io_surface_id); |
| 59 } |
| 60 |
| 35 protected: | 61 protected: |
| 36 MachBroker broker_; | 62 MachBroker broker_; |
| 37 }; | 63 }; |
| 38 | 64 |
| 39 TEST_F(MachBrokerTest, Locks) { | 65 TEST_F(MachBrokerTest, Locks) { |
| 40 // Acquire and release the locks. Nothing bad should happen. | 66 // Acquire and release the locks. Nothing bad should happen. |
| 41 base::AutoLock lock(broker_.GetLock()); | 67 base::AutoLock lock(broker_.GetLock()); |
| 42 } | 68 } |
| 43 | 69 |
| 44 TEST_F(MachBrokerTest, AddPlaceholderAndFinalize) { | 70 TEST_F(MachBrokerTest, AddPlaceholderAndFinalize) { |
| 45 // Add a placeholder for PID 1. | 71 // Add a placeholder for PID 1. |
| 46 AddPlaceholderForPid(1, 1); | 72 AddPlaceholderForPid(1, 1, false); |
| 47 EXPECT_EQ(0u, broker_.TaskForPid(1)); | 73 EXPECT_EQ(0u, broker_.TaskForPid(1)); |
| 48 | 74 |
| 49 // Finalize PID 1. | 75 // Finalize PID 1. |
| 50 FinalizePid(1, 100u); | 76 FinalizePid(1, 100u); |
| 51 EXPECT_EQ(100u, broker_.TaskForPid(1)); | 77 EXPECT_EQ(100u, broker_.TaskForPid(1)); |
| 52 | 78 |
| 53 // Should be no entry for PID 2. | 79 // Should be no entry for PID 2. |
| 54 EXPECT_EQ(0u, broker_.TaskForPid(2)); | 80 EXPECT_EQ(0u, broker_.TaskForPid(2)); |
| 55 } | 81 } |
| 56 | 82 |
| 57 TEST_F(MachBrokerTest, InvalidateChildProcessId) { | 83 TEST_F(MachBrokerTest, InvalidateChildProcessId) { |
| 58 // Add a placeholder for PID 1 and child process id 50. | 84 // Add a placeholder for PID 1 and child process id 50. |
| 59 AddPlaceholderForPid(1, 50); | 85 AddPlaceholderForPid(1, 50, false); |
| 60 FinalizePid(1, 100u); | 86 FinalizePid(1, 100u); |
| 61 | 87 |
| 62 EXPECT_EQ(100u, broker_.TaskForPid(1)); | 88 EXPECT_EQ(100u, broker_.TaskForPid(1)); |
| 63 InvalidateChildProcessId(50); | 89 InvalidateChildProcessId(50); |
| 64 EXPECT_EQ(0u, broker_.TaskForPid(1)); | 90 EXPECT_EQ(0u, broker_.TaskForPid(1)); |
| 65 } | 91 } |
| 66 | 92 |
| 67 TEST_F(MachBrokerTest, ValidateChildProcessIdMap) { | 93 TEST_F(MachBrokerTest, ValidateChildProcessIdMap) { |
| 68 // Add a placeholder for PID 1 and child process id 50. | 94 // Add a placeholder for PID 1 and child process id 50. |
| 69 AddPlaceholderForPid(1, 50); | 95 AddPlaceholderForPid(1, 50, false); |
| 70 FinalizePid(1, 100u); | 96 FinalizePid(1, 100u); |
| 71 | 97 |
| 72 EXPECT_EQ(1, GetChildProcessCount(50)); | 98 EXPECT_EQ(1, GetChildProcessCount(50)); |
| 73 InvalidateChildProcessId(50); | 99 InvalidateChildProcessId(50); |
| 74 EXPECT_EQ(0, GetChildProcessCount(50)); | 100 EXPECT_EQ(0, GetChildProcessCount(50)); |
| 75 } | 101 } |
| 76 | 102 |
| 77 TEST_F(MachBrokerTest, FinalizeUnknownPid) { | 103 TEST_F(MachBrokerTest, FinalizeUnknownPid) { |
| 78 // Finalizing an entry for an unknown pid should not add it to the map. | 104 // Finalizing an entry for an unknown pid should not add it to the map. |
| 79 FinalizePid(1u, 100u); | 105 FinalizePid(1u, 100u); |
| 80 EXPECT_EQ(0u, broker_.TaskForPid(1u)); | 106 EXPECT_EQ(0u, broker_.TaskForPid(1u)); |
| 81 } | 107 } |
| 82 | 108 |
| 109 TEST_F(MachBrokerTest, IOSurfaces) { |
| 110 const int kIOSurfaceId = 0; |
| 111 // Add a placeholder and finalize PID 1 (GPU process). |
| 112 AddPlaceholderForPid(1, 1, true); |
| 113 FinalizePid(1, 100u); |
| 114 |
| 115 // Add a placeholder and finalize PID 2. |
| 116 AddPlaceholderForPid(2, 2, false); |
| 117 FinalizePid(2, 101u); |
| 118 |
| 119 // Add a placeholder and finalize PID 3. |
| 120 AddPlaceholderForPid(3, 3, false); |
| 121 FinalizePid(3, 103u); |
| 122 |
| 123 // Attempt to register an IOSurface using PID 2. Should fail as only GPU |
| 124 // process is allowed to register IOSurfaces. |
| 125 bool rv = RegisterIOSurface(2, kIOSurfaceId, 2, 200u); |
| 126 EXPECT_FALSE(rv); |
| 127 EXPECT_EQ(0u, AcquireIOSurface(2, kIOSurfaceId)); |
| 128 |
| 129 // Register an IOSurface using PID 1 for usage by child process id 2. |
| 130 rv = RegisterIOSurface(1, kIOSurfaceId, 2, 200u); |
| 131 EXPECT_TRUE(rv); |
| 132 |
| 133 // PID 3 should not be able to acquire an IOSurface reference. |
| 134 EXPECT_EQ(0u, AcquireIOSurface(3, kIOSurfaceId)); |
| 135 |
| 136 // PID 2 should be able to acquire an IOSurface reference. |
| 137 EXPECT_EQ(200u, AcquireIOSurface(2, kIOSurfaceId)); |
| 138 |
| 139 // PID 2/3 should not be able to unregister IOSurface. |
| 140 UnregisterIOSurface(2, kIOSurfaceId, 2); |
| 141 UnregisterIOSurface(3, kIOSurfaceId, 2); |
| 142 EXPECT_EQ(200u, AcquireIOSurface(2, kIOSurfaceId)); |
| 143 |
| 144 // GPU process can unregister IOSurface. |
| 145 UnregisterIOSurface(1, kIOSurfaceId, 2); |
| 146 EXPECT_EQ(0u, AcquireIOSurface(2, kIOSurfaceId)); |
| 147 } |
| 148 |
| 83 } // namespace content | 149 } // namespace content |
| OLD | NEW |