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