Commit ca0fec34 authored by Nika Layzell's avatar Nika Layzell
Browse files

Bug 1337056 - Part 1: Add a mechanism for grouping permissions into groups to...

Bug 1337056 - Part 1: Add a mechanism for grouping permissions into groups to be sent over IPC, r=baku

MozReview-Commit-ID: IQiSsVGaAOQ
parent 952d1e94
Loading
Loading
Loading
Loading
+42 −0
Original line number Diff line number Diff line
@@ -2933,3 +2933,45 @@ nsPermissionManager::FetchPermissions() {
  }
  return NS_OK;
}

// XXX: Support file URIs here as well!
/* static */ void
nsPermissionManager::GetKeyForPrincipal(nsIPrincipal* aPrincipal, nsACString& aKey)
{
  MOZ_ASSERT(aPrincipal);
  aKey.Truncate();

  nsCOMPtr<nsIURI> uri;
  nsresult rv = aPrincipal->GetURI(getter_AddRefs(uri));
  if (NS_WARN_IF(NS_FAILED(rv) || !uri)) {
    // NOTE: We don't propagate the error here, instead we produce the default
    // "" permission key. This means that we can assign every principal a key,
    // even if the GetURI operation on that principal is not meaningful.
    return;
  }

  // If the URI isn't of one of the supported schemes, it has the "" permission
  // key. We can do an early return in that case.
  nsAutoCString scheme;
  uri->GetScheme(scheme);
  if (!scheme.EqualsLiteral("http") &&
      !scheme.EqualsLiteral("https") &&
      !scheme.EqualsLiteral("ftp")) {
    return;
  }

  // We key sets of permissions to be sent over IPC based on their eTLD+1, or in
  // the case where that isn't meaningful, on their IP address or spec.
  nsCOMPtr<nsIEffectiveTLDService> etldService =
    do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID);
  rv = etldService->GetBaseDomain(uri, 0, aKey);
  if (NS_FAILED(rv)) {
    rv = uri->GetHost(aKey);
  }
  if (NS_FAILED(rv)) {
    rv = uri->GetSpec(aKey);
  }
  if (NS_FAILED(rv)) {
    aKey.Truncate();
  }
}
+14 −0
Original line number Diff line number Diff line
@@ -209,6 +209,20 @@ public:
  nsresult
  RemovePermissionsWithAttributes(mozilla::OriginAttributesPattern& aAttrs);

  /**
   * See `nsIPermissionManager::GetPermissionsWithKey` for more info on
   * permission keys.
   *
   * Get the permission key corresponding to the given Principal. This method is
   * intentionally infallible, as we want to provide an permission key to every
   * principal. Principals which don't have meaningful URIs with http://,
   * https://, or ftp:// schemes are given the default "" Permission Key.
   *
   * @param aPrincipal  The Principal which the key is to be extracted from.
   * @param aPermissionKey  A string which will be filled with the permission key.
   */
  static void GetKeyForPrincipal(nsIPrincipal* aPrincipal, nsACString& aPermissionKey);

private:
  virtual ~nsPermissionManager();