1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/chromium/src/chrome/common/mach_message_source_mac.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,66 @@ 1.4 +// Copyright (c) 2008 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +#include "chrome/common/mach_message_source_mac.h" 1.9 + 1.10 +#include "base/logging.h" 1.11 + 1.12 +MachMessageSource::MachMessageSource(mach_port_t port, 1.13 + MachPortListener* msg_listener, 1.14 + bool* success) { 1.15 + DCHECK(msg_listener); 1.16 + DCHECK(success); 1.17 + DCHECK(port != MACH_PORT_NULL); 1.18 + 1.19 + CFMachPortContext port_context = {0}; 1.20 + port_context.info = msg_listener; 1.21 + 1.22 + scoped_cftyperef<CFMachPortRef> cf_mach_port_ref( 1.23 + CFMachPortCreateWithPort(kCFAllocatorDefault, 1.24 + port, 1.25 + MachMessageSource::OnReceiveMachMessage, 1.26 + &port_context, 1.27 + NULL)); 1.28 + 1.29 + if (cf_mach_port_ref.get() == NULL) { 1.30 + CHROMIUM_LOG(WARNING) << "CFMachPortCreate failed"; 1.31 + *success = false; 1.32 + return; 1.33 + } 1.34 + 1.35 + // Create a RL source. 1.36 + machport_runloop_ref_.reset( 1.37 + CFMachPortCreateRunLoopSource(kCFAllocatorDefault, 1.38 + cf_mach_port_ref.get(), 1.39 + 0)); 1.40 + 1.41 + if (machport_runloop_ref_.get() == NULL) { 1.42 + CHROMIUM_LOG(WARNING) << "CFMachPortCreateRunLoopSource failed"; 1.43 + *success = false; 1.44 + return; 1.45 + } 1.46 + 1.47 + CFRunLoopAddSource(CFRunLoopGetCurrent(), 1.48 + machport_runloop_ref_.get(), 1.49 + kCFRunLoopCommonModes); 1.50 + *success = true; 1.51 +} 1.52 + 1.53 +MachMessageSource::~MachMessageSource() { 1.54 + CFRunLoopRemoveSource(CFRunLoopGetCurrent(), 1.55 + machport_runloop_ref_.get(), 1.56 + kCFRunLoopCommonModes); 1.57 +} 1.58 + 1.59 +// static 1.60 +void MachMessageSource::OnReceiveMachMessage(CFMachPortRef port, void* msg, 1.61 + CFIndex size, void* closure) { 1.62 + MachPortListener *msg_listener = static_cast<MachPortListener*>(closure); 1.63 + size_t msg_size = (size < 0) ? 0 : static_cast<size_t>(size); 1.64 + DCHECK(msg && msg_size > 0); // this should never happen! 1.65 + 1.66 + if (msg_listener && msg && msg_size > 0) { 1.67 + msg_listener->OnMachMessageReceived(msg, msg_size); 1.68 + } 1.69 +}