Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/nsComponentManagerUtils.h
Line
Count
Source (jump to first uncovered line)
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 nsComponentManagerUtils_h__
8
#define nsComponentManagerUtils_h__
9
10
#include "nscore.h"
11
#include "nsCOMPtr.h"
12
13
#include "nsIFactory.h"
14
15
16
nsresult CallCreateInstance(const nsCID& aClass, nsISupports* aDelegate,
17
                            const nsIID& aIID, void** aResult);
18
19
nsresult CallCreateInstance(const char* aContractID, nsISupports* aDelegate,
20
                            const nsIID& aIID, void** aResult);
21
22
nsresult CallGetClassObject(const nsCID& aClass, const nsIID& aIID,
23
                            void** aResult);
24
25
nsresult CallGetClassObject(const char* aContractID, const nsIID& aIID,
26
                            void** aResult);
27
28
29
class MOZ_STACK_CLASS nsCreateInstanceByCID final : public nsCOMPtr_helper
30
{
31
public:
32
  nsCreateInstanceByCID(const nsCID& aCID, nsresult* aErrorPtr)
33
    : mCID(aCID)
34
    , mErrorPtr(aErrorPtr)
35
3
  {
36
3
  }
37
38
  virtual nsresult NS_FASTCALL operator()(const nsIID&, void**) const
39
    override;
40
41
private:
42
  const nsCID&    mCID;
43
  nsresult*       mErrorPtr;
44
};
45
46
class MOZ_STACK_CLASS nsCreateInstanceByContractID final : public nsCOMPtr_helper
47
{
48
public:
49
  nsCreateInstanceByContractID(const char* aContractID, nsresult* aErrorPtr)
50
    : mContractID(aContractID)
51
    , mErrorPtr(aErrorPtr)
52
296k
  {
53
296k
  }
54
55
  virtual nsresult NS_FASTCALL operator()(const nsIID&, void**) const override;
56
57
private:
58
  const char*   mContractID;
59
  nsresult*     mErrorPtr;
60
};
61
62
class MOZ_STACK_CLASS nsCreateInstanceFromFactory final : public nsCOMPtr_helper
63
{
64
public:
65
  nsCreateInstanceFromFactory(nsIFactory* aFactory, nsresult* aErrorPtr)
66
    : mFactory(aFactory)
67
    , mErrorPtr(aErrorPtr)
68
0
  {
69
0
  }
70
71
  virtual nsresult NS_FASTCALL operator()(const nsIID&, void**) const override;
72
73
private:
74
  nsIFactory* MOZ_NON_OWNING_REF mFactory;
75
  nsresult*     mErrorPtr;
76
};
77
78
79
inline const nsCreateInstanceByCID
80
do_CreateInstance(const nsCID& aCID, nsresult* aError = 0)
81
3
{
82
3
  return nsCreateInstanceByCID(aCID, aError);
83
3
}
84
85
inline const nsCreateInstanceByContractID
86
do_CreateInstance(const char* aContractID, nsresult* aError = 0)
87
296k
{
88
296k
  return nsCreateInstanceByContractID(aContractID, aError);
89
296k
}
90
91
inline const nsCreateInstanceFromFactory
92
do_CreateInstance(nsIFactory* aFactory, nsresult* aError = 0)
93
0
{
94
0
  return nsCreateInstanceFromFactory(aFactory, aError);
95
0
}
96
97
98
class MOZ_STACK_CLASS nsGetClassObjectByCID final : public nsCOMPtr_helper
99
{
100
public:
101
  nsGetClassObjectByCID(const nsCID& aCID, nsresult* aErrorPtr)
102
    : mCID(aCID)
103
    , mErrorPtr(aErrorPtr)
104
0
  {
105
0
  }
106
107
  virtual nsresult NS_FASTCALL operator()(const nsIID&, void**) const override;
108
109
private:
110
  const nsCID&    mCID;
111
  nsresult*       mErrorPtr;
112
};
113
114
class MOZ_STACK_CLASS nsGetClassObjectByContractID final : public nsCOMPtr_helper
115
{
116
public:
117
  nsGetClassObjectByContractID(const char* aContractID, nsresult* aErrorPtr)
118
    : mContractID(aContractID)
119
    , mErrorPtr(aErrorPtr)
120
0
  {
121
0
  }
122
123
  virtual nsresult NS_FASTCALL operator()(const nsIID&, void**) const override;
124
125
private:
126
  const char*   mContractID;
127
  nsresult*     mErrorPtr;
128
};
129
130
/**
131
 * do_GetClassObject can be used to improve performance of callers
132
 * that call |CreateInstance| many times.  They can cache the factory
133
 * and call do_CreateInstance or CallCreateInstance with the cached
134
 * factory rather than having the component manager retrieve it every
135
 * time.
136
 */
137
inline const nsGetClassObjectByCID
138
do_GetClassObject(const nsCID& aCID, nsresult* aError = 0)
139
0
{
140
0
  return nsGetClassObjectByCID(aCID, aError);
141
0
}
142
143
inline const nsGetClassObjectByContractID
144
do_GetClassObject(const char* aContractID, nsresult* aError = 0)
145
0
{
146
0
  return nsGetClassObjectByContractID(aContractID, aError);
147
0
}
148
149
// type-safe shortcuts for calling |CreateInstance|
150
template<class DestinationType>
151
inline nsresult
152
CallCreateInstance(const nsCID& aClass,
153
                   nsISupports* aDelegate,
154
                   DestinationType** aDestination)
155
0
{
156
0
  MOZ_ASSERT(aDestination, "null parameter");
157
0
158
0
  return CallCreateInstance(aClass, aDelegate,
159
0
                            NS_GET_TEMPLATE_IID(DestinationType),
160
0
                            reinterpret_cast<void**>(aDestination));
161
0
}
162
163
template<class DestinationType>
164
inline nsresult
165
CallCreateInstance(const nsCID& aClass, DestinationType** aDestination)
166
0
{
167
0
  MOZ_ASSERT(aDestination, "null parameter");
168
0
169
0
  return CallCreateInstance(aClass, nullptr,
170
0
                            NS_GET_TEMPLATE_IID(DestinationType),
171
0
                            reinterpret_cast<void**>(aDestination));
172
0
}
173
174
template<class DestinationType>
175
inline nsresult
176
CallCreateInstance(const char* aContractID,
177
                   nsISupports* aDelegate,
178
                   DestinationType** aDestination)
179
0
{
180
0
  MOZ_ASSERT(aContractID, "null parameter");
181
0
  MOZ_ASSERT(aDestination, "null parameter");
182
0
183
0
  return CallCreateInstance(aContractID,
184
0
                            aDelegate,
185
0
                            NS_GET_TEMPLATE_IID(DestinationType),
186
0
                            reinterpret_cast<void**>(aDestination));
187
0
}
188
189
template<class DestinationType>
190
inline nsresult
191
CallCreateInstance(const char* aContractID, DestinationType** aDestination)
192
0
{
193
0
  MOZ_ASSERT(aContractID, "null parameter");
194
0
  MOZ_ASSERT(aDestination, "null parameter");
195
0
196
0
  return CallCreateInstance(aContractID, nullptr,
197
0
                            NS_GET_TEMPLATE_IID(DestinationType),
198
0
                            reinterpret_cast<void**>(aDestination));
199
0
}
Unexecuted instantiation: nsresult CallCreateInstance<nsIAuthModule>(char const*, nsIAuthModule**)
Unexecuted instantiation: nsresult CallCreateInstance<nsITransferable>(char const*, nsITransferable**)
Unexecuted instantiation: nsresult CallCreateInstance<nsITestService>(char const*, nsITestService**)
200
201
template<class DestinationType>
202
inline nsresult
203
CallCreateInstance(nsIFactory* aFactory,
204
                   nsISupports* aDelegate,
205
                   DestinationType** aDestination)
206
{
207
  MOZ_ASSERT(aFactory, "null parameter");
208
  MOZ_ASSERT(aDestination, "null parameter");
209
210
  return aFactory->CreateInstance(aDelegate,
211
                                  NS_GET_TEMPLATE_IID(DestinationType),
212
                                  reinterpret_cast<void**>(aDestination));
213
}
214
215
template<class DestinationType>
216
inline nsresult
217
CallCreateInstance(nsIFactory* aFactory, DestinationType** aDestination)
218
{
219
  MOZ_ASSERT(aFactory, "null parameter");
220
  MOZ_ASSERT(aDestination, "null parameter");
221
222
  return aFactory->CreateInstance(nullptr,
223
                                  NS_GET_TEMPLATE_IID(DestinationType),
224
                                  reinterpret_cast<void**>(aDestination));
225
}
226
227
template<class DestinationType>
228
inline nsresult
229
CallGetClassObject(const nsCID& aClass, DestinationType** aDestination)
230
{
231
  MOZ_ASSERT(aDestination, "null parameter");
232
233
  return CallGetClassObject(aClass, NS_GET_TEMPLATE_IID(DestinationType),
234
                            reinterpret_cast<void**>(aDestination));
235
}
236
237
template<class DestinationType>
238
inline nsresult
239
CallGetClassObject(const char* aContractID, DestinationType** aDestination)
240
{
241
  MOZ_ASSERT(aDestination, "null parameter");
242
243
  return CallGetClassObject(aContractID, NS_GET_TEMPLATE_IID(DestinationType),
244
                            reinterpret_cast<void**>(aDestination));
245
}
246
247
#endif /* nsComponentManagerUtils_h__ */