forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
nsDOMScriptObjectFactory.cpp
414 lines (371 loc) · 15 KB
/
nsDOMScriptObjectFactory.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=2 sw=2 et tw=78:
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK *****
*
*
* This Original Code has been modified by IBM Corporation.
* Modifications made by IBM described herein are
* Copyright (c) International Business Machines
* Corporation, 2000
*
* Modifications to Mozilla code or documentation
* identified per MPL Section 3.3
*
* Date Modified by Description of modification
* 03/27/2000 IBM Corp. Added PR_CALLBACK for Optlink
* use in OS2
*/
#include "nsDOMScriptObjectFactory.h"
#include "xpcexception.h"
#include "nsScriptNameSpaceManager.h"
#include "nsIObserverService.h"
#include "nsJSEnvironment.h"
#include "nsJSEventListener.h"
#include "nsGlobalWindow.h"
#include "nsIJSContextStack.h"
#include "nsISupportsPrimitives.h"
#include "nsDOMException.h"
#include "nsCRT.h"
#ifdef MOZ_XUL
#include "nsXULPrototypeCache.h"
#endif
#include "nsThreadUtils.h"
static NS_DEFINE_CID(kDOMScriptObjectFactoryCID, NS_DOM_SCRIPT_OBJECT_FACTORY_CID);
nsIExceptionProvider* gExceptionProvider = nsnull;
nsDOMScriptObjectFactory::nsDOMScriptObjectFactory() :
mLoadedAllLanguages(PR_FALSE)
{
nsCOMPtr<nsIObserverService> observerService =
mozilla::services::GetObserverService();
if (observerService) {
observerService->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, PR_FALSE);
}
nsCOMPtr<nsIExceptionProvider> provider(new nsDOMExceptionProvider());
if (provider) {
nsCOMPtr<nsIExceptionService> xs =
do_GetService(NS_EXCEPTIONSERVICE_CONTRACTID);
if (xs) {
xs->RegisterExceptionProvider(provider, NS_ERROR_MODULE_DOM);
xs->RegisterExceptionProvider(provider, NS_ERROR_MODULE_DOM_RANGE);
#ifdef MOZ_SVG
xs->RegisterExceptionProvider(provider, NS_ERROR_MODULE_SVG);
#endif
xs->RegisterExceptionProvider(provider, NS_ERROR_MODULE_DOM_XPATH);
xs->RegisterExceptionProvider(provider, NS_ERROR_MODULE_DOM_INDEXEDDB);
xs->RegisterExceptionProvider(provider, NS_ERROR_MODULE_XPCONNECT);
xs->RegisterExceptionProvider(provider, NS_ERROR_MODULE_DOM_EVENTS);
}
NS_ASSERTION(!gExceptionProvider, "Registered twice?!");
provider.swap(gExceptionProvider);
}
// And pre-create the javascript language.
NS_CreateJSRuntime(getter_AddRefs(mLanguageArray[NS_STID_INDEX(nsIProgrammingLanguage::JAVASCRIPT)]));
}
NS_INTERFACE_MAP_BEGIN(nsDOMScriptObjectFactory)
NS_INTERFACE_MAP_ENTRY(nsIDOMScriptObjectFactory)
NS_INTERFACE_MAP_ENTRY(nsIObserver)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMScriptObjectFactory)
NS_INTERFACE_MAP_END
NS_IMPL_ADDREF(nsDOMScriptObjectFactory)
NS_IMPL_RELEASE(nsDOMScriptObjectFactory)
/**
* Notes about language registration (for language other than js):
* - All language are expected to register (at least) 2 contract IDs
* @mozilla.org/script-language;1?id=%d
* using the language ID as defined in nsIProgrammingLanguage, and
* @mozilla.org/script-language;1?script-type=%s
* using the "mime-type" of the script language
*
* Theoretically, a language could register multiple script-type
* names, although this is discouraged - each language should have one,
* canonical name.
*
* The most common case is that languages are looked up by ID. For this
* reason, we keep an array of languages indexed by this ID - the registry
* is only looked the first request for a language ID.
*
* The registry is looked up and getService called for each query by name.
* (As services are cached by CID, multiple contractIDs will still work
* correctly)
**/
NS_IMETHODIMP
nsDOMScriptObjectFactory::GetScriptRuntime(const nsAString &aLanguageName,
nsIScriptRuntime **aLanguage)
{
// Note that many callers have optimized detection for JS (along with
// supporting various alternate names for JS), so don't call this.
// One exception is for the new "script-type" attribute on a node - and
// there is no need to support backwards compatible names.
// As JS is the default language, this is still rarely called for JS -
// only when a node explicitly sets JS - so that is done last.
nsCAutoString contractid(NS_LITERAL_CSTRING(
"@mozilla.org/script-language;1?script-type="));
// Arbitrarily use utf8 encoding should the name have extended chars
AppendUTF16toUTF8(aLanguageName, contractid);
nsresult rv;
nsCOMPtr<nsIScriptRuntime> lang =
do_GetService(contractid.get(), &rv);
if (NS_FAILED(rv)) {
if (aLanguageName.Equals(NS_LITERAL_STRING("application/javascript")))
return GetScriptRuntimeByID(nsIProgrammingLanguage::JAVASCRIPT, aLanguage);
// Not JS and nothing else we know about.
NS_WARNING("No script language registered for this mime-type");
return NS_ERROR_FACTORY_NOT_REGISTERED;
}
// And stash it away in our array for fast lookup by ID.
PRUint32 lang_ndx = NS_STID_INDEX(lang->GetScriptTypeID());
if (mLanguageArray[lang_ndx] == nsnull) {
mLanguageArray[lang_ndx] = lang;
} else {
// All languages are services - we should have an identical object!
NS_ASSERTION(mLanguageArray[lang_ndx] == lang,
"Got a different language for this ID???");
}
*aLanguage = lang;
NS_IF_ADDREF(*aLanguage);
return NS_OK;
}
NS_IMETHODIMP
nsDOMScriptObjectFactory::GetScriptRuntimeByID(PRUint32 aLanguageID,
nsIScriptRuntime **aLanguage)
{
if (!NS_STID_VALID(aLanguageID)) {
NS_WARNING("Unknown script language");
return NS_ERROR_UNEXPECTED;
}
*aLanguage = mLanguageArray[NS_STID_INDEX(aLanguageID)];
if (!*aLanguage) {
nsCAutoString contractid(NS_LITERAL_CSTRING(
"@mozilla.org/script-language;1?id="));
char langIdStr[25]; // space for an int.
sprintf(langIdStr, "%d", aLanguageID);
contractid += langIdStr;
nsresult rv;
nsCOMPtr<nsIScriptRuntime> lang = do_GetService(contractid.get(), &rv);
if (NS_FAILED(rv)) {
NS_ERROR("Failed to get the script language");
return rv;
}
// Stash it away in our array for fast lookup by ID.
mLanguageArray[NS_STID_INDEX(aLanguageID)] = lang;
*aLanguage = lang;
}
NS_IF_ADDREF(*aLanguage);
return NS_OK;
}
NS_IMETHODIMP
nsDOMScriptObjectFactory::GetIDForScriptType(const nsAString &aLanguageName,
PRUint32 *aScriptTypeID)
{
nsCOMPtr<nsIScriptRuntime> languageRuntime;
nsresult rv;
rv = GetScriptRuntime(aLanguageName, getter_AddRefs(languageRuntime));
if (NS_FAILED(rv))
return rv;
*aScriptTypeID = languageRuntime->GetScriptTypeID();
return NS_OK;
}
NS_IMETHODIMP
nsDOMScriptObjectFactory::NewScriptGlobalObject(PRBool aIsChrome,
PRBool aIsModalContentWindow,
nsIScriptGlobalObject **aGlobal)
{
return NS_NewScriptGlobalObject(aIsChrome, aIsModalContentWindow, aGlobal);
}
NS_IMETHODIMP_(nsISupports *)
nsDOMScriptObjectFactory::GetClassInfoInstance(nsDOMClassInfoID aID)
{
return NS_GetDOMClassInfoInstance(aID);
}
NS_IMETHODIMP_(nsISupports *)
nsDOMScriptObjectFactory::GetExternalClassInfoInstance(const nsAString& aName)
{
nsScriptNameSpaceManager *nameSpaceManager = nsJSRuntime::GetNameSpaceManager();
NS_ENSURE_TRUE(nameSpaceManager, nsnull);
const nsGlobalNameStruct *globalStruct;
nameSpaceManager->LookupName(aName, &globalStruct);
if (globalStruct) {
if (globalStruct->mType == nsGlobalNameStruct::eTypeExternalClassInfoCreator) {
nsresult rv;
nsCOMPtr<nsIDOMCIExtension> creator(do_CreateInstance(globalStruct->mCID, &rv));
NS_ENSURE_SUCCESS(rv, nsnull);
rv = creator->RegisterDOMCI(NS_ConvertUTF16toUTF8(aName).get(), this);
NS_ENSURE_SUCCESS(rv, nsnull);
rv = nameSpaceManager->LookupName(aName, &globalStruct);
NS_ENSURE_TRUE(NS_SUCCEEDED(rv) && globalStruct, nsnull);
NS_ASSERTION(globalStruct->mType == nsGlobalNameStruct::eTypeExternalClassInfo,
"The classinfo data for this class didn't get registered.");
}
if (globalStruct->mType == nsGlobalNameStruct::eTypeExternalClassInfo) {
return nsDOMClassInfo::GetClassInfoInstance(globalStruct->mData);
}
}
return nsnull;
}
NS_IMETHODIMP
nsDOMScriptObjectFactory::Observe(nsISupports *aSubject,
const char *aTopic,
const PRUnichar *someData)
{
if (!nsCRT::strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
#ifdef MOZ_XUL
// Flush the XUL cache since it holds JS roots, and we're about to
// start the final GC.
nsXULPrototypeCache* cache = nsXULPrototypeCache::GetInstance();
if (cache)
cache->Flush();
#endif
nsGlobalWindow::ShutDown();
nsDOMClassInfo::ShutDown();
if (gExceptionProvider) {
nsCOMPtr<nsIExceptionService> xs =
do_GetService(NS_EXCEPTIONSERVICE_CONTRACTID);
if (xs) {
xs->UnregisterExceptionProvider(gExceptionProvider,
NS_ERROR_MODULE_DOM);
xs->UnregisterExceptionProvider(gExceptionProvider,
NS_ERROR_MODULE_DOM_RANGE);
#ifdef MOZ_SVG
xs->UnregisterExceptionProvider(gExceptionProvider,
NS_ERROR_MODULE_SVG);
#endif
xs->UnregisterExceptionProvider(gExceptionProvider,
NS_ERROR_MODULE_DOM_XPATH);
xs->UnregisterExceptionProvider(gExceptionProvider,
NS_ERROR_MODULE_XPCONNECT);
xs->UnregisterExceptionProvider(gExceptionProvider,
NS_ERROR_MODULE_DOM_EVENTS);
}
NS_RELEASE(gExceptionProvider);
}
}
return NS_OK;
}
static nsresult
CreateXPConnectException(nsresult aResult, nsIException *aDefaultException,
nsIException **_retval)
{
// See whether we already have a useful XPConnect exception. If we
// do, let's not create one with _less_ information!
nsCOMPtr<nsIXPCException> exception(do_QueryInterface(aDefaultException));
if (!exception) {
nsresult rv = NS_OK;
exception = do_CreateInstance("@mozilla.org/js/xpc/Exception;1", &rv);
NS_ENSURE_SUCCESS(rv, rv);
rv = exception->Initialize(nsnull, aResult, nsnull, nsnull, nsnull,
nsnull);
NS_ENSURE_SUCCESS(rv, rv);
}
NS_ADDREF(*_retval = exception);
return NS_OK;
}
NS_IMETHODIMP
nsDOMScriptObjectFactory::RegisterDOMClassInfo(const char *aName,
nsDOMClassInfoExternalConstructorFnc aConstructorFptr,
const nsIID *aProtoChainInterface,
const nsIID **aInterfaces,
PRUint32 aScriptableFlags,
PRBool aHasClassInterface,
const nsCID *aConstructorCID)
{
nsScriptNameSpaceManager *nameSpaceManager = nsJSRuntime::GetNameSpaceManager();
NS_ENSURE_TRUE(nameSpaceManager, NS_ERROR_NOT_INITIALIZED);
return nameSpaceManager->RegisterDOMCIData(aName,
aConstructorFptr,
aProtoChainInterface,
aInterfaces,
aScriptableFlags,
aHasClassInterface,
aConstructorCID);
}
// Factories
nsresult NS_GetScriptRuntime(const nsAString &aLanguageName,
nsIScriptRuntime **aLanguage)
{
nsresult rv;
*aLanguage = nsnull;
nsCOMPtr<nsIDOMScriptObjectFactory> factory = \
do_GetService(kDOMScriptObjectFactoryCID, &rv);
if (NS_FAILED(rv))
return rv;
return factory->GetScriptRuntime(aLanguageName, aLanguage);
}
nsresult NS_GetScriptRuntimeByID(PRUint32 aScriptTypeID,
nsIScriptRuntime **aLanguage)
{
nsresult rv;
*aLanguage = nsnull;
nsCOMPtr<nsIDOMScriptObjectFactory> factory = \
do_GetService(kDOMScriptObjectFactoryCID, &rv);
if (NS_FAILED(rv))
return rv;
return factory->GetScriptRuntimeByID(aScriptTypeID, aLanguage);
}
NS_IMPL_THREADSAFE_ISUPPORTS1(nsDOMExceptionProvider, nsIExceptionProvider)
NS_IMETHODIMP
nsDOMExceptionProvider::GetException(nsresult result,
nsIException *aDefaultException,
nsIException **_retval)
{
if (!NS_IsMainThread()) {
return NS_ERROR_NOT_IMPLEMENTED;
}
switch (NS_ERROR_GET_MODULE(result))
{
case NS_ERROR_MODULE_DOM_RANGE:
return NS_NewRangeException(result, aDefaultException, _retval);
#ifdef MOZ_SVG
case NS_ERROR_MODULE_SVG:
return NS_NewSVGException(result, aDefaultException, _retval);
#endif
case NS_ERROR_MODULE_DOM_XPATH:
return NS_NewXPathException(result, aDefaultException, _retval);
case NS_ERROR_MODULE_XPCONNECT:
return CreateXPConnectException(result, aDefaultException, _retval);
case NS_ERROR_MODULE_DOM_FILE:
return NS_NewFileException(result, aDefaultException, _retval);
case NS_ERROR_MODULE_DOM_INDEXEDDB:
return NS_NewIDBDatabaseException(result, aDefaultException, _retval);
case NS_ERROR_MODULE_DOM_EVENTS:
return NS_NewEventException(result, aDefaultException, _retval);
default:
return NS_NewDOMException(result, aDefaultException, _retval);
}
NS_NOTREACHED("Not reached");
return NS_ERROR_UNEXPECTED;
}