Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / content / browser / browser_io_surface_manager_mac.cc
blob519b48c684ad00058f38b6bf4dbcd78b895572f9
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 base::Singleton<
35 BrowserIOSurfaceManager,
36 base::LeakySingletonTraits<BrowserIOSurfaceManager>>::get();
39 // static
40 base::mac::ScopedMachSendRight BrowserIOSurfaceManager::LookupServicePort(
41 pid_t pid) {
42 // Look up the named IOSurfaceManager port that's been registered with
43 // the bootstrap server.
44 mach_port_t port;
45 kern_return_t kr =
46 bootstrap_look_up(bootstrap_port, GetMachPortName(pid).c_str(), &port);
47 if (kr != KERN_SUCCESS) {
48 BOOTSTRAP_LOG(ERROR, kr) << "bootstrap_look_up";
49 return base::mac::ScopedMachSendRight();
52 return base::mac::ScopedMachSendRight(port);
55 bool BrowserIOSurfaceManager::RegisterIOSurface(IOSurfaceId io_surface_id,
56 int client_id,
57 IOSurfaceRef io_surface) {
58 base::AutoLock lock(lock_);
60 IOSurfaceMapKey key(io_surface_id, client_id);
61 DCHECK(io_surfaces_.find(key) == io_surfaces_.end());
62 io_surfaces_.add(key, make_scoped_ptr(new base::mac::ScopedMachSendRight(
63 IOSurfaceCreateMachPort(io_surface))));
64 return true;
67 void BrowserIOSurfaceManager::UnregisterIOSurface(IOSurfaceId io_surface_id,
68 int client_id) {
69 base::AutoLock lock(lock_);
71 IOSurfaceMapKey key(io_surface_id, client_id);
72 DCHECK(io_surfaces_.find(key) != io_surfaces_.end());
73 io_surfaces_.erase(key);
76 IOSurfaceRef BrowserIOSurfaceManager::AcquireIOSurface(
77 IOSurfaceId io_surface_id) {
78 base::AutoLock lock(lock_);
80 IOSurfaceMapKey key(
81 io_surface_id,
82 BrowserGpuChannelHostFactory::instance()->GetGpuChannelId());
83 auto it = io_surfaces_.find(key);
84 if (it == io_surfaces_.end()) {
85 LOG(ERROR) << "Invalid Id for IOSurface " << io_surface_id.id;
86 return nullptr;
89 return IOSurfaceLookupFromMachPort(it->second->get());
92 void BrowserIOSurfaceManager::EnsureRunning() {
93 base::AutoLock lock(lock_);
95 if (initialized_)
96 return;
98 // Do not attempt to reinitialize in the event of failure.
99 initialized_ = true;
101 if (!Initialize()) {
102 LOG(ERROR) << "Failed to initialize the BrowserIOSurfaceManager";
106 IOSurfaceManagerToken BrowserIOSurfaceManager::GenerateGpuProcessToken() {
107 base::AutoLock lock(lock_);
109 DCHECK(gpu_process_token_.IsZero());
110 gpu_process_token_ = IOSurfaceManagerToken::Generate();
111 DCHECK(gpu_process_token_.Verify());
112 return gpu_process_token_;
115 void BrowserIOSurfaceManager::InvalidateGpuProcessToken() {
116 base::AutoLock lock(lock_);
118 DCHECK(!gpu_process_token_.IsZero());
119 gpu_process_token_.SetZero();
120 io_surfaces_.clear();
123 IOSurfaceManagerToken BrowserIOSurfaceManager::GenerateChildProcessToken(
124 int child_process_id) {
125 base::AutoLock lock(lock_);
127 IOSurfaceManagerToken token = IOSurfaceManagerToken::Generate();
128 DCHECK(token.Verify());
129 child_process_ids_[token] = child_process_id;
130 return token;
133 void BrowserIOSurfaceManager::InvalidateChildProcessToken(
134 const IOSurfaceManagerToken& token) {
135 base::AutoLock lock(lock_);
137 DCHECK(child_process_ids_.find(token) != child_process_ids_.end());
138 child_process_ids_.erase(token);
141 BrowserIOSurfaceManager::BrowserIOSurfaceManager() : initialized_(false) {
144 BrowserIOSurfaceManager::~BrowserIOSurfaceManager() {
147 bool BrowserIOSurfaceManager::Initialize() {
148 lock_.AssertAcquired();
149 DCHECK(!server_port_.is_valid());
151 // Check in with launchd and publish the service name.
152 mach_port_t port;
153 kern_return_t kr = bootstrap_check_in(
154 bootstrap_port, GetMachPortName(getpid()).c_str(), &port);
155 if (kr != KERN_SUCCESS) {
156 BOOTSTRAP_LOG(ERROR, kr) << "bootstrap_check_in";
157 return false;
159 server_port_.reset(port);
161 // Start the dispatch source.
162 std::string queue_name =
163 base::StringPrintf("%s.IOSurfaceManager", base::mac::BaseBundleID());
164 dispatch_source_.reset(
165 new base::DispatchSourceMach(queue_name.c_str(), server_port_.get(), ^{
166 HandleRequest();
167 }));
168 dispatch_source_->Resume();
170 return true;
173 void BrowserIOSurfaceManager::HandleRequest() {
174 struct {
175 union {
176 mach_msg_header_t header;
177 IOSurfaceManagerHostMsg_RegisterIOSurface register_io_surface;
178 IOSurfaceManagerHostMsg_UnregisterIOSurface unregister_io_surface;
179 IOSurfaceManagerHostMsg_AcquireIOSurface acquire_io_surface;
180 } msg;
181 mach_msg_trailer_t trailer;
182 } request = {{{0}}};
183 request.msg.header.msgh_size = sizeof(request);
184 request.msg.header.msgh_local_port = server_port_.get();
186 kern_return_t kr =
187 mach_msg(&request.msg.header, MACH_RCV_MSG, 0, sizeof(request),
188 server_port_, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
189 if (kr != KERN_SUCCESS) {
190 MACH_LOG(ERROR, kr) << "mach_msg";
191 return;
194 union {
195 mach_msg_header_t header;
196 IOSurfaceManagerMsg_RegisterIOSurfaceReply register_io_surface;
197 IOSurfaceManagerMsg_AcquireIOSurfaceReply acquire_io_surface;
198 } reply = {{0}};
200 switch (request.msg.header.msgh_id) {
201 case IOSurfaceManagerHostMsg_RegisterIOSurface::ID:
202 if (!HandleRegisterIOSurfaceRequest(request.msg.register_io_surface,
203 &reply.register_io_surface)) {
204 return;
206 break;
207 case IOSurfaceManagerHostMsg_UnregisterIOSurface::ID:
208 HandleUnregisterIOSurfaceRequest(request.msg.unregister_io_surface);
209 // Unregister requests are asynchronous and do not require a reply as
210 // there is no guarantee for how quickly an IO surface is removed from
211 // the IOSurfaceManager instance after it has been deleted by a child
212 // process.
213 return;
214 case IOSurfaceManagerHostMsg_AcquireIOSurface::ID:
215 if (!HandleAcquireIOSurfaceRequest(request.msg.acquire_io_surface,
216 &reply.acquire_io_surface)) {
217 return;
219 break;
220 default:
221 LOG(ERROR) << "Unknown message received!";
222 return;
225 kr = mach_msg(&reply.header, MACH_SEND_MSG | MACH_SEND_TIMEOUT,
226 reply.header.msgh_size, 0, MACH_PORT_NULL, kSendReplyTimeoutMs,
227 MACH_PORT_NULL);
228 if (kr != KERN_SUCCESS) {
229 MACH_LOG(ERROR, kr) << "mach_msg";
233 bool BrowserIOSurfaceManager::HandleRegisterIOSurfaceRequest(
234 const IOSurfaceManagerHostMsg_RegisterIOSurface& request,
235 IOSurfaceManagerMsg_RegisterIOSurfaceReply* reply) {
236 base::AutoLock lock(lock_);
238 IOSurfaceManagerToken token;
239 static_assert(sizeof(request.token_name) == sizeof(token.name),
240 "Mach message token size doesn't match expectation.");
241 token.SetName(request.token_name);
242 if (token.IsZero() || token != gpu_process_token_) {
243 LOG(ERROR) << "Illegal message from non-GPU process!";
244 return false;
247 IOSurfaceMapKey key(IOSurfaceId(request.io_surface_id), request.client_id);
248 io_surfaces_.add(key, make_scoped_ptr(new base::mac::ScopedMachSendRight(
249 request.io_surface_port.name)));
251 reply->header.msgh_bits = MACH_MSGH_BITS_REMOTE(request.header.msgh_bits);
252 reply->header.msgh_remote_port = request.header.msgh_remote_port;
253 reply->header.msgh_size = sizeof(*reply);
254 reply->result = true;
255 return true;
258 bool BrowserIOSurfaceManager::HandleUnregisterIOSurfaceRequest(
259 const IOSurfaceManagerHostMsg_UnregisterIOSurface& request) {
260 base::AutoLock lock(lock_);
262 IOSurfaceManagerToken token;
263 static_assert(sizeof(request.token_name) == sizeof(token.name),
264 "Mach message token size doesn't match expectation.");
265 token.SetName(request.token_name);
266 if (token.IsZero() || token != gpu_process_token_) {
267 LOG(ERROR) << "Illegal message from non-GPU process!";
268 return false;
271 IOSurfaceMapKey key(IOSurfaceId(request.io_surface_id), request.client_id);
272 io_surfaces_.erase(key);
273 return true;
276 bool BrowserIOSurfaceManager::HandleAcquireIOSurfaceRequest(
277 const IOSurfaceManagerHostMsg_AcquireIOSurface& request,
278 IOSurfaceManagerMsg_AcquireIOSurfaceReply* reply) {
279 base::AutoLock lock(lock_);
281 IOSurfaceManagerToken token;
282 static_assert(sizeof(request.token_name) == sizeof(token.name),
283 "Mach message token size doesn't match expectation.");
284 token.SetName(request.token_name);
285 auto child_process_id_it = child_process_ids_.find(token);
286 if (child_process_id_it == child_process_ids_.end()) {
287 LOG(ERROR) << "Illegal message from non-child process!";
288 return false;
291 reply->header.msgh_bits =
292 MACH_MSGH_BITS_REMOTE(request.header.msgh_bits) | MACH_MSGH_BITS_COMPLEX;
293 reply->header.msgh_remote_port = request.header.msgh_remote_port;
294 reply->header.msgh_size = sizeof(*reply);
296 IOSurfaceMapKey key(IOSurfaceId(request.io_surface_id),
297 child_process_id_it->second);
298 auto it = io_surfaces_.find(key);
299 if (it == io_surfaces_.end()) {
300 LOG(ERROR) << "Invalid Id for IOSurface " << request.io_surface_id;
301 return true;
304 reply->body.msgh_descriptor_count = 1;
305 reply->io_surface_port.name = it->second->get();
306 reply->io_surface_port.disposition = MACH_MSG_TYPE_COPY_SEND;
307 reply->io_surface_port.type = MACH_MSG_PORT_DESCRIPTOR;
308 return true;
311 } // namespace content