layout/style/ErrorReporter.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/layout/style/ErrorReporter.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,356 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/* diagnostic reporting for CSS style sheet parser */
    1.10 +
    1.11 +#include "mozilla/css/ErrorReporter.h"
    1.12 +#include "mozilla/css/Loader.h"
    1.13 +#include "mozilla/Preferences.h"
    1.14 +#include "mozilla/Services.h"
    1.15 +#include "nsCSSScanner.h"
    1.16 +#include "nsCSSStyleSheet.h"
    1.17 +#include "nsIConsoleService.h"
    1.18 +#include "nsIDocument.h"
    1.19 +#include "nsIFactory.h"
    1.20 +#include "nsIScriptError.h"
    1.21 +#include "nsIStringBundle.h"
    1.22 +#include "nsServiceManagerUtils.h"
    1.23 +#include "nsStyleUtil.h"
    1.24 +#include "nsThreadUtils.h"
    1.25 +
    1.26 +#ifdef CSS_REPORT_PARSE_ERRORS
    1.27 +
    1.28 +using mozilla::Preferences;
    1.29 +namespace services = mozilla::services;
    1.30 +
    1.31 +namespace {
    1.32 +class ShortTermURISpecCache : public nsRunnable {
    1.33 +public:
    1.34 +  ShortTermURISpecCache() : mPending(false) {}
    1.35 +
    1.36 +  nsString const& GetSpec(nsIURI* aURI) {
    1.37 +    if (mURI != aURI) {
    1.38 +      mURI = aURI;
    1.39 +
    1.40 +      nsAutoCString cSpec;
    1.41 +      mURI->GetSpec(cSpec);
    1.42 +      CopyUTF8toUTF16(cSpec, mSpec);
    1.43 +    }
    1.44 +    return mSpec;
    1.45 +  }
    1.46 +
    1.47 +  bool IsInUse() const { return mURI != nullptr; }
    1.48 +  bool IsPending() const { return mPending; }
    1.49 +  void SetPending() { mPending = true; }
    1.50 +
    1.51 +  // When invoked as a runnable, zap the cache.
    1.52 +  NS_IMETHOD Run() {
    1.53 +    mURI = nullptr;
    1.54 +    mSpec.Truncate();
    1.55 +    mPending = false;
    1.56 +    return NS_OK;
    1.57 +  }
    1.58 +
    1.59 +private:
    1.60 +  nsCOMPtr<nsIURI> mURI;
    1.61 +  nsString mSpec;
    1.62 +  bool mPending;
    1.63 +};
    1.64 +}
    1.65 +
    1.66 +static bool sReportErrors;
    1.67 +static nsIConsoleService *sConsoleService;
    1.68 +static nsIFactory *sScriptErrorFactory;
    1.69 +static nsIStringBundle *sStringBundle;
    1.70 +static ShortTermURISpecCache *sSpecCache;
    1.71 +
    1.72 +#define CSS_ERRORS_PREF "layout.css.report_errors"
    1.73 +
    1.74 +static bool
    1.75 +InitGlobals()
    1.76 +{
    1.77 +  NS_ABORT_IF_FALSE(!sConsoleService && !sScriptErrorFactory && !sStringBundle,
    1.78 +                    "should not have been called");
    1.79 +
    1.80 +  if (NS_FAILED(Preferences::AddBoolVarCache(&sReportErrors, CSS_ERRORS_PREF,
    1.81 +                                             true))) {
    1.82 +    return false;
    1.83 +  }
    1.84 +
    1.85 +  nsCOMPtr<nsIConsoleService> cs = do_GetService(NS_CONSOLESERVICE_CONTRACTID);
    1.86 +  if (!cs) {
    1.87 +    return false;
    1.88 +  }
    1.89 +
    1.90 +  nsCOMPtr<nsIFactory> sf = do_GetClassObject(NS_SCRIPTERROR_CONTRACTID);
    1.91 +  if (!sf) {
    1.92 +    return false;
    1.93 +  }
    1.94 +
    1.95 +  nsCOMPtr<nsIStringBundleService> sbs = services::GetStringBundleService();
    1.96 +  if (!sbs) {
    1.97 +    return false;
    1.98 +  }
    1.99 +
   1.100 +  nsCOMPtr<nsIStringBundle> sb;
   1.101 +  nsresult rv = sbs->CreateBundle("chrome://global/locale/css.properties",
   1.102 +                                  getter_AddRefs(sb));
   1.103 +  if (NS_FAILED(rv) || !sb) {
   1.104 +    return false;
   1.105 +  }
   1.106 +
   1.107 +  cs.forget(&sConsoleService);
   1.108 +  sf.forget(&sScriptErrorFactory);
   1.109 +  sb.forget(&sStringBundle);
   1.110 +
   1.111 +  return true;
   1.112 +}
   1.113 +
   1.114 +static inline bool
   1.115 +ShouldReportErrors()
   1.116 +{
   1.117 +  if (!sConsoleService) {
   1.118 +    if (!InitGlobals()) {
   1.119 +      return false;
   1.120 +    }
   1.121 +  }
   1.122 +  return sReportErrors;
   1.123 +}
   1.124 +
   1.125 +namespace mozilla {
   1.126 +namespace css {
   1.127 +
   1.128 +/* static */ void
   1.129 +ErrorReporter::ReleaseGlobals()
   1.130 +{
   1.131 +  NS_IF_RELEASE(sConsoleService);
   1.132 +  NS_IF_RELEASE(sScriptErrorFactory);
   1.133 +  NS_IF_RELEASE(sStringBundle);
   1.134 +  NS_IF_RELEASE(sSpecCache);
   1.135 +}
   1.136 +
   1.137 +ErrorReporter::ErrorReporter(const nsCSSScanner& aScanner,
   1.138 +                             const nsCSSStyleSheet* aSheet,
   1.139 +                             const Loader* aLoader,
   1.140 +                             nsIURI* aURI)
   1.141 +  : mScanner(&aScanner), mSheet(aSheet), mLoader(aLoader), mURI(aURI),
   1.142 +    mInnerWindowID(0), mErrorLineNumber(0), mPrevErrorLineNumber(0),
   1.143 +    mErrorColNumber(0)
   1.144 +{
   1.145 +}
   1.146 +
   1.147 +ErrorReporter::~ErrorReporter()
   1.148 +{
   1.149 +  // Schedule deferred cleanup for cached data. We want to strike a
   1.150 +  // balance between performance and memory usage, so we only allow
   1.151 +  // short-term caching.
   1.152 +  if (sSpecCache && sSpecCache->IsInUse() && !sSpecCache->IsPending()) {
   1.153 +    if (NS_FAILED(NS_DispatchToCurrentThread(sSpecCache))) {
   1.154 +      // Peform the "deferred" cleanup immediately if the dispatch fails.
   1.155 +      sSpecCache->Run();
   1.156 +    } else {
   1.157 +      sSpecCache->SetPending();
   1.158 +    }
   1.159 +  }
   1.160 +}
   1.161 +
   1.162 +void
   1.163 +ErrorReporter::OutputError()
   1.164 +{
   1.165 +  if (mError.IsEmpty()) {
   1.166 +    return;
   1.167 +  }
   1.168 +  if (!ShouldReportErrors()) {
   1.169 +    ClearError();
   1.170 +    return;
   1.171 +  }
   1.172 +
   1.173 +  if (mInnerWindowID == 0 && (mSheet || mLoader)) {
   1.174 +    if (mSheet) {
   1.175 +      mInnerWindowID = mSheet->FindOwningWindowInnerID();
   1.176 +    }
   1.177 +    if (mInnerWindowID == 0 && mLoader) {
   1.178 +      nsIDocument* doc = mLoader->GetDocument();
   1.179 +      if (doc) {
   1.180 +        mInnerWindowID = doc->InnerWindowID();
   1.181 +      }
   1.182 +    }
   1.183 +    // don't attempt this again, even if we failed
   1.184 +    mSheet = nullptr;
   1.185 +    mLoader = nullptr;
   1.186 +  }
   1.187 +
   1.188 +  if (mFileName.IsEmpty()) {
   1.189 +    if (mURI) {
   1.190 +      if (!sSpecCache) {
   1.191 +        sSpecCache = new ShortTermURISpecCache;
   1.192 +        NS_ADDREF(sSpecCache);
   1.193 +      }
   1.194 +      mFileName = sSpecCache->GetSpec(mURI);
   1.195 +      mURI = nullptr;
   1.196 +    } else {
   1.197 +      mFileName.AssignLiteral("from DOM");
   1.198 +    }
   1.199 +  }
   1.200 +
   1.201 +  nsresult rv;
   1.202 +  nsCOMPtr<nsIScriptError> errorObject =
   1.203 +    do_CreateInstance(sScriptErrorFactory, &rv);
   1.204 +
   1.205 +  if (NS_SUCCEEDED(rv)) {
   1.206 +    rv = errorObject->InitWithWindowID(mError,
   1.207 +                                       mFileName,
   1.208 +                                       mErrorLine,
   1.209 +                                       mErrorLineNumber,
   1.210 +                                       mErrorColNumber,
   1.211 +                                       nsIScriptError::warningFlag,
   1.212 +                                       "CSS Parser",
   1.213 +                                       mInnerWindowID);
   1.214 +    if (NS_SUCCEEDED(rv)) {
   1.215 +      sConsoleService->LogMessage(errorObject);
   1.216 +    }
   1.217 +  }
   1.218 +
   1.219 +  ClearError();
   1.220 +}
   1.221 +
   1.222 +void
   1.223 +ErrorReporter::OutputError(uint32_t aLineNumber, uint32_t aLineOffset)
   1.224 +{
   1.225 +  mErrorLineNumber = aLineNumber;
   1.226 +  mErrorColNumber = aLineOffset;
   1.227 +  OutputError();
   1.228 +}
   1.229 +
   1.230 +void
   1.231 +ErrorReporter::ClearError()
   1.232 +{
   1.233 +  mError.Truncate();
   1.234 +}
   1.235 +
   1.236 +void
   1.237 +ErrorReporter::AddToError(const nsString &aErrorText)
   1.238 +{
   1.239 +  if (!ShouldReportErrors()) return;
   1.240 +
   1.241 +  if (mError.IsEmpty()) {
   1.242 +    mError = aErrorText;
   1.243 +    mErrorLineNumber = mScanner->GetLineNumber();
   1.244 +    mErrorColNumber = mScanner->GetColumnNumber();
   1.245 +    // Retrieve the error line once per line, and reuse the same nsString
   1.246 +    // for all errors on that line.  That causes the text of the line to
   1.247 +    // be shared among all the nsIScriptError objects.
   1.248 +    if (mErrorLine.IsEmpty() || mErrorLineNumber != mPrevErrorLineNumber) {
   1.249 +      mErrorLine = mScanner->GetCurrentLine();
   1.250 +      mPrevErrorLineNumber = mErrorLineNumber;
   1.251 +    }
   1.252 +  } else {
   1.253 +    mError.AppendLiteral("  ");
   1.254 +    mError.Append(aErrorText);
   1.255 +  }
   1.256 +}
   1.257 +
   1.258 +void
   1.259 +ErrorReporter::ReportUnexpected(const char *aMessage)
   1.260 +{
   1.261 +  if (!ShouldReportErrors()) return;
   1.262 +
   1.263 +  nsAutoString str;
   1.264 +  sStringBundle->GetStringFromName(NS_ConvertASCIItoUTF16(aMessage).get(),
   1.265 +                                   getter_Copies(str));
   1.266 +  AddToError(str);
   1.267 +}
   1.268 +
   1.269 +void
   1.270 +ErrorReporter::ReportUnexpected(const char *aMessage,
   1.271 +                                const nsString &aParam)
   1.272 +{
   1.273 +  if (!ShouldReportErrors()) return;
   1.274 +
   1.275 +  nsAutoString qparam;
   1.276 +  nsStyleUtil::AppendEscapedCSSIdent(aParam, qparam);
   1.277 +  const char16_t *params[1] = { qparam.get() };
   1.278 +
   1.279 +  nsAutoString str;
   1.280 +  sStringBundle->FormatStringFromName(NS_ConvertASCIItoUTF16(aMessage).get(),
   1.281 +                                      params, ArrayLength(params),
   1.282 +                                      getter_Copies(str));
   1.283 +  AddToError(str);
   1.284 +}
   1.285 +
   1.286 +void
   1.287 +ErrorReporter::ReportUnexpected(const char *aMessage,
   1.288 +                                const nsCSSToken &aToken)
   1.289 +{
   1.290 +  if (!ShouldReportErrors()) return;
   1.291 +
   1.292 +  nsAutoString tokenString;
   1.293 +  aToken.AppendToString(tokenString);
   1.294 +  const char16_t *params[1] = { tokenString.get() };
   1.295 +
   1.296 +  nsAutoString str;
   1.297 +  sStringBundle->FormatStringFromName(NS_ConvertASCIItoUTF16(aMessage).get(),
   1.298 +                                      params, ArrayLength(params),
   1.299 +                                      getter_Copies(str));
   1.300 +  AddToError(str);
   1.301 +}
   1.302 +
   1.303 +void
   1.304 +ErrorReporter::ReportUnexpected(const char *aMessage,
   1.305 +                                const nsCSSToken &aToken,
   1.306 +                                char16_t aChar)
   1.307 +{
   1.308 +  if (!ShouldReportErrors()) return;
   1.309 +
   1.310 +  nsAutoString tokenString;
   1.311 +  aToken.AppendToString(tokenString);
   1.312 +  const char16_t charStr[2] = { aChar, 0 };
   1.313 +  const char16_t *params[2] = { tokenString.get(), charStr };
   1.314 +
   1.315 +  nsAutoString str;
   1.316 +  sStringBundle->FormatStringFromName(NS_ConvertASCIItoUTF16(aMessage).get(),
   1.317 +                                      params, ArrayLength(params),
   1.318 +                                      getter_Copies(str));
   1.319 +  AddToError(str);
   1.320 +}
   1.321 +
   1.322 +void
   1.323 +ErrorReporter::ReportUnexpectedEOF(const char *aMessage)
   1.324 +{
   1.325 +  if (!ShouldReportErrors()) return;
   1.326 +
   1.327 +  nsAutoString innerStr;
   1.328 +  sStringBundle->GetStringFromName(NS_ConvertASCIItoUTF16(aMessage).get(),
   1.329 +                                   getter_Copies(innerStr));
   1.330 +  const char16_t *params[1] = { innerStr.get() };
   1.331 +
   1.332 +  nsAutoString str;
   1.333 +  sStringBundle->FormatStringFromName(MOZ_UTF16("PEUnexpEOF2"),
   1.334 +                                      params, ArrayLength(params),
   1.335 +                                      getter_Copies(str));
   1.336 +  AddToError(str);
   1.337 +}
   1.338 +
   1.339 +void
   1.340 +ErrorReporter::ReportUnexpectedEOF(char16_t aExpected)
   1.341 +{
   1.342 +  if (!ShouldReportErrors()) return;
   1.343 +
   1.344 +  const char16_t expectedStr[] = {
   1.345 +    char16_t('\''), aExpected, char16_t('\''), char16_t(0)
   1.346 +  };
   1.347 +  const char16_t *params[1] = { expectedStr };
   1.348 +
   1.349 +  nsAutoString str;
   1.350 +  sStringBundle->FormatStringFromName(MOZ_UTF16("PEUnexpEOF2"),
   1.351 +                                      params, ArrayLength(params),
   1.352 +                                      getter_Copies(str));
   1.353 +  AddToError(str);
   1.354 +}
   1.355 +
   1.356 +} // namespace css
   1.357 +} // namespace mozilla
   1.358 +
   1.359 +#endif

mercurial