Bug 1724768 - Part 1: Add ChromeUtils.getPartitionKeyFromURL(). r=pbz,robwu
authorTim Huang <tihuang@mozilla.com>
Fri, 13 Aug 2021 07:57:17 +0000
changeset 588784 33be4ce4b570046da34f9aa21fb9a9a5836a3a4c
parent 588783 c862c1a89dc73744c6bf219687c525dffcb915eb
child 588785 f9710aa1b1c32c2f68f8d08328b8f306bdde62f9
push id38703
push userabutkovits@mozilla.com
push dateFri, 13 Aug 2021 16:07:47 +0000
treeherdermozilla-central@2e3e1b631c62 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspbz, robwu
bugs1724768
milestone93.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1724768 - Part 1: Add ChromeUtils.getPartitionKeyFromURL(). r=pbz,robwu Differential Revision: https://phabricator.services.mozilla.com/D122320
dom/base/ChromeUtils.cpp
dom/base/ChromeUtils.h
dom/chrome-webidl/ChromeUtils.webidl
--- a/dom/base/ChromeUtils.cpp
+++ b/dom/base/ChromeUtils.cpp
@@ -776,16 +776,36 @@ void ChromeUtils::GetBaseDomainFromParti
                                                     pkBaseDomain, port)) {
     aRv.Throw(NS_ERROR_FAILURE);
     return;
   }
 
   aBaseDomain = pkBaseDomain;
 }
 
+/* static */
+void ChromeUtils::GetPartitionKeyFromURL(dom::GlobalObject& aGlobal,
+                                         const nsAString& aURL,
+                                         nsAString& aPartitionKey,
+                                         ErrorResult& aRv) {
+  nsCOMPtr<nsIURI> uri;
+  nsresult rv = NS_NewURI(getter_AddRefs(uri), aURL);
+
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    aPartitionKey.Truncate();
+    aRv.Throw(rv);
+    return;
+  }
+
+  mozilla::OriginAttributes attrs;
+  attrs.SetPartitionKey(uri);
+
+  aPartitionKey = attrs.mPartitionKey;
+}
+
 #ifdef NIGHTLY_BUILD
 /* static */
 void ChromeUtils::GetRecentJSDevError(GlobalObject& aGlobal,
                                       JS::MutableHandleValue aRetval,
                                       ErrorResult& aRv) {
   aRetval.setUndefined();
   auto runtime = CycleCollectedJSRuntime::Get();
   MOZ_ASSERT(runtime);
--- a/dom/base/ChromeUtils.h
+++ b/dom/base/ChromeUtils.h
@@ -124,16 +124,21 @@ class ChromeUtils {
            aA.mPrivateBrowsingId == aB.mPrivateBrowsingId;
   }
 
   static void GetBaseDomainFromPartitionKey(dom::GlobalObject& aGlobal,
                                             const nsAString& aPartitionKey,
                                             nsAString& aBaseDomain,
                                             ErrorResult& aRv);
 
+  static void GetPartitionKeyFromURL(dom::GlobalObject& aGlobal,
+                                     const nsAString& aURL,
+                                     nsAString& aPartitionKey,
+                                     ErrorResult& aRv);
+
   // Implemented in js/xpconnect/loader/ChromeScriptLoader.cpp
   static already_AddRefed<Promise> CompileScript(
       GlobalObject& aGlobal, const nsAString& aUrl,
       const dom::CompileScriptOptionsDictionary& aOptions, ErrorResult& aRv);
 
   static MozQueryInterface* GenerateQI(const GlobalObject& global,
                                        const Sequence<JS::Value>& interfaces,
                                        ErrorResult& aRv);
--- a/dom/chrome-webidl/ChromeUtils.webidl
+++ b/dom/chrome-webidl/ChromeUtils.webidl
@@ -312,16 +312,28 @@ partial namespace ChromeUtils {
    * Returns the empty string for an empty partitionKey.
    * Throws for invalid partition keys.
    */
   [Throws]
   DOMString
   getBaseDomainFromPartitionKey(DOMString partitionKey);
 
   /**
+   * Returns the partitionKey for a given URL.
+   *
+   * The function will treat the URL as a first party and construct the
+   * partitionKey according to the scheme, site and port in the URL.
+   *
+   * Throws for invalid urls.
+   */
+  [Throws]
+  DOMString
+  getPartitionKeyFromURL(DOMString url);
+
+  /**
    * Loads and compiles the script at the given URL and returns an object
    * which may be used to execute it repeatedly, in different globals, without
    * re-parsing.
    */
   [NewObject]
   Promise<PrecompiledScript>
   compileScript(DOMString url, optional CompileScriptOptionsDictionary options = {});