| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/child/child_io_surface_manager_mac.h" | |
| 6 | |
| 7 #include "base/mac/mach_logging.h" | |
| 8 #include "content/common/mac/io_surface_manager_messages.h" | |
| 9 | |
| 10 namespace content { | |
| 11 | |
| 12 // static | |
| 13 ChildIOSurfaceManager* ChildIOSurfaceManager::GetInstance() { | |
| 14 return base::Singleton< | |
| 15 ChildIOSurfaceManager, | |
| 16 base::LeakySingletonTraits<ChildIOSurfaceManager>>::get(); | |
| 17 } | |
| 18 | |
| 19 bool ChildIOSurfaceManager::RegisterIOSurface(gfx::IOSurfaceId io_surface_id, | |
| 20 int client_id, | |
| 21 IOSurfaceRef io_surface) { | |
| 22 DCHECK(service_port_.is_valid()); | |
| 23 CHECK(!token_.IsZero()); | |
| 24 | |
| 25 mach_port_t reply_port; | |
| 26 kern_return_t kr = mach_port_allocate(mach_task_self(), | |
| 27 MACH_PORT_RIGHT_RECEIVE, &reply_port); | |
| 28 if (kr != KERN_SUCCESS) { | |
| 29 MACH_LOG(ERROR, kr) << "mach_port_allocate"; | |
| 30 return false; | |
| 31 } | |
| 32 base::mac::ScopedMachReceiveRight scoped_receive_right(reply_port); | |
| 33 | |
| 34 // Deallocate the right after sending a copy to the parent. | |
| 35 base::mac::ScopedMachSendRight scoped_io_surface_right( | |
| 36 IOSurfaceCreateMachPort(io_surface)); | |
| 37 | |
| 38 union { | |
| 39 IOSurfaceManagerHostMsg_RegisterIOSurface request; | |
| 40 struct { | |
| 41 IOSurfaceManagerMsg_RegisterIOSurfaceReply msg; | |
| 42 mach_msg_trailer_t trailer; | |
| 43 } reply; | |
| 44 } data = {{{0}}}; | |
| 45 data.request.header.msgh_bits = | |
| 46 MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, MACH_MSG_TYPE_MAKE_SEND_ONCE) | | |
| 47 MACH_MSGH_BITS_COMPLEX; | |
| 48 data.request.header.msgh_remote_port = service_port_.get(); | |
| 49 data.request.header.msgh_local_port = reply_port; | |
| 50 data.request.header.msgh_size = sizeof(data.request); | |
| 51 data.request.header.msgh_id = IOSurfaceManagerHostMsg_RegisterIOSurface::ID; | |
| 52 data.request.body.msgh_descriptor_count = 1; | |
| 53 data.request.io_surface_port.name = scoped_io_surface_right.get(); | |
| 54 data.request.io_surface_port.disposition = MACH_MSG_TYPE_COPY_SEND; | |
| 55 data.request.io_surface_port.type = MACH_MSG_PORT_DESCRIPTOR; | |
| 56 data.request.io_surface_id = io_surface_id.id; | |
| 57 data.request.client_id = client_id; | |
| 58 memcpy(data.request.token_name, token_.name, sizeof(token_.name)); | |
| 59 | |
| 60 kr = mach_msg(&data.request.header, MACH_SEND_MSG | MACH_RCV_MSG, | |
| 61 sizeof(data.request), sizeof(data.reply), reply_port, | |
| 62 MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL); | |
| 63 if (kr != KERN_SUCCESS) { | |
| 64 MACH_LOG(ERROR, kr) << "mach_msg"; | |
| 65 return false; | |
| 66 } | |
| 67 | |
| 68 return data.reply.msg.result; | |
| 69 } | |
| 70 | |
| 71 void ChildIOSurfaceManager::UnregisterIOSurface(gfx::IOSurfaceId io_surface_id, | |
| 72 int client_id) { | |
| 73 DCHECK(service_port_.is_valid()); | |
| 74 CHECK(!token_.IsZero()); | |
| 75 | |
| 76 IOSurfaceManagerHostMsg_UnregisterIOSurface request = {{0}}; | |
| 77 request.header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0); | |
| 78 request.header.msgh_remote_port = service_port_.get(); | |
| 79 request.header.msgh_local_port = MACH_PORT_NULL; | |
| 80 request.header.msgh_size = sizeof(request); | |
| 81 request.header.msgh_id = IOSurfaceManagerHostMsg_UnregisterIOSurface::ID; | |
| 82 request.io_surface_id = io_surface_id.id; | |
| 83 request.client_id = client_id; | |
| 84 memcpy(request.token_name, token_.name, sizeof(token_.name)); | |
| 85 | |
| 86 kern_return_t kr = | |
| 87 mach_msg(&request.header, MACH_SEND_MSG, sizeof(request), 0, | |
| 88 MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL); | |
| 89 if (kr != KERN_SUCCESS) { | |
| 90 MACH_LOG(ERROR, kr) << "mach_msg"; | |
| 91 } | |
| 92 } | |
| 93 | |
| 94 IOSurfaceRef ChildIOSurfaceManager::AcquireIOSurface( | |
| 95 gfx::IOSurfaceId io_surface_id) { | |
| 96 DCHECK(service_port_.is_valid()); | |
| 97 CHECK(!token_.IsZero()); | |
| 98 | |
| 99 mach_port_t reply_port; | |
| 100 kern_return_t kr = mach_port_allocate(mach_task_self(), | |
| 101 MACH_PORT_RIGHT_RECEIVE, &reply_port); | |
| 102 CHECK_EQ(KERN_SUCCESS, kr); | |
| 103 base::mac::ScopedMachReceiveRight scoped_receive_right(reply_port); | |
| 104 | |
| 105 union { | |
| 106 IOSurfaceManagerHostMsg_AcquireIOSurface request; | |
| 107 struct { | |
| 108 IOSurfaceManagerMsg_AcquireIOSurfaceReply msg; | |
| 109 mach_msg_trailer_t trailer; | |
| 110 } reply; | |
| 111 } data = {{{0}}}; | |
| 112 data.request.header.msgh_bits = | |
| 113 MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, MACH_MSG_TYPE_MAKE_SEND_ONCE); | |
| 114 data.request.header.msgh_remote_port = service_port_.get(); | |
| 115 data.request.header.msgh_local_port = reply_port; | |
| 116 data.request.header.msgh_size = sizeof(data.request); | |
| 117 data.request.header.msgh_id = IOSurfaceManagerHostMsg_AcquireIOSurface::ID; | |
| 118 data.request.io_surface_id = io_surface_id.id; | |
| 119 memcpy(data.request.token_name, token_.name, sizeof(token_.name)); | |
| 120 | |
| 121 kr = mach_msg(&data.request.header, MACH_SEND_MSG | MACH_RCV_MSG, | |
| 122 sizeof(data.request), sizeof(data.reply), reply_port, | |
| 123 MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL); | |
| 124 if (kr != KERN_SUCCESS) { | |
| 125 MACH_LOG(ERROR, kr) << "mach_msg"; | |
| 126 return nullptr; | |
| 127 } | |
| 128 if (!data.reply.msg.result) { | |
| 129 DLOG(ERROR) << "Browser refused AcquireIOSurface request"; | |
| 130 return nullptr; | |
| 131 } | |
| 132 | |
| 133 // Deallocate the right after creating an IOSurface reference. | |
| 134 base::mac::ScopedMachSendRight scoped_io_surface_right( | |
| 135 data.reply.msg.io_surface_port.name); | |
| 136 | |
| 137 // If a port was successfully received, it should be valid, and opening it | |
| 138 // should not fail. | |
| 139 CHECK(scoped_io_surface_right.is_valid()); | |
| 140 IOSurfaceRef io_surface = IOSurfaceLookupFromMachPort( | |
| 141 scoped_io_surface_right.get()); | |
| 142 CHECK(io_surface); | |
| 143 return io_surface; | |
| 144 } | |
| 145 | |
| 146 ChildIOSurfaceManager::ChildIOSurfaceManager() {} | |
| 147 | |
| 148 ChildIOSurfaceManager::~ChildIOSurfaceManager() {} | |
| 149 | |
| 150 } // namespace content | |
| OLD | NEW |