Bug 1581859: Part 4d - Add web progress listeners to WebNavigationContent. r=nika

This ports the WebProgressListener logic from WebNavigationContent.js to the
C++ implementation, and adds IPC messages to send them to the parent process.
Linkage between the parent IPDL endpoints and the listeners in
WebNavigation.jsm is added in a subsequent patch.

Differential Revision: https://phabricator.services.mozilla.com/D103215
This commit is contained in:
Kris Maglione 2021-03-23 23:57:12 +00:00
Родитель 9dec8af14b
Коммит 547cfc9377
6 изменённых файлов: 368 добавлений и 10 удалений

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

@ -4,13 +4,88 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/extensions/ExtensionsParent.h"
#include "mozilla/dom/BrowsingContext.h"
#include "mozilla/dom/ContentParent.h"
#include "mozilla/dom/ScriptSettings.h"
#include "mozilla/RefPtr.h"
#include "jsapi.h"
#include "xpcpublic.h"
namespace mozilla {
namespace extensions {
void ExtensionsParent::ActorDestroy(ActorDestroyReason aWhy) {}
static inline JS::Handle<JS::Value> ToJSBoolean(bool aValue) {
return aValue ? JS::TrueHandleValue : JS::FalseHandleValue;
}
JS::Value FrameTransitionDataToJSValue(const FrameTransitionData& aData) {
JS::Rooted<JSObject*> obj(dom::RootingCx());
{
dom::AutoJSAPI jsapi;
MOZ_ALWAYS_TRUE(jsapi.Init(xpc::PrivilegedJunkScope()));
JSContext* cx = jsapi.cx();
obj = JS_NewPlainObject(cx);
if (!obj ||
!JS_SetProperty(cx, obj, "forward_back",
ToJSBoolean(aData.forwardBack())) ||
!JS_SetProperty(cx, obj, "form_submit",
ToJSBoolean(aData.formSubmit())) ||
!JS_SetProperty(cx, obj, "reload", ToJSBoolean(aData.reload())) ||
!JS_SetProperty(cx, obj, "server_redirect",
ToJSBoolean(aData.serverRedirect())) ||
!JS_SetProperty(cx, obj, "client_redirect",
ToJSBoolean(aData.clientRedirect()))) {
return JS::UndefinedValue();
}
}
return JS::ObjectValue(*obj);
}
ipc::IPCResult ExtensionsParent::RecvDocumentChange(
MaybeDiscardedBrowsingContext&& aBC, FrameTransitionData&& aTransitionData,
nsIURI* aLocation) {
if (aBC.IsNullOrDiscarded()) {
return IPC_OK();
}
JS::Rooted<JS::Value> transitionData(
dom::RootingCx(), FrameTransitionDataToJSValue(aTransitionData));
return IPC_OK();
}
ipc::IPCResult ExtensionsParent::RecvHistoryChange(
MaybeDiscardedBrowsingContext&& aBC, FrameTransitionData&& aTransitionData,
nsIURI* aLocation, bool aIsHistoryStateUpdated,
bool aIsReferenceFragmentUpdated) {
if (aBC.IsNullOrDiscarded()) {
return IPC_OK();
}
JS::Rooted<JS::Value> transitionData(
dom::RootingCx(), FrameTransitionDataToJSValue(aTransitionData));
return IPC_OK();
}
ipc::IPCResult ExtensionsParent::RecvStateChange(
MaybeDiscardedBrowsingContext&& aBC, nsIURI* aRequestURI, nsresult aStatus,
uint32_t aStateFlags) {
if (aBC.IsNullOrDiscarded()) {
return IPC_OK();
}
return IPC_OK();
}
ipc::IPCResult ExtensionsParent::RecvCreatedNavigationTarget(
MaybeDiscardedBrowsingContext&& aBC,
MaybeDiscardedBrowsingContext&& aSourceBC, const nsCString& aURI) {
if (aBC.IsNullOrDiscarded() || aSourceBC.IsNull()) {
return IPC_OK();
}
return IPC_OK();
}
} // namespace extensions
} // namespace mozilla

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

@ -18,6 +18,24 @@ class ExtensionsParent final : public PExtensionsParent {
ExtensionsParent() = default;
ipc::IPCResult RecvDocumentChange(MaybeDiscardedBrowsingContext&& aBC,
FrameTransitionData&& aTransitionData,
nsIURI* aLocation);
ipc::IPCResult RecvHistoryChange(MaybeDiscardedBrowsingContext&& aBC,
FrameTransitionData&& aTransitionData,
nsIURI* aLocation,
bool aIsHistoryStateUpdated,
bool aIsReferenceFragmentUpdated);
ipc::IPCResult RecvStateChange(MaybeDiscardedBrowsingContext&& aBC,
nsIURI* aRequestURI, nsresult aStatus,
uint32_t aStateFlags);
ipc::IPCResult RecvCreatedNavigationTarget(
MaybeDiscardedBrowsingContext&& aBC,
MaybeDiscardedBrowsingContext&& aSourceBC, const nsCString& aURI);
private:
~ExtensionsParent() = default;

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

@ -6,10 +6,21 @@
include protocol PContent;
include protocol PInProcess;
include DOMTypes;
include "mozilla/ipc/URIUtils.h";
namespace mozilla {
namespace extensions {
struct FrameTransitionData
{
bool clientRedirect;
bool formSubmit;
bool forwardBack;
bool reload;
bool serverRedirect;
};
/**
* A generic protocol used by the extension framework for process-level IPC. A
* child instance is created at startup in the parent process and each content
@ -22,6 +33,25 @@ refcounted protocol PExtensions
parent:
async __delete__();
async DocumentChange(MaybeDiscardedBrowsingContext bc,
FrameTransitionData transitionData,
nsIURI location);
async HistoryChange(MaybeDiscardedBrowsingContext bc,
FrameTransitionData transitionData,
nsIURI location,
bool isHistoryStateUpdated,
bool isReferenceFragmentUpdated);
async StateChange(MaybeDiscardedBrowsingContext bc,
nsIURI requestURI,
nsresult status,
uint32_t stateFlags);
async CreatedNavigationTarget(MaybeDiscardedBrowsingContext bc,
MaybeDiscardedBrowsingContext sourceBC,
nsCString url);
};
} // namespace extensions

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

@ -261,8 +261,7 @@ add_task(async function webnav_transitions_props() {
win.location = CLIENT_REDIRECT_HTTPHEADER;
});
found = received.find((data) => (data.event == "onCommitted" &&
data.url == CLIENT_REDIRECT_HTTPHEADER));
found = received.find((data) => (data.event == "onCommitted" && data.url == REDIRECTED));
ok(found, "Got the onCommitted event");

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

@ -5,20 +5,32 @@
#include "mozilla/extensions/WebNavigationContent.h"
#include "mozilla/Assertions.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/dom/BrowsingContext.h"
#include "mozilla/dom/ContentFrameMessageManager.h"
#include "mozilla/dom/Event.h"
#include "mozilla/dom/EventTarget.h"
#include "mozilla/extensions/ExtensionsChild.h"
#include "mozilla/RefPtr.h"
#include "mozilla/ResultExtensions.h"
#include "mozilla/Services.h"
#include "nsCRT.h"
#include "nsDocShellLoadTypes.h"
#include "nsPIWindowRoot.h"
#include "nsIChannel.h"
#include "nsIDocShell.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsIObserverService.h"
#include "nsIPropertyBag2.h"
#include "nsIWebNavigation.h"
#include "nsIWebProgress.h"
#include "nsGlobalWindowOuter.h"
#include "nsQueryObject.h"
namespace mozilla {
namespace extensions {
WebNavigationContent::WebNavigationContent() {}
/* static */
already_AddRefed<WebNavigationContent> WebNavigationContent::GetSingleton() {
static RefPtr<WebNavigationContent> sSingleton;
@ -30,14 +42,14 @@ already_AddRefed<WebNavigationContent> WebNavigationContent::GetSingleton() {
return do_AddRef(sSingleton);
}
NS_IMPL_ISUPPORTS(WebNavigationContent, nsIObserver)
NS_IMPL_ISUPPORTS(WebNavigationContent, nsIObserver, nsIWebProgressListener,
nsISupportsWeakReference)
void WebNavigationContent::Init() {
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
obs->AddObserver(this, "chrome-event-target-created", false);
obs->AddObserver(this, "webNavigation-createdNavigationTarget-from-js",
false);
obs->AddObserver(this, "chrome-event-target-created", true);
obs->AddObserver(this, "webNavigation-createdNavigationTarget-from-js", true);
}
NS_IMETHODIMP WebNavigationContent::Observe(nsISupports* aSubject,
@ -52,8 +64,26 @@ NS_IMETHODIMP WebNavigationContent::Observe(nsISupports* aSubject,
if (RefPtr<dom::EventTarget> eventTarget = do_QueryObject(aSubject)) {
AttachListeners(eventTarget);
}
nsCOMPtr<nsIDocShell> docShell;
if (nsCOMPtr<nsPIWindowRoot> root = do_QueryInterface(aSubject)) {
docShell = root->GetWindow()->GetDocShell();
} else if (RefPtr<dom::ContentFrameMessageManager> mm =
do_QueryObject(aSubject)) {
docShell = mm->GetDocShell(IgnoreErrors());
}
if (docShell && docShell->GetBrowsingContext()->IsContent()) {
nsCOMPtr<nsIWebProgress> webProgress(do_GetInterface(docShell));
webProgress->AddProgressListener(this,
nsIWebProgress::NOTIFY_STATE_WINDOW |
nsIWebProgress::NOTIFY_LOCATION);
}
} else if (!nsCRT::strcmp(aTopic,
"webNavigation-createdNavigationTarget-from-js")) {
if (nsCOMPtr<nsIPropertyBag2> props = do_QueryInterface(aSubject)) {
return OnCreatedNavigationTargetFromJS(props);
}
}
return NS_OK;
}
@ -64,5 +94,195 @@ void WebNavigationContent::AttachListeners(
NS_IMETHODIMP
WebNavigationContent::HandleEvent(dom::Event* aEvent) { return NS_OK; }
static dom::BrowsingContext* GetBrowsingContext(nsIWebProgress* aWebProgress) {
// FIXME: Get this via nsIWebNavigation instead.
nsCOMPtr<nsIDocShell> docShell(do_GetInterface(aWebProgress));
return docShell->GetBrowsingContext();
}
FrameTransitionData WebNavigationContent::GetFrameTransitionData(
nsIWebProgress* aWebProgress, nsIRequest* aRequest) {
FrameTransitionData result;
uint32_t loadType = 0;
Unused << aWebProgress->GetLoadType(&loadType);
if (loadType & nsIDocShell::LOAD_CMD_HISTORY) {
result.forwardBack() = true;
}
if (loadType & nsIDocShell::LOAD_CMD_RELOAD) {
result.reload() = true;
}
if (LOAD_TYPE_HAS_FLAGS(loadType, nsIWebNavigation::LOAD_FLAGS_IS_REFRESH)) {
result.clientRedirect() = true;
}
if (nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest)) {
nsCOMPtr<nsILoadInfo> loadInfo(channel->LoadInfo());
if (loadInfo->RedirectChain().Length()) {
result.serverRedirect() = true;
}
if (loadInfo->GetIsFormSubmission() &&
!(loadType & (nsIDocShell::LOAD_CMD_HISTORY |
nsIDocShell::LOAD_CMD_RELOAD))) {
result.formSubmit() = true;
}
}
return result;
}
nsresult WebNavigationContent::OnCreatedNavigationTargetFromJS(
nsIPropertyBag2* aProps) {
nsCOMPtr<nsIDocShell> createdDocShell(
do_GetProperty(aProps, u"createdTabDocShell"_ns));
nsCOMPtr<nsIDocShell> sourceDocShell(
do_GetProperty(aProps, u"sourceTabDocShell"_ns));
NS_ENSURE_ARG_POINTER(createdDocShell);
NS_ENSURE_ARG_POINTER(sourceDocShell);
dom::BrowsingContext* createdBC = createdDocShell->GetBrowsingContext();
dom::BrowsingContext* sourceBC = sourceDocShell->GetBrowsingContext();
if (createdBC->IsContent() && sourceBC->IsContent()) {
nsCString url;
Unused << aProps->GetPropertyAsACString(u"url"_ns, url);
ExtensionsChild::Get().SendCreatedNavigationTarget(createdBC, sourceBC,
url);
}
return NS_OK;
}
// nsIWebProgressListener
NS_IMETHODIMP
WebNavigationContent::OnStateChange(nsIWebProgress* aWebProgress,
nsIRequest* aRequest, uint32_t aStateFlags,
nsresult aStatus) {
nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest));
NS_ENSURE_TRUE(channel, NS_ERROR_UNEXPECTED);
nsCOMPtr<nsIURI> uri;
MOZ_TRY(channel->GetURI(getter_AddRefs(uri)));
// Prevents "about", "chrome", "resource" and "moz-extension" URI schemes to
// be reported with the resolved "file" or "jar" URIs (see bug 1246125)
if (uri->SchemeIs("file") || uri->SchemeIs("jar")) {
nsCOMPtr<nsIURI> originalURI;
MOZ_TRY(channel->GetOriginalURI(getter_AddRefs(originalURI)));
// FIXME: We probably actually want NS_GetFinalChannelURI here.
if (originalURI->SchemeIs("about") || originalURI->SchemeIs("chrome") ||
originalURI->SchemeIs("resource") ||
originalURI->SchemeIs("moz-extension")) {
uri = originalURI.forget();
}
}
RefPtr<dom::BrowsingContext> bc(GetBrowsingContext(aWebProgress));
NS_ENSURE_ARG_POINTER(bc);
ExtensionsChild::Get().SendStateChange(bc, uri, aStatus, aStateFlags);
// Based on the docs of the webNavigation.onCommitted event, it should be
// raised when: "The document might still be downloading, but at least part
// of the document has been received" and for some reason we don't fire
// onLocationChange for the initial navigation of a sub-frame. For the above
// two reasons, when the navigation event is related to a sub-frame we process
// the document change here and then send an OnDocumentChange message to the
// main process, where it will be turned into a webNavigation.onCommitted
// event. (bug 1264936 and bug 125662)
if (!bc->IsTop() && aStateFlags & nsIWebProgressListener::STATE_IS_DOCUMENT) {
ExtensionsChild::Get().SendDocumentChange(
bc, GetFrameTransitionData(aWebProgress, aRequest), uri);
}
return NS_OK;
}
NS_IMETHODIMP
WebNavigationContent::OnProgressChange(nsIWebProgress* aWebProgress,
nsIRequest* aRequest,
int32_t aCurSelfProgress,
int32_t aMaxSelfProgress,
int32_t aCurTotalProgress,
int32_t aMaxTotalProgress) {
MOZ_ASSERT_UNREACHABLE("Listener did not request ProgressChange events");
return NS_OK;
}
NS_IMETHODIMP
WebNavigationContent::OnLocationChange(nsIWebProgress* aWebProgress,
nsIRequest* aRequest, nsIURI* aLocation,
uint32_t aFlags) {
RefPtr<dom::BrowsingContext> bc(GetBrowsingContext(aWebProgress));
NS_ENSURE_ARG_POINTER(bc);
// When a frame navigation doesn't change the current loaded document
// (which can be due to history.pushState/replaceState or to a changed hash in
// the url), it is reported only to the onLocationChange, for this reason we
// process the history change here and then we are going to send an
// OnHistoryChange message to the main process, where it will be turned into
// a webNavigation.onHistoryStateUpdated/onReferenceFragmentUpdated event.
if (aFlags & nsIWebProgressListener::LOCATION_CHANGE_SAME_DOCUMENT) {
uint32_t loadType = 0;
MOZ_TRY(aWebProgress->GetLoadType(&loadType));
// When the location changes but the document is the same:
// - path not changed and hash changed -> |onReferenceFragmentUpdated|
// (even if it changed using |history.pushState|)
// - path not changed and hash not changed -> |onHistoryStateUpdated|
// (only if it changes using |history.pushState|)
// - path changed -> |onHistoryStateUpdated|
bool isHistoryStateUpdated = false;
bool isReferenceFragmentUpdated = false;
if (aFlags & nsIWebProgressListener::LOCATION_CHANGE_HASHCHANGE) {
isReferenceFragmentUpdated = true;
} else if (loadType & nsIDocShell::LOAD_CMD_PUSHSTATE) {
isHistoryStateUpdated = true;
} else if (loadType & nsIDocShell::LOAD_CMD_HISTORY) {
isHistoryStateUpdated = true;
}
if (isHistoryStateUpdated || isReferenceFragmentUpdated) {
ExtensionsChild::Get().SendHistoryChange(
bc, GetFrameTransitionData(aWebProgress, aRequest), aLocation,
isHistoryStateUpdated, isReferenceFragmentUpdated);
}
} else if (bc->IsTop()) {
// We have to catch the document changes from top level frames here,
// where we can detect the "server redirect" transition.
// (bug 1264936 and bug 125662)
ExtensionsChild::Get().SendDocumentChange(
bc, GetFrameTransitionData(aWebProgress, aRequest), aLocation);
}
return NS_OK;
}
NS_IMETHODIMP
WebNavigationContent::OnStatusChange(nsIWebProgress* aWebProgress,
nsIRequest* aRequest, nsresult aStatus,
const char16_t* aMessage) {
MOZ_ASSERT_UNREACHABLE("Listener did not request StatusChange events");
return NS_OK;
}
NS_IMETHODIMP
WebNavigationContent::OnSecurityChange(nsIWebProgress* aWebProgress,
nsIRequest* aRequest, uint32_t aState) {
MOZ_ASSERT_UNREACHABLE("Listener did not request SecurityChange events");
return NS_OK;
}
NS_IMETHODIMP
WebNavigationContent::OnContentBlockingEvent(nsIWebProgress* aWebProgress,
nsIRequest* aRequest,
uint32_t aEvent) {
MOZ_ASSERT_UNREACHABLE("Listener did not request ContentBlocking events");
return NS_OK;
}
} // namespace extensions
} // namespace mozilla

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

@ -9,6 +9,12 @@
#include "nsIDOMEventListener.h"
#include "nsIObserver.h"
#include "nsIWebProgressListener.h"
#include "nsWeakReference.h"
class nsIPropertyBag2;
class nsIRequest;
class nsIWebProgress;
namespace mozilla {
namespace dom {
@ -17,22 +23,32 @@ class EventTarget;
namespace extensions {
class FrameTransitionData;
class WebNavigationContent final : public nsIObserver,
public nsIDOMEventListener {
public nsIDOMEventListener,
public nsIWebProgressListener,
public nsSupportsWeakReference {
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIOBSERVER
NS_DECL_NSIDOMEVENTLISTENER
NS_DECL_NSIWEBPROGRESSLISTENER
static already_AddRefed<WebNavigationContent> GetSingleton();
private:
WebNavigationContent();
WebNavigationContent() = default;
~WebNavigationContent() = default;
void AttachListeners(mozilla::dom::EventTarget* aEventTarget);
void Init();
FrameTransitionData GetFrameTransitionData(nsIWebProgress* aWebProgress,
nsIRequest* aRequest);
nsresult OnCreatedNavigationTargetFromJS(nsIPropertyBag2* aProps);
};
} // namespace extensions