]> git.saurik.com Git - apple/securityd.git/blob - src/notifications.h
securityd-30544.tar.gz
[apple/securityd.git] / src / notifications.h
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 //
26 //
27 //
28 #ifndef _H_NOTIFICATIONS
29 #define _H_NOTIFICATIONS
30
31 #include <security_utilities/mach++.h>
32 #include <security_utilities/globalizer.h>
33 #include <securityd_client/ssclient.h>
34 #include <map>
35
36 using MachPlusPlus::Port;
37 using SecurityServer::NotificationDomain;
38 using SecurityServer::NotificationEvent;
39 using SecurityServer::NotificationMask;
40
41
42 //
43 // A registered receiver of notifications.
44 // This is an abstract class; you must subclass to define notifyMe().
45 //
46 // All Listeners in existence are collected in an internal map of ports to
47 // Listener*s, which makes them eligible to have events delivered to them via
48 // their notifyMe() method. There are (only) two viable lifetime management
49 // strategies for your Listener subclass:
50 // (1) Eternal: don't ever destroy your Listener. All is well. By convention,
51 // such Listeners use the null port.
52 // (2) Port-based: To get rid of your Listeners, call Listener::remove(port),
53 // which will delete(!) all Listeners constructed with that port.
54 // Except for the remove() functionality, Listener does not interpret the port.
55 //
56 // If you need another Listener lifetime management strategy, you will probably
57 // have to change things around here.
58 //
59 class Listener {
60 public:
61 Listener(Port port, NotificationDomain domain, NotificationMask events);
62 Listener(NotificationDomain domain, NotificationMask events);
63 virtual ~Listener();
64
65 // inject an event into the notification system
66 static void notify(NotificationDomain domain,
67 NotificationEvent event, const CssmData &data);
68 static bool remove(Port port);
69
70 // consume an event for this Listener
71 virtual void notifyMe(NotificationDomain domain,
72 NotificationEvent event, const CssmData &data) = 0;
73
74 const NotificationDomain domain;
75 const NotificationMask events;
76
77 bool wants(NotificationEvent event)
78 { return (1 << event) & events; }
79
80 protected:
81 Port mPort;
82
83 private:
84 void setup();
85
86 private:
87 typedef multimap<mach_port_t, Listener *> ListenerMap;
88 static ListenerMap listeners;
89 static Mutex setLock;
90 };
91
92
93 //
94 // A registered receiver of notifications.
95 // Each one is for a particular database (or all), set of events,
96 // and to a particular Mach port. A process may have any number
97 // of listeners, each independent; so that multiple notifications can
98 // be sent to the same process if it registers repeatedly.
99 //
100 class Process;
101
102 class ProcessListener : public Listener {
103 public:
104 ProcessListener(Process &proc, Port receiver, NotificationDomain domain,
105 NotificationMask evs = SecurityServer::kNotificationAllEvents);
106
107 Process &process;
108
109 void notifyMe(NotificationDomain domain,
110 NotificationEvent event, const CssmData &data);
111 };
112
113
114 #endif //_H_NOTIFICATIONS