js/xpconnect/src/nsScriptError.cpp

Thu, 15 Jan 2015 15:55:04 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:55:04 +0100
branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
permissions
-rw-r--r--

Back out 97036ab72558 which inappropriately compared turds to third parties.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* vim: set ts=8 sts=4 et sw=4 tw=99: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 /*
michael@0 8 * nsIScriptError implementation. Defined here, lacking a JS-specific
michael@0 9 * place to put XPCOM things.
michael@0 10 */
michael@0 11
michael@0 12 #include "xpcprivate.h"
michael@0 13 #include "jsprf.h"
michael@0 14 #include "nsGlobalWindow.h"
michael@0 15 #include "nsPIDOMWindow.h"
michael@0 16 #include "nsILoadContext.h"
michael@0 17 #include "nsIDocShell.h"
michael@0 18
michael@0 19 NS_IMPL_ISUPPORTS(nsScriptError, nsIConsoleMessage, nsIScriptError)
michael@0 20
michael@0 21 nsScriptError::nsScriptError()
michael@0 22 : mMessage(),
michael@0 23 mSourceName(),
michael@0 24 mLineNumber(0),
michael@0 25 mSourceLine(),
michael@0 26 mColumnNumber(0),
michael@0 27 mFlags(0),
michael@0 28 mCategory(),
michael@0 29 mOuterWindowID(0),
michael@0 30 mInnerWindowID(0),
michael@0 31 mTimeStamp(0),
michael@0 32 mIsFromPrivateWindow(false)
michael@0 33 {
michael@0 34 }
michael@0 35
michael@0 36 nsScriptError::~nsScriptError() {}
michael@0 37
michael@0 38 // nsIConsoleMessage methods
michael@0 39 NS_IMETHODIMP
michael@0 40 nsScriptError::GetMessageMoz(char16_t **result) {
michael@0 41 nsresult rv;
michael@0 42
michael@0 43 nsAutoCString message;
michael@0 44 rv = ToString(message);
michael@0 45 if (NS_FAILED(rv))
michael@0 46 return rv;
michael@0 47
michael@0 48 *result = UTF8ToNewUnicode(message);
michael@0 49 if (!*result)
michael@0 50 return NS_ERROR_OUT_OF_MEMORY;
michael@0 51
michael@0 52 return NS_OK;
michael@0 53 }
michael@0 54
michael@0 55 // nsIScriptError methods
michael@0 56 NS_IMETHODIMP
michael@0 57 nsScriptError::GetErrorMessage(nsAString& aResult) {
michael@0 58 aResult.Assign(mMessage);
michael@0 59 return NS_OK;
michael@0 60 }
michael@0 61
michael@0 62 NS_IMETHODIMP
michael@0 63 nsScriptError::GetSourceName(nsAString& aResult) {
michael@0 64 aResult.Assign(mSourceName);
michael@0 65 return NS_OK;
michael@0 66 }
michael@0 67
michael@0 68 NS_IMETHODIMP
michael@0 69 nsScriptError::GetSourceLine(nsAString& aResult) {
michael@0 70 aResult.Assign(mSourceLine);
michael@0 71 return NS_OK;
michael@0 72 }
michael@0 73
michael@0 74 NS_IMETHODIMP
michael@0 75 nsScriptError::GetLineNumber(uint32_t *result) {
michael@0 76 *result = mLineNumber;
michael@0 77 return NS_OK;
michael@0 78 }
michael@0 79
michael@0 80 NS_IMETHODIMP
michael@0 81 nsScriptError::GetColumnNumber(uint32_t *result) {
michael@0 82 *result = mColumnNumber;
michael@0 83 return NS_OK;
michael@0 84 }
michael@0 85
michael@0 86 NS_IMETHODIMP
michael@0 87 nsScriptError::GetFlags(uint32_t *result) {
michael@0 88 *result = mFlags;
michael@0 89 return NS_OK;
michael@0 90 }
michael@0 91
michael@0 92 NS_IMETHODIMP
michael@0 93 nsScriptError::GetCategory(char **result) {
michael@0 94 *result = ToNewCString(mCategory);
michael@0 95 return NS_OK;
michael@0 96 }
michael@0 97
michael@0 98 NS_IMETHODIMP
michael@0 99 nsScriptError::Init(const nsAString& message,
michael@0 100 const nsAString& sourceName,
michael@0 101 const nsAString& sourceLine,
michael@0 102 uint32_t lineNumber,
michael@0 103 uint32_t columnNumber,
michael@0 104 uint32_t flags,
michael@0 105 const char *category)
michael@0 106 {
michael@0 107 return InitWithWindowID(message, sourceName, sourceLine, lineNumber,
michael@0 108 columnNumber, flags,
michael@0 109 category ? nsDependentCString(category)
michael@0 110 : EmptyCString(),
michael@0 111 0);
michael@0 112 }
michael@0 113
michael@0 114 NS_IMETHODIMP
michael@0 115 nsScriptError::InitWithWindowID(const nsAString& message,
michael@0 116 const nsAString& sourceName,
michael@0 117 const nsAString& sourceLine,
michael@0 118 uint32_t lineNumber,
michael@0 119 uint32_t columnNumber,
michael@0 120 uint32_t flags,
michael@0 121 const nsACString& category,
michael@0 122 uint64_t aInnerWindowID)
michael@0 123 {
michael@0 124 mMessage.Assign(message);
michael@0 125 mSourceName.Assign(sourceName);
michael@0 126 mLineNumber = lineNumber;
michael@0 127 mSourceLine.Assign(sourceLine);
michael@0 128 mColumnNumber = columnNumber;
michael@0 129 mFlags = flags;
michael@0 130 mCategory = category;
michael@0 131 mTimeStamp = JS_Now() / 1000;
michael@0 132 mInnerWindowID = aInnerWindowID;
michael@0 133
michael@0 134 if (aInnerWindowID) {
michael@0 135 nsGlobalWindow* window =
michael@0 136 nsGlobalWindow::GetInnerWindowWithId(aInnerWindowID);
michael@0 137 if (window) {
michael@0 138 nsPIDOMWindow* outer = window->GetOuterWindow();
michael@0 139 if (outer)
michael@0 140 mOuterWindowID = outer->WindowID();
michael@0 141
michael@0 142 nsIDocShell* docShell = window->GetDocShell();
michael@0 143 nsCOMPtr<nsILoadContext> loadContext = do_QueryInterface(docShell);
michael@0 144
michael@0 145 if (loadContext) {
michael@0 146 // Never mark exceptions from chrome windows as having come from
michael@0 147 // private windows, since we always want them to be reported.
michael@0 148 nsIPrincipal* winPrincipal = window->GetPrincipal();
michael@0 149 mIsFromPrivateWindow = loadContext->UsePrivateBrowsing() &&
michael@0 150 !nsContentUtils::IsSystemPrincipal(winPrincipal);
michael@0 151 }
michael@0 152
michael@0 153 }
michael@0 154 }
michael@0 155
michael@0 156 return NS_OK;
michael@0 157 }
michael@0 158
michael@0 159 NS_IMETHODIMP
michael@0 160 nsScriptError::ToString(nsACString& /*UTF8*/ aResult)
michael@0 161 {
michael@0 162 static const char format0[] =
michael@0 163 "[%s: \"%s\" {file: \"%s\" line: %d column: %d source: \"%s\"}]";
michael@0 164 static const char format1[] =
michael@0 165 "[%s: \"%s\" {file: \"%s\" line: %d}]";
michael@0 166 static const char format2[] =
michael@0 167 "[%s: \"%s\"]";
michael@0 168
michael@0 169 static const char error[] = "JavaScript Error";
michael@0 170 static const char warning[] = "JavaScript Warning";
michael@0 171
michael@0 172 const char* severity = !(mFlags & JSREPORT_WARNING) ? error : warning;
michael@0 173
michael@0 174 char* temp;
michael@0 175 char* tempMessage = nullptr;
michael@0 176 char* tempSourceName = nullptr;
michael@0 177 char* tempSourceLine = nullptr;
michael@0 178
michael@0 179 if (!mMessage.IsEmpty())
michael@0 180 tempMessage = ToNewUTF8String(mMessage);
michael@0 181 if (!mSourceName.IsEmpty())
michael@0 182 tempSourceName = ToNewUTF8String(mSourceName);
michael@0 183 if (!mSourceLine.IsEmpty())
michael@0 184 tempSourceLine = ToNewUTF8String(mSourceLine);
michael@0 185
michael@0 186 if (nullptr != tempSourceName && nullptr != tempSourceLine)
michael@0 187 temp = JS_smprintf(format0,
michael@0 188 severity,
michael@0 189 tempMessage,
michael@0 190 tempSourceName,
michael@0 191 mLineNumber,
michael@0 192 mColumnNumber,
michael@0 193 tempSourceLine);
michael@0 194 else if (!mSourceName.IsEmpty())
michael@0 195 temp = JS_smprintf(format1,
michael@0 196 severity,
michael@0 197 tempMessage,
michael@0 198 tempSourceName,
michael@0 199 mLineNumber);
michael@0 200 else
michael@0 201 temp = JS_smprintf(format2,
michael@0 202 severity,
michael@0 203 tempMessage);
michael@0 204
michael@0 205 if (nullptr != tempMessage)
michael@0 206 nsMemory::Free(tempMessage);
michael@0 207 if (nullptr != tempSourceName)
michael@0 208 nsMemory::Free(tempSourceName);
michael@0 209 if (nullptr != tempSourceLine)
michael@0 210 nsMemory::Free(tempSourceLine);
michael@0 211
michael@0 212 if (!temp)
michael@0 213 return NS_ERROR_OUT_OF_MEMORY;
michael@0 214
michael@0 215 aResult.Assign(temp);
michael@0 216 JS_smprintf_free(temp);
michael@0 217 return NS_OK;
michael@0 218 }
michael@0 219
michael@0 220 NS_IMETHODIMP
michael@0 221 nsScriptError::GetOuterWindowID(uint64_t *aOuterWindowID)
michael@0 222 {
michael@0 223 *aOuterWindowID = mOuterWindowID;
michael@0 224 return NS_OK;
michael@0 225 }
michael@0 226
michael@0 227 NS_IMETHODIMP
michael@0 228 nsScriptError::GetInnerWindowID(uint64_t *aInnerWindowID)
michael@0 229 {
michael@0 230 *aInnerWindowID = mInnerWindowID;
michael@0 231 return NS_OK;
michael@0 232 }
michael@0 233
michael@0 234 NS_IMETHODIMP
michael@0 235 nsScriptError::GetTimeStamp(int64_t *aTimeStamp)
michael@0 236 {
michael@0 237 *aTimeStamp = mTimeStamp;
michael@0 238 return NS_OK;
michael@0 239 }
michael@0 240
michael@0 241 NS_IMETHODIMP
michael@0 242 nsScriptError::GetIsFromPrivateWindow(bool *aIsFromPrivateWindow)
michael@0 243 {
michael@0 244 *aIsFromPrivateWindow = mIsFromPrivateWindow;
michael@0 245 return NS_OK;
michael@0 246 }

mercurial