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