Make USB permissions work in the new permission message system
[chromium-blink-merge.git] / content / child / child_io_surface_manager_mac.cc
blob65df72b04aa034053d80a292362a0c234f574884
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.
5 #include "content/child/child_io_surface_manager_mac.h"
7 #include "base/mac/mach_logging.h"
8 #include "content/common/mac/io_surface_manager_messages.h"
10 namespace content {
12 // static
13 ChildIOSurfaceManager* ChildIOSurfaceManager::GetInstance() {
14 return Singleton<ChildIOSurfaceManager,
15 LeakySingletonTraits<ChildIOSurfaceManager>>::get();
18 bool ChildIOSurfaceManager::RegisterIOSurface(IOSurfaceId io_surface_id,
19 int client_id,
20 IOSurfaceRef io_surface) {
21 DCHECK(service_port_.is_valid());
22 DCHECK(!token_.IsZero());
24 mach_port_t reply_port;
25 kern_return_t kr = mach_port_allocate(mach_task_self(),
26 MACH_PORT_RIGHT_RECEIVE, &reply_port);
27 if (kr != KERN_SUCCESS) {
28 MACH_LOG(ERROR, kr) << "mach_port_allocate";
29 return false;
31 base::mac::ScopedMachReceiveRight scoped_receive_right(reply_port);
33 // Deallocate the right after sending a copy to the parent.
34 base::mac::ScopedMachSendRight scoped_io_surface_right(
35 IOSurfaceCreateMachPort(io_surface));
37 union {
38 IOSurfaceManagerHostMsg_RegisterIOSurface request;
39 struct {
40 IOSurfaceManagerMsg_RegisterIOSurfaceReply msg;
41 mach_msg_trailer_t trailer;
42 } reply;
43 } data = {{{0}}};
44 data.request.header.msgh_bits =
45 MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, MACH_MSG_TYPE_MAKE_SEND_ONCE) |
46 MACH_MSGH_BITS_COMPLEX;
47 data.request.header.msgh_remote_port = service_port_;
48 data.request.header.msgh_local_port = reply_port;
49 data.request.header.msgh_size = sizeof(data.request);
50 data.request.header.msgh_id = IOSurfaceManagerHostMsg_RegisterIOSurface::ID;
51 data.request.body.msgh_descriptor_count = 1;
52 data.request.io_surface_port.name = scoped_io_surface_right;
53 data.request.io_surface_port.disposition = MACH_MSG_TYPE_COPY_SEND;
54 data.request.io_surface_port.type = MACH_MSG_PORT_DESCRIPTOR;
55 data.request.io_surface_id = io_surface_id.id;
56 data.request.client_id = client_id;
57 memcpy(data.request.token_name, token_.name, sizeof(token_.name));
59 kr = mach_msg(&data.request.header, MACH_SEND_MSG | MACH_RCV_MSG,
60 sizeof(data.request), sizeof(data.reply), reply_port,
61 MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
62 if (kr != KERN_SUCCESS) {
63 MACH_LOG(ERROR, kr) << "mach_msg";
64 return false;
67 return data.reply.msg.result;
70 void ChildIOSurfaceManager::UnregisterIOSurface(IOSurfaceId io_surface_id,
71 int client_id) {
72 DCHECK(service_port_.is_valid());
73 DCHECK(!token_.IsZero());
75 IOSurfaceManagerHostMsg_UnregisterIOSurface request = {{0}};
76 request.header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0);
77 request.header.msgh_remote_port = service_port_;
78 request.header.msgh_local_port = MACH_PORT_NULL;
79 request.header.msgh_size = sizeof(request);
80 request.header.msgh_id = IOSurfaceManagerHostMsg_UnregisterIOSurface::ID;
81 request.io_surface_id = io_surface_id.id;
82 request.client_id = client_id;
83 memcpy(request.token_name, token_.name, sizeof(token_.name));
85 kern_return_t kr =
86 mach_msg(&request.header, MACH_SEND_MSG, sizeof(request), 0,
87 MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
88 if (kr != KERN_SUCCESS) {
89 MACH_LOG(ERROR, kr) << "mach_msg";
93 IOSurfaceRef ChildIOSurfaceManager::AcquireIOSurface(
94 IOSurfaceId io_surface_id) {
95 DCHECK(service_port_.is_valid());
97 // A valid token is required to acquire an IOSurface. This will wait for a
98 // valid token if one has not yet been set.
99 set_token_event_.Wait();
100 #if !defined(NDEBUG)
101 DCHECK(!(set_token_thread_id_ == base::PlatformThread::CurrentRef()));
102 #endif
103 DCHECK(!token_.IsZero());
105 mach_port_t reply_port;
106 kern_return_t kr = mach_port_allocate(mach_task_self(),
107 MACH_PORT_RIGHT_RECEIVE, &reply_port);
108 if (kr != KERN_SUCCESS) {
109 MACH_LOG(ERROR, kr) << "mach_port_allocate";
110 return nullptr;
112 base::mac::ScopedMachReceiveRight scoped_receive_right(reply_port);
114 union {
115 IOSurfaceManagerHostMsg_AcquireIOSurface request;
116 struct {
117 IOSurfaceManagerMsg_AcquireIOSurfaceReply msg;
118 mach_msg_trailer_t trailer;
119 } reply;
120 } data = {{{0}}};
121 data.request.header.msgh_bits =
122 MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, MACH_MSG_TYPE_MAKE_SEND_ONCE);
123 data.request.header.msgh_remote_port = service_port_;
124 data.request.header.msgh_local_port = reply_port;
125 data.request.header.msgh_size = sizeof(data.request);
126 data.request.header.msgh_id = IOSurfaceManagerHostMsg_AcquireIOSurface::ID;
127 data.request.io_surface_id = io_surface_id.id;
128 memcpy(data.request.token_name, token_.name, sizeof(token_.name));
130 kr = mach_msg(&data.request.header, MACH_SEND_MSG | MACH_RCV_MSG,
131 sizeof(data.request), sizeof(data.reply), reply_port,
132 MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
133 if (kr != KERN_SUCCESS) {
134 MACH_LOG(ERROR, kr) << "mach_msg";
135 return nullptr;
138 // Deallocate the right after creating an IOSurface reference.
139 base::mac::ScopedMachSendRight scoped_io_surface_right(
140 data.reply.msg.io_surface_port.name);
142 return IOSurfaceLookupFromMachPort(scoped_io_surface_right);
145 ChildIOSurfaceManager::ChildIOSurfaceManager()
146 : set_token_event_(true, false) {}
148 ChildIOSurfaceManager::~ChildIOSurfaceManager() {
151 } // namespace content