Make USB permissions work in the new permission message system
[chromium-blink-merge.git] / content / browser / browser_io_surface_manager_mac.cc
blobdbc87e66d2371fe51f4be324a6c2df055ae6f826
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/browser/browser_io_surface_manager_mac.h"
7 #include <servers/bootstrap.h>
9 #include <string>
11 #include "base/logging.h"
12 #include "base/mac/foundation_util.h"
13 #include "base/mac/mach_logging.h"
14 #include "base/strings/stringprintf.h"
15 #include "content/browser/gpu/browser_gpu_channel_host_factory.h"
17 namespace content {
18 namespace {
20 // Returns the Mach port name to use when sending or receiving messages. |pid|
21 // is the process ID of the service.
22 std::string GetMachPortName(pid_t pid) {
23 return base::StringPrintf("%s.iosurfacemgr.%d", base::mac::BaseBundleID(),
24 pid);
27 // Amount of time to wait before giving up when sending a reply message.
28 const int kSendReplyTimeoutMs = 100;
30 } // namespace
32 // static
33 BrowserIOSurfaceManager* BrowserIOSurfaceManager::GetInstance() {
34 return Singleton<BrowserIOSurfaceManager,
35 LeakySingletonTraits<BrowserIOSurfaceManager>>::get();
38 // static
39 base::mac::ScopedMachSendRight BrowserIOSurfaceManager::LookupServicePort(
40 pid_t pid) {
41 // Look up the named IOSurfaceManager port that's been registered with
42 // the bootstrap server.
43 mach_port_t port;
44 kern_return_t kr =
45 bootstrap_look_up(bootstrap_port, GetMachPortName(pid).c_str(), &port);
46 if (kr != KERN_SUCCESS) {
47 BOOTSTRAP_LOG(ERROR, kr) << "bootstrap_look_up";
48 return base::mac::ScopedMachSendRight();
51 return base::mac::ScopedMachSendRight(port);
54 bool BrowserIOSurfaceManager::RegisterIOSurface(IOSurfaceId io_surface_id,
55 int client_id,
56 IOSurfaceRef io_surface) {
57 base::AutoLock lock(lock_);
59 IOSurfaceMapKey key(io_surface_id, client_id);
60 DCHECK(io_surfaces_.find(key) == io_surfaces_.end());
61 io_surfaces_.add(key, make_scoped_ptr(new base::mac::ScopedMachSendRight(
62 IOSurfaceCreateMachPort(io_surface))));
63 return true;
66 void BrowserIOSurfaceManager::UnregisterIOSurface(IOSurfaceId io_surface_id,
67 int client_id) {
68 base::AutoLock lock(lock_);
70 IOSurfaceMapKey key(io_surface_id, client_id);
71 DCHECK(io_surfaces_.find(key) != io_surfaces_.end());
72 io_surfaces_.erase(key);
75 IOSurfaceRef BrowserIOSurfaceManager::AcquireIOSurface(
76 IOSurfaceId io_surface_id) {
77 base::AutoLock lock(lock_);
79 IOSurfaceMapKey key(
80 io_surface_id,
81 BrowserGpuChannelHostFactory::instance()->GetGpuChannelId());
82 auto it = io_surfaces_.find(key);
83 if (it == io_surfaces_.end()) {
84 LOG(ERROR) << "Invalid Id for IOSurface " << io_surface_id.id;
85 return nullptr;
88 return IOSurfaceLookupFromMachPort(it->second->get());
91 void BrowserIOSurfaceManager::EnsureRunning() {
92 base::AutoLock lock(lock_);
94 if (initialized_)
95 return;
97 // Do not attempt to reinitialize in the event of failure.
98 initialized_ = true;
100 if (!Initialize()) {
101 LOG(ERROR) << "Failed to initialize the BrowserIOSurfaceManager";
105 IOSurfaceManagerToken BrowserIOSurfaceManager::GenerateGpuProcessToken() {
106 base::AutoLock lock(lock_);
108 DCHECK(gpu_process_token_.IsZero());
109 gpu_process_token_ = IOSurfaceManagerToken::Generate();
110 DCHECK(gpu_process_token_.Verify());
111 return gpu_process_token_;
114 void BrowserIOSurfaceManager::InvalidateGpuProcessToken() {
115 base::AutoLock lock(lock_);
117 DCHECK(!gpu_process_token_.IsZero());
118 gpu_process_token_.SetZero();
119 io_surfaces_.clear();
122 IOSurfaceManagerToken BrowserIOSurfaceManager::GenerateChildProcessToken(
123 int child_process_id) {
124 base::AutoLock lock(lock_);
126 IOSurfaceManagerToken token = IOSurfaceManagerToken::Generate();
127 DCHECK(token.Verify());
128 child_process_ids_[token] = child_process_id;
129 return token;
132 void BrowserIOSurfaceManager::InvalidateChildProcessToken(
133 const IOSurfaceManagerToken& token) {
134 base::AutoLock lock(lock_);
136 DCHECK(child_process_ids_.find(token) != child_process_ids_.end());
137 child_process_ids_.erase(token);
140 BrowserIOSurfaceManager::BrowserIOSurfaceManager() : initialized_(false) {
143 BrowserIOSurfaceManager::~BrowserIOSurfaceManager() {
146 bool BrowserIOSurfaceManager::Initialize() {
147 lock_.AssertAcquired();
148 DCHECK(!server_port_.is_valid());
150 // Check in with launchd and publish the service name.
151 mach_port_t port;
152 kern_return_t kr = bootstrap_check_in(
153 bootstrap_port, GetMachPortName(getpid()).c_str(), &port);
154 if (kr != KERN_SUCCESS) {
155 BOOTSTRAP_LOG(ERROR, kr) << "bootstrap_check_in";
156 return false;
158 server_port_.reset(port);
160 // Start the dispatch source.
161 std::string queue_name =
162 base::StringPrintf("%s.IOSurfaceManager", base::mac::BaseBundleID());
163 dispatch_source_.reset(
164 new base::DispatchSourceMach(queue_name.c_str(), server_port_.get(), ^{
165 HandleRequest();
166 }));
167 dispatch_source_->Resume();
169 return true;
172 void BrowserIOSurfaceManager::HandleRequest() {
173 struct {
174 union {
175 mach_msg_header_t header;
176 IOSurfaceManagerHostMsg_RegisterIOSurface register_io_surface;
177 IOSurfaceManagerHostMsg_UnregisterIOSurface unregister_io_surface;
178 IOSurfaceManagerHostMsg_AcquireIOSurface acquire_io_surface;
179 } msg;
180 mach_msg_trailer_t trailer;
181 } request = {{{0}}};
182 request.msg.header.msgh_size = sizeof(request);
183 request.msg.header.msgh_local_port = server_port_.get();
185 kern_return_t kr =
186 mach_msg(&request.msg.header, MACH_RCV_MSG, 0, sizeof(request),
187 server_port_, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
188 if (kr != KERN_SUCCESS) {
189 MACH_LOG(ERROR, kr) << "mach_msg";
190 return;
193 union {
194 mach_msg_header_t header;
195 IOSurfaceManagerMsg_RegisterIOSurfaceReply register_io_surface;
196 IOSurfaceManagerMsg_AcquireIOSurfaceReply acquire_io_surface;
197 } reply = {{0}};
199 switch (request.msg.header.msgh_id) {
200 case IOSurfaceManagerHostMsg_RegisterIOSurface::ID:
201 if (!HandleRegisterIOSurfaceRequest(request.msg.register_io_surface,
202 &reply.register_io_surface)) {
203 return;
205 break;
206 case IOSurfaceManagerHostMsg_UnregisterIOSurface::ID:
207 HandleUnregisterIOSurfaceRequest(request.msg.unregister_io_surface);
208 // Unregister requests are asynchronous and do not require a reply as
209 // there is no guarantee for how quickly an IO surface is removed from
210 // the IOSurfaceManager instance after it has been deleted by a child
211 // process.
212 return;
213 case IOSurfaceManagerHostMsg_AcquireIOSurface::ID:
214 if (!HandleAcquireIOSurfaceRequest(request.msg.acquire_io_surface,
215 &reply.acquire_io_surface)) {
216 return;
218 break;
219 default:
220 LOG(ERROR) << "Unknown message received!";
221 return;
224 kr = mach_msg(&reply.header, MACH_SEND_MSG | MACH_SEND_TIMEOUT,
225 reply.header.msgh_size, 0, MACH_PORT_NULL, kSendReplyTimeoutMs,
226 MACH_PORT_NULL);
227 if (kr != KERN_SUCCESS) {
228 MACH_LOG(ERROR, kr) << "mach_msg";
232 bool BrowserIOSurfaceManager::HandleRegisterIOSurfaceRequest(
233 const IOSurfaceManagerHostMsg_RegisterIOSurface& request,
234 IOSurfaceManagerMsg_RegisterIOSurfaceReply* reply) {
235 base::AutoLock lock(lock_);
237 IOSurfaceManagerToken token;
238 static_assert(sizeof(request.token_name) == sizeof(token.name),
239 "Mach message token size doesn't match expectation.");
240 token.SetName(request.token_name);
241 if (token.IsZero() || token != gpu_process_token_) {
242 LOG(ERROR) << "Illegal message from non-GPU process!";
243 return false;
246 IOSurfaceMapKey key(IOSurfaceId(request.io_surface_id), request.client_id);
247 io_surfaces_.add(key, make_scoped_ptr(new base::mac::ScopedMachSendRight(
248 request.io_surface_port.name)));
250 reply->header.msgh_bits = MACH_MSGH_BITS_REMOTE(request.header.msgh_bits);
251 reply->header.msgh_remote_port = request.header.msgh_remote_port;
252 reply->header.msgh_size = sizeof(*reply);
253 reply->result = true;
254 return true;
257 bool BrowserIOSurfaceManager::HandleUnregisterIOSurfaceRequest(
258 const IOSurfaceManagerHostMsg_UnregisterIOSurface& request) {
259 base::AutoLock lock(lock_);
261 IOSurfaceManagerToken token;
262 static_assert(sizeof(request.token_name) == sizeof(token.name),
263 "Mach message token size doesn't match expectation.");
264 token.SetName(request.token_name);
265 if (token.IsZero() || token != gpu_process_token_) {
266 LOG(ERROR) << "Illegal message from non-GPU process!";
267 return false;
270 IOSurfaceMapKey key(IOSurfaceId(request.io_surface_id), request.client_id);
271 io_surfaces_.erase(key);
272 return true;
275 bool BrowserIOSurfaceManager::HandleAcquireIOSurfaceRequest(
276 const IOSurfaceManagerHostMsg_AcquireIOSurface& request,
277 IOSurfaceManagerMsg_AcquireIOSurfaceReply* reply) {
278 base::AutoLock lock(lock_);
280 IOSurfaceManagerToken token;
281 static_assert(sizeof(request.token_name) == sizeof(token.name),
282 "Mach message token size doesn't match expectation.");
283 token.SetName(request.token_name);
284 auto child_process_id_it = child_process_ids_.find(token);
285 if (child_process_id_it == child_process_ids_.end()) {
286 LOG(ERROR) << "Illegal message from non-child process!";
287 return false;
290 reply->header.msgh_bits =
291 MACH_MSGH_BITS_REMOTE(request.header.msgh_bits) | MACH_MSGH_BITS_COMPLEX;
292 reply->header.msgh_remote_port = request.header.msgh_remote_port;
293 reply->header.msgh_size = sizeof(*reply);
295 IOSurfaceMapKey key(IOSurfaceId(request.io_surface_id),
296 child_process_id_it->second);
297 auto it = io_surfaces_.find(key);
298 if (it == io_surfaces_.end()) {
299 LOG(ERROR) << "Invalid Id for IOSurface " << request.io_surface_id;
300 return true;
303 reply->body.msgh_descriptor_count = 1;
304 reply->io_surface_port.name = it->second->get();
305 reply->io_surface_port.disposition = MACH_MSG_TYPE_COPY_SEND;
306 reply->io_surface_port.type = MACH_MSG_PORT_DESCRIPTOR;
307 return true;
310 } // namespace content