]> git.saurik.com Git - apple/cf.git/blob - PlugIn.subproj/CFPlugIn.c
CF-299.32.tar.gz
[apple/cf.git] / PlugIn.subproj / CFPlugIn.c
1 /*
2 * Copyright (c) 2003 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 /* CFPlugIn.c
26 Copyright 1999-2002, Apple, Inc. All rights reserved.
27 Responsibility: Doug Davidson
28 */
29
30 #include "CFBundle_Internal.h"
31 #include "CFInternal.h"
32
33 CONST_STRING_DECL(kCFPlugInDynamicRegistrationKey, "CFPlugInDynamicRegistration")
34 CONST_STRING_DECL(kCFPlugInDynamicRegisterFunctionKey, "CFPlugInDynamicRegisterFunction")
35 CONST_STRING_DECL(kCFPlugInUnloadFunctionKey, "CFPlugInUnloadFunction")
36 CONST_STRING_DECL(kCFPlugInFactoriesKey, "CFPlugInFactories")
37 CONST_STRING_DECL(kCFPlugInTypesKey, "CFPlugInTypes")
38
39 __private_extern__ void __CFPlugInInitialize(void) {
40 }
41
42 /* ===================== Finding factories and creating instances ===================== */
43 /* For plugIn hosts. */
44 /* Functions for finding factories to create specific types and actually creating instances of a type. */
45
46 CF_EXPORT CFArrayRef CFPlugInFindFactoriesForPlugInType(CFUUIDRef typeID) {
47 CFArrayRef array = _CFPFactoryFindForType(typeID);
48 CFMutableArrayRef result = NULL;
49
50 if (array) {
51 SInt32 i, c = CFArrayGetCount(array);
52
53 // Use default allocator
54 result = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
55
56 for (i=0; i<c; i++) {
57 CFArrayAppendValue(result, _CFPFactoryGetFactoryID((_CFPFactory *)CFArrayGetValueAtIndex(array, i)));
58 }
59 }
60 return result;
61 }
62
63 CF_EXPORT CFArrayRef CFPlugInFindFactoriesForPlugInTypeInPlugIn(CFUUIDRef typeID, CFPlugInRef plugIn) {
64 CFArrayRef array = _CFPFactoryFindForType(typeID);
65 CFMutableArrayRef result = NULL;
66
67 if (array) {
68 SInt32 i, c = CFArrayGetCount(array);
69 _CFPFactory *factory;
70
71 // Use default allocator
72 result = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
73
74 for (i=0; i<c; i++) {
75 factory = (_CFPFactory *)CFArrayGetValueAtIndex(array, i);
76 if (_CFPFactoryGetPlugIn(factory) == plugIn) {
77 CFArrayAppendValue(result, _CFPFactoryGetFactoryID(factory));
78 }
79 }
80 }
81 return result;
82 }
83
84 CF_EXPORT void *CFPlugInInstanceCreate(CFAllocatorRef allocator, CFUUIDRef factoryID, CFUUIDRef typeID) {
85 _CFPFactory *factory = _CFPFactoryFind(factoryID, true);
86 void *result = NULL;
87 if (!factory) {
88 /* MF:!!! No such factory. */
89 CFLog(__kCFLogPlugIn, CFSTR("Cannot find factory %@"), factoryID);
90 } else {
91 if (!_CFPFactorySupportsType(factory, typeID)) {
92 /* MF:!!! Factory does not support type. */
93 CFLog(__kCFLogPlugIn, CFSTR("Factory %@ does not support type %@"), factoryID, typeID);
94 } else {
95 result = _CFPFactoryCreateInstance(allocator, factory, typeID);
96 }
97 }
98 return result;
99 }
100
101 /* ===================== Registering factories and types ===================== */
102 /* For plugIn writers who must dynamically register things. */
103 /* Functions to register factory functions and to associate factories with types. */
104
105 CF_EXPORT Boolean CFPlugInRegisterFactoryFunction(CFUUIDRef factoryID, CFPlugInFactoryFunction func) {
106 // Create factories without plugIns from default allocator
107 // MF:!!! Should probably check that this worked, and maybe do some pre-checking to see if it already exists
108 // _CFPFactory *factory =
109 (void)_CFPFactoryCreate(NULL, factoryID, func);
110 return true;
111 }
112
113 CF_EXPORT Boolean CFPlugInRegisterFactoryFunctionByName(CFUUIDRef factoryID, CFPlugInRef plugIn, CFStringRef functionName) {
114 // Create factories with plugIns from plugIn's allocator
115 // MF:!!! Should probably check that this worked, and maybe do some pre-checking to see if it already exists
116 // _CFPFactory *factory =
117 (void)_CFPFactoryCreateByName(CFGetAllocator(plugIn), factoryID, plugIn, functionName);
118 return true;
119 }
120
121 CF_EXPORT Boolean CFPlugInUnregisterFactory(CFUUIDRef factoryID) {
122 _CFPFactory *factory = _CFPFactoryFind(factoryID, true);
123
124 if (!factory) {
125 /* MF:!!! Error. No factory registered for this ID. */
126 } else {
127 _CFPFactoryDisable(factory);
128 }
129 return true;
130 }
131
132 CF_EXPORT Boolean CFPlugInRegisterPlugInType(CFUUIDRef factoryID, CFUUIDRef typeID) {
133 _CFPFactory *factory = _CFPFactoryFind(factoryID, true);
134
135 if (!factory) {
136 /* MF:!!! Error. Factory must be registered (and not disabled) before types can be associated with it. */
137 } else {
138 _CFPFactoryAddType(factory, typeID);
139 }
140 return true;
141 }
142
143 CF_EXPORT Boolean CFPlugInUnregisterPlugInType(CFUUIDRef factoryID, CFUUIDRef typeID) {
144 _CFPFactory *factory = _CFPFactoryFind(factoryID, true);
145
146 if (!factory) {
147 /* MF:!!! Error. Could not find factory. */
148 } else {
149 _CFPFactoryRemoveType(factory, typeID);
150 }
151 return true;
152 }
153
154
155 /* ================= Registering instances ================= */
156 /* 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. */
157 /* 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. */
158
159 CF_EXPORT void CFPlugInAddInstanceForFactory(CFUUIDRef factoryID) {
160 _CFPFactory *factory = _CFPFactoryFind(factoryID, true);
161
162 if (!factory) {
163 /* MF:!!! Error. Could not find factory. */
164 } else {
165 _CFPFactoryAddInstance(factory);
166 }
167 }
168
169 CF_EXPORT void CFPlugInRemoveInstanceForFactory(CFUUIDRef factoryID) {
170 _CFPFactory *factory = _CFPFactoryFind(factoryID, true);
171
172 if (!factory) {
173 /* MF:!!! Error. Could not find factory. */
174 } else {
175 _CFPFactoryRemoveInstance(factory);
176 }
177 }