2 * Copyright (c) 2013 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@
25 Copyright (c) 1999-2013, Apple Inc. All rights reserved.
26 Responsibility: Tony Parker
29 #include "CFBundle_Internal.h"
30 #include "CFInternal.h"
32 CONST_STRING_DECL(kCFPlugInDynamicRegistrationKey
, "CFPlugInDynamicRegistration")
33 CONST_STRING_DECL(kCFPlugInDynamicRegisterFunctionKey
, "CFPlugInDynamicRegisterFunction")
34 CONST_STRING_DECL(kCFPlugInUnloadFunctionKey
, "CFPlugInUnloadFunction")
35 CONST_STRING_DECL(kCFPlugInFactoriesKey
, "CFPlugInFactories")
36 CONST_STRING_DECL(kCFPlugInTypesKey
, "CFPlugInTypes")
38 CF_PRIVATE
void __CFPlugInInitialize(void) {
41 /* ===================== Finding factories and creating instances ===================== */
42 /* For plugIn hosts. */
43 /* Functions for finding factories to create specific types and actually creating instances of a type. */
45 CF_EXPORT CFArrayRef
CFPlugInFindFactoriesForPlugInType(CFUUIDRef typeID
) {
46 CFArrayRef array
= _CFPFactoryFindCopyForType(typeID
);
47 CFMutableArrayRef result
= NULL
;
50 SInt32 i
, c
= CFArrayGetCount(array
);
51 result
= CFArrayCreateMutable(kCFAllocatorSystemDefault
, 0, &kCFTypeArrayCallBacks
);
52 for (i
= 0; i
< c
; i
++) {
53 CFUUIDRef factoryId
= _CFPFactoryCopyFactoryID((_CFPFactoryRef
)CFArrayGetValueAtIndex(array
, i
));
55 CFArrayAppendValue(result
, factoryId
);
64 CF_EXPORT CFArrayRef
CFPlugInFindFactoriesForPlugInTypeInPlugIn(CFUUIDRef typeID
, CFPlugInRef plugIn
) {
65 CFArrayRef array
= _CFPFactoryFindCopyForType(typeID
);
66 CFMutableArrayRef result
= NULL
;
69 SInt32 i
, c
= CFArrayGetCount(array
);
70 _CFPFactoryRef factory
;
71 result
= CFArrayCreateMutable(kCFAllocatorSystemDefault
, 0, &kCFTypeArrayCallBacks
);
72 for (i
= 0; i
< c
; i
++) {
73 factory
= (_CFPFactoryRef
)CFArrayGetValueAtIndex(array
, i
);
74 CFPlugInRef factoryPlugIn
= _CFPFactoryCopyPlugIn(factory
);
75 if (factoryPlugIn
== plugIn
) {
76 CFUUIDRef factoryId
= _CFPFactoryCopyFactoryID(factory
);
77 CFArrayAppendValue(result
, factoryId
);
80 if (factoryPlugIn
) CFRelease(factoryPlugIn
);
87 CF_EXPORT
void *CFPlugInInstanceCreate(CFAllocatorRef allocator
, CFUUIDRef factoryID
, CFUUIDRef typeID
) {
88 _CFPFactoryRef factory
= _CFPFactoryFind(factoryID
, true);
91 /* MF:!!! No such factory. */
92 CFLog(__kCFLogPlugIn
, CFSTR("Cannot find factory %@"), factoryID
);
94 if (!_CFPFactorySupportsType(factory
, typeID
)) {
95 /* MF:!!! Factory does not support type. */
96 CFLog(__kCFLogPlugIn
, CFSTR("Factory %@ does not support type %@"), factoryID
, typeID
);
98 result
= _CFPFactoryCreateInstance(allocator
, factory
, typeID
);
104 /* ===================== Registering factories and types ===================== */
105 /* For plugIn writers who must dynamically register things. */
106 /* Functions to register factory functions and to associate factories with types. */
108 CF_EXPORT Boolean
CFPlugInRegisterFactoryFunction(CFUUIDRef factoryID
, CFPlugInFactoryFunction func
) {
109 // Create factories without plugIns from default allocator
110 // MF:!!! Should probably check that this worked, and maybe do some pre-checking to see if it already exists
111 // _CFPFactoryRef factory =
112 (void)_CFPFactoryCreate(kCFAllocatorSystemDefault
, factoryID
, func
);
116 CF_EXPORT Boolean
CFPlugInRegisterFactoryFunctionByName(CFUUIDRef factoryID
, CFPlugInRef plugIn
, CFStringRef functionName
) {
117 // Create factories with plugIns from plugIn's allocator
118 // MF:!!! Should probably check that this worked, and maybe do some pre-checking to see if it already exists
119 // _CFPFactoryRef factory =
120 (void)_CFPFactoryCreateByName(CFGetAllocator(plugIn
), factoryID
, plugIn
, functionName
);
124 CF_EXPORT Boolean
CFPlugInUnregisterFactory(CFUUIDRef factoryID
) {
125 _CFPFactoryRef factory
= _CFPFactoryFind(factoryID
, true);
128 /* MF:!!! Error. No factory registered for this ID. */
130 _CFPFactoryDisable(factory
);
135 CF_EXPORT Boolean
CFPlugInRegisterPlugInType(CFUUIDRef factoryID
, CFUUIDRef typeID
) {
136 _CFPFactoryRef factory
= _CFPFactoryFind(factoryID
, true);
139 /* MF:!!! Error. Factory must be registered (and not disabled) before types can be associated with it. */
141 _CFPFactoryAddType(factory
, typeID
);
146 CF_EXPORT Boolean
CFPlugInUnregisterPlugInType(CFUUIDRef factoryID
, CFUUIDRef typeID
) {
147 _CFPFactoryRef factory
= _CFPFactoryFind(factoryID
, true);
150 /* MF:!!! Error. Could not find factory. */
152 _CFPFactoryRemoveType(factory
, typeID
);
158 /* ================= Registering instances ================= */
159 /* 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. */
160 /* 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. */
162 CF_EXPORT
void CFPlugInAddInstanceForFactory(CFUUIDRef factoryID
) {
163 _CFPFactoryRef factory
= _CFPFactoryFind(factoryID
, true);
166 /* MF:!!! Error. Could not find factory. */
168 _CFPFactoryAddInstance(factory
);
172 CF_EXPORT
void CFPlugInRemoveInstanceForFactory(CFUUIDRef factoryID
) {
173 _CFPFactoryRef factory
= _CFPFactoryFind(factoryID
, true);
176 /* MF:!!! Error. Could not find factory. */
178 _CFPFactoryRemoveInstance(factory
);