tor-browser

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

ImportMap.h (4468B)


      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 #ifndef js_loader_ImportMap_h
      8 #define js_loader_ImportMap_h
      9 
     10 #include <functional>
     11 #include <map>
     12 
     13 #include "js/SourceText.h"
     14 #include "mozilla/Logging.h"
     15 #include "mozilla/RefPtr.h"
     16 #include "mozilla/UniquePtr.h"
     17 #include "nsStringFwd.h"
     18 #include "nsTArray.h"
     19 #include "ResolveResult.h"
     20 
     21 struct JSContext;
     22 class nsIScriptElement;
     23 class nsIURI;
     24 
     25 namespace JS::loader {
     26 class LoadedScript;
     27 class ScriptLoaderInterface;
     28 class ScriptLoadRequest;
     29 
     30 /**
     31 * A helper class to report warning to ScriptLoaderInterface.
     32 */
     33 class ReportWarningHelper {
     34 public:
     35  ReportWarningHelper(ScriptLoaderInterface* aLoader,
     36                      ScriptLoadRequest* aRequest)
     37      : mLoader(aLoader), mRequest(aRequest) {}
     38 
     39  void Report(const char* aMessageName,
     40              const nsTArray<nsString>& aParams = nsTArray<nsString>()) const;
     41 
     42 private:
     43  RefPtr<ScriptLoaderInterface> mLoader;
     44  ScriptLoadRequest* mRequest;
     45 };
     46 
     47 // Specifier map from import maps.
     48 // https://html.spec.whatwg.org/multipage/webappapis.html#module-specifier-map
     49 using SpecifierMap =
     50    std::map<nsString, nsCOMPtr<nsIURI>, std::greater<nsString>>;
     51 
     52 // Scope map from import maps.
     53 // https://html.spec.whatwg.org/multipage/webappapis.html#concept-import-map-scopes
     54 using ScopeMap = std::map<nsCString, mozilla::UniquePtr<SpecifierMap>,
     55                          std::greater<nsCString>>;
     56 
     57 // Integrity map from import maps.
     58 // https://html.spec.whatwg.org/multipage/webappapis.html#concept-import-map-integrity
     59 using IntegrityMap = std::map<nsCString, nsString, std::greater<nsCString>>;
     60 
     61 /**
     62 * Implementation of Import maps.
     63 * https://html.spec.whatwg.org/multipage/webappapis.html#import-maps
     64 */
     65 class ImportMap {
     66 public:
     67  ImportMap(mozilla::UniquePtr<SpecifierMap> aImports,
     68            mozilla::UniquePtr<ScopeMap> aScopes,
     69            mozilla::UniquePtr<IntegrityMap> aIntegrity)
     70      : mImports(std::move(aImports)),
     71        mScopes(std::move(aScopes)),
     72        mIntegrity(std::move(aIntegrity)) {}
     73 
     74  /**
     75   * Parse the JSON string from the Import map script.
     76   * This function will throw a TypeError if there's any invalid key or value in
     77   * the JSON text according to the spec.
     78   *
     79   * https://html.spec.whatwg.org/multipage/webappapis.html#parse-an-import-map-string
     80   */
     81  static mozilla::UniquePtr<ImportMap> ParseString(
     82      JSContext* aCx, SourceText<char16_t>& aInput, nsIURI* aBaseURL,
     83      const ReportWarningHelper& aWarning);
     84 
     85  /**
     86   * This implements "Resolve a module specifier" algorithm defined in the
     87   * Import maps spec.
     88   *
     89   * See
     90   * https://html.spec.whatwg.org/multipage/webappapis.html#resolve-a-module-specifier
     91   *
     92   * Impl note: According to the spec, if the specifier cannot be resolved, this
     93   * method will throw a TypeError(Step 13). But the tricky part is when
     94   * creating a module script,
     95   * see
     96   * https://html.spec.whatwg.org/multipage/webappapis.html#validate-requested-module-specifiers
     97   * If the resolving failed, it shall catch the exception and set to the
     98   * script's parse error.
     99   * For implementation we return a ResolveResult here, and the callers will
    100   * need to convert the result to a TypeError if it fails.
    101   */
    102  static ResolveResult ResolveModuleSpecifier(ImportMap* aImportMap,
    103                                              ScriptLoaderInterface* aLoader,
    104                                              LoadedScript* aScript,
    105                                              const nsAString& aSpecifier);
    106 
    107  static mozilla::Maybe<nsString> LookupIntegrity(ImportMap* aImportMap,
    108                                                  nsIURI* aURL);
    109 
    110  // Logging
    111  static mozilla::LazyLogModule gImportMapLog;
    112 
    113 private:
    114  /**
    115   * https://html.spec.whatwg.org/multipage/webappapis.html#import-map-processing-model
    116   *
    117   * Formally, an import map is a struct with two items:
    118   * 1. imports, a module specifier map, and
    119   * 2. scopes, an ordered map of URLs to module specifier maps.
    120   */
    121  mozilla::UniquePtr<SpecifierMap> mImports;
    122  mozilla::UniquePtr<ScopeMap> mScopes;
    123  mozilla::UniquePtr<IntegrityMap> mIntegrity;
    124 };
    125 
    126 }  // namespace JS::loader
    127 
    128 #endif  // js_loader_ImportMap_h