Module.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef mozilla_Module_h
  6. #define mozilla_Module_h
  7. #include "nscore.h"
  8. #include "nsID.h"
  9. #include "nsIFactory.h"
  10. #include "nsCOMPtr.h" // for already_AddRefed
  11. namespace mozilla {
  12. /**
  13. * A module implements one or more XPCOM components. This structure is used
  14. * for both binary and script modules, but the registration members
  15. * (cids/contractids/categoryentries) are unused for modules which are loaded
  16. * via a module loader.
  17. */
  18. struct Module
  19. {
  20. static const unsigned int kVersion = 52;
  21. struct CIDEntry;
  22. typedef already_AddRefed<nsIFactory> (*GetFactoryProcPtr)(
  23. const Module& module, const CIDEntry& entry);
  24. typedef nsresult (*ConstructorProcPtr)(nsISupports* aOuter,
  25. const nsIID& aIID,
  26. void** aResult);
  27. typedef nsresult (*LoadFuncPtr)();
  28. typedef void (*UnloadFuncPtr)();
  29. /**
  30. * This selector allows CIDEntrys to be marked so that they're only loaded
  31. * into certain kinds of processes. Selectors can be combined.
  32. */
  33. enum ProcessSelector
  34. {
  35. ANY_PROCESS = 0x0,
  36. MAIN_PROCESS_ONLY = 0x1,
  37. CONTENT_PROCESS_ONLY = 0x2,
  38. /**
  39. * By default, modules are not loaded in the GPU process, even if
  40. * ANY_PROCESS is specified. This flag enables a module in the
  41. * GPU process.
  42. */
  43. ALLOW_IN_GPU_PROCESS = 0x4
  44. };
  45. /**
  46. * The constructor callback is an implementation detail of the default binary
  47. * loader and may be null.
  48. */
  49. struct CIDEntry
  50. {
  51. const nsCID* cid;
  52. bool service;
  53. GetFactoryProcPtr getFactoryProc;
  54. ConstructorProcPtr constructorProc;
  55. ProcessSelector processSelector;
  56. };
  57. struct ContractIDEntry
  58. {
  59. const char* contractid;
  60. nsID const* cid;
  61. ProcessSelector processSelector;
  62. };
  63. struct CategoryEntry
  64. {
  65. const char* category;
  66. const char* entry;
  67. const char* value;
  68. };
  69. /**
  70. * Binary compatibility check, should be kModuleVersion.
  71. */
  72. unsigned int mVersion;
  73. /**
  74. * An array of CIDs (class IDs) implemented by this module. The final entry
  75. * should be { nullptr }.
  76. */
  77. const CIDEntry* mCIDs;
  78. /**
  79. * An array of mappings from contractid to CID. The final entry should
  80. * be { nullptr }.
  81. */
  82. const ContractIDEntry* mContractIDs;
  83. /**
  84. * An array of category manager entries. The final entry should be
  85. * { nullptr }.
  86. */
  87. const CategoryEntry* mCategoryEntries;
  88. /**
  89. * When the component manager tries to get the factory for a CID, it first
  90. * checks for this module-level getfactory callback. If this function is
  91. * not implemented, it checks the CIDEntry getfactory callback. If that is
  92. * also nullptr, a generic factory is generated using the CIDEntry
  93. * constructor callback which must be non-nullptr.
  94. */
  95. GetFactoryProcPtr getFactoryProc;
  96. /**
  97. * Optional Function which are called when this module is loaded and
  98. * at shutdown. These are not C++ constructor/destructors to avoid
  99. * calling them too early in startup or too late in shutdown.
  100. */
  101. LoadFuncPtr loadProc;
  102. UnloadFuncPtr unloadProc;
  103. /**
  104. * Optional flags which control whether the module loads on a process-type
  105. * basis.
  106. */
  107. ProcessSelector selector;
  108. };
  109. } // namespace mozilla
  110. #if defined(MOZILLA_INTERNAL_API)
  111. # define NSMODULE_NAME(_name) _name##_NSModule
  112. # if defined(_MSC_VER)
  113. # pragma section(".kPStaticModules$M", read)
  114. # pragma comment(linker, "/merge:.kPStaticModules=.rdata")
  115. # define NSMODULE_SECTION __declspec(allocate(".kPStaticModules$M"), dllexport)
  116. # elif defined(__GNUC__)
  117. # if defined(__ELF__)
  118. # define NSMODULE_SECTION __attribute__((section(".kPStaticModules"), visibility("default")))
  119. # elif defined(__MACH__)
  120. # define NSMODULE_SECTION __attribute__((section("__DATA, .kPStaticModules"), visibility("default")))
  121. # elif defined (_WIN32)
  122. # define NSMODULE_SECTION __attribute__((section(".kPStaticModules"), dllexport))
  123. # endif
  124. # endif
  125. # if !defined(NSMODULE_SECTION)
  126. # error Do not know how to define sections.
  127. # endif
  128. # define NSMODULE_DEFN(_name) extern NSMODULE_SECTION mozilla::Module const *const NSMODULE_NAME(_name)
  129. #else
  130. # define NSMODULE_NAME(_name) NSModule
  131. # define NSMODULE_DEFN(_name) extern "C" NS_EXPORT mozilla::Module const *const NSModule
  132. #endif
  133. #endif // mozilla_Module_h