1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef mozilla_Module_h
8 #define mozilla_Module_h
9 
10 #include "nscore.h"
11 #include "nsID.h"
12 #include "nsIFactory.h"
13 #include "nsCOMPtr.h"  // for already_AddRefed
14 
15 namespace mozilla {
16 
17 /**
18  * A module implements one or more XPCOM components. This structure is used
19  * for both binary and script modules, but the registration members
20  * (cids/contractids/categoryentries) are unused for modules which are loaded
21  * via a module loader.
22  */
23 struct Module {
24   static const unsigned int kVersion = 91;
25 
26   struct CIDEntry;
27 
28   typedef already_AddRefed<nsIFactory> (*GetFactoryProcPtr)(
29       const Module& module, const CIDEntry& entry);
30 
31   typedef nsresult (*ConstructorProcPtr)(nsISupports* aOuter, const nsIID& aIID,
32                                          void** aResult);
33 
34   typedef nsresult (*LoadFuncPtr)();
35   typedef void (*UnloadFuncPtr)();
36 
37   /**
38    * This selector allows CIDEntrys to be marked so that they're only loaded
39    * into certain kinds of processes. Selectors can be combined.
40    */
41   // Note: This must be kept in sync with the selector matching in
42   // nsComponentManager.cpp.
43   enum ProcessSelector {
44     ANY_PROCESS = 0x0,
45     MAIN_PROCESS_ONLY = 0x1,
46     CONTENT_PROCESS_ONLY = 0x2,
47 
48     /**
49      * By default, modules are not loaded in the GPU process, even if
50      * ANY_PROCESS is specified. This flag enables a module in the
51      * GPU process.
52      */
53     ALLOW_IN_GPU_PROCESS = 0x4,
54     ALLOW_IN_VR_PROCESS = 0x8,
55     ALLOW_IN_SOCKET_PROCESS = 0x10,
56     ALLOW_IN_RDD_PROCESS = 0x20,
57     ALLOW_IN_GPU_AND_MAIN_PROCESS = ALLOW_IN_GPU_PROCESS | MAIN_PROCESS_ONLY,
58     ALLOW_IN_GPU_AND_VR_PROCESS = ALLOW_IN_GPU_PROCESS | ALLOW_IN_VR_PROCESS,
59     ALLOW_IN_GPU_AND_SOCKET_PROCESS =
60         ALLOW_IN_GPU_PROCESS | ALLOW_IN_SOCKET_PROCESS,
61     ALLOW_IN_GPU_VR_AND_SOCKET_PROCESS =
62         ALLOW_IN_GPU_PROCESS | ALLOW_IN_VR_PROCESS | ALLOW_IN_SOCKET_PROCESS,
63     ALLOW_IN_RDD_AND_SOCKET_PROCESS =
64         ALLOW_IN_RDD_PROCESS | ALLOW_IN_SOCKET_PROCESS,
65     ALLOW_IN_GPU_RDD_AND_SOCKET_PROCESS =
66         ALLOW_IN_GPU_PROCESS | ALLOW_IN_RDD_PROCESS | ALLOW_IN_SOCKET_PROCESS,
67     ALLOW_IN_GPU_RDD_VR_AND_SOCKET_PROCESS =
68         ALLOW_IN_GPU_PROCESS | ALLOW_IN_RDD_PROCESS | ALLOW_IN_VR_PROCESS |
69         ALLOW_IN_SOCKET_PROCESS
70   };
71 
72   static constexpr size_t kMaxProcessSelector =
73       size_t(ProcessSelector::ALLOW_IN_GPU_RDD_VR_AND_SOCKET_PROCESS);
74 
75   /**
76    * This allows category entries to be marked so that they are or are
77    * not loaded when in backgroundtask mode.
78    */
79   // Note: This must be kept in sync with the selector matching in
80   // StaticComponents.cpp.in.
81   enum BackgroundTasksSelector {
82     NO_TASKS = 0x0,
83     ALL_TASKS = 0xFFFF,
84   };
85 
86   /**
87    * The constructor callback is an implementation detail of the default binary
88    * loader and may be null.
89    */
90   struct CIDEntry {
91     const nsCID* cid;
92     bool service;
93     GetFactoryProcPtr getFactoryProc;
94     ConstructorProcPtr constructorProc;
95     ProcessSelector processSelector;
96   };
97 
98   struct ContractIDEntry {
99     const char* contractid;
100     nsID const* cid;
101     ProcessSelector processSelector;
102   };
103 
104   struct CategoryEntry {
105     const char* category;
106     const char* entry;
107     const char* value;
108   };
109 
110   /**
111    * Binary compatibility check, should be kModuleVersion.
112    */
113   unsigned int mVersion;
114 
115   /**
116    * An array of CIDs (class IDs) implemented by this module. The final entry
117    * should be { nullptr }.
118    */
119   const CIDEntry* mCIDs;
120 
121   /**
122    * An array of mappings from contractid to CID. The final entry should
123    * be { nullptr }.
124    */
125   const ContractIDEntry* mContractIDs;
126 
127   /**
128    * An array of category manager entries. The final entry should be
129    * { nullptr }.
130    */
131   const CategoryEntry* mCategoryEntries;
132 
133   /**
134    * When the component manager tries to get the factory for a CID, it first
135    * checks for this module-level getfactory callback. If this function is
136    * not implemented, it checks the CIDEntry getfactory callback. If that is
137    * also nullptr, a generic factory is generated using the CIDEntry
138    * constructor callback which must be non-nullptr.
139    */
140   GetFactoryProcPtr getFactoryProc;
141 
142   /**
143    * Optional Function which are called when this module is loaded and
144    * at shutdown. These are not C++ constructor/destructors to avoid
145    * calling them too early in startup or too late in shutdown.
146    */
147   LoadFuncPtr loadProc;
148   UnloadFuncPtr unloadProc;
149 
150   /**
151    * Optional flags which control whether the module loads on a process-type
152    * basis.
153    */
154   ProcessSelector selector;
155 };
156 
157 }  // namespace mozilla
158 
159 #endif  // mozilla_Module_h
160