From 68794e5c6e001c05a89bb607f1b2bf48cdf39fa5 Mon Sep 17 00:00:00 2001 From: Masatoshi Kimura Date: Sun, 25 Jun 2017 22:46:08 +0900 Subject: [PATCH] Bug 1376164 - Turn nsDocumentViewer::mHintCharset and mForceCharacterSet into const mozilla::Encoding*. r=hsivonen MozReview-Commit-ID: HTi2eNwDH99 --HG-- extra : rebase_source : 13c702981c188a395b6026883c3731e816f55240 --- docshell/base/nsDocShell.cpp | 16 +++--- docshell/base/nsIContentViewer.idl | 9 ++++ dom/html/nsHTMLDocument.cpp | 39 ++++---------- layout/base/nsDocumentViewer.cpp | 86 ++++++++++++++++++++---------- 4 files changed, 82 insertions(+), 68 deletions(-) diff --git a/docshell/base/nsDocShell.cpp b/docshell/base/nsDocShell.cpp index 13ef1177887e..52dd91a22c87 100644 --- a/docshell/base/nsDocShell.cpp +++ b/docshell/base/nsDocShell.cpp @@ -9392,8 +9392,8 @@ nsDocShell::SetupNewViewer(nsIContentViewer* aNewViewer) NS_ERROR_FAILURE); nsCOMPtr parent(do_QueryInterface(parentAsItem)); - nsAutoCString forceCharset; - nsAutoCString hintCharset; + const Encoding* forceCharset = nullptr; + const Encoding* hintCharset = nullptr; int32_t hintCharsetSource; int32_t minFontSize; float textZoom; @@ -9428,10 +9428,8 @@ nsDocShell::SetupNewViewer(nsIContentViewer* aNewViewer) if (oldCv) { newCv = aNewViewer; if (newCv) { - NS_ENSURE_SUCCESS(oldCv->GetForceCharacterSet(forceCharset), - NS_ERROR_FAILURE); - NS_ENSURE_SUCCESS(oldCv->GetHintCharacterSet(hintCharset), - NS_ERROR_FAILURE); + forceCharset = oldCv->GetForceCharset(); + hintCharset = oldCv->GetHintCharset(); NS_ENSURE_SUCCESS(oldCv->GetHintCharacterSetSource(&hintCharsetSource), NS_ERROR_FAILURE); NS_ENSURE_SUCCESS(oldCv->GetMinFontSize(&minFontSize), @@ -9498,10 +9496,8 @@ nsDocShell::SetupNewViewer(nsIContentViewer* aNewViewer) // If we have old state to copy, set the old state onto the new content // viewer if (newCv) { - NS_ENSURE_SUCCESS(newCv->SetForceCharacterSet(forceCharset), - NS_ERROR_FAILURE); - NS_ENSURE_SUCCESS(newCv->SetHintCharacterSet(hintCharset), - NS_ERROR_FAILURE); + newCv->SetForceCharset(forceCharset); + newCv->SetHintCharset(hintCharset); NS_ENSURE_SUCCESS(newCv->SetHintCharacterSetSource(hintCharsetSource), NS_ERROR_FAILURE); NS_ENSURE_SUCCESS(newCv->SetMinFontSize(minFontSize), diff --git a/docshell/base/nsIContentViewer.idl b/docshell/base/nsIContentViewer.idl index d54528463524..75f457d2d9c7 100644 --- a/docshell/base/nsIContentViewer.idl +++ b/docshell/base/nsIContentViewer.idl @@ -21,6 +21,9 @@ class nsIPresShell; class nsPresContext; class nsView; class nsDOMNavigationTiming; +namespace mozilla { +class Encoding; +} %} [ptr] native nsIWidgetPtr(nsIWidget); @@ -30,6 +33,7 @@ class nsDOMNavigationTiming; [ptr] native nsViewPtr(nsView); [ptr] native nsDOMNavigationTimingPtr(nsDOMNavigationTiming); [ref] native nsIContentViewerTArray(nsTArray >); +[ptr] native Encoding(const mozilla::Encoding); [scriptable, builtinclass, uuid(2da17016-7851-4a45-a7a8-00b360e01595)] interface nsIContentViewer : nsISupports @@ -282,4 +286,9 @@ interface nsIContentViewer : nsISupports * Restore the viewer's natural media type */ void stopEmulatingMedium(); + + [noscript, notxpcom] Encoding getHintCharset(); + [noscript, notxpcom] void setHintCharset(in Encoding aEncoding); + [noscript, notxpcom] Encoding getForceCharset(); + [noscript, notxpcom] void setForceCharset(in Encoding aEncoding); }; diff --git a/dom/html/nsHTMLDocument.cpp b/dom/html/nsHTMLDocument.cpp index a8875a1a88e5..7a8b72c4777a 100644 --- a/dom/html/nsHTMLDocument.cpp +++ b/dom/html/nsHTMLDocument.cpp @@ -150,20 +150,6 @@ static bool ConvertToMidasInternalCommand(const nsAString & inCommandID, // = // ================================================================== -static bool -IsAsciiCompatible(const nsACString& aPreferredName) -{ - // HZ and UTF-7 are no longer in mozilla-central, but keeping them here - // just in case for the benefit of comm-central. - return !(aPreferredName.LowerCaseEqualsLiteral("utf-16") || - aPreferredName.LowerCaseEqualsLiteral("utf-16be") || - aPreferredName.LowerCaseEqualsLiteral("utf-16le") || - aPreferredName.LowerCaseEqualsLiteral("replacement") || - aPreferredName.LowerCaseEqualsLiteral("hz-gb-2312") || - aPreferredName.LowerCaseEqualsLiteral("utf-7") || - aPreferredName.LowerCaseEqualsLiteral("x-imap4-modified-utf7")); -} - static bool IsAsciiCompatible(const Encoding* aEncoding) { @@ -300,21 +286,17 @@ nsHTMLDocument::TryHintCharset(nsIContentViewer* aCv, nsresult rv = aCv->GetHintCharacterSetSource(&requestCharsetSource); if(NS_SUCCEEDED(rv) && kCharsetUninitialized != requestCharsetSource) { - nsAutoCString requestCharset; - rv = aCv->GetHintCharacterSet(requestCharset); + auto requestCharset = aCv->GetHintCharset(); aCv->SetHintCharacterSetSource((int32_t)(kCharsetUninitialized)); if (requestCharsetSource <= aCharsetSource) return; - if (NS_SUCCEEDED(rv) && !requestCharset.IsEmpty()) { - auto encoding = Encoding::ForName(requestCharset); - if (IsAsciiCompatible(encoding)) { - aCharsetSource = requestCharsetSource; - aEncoding = encoding; - } - return; + if (requestCharset && IsAsciiCompatible(requestCharset)) { + aCharsetSource = requestCharsetSource; + aEncoding = WrapNotNull(requestCharset); } + return; } } return; @@ -327,8 +309,6 @@ nsHTMLDocument::TryUserForcedCharset(nsIContentViewer* aCv, int32_t& aCharsetSource, NotNull& aEncoding) { - nsresult rv = NS_OK; - if(kCharsetFromUserForced <= aCharsetSource) return; @@ -337,16 +317,15 @@ nsHTMLDocument::TryUserForcedCharset(nsIContentViewer* aCv, return; } - nsAutoCString forceCharsetFromDocShell; + const Encoding* forceCharsetFromDocShell = nullptr; if (aCv) { // XXX mailnews-only - rv = aCv->GetForceCharacterSet(forceCharsetFromDocShell); + forceCharsetFromDocShell = aCv->GetForceCharset(); } - if(NS_SUCCEEDED(rv) && - !forceCharsetFromDocShell.IsEmpty() && + if(forceCharsetFromDocShell && IsAsciiCompatible(forceCharsetFromDocShell)) { - aEncoding = Encoding::ForName(forceCharsetFromDocShell); + aEncoding = WrapNotNull(forceCharsetFromDocShell); aCharsetSource = kCharsetFromUserForced; return; } diff --git a/layout/base/nsDocumentViewer.cpp b/layout/base/nsDocumentViewer.cpp index ab2dea33a8c3..7f4d13977ab5 100644 --- a/layout/base/nsDocumentViewer.cpp +++ b/layout/base/nsDocumentViewer.cpp @@ -402,9 +402,9 @@ protected: /* character set member data */ int32_t mHintCharsetSource; - nsCString mHintCharset; - nsCString mForceCharacterSet; - + const Encoding* mHintCharset; + const Encoding* mForceCharacterSet; + bool mIsPageMode; bool mInitializedForPrintPreview; bool mHidden; @@ -542,6 +542,8 @@ nsDocumentViewer::nsDocumentViewer() #endif // DEBUG #endif // NS_PRINTING mHintCharsetSource(kCharsetUninitialized), + mHintCharset(nullptr), + mForceCharacterSet(nullptr), mIsPageMode(false), mInitializedForPrintPreview(false), mHidden(false), @@ -3364,15 +3366,27 @@ nsDocumentViewer::StopEmulatingMedium() NS_IMETHODIMP nsDocumentViewer::GetForceCharacterSet(nsACString& aForceCharacterSet) { - aForceCharacterSet = mForceCharacterSet; + auto encoding = nsDocumentViewer::GetForceCharset(); + if (encoding) { + encoding->Name(aForceCharacterSet); + } else { + aForceCharacterSet.Truncate(); + } return NS_OK; } +/* [noscript,notxpcom] Encoding getForceCharset (); */ +NS_IMETHODIMP_(const Encoding *) +nsDocumentViewer::GetForceCharset() +{ + return mForceCharacterSet; +} + static void SetChildForceCharacterSet(nsIContentViewer* aChild, void* aClosure) { - const nsACString* charset = static_cast(aClosure); - aChild->SetForceCharacterSet(*charset); + auto encoding = static_cast(aClosure); + aChild->SetForceCharset(encoding); } NS_IMETHODIMP @@ -3391,29 +3405,42 @@ nsDocumentViewer::SetForceCharacterSet(const nsACString& aForceCharacterSet) return NS_ERROR_INVALID_ARG; } } - if (encoding) { - encoding->Name(mForceCharacterSet); - } else { - mForceCharacterSet.Truncate(); - } - // now set the force char set on all children of mContainer - CallChildren(SetChildForceCharacterSet, (void*) &aForceCharacterSet); + nsDocumentViewer::SetForceCharset(encoding); return NS_OK; } +/* [noscript,notxpcom] void setForceCharset (in Encoding aEncoding); */ +NS_IMETHODIMP_(void) +nsDocumentViewer::SetForceCharset(const Encoding *aEncoding) +{ + mForceCharacterSet = aEncoding; + // now set the force char set on all children of mContainer + CallChildren(SetChildForceCharacterSet, (void*) aEncoding); +} + NS_IMETHODIMP nsDocumentViewer::GetHintCharacterSet(nsACString& aHintCharacterSet) { - - if(kCharsetUninitialized == mHintCharsetSource) { - aHintCharacterSet.Truncate(); + auto encoding = nsDocumentViewer::GetHintCharset(); + if (encoding) { + encoding->Name(aHintCharacterSet); } else { - aHintCharacterSet = mHintCharset; - // this can't possibly be right. we can't set a value just because somebody got a related value! - //mHintCharsetSource = kCharsetUninitialized; + aHintCharacterSet.Truncate(); } return NS_OK; } +/* [noscript,notxpcom] Encoding getHintCharset (); */ +NS_IMETHODIMP_(const Encoding *) +nsDocumentViewer::GetHintCharset() +{ + if(kCharsetUninitialized == mHintCharsetSource) { + return nullptr; + } + // this can't possibly be right. we can't set a value just because somebody got a related value! + //mHintCharsetSource = kCharsetUninitialized; + return mHintCharset; +} + NS_IMETHODIMP nsDocumentViewer::GetHintCharacterSetSource(int32_t *aHintCharacterSetSource) { NS_ENSURE_ARG_POINTER(aHintCharacterSetSource); @@ -3441,8 +3468,8 @@ nsDocumentViewer::SetHintCharacterSetSource(int32_t aHintCharacterSetSource) static void SetChildHintCharacterSet(nsIContentViewer* aChild, void* aClosure) { - const nsACString* charset = static_cast(aClosure); - aChild->SetHintCharacterSet(*charset); + auto encoding = static_cast(aClosure); + aChild->SetHintCharset(encoding); } NS_IMETHODIMP @@ -3461,16 +3488,19 @@ nsDocumentViewer::SetHintCharacterSet(const nsACString& aHintCharacterSet) return NS_ERROR_INVALID_ARG; } } - if (encoding) { - encoding->Name(mHintCharset); - } else { - mHintCharset.Truncate(); - } - // now set the hint char set on all children of mContainer - CallChildren(SetChildHintCharacterSet, (void*) &aHintCharacterSet); + nsDocumentViewer::SetHintCharset(encoding); return NS_OK; } +/* [noscript,notxpcom] void setHintCharset (in Encoding aEncoding); */ +NS_IMETHODIMP_(void) +nsDocumentViewer::SetHintCharset(const Encoding *aEncoding) +{ + mHintCharset = aEncoding; + // now set the hint char set on all children of mContainer + CallChildren(SetChildHintCharacterSet, (void*) aEncoding); +} + static void AppendChildSubtree(nsIContentViewer* aChild, void* aClosure) {