mach_message_source_mac.h (2072B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 // Copyright (c) 2008 The Chromium Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style license that can be 5 // found in the LICENSE file. 6 7 #ifndef CHROME_COMMON_MACH_MESSAGE_SOURCE_MAC_H_ 8 #define CHROME_COMMON_MACH_MESSAGE_SOURCE_MAC_H_ 9 10 #include <CoreServices/CoreServices.h> 11 12 #include "base/scoped_cftyperef.h" 13 14 // Handles registering and cleaning up after a CFRunloopSource for a Mach port. 15 // Messages received on the port are piped through to a delegate. 16 // 17 // Example: 18 // class MyListener : public MachMessageSource::MachPortListener { 19 // public: 20 // void OnMachMessageReceived(void* mach_msg, size_t size) { 21 // printf("received message on Mach port\n"); 22 // } 23 // }; 24 // 25 // mach_port_t a_port = ...; 26 // MyListener listener; 27 // bool success = false; 28 // MachMessageSource message_source(port, listener, &success); 29 // 30 // if (!success) { 31 // exit(1); // Couldn't register mach runloop source. 32 // } 33 // 34 // CFRunLoopRun(); // Process messages on runloop... 35 class MachMessageSource { 36 public: 37 // Classes that want to listen on a Mach port can implement 38 // OnMachMessageReceived, |mach_msg| is a pointer to the raw message data and 39 // |size| is the buffer size; 40 class MachPortListener { 41 public: 42 virtual void OnMachMessageReceived(void* mach_msg, size_t size) = 0; 43 }; 44 45 // |listener| is a week reference passed to CF, it needs to remain in 46 // existence till this object is destroeyd. 47 MachMessageSource(mach_port_t port, MachPortListener* listener, 48 bool* success); 49 ~MachMessageSource(); 50 51 private: 52 // Called by CF when a new message arrives on the Mach port. 53 static void OnReceiveMachMessage(CFMachPortRef port, void* msg, CFIndex size, 54 void* closure); 55 56 scoped_cftyperef<CFRunLoopSourceRef> machport_runloop_ref_; 57 DISALLOW_COPY_AND_ASSIGN(MachMessageSource); 58 }; 59 60 #endif // CHROME_COMMON_MACH_MESSAGE_SOURCE_MAC_H_