Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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 | #ifndef CHROME_COMMON_MACH_MESSAGE_SOURCE_MAC_H_ |
michael@0 | 6 | #define CHROME_COMMON_MACH_MESSAGE_SOURCE_MAC_H_ |
michael@0 | 7 | |
michael@0 | 8 | #include <CoreServices/CoreServices.h> |
michael@0 | 9 | |
michael@0 | 10 | #include "base/scoped_cftyperef.h" |
michael@0 | 11 | |
michael@0 | 12 | // Handles registering and cleaning up after a CFRunloopSource for a Mach port. |
michael@0 | 13 | // Messages received on the port are piped through to a delegate. |
michael@0 | 14 | // |
michael@0 | 15 | // Example: |
michael@0 | 16 | // class MyListener : public MachMessageSource::MachPortListener { |
michael@0 | 17 | // public: |
michael@0 | 18 | // void OnMachMessageReceived(void* mach_msg, size_t size) { |
michael@0 | 19 | // printf("received message on Mach port\n"); |
michael@0 | 20 | // } |
michael@0 | 21 | // }; |
michael@0 | 22 | // |
michael@0 | 23 | // mach_port_t a_port = ...; |
michael@0 | 24 | // MyListener listener; |
michael@0 | 25 | // bool success = false; |
michael@0 | 26 | // MachMessageSource message_source(port, listener, &success); |
michael@0 | 27 | // |
michael@0 | 28 | // if (!success) { |
michael@0 | 29 | // exit(1); // Couldn't register mach runloop source. |
michael@0 | 30 | // } |
michael@0 | 31 | // |
michael@0 | 32 | // CFRunLoopRun(); // Process messages on runloop... |
michael@0 | 33 | class MachMessageSource { |
michael@0 | 34 | public: |
michael@0 | 35 | // Classes that want to listen on a Mach port can implement |
michael@0 | 36 | // OnMachMessageReceived, |mach_msg| is a pointer to the raw message data and |
michael@0 | 37 | // |size| is the buffer size; |
michael@0 | 38 | class MachPortListener { |
michael@0 | 39 | public: |
michael@0 | 40 | virtual void OnMachMessageReceived(void* mach_msg, size_t size) = 0; |
michael@0 | 41 | }; |
michael@0 | 42 | |
michael@0 | 43 | // |listener| is a week reference passed to CF, it needs to remain in |
michael@0 | 44 | // existence till this object is destroeyd. |
michael@0 | 45 | MachMessageSource(mach_port_t port, |
michael@0 | 46 | MachPortListener* listener, |
michael@0 | 47 | bool* success); |
michael@0 | 48 | ~MachMessageSource(); |
michael@0 | 49 | |
michael@0 | 50 | private: |
michael@0 | 51 | // Called by CF when a new message arrives on the Mach port. |
michael@0 | 52 | static void OnReceiveMachMessage(CFMachPortRef port, void* msg, CFIndex size, |
michael@0 | 53 | void* closure); |
michael@0 | 54 | |
michael@0 | 55 | scoped_cftyperef<CFRunLoopSourceRef> machport_runloop_ref_; |
michael@0 | 56 | DISALLOW_COPY_AND_ASSIGN(MachMessageSource); |
michael@0 | 57 | }; |
michael@0 | 58 | |
michael@0 | 59 | #endif // CHROME_COMMON_MACH_MESSAGE_SOURCE_MAC_H_ |