diff options
author | jeremy@chromium.org <jeremy@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-11-24 21:37:13 +0000 |
---|---|---|
committer | jeremy@chromium.org <jeremy@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-11-24 21:37:13 +0000 |
commit | 37eedd8a6922e9810fd35df3a609126edf1c7a05 (patch) | |
tree | 26ee21747afcf74a4e03d9f2865769e7f4b7dff1 /chrome/common/mach_message_source_mac.h | |
parent | 22bb1a47a2fa1510c525c7dfe8c8d14058a337ba (diff) | |
download | chromium_src-37eedd8a6922e9810fd35df3a609126edf1c7a05.zip chromium_src-37eedd8a6922e9810fd35df3a609126edf1c7a05.tar.gz chromium_src-37eedd8a6922e9810fd35df3a609126edf1c7a05.tar.bz2 |
Add class to encapsulate a CF Mach Message Source and handle it's liftime.
This class handles adding a listener on Mach port to the current CFRunloop and
calling a delegate when a message is received on that port.
Review URL: http://codereview.chromium.org/11331
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@5936 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common/mach_message_source_mac.h')
-rw-r--r-- | chrome/common/mach_message_source_mac.h | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/chrome/common/mach_message_source_mac.h b/chrome/common/mach_message_source_mac.h new file mode 100644 index 0000000..f57539d --- /dev/null +++ b/chrome/common/mach_message_source_mac.h @@ -0,0 +1,59 @@ +// Copyright (c) 2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_COMMON_MACH_MESSAGE_SOURCE_MAC_H_ +#define CHROME_COMMON_MACH_MESSAGE_SOURCE_MAC_H_ + +#include <CoreServices/CoreServices.h> + +#include "base/scoped_cftyperef.h" + +// Handles registering and cleaning up after a CFRunloopSource for a Mach port. +// Messages received on the port are piped through to a delegate. +// +// Example: +// class MyListener : public MachMessageSource::MachPortListener { +// public: +// void OnMachMessageReceived(void* mach_msg, size_t size) { +// printf("received message on Mach port\n"); +// } +// }; +// +// mach_port_t a_port = ...; +// MyListener listener; +// bool success = false; +// MachMessageSource message_source(port, listener, &success); +// +// if (!success) { +// exit(1); // Couldn't register mach runloop source. +// } +// +// CFRunLoopRun(); // Process messages on runloop... +class MachMessageSource { + public: + // Classes that want to listen on a Mach port can implement + // OnMachMessageReceived, |mach_msg| is a pointer to the raw message data and + // |size| is the buffer size; + class MachPortListener { + public: + virtual void OnMachMessageReceived(void* mach_msg, size_t size) = 0; + }; + + // |listener| is a week reference passed to CF, it needs to remain in + // existence till this object is destroeyd. + MachMessageSource(mach_port_t port, + MachPortListener* listener, + bool* success); + ~MachMessageSource(); + + private: + // Called by CF when a new message arrives on the Mach port. + static void OnReceiveMachMessage(CFMachPortRef port, void* msg, CFIndex size, + void* closure); + + scoped_cftyperef<CFRunLoopSourceRef> machport_runloop_ref_; + DISALLOW_COPY_AND_ASSIGN(MachMessageSource); +}; + +#endif // CHROME_COMMON_MACH_MESSAGE_SOURCE_MAC_H_ |