From a267a9ea0c20b494d90ea8e55955a2d2e3b81b2d Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Thu, 28 Jan 2021 17:24:09 +0000 Subject: [PATCH] Bug 1686194 - Fire pagetitlechanged when initialized with an empty string r=smaug Differential Revision: https://phabricator.services.mozilla.com/D102054 --- dom/ipc/WindowGlobalParent.cpp | 4 +-- dom/ipc/WindowGlobalParent.h | 6 ++-- dom/ipc/tests/browser.ini | 2 ++ dom/ipc/tests/browser_bug1686194.js | 47 +++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 dom/ipc/tests/browser_bug1686194.js diff --git a/dom/ipc/WindowGlobalParent.cpp b/dom/ipc/WindowGlobalParent.cpp index ae32c8840288..4b71936a32f7 100644 --- a/dom/ipc/WindowGlobalParent.cpp +++ b/dom/ipc/WindowGlobalParent.cpp @@ -364,11 +364,11 @@ IPCResult WindowGlobalParent::RecvUpdateDocumentPrincipal( } mozilla::ipc::IPCResult WindowGlobalParent::RecvUpdateDocumentTitle( const nsString& aTitle) { - if (mDocumentTitle == aTitle) { + if (mDocumentTitle.isSome() && mDocumentTitle.value() == aTitle) { return IPC_OK(); } - mDocumentTitle = aTitle; + mDocumentTitle = Some(aTitle); // Send a pagetitlechanged event only for changes to the title // for top-level frames. diff --git a/dom/ipc/WindowGlobalParent.h b/dom/ipc/WindowGlobalParent.h index 77a5b09ab6e1..635e1b4f4f7d 100644 --- a/dom/ipc/WindowGlobalParent.h +++ b/dom/ipc/WindowGlobalParent.h @@ -119,7 +119,9 @@ class WindowGlobalParent final : public WindowContext, // The current URI which loaded in the document. nsIURI* GetDocumentURI() override { return mDocumentURI; } - void GetDocumentTitle(nsAString& aTitle) const { aTitle = mDocumentTitle; } + void GetDocumentTitle(nsAString& aTitle) const { + aTitle = mDocumentTitle.valueOr(nsString()); + } nsIPrincipal* GetContentBlockingAllowListPrincipal() const { return mDocContentBlockingAllowListPrincipal; @@ -284,7 +286,7 @@ class WindowGlobalParent final : public WindowContext, // The principal to use for the content blocking allow list. nsCOMPtr mDocContentBlockingAllowListPrincipal; nsCOMPtr mDocumentURI; - nsString mDocumentTitle; + Maybe mDocumentTitle; bool mIsInitialDocument; diff --git a/dom/ipc/tests/browser.ini b/dom/ipc/tests/browser.ini index aa602267641a..b92075760806 100644 --- a/dom/ipc/tests/browser.ini +++ b/dom/ipc/tests/browser.ini @@ -20,3 +20,5 @@ skip-if = !e10s [browser_bug1646088.js] support-files = file_dummy.html skip-if = !e10s +[browser_bug1686194.js] +support-files = file_dummy.html diff --git a/dom/ipc/tests/browser_bug1686194.js b/dom/ipc/tests/browser_bug1686194.js new file mode 100644 index 000000000000..15adb824830a --- /dev/null +++ b/dom/ipc/tests/browser_bug1686194.js @@ -0,0 +1,47 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +"use strict"; + +const TEST_PAGE = + "http://mochi.test:8888/browser/dom/ipc/tests/file_dummy.html"; + +function untilPageTitleChanged() { + return new Promise(resolve => + gBrowser.addEventListener("pagetitlechanged", resolve, { once: true }) + ); +} + +add_task(async () => { + const tab = await BrowserTestUtils.openNewForegroundTab({ + gBrowser, + opening: TEST_PAGE, + }); + + const { linkedBrowser } = tab; + ok( + tab.getAttribute("label").includes("file_dummy.html"), + "The title should be the raw path" + ); + + await Promise.all([ + SpecialPowers.spawn(linkedBrowser, [], function() { + content.document.title = "Title"; + }), + untilPageTitleChanged(), + ]); + + is(tab.getAttribute("label"), "Title", "The title should change"); + + linkedBrowser.reload(); + + await untilPageTitleChanged(); + + ok( + tab.getAttribute("label").includes("file_dummy.html"), + "The title should be the raw path again" + ); + + BrowserTestUtils.removeTab(tab); +});