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