Bug 1883693 - Remove early return in nsExternalProtocolHandler::NewChannel when handler doesn't exist r=nika

Differential Revision: https://phabricator.services.mozilla.com/D205296
This commit is contained in:
Valentin Gosu 2024-05-02 18:11:51 +00:00
parent 08583baa97
commit b74a2369f2
5 changed files with 14 additions and 45 deletions

View file

@ -1,6 +1,3 @@
[same-origin.html] [same-origin.html]
[unsupported_scheme] expected:
expected: if (os == "android") and fission: [OK, TIMEOUT]
if (os == "mac") and not debug: [FAIL, PASS]
if (os == "android") and not debug: [FAIL, PASS]
FAIL

View file

@ -1,6 +1,3 @@
[same-origin.html] [same-origin.html]
[unsupported_scheme] expected:
expected: if (os == "android") and fission: [OK, TIMEOUT]
if (os == "mac") and not debug: [FAIL, PASS]
if (os == "android") and not debug: [FAIL, PASS]
FAIL

View file

@ -7,6 +7,7 @@
#include "mozilla/dom/ContentChild.h" #include "mozilla/dom/ContentChild.h"
#include "mozilla/BasePrincipal.h" #include "mozilla/BasePrincipal.h"
#include "mozilla/ScopeExit.h"
#include "nsIURI.h" #include "nsIURI.h"
#include "nsExternalProtocolHandler.h" #include "nsExternalProtocolHandler.h"
#include "nsString.h" #include "nsString.h"
@ -148,22 +149,25 @@ nsresult nsExtProtocolChannel::OpenURL() {
nsCOMPtr<nsIExternalProtocolService> extProtService( nsCOMPtr<nsIExternalProtocolService> extProtService(
do_GetService(NS_EXTERNALPROTOCOLSERVICE_CONTRACTID)); do_GetService(NS_EXTERNALPROTOCOLSERVICE_CONTRACTID));
auto cleanup = mozilla::MakeScopeExit([&] {
mCallbacks = nullptr;
mListener = nullptr;
});
if (extProtService) { if (extProtService) {
#ifdef DEBUG
nsAutoCString urlScheme; nsAutoCString urlScheme;
mUrl->GetScheme(urlScheme); mUrl->GetScheme(urlScheme);
bool haveHandler = false; bool haveHandler = false;
extProtService->ExternalProtocolHandlerExists(urlScheme.get(), extProtService->ExternalProtocolHandlerExists(urlScheme.get(),
&haveHandler); &haveHandler);
NS_ASSERTION(haveHandler, if (!haveHandler) {
"Why do we have a channel for this url if we don't support " return NS_ERROR_UNKNOWN_PROTOCOL;
"the protocol?"); }
#endif
RefPtr<mozilla::dom::BrowsingContext> ctx; RefPtr<mozilla::dom::BrowsingContext> ctx;
rv = mLoadInfo->GetTargetBrowsingContext(getter_AddRefs(ctx)); rv = mLoadInfo->GetTargetBrowsingContext(getter_AddRefs(ctx));
if (NS_FAILED(rv)) { if (NS_FAILED(rv)) {
goto finish; return rv;
} }
RefPtr<nsIPrincipal> triggeringPrincipal = mLoadInfo->TriggeringPrincipal(); RefPtr<nsIPrincipal> triggeringPrincipal = mLoadInfo->TriggeringPrincipal();
@ -189,9 +193,6 @@ nsresult nsExtProtocolChannel::OpenURL() {
} }
} }
finish:
mCallbacks = nullptr;
mListener = nullptr;
return rv; return rv;
} }
@ -497,22 +498,6 @@ nsExternalProtocolHandler::AllowPort(int32_t port, const char* scheme,
*_retval = false; *_retval = false;
return NS_OK; return NS_OK;
} }
// returns TRUE if the OS can handle this protocol scheme and false otherwise.
bool nsExternalProtocolHandler::HaveExternalProtocolHandler(nsIURI* aURI) {
MOZ_ASSERT(aURI);
nsAutoCString scheme;
aURI->GetScheme(scheme);
nsCOMPtr<nsIExternalProtocolService> extProtSvc(
do_GetService(NS_EXTERNALPROTOCOLSERVICE_CONTRACTID));
if (!extProtSvc) {
return false;
}
bool haveHandler = false;
extProtSvc->ExternalProtocolHandlerExists(scheme.get(), &haveHandler);
return haveHandler;
}
NS_IMETHODIMP NS_IMETHODIMP
nsExternalProtocolHandler::NewChannel(nsIURI* aURI, nsILoadInfo* aLoadInfo, nsExternalProtocolHandler::NewChannel(nsIURI* aURI, nsILoadInfo* aLoadInfo,
@ -520,14 +505,6 @@ nsExternalProtocolHandler::NewChannel(nsIURI* aURI, nsILoadInfo* aLoadInfo,
NS_ENSURE_TRUE(aURI, NS_ERROR_UNKNOWN_PROTOCOL); NS_ENSURE_TRUE(aURI, NS_ERROR_UNKNOWN_PROTOCOL);
NS_ENSURE_TRUE(aRetval, NS_ERROR_UNKNOWN_PROTOCOL); NS_ENSURE_TRUE(aRetval, NS_ERROR_UNKNOWN_PROTOCOL);
// Only try to return a channel if we have a protocol handler for the url.
// nsOSHelperAppService::LoadUriInternal relies on this to check trustedness
// for some platforms at least. (win uses ::ShellExecute and unix uses
// gnome_url_show.)
if (!HaveExternalProtocolHandler(aURI)) {
return NS_ERROR_UNKNOWN_PROTOCOL;
}
nsCOMPtr<nsIChannel> channel = new nsExtProtocolChannel(aURI, aLoadInfo); nsCOMPtr<nsIChannel> channel = new nsExtProtocolChannel(aURI, aLoadInfo);
channel.forget(aRetval); channel.forget(aRetval);
return NS_OK; return NS_OK;

View file

@ -30,7 +30,6 @@ class nsExternalProtocolHandler final : public nsIExternalProtocolHandler,
~nsExternalProtocolHandler(); ~nsExternalProtocolHandler();
// helper function // helper function
bool HaveExternalProtocolHandler(nsIURI* aURI);
nsCString m_schemeName; nsCString m_schemeName;
}; };

View file

@ -28,7 +28,6 @@ class nsOSHelperAppService : public nsExternalHelperAppService {
// override nsIExternalProtocolService methods // override nsIExternalProtocolService methods
NS_IMETHOD OSProtocolHandlerExists(const char* aProtocolScheme, NS_IMETHOD OSProtocolHandlerExists(const char* aProtocolScheme,
bool* aHandlerExists) override; bool* aHandlerExists) override;
nsresult LoadUriInternal(nsIURI* aURL);
NS_IMETHOD GetApplicationDescription(const nsACString& aScheme, NS_IMETHOD GetApplicationDescription(const nsACString& aScheme,
nsAString& _retval) override; nsAString& _retval) override;