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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/chrome/common/mach_message_source_mac.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,59 @@
     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 +#ifndef CHROME_COMMON_MACH_MESSAGE_SOURCE_MAC_H_
     1.9 +#define CHROME_COMMON_MACH_MESSAGE_SOURCE_MAC_H_
    1.10 +
    1.11 +#include <CoreServices/CoreServices.h>
    1.12 +
    1.13 +#include "base/scoped_cftyperef.h"
    1.14 +
    1.15 +// Handles registering and cleaning up after a CFRunloopSource for a Mach port.
    1.16 +// Messages received on the port are piped through to a delegate.
    1.17 +//
    1.18 +// Example:
    1.19 +//  class MyListener : public MachMessageSource::MachPortListener {
    1.20 +//   public:
    1.21 +//    void OnMachMessageReceived(void* mach_msg, size_t size) {
    1.22 +//      printf("received message on Mach port\n");
    1.23 +//    }
    1.24 +//  };
    1.25 +//
    1.26 +//  mach_port_t a_port = ...;
    1.27 +//  MyListener listener;
    1.28 +//  bool success = false;
    1.29 +//  MachMessageSource message_source(port, listener, &success);
    1.30 +//
    1.31 +//  if (!success) {
    1.32 +//    exit(1);  // Couldn't register mach runloop source.
    1.33 +//  }
    1.34 +//
    1.35 +//  CFRunLoopRun();  // Process messages on runloop...
    1.36 +class MachMessageSource {
    1.37 + public:
    1.38 +  // Classes that want to listen on a Mach port can implement
    1.39 +  // OnMachMessageReceived, |mach_msg| is a pointer to the raw message data and
    1.40 +  // |size| is the buffer size;
    1.41 +  class MachPortListener {
    1.42 +   public:
    1.43 +    virtual void OnMachMessageReceived(void* mach_msg, size_t size) = 0;
    1.44 +  };
    1.45 +
    1.46 +  // |listener| is a week reference passed to CF, it needs to remain in
    1.47 +  // existence till this object is destroeyd.
    1.48 +  MachMessageSource(mach_port_t port,
    1.49 +                    MachPortListener* listener,
    1.50 +                    bool* success);
    1.51 +  ~MachMessageSource();
    1.52 +
    1.53 + private:
    1.54 +  // Called by CF when a new message arrives on the Mach port.
    1.55 +  static void OnReceiveMachMessage(CFMachPortRef port, void* msg, CFIndex size,
    1.56 +                                   void* closure);
    1.57 +
    1.58 +  scoped_cftyperef<CFRunLoopSourceRef> machport_runloop_ref_;
    1.59 +  DISALLOW_COPY_AND_ASSIGN(MachMessageSource);
    1.60 +};
    1.61 +
    1.62 +#endif // CHROME_COMMON_MACH_MESSAGE_SOURCE_MAC_H_

mercurial