ipc/chromium/src/chrome/common/mach_message_source_mac.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 // Copyright (c) 2008 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 "chrome/common/mach_message_source_mac.h"
     7 #include "base/logging.h"
     9 MachMessageSource::MachMessageSource(mach_port_t port,
    10                                      MachPortListener* msg_listener,
    11                                      bool* success) {
    12   DCHECK(msg_listener);
    13   DCHECK(success);
    14   DCHECK(port != MACH_PORT_NULL);
    16   CFMachPortContext port_context = {0};
    17   port_context.info = msg_listener;
    19   scoped_cftyperef<CFMachPortRef> cf_mach_port_ref(
    20      CFMachPortCreateWithPort(kCFAllocatorDefault,
    21                               port,
    22                               MachMessageSource::OnReceiveMachMessage,
    23                               &port_context,
    24                               NULL));
    26   if (cf_mach_port_ref.get() == NULL) {
    27    CHROMIUM_LOG(WARNING) << "CFMachPortCreate failed";
    28    *success = false;
    29    return;
    30   }
    32   // Create a RL source.
    33   machport_runloop_ref_.reset(
    34      CFMachPortCreateRunLoopSource(kCFAllocatorDefault,
    35                                    cf_mach_port_ref.get(),
    36                                    0));
    38   if (machport_runloop_ref_.get() == NULL) {
    39    CHROMIUM_LOG(WARNING) << "CFMachPortCreateRunLoopSource failed";
    40    *success = false;
    41    return;
    42   }
    44   CFRunLoopAddSource(CFRunLoopGetCurrent(),
    45                     machport_runloop_ref_.get(),
    46                     kCFRunLoopCommonModes);
    47   *success = true;
    48 }
    50 MachMessageSource::~MachMessageSource() {
    51   CFRunLoopRemoveSource(CFRunLoopGetCurrent(),
    52                         machport_runloop_ref_.get(),
    53                         kCFRunLoopCommonModes);
    54 }
    56 // static
    57 void MachMessageSource::OnReceiveMachMessage(CFMachPortRef port, void* msg,
    58                                              CFIndex size, void* closure) {
    59   MachPortListener *msg_listener = static_cast<MachPortListener*>(closure);
    60   size_t msg_size = (size < 0) ? 0 : static_cast<size_t>(size);
    61   DCHECK(msg && msg_size > 0);  // this should never happen!
    63   if (msg_listener && msg && msg_size > 0) {
    64     msg_listener->OnMachMessageReceived(msg, msg_size);
    65   }
    66 }

mercurial