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