Wed, 31 Dec 2014 13:27:57 +0100
Ignore runtime configuration files generated during quality assurance.
michael@0 | 1 | // Copyright (c) 2008 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #include "chrome/common/mach_message_source_mac.h" |
michael@0 | 6 | |
michael@0 | 7 | #include "base/logging.h" |
michael@0 | 8 | |
michael@0 | 9 | MachMessageSource::MachMessageSource(mach_port_t port, |
michael@0 | 10 | MachPortListener* msg_listener, |
michael@0 | 11 | bool* success) { |
michael@0 | 12 | DCHECK(msg_listener); |
michael@0 | 13 | DCHECK(success); |
michael@0 | 14 | DCHECK(port != MACH_PORT_NULL); |
michael@0 | 15 | |
michael@0 | 16 | CFMachPortContext port_context = {0}; |
michael@0 | 17 | port_context.info = msg_listener; |
michael@0 | 18 | |
michael@0 | 19 | scoped_cftyperef<CFMachPortRef> cf_mach_port_ref( |
michael@0 | 20 | CFMachPortCreateWithPort(kCFAllocatorDefault, |
michael@0 | 21 | port, |
michael@0 | 22 | MachMessageSource::OnReceiveMachMessage, |
michael@0 | 23 | &port_context, |
michael@0 | 24 | NULL)); |
michael@0 | 25 | |
michael@0 | 26 | if (cf_mach_port_ref.get() == NULL) { |
michael@0 | 27 | CHROMIUM_LOG(WARNING) << "CFMachPortCreate failed"; |
michael@0 | 28 | *success = false; |
michael@0 | 29 | return; |
michael@0 | 30 | } |
michael@0 | 31 | |
michael@0 | 32 | // Create a RL source. |
michael@0 | 33 | machport_runloop_ref_.reset( |
michael@0 | 34 | CFMachPortCreateRunLoopSource(kCFAllocatorDefault, |
michael@0 | 35 | cf_mach_port_ref.get(), |
michael@0 | 36 | 0)); |
michael@0 | 37 | |
michael@0 | 38 | if (machport_runloop_ref_.get() == NULL) { |
michael@0 | 39 | CHROMIUM_LOG(WARNING) << "CFMachPortCreateRunLoopSource failed"; |
michael@0 | 40 | *success = false; |
michael@0 | 41 | return; |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | CFRunLoopAddSource(CFRunLoopGetCurrent(), |
michael@0 | 45 | machport_runloop_ref_.get(), |
michael@0 | 46 | kCFRunLoopCommonModes); |
michael@0 | 47 | *success = true; |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | MachMessageSource::~MachMessageSource() { |
michael@0 | 51 | CFRunLoopRemoveSource(CFRunLoopGetCurrent(), |
michael@0 | 52 | machport_runloop_ref_.get(), |
michael@0 | 53 | kCFRunLoopCommonModes); |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | // static |
michael@0 | 57 | void MachMessageSource::OnReceiveMachMessage(CFMachPortRef port, void* msg, |
michael@0 | 58 | CFIndex size, void* closure) { |
michael@0 | 59 | MachPortListener *msg_listener = static_cast<MachPortListener*>(closure); |
michael@0 | 60 | size_t msg_size = (size < 0) ? 0 : static_cast<size_t>(size); |
michael@0 | 61 | DCHECK(msg && msg_size > 0); // this should never happen! |
michael@0 | 62 | |
michael@0 | 63 | if (msg_listener && msg && msg_size > 0) { |
michael@0 | 64 | msg_listener->OnMachMessageReceived(msg, msg_size); |
michael@0 | 65 | } |
michael@0 | 66 | } |