]> git.saurik.com Git - apple/cf.git/blob - CFPlugIn_PlugIn.c
CF-476.15.tar.gz
[apple/cf.git] / CFPlugIn_PlugIn.c
1 /*
2 * Copyright (c) 2008 Apple 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 /* CFPlugIn_PlugIn.c
24 Copyright (c) 1999-2007 Apple Inc. All rights reserved.
25 Responsibility: Doug Davidson
26 */
27
28 #include "CFBundle_Internal.h"
29 #include "CFInternal.h"
30
31
32 static void _registerFactory(const void *key, const void *val, void *context) {
33 CFStringRef factoryIDStr = (CFStringRef)key;
34 CFStringRef factoryFuncStr = (CFStringRef)val;
35 CFBundleRef bundle = (CFBundleRef)context;
36 CFUUIDRef factoryID = (CFGetTypeID(factoryIDStr) == CFStringGetTypeID()) ? CFUUIDCreateFromString(kCFAllocatorSystemDefault, factoryIDStr) : NULL;
37 if (NULL == factoryID) factoryID = (CFUUIDRef)CFRetain(factoryIDStr);
38 if (CFGetTypeID(factoryFuncStr) != CFStringGetTypeID() || CFStringGetLength(factoryFuncStr) <= 0) factoryFuncStr = NULL;
39 CFPlugInRegisterFactoryFunctionByName(factoryID, bundle, factoryFuncStr);
40 if (NULL != factoryID) CFRelease(factoryID);
41 }
42
43 static void _registerType(const void *key, const void *val, void *context) {
44 CFStringRef typeIDStr = (CFStringRef)key;
45 CFArrayRef factoryIDStrArray = (CFArrayRef)val;
46 CFBundleRef bundle = (CFBundleRef)context;
47 SInt32 i, c = (CFGetTypeID(factoryIDStrArray) == CFArrayGetTypeID()) ? CFArrayGetCount(factoryIDStrArray) : 0;
48 CFStringRef curFactoryIDStr;
49 CFUUIDRef typeID = (CFGetTypeID(typeIDStr) == CFStringGetTypeID()) ? CFUUIDCreateFromString(kCFAllocatorSystemDefault, typeIDStr) : NULL;
50 CFUUIDRef curFactoryID;
51 if (NULL == typeID) typeID = (CFUUIDRef)CFRetain(typeIDStr);
52 if (0 == c && (CFGetTypeID(factoryIDStrArray) != CFArrayGetTypeID())) {
53 curFactoryIDStr = (CFStringRef)val;
54 curFactoryID = (CFGetTypeID(curFactoryIDStr) == CFStringGetTypeID()) ? CFUUIDCreateFromString(CFGetAllocator(bundle), curFactoryIDStr) : NULL;
55 if (NULL == curFactoryID) curFactoryID = (CFUUIDRef)CFRetain(curFactoryIDStr);
56 CFPlugInRegisterPlugInType(curFactoryID, typeID);
57 if (NULL != curFactoryID) CFRelease(curFactoryID);
58 } else for (i=0; i<c; i++) {
59 curFactoryIDStr = (CFStringRef)CFArrayGetValueAtIndex(factoryIDStrArray, i);
60 curFactoryID = (CFGetTypeID(curFactoryIDStr) == CFStringGetTypeID()) ? CFUUIDCreateFromString(CFGetAllocator(bundle), curFactoryIDStr) : NULL;
61 if (NULL == curFactoryID) curFactoryID = (CFUUIDRef)CFRetain(curFactoryIDStr);
62 CFPlugInRegisterPlugInType(curFactoryID, typeID);
63 if (NULL != curFactoryID) CFRelease(curFactoryID);
64 }
65 if (NULL != typeID) CFRelease(typeID);
66 }
67
68 __private_extern__ Boolean _CFBundleNeedsInitPlugIn(CFBundleRef bundle) {
69 Boolean result = false;
70 CFDictionaryRef infoDict = CFBundleGetInfoDictionary(bundle), factoryDict;
71 CFStringRef tempStr;
72 if (infoDict) {
73 factoryDict = (CFDictionaryRef)CFDictionaryGetValue(infoDict, kCFPlugInFactoriesKey);
74 if (factoryDict != NULL && CFGetTypeID(factoryDict) == CFDictionaryGetTypeID()) result = true;
75 tempStr = (CFStringRef)CFDictionaryGetValue(infoDict, kCFPlugInDynamicRegistrationKey);
76 if (tempStr != NULL && CFGetTypeID(tempStr) == CFStringGetTypeID() && CFStringCompare(tempStr, CFSTR("YES"), kCFCompareCaseInsensitive) == kCFCompareEqualTo) result = true;
77 }
78 return result;
79 }
80
81 __private_extern__ void _CFBundleInitPlugIn(CFBundleRef bundle) {
82 CFArrayCallBacks _pluginFactoryArrayCallbacks = {0, NULL, NULL, NULL, NULL};
83 Boolean doDynamicReg = false;
84 CFDictionaryRef infoDict;
85 CFDictionaryRef factoryDict;
86 CFDictionaryRef typeDict;
87 CFStringRef tempStr;
88
89 infoDict = CFBundleGetInfoDictionary(bundle);
90 if (infoDict == NULL) {
91 return;
92 }
93 factoryDict = (CFDictionaryRef)CFDictionaryGetValue(infoDict, kCFPlugInFactoriesKey);
94 if (factoryDict != NULL && CFGetTypeID(factoryDict) != CFDictionaryGetTypeID()) factoryDict = NULL;
95 tempStr = (CFStringRef)CFDictionaryGetValue(infoDict, kCFPlugInDynamicRegistrationKey);
96 if (tempStr != NULL && CFGetTypeID(tempStr) == CFStringGetTypeID() && CFStringCompare(tempStr, CFSTR("YES"), kCFCompareCaseInsensitive) == kCFCompareEqualTo) {
97 doDynamicReg = true;
98 }
99 if (!factoryDict && !doDynamicReg) {
100 // This is not a plugIn.
101 return;
102 }
103
104 /* loadOnDemand is true by default if the plugIn does not do dynamic registration. It is false, by default if it does do dynamic registration. The dynamic register function can set this. */
105 __CFBundleGetPlugInData(bundle)->_isPlugIn = true;
106 __CFBundleGetPlugInData(bundle)->_loadOnDemand = true;
107 __CFBundleGetPlugInData(bundle)->_isDoingDynamicRegistration = false;
108 __CFBundleGetPlugInData(bundle)->_instanceCount = 0;
109
110 __CFBundleGetPlugInData(bundle)->_factories = CFArrayCreateMutable(CFGetAllocator(bundle), 0, &_pluginFactoryArrayCallbacks);
111
112 /* Now do the registration */
113
114 /* First do static registrations, if any. */
115 if (factoryDict != NULL) {
116 CFDictionaryApplyFunction(factoryDict, _registerFactory, bundle);
117 }
118 typeDict = (CFDictionaryRef)CFDictionaryGetValue(infoDict, kCFPlugInTypesKey);
119 if (typeDict != NULL && CFGetTypeID(typeDict) != CFDictionaryGetTypeID()) typeDict = NULL;
120 if (typeDict != NULL) {
121 CFDictionaryApplyFunction(typeDict, _registerType, bundle);
122 }
123
124 /* Now set key for dynamic registration if necessary */
125 if (doDynamicReg) {
126 CFDictionarySetValue((CFMutableDictionaryRef)infoDict, CFSTR("CFPlugInNeedsDynamicRegistration"), CFSTR("YES"));
127 if (CFBundleIsExecutableLoaded(bundle)) _CFBundlePlugInLoaded(bundle);
128 }
129 }
130
131 __private_extern__ void _CFBundlePlugInLoaded(CFBundleRef bundle) {
132 CFDictionaryRef infoDict = CFBundleGetInfoDictionary(bundle);
133 CFStringRef tempStr;
134 CFPlugInDynamicRegisterFunction func = NULL;
135
136 if (!__CFBundleGetPlugInData(bundle)->_isPlugIn || __CFBundleGetPlugInData(bundle)->_isDoingDynamicRegistration || !infoDict || !CFBundleIsExecutableLoaded(bundle)) return;
137
138 tempStr = (CFStringRef)CFDictionaryGetValue(infoDict, CFSTR("CFPlugInNeedsDynamicRegistration"));
139 if (tempStr != NULL && CFGetTypeID(tempStr) == CFStringGetTypeID() && CFStringCompare(tempStr, CFSTR("YES"), kCFCompareCaseInsensitive) == kCFCompareEqualTo) {
140 CFDictionaryRemoveValue((CFMutableDictionaryRef)infoDict, CFSTR("CFPlugInNeedsDynamicRegistration"));
141 tempStr = (CFStringRef)CFDictionaryGetValue(infoDict, kCFPlugInDynamicRegisterFunctionKey);
142 if (tempStr == NULL || CFGetTypeID(tempStr) != CFStringGetTypeID() || CFStringGetLength(tempStr) <= 0) {
143 tempStr = CFSTR("CFPlugInDynamicRegister");
144 }
145 __CFBundleGetPlugInData(bundle)->_loadOnDemand = false;
146 __CFBundleGetPlugInData(bundle)->_isDoingDynamicRegistration = true;
147
148 /* Find the symbol and call it. */
149 func = (CFPlugInDynamicRegisterFunction)CFBundleGetFunctionPointerForName(bundle, tempStr);
150 if (func) {
151 func(bundle);
152 // MF:!!! Unload function is never called. Need to deal with this!
153 }
154
155 __CFBundleGetPlugInData(bundle)->_isDoingDynamicRegistration = false;
156 if (__CFBundleGetPlugInData(bundle)->_loadOnDemand && (__CFBundleGetPlugInData(bundle)->_instanceCount == 0)) {
157 /* Unload now if we can/should. */
158 CFBundleUnloadExecutable(bundle);
159 }
160 } else {
161 CFDictionaryRemoveValue((CFMutableDictionaryRef)infoDict, CFSTR("CFPlugInNeedsDynamicRegistration"));
162 }
163 }
164
165 __private_extern__ void _CFBundleDeallocatePlugIn(CFBundleRef bundle) {
166 if (__CFBundleGetPlugInData(bundle)->_isPlugIn) {
167 SInt32 c;
168
169 /* Go through factories disabling them. Disabling these factories should cause them to dealloc since we wouldn't be deallocating if any of the factories had outstanding instances. So go backwards. */
170 c = CFArrayGetCount(__CFBundleGetPlugInData(bundle)->_factories);
171 while (c--) {
172 _CFPFactoryDisable((_CFPFactory *)CFArrayGetValueAtIndex(__CFBundleGetPlugInData(bundle)->_factories, c));
173 }
174 CFRelease(__CFBundleGetPlugInData(bundle)->_factories);
175
176 __CFBundleGetPlugInData(bundle)->_isPlugIn = false;
177 }
178 }
179
180 CFTypeID CFPlugInGetTypeID(void) {
181 return CFBundleGetTypeID();
182 }
183
184 CFPlugInRef CFPlugInCreate(CFAllocatorRef allocator, CFURLRef plugInURL) {
185 CFBundleRef bundle = CFBundleCreate(allocator, plugInURL);
186 return (CFPlugInRef)bundle;
187 }
188
189 CFBundleRef CFPlugInGetBundle(CFPlugInRef plugIn) {
190 return (CFBundleRef)plugIn;
191 }
192
193 void CFPlugInSetLoadOnDemand(CFPlugInRef plugIn, Boolean flag) {
194 if (__CFBundleGetPlugInData(plugIn)->_isPlugIn) {
195 __CFBundleGetPlugInData(plugIn)->_loadOnDemand = flag;
196 if (__CFBundleGetPlugInData(plugIn)->_loadOnDemand && !__CFBundleGetPlugInData(plugIn)->_isDoingDynamicRegistration && (__CFBundleGetPlugInData(plugIn)->_instanceCount == 0)) {
197 /* Unload now if we can/should. */
198 /* If we are doing dynamic registration currently, do not unload. The unloading will happen when dynamic registration is done, if necessary. */
199 CFBundleUnloadExecutable(plugIn);
200 } else if (!__CFBundleGetPlugInData(plugIn)->_loadOnDemand) {
201 /* Make sure we're loaded now. */
202 CFBundleLoadExecutable(plugIn);
203 }
204 }
205 }
206
207 Boolean CFPlugInIsLoadOnDemand(CFPlugInRef plugIn) {
208 if (__CFBundleGetPlugInData(plugIn)->_isPlugIn) {
209 return __CFBundleGetPlugInData(plugIn)->_loadOnDemand;
210 } else {
211 return false;
212 }
213 }
214
215 __private_extern__ void _CFPlugInWillUnload(CFPlugInRef plugIn) {
216 if (__CFBundleGetPlugInData(plugIn)->_isPlugIn) {
217 SInt32 c = CFArrayGetCount(__CFBundleGetPlugInData(plugIn)->_factories);
218 /* First, flush all the function pointers that may be cached by our factories. */
219 while (c--) {
220 _CFPFactoryFlushFunctionCache((_CFPFactory *)CFArrayGetValueAtIndex(__CFBundleGetPlugInData(plugIn)->_factories, c));
221 }
222 }
223 }
224
225 __private_extern__ void _CFPlugInAddPlugInInstance(CFPlugInRef plugIn) {
226 if (__CFBundleGetPlugInData(plugIn)->_isPlugIn) {
227 if ((__CFBundleGetPlugInData(plugIn)->_instanceCount == 0) && (__CFBundleGetPlugInData(plugIn)->_loadOnDemand)) {
228 /* Make sure we are not scheduled for unloading */
229 _CFBundleUnscheduleForUnloading(CFPlugInGetBundle(plugIn));
230 }
231 __CFBundleGetPlugInData(plugIn)->_instanceCount++;
232 /* Instances also retain the CFBundle */
233 CFRetain(plugIn);
234 }
235 }
236
237 __private_extern__ void _CFPlugInRemovePlugInInstance(CFPlugInRef plugIn) {
238 if (__CFBundleGetPlugInData(plugIn)->_isPlugIn) {
239 /* MF:!!! Assert that instanceCount > 0. */
240 __CFBundleGetPlugInData(plugIn)->_instanceCount--;
241 if ((__CFBundleGetPlugInData(plugIn)->_instanceCount == 0) && (__CFBundleGetPlugInData(plugIn)->_loadOnDemand)) {
242 // We unload the code lazily because the code that caused this function to be called is probably code from the plugin itself. If we unload now, we will hose things.
243 //CFBundleUnloadExecutable(plugIn);
244 _CFBundleScheduleForUnloading(CFPlugInGetBundle(plugIn));
245 }
246 /* Instances also retain the CFPlugIn */
247 /* MF:!!! This will cause immediate unloading if it was the last ref on the plugin. */
248 CFRelease(plugIn);
249 }
250 }
251
252 __private_extern__ void _CFPlugInAddFactory(CFPlugInRef plugIn, _CFPFactory *factory) {
253 if (__CFBundleGetPlugInData(plugIn)->_isPlugIn) {
254 CFArrayAppendValue(__CFBundleGetPlugInData(plugIn)->_factories, factory);
255 }
256 }
257
258 __private_extern__ void _CFPlugInRemoveFactory(CFPlugInRef plugIn, _CFPFactory *factory) {
259 if (__CFBundleGetPlugInData(plugIn)->_isPlugIn) {
260 SInt32 idx = CFArrayGetFirstIndexOfValue(__CFBundleGetPlugInData(plugIn)->_factories, CFRangeMake(0, CFArrayGetCount(__CFBundleGetPlugInData(plugIn)->_factories)), factory);
261 if (idx >= 0) {
262 CFArrayRemoveValueAtIndex(__CFBundleGetPlugInData(plugIn)->_factories, idx);
263 }
264 }
265 }