~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/dom/src/base/nsDOMScriptObjectFactory.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 
2
 *
 
3
 * The contents of this file are subject to the Netscape Public
 
4
 * License Version 1.1 (the "License"); you may not use this file
 
5
 * except in compliance with the License. You may obtain a copy of
 
6
 * the License at http://www.mozilla.org/NPL/
 
7
 *
 
8
 * Software distributed under the License is distributed on an "AS
 
9
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 
10
 * implied. See the License for the specific language governing
 
11
 * rights and limitations under the License.
 
12
 *
 
13
 * The Original Code is mozilla.org code.
 
14
 *
 
15
 * The Initial Developer of the Original Code is Netscape
 
16
 * Communications Corporation.  Portions created by Netscape are
 
17
 * Copyright (C) 1998 Netscape Communications Corporation. All
 
18
 * Rights Reserved.
 
19
 *
 
20
 * Contributor(s): 
 
21
 *
 
22
 *
 
23
 * This Original Code has been modified by IBM Corporation.
 
24
 * Modifications made by IBM described herein are
 
25
 * Copyright (c) International Business Machines
 
26
 * Corporation, 2000
 
27
 *
 
28
 * Modifications to Mozilla code or documentation
 
29
 * identified per MPL Section 3.3
 
30
 *
 
31
 * Date         Modified by     Description of modification
 
32
 * 03/27/2000   IBM Corp.       Added PR_CALLBACK for Optlink
 
33
 *                               use in OS2
 
34
 */
 
35
 
 
36
#include "nsDOMScriptObjectFactory.h"
 
37
#include "nsScriptNameSpaceManager.h"
 
38
#include "nsIObserverService.h"
 
39
#include "nsJSEnvironment.h"
 
40
#include "nsJSEventListener.h"
 
41
#include "nsGlobalWindow.h"
 
42
#include "nsIJSContextStack.h"
 
43
#include "nsDOMException.h"
 
44
#ifdef MOZ_XUL
 
45
#include "nsIXULPrototypeCache.h"
 
46
#endif
 
47
 
 
48
nsDOMScriptObjectFactory::nsDOMScriptObjectFactory()
 
49
{
 
50
  nsCOMPtr<nsIObserverService> observerService =
 
51
    do_GetService("@mozilla.org/observer-service;1");
 
52
 
 
53
  if (observerService) {
 
54
    observerService->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, PR_FALSE);
 
55
  }
 
56
 
 
57
  nsCOMPtr<nsIExceptionService> xs =
 
58
    do_GetService(NS_EXCEPTIONSERVICE_CONTRACTID);
 
59
 
 
60
  if (xs) {
 
61
    xs->RegisterExceptionProvider(this, NS_ERROR_MODULE_DOM);
 
62
    xs->RegisterExceptionProvider(this, NS_ERROR_MODULE_DOM_RANGE);
 
63
  }
 
64
}
 
65
 
 
66
NS_INTERFACE_MAP_BEGIN(nsDOMScriptObjectFactory)
 
67
  NS_INTERFACE_MAP_ENTRY(nsIDOMScriptObjectFactory)
 
68
  NS_INTERFACE_MAP_ENTRY(nsIObserver)
 
69
  NS_INTERFACE_MAP_ENTRY(nsIExceptionProvider)
 
70
  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMScriptObjectFactory)
 
71
NS_INTERFACE_MAP_END
 
72
 
 
73
 
 
74
NS_IMPL_ADDREF(nsDOMScriptObjectFactory)
 
75
NS_IMPL_RELEASE(nsDOMScriptObjectFactory)
 
76
 
 
77
 
 
78
NS_IMETHODIMP
 
79
nsDOMScriptObjectFactory::NewScriptContext(nsIScriptGlobalObject *aGlobal,
 
80
                                           nsIScriptContext **aContext)
 
81
{
 
82
  return NS_CreateScriptContext(aGlobal, aContext);
 
83
}
 
84
 
 
85
NS_IMETHODIMP
 
86
nsDOMScriptObjectFactory::NewJSEventListener(nsIScriptContext *aContext,
 
87
                                             nsISupports *aObject,
 
88
                                             nsIDOMEventListener **aInstancePtrResult)
 
89
{
 
90
  return NS_NewJSEventListener(aInstancePtrResult, aContext, aObject);
 
91
}
 
92
 
 
93
NS_IMETHODIMP
 
94
nsDOMScriptObjectFactory::NewScriptGlobalObject(PRBool aIsChrome,
 
95
                                                nsIScriptGlobalObject **aGlobal)
 
96
{
 
97
  return NS_NewScriptGlobalObject(aIsChrome, aGlobal);
 
98
}
 
99
 
 
100
NS_IMETHODIMP_(nsISupports *)
 
101
nsDOMScriptObjectFactory::GetClassInfoInstance(nsDOMClassInfoID aID)
 
102
{
 
103
  return nsDOMClassInfo::GetClassInfoInstance(aID);
 
104
}
 
105
 
 
106
NS_IMETHODIMP_(nsISupports *)
 
107
nsDOMScriptObjectFactory::GetExternalClassInfoInstance(const nsAString& aName)
 
108
{
 
109
  extern nsScriptNameSpaceManager *gNameSpaceManager;
 
110
 
 
111
  NS_ENSURE_TRUE(gNameSpaceManager, nsnull);
 
112
 
 
113
  const nsGlobalNameStruct *globalStruct;
 
114
  gNameSpaceManager->LookupName(aName, &globalStruct);
 
115
  if (globalStruct) {
 
116
    if (globalStruct->mType == nsGlobalNameStruct::eTypeExternalClassInfoCreator) {
 
117
      nsresult rv;
 
118
      nsCOMPtr<nsIDOMCIExtension> creator(do_CreateInstance(globalStruct->mCID, &rv));
 
119
      NS_ENSURE_SUCCESS(rv, nsnull);
 
120
 
 
121
      rv = creator->RegisterDOMCI(NS_ConvertUCS2toUTF8(aName).get(), this);
 
122
      NS_ENSURE_SUCCESS(rv, nsnull);
 
123
 
 
124
      rv = gNameSpaceManager->LookupName(aName, &globalStruct);
 
125
      NS_ENSURE_TRUE(NS_SUCCEEDED(rv) && globalStruct, nsnull);
 
126
 
 
127
      NS_ASSERTION(globalStruct->mType == nsGlobalNameStruct::eTypeExternalClassInfo,
 
128
                   "The classinfo data for this class didn't get registered.");
 
129
    }
 
130
    if (globalStruct->mType == nsGlobalNameStruct::eTypeExternalClassInfo) {
 
131
      return nsDOMClassInfo::GetClassInfoInstance(globalStruct->mData);
 
132
    }
 
133
  }
 
134
  return nsnull;
 
135
}
 
136
 
 
137
NS_IMETHODIMP
 
138
nsDOMScriptObjectFactory::Observe(nsISupports *aSubject,
 
139
                                  const char *aTopic,
 
140
                                  const PRUnichar *someData)
 
141
{
 
142
  if (!nsCRT::strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID)) {
 
143
#ifdef MOZ_XUL
 
144
    // Flush the XUL cache since it holds JS roots, and we're about to
 
145
    // start the final GC.
 
146
    nsCOMPtr<nsIXULPrototypeCache> cache =
 
147
      do_GetService("@mozilla.org/xul/xul-prototype-cache;1");
 
148
 
 
149
    if (cache)
 
150
      cache->Flush();
 
151
#endif
 
152
 
 
153
    nsCOMPtr<nsIThreadJSContextStack> stack =
 
154
      do_GetService("@mozilla.org/js/xpc/ContextStack;1");
 
155
 
 
156
    if (stack) {
 
157
      JSContext *cx = nsnull;
 
158
 
 
159
      stack->GetSafeJSContext(&cx);
 
160
 
 
161
      if (cx) {
 
162
        // Do one final GC to clean things up before shutdown.
 
163
 
 
164
        ::JS_GC(cx);
 
165
      }
 
166
    }
 
167
 
 
168
    GlobalWindowImpl::ShutDown();
 
169
    nsDOMClassInfo::ShutDown();
 
170
    nsJSEnvironment::ShutDown();
 
171
 
 
172
    nsCOMPtr<nsIExceptionService> xs =
 
173
      do_GetService(NS_EXCEPTIONSERVICE_CONTRACTID);
 
174
 
 
175
    if (xs) {
 
176
      xs->UnregisterExceptionProvider(this, NS_ERROR_MODULE_DOM);
 
177
    }
 
178
  }
 
179
 
 
180
  return NS_OK;
 
181
}
 
182
 
 
183
NS_IMETHODIMP
 
184
nsDOMScriptObjectFactory::GetException(nsresult result,
 
185
                                       nsIException *aDefaultException,
 
186
                                       nsIException **_retval)
 
187
{
 
188
  if (NS_ERROR_GET_MODULE(result) == NS_ERROR_MODULE_DOM_RANGE) {
 
189
    return NS_NewRangeException(result, aDefaultException, _retval);
 
190
  }
 
191
  return NS_NewDOMException(result, aDefaultException, _retval);
 
192
}
 
193
 
 
194
NS_IMETHODIMP
 
195
nsDOMScriptObjectFactory::RegisterDOMClassInfo(const char *aName,
 
196
                                               nsDOMClassInfoExternalConstructorFnc aConstructorFptr,
 
197
                                               const nsIID *aProtoChainInterface,
 
198
                                               const nsIID **aInterfaces,
 
199
                                               PRUint32 aScriptableFlags,
 
200
                                               PRBool aHasClassInterface,
 
201
                                               const nsCID *aConstructorCID)
 
202
{
 
203
  extern nsScriptNameSpaceManager *gNameSpaceManager;
 
204
 
 
205
  NS_ENSURE_TRUE(gNameSpaceManager, NS_ERROR_NOT_INITIALIZED);
 
206
 
 
207
  return gNameSpaceManager->RegisterDOMCIData(aName,
 
208
                                              aConstructorFptr,
 
209
                                              aProtoChainInterface,
 
210
                                              aInterfaces,
 
211
                                              aScriptableFlags,
 
212
                                              aHasClassInterface,
 
213
                                              aConstructorCID);
 
214
}