Bug 1621315 - Refactor Geturi Usage in AddonContentPolicy.cpp r=ckerschb

Differential Revision: https://phabricator.services.mozilla.com/D66595
This commit is contained in:
Sebastian Streich 2020-04-20 15:57:51 +00:00
Родитель dc8aa85dad
Коммит 09ee22a251
1 изменённых файлов: 13 добавлений и 12 удалений

Просмотреть файл

@ -57,7 +57,8 @@ static nsresult GetWindowIDFromContext(nsISupports* aContext,
return NS_OK;
}
static nsresult LogMessage(const nsAString& aMessage, nsIURI* aSourceURI,
static nsresult LogMessage(const nsAString& aMessage,
const nsAString& aSourceName,
const nsAString& aSourceSample,
nsISupports* aContext) {
nsCOMPtr<nsIScriptError> error = do_CreateInstance(NS_SCRIPTERROR_CONTRACTID);
@ -66,9 +67,9 @@ static nsresult LogMessage(const nsAString& aMessage, nsIURI* aSourceURI,
uint64_t windowID = 0;
GetWindowIDFromContext(aContext, &windowID);
nsresult rv = error->InitWithSourceURI(aMessage, aSourceURI, aSourceSample, 0,
0, nsIScriptError::errorFlag,
"JavaScript", windowID);
nsresult rv = error->InitWithSanitizedSource(
aMessage, aSourceName, aSourceSample, 0, 0, nsIScriptError::errorFlag,
"JavaScript", windowID);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIConsoleService> console =
@ -93,25 +94,21 @@ AddonContentPolicy::ShouldLoad(nsIURI* aContentLocation, nsILoadInfo* aLoadInfo,
}
uint32_t contentType = aLoadInfo->GetExternalContentPolicyType();
nsCOMPtr<nsIURI> requestOrigin;
if (nsIPrincipal* loadingPrincipal = aLoadInfo->GetLoadingPrincipal()) {
loadingPrincipal->GetURI(getter_AddRefs(requestOrigin));
}
MOZ_ASSERT(contentType == nsContentUtils::InternalContentPolicyTypeToExternal(
contentType),
"We should only see external content policy types here.");
*aShouldLoad = nsIContentPolicy::ACCEPT;
if (!requestOrigin) {
nsCOMPtr<nsIPrincipal> loadingPrincipal = aLoadInfo->GetLoadingPrincipal();
if (!loadingPrincipal) {
return NS_OK;
}
// Only apply this policy to requests from documents loaded from
// moz-extension URLs, or to resources being loaded from moz-extension URLs.
if (!(aContentLocation->SchemeIs("moz-extension") ||
requestOrigin->SchemeIs("moz-extension"))) {
loadingPrincipal->SchemeIs("moz-extension"))) {
return NS_OK;
}
@ -128,8 +125,12 @@ AddonContentPolicy::ShouldLoad(nsIURI* aContentLocation, nsILoadInfo* aLoadInfo,
aLoadInfo, nsILoadInfo::BLOCKING_REASON_CONTENT_POLICY_WEBEXT);
*aShouldLoad = nsIContentPolicy::REJECT_REQUEST;
nsCString sourceName;
loadingPrincipal->GetExposableSpec(sourceName);
NS_ConvertUTF8toUTF16 nameString(sourceName);
nsCOMPtr<nsISupports> context = aLoadInfo->GetLoadingContext();
LogMessage(NS_LITERAL_STRING(VERSIONED_JS_BLOCKED_MESSAGE), requestOrigin,
LogMessage(NS_LITERAL_STRING(VERSIONED_JS_BLOCKED_MESSAGE), nameString,
typeString, context);
return NS_OK;
}