Bug 1619403. Merge nsPrintJob's GetDisplayTitleAndURL and GetDocumentTitleAndURL. r=nordzilla

This also removes nsPrintJob::mFallbackDocTitle since there really is no point
in blindly calculating it ahead of time when we will virtually never use it,
and even if there was there's no reason to store in on the nsPrintJob given
that we create a new nsPrintJob every time we print!

Differential Revision: https://phabricator.services.mozilla.com/D65014

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Jonathan Watt 2020-03-03 20:08:53 +00:00
Родитель 7f6af6cae2
Коммит 56e18a7319
2 изменённых файлов: 85 добавлений и 91 удалений

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

@ -266,37 +266,6 @@ static nsPrintObject* FindPrintObjectByDOMWin(nsPrintObject* aPO,
return nullptr;
}
static void GetDocumentTitleAndURL(Document* aDoc, nsAString& aTitle,
nsAString& aURLStr) {
NS_ASSERTION(aDoc, "Pointer is null!");
aTitle.Truncate();
aURLStr.Truncate();
aDoc->GetTitle(aTitle);
nsIURI* url = aDoc->GetDocumentURI();
if (!url) return;
nsCOMPtr<nsIURIFixup> urifixup(components::URIFixup::Service());
if (!urifixup) return;
nsCOMPtr<nsIURI> exposableURI;
urifixup->CreateExposableURI(url, getter_AddRefs(exposableURI));
if (!exposableURI) return;
nsAutoCString urlCStr;
nsresult rv = exposableURI->GetSpec(urlCStr);
if (NS_FAILED(rv)) return;
nsCOMPtr<nsITextToSubURI> textToSubURI =
do_GetService(NS_ITEXTTOSUBURI_CONTRACTID, &rv);
if (NS_FAILED(rv)) return;
textToSubURI->UnEscapeURIForUI(NS_LITERAL_CSTRING("UTF-8"), urlCStr, aURLStr);
}
static nsresult GetSeqFrameAndCountPagesInternal(
const UniquePtr<nsPrintObject>& aPO, nsIFrame*& aSeqFrame,
int32_t& aCount) {
@ -624,20 +593,6 @@ nsresult nsPrintJob::Initialize(nsIDocumentViewerPrint* aDocViewerPrint,
mHasMozPrintCallback =
hasMozPrintCallback || AnySubdocHasPrintCallbackCanvas(*aOriginalDoc);
nsCOMPtr<nsIStringBundle> brandBundle;
nsCOMPtr<nsIStringBundleService> svc =
mozilla::services::GetStringBundleService();
if (svc) {
svc->CreateBundle("chrome://branding/locale/brand.properties",
getter_AddRefs(brandBundle));
if (brandBundle) {
brandBundle->GetStringFromName("brandShortName", mFallbackDocTitle);
}
}
if (mFallbackDocTitle.IsEmpty()) {
mFallbackDocTitle.AssignLiteral(u"Mozilla Document");
}
return NS_OK;
}
@ -1278,49 +1233,75 @@ void nsPrintJob::SetPrintPO(nsPrintObject* aPO, bool aPrint) {
}
}
//---------------------------------------------------------------------
// This will first use a Title and/or URL from the PrintSettings
// if one isn't set then it uses the one from the document
// then if not title is there we will make sure we send something back
// depending on the situation.
void nsPrintJob::GetDisplayTitleAndURL(const UniquePtr<nsPrintObject>& aPO,
nsAString& aTitle, nsAString& aURLStr,
eDocTitleDefault aDefType) {
NS_ASSERTION(aPO, "Pointer is null!");
if (!mPrt) return;
// static
void nsPrintJob::GetDisplayTitleAndURL(Document& aDoc,
nsIPrintSettings* aSettings,
DocTitleDefault aTitleDefault,
nsAString& aTitle, nsAString& aURLStr) {
aTitle.Truncate();
aURLStr.Truncate();
// First check to see if the PrintSettings has defined an alternate title
// and use that if it did
if (mPrt->mPrintSettings) {
mPrt->mPrintSettings->GetTitle(aTitle);
mPrt->mPrintSettings->GetDocURL(aURLStr);
}
nsAutoString docTitle;
nsAutoString docUrl;
GetDocumentTitleAndURL(aPO->mDocument, docTitle, docUrl);
if (aURLStr.IsEmpty() && !docUrl.IsEmpty()) {
aURLStr = docUrl;
if (aSettings) {
aSettings->GetTitle(aTitle);
aSettings->GetDocURL(aURLStr);
}
if (aTitle.IsEmpty()) {
if (!docTitle.IsEmpty()) {
aTitle = docTitle;
} else {
if (aDefType == eDocTitleDefURLDoc) {
if (!aURLStr.IsEmpty()) {
aTitle = aURLStr;
} else if (!mFallbackDocTitle.IsEmpty()) {
aTitle = mFallbackDocTitle;
aDoc.GetTitle(aTitle);
if (aTitle.IsEmpty()) {
if (!aURLStr.IsEmpty() &&
aTitleDefault == DocTitleDefault::eDocURLElseFallback) {
aTitle = aURLStr;
} else {
nsCOMPtr<nsIStringBundle> brandBundle;
nsCOMPtr<nsIStringBundleService> svc =
mozilla::services::GetStringBundleService();
if (svc) {
svc->CreateBundle("chrome://branding/locale/brand.properties",
getter_AddRefs(brandBundle));
if (brandBundle) {
brandBundle->GetStringFromName("brandShortName", aTitle);
}
}
if (aTitle.IsEmpty()) {
aTitle.AssignLiteral(u"Mozilla Document");
}
}
}
}
if (aURLStr.IsEmpty()) {
nsIURI* url = aDoc.GetDocumentURI();
if (!url) {
return;
}
nsCOMPtr<nsIURIFixup> urifixup(components::URIFixup::Service());
if (!urifixup) {
return;
}
nsCOMPtr<nsIURI> exposableURI;
urifixup->CreateExposableURI(url, getter_AddRefs(exposableURI));
if (!exposableURI) {
return;
}
nsAutoCString urlCStr;
nsresult rv = exposableURI->GetSpec(urlCStr);
if (NS_FAILED(rv)) {
return;
}
nsCOMPtr<nsITextToSubURI> textToSubURI =
do_GetService(NS_ITEXTTOSUBURI_CONTRACTID, &rv);
if (NS_FAILED(rv)) {
return;
}
textToSubURI->UnEscapeURIForUI(NS_LITERAL_CSTRING("UTF-8"), urlCStr,
aURLStr);
}
}
//---------------------------------------------------------------------
@ -1651,8 +1632,9 @@ nsresult nsPrintJob::SetupToPrintContent() {
nsAutoString docTitleStr;
nsAutoString docURLStr;
GetDisplayTitleAndURL(printData->mPrintObject, docTitleStr, docURLStr,
eDocTitleDefURLDoc);
GetDisplayTitleAndURL(
*printData->mPrintObject->mDocument, printData->mPrintSettings,
DocTitleDefault::eDocURLElseFallback, docTitleStr, docURLStr);
int32_t startPage = 1;
int32_t endPage = printData->mNumPrintablePages;
@ -2397,7 +2379,8 @@ nsresult nsPrintJob::DoPrint(const UniquePtr<nsPrintObject>& aPO) {
nsAutoString docTitleStr;
nsAutoString docURLStr;
GetDisplayTitleAndURL(aPO, docTitleStr, docURLStr, eDocTitleDefBlank);
GetDisplayTitleAndURL(*aPO->mDocument, mPrt->mPrintSettings,
DocTitleDefault::eFallback, docTitleStr, docURLStr);
if (!seqFrame) {
SetIsPrinting(false);
@ -2430,7 +2413,9 @@ void nsPrintJob::SetURLAndTitleOnProgressParams(
nsAutoString docTitleStr;
nsAutoString docURLStr;
GetDisplayTitleAndURL(aPO, docTitleStr, docURLStr, eDocTitleDefURLDoc);
GetDisplayTitleAndURL(*aPO->mDocument, mPrt->mPrintSettings,
DocTitleDefault::eDocURLElseFallback, docTitleStr,
docURLStr);
// Make sure the Titles & URLS don't get too long for the progress dialog
EllipseLongString(docTitleStr, kTitleLength, false);

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

@ -126,10 +126,6 @@ class nsPrintJob final : public nsIObserver,
bool GetIsPrintPreview() { return mIsDoingPrintPreview; }
bool GetIsCreatingPrintPreview() { return mIsCreatingPrintPreview; }
// This enum tells indicates what the default should be for the title
// if the title from the document is null
enum eDocTitleDefault { eDocTitleDefBlank, eDocTitleDefURLDoc };
nsresult GetSeqFrameAndCountPages(nsIFrame*& aSeqFrame, int32_t& aCount);
void TurnScriptingOn(bool aDoTurnOn);
@ -211,9 +207,24 @@ class nsPrintJob final : public nsIObserver,
// get the currently infocus frame for the document viewer
already_AddRefed<nsPIDOMWindowOuter> FindFocusedDOMWindow() const;
void GetDisplayTitleAndURL(const mozilla::UniquePtr<nsPrintObject>& aPO,
nsAString& aTitle, nsAString& aURLStr,
eDocTitleDefault aDefType);
/// Customizes the behaviour of GetDisplayTitleAndURL.
enum class DocTitleDefault : uint32_t { eDocURLElseFallback, eFallback };
/**
* Gets the title and URL of the document for display in save-to-PDF dialogs,
* print spooler lists and page headers/footers. This will get the title/URL
* from the PrintSettings, if set, otherwise it will get them from the
* document.
*
* For the title specifically, if a value is not provided by the settings
* object or the document then, if eDocURLElseFallback is passed, the document
* URL will be returned as the title if it's non-empty (which should always be
* the case). Otherwise a non-empty fallback title will be returned.
*/
static void GetDisplayTitleAndURL(mozilla::dom::Document& aDoc,
nsIPrintSettings* aSettings,
DocTitleDefault aTitleDefault,
nsAString& aTitle, nsAString& aURLStr);
MOZ_CAN_RUN_SCRIPT nsresult
CommonPrint(bool aIsPrintPreview, nsIPrintSettings* aPrintSettings,
@ -282,8 +293,6 @@ class nsPrintJob final : public nsIObserver,
nsPagePrintTimer* mPagePrintTimer = nullptr;
nsString mFallbackDocTitle;
float mScreenDPI = 115.0f;
int32_t mLoadCounter = 0;