1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Mike McCabe <mccabe@netscape.com>
24 : * John Bandhauer <jband@netscape.com>
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either of the GNU General Public License Version 2 or later (the "GPL"),
28 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : /*
41 : * nsIScriptError implementation. Defined here, lacking a JS-specific
42 : * place to put XPCOM things.
43 : */
44 :
45 : #include "xpcprivate.h"
46 : #include "nsGlobalWindow.h"
47 : #include "nsPIDOMWindow.h"
48 :
49 55918 : NS_IMPL_THREADSAFE_ISUPPORTS2(nsScriptError, nsIConsoleMessage, nsIScriptError)
50 :
51 3316 : nsScriptError::nsScriptError()
52 : : mMessage(),
53 : mSourceName(),
54 : mLineNumber(0),
55 : mSourceLine(),
56 : mColumnNumber(0),
57 : mFlags(0),
58 : mCategory(),
59 : mOuterWindowID(0),
60 : mInnerWindowID(0),
61 3316 : mTimeStamp(0)
62 : {
63 3316 : }
64 :
65 13264 : nsScriptError::~nsScriptError() {}
66 :
67 : // nsIConsoleMessage methods
68 : NS_IMETHODIMP
69 256 : nsScriptError::GetMessageMoz(PRUnichar **result) {
70 : nsresult rv;
71 :
72 512 : nsCAutoString message;
73 256 : rv = ToString(message);
74 256 : if (NS_FAILED(rv))
75 0 : return rv;
76 :
77 256 : *result = UTF8ToNewUnicode(message);
78 256 : if (!*result)
79 0 : return NS_ERROR_OUT_OF_MEMORY;
80 :
81 256 : return NS_OK;
82 : }
83 :
84 : // nsIScriptError methods
85 : NS_IMETHODIMP
86 12 : nsScriptError::GetErrorMessage(nsAString& aResult) {
87 12 : aResult.Assign(mMessage);
88 12 : return NS_OK;
89 : }
90 :
91 : NS_IMETHODIMP
92 5 : nsScriptError::GetSourceName(nsAString& aResult) {
93 5 : aResult.Assign(mSourceName);
94 5 : return NS_OK;
95 : }
96 :
97 : NS_IMETHODIMP
98 0 : nsScriptError::GetSourceLine(nsAString& aResult) {
99 0 : aResult.Assign(mSourceLine);
100 0 : return NS_OK;
101 : }
102 :
103 : NS_IMETHODIMP
104 32 : nsScriptError::GetLineNumber(PRUint32 *result) {
105 32 : *result = mLineNumber;
106 32 : return NS_OK;
107 : }
108 :
109 : NS_IMETHODIMP
110 2 : nsScriptError::GetColumnNumber(PRUint32 *result) {
111 2 : *result = mColumnNumber;
112 2 : return NS_OK;
113 : }
114 :
115 : NS_IMETHODIMP
116 0 : nsScriptError::GetFlags(PRUint32 *result) {
117 0 : *result = mFlags;
118 0 : return NS_OK;
119 : }
120 :
121 : NS_IMETHODIMP
122 0 : nsScriptError::GetCategory(char **result) {
123 0 : *result = ToNewCString(mCategory);
124 0 : return NS_OK;
125 : }
126 :
127 : NS_IMETHODIMP
128 2833 : nsScriptError::Init(const PRUnichar *message,
129 : const PRUnichar *sourceName,
130 : const PRUnichar *sourceLine,
131 : PRUint32 lineNumber,
132 : PRUint32 columnNumber,
133 : PRUint32 flags,
134 : const char *category)
135 : {
136 : return InitWithWindowID(message, sourceName, sourceLine, lineNumber,
137 2833 : columnNumber, flags, category, 0);
138 : }
139 :
140 : NS_IMETHODIMP
141 3316 : nsScriptError::InitWithWindowID(const PRUnichar *message,
142 : const PRUnichar *sourceName,
143 : const PRUnichar *sourceLine,
144 : PRUint32 lineNumber,
145 : PRUint32 columnNumber,
146 : PRUint32 flags,
147 : const char *category,
148 : PRUint64 aInnerWindowID)
149 : {
150 3316 : mMessage.Assign(message);
151 3316 : mSourceName.Assign(sourceName);
152 3316 : mLineNumber = lineNumber;
153 3316 : mSourceLine.Assign(sourceLine);
154 3316 : mColumnNumber = columnNumber;
155 3316 : mFlags = flags;
156 3316 : mCategory.Assign(category);
157 3316 : mTimeStamp = JS_Now() / 1000;
158 3316 : mInnerWindowID = aInnerWindowID;
159 :
160 3316 : if (aInnerWindowID) {
161 : nsGlobalWindow* window =
162 0 : nsGlobalWindow::GetInnerWindowWithId(aInnerWindowID);
163 0 : if (window) {
164 0 : nsPIDOMWindow* outer = window->GetOuterWindow();
165 0 : if (outer)
166 0 : mOuterWindowID = outer->WindowID();
167 : }
168 : }
169 :
170 3316 : return NS_OK;
171 : }
172 :
173 : NS_IMETHODIMP
174 512 : nsScriptError::ToString(nsACString& /*UTF8*/ aResult)
175 : {
176 : static const char format0[] =
177 : "[%s: \"%s\" {file: \"%s\" line: %d column: %d source: \"%s\"}]";
178 : static const char format1[] =
179 : "[%s: \"%s\" {file: \"%s\" line: %d}]";
180 : static const char format2[] =
181 : "[%s: \"%s\"]";
182 :
183 : static const char error[] = "JavaScript Error";
184 : static const char warning[] = "JavaScript Warning";
185 :
186 512 : const char* severity = !(mFlags & JSREPORT_WARNING) ? error : warning;
187 :
188 : char* temp;
189 512 : char* tempMessage = nsnull;
190 512 : char* tempSourceName = nsnull;
191 512 : char* tempSourceLine = nsnull;
192 :
193 512 : if (!mMessage.IsEmpty())
194 512 : tempMessage = ToNewUTF8String(mMessage);
195 512 : if (!mSourceName.IsEmpty())
196 512 : tempSourceName = ToNewUTF8String(mSourceName);
197 512 : if (!mSourceLine.IsEmpty())
198 0 : tempSourceLine = ToNewUTF8String(mSourceLine);
199 :
200 512 : if (nsnull != tempSourceName && nsnull != tempSourceLine)
201 : temp = JS_smprintf(format0,
202 : severity,
203 : tempMessage,
204 : tempSourceName,
205 : mLineNumber,
206 : mColumnNumber,
207 0 : tempSourceLine);
208 512 : else if (!mSourceName.IsEmpty())
209 : temp = JS_smprintf(format1,
210 : severity,
211 : tempMessage,
212 : tempSourceName,
213 512 : mLineNumber);
214 : else
215 : temp = JS_smprintf(format2,
216 : severity,
217 0 : tempMessage);
218 :
219 512 : if (nsnull != tempMessage)
220 512 : nsMemory::Free(tempMessage);
221 512 : if (nsnull != tempSourceName)
222 512 : nsMemory::Free(tempSourceName);
223 512 : if (nsnull != tempSourceLine)
224 0 : nsMemory::Free(tempSourceLine);
225 :
226 512 : if (!temp)
227 0 : return NS_ERROR_OUT_OF_MEMORY;
228 :
229 512 : aResult.Assign(temp);
230 512 : JS_smprintf_free(temp);
231 512 : return NS_OK;
232 : }
233 :
234 : NS_IMETHODIMP
235 0 : nsScriptError::GetOuterWindowID(PRUint64 *aOuterWindowID)
236 : {
237 0 : *aOuterWindowID = mOuterWindowID;
238 0 : return NS_OK;
239 : }
240 :
241 : NS_IMETHODIMP
242 0 : nsScriptError::GetInnerWindowID(PRUint64 *aInnerWindowID)
243 : {
244 0 : *aInnerWindowID = mInnerWindowID;
245 0 : return NS_OK;
246 : }
247 :
248 : NS_IMETHODIMP
249 0 : nsScriptError::GetTimeStamp(PRInt64 *aTimeStamp)
250 : {
251 0 : *aTimeStamp = mTimeStamp;
252 0 : return NS_OK;
253 : }
|