/core/externals/update-engine/externals/google-toolbox-for-mac/SpotlightPlugins/Common/main.c

http://macfuse.googlecode.com/ · C · 204 lines · 93 code · 21 blank · 90 comment · 10 complexity · fc5a76a2d210c9273a59030acc598747 MD5 · raw file

  1. //
  2. // main.c
  3. //
  4. // Copyright 2008 Google Inc.
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7. // use this file except in compliance with the License. You may obtain a copy
  8. // of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  15. // License for the specific language governing permissions and limitations under
  16. // the License.
  17. //
  18. #include <CoreFoundation/CoreFoundation.h>
  19. #include <CoreFoundation/CFPlugInCOM.h>
  20. #include <CoreServices/CoreServices.h>
  21. #include "PluginID.h"
  22. // -----------------------------------------------------------------------------
  23. // constants
  24. // -----------------------------------------------------------------------------
  25. //
  26. // Below is the generic glue code for all plug-ins.
  27. //
  28. // You should not have to modify this code aside from changing
  29. // names if you decide to change the names defined in the Info.plist
  30. //
  31. // -----------------------------------------------------------------------------
  32. // typedefs
  33. // -----------------------------------------------------------------------------
  34. // The import function to be implemented in GetMetadataForFile.c
  35. Boolean GetMetadataForFile(void *thisInterface,
  36. CFMutableDictionaryRef attributes,
  37. CFStringRef contentTypeUTI,
  38. CFStringRef pathToFile);
  39. // The layout for an instance of MetaDataImporterPlugIn
  40. typedef struct __MetadataImporterPluginType {
  41. MDImporterInterfaceStruct *conduitInterface;
  42. CFUUIDRef factoryID;
  43. UInt32 refCount;
  44. } MetadataImporterPluginType;
  45. // -----------------------------------------------------------------------------
  46. // prototypes
  47. // -----------------------------------------------------------------------------
  48. // Forward declaration for the IUnknown implementation.
  49. //
  50. MetadataImporterPluginType* AllocMetadataImporterPluginType(CFUUIDRef inFactoryID);
  51. void DeallocMetadataImporterPluginType(MetadataImporterPluginType *instance);
  52. void* MetadataImporterPluginFactory(CFAllocatorRef allocator, CFUUIDRef typeID);
  53. static ULONG MetadataImporterPluginAddRef(void *instance);
  54. static ULONG MetadataImporterPluginRelease(void *instance);
  55. static HRESULT MetadataImporterQueryInterface(void *instance, REFIID iid, LPVOID *ppv);
  56. // -----------------------------------------------------------------------------
  57. // testInterfaceFtbl definition
  58. // -----------------------------------------------------------------------------
  59. // The TestInterface function table.
  60. //
  61. static MDImporterInterfaceStruct testInterfaceFtbl = {
  62. NULL,
  63. MetadataImporterQueryInterface,
  64. MetadataImporterPluginAddRef,
  65. MetadataImporterPluginRelease,
  66. GetMetadataForFile
  67. };
  68. // -----------------------------------------------------------------------------
  69. // AllocMetadataImporterPluginType
  70. // -----------------------------------------------------------------------------
  71. // Utility function that allocates a new instance.
  72. // You can do some initial setup for the importer here if you wish
  73. // like allocating globals etc...
  74. //
  75. MetadataImporterPluginType *AllocMetadataImporterPluginType(CFUUIDRef inFactoryID) {
  76. MetadataImporterPluginType *theNewInstance
  77. = (MetadataImporterPluginType *)malloc(sizeof(MetadataImporterPluginType));
  78. memset(theNewInstance, 0, sizeof(MetadataImporterPluginType));
  79. // Point to the function table
  80. theNewInstance->conduitInterface = &testInterfaceFtbl;
  81. // Retain and keep an open instance refcount for each factory.
  82. theNewInstance->factoryID = CFRetain(inFactoryID);
  83. CFPlugInAddInstanceForFactory(inFactoryID);
  84. // This function returns the IUnknown interface so set the refCount to one.
  85. theNewInstance->refCount = 1;
  86. return theNewInstance;
  87. }
  88. // -----------------------------------------------------------------------------
  89. // DeallocXcodeProjectSpotlightPluginMDImporterPluginType
  90. // -----------------------------------------------------------------------------
  91. // Utility function that deallocates the instance when
  92. // the refCount goes to zero.
  93. // In the current implementation importer interfaces are never deallocated
  94. // but implement this as this might change in the future
  95. //
  96. void DeallocMetadataImporterPluginType(MetadataImporterPluginType *instance) {
  97. CFUUIDRef theFactoryID = instance->factoryID;
  98. free(instance);
  99. if (theFactoryID) {
  100. CFPlugInRemoveInstanceForFactory(theFactoryID);
  101. CFRelease(theFactoryID);
  102. }
  103. }
  104. // -----------------------------------------------------------------------------
  105. // MetadataImporterQueryInterface
  106. // -----------------------------------------------------------------------------
  107. // Implementation of the IUnknown QueryInterface function.
  108. //
  109. HRESULT MetadataImporterQueryInterface(void *instance, REFIID iid, LPVOID *ppv) {
  110. CFUUIDRef interfaceID = CFUUIDCreateFromUUIDBytes(kCFAllocatorDefault, iid);
  111. MetadataImporterPluginType *plugin = ((MetadataImporterPluginType*)instance);
  112. HRESULT result = E_INVALIDARG;
  113. if (interfaceID) {
  114. if (CFEqual(interfaceID, kMDImporterInterfaceID)) {
  115. // If the Right interface was requested, bump the ref count,
  116. // set the ppv parameter equal to the instance, and
  117. // return good status.
  118. plugin->conduitInterface->AddRef(instance);
  119. *ppv = instance;
  120. result = S_OK;
  121. } else {
  122. if (CFEqual(interfaceID, IUnknownUUID)) {
  123. // If the IUnknown interface was requested, same as above.
  124. plugin->conduitInterface->AddRef(instance);
  125. *ppv = instance;
  126. result = S_OK;
  127. } else {
  128. // Requested interface unknown, bail with error.
  129. *ppv = NULL;
  130. result = E_NOINTERFACE;
  131. }
  132. }
  133. CFRelease(interfaceID);
  134. }
  135. return result;
  136. }
  137. // -----------------------------------------------------------------------------
  138. // MetadataImporterPluginAddRef
  139. // -----------------------------------------------------------------------------
  140. // Implementation of reference counting for this type. Whenever an interface
  141. // is requested, bump the refCount for the instance. NOTE: returning the
  142. // refcount is a convention but is not required so don't rely on it.
  143. //
  144. ULONG MetadataImporterPluginAddRef(void *instance) {
  145. MetadataImporterPluginType *plugin = ((MetadataImporterPluginType*)instance);
  146. plugin->refCount += 1;
  147. return plugin->refCount;
  148. }
  149. // -----------------------------------------------------------------------------
  150. // SampleCMPluginRelease
  151. // -----------------------------------------------------------------------------
  152. // When an interface is released, decrement the refCount.
  153. // If the refCount goes to zero, deallocate the instance.
  154. //
  155. ULONG MetadataImporterPluginRelease(void *instance) {
  156. ULONG refCount = 0;
  157. MetadataImporterPluginType *plugin = ((MetadataImporterPluginType*)instance);
  158. plugin->refCount -= 1;
  159. if (plugin->refCount == 0) {
  160. DeallocMetadataImporterPluginType(plugin);
  161. refCount = 0;
  162. } else {
  163. refCount = (plugin)->refCount;
  164. }
  165. return refCount;
  166. }
  167. // -----------------------------------------------------------------------------
  168. // XcodeProjectSpotlightPluginMDImporterPluginFactory
  169. // -----------------------------------------------------------------------------
  170. // Implementation of the factory function for this type.
  171. //
  172. void *MetadataImporterPluginFactory(CFAllocatorRef allocator, CFUUIDRef typeID) {
  173. // If correct type is being requested, allocate an
  174. //instance of TestType and return the IUnknown interface.
  175. MetadataImporterPluginType *result = NULL;
  176. if (CFEqual(typeID, kMDImporterTypeID)){
  177. CFUUIDRef uuid = CFUUIDCreateFromString(kCFAllocatorDefault, CFSTR(PLUGIN_ID));
  178. result = AllocMetadataImporterPluginType(uuid);
  179. CFRelease(uuid);
  180. }
  181. // If the requested type is incorrect, return NULL.
  182. return result;
  183. }