tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

nsImportModule.cpp (2565B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "nsImportModule.h"
      8 
      9 #include "mozilla/dom/ScriptSettings.h"
     10 #include "mozJSModuleLoader.h"
     11 #include "nsContentUtils.h"
     12 #include "nsExceptionHandler.h"
     13 #include "nsPrintfCString.h"
     14 #include "xpcpublic.h"
     15 #include "xpcprivate.h"
     16 #include "js/PropertyAndElement.h"  // JS_GetProperty
     17 
     18 using mozilla::dom::AutoJSAPI;
     19 
     20 namespace mozilla {
     21 namespace loader {
     22 
     23 static void AnnotateCrashReportWithJSException(JSContext* aCx,
     24                                               const char* aURI) {
     25  JS::RootedValue exn(aCx);
     26  if (JS_GetPendingException(aCx, &exn)) {
     27    JS_ClearPendingException(aCx);
     28 
     29    JSAutoRealm ar(aCx, xpc::PrivilegedJunkScope());
     30    JS_WrapValue(aCx, &exn);
     31 
     32    nsAutoCString file;
     33    uint32_t line;
     34    uint32_t column;
     35    nsAutoString msg;
     36    nsContentUtils::ExtractErrorValues(aCx, exn, file, &line, &column, msg);
     37 
     38    nsPrintfCString errorString("Failed to load module \"%s\": %s:%u:%u: %s",
     39                                aURI, file.get(), line, column,
     40                                NS_ConvertUTF16toUTF8(msg).get());
     41 
     42    CrashReporter::RecordAnnotationNSCString(
     43        CrashReporter::Annotation::JSModuleLoadError, errorString);
     44  }
     45 }
     46 
     47 nsresult ImportESModule(const char* aURI, const char* aExportName,
     48                        const nsIID& aIID, void** aResult, bool aInfallible) {
     49  AutoJSAPI jsapi;
     50  MOZ_ALWAYS_TRUE(jsapi.Init(xpc::PrivilegedJunkScope()));
     51  JSContext* cx = jsapi.cx();
     52 
     53  JS::RootedObject moduleNamespace(cx);
     54  nsresult rv = mozJSModuleLoader::Get()->ImportESModule(
     55      cx, nsDependentCString(aURI), &moduleNamespace);
     56  if (NS_WARN_IF(NS_FAILED(rv))) {
     57    if (aInfallible) {
     58      AnnotateCrashReportWithJSException(cx, aURI);
     59 
     60      MOZ_CRASH_UNSAFE_PRINTF("Failed to load critical module \"%s\"", aURI);
     61    }
     62    return rv;
     63  }
     64 
     65  if (aExportName) {
     66    JS::RootedValue namedExport(cx);
     67    if (!JS_GetProperty(cx, moduleNamespace, aExportName, &namedExport)) {
     68      return NS_ERROR_FAILURE;
     69    }
     70    if (!namedExport.isObject()) {
     71      return NS_ERROR_XPC_BAD_CONVERT_JS;
     72    }
     73    moduleNamespace.set(&namedExport.toObject());
     74  }
     75 
     76  return nsXPConnect::XPConnect()->WrapJS(cx, moduleNamespace, aIID, aResult);
     77 }
     78 
     79 }  // namespace loader
     80 }  // namespace mozilla