DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (a5710687f9b4)

VCS Links

Line Code
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 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
 * nsIScriptError implementation.
 */

#include "nsScriptError.h"
#include "js/Printf.h"
#include "MainThreadUtils.h"
#include "mozilla/Assertions.h"
#include "nsContentUtils.h"
#include "nsGlobalWindow.h"
#include "nsNetUtil.h"
#include "nsPIDOMWindow.h"
#include "nsILoadContext.h"
#include "nsIDocShell.h"
#include "nsIMutableArray.h"
#include "nsIScriptError.h"
#include "nsISensitiveInfoHiddenURI.h"

static_assert(nsIScriptError::errorFlag == JSREPORT_ERROR &&
                  nsIScriptError::warningFlag == JSREPORT_WARNING &&
                  nsIScriptError::exceptionFlag == JSREPORT_EXCEPTION &&
                  nsIScriptError::strictFlag == JSREPORT_STRICT &&
                  nsIScriptError::infoFlag == JSREPORT_USER_1,
              "flags should be consistent");

nsScriptErrorBase::nsScriptErrorBase()
    : mMessage(),
      mMessageName(),
      mSourceName(),
      mCssSelectors(),
      mSourceId(0),
      mLineNumber(0),
      mSourceLine(),
      mColumnNumber(0),
      mFlags(0),
      mCategory(),
      mOuterWindowID(0),
      mInnerWindowID(0),
      mTimeStamp(0),
      mTimeWarpTarget(0),
      mInitializedOnMainThread(false),
      mIsFromPrivateWindow(false),
      mIsFromChromeContext(false) {}

nsScriptErrorBase::~nsScriptErrorBase() {}

void nsScriptErrorBase::AddNote(nsIScriptErrorNote* note) {
  mNotes.AppendObject(note);
}

void nsScriptErrorBase::InitializeOnMainThread() {
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(!mInitializedOnMainThread);

  if (mInnerWindowID) {
    nsGlobalWindowInner* window =
        nsGlobalWindowInner::GetInnerWindowWithId(mInnerWindowID);
    if (window) {
      nsPIDOMWindowOuter* outer = window->GetOuterWindow();
      if (outer) mOuterWindowID = outer->WindowID();
      mIsFromChromeContext = ComputeIsFromChromeContext(window);
      mIsFromPrivateWindow = ComputeIsFromPrivateWindow(window);
    }
  }

  mInitializedOnMainThread = true;
}

NS_IMETHODIMP
nsScriptErrorBase::InitSourceId(uint32_t value) {
  mSourceId = value;
  return NS_OK;
}

// nsIConsoleMessage methods
NS_IMETHODIMP
nsScriptErrorBase::GetMessageMoz(nsAString& aMessage) {
  nsAutoCString message;
  nsresult rv = ToString(message);
  if (NS_FAILED(rv)) {
    return rv;
  }

  CopyUTF8toUTF16(message, aMessage);
  return NS_OK;
}

NS_IMETHODIMP
nsScriptErrorBase::GetLogLevel(uint32_t* aLogLevel) {
  if (mFlags & (uint32_t)nsIScriptError::infoFlag) {
    *aLogLevel = nsIConsoleMessage::info;
  } else if (mFlags & (uint32_t)nsIScriptError::warningFlag) {
    *aLogLevel = nsIConsoleMessage::warn;
  } else {
    *aLogLevel = nsIConsoleMessage::error;
  }
  return NS_OK;
}

// nsIScriptError methods
NS_IMETHODIMP
nsScriptErrorBase::GetErrorMessage(nsAString& aResult) {
  aResult.Assign(mMessage);
  return NS_OK;
}

NS_IMETHODIMP
nsScriptErrorBase::GetSourceName(nsAString& aResult) {
  aResult.Assign(mSourceName);
  return NS_OK;
}

NS_IMETHODIMP
nsScriptErrorBase::GetCssSelectors(nsAString& aResult) {
  aResult.Assign(mCssSelectors);
  return NS_OK;
}

NS_IMETHODIMP
nsScriptErrorBase::SetCssSelectors(const nsAString& aCssSelectors) {
  mCssSelectors = aCssSelectors;
  return NS_OK;
}

NS_IMETHODIMP
nsScriptErrorBase::GetSourceId(uint32_t* result) {
  *result = mSourceId;
  return NS_OK;
}

NS_IMETHODIMP
nsScriptErrorBase::GetSourceLine(nsAString& aResult) {
  aResult.Assign(mSourceLine);
  return NS_OK;
}

NS_IMETHODIMP
nsScriptErrorBase::GetLineNumber(uint32_t* result) {
  *result = mLineNumber;
  return NS_OK;
}

NS_IMETHODIMP
nsScriptErrorBase::GetColumnNumber(uint32_t* result) {
  *result = mColumnNumber;
  return NS_OK;
}

NS_IMETHODIMP
nsScriptErrorBase::GetFlags(uint32_t* result) {
  *result = mFlags;
  return NS_OK;
}

NS_IMETHODIMP
nsScriptErrorBase::GetCategory(char** result) {
  *result = ToNewCString(mCategory);
  return NS_OK;
}

NS_IMETHODIMP
nsScriptErrorBase::GetStack(JS::MutableHandleValue aStack) {
  aStack.setUndefined();
  return NS_OK;
}

NS_IMETHODIMP
nsScriptErrorBase::SetStack(JS::HandleValue aStack) { return NS_OK; }

NS_IMETHODIMP
nsScriptErrorBase::GetStackGlobal(JS::MutableHandleValue aStackGlobal) {
  aStackGlobal.setUndefined();
  return NS_OK;
}

NS_IMETHODIMP
nsScriptErrorBase::GetErrorMessageName(nsAString& aErrorMessageName) {
  aErrorMessageName = mMessageName;
  return NS_OK;
}

NS_IMETHODIMP
nsScriptErrorBase::SetErrorMessageName(const nsAString& aErrorMessageName) {
  mMessageName = aErrorMessageName;
  return NS_OK;
}

static void AssignSourceNameHelper(nsString& aSourceNameDest,
                                   const nsAString& aSourceNameSrc) {
  if (aSourceNameSrc.IsEmpty()) return;

  aSourceNameDest.Assign(aSourceNameSrc);

  nsCOMPtr<nsIURI> uri;
  nsAutoCString pass;
  if (NS_SUCCEEDED(NS_NewURI(getter_AddRefs(uri), aSourceNameSrc)) &&
      NS_SUCCEEDED(uri->GetPassword(pass)) && !pass.IsEmpty()) {
    NS_GetSanitizedURIStringFromURI(uri, aSourceNameDest);
  }
}

static void AssignSourceNameHelper(nsIURI* aSourceURI,
                                   nsString& aSourceNameDest) {
  if (!aSourceURI) return;

  if (NS_FAILED(NS_GetSanitizedURIStringFromURI(aSourceURI, aSourceNameDest))) {
    aSourceNameDest.AssignLiteral("[nsIURI::GetSpec failed]");
  }
}

NS_IMETHODIMP
nsScriptErrorBase::Init(const nsAString& message, const nsAString& sourceName,
                        const nsAString& sourceLine, uint32_t lineNumber,
                        uint32_t columnNumber, uint32_t flags,
                        const char* category, bool fromPrivateWindow,
                        bool fromChromeContext) {
  InitializationHelper(message, sourceLine, lineNumber, columnNumber, flags,
                       category ? nsDependentCString(category) : EmptyCString(),
                       0 /* inner Window ID */, fromChromeContext);
  AssignSourceNameHelper(mSourceName, sourceName);

  mIsFromPrivateWindow = fromPrivateWindow;
  mIsFromChromeContext = fromChromeContext;
  return NS_OK;
}

void nsScriptErrorBase::InitializationHelper(
    const nsAString& message, const nsAString& sourceLine, uint32_t lineNumber,
    uint32_t columnNumber, uint32_t flags, const nsACString& category,
    uint64_t aInnerWindowID, bool aFromChromeContext) {
  mMessage.Assign(message);
  mLineNumber = lineNumber;
  mSourceLine.Assign(sourceLine);
  mColumnNumber = columnNumber;
  mFlags = flags;
  mCategory = category;
  mTimeStamp = JS_Now() / 1000;
  mInnerWindowID = aInnerWindowID;
  mIsFromChromeContext = aFromChromeContext;
}

NS_IMETHODIMP
nsScriptErrorBase::InitWithWindowID(const nsAString& message,
                                    const nsAString& sourceName,
                                    const nsAString& sourceLine,
                                    uint32_t lineNumber, uint32_t columnNumber,
                                    uint32_t flags, const nsACString& category,
                                    uint64_t aInnerWindowID,
                                    bool aFromChromeContext) {
  InitializationHelper(message, sourceLine, lineNumber, columnNumber, flags,
                       category, aInnerWindowID, aFromChromeContext);
  AssignSourceNameHelper(mSourceName, sourceName);

  if (aInnerWindowID && NS_IsMainThread()) InitializeOnMainThread();

  return NS_OK;
}

NS_IMETHODIMP
nsScriptErrorBase::InitWithSanitizedSource(
    const nsAString& message, const nsAString& sourceName,
    const nsAString& sourceLine, uint32_t lineNumber, uint32_t columnNumber,
    uint32_t flags, const nsACString& category, uint64_t aInnerWindowID,
    bool aFromChromeContext) {
  InitializationHelper(message, sourceLine, lineNumber, columnNumber, flags,
                       category, aInnerWindowID, aFromChromeContext);
  mSourceName = sourceName;

  if (aInnerWindowID && NS_IsMainThread()) InitializeOnMainThread();

  return NS_OK;
}

NS_IMETHODIMP
nsScriptErrorBase::InitWithSourceURI(const nsAString& message,
                                     nsIURI* sourceURI,
                                     const nsAString& sourceLine,
                                     uint32_t lineNumber, uint32_t columnNumber,
                                     uint32_t flags, const nsACString& category,
                                     uint64_t aInnerWindowID,
                                     bool aFromChromeContext) {
  InitializationHelper(message, sourceLine, lineNumber, columnNumber, flags,
                       category, aInnerWindowID, aFromChromeContext);
  AssignSourceNameHelper(sourceURI, mSourceName);

  if (aInnerWindowID && NS_IsMainThread()) InitializeOnMainThread();

  return NS_OK;
}

static nsresult ToStringHelper(const char* aSeverity, const nsString& aMessage,
                               const nsString& aSourceName,
                               const nsString* aSourceLine,
                               uint32_t aLineNumber, uint32_t aColumnNumber,
                               nsACString& /*UTF8*/ aResult) {
  static const char format0[] =
      "[%s: \"%s\" {file: \"%s\" line: %d column: %d source: \"%s\"}]";
  static const char format1[] = "[%s: \"%s\" {file: \"%s\" line: %d}]";
  static const char format2[] = "[%s: \"%s\"]";

  JS::UniqueChars temp;
  char* tempMessage = nullptr;
  char* tempSourceName = nullptr;
  char* tempSourceLine = nullptr;

  if (!aMessage.IsEmpty()) tempMessage = ToNewUTF8String(aMessage);
  if (!aSourceName.IsEmpty())
    // Use at most 512 characters from mSourceName.
    tempSourceName = ToNewUTF8String(StringHead(aSourceName, 512));
  if (aSourceLine && !aSourceLine->IsEmpty())
    // Use at most 512 characters from mSourceLine.
    tempSourceLine = ToNewUTF8String(StringHead(*aSourceLine, 512));

  if (nullptr != tempSourceName && nullptr != tempSourceLine) {
    temp = JS_smprintf(format0, aSeverity, tempMessage, tempSourceName,
                       aLineNumber, aColumnNumber, tempSourceLine);
  } else if (!aSourceName.IsEmpty()) {
    temp = JS_smprintf(format1, aSeverity, tempMessage, tempSourceName,
                       aLineNumber);
  } else {
    temp = JS_smprintf(format2, aSeverity, tempMessage);
  }

  if (nullptr != tempMessage) free(tempMessage);
  if (nullptr != tempSourceName) free(tempSourceName);
  if (nullptr != tempSourceLine) free(tempSourceLine);

  if (!temp) return NS_ERROR_OUT_OF_MEMORY;

  aResult.Assign(temp.get());
  return NS_OK;
}

NS_IMETHODIMP
nsScriptErrorBase::ToString(nsACString& /*UTF8*/ aResult) {
  static const char error[] = "JavaScript Error";
  static const char warning[] = "JavaScript Warning";

  const char* severity = !(mFlags & JSREPORT_WARNING) ? error : warning;

  return ToStringHelper(severity, mMessage, mSourceName, &mSourceLine,
                        mLineNumber, mColumnNumber, aResult);
}

NS_IMETHODIMP
nsScriptErrorBase::GetOuterWindowID(uint64_t* aOuterWindowID) {
  NS_WARNING_ASSERTION(NS_IsMainThread() || mInitializedOnMainThread,
                       "This can't be safely determined off the main thread, "
                       "returning an inaccurate value!");

  if (!mInitializedOnMainThread && NS_IsMainThread()) {
    InitializeOnMainThread();
  }

  *aOuterWindowID = mOuterWindowID;
  return NS_OK;
}

NS_IMETHODIMP
nsScriptErrorBase::GetInnerWindowID(uint64_t* aInnerWindowID) {
  *aInnerWindowID = mInnerWindowID;
  return NS_OK;
}

NS_IMETHODIMP
nsScriptErrorBase::GetTimeStamp(int64_t* aTimeStamp) {
  *aTimeStamp = mTimeStamp;
  return NS_OK;
}

NS_IMETHODIMP
nsScriptErrorBase::GetIsFromPrivateWindow(bool* aIsFromPrivateWindow) {
  NS_WARNING_ASSERTION(NS_IsMainThread() || mInitializedOnMainThread,
                       "This can't be safely determined off the main thread, "
                       "returning an inaccurate value!");

  if (!mInitializedOnMainThread && NS_IsMainThread()) {
    InitializeOnMainThread();
  }

  *aIsFromPrivateWindow = mIsFromPrivateWindow;
  return NS_OK;
}

NS_IMETHODIMP
nsScriptErrorBase::SetTimeWarpTarget(uint64_t aTarget) {
  mTimeWarpTarget = aTarget;
  return NS_OK;
}

NS_IMETHODIMP
nsScriptErrorBase::GetTimeWarpTarget(uint64_t* aTarget) {
  *aTarget = mTimeWarpTarget;
  return NS_OK;
}

NS_IMETHODIMP
nsScriptErrorBase::GetIsFromChromeContext(bool* aIsFromChromeContext) {
  NS_WARNING_ASSERTION(NS_IsMainThread() || mInitializedOnMainThread,
                       "This can't be safely determined off the main thread, "
                       "returning an inaccurate value!");
  if (!mInitializedOnMainThread && NS_IsMainThread()) {
    InitializeOnMainThread();
  }
  *aIsFromChromeContext = mIsFromChromeContext;
  return NS_OK;
}

NS_IMETHODIMP
nsScriptErrorBase::GetNotes(nsIArray** aNotes) {
  nsresult rv = NS_OK;
  nsCOMPtr<nsIMutableArray> array = do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
  NS_ENSURE_SUCCESS(rv, rv);

  uint32_t len = mNotes.Length();
  for (uint32_t i = 0; i < len; i++) array->AppendElement(mNotes[i]);
  array.forget(aNotes);

  return NS_OK;
}

/* static */
bool nsScriptErrorBase::ComputeIsFromPrivateWindow(
    nsGlobalWindowInner* aWindow) {
  // Never mark exceptions from chrome windows as having come from private
  // windows, since we always want them to be reported.
  nsIPrincipal* winPrincipal = aWindow->GetPrincipal();
  return aWindow->IsPrivateBrowsing() &&
         !nsContentUtils::IsSystemPrincipal(winPrincipal);
}

/* static */
bool nsScriptErrorBase::ComputeIsFromChromeContext(
    nsGlobalWindowInner* aWindow) {
  nsIPrincipal* winPrincipal = aWindow->GetPrincipal();
  return nsContentUtils::IsSystemPrincipal(winPrincipal);
}

NS_IMPL_ISUPPORTS(nsScriptError, nsIConsoleMessage, nsIScriptError)

nsScriptErrorNote::nsScriptErrorNote()
    : mMessage(),
      mSourceName(),
      mSourceId(0),
      mLineNumber(0),
      mColumnNumber(0) {}

nsScriptErrorNote::~nsScriptErrorNote() {}

void nsScriptErrorNote::Init(const nsAString& message,
                             const nsAString& sourceName, uint32_t sourceId,
                             uint32_t lineNumber, uint32_t columnNumber) {
  mMessage.Assign(message);
  AssignSourceNameHelper(mSourceName, sourceName);
  mSourceId = sourceId;
  mLineNumber = lineNumber;
  mColumnNumber = columnNumber;
}

// nsIScriptErrorNote methods
NS_IMETHODIMP
nsScriptErrorNote::GetErrorMessage(nsAString& aResult) {
  aResult.Assign(mMessage);
  return NS_OK;
}

NS_IMETHODIMP
nsScriptErrorNote::GetSourceName(nsAString& aResult) {
  aResult.Assign(mSourceName);
  return NS_OK;
}

NS_IMETHODIMP
nsScriptErrorNote::GetSourceId(uint32_t* result) {
  *result = mSourceId;
  return NS_OK;
}

NS_IMETHODIMP
nsScriptErrorNote::GetLineNumber(uint32_t* result) {
  *result = mLineNumber;
  return NS_OK;
}

NS_IMETHODIMP
nsScriptErrorNote::GetColumnNumber(uint32_t* result) {
  *result = mColumnNumber;
  return NS_OK;
}

NS_IMETHODIMP
nsScriptErrorNote::ToString(nsACString& /*UTF8*/ aResult) {
  return ToStringHelper("JavaScript Note", mMessage, mSourceName, nullptr,
                        mLineNumber, mColumnNumber, aResult);
}

NS_IMPL_ISUPPORTS(nsScriptErrorNote, nsIScriptErrorNote)