2 * Copyright (c) 2008 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 Copyright (c) 1999-2007 Apple Inc. All rights reserved.
25 Responsibility: Doug Davidson
28 #include "CFBundle_Internal.h"
29 #include "CFInternal.h"
31 CONST_STRING_DECL(kCFPlugInDynamicRegistrationKey
, "CFPlugInDynamicRegistration")
32 CONST_STRING_DECL(kCFPlugInDynamicRegisterFunctionKey
, "CFPlugInDynamicRegisterFunction")
33 CONST_STRING_DECL(kCFPlugInUnloadFunctionKey
, "CFPlugInUnloadFunction")
34 CONST_STRING_DECL(kCFPlugInFactoriesKey
, "CFPlugInFactories")
35 CONST_STRING_DECL(kCFPlugInTypesKey
, "CFPlugInTypes")
37 __private_extern__
void __CFPlugInInitialize(void) {
40 /* ===================== Finding factories and creating instances ===================== */
41 /* For plugIn hosts. */
42 /* Functions for finding factories to create specific types and actually creating instances of a type. */
44 CF_EXPORT CFArrayRef
CFPlugInFindFactoriesForPlugInType(CFUUIDRef typeID
) {
45 CFArrayRef array
= _CFPFactoryFindForType(typeID
);
46 CFMutableArrayRef result
= NULL
;
49 SInt32 i
, c
= CFArrayGetCount(array
);
51 // Use default allocator
52 result
= CFArrayCreateMutable(kCFAllocatorSystemDefault
, 0, &kCFTypeArrayCallBacks
);
55 CFArrayAppendValue(result
, _CFPFactoryGetFactoryID((_CFPFactory
*)CFArrayGetValueAtIndex(array
, i
)));
61 CF_EXPORT CFArrayRef
CFPlugInFindFactoriesForPlugInTypeInPlugIn(CFUUIDRef typeID
, CFPlugInRef plugIn
) {
62 CFArrayRef array
= _CFPFactoryFindForType(typeID
);
63 CFMutableArrayRef result
= NULL
;
66 SInt32 i
, c
= CFArrayGetCount(array
);
69 // Use default allocator
70 result
= CFArrayCreateMutable(kCFAllocatorSystemDefault
, 0, &kCFTypeArrayCallBacks
);
73 factory
= (_CFPFactory
*)CFArrayGetValueAtIndex(array
, i
);
74 if (_CFPFactoryGetPlugIn(factory
) == plugIn
) {
75 CFArrayAppendValue(result
, _CFPFactoryGetFactoryID(factory
));
82 CF_EXPORT
void *CFPlugInInstanceCreate(CFAllocatorRef allocator
, CFUUIDRef factoryID
, CFUUIDRef typeID
) {
83 _CFPFactory
*factory
= _CFPFactoryFind(factoryID
, true);
86 /* MF:!!! No such factory. */
87 CFLog(__kCFLogPlugIn
, CFSTR("Cannot find factory %@"), factoryID
);
89 if (!_CFPFactorySupportsType(factory
, typeID
)) {
90 /* MF:!!! Factory does not support type. */
91 CFLog(__kCFLogPlugIn
, CFSTR("Factory %@ does not support type %@"), factoryID
, typeID
);
93 result
= _CFPFactoryCreateInstance(allocator
, factory
, typeID
);
99 /* ===================== Registering factories and types ===================== */
100 /* For plugIn writers who must dynamically register things. */
101 /* Functions to register factory functions and to associate factories with types. */
103 CF_EXPORT Boolean
CFPlugInRegisterFactoryFunction(CFUUIDRef factoryID
, CFPlugInFactoryFunction func
) {
104 // Create factories without plugIns from default allocator
105 // MF:!!! Should probably check that this worked, and maybe do some pre-checking to see if it already exists
106 // _CFPFactory *factory =
107 (void)_CFPFactoryCreate(kCFAllocatorSystemDefault
, factoryID
, func
);
111 CF_EXPORT Boolean
CFPlugInRegisterFactoryFunctionByName(CFUUIDRef factoryID
, CFPlugInRef plugIn
, CFStringRef functionName
) {
112 // Create factories with plugIns from plugIn's allocator
113 // MF:!!! Should probably check that this worked, and maybe do some pre-checking to see if it already exists
114 // _CFPFactory *factory =
115 (void)_CFPFactoryCreateByName(CFGetAllocator(plugIn
), factoryID
, plugIn
, functionName
);
119 CF_EXPORT Boolean
CFPlugInUnregisterFactory(CFUUIDRef factoryID
) {
120 _CFPFactory
*factory
= _CFPFactoryFind(factoryID
, true);
123 /* MF:!!! Error. No factory registered for this ID. */
125 _CFPFactoryDisable(factory
);
130 CF_EXPORT Boolean
CFPlugInRegisterPlugInType(CFUUIDRef factoryID
, CFUUIDRef typeID
) {
131 _CFPFactory
*factory
= _CFPFactoryFind(factoryID
, true);
134 /* MF:!!! Error. Factory must be registered (and not disabled) before types can be associated with it. */
136 _CFPFactoryAddType(factory
, typeID
);
141 CF_EXPORT Boolean
CFPlugInUnregisterPlugInType(CFUUIDRef factoryID
, CFUUIDRef typeID
) {
142 _CFPFactory
*factory
= _CFPFactoryFind(factoryID
, true);
145 /* MF:!!! Error. Could not find factory. */
147 _CFPFactoryRemoveType(factory
, typeID
);
153 /* ================= Registering instances ================= */
154 /* When a new instance of a type is created, the instance is responsible for registering itself with the factory that created it and unregistering when it deallocates. */
155 /* This means that an instance must keep track of the CFUUIDRef of the factory that created it so it can unregister when it goes away. */
157 CF_EXPORT
void CFPlugInAddInstanceForFactory(CFUUIDRef factoryID
) {
158 _CFPFactory
*factory
= _CFPFactoryFind(factoryID
, true);
161 /* MF:!!! Error. Could not find factory. */
163 _CFPFactoryAddInstance(factory
);
167 CF_EXPORT
void CFPlugInRemoveInstanceForFactory(CFUUIDRef factoryID
) {
168 _CFPFactory
*factory
= _CFPFactoryFind(factoryID
, true);
171 /* MF:!!! Error. Could not find factory. */
173 _CFPFactoryRemoveInstance(factory
);