Bug 1373984 - Turn nsIDocument::mCharacterSet into mozilla::NotNull<const mozilla::Encoding*>. r=hsivonen

MozReview-Commit-ID: GF0YXDwfA14

--HG--
extra : rebase_source : fdae0046f882d47fb539a7f882364e5c5caafdcd
extra : source : 49249788c0dee331ac2989dc39f0505d965a7bd8
This commit is contained in:
Masatoshi Kimura 2017-06-18 20:37:50 +09:00
Родитель acba8482a8
Коммит b515c9c804
81 изменённых файлов: 584 добавлений и 450 удалений

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

@ -281,7 +281,7 @@ XULLinkAccessible::AnchorURIAt(uint32_t aAnchorIndex)
nsCOMPtr<nsIURI> anchorURI;
NS_NewURI(getter_AddRefs(anchorURI), href,
document->GetDocumentCharacterSet().get(),
document->GetDocumentCharacterSet(),
baseURI);
return anchorURI.forget();

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

@ -374,14 +374,15 @@ ForEachPing(nsIContent* aContent, ForEachPingCallback aCallback, void* aClosure)
}
nsIDocument* doc = aContent->OwnerDoc();
nsAutoCString charset;
doc->GetDocumentCharacterSet()->Name(charset);
nsWhitespaceTokenizer tokenizer(value);
while (tokenizer.hasMoreTokens()) {
nsCOMPtr<nsIURI> uri, baseURI = aContent->GetBaseURI();
ios->NewURI(NS_ConvertUTF16toUTF8(tokenizer.nextToken()),
doc->GetDocumentCharacterSet().get(),
baseURI, getter_AddRefs(uri));
charset.get(), baseURI, getter_AddRefs(uri));
// if we can't generate a valid URI, then there is nothing to do
if (!uri) {
continue;
@ -832,6 +833,8 @@ nsDocShell::nsDocShell()
, mDefaultLoadFlags(nsIRequest::LOAD_NORMAL)
, mFrameType(FRAME_TYPE_REGULAR)
, mPrivateBrowsingId(0)
, mForcedCharset(nullptr)
, mParentCharset(nullptr)
, mParentCharsetSource(0)
, mJSRunToCompletionDepth(0)
, mTouchEventsOverride(nsIDocShell::TOUCHEVENTS_OVERRIDE_NONE)
@ -2030,7 +2033,7 @@ nsDocShell::GetCharset(nsACString& aCharset)
NS_ENSURE_TRUE(presShell, NS_ERROR_FAILURE);
nsIDocument* doc = presShell->GetDocument();
NS_ENSURE_TRUE(doc, NS_ERROR_FAILURE);
aCharset = doc->GetDocumentCharacterSet();
doc->GetDocumentCharacterSet()->Name(aCharset);
return NS_OK;
}
@ -2116,7 +2119,7 @@ NS_IMETHODIMP
nsDocShell::SetForcedCharset(const nsACString& aCharset)
{
if (aCharset.IsEmpty()) {
mForcedCharset.Truncate();
mForcedCharset = nullptr;
return NS_OK;
}
const Encoding* encoding = Encoding::ForLabel(aCharset);
@ -2128,19 +2131,19 @@ nsDocShell::SetForcedCharset(const nsACString& aCharset)
// Reject XSS hazards
return NS_ERROR_INVALID_ARG;
}
encoding->Name(mForcedCharset);
mForcedCharset = encoding;
return NS_OK;
}
NS_IMETHODIMP
nsDocShell::GetForcedCharset(nsACString& aResult)
{
aResult = mForcedCharset;
mForcedCharset->Name(aResult);
return NS_OK;
}
void
nsDocShell::SetParentCharset(const nsACString& aCharset,
nsDocShell::SetParentCharset(const Encoding*& aCharset,
int32_t aCharsetSource,
nsIPrincipal* aPrincipal)
{
@ -2150,7 +2153,7 @@ nsDocShell::SetParentCharset(const nsACString& aCharset,
}
void
nsDocShell::GetParentCharset(nsACString& aCharset,
nsDocShell::GetParentCharset(const Encoding*& aCharset,
int32_t* aCharsetSource,
nsIPrincipal** aPrincipal)
{
@ -4192,7 +4195,7 @@ nsDocShell::AddChild(nsIDocShellTreeItem* aChild)
// the actual source charset, which is what we're trying to
// expose here.
const nsACString& parentCS = doc->GetDocumentCharacterSet();
const Encoding* parentCS = doc->GetDocumentCharacterSet();
int32_t charsetSource = doc->GetDocumentCharacterSetSource();
// set the child's parentCharset
childAsDocShell->SetParentCharset(parentCS,
@ -11621,7 +11624,8 @@ nsDocShell::ScrollToAnchor(bool aCurHasRef, bool aNewHasRef,
NS_ENSURE_TRUE(mContentViewer, NS_ERROR_FAILURE);
nsIDocument* doc = mContentViewer->GetDocument();
NS_ENSURE_TRUE(doc, NS_ERROR_FAILURE);
const nsACString& charset = doc->GetDocumentCharacterSet();
nsAutoCString charset;
doc->GetDocumentCharacterSet()->Name(charset);
nsCOMPtr<nsITextToSubURI> textToSubURI =
do_GetService(NS_ITEXTTOSUBURI_CONTRACTID, &rv);

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

@ -62,6 +62,7 @@
#include "nsIDeprecationWarner.h"
namespace mozilla {
class Encoding;
enum class TaskCategory;
namespace dom {
class EventTarget;
@ -155,6 +156,7 @@ class nsDocShell final
{
friend class nsDSURIContentListener;
friend class FramingChecker;
using Encoding = mozilla::Encoding;
public:
MOZ_DECLARE_WEAKREFERENCE_TYPENAME(nsDocShell)
@ -274,6 +276,8 @@ public:
}
bool InFrameSwap();
const Encoding* GetForcedCharset() { return mForcedCharset; }
private:
bool CanSetOriginAttributes();
@ -1036,8 +1040,8 @@ protected:
nsString mInterceptedDocumentId;
private:
nsCString mForcedCharset;
nsCString mParentCharset;
const Encoding* mForcedCharset;
const Encoding* mParentCharset;
int32_t mParentCharsetSource;
nsCOMPtr<nsIPrincipal> mParentCharsetPrincipal;
nsTObserverArray<nsWeakPtr> mPrivacyObservers;

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

@ -11,8 +11,12 @@
%{ C++
#include "js/TypeDecls.h"
#include "mozilla/Maybe.h"
#include "mozilla/NotNull.h"
class nsPresContext;
class nsIPresShell;
namespace mozilla {
class Encoding;
}
%}
/**
@ -22,6 +26,7 @@ class nsIPresShell;
[ptr] native nsPresContext(nsPresContext);
[ptr] native nsIPresShell(nsIPresShell);
[ref] native MaybeURI(mozilla::Maybe<nsCOMPtr<nsIURI>>);
[ref] native Encoding(const mozilla::Encoding*);
interface nsIURI;
interface nsIChannel;
@ -713,11 +718,11 @@ interface nsIDocShell : nsIDocShellTreeItem
* In a child docshell, this is the charset of the parent docshell
*/
[noscript, notxpcom, nostdcall] void setParentCharset(
in ACString parentCharset,
in Encoding parentCharset,
in int32_t parentCharsetSource,
in nsIPrincipal parentCharsetPrincipal);
[noscript, notxpcom, nostdcall] void getParentCharset(
out ACString parentCharset,
out Encoding parentCharset,
out int32_t parentCharsetSource,
out nsIPrincipal parentCharsetPrincipal);

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

@ -429,7 +429,7 @@ nsIContent::GetBaseURI(bool aTryUseXHRDocBaseURI) const
for (uint32_t i = baseAttrs.Length() - 1; i != uint32_t(-1); --i) {
nsCOMPtr<nsIURI> newBase;
nsresult rv = NS_NewURI(getter_AddRefs(newBase), baseAttrs[i],
doc->GetDocumentCharacterSet().get(), base);
doc->GetDocumentCharacterSet(), base);
// Do a security check, almost the same as nsDocument::SetBaseURL()
// Only need to do this on the final uri
if (NS_SUCCEEDED(rv) && i == 0) {

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

@ -39,18 +39,6 @@
namespace mozilla {
namespace dom {
static nsresult
GetDocumentCharacterSetForURI(const nsAString& aHref, nsACString& aCharset)
{
aCharset.Truncate();
if (nsIDocument* doc = GetEntryDocument()) {
aCharset = doc->GetDocumentCharacterSet();
}
return NS_OK;
}
Location::Location(nsPIDOMWindowInner* aWindow, nsIDocShell *aDocShell)
: mInnerWindow(aWindow)
{
@ -506,11 +494,12 @@ Location::SetHrefWithBase(const nsAString& aHref, nsIURI* aBase,
nsCOMPtr<nsIDocShell> docShell(do_QueryReferent(mDocShell));
nsAutoCString docCharset;
if (NS_SUCCEEDED(GetDocumentCharacterSetForURI(aHref, docCharset)))
result = NS_NewURI(getter_AddRefs(newUri), aHref, docCharset.get(), aBase);
else
if (nsIDocument* doc = GetEntryDocument()) {
result = NS_NewURI(getter_AddRefs(newUri), aHref,
doc->GetDocumentCharacterSet(), aBase);
} else {
result = NS_NewURI(getter_AddRefs(newUri), aHref, nullptr, aBase);
}
if (newUri) {
/* Check with the scriptContext if it is currently processing a script tag.

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

@ -845,10 +845,9 @@ nsContentSink::PrefetchHref(const nsAString &aHref,
nsCOMPtr<nsIPrefetchService> prefetchService(do_GetService(NS_PREFETCHSERVICE_CONTRACTID));
if (prefetchService) {
// construct URI using document charset
const nsACString &charset = mDocument->GetDocumentCharacterSet();
auto encoding = mDocument->GetDocumentCharacterSet();
nsCOMPtr<nsIURI> uri;
NS_NewURI(getter_AddRefs(uri), aHref,
charset.IsEmpty() ? nullptr : PromiseFlatCString(charset).get(),
NS_NewURI(getter_AddRefs(uri), aHref, encoding,
mDocument->GetDocBaseURI());
if (uri) {
nsCOMPtr<nsIDOMNode> domNode = do_QueryInterface(aSource);
@ -892,10 +891,9 @@ void
nsContentSink::Preconnect(const nsAString& aHref, const nsAString& aCrossOrigin)
{
// construct URI using document charset
const nsACString& charset = mDocument->GetDocumentCharacterSet();
auto encoding = mDocument->GetDocumentCharacterSet();
nsCOMPtr<nsIURI> uri;
NS_NewURI(getter_AddRefs(uri), aHref,
charset.IsEmpty() ? nullptr : PromiseFlatCString(charset).get(),
NS_NewURI(getter_AddRefs(uri), aHref, encoding,
mDocument->GetDocBaseURI());
if (uri && mDocument) {

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

@ -3158,9 +3158,12 @@ nsContentUtils::NewURIWithDocumentCharset(nsIURI** aResult,
nsIDocument* aDocument,
nsIURI* aBaseURI)
{
return NS_NewURI(aResult, aSpec,
aDocument ? aDocument->GetDocumentCharacterSet().get() : nullptr,
aBaseURI, sIOService);
if (aDocument) {
return NS_NewURI(aResult, aSpec,
aDocument->GetDocumentCharacterSet(),
aBaseURI, sIOService);
}
return NS_NewURI(aResult, aSpec, nullptr, aBaseURI, sIOService);
}
// static

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

@ -6,6 +6,7 @@
#include "nsDOMSerializer.h"
#include "mozilla/Encoding.h"
#include "nsIDocument.h"
#include "nsIDocumentEncoder.h"
#include "nsIDOMDocument.h"
@ -71,7 +72,7 @@ SetUpEncoder(nsIDOMNode *aRoot, const nsACString& aCharset,
if (charset.IsEmpty()) {
nsCOMPtr<nsIDocument> doc = do_QueryInterface(domDoc);
NS_ASSERTION(doc, "Need a document");
charset = doc->GetDocumentCharacterSet();
doc->GetDocumentCharacterSet()->Name(charset);
}
rv = encoder->SetCharset(charset);
if (NS_FAILED(rv))

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

@ -1294,7 +1294,7 @@ nsIDocument::nsIDocument()
mBlockAllMixedContentPreloads(false),
mUpgradeInsecureRequests(false),
mUpgradeInsecurePreloads(false),
mCharacterSet(NS_LITERAL_CSTRING("windows-1252")),
mCharacterSet(WINDOWS_1252_ENCODING),
mCharacterSetSource(0),
mParentDocument(nullptr),
mCachedRootElement(nullptr),
@ -3678,13 +3678,14 @@ nsDocument::GetBaseTarget(nsAString &aBaseTarget)
}
void
nsDocument::SetDocumentCharacterSet(const nsACString& aCharSetID)
nsDocument::SetDocumentCharacterSet(NotNull<const Encoding*> aEncoding)
{
// XXX it would be a good idea to assert the sanity of the argument,
// but before we figure out what to do about non-Encoding Standard
// encodings in the charset menu and in mailnews, assertions are futile.
if (!mCharacterSet.Equals(aCharSetID)) {
mCharacterSet = aCharSetID;
if (mCharacterSet != aEncoding) {
mCharacterSet = aEncoding;
nsAutoCString charsetID;
aEncoding->Name(charsetID);
NS_ConvertASCIItoUTF16 charset16(charsetID);
int32_t n = mCharSetObservers.Length();
@ -3692,7 +3693,7 @@ nsDocument::SetDocumentCharacterSet(const nsACString& aCharSetID)
nsIObserver* observer = mCharSetObservers.ElementAt(i);
observer->Observe(static_cast<nsIDocument *>(this), "charset",
NS_ConvertASCIItoUTF16(aCharSetID).get());
charset16.get());
}
}
}
@ -3849,7 +3850,7 @@ nsDocument::SetHeaderData(nsIAtom* aHeaderField, const nsAString& aData)
void
nsDocument::TryChannelCharset(nsIChannel *aChannel,
int32_t& aCharsetSource,
nsACString& aCharset,
NotNull<const Encoding*>& aEncoding,
nsHtml5TreeOpExecutor* aExecutor)
{
if (aChannel) {
@ -3858,7 +3859,7 @@ nsDocument::TryChannelCharset(nsIChannel *aChannel,
if (NS_SUCCEEDED(rv)) {
const Encoding* preferred = Encoding::ForLabel(charsetVal);
if (preferred) {
preferred->Name(aCharset);
aEncoding = WrapNotNull(preferred);
aCharsetSource = kCharsetFromChannel;
return;
} else if (aExecutor && !charsetVal.IsEmpty()) {
@ -6489,7 +6490,9 @@ nsDocument::GetCharacterSet(nsAString& aCharacterSet)
void
nsIDocument::GetCharacterSet(nsAString& aCharacterSet) const
{
CopyASCIItoUTF16(GetDocumentCharacterSet(), aCharacterSet);
nsAutoCString charset;
GetDocumentCharacterSet()->Name(charset);
CopyASCIItoUTF16(charset, aCharacterSet);
}
NS_IMETHODIMP
@ -6586,9 +6589,7 @@ nsIDocument::LoadBindingDocument(const nsAString& aURI,
ErrorResult& rv)
{
nsCOMPtr<nsIURI> uri;
rv = NS_NewURI(getter_AddRefs(uri), aURI,
mCharacterSet.get(),
GetDocBaseURI());
rv = NS_NewURI(getter_AddRefs(uri), aURI, mCharacterSet, GetDocBaseURI());
if (rv.Failed()) {
return;
}
@ -9901,8 +9902,7 @@ nsDocument::ScrollToRef()
// document's charset.
if (NS_FAILED(rv)) {
const nsACString &docCharset = GetDocumentCharacterSet();
const Encoding* encoding = Encoding::ForName(docCharset);
auto encoding = GetDocumentCharacterSet();
rv = encoding->DecodeWithoutBOMHandling(unescapedRef, ref);

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

@ -424,10 +424,11 @@ public:
virtual void GetBaseTarget(nsAString &aBaseTarget) override;
/**
* Return a standard name for the document's character set. This will
* Set the document's character encoding. This will
* trigger a startDocumentLoad if necessary to answer the question.
*/
virtual void SetDocumentCharacterSet(const nsACString& aCharSetID) override;
virtual void
SetDocumentCharacterSet(NotNull<const Encoding*> aEncoding) override;
/**
* Add an observer that gets notified whenever the charset changes.
@ -1048,7 +1049,7 @@ protected:
void TryChannelCharset(nsIChannel *aChannel,
int32_t& aCharsetSource,
nsACString& aCharset,
NotNull<const Encoding*>& aEncoding,
nsHtml5TreeOpExecutor* aExecutor);
// Call this before the document does something that will unbind all content.

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

@ -272,16 +272,15 @@ nsFrameLoader::LoadFrame()
}
nsCOMPtr<nsIURI> base_uri = mOwnerContent->GetBaseURI();
const nsCString& doc_charset = doc->GetDocumentCharacterSet();
const char *charset = doc_charset.IsEmpty() ? nullptr : doc_charset.get();
auto encoding = doc->GetDocumentCharacterSet();
nsCOMPtr<nsIURI> uri;
nsresult rv = NS_NewURI(getter_AddRefs(uri), src, charset, base_uri);
nsresult rv = NS_NewURI(getter_AddRefs(uri), src, encoding, base_uri);
// If the URI was malformed, try to recover by loading about:blank.
if (rv == NS_ERROR_MALFORMED_URI) {
rv = NS_NewURI(getter_AddRefs(uri), NS_LITERAL_STRING("about:blank"),
charset, base_uri);
encoding, base_uri);
}
if (NS_SUCCEEDED(rv)) {

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

@ -13397,14 +13397,14 @@ nsGlobalWindow::SecurityCheckURL(const char *aURL)
// used to actually kick off the load in nsWindowWatcher.cpp.
nsCOMPtr<nsIDocument> doc = sourceWindow->GetDoc();
nsIURI* baseURI = nullptr;
nsAutoCString charset(NS_LITERAL_CSTRING("UTF-8")); // default to utf-8
auto encoding = UTF_8_ENCODING; // default to utf-8
if (doc) {
baseURI = doc->GetDocBaseURI();
charset = doc->GetDocumentCharacterSet();
encoding = doc->GetDocumentCharacterSet();
}
nsCOMPtr<nsIURI> uri;
nsresult rv = NS_NewURI(getter_AddRefs(uri), nsDependentCString(aURL),
charset.get(), baseURI);
encoding, baseURI);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}

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

@ -34,6 +34,7 @@
#include "mozilla/CORSMode.h"
#include "mozilla/dom/DispatcherTrait.h"
#include "mozilla/LinkedList.h"
#include "mozilla/NotNull.h"
#include "mozilla/SegmentedVector.h"
#include "mozilla/StyleBackendType.h"
#include "mozilla/StyleSheet.h"
@ -104,6 +105,7 @@ struct nsCSSSelectorList;
namespace mozilla {
class AbstractThread;
class CSSStyleSheet;
class Encoding;
class ErrorResult;
class EventStates;
class PendingAnimationTracker;
@ -211,6 +213,10 @@ class nsIDocument : public nsINode,
{
typedef mozilla::dom::GlobalObject GlobalObject;
protected:
using Encoding = mozilla::Encoding;
template <typename T> using NotNull = mozilla::NotNull<T>;
public:
typedef mozilla::net::ReferrerPolicy ReferrerPolicyEnum;
typedef mozilla::dom::Element Element;
@ -504,16 +510,15 @@ public:
/**
* Return a standard name for the document's character set.
*/
const nsCString& GetDocumentCharacterSet() const
NotNull<const Encoding*> GetDocumentCharacterSet() const
{
return mCharacterSet;
}
/**
* Set the document's character encoding. |aCharSetID| should be canonical.
* That is, callers are responsible for the charset alias resolution.
* Set the document's character encoding.
*/
virtual void SetDocumentCharacterSet(const nsACString& aCharSetID) = 0;
virtual void SetDocumentCharacterSet(NotNull<const Encoding*> aEncoding) = 0;
int32_t GetDocumentCharacterSetSource() const
{
@ -3061,7 +3066,7 @@ protected:
mozilla::WeakPtr<nsDocShell> mDocumentContainer;
nsCString mCharacterSet;
NotNull<const Encoding*> mCharacterSet;
int32_t mCharacterSetSource;
// This is just a weak pointer; the parent document owns its children.

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

@ -1128,12 +1128,12 @@ nsImageLoadingContent::StringToURI(const nsAString& aSpec,
nsCOMPtr<nsIURI> baseURL = thisContent->GetBaseURI();
// (2) Get the charset
const nsCString& charset = aDocument->GetDocumentCharacterSet();
auto encoding = aDocument->GetDocumentCharacterSet();
// (3) Construct the silly thing
return NS_NewURI(aURI,
aSpec,
charset.IsEmpty() ? nullptr : charset.get(),
encoding,
baseURL,
nsContentUtils::GetIOService());
}

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

@ -33,8 +33,10 @@ nsReferencedElement::Reset(nsIContent* aFromContent, nsIURI* aURI,
nsAutoCString charset;
aURI->GetOriginCharset(charset);
const Encoding* encoding = charset.IsEmpty() ?
UTF_8_ENCODING : Encoding::ForName(charset);
auto encoding = Encoding::ForLabelNoReplacement(charset);
if (!encoding) {
encoding = UTF_8_ENCODING;
}
nsAutoString ref;
nsresult rv = encoding->DecodeWithoutBOMHandling(refPart, ref);
if (NS_FAILED(rv) || ref.IsEmpty()) {

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

@ -36,34 +36,33 @@ FallbackEncoding* FallbackEncoding::sInstance = nullptr;
bool FallbackEncoding::sGuessFallbackFromTopLevelDomain = true;
FallbackEncoding::FallbackEncoding()
: mFallback(nullptr)
{
MOZ_ASSERT(!FallbackEncoding::sInstance,
"Singleton already exists.");
}
void
FallbackEncoding::Get(nsACString& aFallback)
NotNull<const Encoding*>
FallbackEncoding::Get()
{
if (!mFallback.IsEmpty()) {
aFallback = mFallback;
return;
if (mFallback) {
return WrapNotNull(mFallback);
}
const nsAdoptingCString& override =
Preferences::GetCString("intl.charset.fallback.override");
// Don't let the user break things by setting the override to unreasonable
// values via about:config
const Encoding* encoding = Encoding::ForLabel(override);
auto encoding = Encoding::ForLabel(override);
if (!encoding || !encoding->IsAsciiCompatible() ||
encoding == UTF_8_ENCODING) {
mFallback.Truncate();
mFallback = nullptr;
} else {
encoding->Name(mFallback);
mFallback = encoding;
}
if (!mFallback.IsEmpty()) {
aFallback = mFallback;
return;
if (mFallback) {
return WrapNotNull(mFallback);
}
nsAutoCString locale;
@ -80,9 +79,8 @@ FallbackEncoding::Get(nsACString& aFallback)
locale.EqualsLiteral("zh-hk") ||
locale.EqualsLiteral("zh-mo") ||
locale.EqualsLiteral("zh-hant")) {
mFallback.AssignLiteral("Big5");
aFallback = mFallback;
return;
mFallback = BIG5_ENCODING;
return WrapNotNull(mFallback);
}
// Throw away regions and other variants to accommodate weird stuff seen
@ -92,20 +90,23 @@ FallbackEncoding::Get(nsACString& aFallback)
locale.Truncate(index);
}
nsAutoCString fallback;
if (NS_FAILED(nsUConvPropertySearch::SearchPropertyValue(
localesFallbacks, ArrayLength(localesFallbacks), locale, mFallback))) {
mFallback.AssignLiteral("windows-1252");
localesFallbacks, ArrayLength(localesFallbacks), locale, fallback))) {
mFallback = WINDOWS_1252_ENCODING;
} else {
mFallback = Encoding::ForName(fallback);
}
aFallback = mFallback;
return WrapNotNull(mFallback);
}
void
FallbackEncoding::FromLocale(nsACString& aFallback)
NotNull<const Encoding*>
FallbackEncoding::FromLocale()
{
MOZ_ASSERT(FallbackEncoding::sInstance,
"Using uninitialized fallback cache.");
FallbackEncoding::sInstance->Get(aFallback);
return FallbackEncoding::sInstance->Get();
}
// PrefChangedFunc
@ -169,14 +170,15 @@ FallbackEncoding::IsParticipatingTopLevelDomain(const nsACString& aTLD)
dummy));
}
void
FallbackEncoding::FromTopLevelDomain(const nsACString& aTLD,
nsACString& aFallback)
NotNull<const Encoding*>
FallbackEncoding::FromTopLevelDomain(const nsACString& aTLD)
{
nsAutoCString fallback;
if (NS_FAILED(nsUConvPropertySearch::SearchPropertyValue(
domainsFallbacks, ArrayLength(domainsFallbacks), aTLD, aFallback))) {
aFallback.AssignLiteral("windows-1252");
domainsFallbacks, ArrayLength(domainsFallbacks), aTLD, fallback))) {
return WINDOWS_1252_ENCODING;
}
return Encoding::ForName(fallback);
}
} // namespace dom

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

@ -7,10 +7,12 @@
#ifndef mozilla_dom_FallbackEncoding_h_
#define mozilla_dom_FallbackEncoding_h_
#include "mozilla/NotNull.h"
#include "nsIObserver.h"
#include "nsString.h"
namespace mozilla {
class Encoding;
namespace dom {
class FallbackEncoding : public nsIObserver
@ -30,7 +32,7 @@ public:
*
* @param aFallback the outparam for the fallback encoding
*/
static void FromLocale(nsACString& aFallback);
static NotNull<const Encoding*> FromLocale();
/**
* Checks if it is appropriate to call FromTopLevelDomain() for a given TLD.
@ -47,7 +49,7 @@ public:
* @param aTLD the top-level domain (in Punycode)
* @param aFallback the outparam for the fallback encoding
*/
static void FromTopLevelDomain(const nsACString& aTLD, nsACString& aFallback);
static NotNull<const Encoding*> FromTopLevelDomain(const nsACString& aTLD);
// public API ends here!
@ -78,7 +80,7 @@ private:
*/
void Invalidate()
{
mFallback.Truncate();
mFallback = nullptr;
}
static void PrefChanged(const char*, void*);
@ -87,9 +89,9 @@ private:
* Gets the fallback encoding label.
* @param aFallback the fallback encoding
*/
void Get(nsACString& aFallback);
NotNull<const Encoding*> Get();
nsCString mFallback;
const Encoding* mFallback;
};
} // namespace dom

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

@ -887,7 +887,7 @@ GetSubmitEncoding(nsGenericHTMLElement* aForm)
// Get the charset from document
nsIDocument* doc = aForm->GetComposedDoc();
if (doc) {
return Encoding::ForName(doc->GetDocumentCharacterSet());
return doc->GetDocumentCharacterSet();
}
return UTF_8_ENCODING;
}

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

@ -171,17 +171,16 @@ MediaDocument::StartDocumentLoad(const char* aCommand,
// not being able to set the charset is not critical.
NS_ENSURE_TRUE(docShell, NS_OK);
nsAutoCString charset;
const Encoding* encoding;
int32_t source;
nsCOMPtr<nsIPrincipal> principal;
// opening in a new tab
docShell->GetParentCharset(charset, &source, getter_AddRefs(principal));
docShell->GetParentCharset(encoding, &source, getter_AddRefs(principal));
if (!charset.IsEmpty() &&
!charset.EqualsLiteral("UTF-8") &&
if (encoding && encoding != UTF_8_ENCODING &&
NodePrincipal()->Equals(principal)) {
SetDocumentCharacterSetSource(source);
SetDocumentCharacterSet(charset);
SetDocumentCharacterSet(WrapNotNull(encoding));
}
return NS_OK;
@ -299,11 +298,14 @@ MediaDocument::GetFileName(nsAString& aResult, nsIChannel* aChannel)
// window or a new tab, in which case |originCharset| of |nsIURI| is not
// reliable.
if (mCharacterSetSource != kCharsetUninitialized) {
docCharset = mCharacterSet;
mCharacterSet->Name(docCharset);
} else {
// resort to |originCharset|
url->GetOriginCharset(docCharset);
SetDocumentCharacterSet(docCharset);
auto encoding = Encoding::ForLabelNoReplacement(docCharset);
if (encoding) {
SetDocumentCharacterSet(WrapNotNull(encoding));
}
}
nsresult rv;

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

@ -136,7 +136,7 @@ public:
NS_IMETHOD WillResume(void) override;
NS_IMETHOD SetParser(nsParserBase* aParser) override;
virtual void FlushPendingNotifications(FlushType aType) override;
NS_IMETHOD SetDocumentCharset(nsACString& aCharset) override;
virtual void SetDocumentCharset(NotNull<const Encoding*> aEncoding) override;
virtual nsISupports *GetTarget() override;
virtual bool IsScriptExecuting() override;
@ -1087,11 +1087,10 @@ HTMLContentSink::FlushTags()
return mCurrentContext ? mCurrentContext->FlushTags() : NS_OK;
}
NS_IMETHODIMP
HTMLContentSink::SetDocumentCharset(nsACString& aCharset)
void
HTMLContentSink::SetDocumentCharset(NotNull<const Encoding*> aEncoding)
{
MOZ_ASSERT_UNREACHABLE("<meta charset> case doesn't occur with about:blank");
return NS_ERROR_NOT_IMPLEMENTED;
}
nsISupports *

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

@ -164,6 +164,12 @@ IsAsciiCompatible(const nsACString& aPreferredName)
aPreferredName.LowerCaseEqualsLiteral("x-imap4-modified-utf7"));
}
static bool
IsAsciiCompatible(const Encoding* aEncoding)
{
return aEncoding->IsAsciiCompatible() || aEncoding == ISO_2022_JP_ENCODING;
}
nsresult
NS_NewHTMLDocument(nsIDocument** aInstancePtrResult, bool aLoadedAsData)
{
@ -286,7 +292,8 @@ nsHTMLDocument::ResetToURI(nsIURI *aURI, nsILoadGroup *aLoadGroup,
void
nsHTMLDocument::TryHintCharset(nsIContentViewer* aCv,
int32_t& aCharsetSource, nsACString& aCharset)
int32_t& aCharsetSource,
NotNull<const Encoding*>& aEncoding)
{
if (aCv) {
int32_t requestCharsetSource;
@ -297,13 +304,15 @@ nsHTMLDocument::TryHintCharset(nsIContentViewer* aCv,
rv = aCv->GetHintCharacterSet(requestCharset);
aCv->SetHintCharacterSetSource((int32_t)(kCharsetUninitialized));
if(requestCharsetSource <= aCharsetSource)
if (requestCharsetSource <= aCharsetSource)
return;
if(NS_SUCCEEDED(rv) && IsAsciiCompatible(requestCharset)) {
aCharsetSource = requestCharsetSource;
aCharset = requestCharset;
if (NS_SUCCEEDED(rv) && !requestCharset.IsEmpty()) {
auto encoding = Encoding::ForName(requestCharset);
if (IsAsciiCompatible(encoding)) {
aCharsetSource = requestCharsetSource;
aEncoding = encoding;
}
return;
}
}
@ -316,15 +325,15 @@ void
nsHTMLDocument::TryUserForcedCharset(nsIContentViewer* aCv,
nsIDocShell* aDocShell,
int32_t& aCharsetSource,
nsACString& aCharset)
NotNull<const Encoding*>& aEncoding)
{
nsresult rv = NS_OK;
if(kCharsetFromUserForced <= aCharsetSource)
return;
// mCharacterSet not updated yet for channel, so check aCharset, too.
if (WillIgnoreCharsetOverride() || !IsAsciiCompatible(aCharset)) {
// mCharacterSet not updated yet for channel, so check aEncoding, too.
if (WillIgnoreCharsetOverride() || !IsAsciiCompatible(aEncoding)) {
return;
}
@ -337,21 +346,20 @@ nsHTMLDocument::TryUserForcedCharset(nsIContentViewer* aCv,
if(NS_SUCCEEDED(rv) &&
!forceCharsetFromDocShell.IsEmpty() &&
IsAsciiCompatible(forceCharsetFromDocShell)) {
aCharset = forceCharsetFromDocShell;
aEncoding = Encoding::ForName(forceCharsetFromDocShell);
aCharsetSource = kCharsetFromUserForced;
return;
}
if (aDocShell) {
// This is the Character Encoding menu code path in Firefox
nsAutoCString charset;
rv = aDocShell->GetForcedCharset(charset);
auto encoding = nsDocShell::Cast(aDocShell)->GetForcedCharset();
if (NS_SUCCEEDED(rv) && !charset.IsEmpty()) {
if (!IsAsciiCompatible(charset)) {
if (encoding) {
if (!IsAsciiCompatible(encoding)) {
return;
}
aCharset = charset;
aEncoding = WrapNotNull(encoding);
aCharsetSource = kCharsetFromUserForced;
aDocShell->SetForcedCharset(NS_LITERAL_CSTRING(""));
}
@ -361,7 +369,7 @@ nsHTMLDocument::TryUserForcedCharset(nsIContentViewer* aCv,
void
nsHTMLDocument::TryCacheCharset(nsICachingChannel* aCachingChannel,
int32_t& aCharsetSource,
nsACString& aCharset)
NotNull<const Encoding*>& aEncoding)
{
nsresult rv;
@ -389,15 +397,14 @@ nsHTMLDocument::TryCacheCharset(nsICachingChannel* aCachingChannel,
if (!encoding->IsAsciiCompatible() && encoding != ISO_2022_JP_ENCODING) {
return;
}
encoding->Name(cachedCharset);
aCharset = cachedCharset;
aEncoding = WrapNotNull(encoding);
aCharsetSource = kCharsetFromCache;
}
void
nsHTMLDocument::TryParentCharset(nsIDocShell* aDocShell,
int32_t& aCharsetSource,
nsACString& aCharset)
NotNull<const Encoding*>& aEncoding)
{
if (!aDocShell) {
return;
@ -407,22 +414,22 @@ nsHTMLDocument::TryParentCharset(nsIDocShell* aDocShell,
}
int32_t parentSource;
nsAutoCString parentCharset;
const Encoding* parentCharset;
nsCOMPtr<nsIPrincipal> parentPrincipal;
aDocShell->GetParentCharset(parentCharset,
&parentSource,
getter_AddRefs(parentPrincipal));
if (parentCharset.IsEmpty()) {
if (!parentCharset) {
return;
}
if (kCharsetFromParentForced == parentSource ||
kCharsetFromUserForced == parentSource) {
if (WillIgnoreCharsetOverride() ||
!IsAsciiCompatible(aCharset) || // if channel said UTF-16
!IsAsciiCompatible(aEncoding) || // if channel said UTF-16
!IsAsciiCompatible(parentCharset)) {
return;
}
aCharset.Assign(parentCharset);
aEncoding = WrapNotNull(parentCharset);
aCharsetSource = kCharsetFromParentForced;
return;
}
@ -438,13 +445,14 @@ nsHTMLDocument::TryParentCharset(nsIDocShell* aDocShell,
return;
}
aCharset.Assign(parentCharset);
aEncoding = WrapNotNull(parentCharset);
aCharsetSource = kCharsetFromParentFrame;
}
}
void
nsHTMLDocument::TryTLD(int32_t& aCharsetSource, nsACString& aCharset)
nsHTMLDocument::TryTLD(int32_t& aCharsetSource,
NotNull<const Encoding*>& aEncoding)
{
if (aCharsetSource >= kCharsetFromTopLevelDomain) {
return;
@ -500,29 +508,32 @@ nsHTMLDocument::TryTLD(int32_t& aCharsetSource, nsACString& aCharset)
return;
}
aCharsetSource = kCharsetFromTopLevelDomain;
FallbackEncoding::FromTopLevelDomain(tld, aCharset);
aEncoding = FallbackEncoding::FromTopLevelDomain(tld);
}
void
nsHTMLDocument::TryFallback(int32_t& aCharsetSource, nsACString& aCharset)
nsHTMLDocument::TryFallback(int32_t& aCharsetSource,
NotNull<const Encoding*>& aEncoding)
{
if (kCharsetFromFallback <= aCharsetSource)
return;
aCharsetSource = kCharsetFromFallback;
FallbackEncoding::FromLocale(aCharset);
aEncoding = FallbackEncoding::FromLocale();
}
void
nsHTMLDocument::SetDocumentCharacterSet(const nsACString& aCharSetID)
nsHTMLDocument::SetDocumentCharacterSet(NotNull<const Encoding*> aEncoding)
{
nsDocument::SetDocumentCharacterSet(aCharSetID);
nsDocument::SetDocumentCharacterSet(aEncoding);
// Make sure to stash this charset on our channel as needed if it's a wyciwyg
// channel.
nsCOMPtr<nsIWyciwygChannel> wyciwygChannel = do_QueryInterface(mChannel);
if (wyciwygChannel) {
nsAutoCString charset;
aEncoding->Name(charset);
wyciwygChannel->SetCharsetAndSource(GetDocumentCharacterSetSource(),
aCharSetID);
charset);
}
}
@ -671,12 +682,12 @@ nsHTMLDocument::StartDocumentLoad(const char* aCommand,
// These are the charset source and charset for our document
int32_t charsetSource;
nsAutoCString charset;
auto encoding = UTF_8_ENCODING;
// These are the charset source and charset for the parser. This can differ
// from that for the document if the channel is a wyciwyg channel.
int32_t parserCharsetSource;
nsAutoCString parserCharset;
auto parserCharset = UTF_8_ENCODING;
nsCOMPtr<nsIWyciwygChannel> wyciwygChannel;
@ -693,16 +704,13 @@ nsHTMLDocument::StartDocumentLoad(const char* aCommand,
if (forceUtf8) {
charsetSource = kCharsetFromUtf8OnlyMime;
charset.AssignLiteral("UTF-8");
parserCharsetSource = charsetSource;
parserCharset = charset;
} else if (!IsHTMLDocument() || !docShell) { // no docshell for text/html XHR
charsetSource = IsHTMLDocument() ? kCharsetFromFallback
: kCharsetFromDocTypeDefault;
charset.AssignLiteral("UTF-8");
TryChannelCharset(aChannel, charsetSource, charset, executor);
TryChannelCharset(aChannel, charsetSource, encoding, executor);
parserCharset = encoding;
parserCharsetSource = charsetSource;
parserCharset = charset;
} else {
NS_ASSERTION(docShell, "Unexpected null value");
@ -726,24 +734,24 @@ nsHTMLDocument::StartDocumentLoad(const char* aCommand,
// content to a UTF-16 site such that the byte have a dangerous
// interpretation as ASCII and the user can be lured to using the
// charset menu.
TryChannelCharset(aChannel, charsetSource, charset, executor);
TryChannelCharset(aChannel, charsetSource, encoding, executor);
}
TryUserForcedCharset(cv, docShell, charsetSource, charset);
TryUserForcedCharset(cv, docShell, charsetSource, encoding);
TryHintCharset(cv, charsetSource, charset); // XXX mailnews-only
TryParentCharset(docShell, charsetSource, charset);
TryHintCharset(cv, charsetSource, encoding); // XXX mailnews-only
TryParentCharset(docShell, charsetSource, encoding);
if (cachingChan && !urlSpec.IsEmpty()) {
TryCacheCharset(cachingChan, charsetSource, charset);
TryCacheCharset(cachingChan, charsetSource, encoding);
}
TryTLD(charsetSource, charset);
TryFallback(charsetSource, charset);
TryTLD(charsetSource, encoding);
TryFallback(charsetSource, encoding);
if (wyciwygChannel) {
// We know for sure that the parser needs to be using UTF16.
parserCharset = "UTF-16LE";
parserCharset = UTF_16LE_ENCODING;
parserCharsetSource = charsetSource < kCharsetFromChannel ?
kCharsetFromChannel : charsetSource;
@ -752,27 +760,34 @@ nsHTMLDocument::StartDocumentLoad(const char* aCommand,
rv = wyciwygChannel->GetCharsetAndSource(&cachedSource, cachedCharset);
if (NS_SUCCEEDED(rv)) {
if (cachedSource > charsetSource) {
charsetSource = cachedSource;
charset = cachedCharset;
auto cachedEncoding = Encoding::ForLabel(cachedCharset);
if (!cachedEncoding && cachedCharset.EqualsLiteral("replacement")) {
cachedEncoding = REPLACEMENT_ENCODING;
}
if (cachedEncoding) {
charsetSource = cachedSource;
encoding = WrapNotNull(cachedEncoding);
}
}
} else {
// Don't propagate this error.
rv = NS_OK;
}
} else {
parserCharset = charset;
parserCharset = encoding;
parserCharsetSource = charsetSource;
}
}
SetDocumentCharacterSetSource(charsetSource);
SetDocumentCharacterSet(charset);
SetDocumentCharacterSet(encoding);
if (cachingChan) {
NS_ASSERTION(charset == parserCharset,
NS_ASSERTION(encoding == parserCharset,
"How did those end up different here? wyciwyg channels are "
"not nsICachingChannel");
nsAutoCString charset;
encoding->Name(charset);
rv = cachingChan->SetCacheTokenCachedCharset(charset);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "cannot SetMetaDataElement");
rv = NS_OK; // don't propagate error
@ -785,7 +800,7 @@ nsHTMLDocument::StartDocumentLoad(const char* aCommand,
#ifdef DEBUG_charset
printf(" charset = %s source %d\n",
charset.get(), charsetSource);
charset.get(), charsetSource);
#endif
mParser->SetDocumentCharset(parserCharset, parserCharsetSource);
mParser->SetCommand(aCommand);
@ -2416,8 +2431,9 @@ nsHTMLDocument::CreateAndAddWyciwygChannel(void)
// Note: we want to treat this like a "previous document" hint so that,
// e.g. a <meta> tag in the document.write content can override it.
SetDocumentCharacterSetSource(kCharsetFromHintPrevDoc);
mWyciwygChannel->SetCharsetAndSource(kCharsetFromHintPrevDoc,
GetDocumentCharacterSet());
nsAutoCString charset;
GetDocumentCharacterSet()->Name(charset);
mWyciwygChannel->SetCharsetAndSource(kCharsetFromHintPrevDoc, charset);
// Inherit load flags from the original document's channel
channel->SetLoadFlags(mLoadFlags);
@ -3742,7 +3758,8 @@ nsHTMLDocument::WillIgnoreCharsetOverride()
if (mCharacterSetSource >= kCharsetFromByteOrderMark) {
return true;
}
if (!IsAsciiCompatible(mCharacterSet)) {
if (!mCharacterSet->IsAsciiCompatible() &&
mCharacterSet != ISO_2022_JP_ENCODING) {
return true;
}
nsCOMPtr<nsIWyciwygChannel> wyciwyg = do_QueryInterface(mChannel);

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

@ -321,21 +321,24 @@ protected:
static void TryHintCharset(nsIContentViewer* aContentViewer,
int32_t& aCharsetSource,
nsACString& aCharset);
NotNull<const Encoding*>& aEncoding);
void TryUserForcedCharset(nsIContentViewer* aCv,
nsIDocShell* aDocShell,
int32_t& aCharsetSource,
nsACString& aCharset);
NotNull<const Encoding*>& aEncoding);
static void TryCacheCharset(nsICachingChannel* aCachingChannel,
int32_t& aCharsetSource,
nsACString& aCharset);
int32_t& aCharsetSource,
NotNull<const Encoding*>& aEncoding);
void TryParentCharset(nsIDocShell* aDocShell,
int32_t& charsetSource, nsACString& aCharset);
void TryTLD(int32_t& aCharsetSource, nsACString& aCharset);
static void TryFallback(int32_t& aCharsetSource, nsACString& aCharset);
int32_t& charsetSource,
NotNull<const Encoding*>& aEncoding);
void TryTLD(int32_t& aCharsetSource, NotNull<const Encoding*>& aCharset);
static void TryFallback(int32_t& aCharsetSource,
NotNull<const Encoding*>& aEncoding);
// Override so we can munge the charset on our wyciwyg channel as needed.
virtual void SetDocumentCharacterSet(const nsACString& aCharSetID) override;
virtual void
SetDocumentCharacterSet(NotNull<const Encoding*> aEncoding) override;
// Tracks if we are currently processing any document.write calls (either
// implicit or explicit). Note that if a write call writes out something which

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

@ -6,6 +6,7 @@
#include "mozilla/dom/Notification.h"
#include "mozilla/Encoding.h"
#include "mozilla/JSONWriter.h"
#include "mozilla/Move.h"
#include "mozilla/OwningNonNull.h"
@ -1922,7 +1923,7 @@ Notification::ResolveIconAndSoundURL(nsString& iconUrl, nsString& soundUrl)
// the API base URL and no override encoding. So we've to use UTF-8 on
// workers, but for backwards compat keeping it document charset on main
// thread.
const char* charset = "UTF-8";
auto encoding = UTF_8_ENCODING;
if (mWorkerPrivate) {
baseUri = mWorkerPrivate->GetBaseURI();
@ -1930,7 +1931,7 @@ Notification::ResolveIconAndSoundURL(nsString& iconUrl, nsString& soundUrl)
nsIDocument* doc = GetOwner() ? GetOwner()->GetExtantDoc() : nullptr;
if (doc) {
baseUri = doc->GetBaseURI();
charset = doc->GetDocumentCharacterSet().get();
encoding = doc->GetDocumentCharacterSet();
} else {
NS_WARNING("No document found for main thread notification!");
return NS_ERROR_FAILURE;
@ -1940,7 +1941,7 @@ Notification::ResolveIconAndSoundURL(nsString& iconUrl, nsString& soundUrl)
if (baseUri) {
if (mIconUrl.Length() > 0) {
nsCOMPtr<nsIURI> srcUri;
rv = NS_NewURI(getter_AddRefs(srcUri), mIconUrl, charset, baseUri);
rv = NS_NewURI(getter_AddRefs(srcUri), mIconUrl, encoding, baseUri);
if (NS_SUCCEEDED(rv)) {
nsAutoCString src;
srcUri->GetSpec(src);
@ -1949,7 +1950,7 @@ Notification::ResolveIconAndSoundURL(nsString& iconUrl, nsString& soundUrl)
}
if (mBehavior.mSoundFile.Length() > 0) {
nsCOMPtr<nsIURI> srcUri;
rv = NS_NewURI(getter_AddRefs(srcUri), mBehavior.mSoundFile, charset, baseUri);
rv = NS_NewURI(getter_AddRefs(srcUri), mBehavior.mSoundFile, encoding, baseUri);
if (NS_SUCCEEDED(rv)) {
nsAutoCString src;
srcUri->GetSpec(src);

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

@ -47,11 +47,13 @@ GetAbsoluteURL(const nsAString& aUrl,
nsAString& aAbsoluteUrl)
{
nsCOMPtr<nsIURI> uri;
nsresult rv = NS_NewURI(getter_AddRefs(uri),
aUrl,
aDocument ? aDocument->GetDocumentCharacterSet().get()
: nullptr,
aBaseUri);
nsresult rv;
if (aDocument) {
rv = NS_NewURI(getter_AddRefs(uri), aUrl,
aDocument->GetDocumentCharacterSet(), aBaseUri);
} else {
rv = NS_NewURI(getter_AddRefs(uri), aUrl, nullptr, aBaseUri);
}
if (NS_FAILED(rv)) {
return rv;

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

@ -228,8 +228,7 @@ ScriptLoadHandler::EnsureDecoder(nsIIncrementalStreamLoader* aLoader,
// Get the charset from the charset of the document.
if (mScriptLoader->mDocument) {
encoding =
Encoding::ForName(mScriptLoader->mDocument->GetDocumentCharacterSet());
encoding = mScriptLoader->mDocument->GetDocumentCharacterSet();
mDecoder = encoding->NewDecoderWithoutBOMHandling();
encoding->Name(oCharset);
return true;

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

@ -2564,7 +2564,7 @@ ScriptLoader::ConvertToUTF16(nsIChannel* aChannel, const uint8_t* aData,
}
if (!unicodeDecoder && aDocument) {
unicodeDecoder = Encoding::ForName(aDocument->GetDocumentCharacterSet())
unicodeDecoder = aDocument->GetDocumentCharacterSet()
->NewDecoderWithoutBOMHandling();
}

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

@ -137,7 +137,7 @@ WebBrowserPersistLocalDocument::GetContentType(nsACString& aContentType)
NS_IMETHODIMP
WebBrowserPersistLocalDocument::GetCharacterSet(nsACString& aCharSet)
{
aCharSet = GetCharacterSet();
GetCharacterSet()->Name(aCharSet);
return NS_OK;
}
@ -236,7 +236,7 @@ WebBrowserPersistLocalDocument::GetHistory()
return history.forget();
}
const nsCString&
NotNull<const Encoding*>
WebBrowserPersistLocalDocument::GetCharacterSet() const
{
return mDocument->GetDocumentCharacterSet();
@ -397,7 +397,7 @@ ResourceReader::OnWalkURI(const nsACString& aURISpec)
rv = NS_NewURI(getter_AddRefs(uri),
aURISpec,
mParent->GetCharacterSet().get(),
mParent->GetCharacterSet(),
mCurrentBaseURI);
NS_ENSURE_SUCCESS(rv, rv);
return OnWalkURI(uri);
@ -552,7 +552,7 @@ ResourceReader::OnWalkDOMNode(nsIDOMNode* aNode)
if (!codebase.IsEmpty()) {
nsCOMPtr<nsIURI> baseURI;
rv = NS_NewURI(getter_AddRefs(baseURI), codebase,
mParent->GetCharacterSet().get(), mCurrentBaseURI);
mParent->GetCharacterSet(), mCurrentBaseURI);
NS_ENSURE_SUCCESS(rv, rv);
if (baseURI) {
mCurrentBaseURI = baseURI;
@ -717,7 +717,7 @@ PersistNodeFixup::FixupURI(nsAString &aURI)
// get the current location of the file (absolutized)
nsCOMPtr<nsIURI> uri;
nsresult rv = NS_NewURI(getter_AddRefs(uri), aURI,
mParent->GetCharacterSet().get(), mCurrentBaseURI);
mParent->GetCharacterSet(), mCurrentBaseURI);
NS_ENSURE_SUCCESS(rv, rv);
nsAutoCString spec;
rv = uri->GetSpec(spec);
@ -804,7 +804,7 @@ PersistNodeFixup::FixupAnchor(nsIDOMNode *aNode)
// Make a new URI to replace the current one
nsCOMPtr<nsIURI> newURI;
rv = NS_NewURI(getter_AddRefs(newURI), oldCValue,
mParent->GetCharacterSet().get(), relativeURI);
mParent->GetCharacterSet(), relativeURI);
if (NS_SUCCEEDED(rv) && newURI) {
newURI->SetUserPass(EmptyCString());
nsAutoCString uriSpec;
@ -1130,7 +1130,7 @@ PersistNodeFixup::FixupNode(nsIDOMNode *aNodeIn,
if (!codebase.IsEmpty()) {
nsCOMPtr<nsIURI> baseURI;
NS_NewURI(getter_AddRefs(baseURI), codebase,
mParent->GetCharacterSet().get(), mCurrentBaseURI);
mParent->GetCharacterSet(), mCurrentBaseURI);
if (baseURI) {
mCurrentBaseURI = baseURI;
}

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

@ -24,7 +24,7 @@ class WebBrowserPersistLocalDocument final
public:
explicit WebBrowserPersistLocalDocument(nsIDocument* aDocument);
const nsCString& GetCharacterSet() const;
NotNull<const Encoding*> GetCharacterSet() const;
uint32_t GetPersistFlags() const;
already_AddRefed<nsIURI> GetBaseURI() const;

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

@ -1632,7 +1632,7 @@ nsXBLPrototypeBinding::ResolveBaseBinding()
mBinding->UnsetAttr(kNameSpaceID_None, nsGkAtoms::display, false);
return NS_NewURI(getter_AddRefs(mBaseBindingURI), value,
doc->GetDocumentCharacterSet().get(),
doc->GetDocumentCharacterSet(),
doc->GetDocBaseURI());
}

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

@ -113,7 +113,7 @@ nsXBLResourceLoader::LoadResources(nsIContent* aBoundElement)
continue;
if (NS_FAILED(NS_NewURI(getter_AddRefs(url), curr->mSrc,
doc->GetDocumentCharacterSet().get(), docURL)))
doc->GetDocumentCharacterSet(), docURL)))
continue;
if (curr->mType == nsGkAtoms::image) {

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

@ -178,6 +178,7 @@ XMLHttpRequestMainThread::sDontWarnAboutSyncXHR = false;
XMLHttpRequestMainThread::XMLHttpRequestMainThread()
: mResponseBodyDecodedPos(0),
mResponseCharset(nullptr),
mResponseType(XMLHttpRequestResponseType::_empty),
mRequestObserver(nullptr),
mState(State::unsent),
@ -492,7 +493,7 @@ XMLHttpRequestMainThread::GetResponseXML(ErrorResult& aRv)
nsresult
XMLHttpRequestMainThread::DetectCharset()
{
mResponseCharset.Truncate();
mResponseCharset = nullptr;
mDecoder = nullptr;
if (mResponseType != XMLHttpRequestResponseType::_empty &&
@ -519,7 +520,7 @@ XMLHttpRequestMainThread::DetectCharset()
encoding = UTF_8_ENCODING;
}
encoding->Name(mResponseCharset);
mResponseCharset = encoding;
mDecoder = encoding->NewDecoderWithBOMRemoval();
return NS_OK;
@ -2431,7 +2432,7 @@ XMLHttpRequestMainThread::MatchCharsetAndDecoderToResponseDocument()
mResponseCharset = mResponseXML->GetDocumentCharacterSet();
TruncateResponseText();
mResponseBodyDecodedPos = 0;
mDecoder = Encoding::ForName(mResponseCharset)->NewDecoderWithBOMRemoval();
mDecoder = mResponseCharset->NewDecoderWithBOMRemoval();
}
}

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

@ -708,7 +708,7 @@ protected:
// part of the surrogate.
mozilla::UniquePtr<mozilla::Decoder> mDecoder;
nsCString mResponseCharset;
const Encoding* mResponseCharset;
void MatchCharsetAndDecoderToResponseDocument();

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

@ -151,7 +151,7 @@ NS_NewDOMDocument(nsIDOMDocument** aInstancePtrResult,
// XMLDocuments and documents "created in memory" get to be UTF-8 by default,
// unlike the legacy HTML mess
doc->SetDocumentCharacterSet(NS_LITERAL_CSTRING("UTF-8"));
doc->SetDocumentCharacterSet(UTF_8_ENCODING);
if (aDoctype) {
nsCOMPtr<nsINode> doctypeAsNode = do_QueryInterface(aDoctype);
@ -322,7 +322,7 @@ XMLDocument::Load(const nsAString& aUrl, CallerType aCallerType,
if (callingDoc) {
baseURI = callingDoc->GetDocBaseURI();
charset = callingDoc->GetDocumentCharacterSet();
callingDoc->GetDocumentCharacterSet()->Name(charset);
}
// Create a new URI
@ -530,8 +530,8 @@ XMLDocument::StartDocumentLoad(const char* aCommand,
int32_t charsetSource = kCharsetFromDocTypeDefault;
nsAutoCString charset(NS_LITERAL_CSTRING("UTF-8"));
TryChannelCharset(aChannel, charsetSource, charset, nullptr);
NotNull<const Encoding*> encoding = UTF_8_ENCODING;
TryChannelCharset(aChannel, charsetSource, encoding, nullptr);
nsCOMPtr<nsIURI> aUrl;
rv = aChannel->GetURI(getter_AddRefs(aUrl));
@ -565,8 +565,8 @@ XMLDocument::StartDocumentLoad(const char* aCommand,
NS_ASSERTION(mChannel, "How can we not have a channel here?");
mChannelIsPending = true;
SetDocumentCharacterSet(charset);
mParser->SetDocumentCharset(charset, charsetSource);
SetDocumentCharacterSet(encoding);
mParser->SetDocumentCharset(encoding, charsetSource);
mParser->SetCommand(aCommand);
mParser->SetContentSink(sink);
mParser->Parse(aUrl, nullptr, (void *)this);

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

@ -114,15 +114,14 @@ XMLStylesheetProcessingInstruction::GetStyleSheetURL(bool* aIsInline)
}
nsIURI *baseURL;
nsAutoCString charset;
nsIDocument *document = OwnerDoc();
baseURL = mOverriddenBaseURI ?
mOverriddenBaseURI.get() :
document->GetDocBaseURI();
charset = document->GetDocumentCharacterSet();
auto encoding = document->GetDocumentCharacterSet();
nsCOMPtr<nsIURI> aURI;
NS_NewURI(getter_AddRefs(aURI), href, charset.get(), baseURL);
NS_NewURI(getter_AddRefs(aURI), href, encoding, baseURL);
return aURI.forget();
}

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

@ -731,14 +731,12 @@ nsXMLContentSink::ProcessStyleLink(nsIContent* aElement,
return rv;
}
NS_IMETHODIMP
nsXMLContentSink::SetDocumentCharset(nsACString& aCharset)
void
nsXMLContentSink::SetDocumentCharset(NotNull<const Encoding*> aEncoding)
{
if (mDocument) {
mDocument->SetDocumentCharacterSet(aCharset);
mDocument->SetDocumentCharacterSet(aEncoding);
}
return NS_OK;
}
nsISupports *

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

@ -70,7 +70,7 @@ public:
NS_IMETHOD WillResume(void) override;
NS_IMETHOD SetParser(nsParserBase* aParser) override;
virtual void FlushPendingNotifications(mozilla::FlushType aType) override;
NS_IMETHOD SetDocumentCharset(nsACString& aCharset) override;
virtual void SetDocumentCharset(NotNull<const Encoding*> aEncoding) override;
virtual nsISupports *GetTarget() override;
virtual bool IsScriptExecuting() override;
virtual void ContinueInterruptedParsingAsync() override;

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

@ -62,7 +62,7 @@ public:
// nsIContentSink
NS_IMETHOD WillBuildModel(nsDTDMode aDTDMode) override;
NS_IMETHOD DidBuildModel(bool aTerminated) override;
NS_IMETHOD SetDocumentCharset(nsACString& aCharset) override;
virtual void SetDocumentCharset(NotNull<const Encoding*> aEncoding) override;
virtual nsISupports* GetTarget() override;
NS_IMETHOD DidProcessATokenImpl();
@ -173,11 +173,11 @@ nsXMLFragmentContentSink::DidBuildModel(bool aTerminated)
return NS_OK;
}
NS_IMETHODIMP
nsXMLFragmentContentSink::SetDocumentCharset(nsACString& aCharset)
void
nsXMLFragmentContentSink::SetDocumentCharset(
NotNull<const Encoding*> aEncoding)
{
NS_NOTREACHED("fragments shouldn't set charset");
return NS_OK;
}
nsISupports *

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

@ -84,7 +84,8 @@ public:
NS_IMETHOD WillResume(void) override { return NS_OK; }
NS_IMETHOD SetParser(nsParserBase* aParser) override { return NS_OK; }
virtual void FlushPendingNotifications(mozilla::FlushType aType) override { }
NS_IMETHOD SetDocumentCharset(nsACString& aCharset) override { return NS_OK; }
virtual void SetDocumentCharset(NotNull<const Encoding*> aEncoding)
override { }
virtual nsISupports *GetTarget() override { return nullptr; }
private:
@ -269,9 +270,7 @@ txStylesheetSink::OnStartRequest(nsIRequest *aRequest, nsISupports *aContext)
encoding = UTF_8_ENCODING;
}
nsAutoCString charset;
encoding->Name(charset);
mParser->SetDocumentCharset(charset, charsetSource);
mParser->SetDocumentCharset(WrapNotNull(encoding), charsetSource);
nsAutoCString contentType;
channel->GetContentType(contentType);

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

@ -163,9 +163,7 @@ txMozillaTextOutput::createResultDocument(nsIDOMDocument* aSourceDocument,
const Encoding* encoding = Encoding::ForLabel(mOutputFormat.mEncoding);
if (encoding) {
mDocument->SetDocumentCharacterSetSource(kCharsetFromOtherComponent);
nsAutoCString canonicalCharset;
encoding->Name(canonicalCharset);
mDocument->SetDocumentCharacterSet(canonicalCharset);
mDocument->SetDocumentCharacterSet(WrapNotNull(encoding));
}
}

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

@ -823,9 +823,7 @@ txMozillaXMLOutput::createResultDocument(const nsAString& aName, int32_t aNsID,
const Encoding* encoding = Encoding::ForLabel(mOutputFormat.mEncoding);
if (encoding) {
mDocument->SetDocumentCharacterSetSource(kCharsetFromOtherComponent);
nsAutoCString canonicalCharset;
encoding->Name(canonicalCharset);
mDocument->SetDocumentCharacterSet(canonicalCharset);
mDocument->SetDocumentCharacterSet(WrapNotNull(encoding));
}
}

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

@ -213,7 +213,7 @@ XULDocument::XULDocument(void)
mHandlingDelayedBroadcasters(false)
{
// Override the default in nsDocument
mCharacterSet.AssignLiteral("UTF-8");
mCharacterSet = UTF_8_ENCODING;
mDefaultElementType = kNameSpaceID_XUL;
mType = eXUL;
@ -2021,7 +2021,7 @@ XULDocument::PrepareToLoadPrototype(nsIURI* aURI, const char* aCommand,
parser->SetCommand(nsCRT::strcmp(aCommand, "view-source") ? eViewNormal :
eViewSource);
parser->SetDocumentCharset(NS_LITERAL_CSTRING("UTF-8"),
parser->SetDocumentCharset(UTF_8_ENCODING,
kCharsetFromDocTypeDefault);
parser->SetContentSink(sink); // grabs a reference to the parser

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

@ -256,15 +256,13 @@ XULContentSinkImpl::SetParser(nsParserBase* aParser)
return NS_OK;
}
NS_IMETHODIMP
XULContentSinkImpl::SetDocumentCharset(nsACString& aCharset)
void
XULContentSinkImpl::SetDocumentCharset(NotNull<const Encoding*> aEncoding)
{
nsCOMPtr<nsIDocument> doc = do_QueryReferent(mDocument);
if (doc) {
doc->SetDocumentCharacterSet(aCharset);
doc->SetDocumentCharacterSet(aEncoding);
}
return NS_OK;
}
nsISupports *

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

@ -41,7 +41,7 @@ public:
NS_IMETHOD WillResume(void) override;
NS_IMETHOD SetParser(nsParserBase* aParser) override;
virtual void FlushPendingNotifications(mozilla::FlushType aType) override { }
NS_IMETHOD SetDocumentCharset(nsACString& aCharset) override;
virtual void SetDocumentCharset(NotNull<const Encoding*> aEncoding) override;
virtual nsISupports *GetTarget() override;
/**

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

@ -6,6 +6,7 @@
#include "mozilla/EditorBase.h"
#include "mozilla/DebugOnly.h" // for DebugOnly
#include "mozilla/Encoding.h" // for Encoding
#include <stdio.h> // for nullptr, stdout
#include <string.h> // for strcmp
@ -1180,7 +1181,7 @@ EditorBase::GetDocumentCharacterSet(nsACString& characterSet)
if (NS_WARN_IF(!document)) {
return NS_ERROR_UNEXPECTED;
}
characterSet = document->GetDocumentCharacterSet();
document->GetDocumentCharacterSet()->Name(characterSet);
return NS_OK;
}
@ -1191,7 +1192,13 @@ EditorBase::SetDocumentCharacterSet(const nsACString& characterSet)
if (NS_WARN_IF(!document)) {
return NS_ERROR_UNEXPECTED;
}
document->SetDocumentCharacterSet(characterSet);
// This method is scriptable, so add-ons could pass in something other
// than a canonical name.
auto encoding = Encoding::ForLabelNoReplacement(characterSet);
if (!encoding) {
return NS_ERROR_INVALID_ARG;
}
document->SetDocumentCharacterSet(WrapNotNull(encoding));
return NS_OK;
}

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

@ -9,6 +9,7 @@
#include "nsIAtom.h"
#include "mozilla/ArrayUtils.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/Encoding.h"
#include "mozilla/intl/OSPreferences.h"
#include "mozilla/ServoBindings.h"
@ -46,11 +47,13 @@ nsLanguageAtomService::LookupLanguage(const nsACString &aLanguage)
}
already_AddRefed<nsIAtom>
nsLanguageAtomService::LookupCharSet(const nsACString& aCharSet)
nsLanguageAtomService::LookupCharSet(NotNull<const Encoding*> aEncoding)
{
nsAutoCString charset;
aEncoding->Name(charset);
nsAutoCString group;
if (NS_FAILED(nsUConvPropertySearch::SearchPropertyValue(
encodingsGroups, ArrayLength(encodingsGroups), aCharSet, group))) {
encodingsGroups, ArrayLength(encodingsGroups), charset, group))) {
return RefPtr<nsIAtom>(nsGkAtoms::Unicode).forget();
}
return NS_Atomize(group);

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

@ -11,17 +11,24 @@
#ifndef nsLanguageAtomService_h_
#define nsLanguageAtomService_h_
#include "mozilla/NotNull.h"
#include "nsCOMPtr.h"
#include "nsIAtom.h"
#include "nsInterfaceHashtable.h"
class nsLanguageAtomService
namespace mozilla {
class Encoding;
}
class nsLanguageAtomService final
{
using Encoding = mozilla::Encoding;
template <typename T> using NotNull = mozilla::NotNull<T>;
public:
static nsLanguageAtomService* GetService();
nsIAtom* LookupLanguage(const nsACString &aLanguage);
already_AddRefed<nsIAtom> LookupCharSet(const nsACString& aCharSet);
already_AddRefed<nsIAtom> LookupCharSet(NotNull<const Encoding*> aCharSet);
nsIAtom* GetLocaleLanguage();
// Returns the language group that the specified language is a part of.

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

@ -7,6 +7,7 @@
#include "mozilla/ArrayUtils.h"
#include "mozilla/DebugOnly.h"
#include "mozilla/Encoding.h"
#include "mozilla/EventDispatcher.h"
#include "mozilla/EventStateManager.h"
@ -120,7 +121,7 @@ class CharSetChangingRunnable : public Runnable
{
public:
CharSetChangingRunnable(nsPresContext* aPresContext,
const nsCString& aCharSet)
NotNull<const Encoding*> aCharSet)
: Runnable("CharSetChangingRunnable"),
mPresContext(aPresContext),
mCharSet(aCharSet)
@ -135,7 +136,7 @@ public:
private:
RefPtr<nsPresContext> mPresContext;
nsCString mCharSet;
NotNull<const Encoding*> mCharSet;
};
} // namespace
@ -196,15 +197,9 @@ nsPresContext::PrefChangedUpdateTimerCallback(nsITimer *aTimer, void *aClosure)
}
static bool
IsVisualCharset(const nsCString& aCharset)
IsVisualCharset(NotNull<const Encoding*> aCharset)
{
if (aCharset.LowerCaseEqualsLiteral("ibm862") // Hebrew
|| aCharset.LowerCaseEqualsLiteral("iso-8859-8") ) { // Hebrew
return true; // visual text type
}
else {
return false; // logical text type
}
return aCharset == ISO_8859_8_ENCODING;
}
nsPresContext::nsPresContext(nsIDocument* aDocument, nsPresContextType aType)
@ -1070,7 +1065,7 @@ nsPresContext::DetachShell()
}
void
nsPresContext::DoChangeCharSet(const nsCString& aCharSet)
nsPresContext::DoChangeCharSet(NotNull<const Encoding*> aCharSet)
{
UpdateCharSet(aCharSet);
mDeviceContext->FlushFontCache();
@ -1078,7 +1073,7 @@ nsPresContext::DoChangeCharSet(const nsCString& aCharSet)
}
void
nsPresContext::UpdateCharSet(const nsCString& aCharSet)
nsPresContext::UpdateCharSet(NotNull<const Encoding*> aCharSet)
{
mLanguage = mLangService->LookupCharSet(aCharSet);
// this will be a language group (or script) code rather than a true language code
@ -1112,8 +1107,9 @@ nsPresContext::Observe(nsISupports* aSubject,
const char16_t* aData)
{
if (!nsCRT::strcmp(aTopic, "charset")) {
auto encoding = Encoding::ForName(NS_LossyConvertUTF16toASCII(aData));
RefPtr<CharSetChangingRunnable> runnable =
new CharSetChangingRunnable(this, NS_LossyConvertUTF16toASCII(aData));
new CharSetChangingRunnable(this, encoding);
return Document()->Dispatch("CharSetChangingRunnable",
TaskCategory::Other,
runnable.forget());

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

@ -9,6 +9,7 @@
#define nsPresContext_h___
#include "mozilla/Attributes.h"
#include "mozilla/NotNull.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/WeakPtr.h"
#include "nsColor.h"
@ -71,6 +72,7 @@ class gfxMissingFontRecorder;
namespace mozilla {
class EffectCompositor;
class Encoding;
class EventStateManager;
class CounterStyleManager;
class RestyleManager;
@ -125,6 +127,8 @@ class nsRootPresContext;
class nsPresContext : public nsIObserver,
public mozilla::SupportsWeakPtr<nsPresContext> {
public:
using Encoding = mozilla::Encoding;
template <typename T> using NotNull = mozilla::NotNull<T>;
typedef mozilla::LangGroupFontPrefs LangGroupFontPrefs;
typedef mozilla::ScrollbarStyles ScrollbarStyles;
typedef mozilla::StaticPresData StaticPresData;
@ -1228,12 +1232,12 @@ protected:
return StaticPresData::Get()->GetFontPrefsForLangHelper(lang, &mLangGroupFontPrefs, aNeedsToCache);
}
void UpdateCharSet(const nsCString& aCharSet);
void UpdateCharSet(NotNull<const Encoding*> aCharSet);
static bool NotifyDidPaintSubdocumentCallback(nsIDocument* aDocument, void* aData);
public:
void DoChangeCharSet(const nsCString& aCharSet);
void DoChangeCharSet(NotNull<const Encoding*> aCharSet);
/**
* Checks for MozAfterPaint listeners on the document

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

@ -3,8 +3,11 @@
/* 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/. */
#include "nsCOMPtr.h"
#include "nsContentDLF.h"
#include "mozilla/Encoding.h"
#include "nsCOMPtr.h"
#include "nsDocShell.h"
#include "nsGenericHTMLElement.h"
#include "nsGkAtoms.h"
@ -327,7 +330,7 @@ nsContentDLF::CreateBlankDocument(nsILoadGroup *aLoadGroup,
// add a nice bow
if (NS_SUCCEEDED(rv)) {
blankDoc->SetDocumentCharacterSetSource(kCharsetFromDocTypeDefault);
blankDoc->SetDocumentCharacterSet(NS_LITERAL_CSTRING("UTF-8"));
blankDoc->SetDocumentCharacterSet(UTF_8_ENCODING);
blankDoc.forget(aDocument);
}

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

@ -11,6 +11,7 @@
#include "gfxContext.h"
#include "gfxUtils.h"
#include "mozilla/DebugOnly.h"
#include "mozilla/Encoding.h"
#include "mozilla/EventStates.h"
#include "mozilla/gfx/2D.h"
#include "mozilla/gfx/Helpers.h"
@ -2248,7 +2249,7 @@ nsImageFrame::GetDocumentCharacterSet(nsACString& aCharset) const
if (mContent) {
NS_ASSERTION(mContent->GetComposedDoc(),
"Frame still alive after content removed from document!");
aCharset = mContent->GetComposedDoc()->GetDocumentCharacterSet();
mContent->GetComposedDoc()->GetDocumentCharacterSet()->Name(aCharset);
}
}

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

@ -761,8 +761,8 @@ SheetLoadData::OnDetermineCharset(nsIUnicharStreamLoader* aLoader,
if (mLoader->mDocument) {
// no useful data on charset. Try the document charset.
aCharset = mLoader->mDocument->GetDocumentCharacterSet();
MOZ_ASSERT(!aCharset.IsEmpty());
auto encoding = mLoader->mDocument->GetDocumentCharacterSet();
encoding->Name(aCharset);
mCharset.Assign(aCharset);
LOG((" Setting from document to: %s", PromiseFlatCString(aCharset).get()));
return NS_OK;

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

@ -7,11 +7,13 @@
// HttpLog.h should generally be included first
#include "HttpLog.h"
#include "nsNetUtil.h"
#include "mozilla/Encoding.h"
#include "mozilla/LoadContext.h"
#include "mozilla/LoadInfo.h"
#include "mozilla/BasePrincipal.h"
#include "mozilla/Telemetry.h"
#include "nsNetUtil.h"
#include "nsCategoryCache.h"
#include "nsContentUtils.h"
#include "nsHashKeys.h"
@ -1535,6 +1537,18 @@ NS_NewURI(nsIURI **result,
return rv;
}
nsresult
NS_NewURI(nsIURI **result,
const nsACString &spec,
NotNull<const Encoding*> encoding,
nsIURI *baseURI /* = nullptr */,
nsIIOService *ioService /* = nullptr */) // pass in nsIIOService to optimize callers
{
nsAutoCString charset;
encoding->Name(charset);
return NS_NewURI(result, spec, charset.get(), baseURI, ioService);
}
nsresult
NS_NewURI(nsIURI **result,
const nsAString &spec,
@ -1545,6 +1559,16 @@ NS_NewURI(nsIURI **result,
return NS_NewURI(result, NS_ConvertUTF16toUTF8(spec), charset, baseURI, ioService);
}
nsresult
NS_NewURI(nsIURI **result,
const nsAString &spec,
NotNull<const Encoding*> encoding,
nsIURI *baseURI /* = nullptr */,
nsIIOService *ioService /* = nullptr */) // pass in nsIIOService to optimize callers
{
return NS_NewURI(result, NS_ConvertUTF16toUTF8(spec), encoding, baseURI, ioService);
}
nsresult
NS_NewURI(nsIURI **result,
const char *spec,

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

@ -15,6 +15,7 @@
#include "nsIRequest.h"
#include "nsILoadInfo.h"
#include "nsIIOService.h"
#include "mozilla/NotNull.h"
#include "mozilla/Services.h"
#include "mozilla/Unused.h"
#include "nsNetCID.h"
@ -51,7 +52,10 @@ class nsIIncrementalStreamLoaderObserver;
class nsIUnicharStreamLoader;
class nsIUnicharStreamLoaderObserver;
namespace mozilla { class OriginAttributes; }
namespace mozilla {
class Encoding;
class OriginAttributes;
}
template <class> class nsCOMPtr;
template <typename> struct already_AddRefed;
@ -69,12 +73,24 @@ nsresult NS_NewURI(nsIURI **result,
nsIURI *baseURI = nullptr,
nsIIOService *ioService = nullptr); // pass in nsIIOService to optimize callers
nsresult NS_NewURI(nsIURI **result,
const nsACString &spec,
mozilla::NotNull<const mozilla::Encoding*> encoding,
nsIURI *baseURI = nullptr,
nsIIOService *ioService = nullptr); // pass in nsIIOService to optimize callers
nsresult NS_NewURI(nsIURI **result,
const nsAString &spec,
const char *charset = nullptr,
nsIURI *baseURI = nullptr,
nsIIOService *ioService = nullptr); // pass in nsIIOService to optimize callers
nsresult NS_NewURI(nsIURI **result,
const nsAString &spec,
mozilla::NotNull<const mozilla::Encoding*> encoding,
nsIURI *baseURI = nullptr,
nsIIOService *ioService = nullptr); // pass in nsIIOService to optimize callers
nsresult NS_NewURI(nsIURI **result,
const char *spec,
nsIURI *baseURI = nullptr,

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

@ -5,17 +5,17 @@
/* This parsing code originally lived in xpfe/components/directory/ - bbaetz */
#include "mozilla/ArrayUtils.h"
#include "prprf.h"
#include "nsDirIndexParser.h"
#include "nsEscape.h"
#include "nsIInputStream.h"
#include "nsCRT.h"
#include "mozilla/ArrayUtils.h"
#include "mozilla/dom/FallbackEncoding.h"
#include "nsITextToSubURI.h"
#include "mozilla/Encoding.h"
#include "prprf.h"
#include "nsCRT.h"
#include "nsEscape.h"
#include "nsIDirIndex.h"
#include "nsIInputStream.h"
#include "nsITextToSubURI.h"
#include "nsServiceManagerUtils.h"
using namespace mozilla;
@ -33,7 +33,8 @@ nsDirIndexParser::Init() {
mLineStart = 0;
mHasDescription = false;
mFormat[0] = -1;
mozilla::dom::FallbackEncoding::FromLocale(mEncoding);
auto encoding = mozilla::dom::FallbackEncoding::FromLocale();
encoding->Name(mEncoding);
nsresult rv;
// XXX not threadsafe

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

@ -49,11 +49,12 @@ nsHtml5DocumentBuilder::MarkAsBroken(nsresult aReason)
}
void
nsHtml5DocumentBuilder::SetDocumentCharsetAndSource(nsACString& aCharset, int32_t aCharsetSource)
nsHtml5DocumentBuilder::SetDocumentCharsetAndSource(NotNull<const Encoding*> aEncoding,
int32_t aCharsetSource)
{
if (mDocument) {
mDocument->SetDocumentCharacterSetSource(aCharsetSource);
mDocument->SetDocumentCharacterSet(aCharset);
mDocument->SetDocumentCharacterSet(aEncoding);
}
}

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

@ -23,6 +23,8 @@ enum eHtml5FlushState {
class nsHtml5DocumentBuilder : public nsContentSink
{
using Encoding = mozilla::Encoding;
template <typename T> using NotNull = mozilla::NotNull<T>;
public:
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(nsHtml5DocumentBuilder,
nsContentSink)
@ -87,7 +89,8 @@ public:
return mFlushState == eInDocUpdate;
}
void SetDocumentCharsetAndSource(nsACString& aCharset, int32_t aCharsetSource);
void SetDocumentCharsetAndSource(NotNull<const Encoding*> aEncoding,
int32_t aCharsetSource);
/**
* Sets up style sheet load / parse

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

@ -77,6 +77,7 @@ nsHtml5MetaScanner::nsHtml5MetaScanner(nsHtml5TreeBuilder* tb)
, charset(nullptr)
, httpEquivState(HTTP_EQUIV_NOT_SEEN)
, treeBuilder(tb)
, mEncoding(nullptr)
{
MOZ_COUNT_CTOR(nsHtml5MetaScanner);
}

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

@ -6,13 +6,13 @@
#include "mozilla/Encoding.h"
void
nsHtml5MetaScanner::sniff(nsHtml5ByteReadable* bytes, nsACString& charset)
const Encoding*
nsHtml5MetaScanner::sniff(nsHtml5ByteReadable* bytes)
{
readable = bytes;
stateLoop(stateSave);
readable = nullptr;
charset.Assign(mCharset);
return mEncoding;
}
bool
@ -25,20 +25,20 @@ nsHtml5MetaScanner::tryCharset(nsHtml5String charset)
nsString charset16; // Not Auto, because using it to hold nsStringBuffer*
charset.ToString(charset16);
CopyUTF16toUTF8(charset16, label);
const mozilla::Encoding* encoding = mozilla::Encoding::ForLabel(label);
const Encoding* encoding = Encoding::ForLabel(label);
if (!encoding) {
return false;
}
if (encoding == UTF_16BE_ENCODING ||
encoding == UTF_16LE_ENCODING) {
mCharset.AssignLiteral("UTF-8");
mEncoding = UTF_8_ENCODING;
return true;
}
if (encoding == X_USER_DEFINED_ENCODING) {
// WebKit/Blink hack for Indian and Armenian legacy sites
mCharset.AssignLiteral("windows-1252");
mEncoding = WINDOWS_1252_ENCODING;
return true;
}
encoding->Name(mCharset);
mEncoding = encoding;
return true;
}

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

@ -1,12 +1,12 @@
/* 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/. */
using Encoding = mozilla::Encoding;
private:
nsCString mCharset;
const Encoding* mEncoding;
inline int32_t read()
{
return readable->read();
}
public:
void sniff(nsHtml5ByteReadable* bytes, nsACString& charset);
const Encoding* sniff(nsHtml5ByteReadable* bytes);

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

@ -99,19 +99,15 @@ nsHtml5Parser::SetCommand(eParserCommands aParserCommand)
"Parser command was not eViewNormal.");
}
NS_IMETHODIMP_(void)
nsHtml5Parser::SetDocumentCharset(const nsACString& aCharset,
void
nsHtml5Parser::SetDocumentCharset(NotNull<const Encoding*> aEncoding,
int32_t aCharsetSource)
{
NS_PRECONDITION(!mExecutor->HasStarted(),
"Document charset set too late.");
NS_PRECONDITION(GetStreamParser(), "Setting charset on a script-only parser.");
nsAutoCString trimmed;
trimmed.Assign(aCharset);
trimmed.Trim(" \t\r\n\f");
GetStreamParser()->SetDocumentCharset(trimmed, aCharsetSource);
mExecutor->SetDocumentCharsetAndSource(trimmed,
aCharsetSource);
GetStreamParser()->SetDocumentCharset(aEncoding, aCharsetSource);
mExecutor->SetDocumentCharsetAndSource(aEncoding, aCharsetSource);
}
NS_IMETHODIMP

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

@ -67,18 +67,11 @@ class nsHtml5Parser final : public nsIParser,
* Call this method once you've created a parser, and want to instruct it
* about what charset to load
*
* @param aCharset the charset of a document
* @param aEncoding the charset of a document
* @param aCharsetSource the source of the charset
*/
NS_IMETHOD_(void) SetDocumentCharset(const nsACString& aCharset, int32_t aSource) override;
/**
* Don't call. For interface compat only.
*/
NS_IMETHOD_(void) GetDocumentCharset(nsACString& aCharset, int32_t& aSource) override
{
NS_NOTREACHED("No one should call this.");
}
virtual void SetDocumentCharset(NotNull<const Encoding*> aEncoding,
int32_t aSource) override;
/**
* Get the channel associated with this parser

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

@ -66,7 +66,7 @@ nsHtml5SpeculativeLoad::Perform(nsHtml5TreeOpExecutor* aExecutor)
NS_ASSERTION(mTypeOrCharsetSourceOrDocumentMode.Length() == 1,
"Unexpected charset source string");
int32_t intSource = (int32_t)mTypeOrCharsetSourceOrDocumentMode.First();
aExecutor->SetDocumentCharsetAndSource(narrowName,
aExecutor->SetDocumentCharsetAndSource(Encoding::ForName(narrowName),
intSource);
}
break;

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

@ -152,6 +152,7 @@ nsHtml5StreamParser::nsHtml5StreamParser(nsHtml5TreeOpExecutor* aExecutor,
: mSniffingLength(0)
, mBomState(eBomState::BOM_SNIFFING_NOT_STARTED)
, mCharsetSource(kCharsetUninitialized)
, mEncoding(WINDOWS_1252_ENCODING)
, mReparseForbidden(false)
, mLastBuffer(nullptr) // Will be filled when starting
, mExecutor(aExecutor)
@ -253,23 +254,21 @@ nsHtml5StreamParser::Notify(const char* aCharset, nsDetectionConfident aConf)
NS_ASSERTION(IsParserThread(), "Wrong thread!");
if (aConf == eBestAnswer || aConf == eSureAnswer) {
mFeedChardet = false; // just in case
const Encoding* encoding = Encoding::ForLabelNoReplacement(
auto encoding = Encoding::ForLabelNoReplacement(
nsDependentCString(aCharset));
if (!encoding) {
return NS_OK;
}
nsAutoCString charset;
encoding->Name(charset);
if (HasDecoder()) {
if (mCharset.Equals(charset)) {
if (mEncoding == encoding) {
NS_ASSERTION(mCharsetSource < kCharsetFromAutoDetection,
"Why are we running chardet at all?");
mCharsetSource = kCharsetFromAutoDetection;
mTreeBuilder->SetDocumentCharset(mCharset, mCharsetSource);
mTreeBuilder->SetDocumentCharset(mEncoding, mCharsetSource);
} else {
// We've already committed to a decoder. Request a reload from the
// docshell.
mTreeBuilder->NeedsCharsetSwitchTo(charset,
mTreeBuilder->NeedsCharsetSwitchTo(WrapNotNull(encoding),
kCharsetFromAutoDetection,
0);
FlushTreeOpsAndDisarmTimer();
@ -278,9 +277,9 @@ nsHtml5StreamParser::Notify(const char* aCharset, nsDetectionConfident aConf)
} else {
// Got a confident answer from the sniffing buffer. That code will
// take care of setting up the decoder.
mCharset.Assign(charset);
mEncoding = WrapNotNull(encoding);
mCharsetSource = kCharsetFromAutoDetection;
mTreeBuilder->SetDocumentCharset(mCharset, mCharsetSource);
mTreeBuilder->SetDocumentCharset(mEncoding, mCharsetSource);
}
}
return NS_OK;
@ -321,7 +320,7 @@ nsHtml5StreamParser::SetupDecodingAndWriteSniffingBufferAndCurrentSegment(const
{
NS_ASSERTION(IsParserThread(), "Wrong thread!");
nsresult rv = NS_OK;
mUnicodeDecoder = Encoding::ForName(mCharset)->NewDecoderWithBOMRemoval();
mUnicodeDecoder = mEncoding->NewDecoderWithBOMRemoval();
if (mSniffingBuffer) {
uint32_t writeCount;
rv = WriteStreamBytes(mSniffingBuffer.get(), mSniffingLength, &writeCount);
@ -336,14 +335,14 @@ nsHtml5StreamParser::SetupDecodingAndWriteSniffingBufferAndCurrentSegment(const
}
nsresult
nsHtml5StreamParser::SetupDecodingFromBom(const char* aDecoderCharsetName)
nsHtml5StreamParser::SetupDecodingFromBom(NotNull<const Encoding*> aEncoding)
{
NS_ASSERTION(IsParserThread(), "Wrong thread!");
mCharset.Assign(aDecoderCharsetName);
mUnicodeDecoder = Encoding::ForName(mCharset)->NewDecoderWithBOMRemoval();
mEncoding = aEncoding;
mUnicodeDecoder = mEncoding->NewDecoderWithBOMRemoval();
mCharsetSource = kCharsetFromByteOrderMark;
mFeedChardet = false;
mTreeBuilder->SetDocumentCharset(mCharset, mCharsetSource);
mTreeBuilder->SetDocumentCharset(mEncoding, mCharsetSource);
mSniffingBuffer = nullptr;
mMetaScanner = nullptr;
mBomState = BOM_SNIFFING_OVER;
@ -398,12 +397,12 @@ nsHtml5StreamParser::SniffBOMlessUTF16BasicLatin(const uint8_t* aFromSegment,
}
if (byteNonZero[0]) {
mCharset.AssignLiteral("UTF-16LE");
mEncoding = UTF_16LE_ENCODING;
} else {
mCharset.AssignLiteral("UTF-16BE");
mEncoding = UTF_16BE_ENCODING;
}
mCharsetSource = kCharsetFromIrreversibleAutoDetection;
mTreeBuilder->SetDocumentCharset(mCharset, mCharsetSource);
mTreeBuilder->SetDocumentCharset(mEncoding, mCharsetSource);
mFeedChardet = false;
mTreeBuilder->MaybeComplainAboutCharset("EncBomlessUtf16",
true,
@ -418,8 +417,9 @@ nsHtml5StreamParser::SetEncodingFromExpat(const char16_t* aEncoding)
nsDependentString utf16(aEncoding);
nsAutoCString utf8;
CopyUTF16toUTF8(utf16, utf8);
if (PreferredForInternalEncodingDecl(utf8)) {
mCharset.Assign(utf8);
auto encoding = PreferredForInternalEncodingDecl(utf8);
if (encoding) {
mEncoding = WrapNotNull(encoding);
mCharsetSource = kCharsetFromMetaTag; // closest for XML
return;
}
@ -428,7 +428,7 @@ nsHtml5StreamParser::SetEncodingFromExpat(const char16_t* aEncoding)
// right away and let the encoding be set to UTF-8 which we'd default to
// anyway.
}
mCharset.AssignLiteral("UTF-8"); // XML defaults to UTF-8 without a BOM
mEncoding = UTF_8_ENCODING; // XML defaults to UTF-8 without a BOM
mCharsetSource = kCharsetFromMetaTag; // means confident
}
@ -560,7 +560,7 @@ nsHtml5StreamParser::FinalizeSniffing(const uint8_t* aFromSegment, // can be nul
// confidently to UTF-8 in this case.
// It is also possible that the document has an XML declaration that is
// longer than 1024 bytes, but that case is not worth worrying about.
mCharset.AssignLiteral("UTF-8");
mEncoding = UTF_8_ENCODING;
mCharsetSource = kCharsetFromMetaTag; // means confident
}
@ -612,18 +612,18 @@ nsHtml5StreamParser::FinalizeSniffing(const uint8_t* aFromSegment, // can be nul
}
if (mCharsetSource == kCharsetUninitialized) {
// Hopefully this case is never needed, but dealing with it anyway
mCharset.AssignLiteral("windows-1252");
mEncoding = WINDOWS_1252_ENCODING;
mCharsetSource = kCharsetFromFallback;
mTreeBuilder->SetDocumentCharset(mCharset, mCharsetSource);
mTreeBuilder->SetDocumentCharset(mEncoding, mCharsetSource);
} else if (mMode == LOAD_AS_DATA &&
mCharsetSource == kCharsetFromFallback) {
NS_ASSERTION(mReparseForbidden, "Reparse should be forbidden for XHR");
NS_ASSERTION(!mFeedChardet, "Should not feed chardet for XHR");
NS_ASSERTION(mCharset.EqualsLiteral("UTF-8"),
NS_ASSERTION(mEncoding == UTF_8_ENCODING,
"XHR should default to UTF-8");
// Now mark charset source as non-weak to signal that we have a decision
mCharsetSource = kCharsetFromDocTypeDefault;
mTreeBuilder->SetDocumentCharset(mCharset, mCharsetSource);
mTreeBuilder->SetDocumentCharset(mEncoding, mCharsetSource);
}
return SetupDecodingAndWriteSniffingBufferAndCurrentSegment(aFromSegment, aCount, aWriteCount);
}
@ -637,9 +637,9 @@ nsHtml5StreamParser::SniffStreamBytes(const uint8_t* aFromSegment,
nsresult rv = NS_OK;
uint32_t writeCount;
// mCharset and mCharsetSource potentially have come from channel or higher
// mEncoding and mCharsetSource potentially have come from channel or higher
// by now. If we find a BOM, SetupDecodingFromBom() will overwrite them.
// If we don't find a BOM, the previously set values of mCharset and
// If we don't find a BOM, the previously set values of mEncoding and
// mCharsetSource are not modified by the BOM sniffing here.
for (uint32_t i = 0; i < aCount && mBomState != BOM_SNIFFING_OVER; i++) {
switch (mBomState) {
@ -662,7 +662,7 @@ nsHtml5StreamParser::SniffStreamBytes(const uint8_t* aFromSegment,
break;
case SEEN_UTF_16_LE_FIRST_BYTE:
if (aFromSegment[i] == 0xFE) {
rv = SetupDecodingFromBom("UTF-16LE"); // upper case is the raw form
rv = SetupDecodingFromBom(UTF_16LE_ENCODING); // upper case is the raw form
NS_ENSURE_SUCCESS(rv, rv);
uint32_t count = aCount - (i + 1);
rv = WriteStreamBytes(aFromSegment + (i + 1), count, &writeCount);
@ -674,7 +674,7 @@ nsHtml5StreamParser::SniffStreamBytes(const uint8_t* aFromSegment,
break;
case SEEN_UTF_16_BE_FIRST_BYTE:
if (aFromSegment[i] == 0xFF) {
rv = SetupDecodingFromBom("UTF-16BE"); // upper case is the raw form
rv = SetupDecodingFromBom(UTF_16BE_ENCODING); // upper case is the raw form
NS_ENSURE_SUCCESS(rv, rv);
uint32_t count = aCount - (i + 1);
rv = WriteStreamBytes(aFromSegment + (i + 1), count, &writeCount);
@ -693,7 +693,7 @@ nsHtml5StreamParser::SniffStreamBytes(const uint8_t* aFromSegment,
break;
case SEEN_UTF_8_SECOND_BYTE:
if (aFromSegment[i] == 0xBF) {
rv = SetupDecodingFromBom("UTF-8"); // upper case is the raw form
rv = SetupDecodingFromBom(UTF_8_ENCODING); // upper case is the raw form
NS_ENSURE_SUCCESS(rv, rv);
uint32_t count = aCount - (i + 1);
rv = WriteStreamBytes(aFromSegment + (i + 1), count, &writeCount);
@ -718,13 +718,13 @@ nsHtml5StreamParser::SniffStreamBytes(const uint8_t* aFromSegment,
if (mBomState == BOM_SNIFFING_OVER &&
mCharsetSource == kCharsetFromChannel) {
// There was no BOM and the charset came from channel. mCharset
// There was no BOM and the charset came from channel. mEncoding
// still contains the charset from the channel as set by an
// earlier call to SetDocumentCharset(), since we didn't find a BOM and
// overwrite mCharset. (Note that if the user has overridden the charset,
// overwrite mEncoding. (Note that if the user has overridden the charset,
// we don't come here but check <meta> for XSS-dangerous charsets first.)
mFeedChardet = false;
mTreeBuilder->SetDocumentCharset(mCharset, mCharsetSource);
mTreeBuilder->SetDocumentCharset(mEncoding, mCharsetSource);
return SetupDecodingAndWriteSniffingBufferAndCurrentSegment(aFromSegment,
aCount, aWriteCount);
}
@ -743,15 +743,14 @@ nsHtml5StreamParser::SniffStreamBytes(const uint8_t* aFromSegment,
nsHtml5ByteReadable readable(aFromSegment, aFromSegment +
countToSniffingLimit);
nsAutoCString charset;
mMetaScanner->sniff(&readable, charset);
auto encoding = mMetaScanner->sniff(&readable);
// Due to the way nsHtml5Portability reports OOM, ask the tree buider
nsresult rv;
if (NS_FAILED((rv = mTreeBuilder->IsBroken()))) {
MarkAsBroken(rv);
return rv;
}
if (!charset.IsEmpty()) {
const Encoding* encoding = Encoding::ForName(charset);
if (encoding) {
// meta scan successful; honor overrides unless meta is XSS-dangerous
if ((mCharsetSource == kCharsetFromParentForced ||
mCharsetSource == kCharsetFromUserForced) &&
@ -761,10 +760,10 @@ nsHtml5StreamParser::SniffStreamBytes(const uint8_t* aFromSegment,
return SetupDecodingAndWriteSniffingBufferAndCurrentSegment(
aFromSegment, aCount, aWriteCount);
}
mCharset.Assign(charset);
mEncoding = WrapNotNull(encoding);
mCharsetSource = kCharsetFromMetaPrescan;
mFeedChardet = false;
mTreeBuilder->SetDocumentCharset(mCharset, mCharsetSource);
mTreeBuilder->SetDocumentCharset(mEncoding, mCharsetSource);
return SetupDecodingAndWriteSniffingBufferAndCurrentSegment(
aFromSegment, aCount, aWriteCount);
}
@ -782,16 +781,14 @@ nsHtml5StreamParser::SniffStreamBytes(const uint8_t* aFromSegment,
// not the last buffer
if (mMode == NORMAL || mMode == VIEW_SOURCE_HTML || mMode == LOAD_AS_DATA) {
nsHtml5ByteReadable readable(aFromSegment, aFromSegment + aCount);
nsAutoCString charset;
mMetaScanner->sniff(&readable, charset);
auto encoding = mMetaScanner->sniff(&readable);
// Due to the way nsHtml5Portability reports OOM, ask the tree buider
nsresult rv;
if (NS_FAILED((rv = mTreeBuilder->IsBroken()))) {
MarkAsBroken(rv);
return rv;
}
if (!charset.IsEmpty()) {
const Encoding* encoding = Encoding::ForName(charset);
if (encoding) {
// meta scan successful; honor overrides unless meta is XSS-dangerous
if ((mCharsetSource == kCharsetFromParentForced ||
mCharsetSource == kCharsetFromUserForced) &&
@ -801,10 +798,10 @@ nsHtml5StreamParser::SniffStreamBytes(const uint8_t* aFromSegment,
return SetupDecodingAndWriteSniffingBufferAndCurrentSegment(aFromSegment,
aCount, aWriteCount);
}
mCharset.Assign(charset);
mEncoding = WrapNotNull(encoding);
mCharsetSource = kCharsetFromMetaPrescan;
mFeedChardet = false;
mTreeBuilder->SetDocumentCharset(mCharset, mCharsetSource);
mTreeBuilder->SetDocumentCharset(mEncoding, mCharsetSource);
return SetupDecodingAndWriteSniffingBufferAndCurrentSegment(aFromSegment,
aCount, aWriteCount);
}
@ -1007,7 +1004,7 @@ nsHtml5StreamParser::OnStartRequest(nsIRequest* aRequest, nsISupports* aContext)
mFeedChardet = false;
// Instantiate the converter here to avoid BOM sniffing.
mUnicodeDecoder = Encoding::ForName(mCharset)->NewDecoderWithBOMRemoval();
mUnicodeDecoder = mEncoding->NewDecoderWithBOMRemoval();
return NS_OK;
}
@ -1231,8 +1228,8 @@ nsHtml5StreamParser::CopySegmentsToParser(nsIInputStream *aInStream,
return NS_OK;
}
bool
nsHtml5StreamParser::PreferredForInternalEncodingDecl(nsACString& aEncoding)
const Encoding*
nsHtml5StreamParser::PreferredForInternalEncodingDecl(const nsACString& aEncoding)
{
const Encoding* newEncoding = Encoding::ForLabel(aEncoding);
if (!newEncoding) {
@ -1240,7 +1237,7 @@ nsHtml5StreamParser::PreferredForInternalEncodingDecl(nsACString& aEncoding)
mTreeBuilder->MaybeComplainAboutCharset("EncMetaUnsupported",
true,
mTokenizer->getLineNumber());
return false;
return nullptr;
}
if (newEncoding == UTF_16BE_ENCODING ||
@ -1259,7 +1256,7 @@ nsHtml5StreamParser::PreferredForInternalEncodingDecl(nsACString& aEncoding)
newEncoding = WINDOWS_1252_ENCODING;
}
if (newEncoding == Encoding::ForName(mCharset)) {
if (newEncoding == mEncoding) {
if (mCharsetSource < kCharsetFromMetaPrescan) {
if (mInitialEncodingWasFromParentFrame) {
mTreeBuilder->MaybeComplainAboutCharset("EncLateMetaFrame",
@ -1273,11 +1270,10 @@ nsHtml5StreamParser::PreferredForInternalEncodingDecl(nsACString& aEncoding)
}
mCharsetSource = kCharsetFromMetaTag; // become confident
mFeedChardet = false; // don't feed chardet when confident
return false;
return nullptr;
}
newEncoding->Name(aEncoding);
return true;
return newEncoding;
}
bool
@ -1296,7 +1292,8 @@ nsHtml5StreamParser::internalEncodingDeclaration(nsHtml5String aEncoding)
nsAutoCString newEncoding;
CopyUTF16toUTF8(newEncoding16, newEncoding);
if (!PreferredForInternalEncodingDecl(newEncoding)) {
auto encoding = PreferredForInternalEncodingDecl(newEncoding);
if (!encoding) {
return false;
}
@ -1314,7 +1311,7 @@ nsHtml5StreamParser::internalEncodingDeclaration(nsHtml5String aEncoding)
// Avoid having the chardet ask for another restart after this restart
// request.
mFeedChardet = false;
mTreeBuilder->NeedsCharsetSwitchTo(newEncoding,
mTreeBuilder->NeedsCharsetSwitchTo(WrapNotNull(encoding),
kCharsetFromMetaTag,
mTokenizer->getLineNumber());
FlushTreeOpsAndDisarmTimer();

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

@ -101,7 +101,9 @@ enum eHtml5StreamState {
STREAM_ENDED = 2
};
class nsHtml5StreamParser : public nsICharsetDetectionObserver {
class nsHtml5StreamParser final : public nsICharsetDetectionObserver {
template <typename T> using NotNull = mozilla::NotNull<T>;
using Encoding = mozilla::Encoding;
friend class nsHtml5RequestStopper;
friend class nsHtml5DataAvailable;
@ -153,14 +155,15 @@ class nsHtml5StreamParser : public nsICharsetDetectionObserver {
* Call this method once you've created a parser, and want to instruct it
* about what charset to load
*
* @param aCharset the charset of a document
* @param aEncoding the charset of a document
* @param aCharsetSource the source of the charset
*/
inline void SetDocumentCharset(const nsACString& aCharset, int32_t aSource) {
inline void SetDocumentCharset(NotNull<const Encoding*> aEncoding,
int32_t aSource) {
NS_PRECONDITION(mStreamState == STREAM_NOT_STARTED,
"SetDocumentCharset called too late.");
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
mCharset = aCharset;
mEncoding = aEncoding;
mCharsetSource = aSource;
}
@ -194,9 +197,9 @@ class nsHtml5StreamParser : public nsICharsetDetectionObserver {
void DropTimer();
/**
* Sets mCharset and mCharsetSource appropriately for the XML View Source
* Sets mEncoding and mCharsetSource appropriately for the XML View Source
* case if aEncoding names a supported rough ASCII superset and sets
* the mCharset and mCharsetSource to the UTF-8 default otherwise.
* the mEncoding and mCharsetSource to the UTF-8 default otherwise.
*/
void SetEncodingFromExpat(const char16_t* aEncoding);
@ -339,7 +342,7 @@ class nsHtml5StreamParser : public nsICharsetDetectionObserver {
* (UTF-16BE, UTF-16LE or UTF-8; the BOM has
* been swallowed)
*/
nsresult SetupDecodingFromBom(const char* aDecoderCharsetName);
nsresult SetupDecodingFromBom(NotNull<const Encoding*> aEncoding);
/**
* Become confident or resolve and encoding name to its preferred form.
@ -349,7 +352,7 @@ class nsHtml5StreamParser : public nsICharsetDetectionObserver {
* aEncoding and false if the parser became confident or if
* the encoding name did not specify a usable encoding
*/
bool PreferredForInternalEncodingDecl(nsACString& aEncoding);
const Encoding* PreferredForInternalEncodingDecl(const nsACString& aEncoding);
/**
* Callback for mFlushTimer.
@ -423,7 +426,7 @@ class nsHtml5StreamParser : public nsICharsetDetectionObserver {
/**
* The character encoding in use
*/
nsCString mCharset;
NotNull<const Encoding*> mEncoding;
/**
* Whether reparse is forbidden

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

@ -1078,17 +1078,19 @@ nsHtml5TreeBuilder::FlushLoads()
}
void
nsHtml5TreeBuilder::SetDocumentCharset(nsACString& aCharset,
nsHtml5TreeBuilder::SetDocumentCharset(NotNull<const Encoding*> aEncoding,
int32_t aCharsetSource)
{
if (mBuilder) {
mBuilder->SetDocumentCharsetAndSource(aCharset, aCharsetSource);
mBuilder->SetDocumentCharsetAndSource(aEncoding, aCharsetSource);
} else if (mSpeculativeLoadStage) {
nsAutoCString charset;
aEncoding->Name(charset);
mSpeculativeLoadQueue.AppendElement()->InitSetDocumentCharset(
aCharset, aCharsetSource);
charset, aCharsetSource);
} else {
mOpQueue.AppendElement()->Init(
eTreeOpSetDocumentCharset, aCharset, aCharsetSource);
eTreeOpSetDocumentCharset, aEncoding, aCharsetSource);
}
}
@ -1103,7 +1105,7 @@ nsHtml5TreeBuilder::StreamEnded()
}
void
nsHtml5TreeBuilder::NeedsCharsetSwitchTo(const nsACString& aCharset,
nsHtml5TreeBuilder::NeedsCharsetSwitchTo(NotNull<const Encoding*> aEncoding,
int32_t aCharsetSource,
int32_t aLineNumber)
{
@ -1114,7 +1116,7 @@ nsHtml5TreeBuilder::NeedsCharsetSwitchTo(const nsACString& aCharset,
nsHtml5TreeOperation* treeOp = mOpQueue.AppendElement();
NS_ASSERTION(treeOp, "Tree op allocation failed.");
treeOp->Init(eTreeOpNeedsCharsetSwitchTo,
aCharset,
aEncoding,
aCharsetSource,
aLineNumber);
}

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

@ -3,8 +3,10 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#define NS_HTML5_TREE_BUILDER_HANDLE_ARRAY_LENGTH 512
private:
using Encoding = mozilla::Encoding;
template <typename T> using NotNull = mozilla::NotNull<T>;
nsHtml5OplessBuilder* mBuilder;
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// If mBuilder is not null, the tree op machinery is not in use and
@ -104,11 +106,12 @@
void FlushLoads();
void SetDocumentCharset(nsACString& aCharset, int32_t aCharsetSource);
void SetDocumentCharset(NotNull<const Encoding*> aEncoding,
int32_t aCharsetSource);
void StreamEnded();
void NeedsCharsetSwitchTo(const nsACString& aEncoding,
void NeedsCharsetSwitchTo(NotNull<const Encoding*> aEncoding,
int32_t aSource,
int32_t aLineNumber);

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

@ -719,7 +719,7 @@ nsHtml5TreeOpExecutor::Start()
}
void
nsHtml5TreeOpExecutor::NeedsCharsetSwitchTo(const char* aEncoding,
nsHtml5TreeOpExecutor::NeedsCharsetSwitchTo(NotNull<const Encoding*> aEncoding,
int32_t aSource,
uint32_t aLineNumber)
{
@ -737,7 +737,9 @@ nsHtml5TreeOpExecutor::NeedsCharsetSwitchTo(const char* aEncoding,
// ask the webshellservice to load the URL
if (NS_SUCCEEDED(wss->StopDocumentLoad())) {
wss->ReloadDocument(aEncoding, aSource);
nsAutoCString charset;
aEncoding->Name(charset);
wss->ReloadDocument(charset.get(), aSource);
}
// if the charset switch was accepted, wss has called Terminate() on the
// parser by now
@ -911,9 +913,9 @@ nsHtml5TreeOpExecutor::ConvertIfNotPreloadedYet(const nsAString& aURL)
}
nsIURI* base = BaseURIForPreload();
const nsCString& charset = mDocument->GetDocumentCharacterSet();
auto encoding = mDocument->GetDocumentCharacterSet();
nsCOMPtr<nsIURI> uri;
nsresult rv = NS_NewURI(getter_AddRefs(uri), aURL, charset.get(), base);
nsresult rv = NS_NewURI(getter_AddRefs(uri), aURL, encoding, base);
if (NS_FAILED(rv)) {
NS_WARNING("Failed to create a URI");
return nullptr;
@ -1015,9 +1017,9 @@ nsHtml5TreeOpExecutor::PreloadEndPicture()
void
nsHtml5TreeOpExecutor::AddBase(const nsAString& aURL)
{
const nsCString& charset = mDocument->GetDocumentCharacterSet();
auto encoding = mDocument->GetDocumentCharacterSet();
nsresult rv = NS_NewURI(getter_AddRefs(mViewSourceBaseURI), aURL,
charset.get(), GetViewSourceBaseURI());
encoding, GetViewSourceBaseURI());
if (NS_FAILED(rv)) {
mViewSourceBaseURI = nullptr;
}
@ -1029,9 +1031,9 @@ nsHtml5TreeOpExecutor::SetSpeculationBase(const nsAString& aURL)
// the first one wins
return;
}
const nsCString& charset = mDocument->GetDocumentCharacterSet();
auto encoding = mDocument->GetDocumentCharacterSet();
DebugOnly<nsresult> rv = NS_NewURI(getter_AddRefs(mSpeculationBaseURI), aURL,
charset.get(), mDocument->GetDocumentURI());
encoding, mDocument->GetDocumentURI());
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Failed to create a URI");
}

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

@ -36,6 +36,8 @@ class nsHtml5TreeOpExecutor final : public nsHtml5DocumentBuilder,
{
friend class nsHtml5FlushLoopGuard;
typedef mozilla::net::ReferrerPolicy ReferrerPolicy;
using Encoding = mozilla::Encoding;
template <typename T> using NotNull = mozilla::NotNull<T>;
public:
NS_DECL_ISUPPORTS_INHERITED
@ -140,9 +142,9 @@ class nsHtml5TreeOpExecutor final : public nsHtml5DocumentBuilder,
/**
* Don't call. For interface compat only.
*/
NS_IMETHOD SetDocumentCharset(nsACString& aCharset) override {
NS_NOTREACHED("No one should call this.");
return NS_ERROR_NOT_IMPLEMENTED;
virtual void SetDocumentCharset(NotNull<const Encoding*> aEncoding) override
{
NS_NOTREACHED("No one should call this.");
}
/**
@ -184,7 +186,7 @@ class nsHtml5TreeOpExecutor final : public nsHtml5DocumentBuilder,
void Start();
void NeedsCharsetSwitchTo(const char* aEncoding,
void NeedsCharsetSwitchTo(NotNull<const Encoding*> aEncoding,
int32_t aSource,
uint32_t aLineNumber);

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

@ -105,7 +105,6 @@ nsHtml5TreeOperation::~nsHtml5TreeOperation()
break;
case eTreeOpSetDocumentCharset:
case eTreeOpNeedsCharsetSwitchTo:
delete[] mOne.charPtr;
break;
case eTreeOpProcessOfflineManifest:
free(mOne.unicharPtr);
@ -789,17 +788,16 @@ nsHtml5TreeOperation::Perform(nsHtml5TreeOpExecutor* aBuilder,
return NS_OK;
}
case eTreeOpSetDocumentCharset: {
char* str = mOne.charPtr;
auto encoding = WrapNotNull(mOne.encoding);
int32_t charsetSource = mFour.integer;
nsDependentCString dependentString(str);
aBuilder->SetDocumentCharsetAndSource(dependentString, charsetSource);
aBuilder->SetDocumentCharsetAndSource(encoding, charsetSource);
return NS_OK;
}
case eTreeOpNeedsCharsetSwitchTo: {
char* str = mOne.charPtr;
auto encoding = WrapNotNull(mOne.encoding);
int32_t charsetSource = mFour.integer;
int32_t lineNumber = mTwo.integer;
aBuilder->NeedsCharsetSwitchTo(str, charsetSource, (uint32_t)lineNumber);
aBuilder->NeedsCharsetSwitchTo(encoding, charsetSource, (uint32_t)lineNumber);
return NS_OK;
}
case eTreeOpUpdateStyleSheet: {
@ -884,11 +882,11 @@ nsHtml5TreeOperation::Perform(nsHtml5TreeOpExecutor* aBuilder,
nsIDocument* doc = aBuilder->GetDocument();
const nsCString& charset = doc->GetDocumentCharacterSet();
auto encoding = doc->GetDocumentCharacterSet();
nsCOMPtr<nsIURI> uri;
nsresult rv = NS_NewURI(getter_AddRefs(uri),
relative,
charset.get(),
encoding,
aBuilder->GetViewSourceBaseURI());
NS_ENSURE_SUCCESS(rv, NS_OK);

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

@ -8,10 +8,14 @@
#include "nsHtml5DocumentMode.h"
#include "nsHtml5HtmlAttributes.h"
#include "mozilla/dom/FromParser.h"
#include "mozilla/NotNull.h"
class nsIContent;
class nsHtml5TreeOpExecutor;
class nsHtml5DocumentBuilder;
namespace mozilla {
class Encoding;
}
enum eHtml5TreeOperation {
eTreeOpUninitialized,
@ -85,7 +89,9 @@ class nsHtml5TreeOperationStringPair {
}
};
class nsHtml5TreeOperation {
class nsHtml5TreeOperation final {
template <typename T> using NotNull = mozilla::NotNull<T>;
using Encoding = mozilla::Encoding;
public:
/**
@ -250,6 +256,27 @@ class nsHtml5TreeOperation {
mTwo.integer = aLineNumber;
}
inline void Init(eHtml5TreeOperation aOpCode,
NotNull<const Encoding*> aEncoding,
int32_t aInt32)
{
NS_PRECONDITION(mOpCode == eTreeOpUninitialized,
"Op code must be uninitialized when initializing.");
mOpCode = aOpCode;
mOne.encoding = aEncoding;
mFour.integer = aInt32;
}
inline void Init(eHtml5TreeOperation aOpCode,
NotNull<const Encoding*> aEncoding,
int32_t aInt32,
int32_t aLineNumber)
{
Init(aOpCode, aEncoding, aInt32);
mTwo.integer = aLineNumber;
}
inline void Init(eHtml5TreeOperation aOpCode,
nsIContentHandle* aNode,
nsIContentHandle* aParent,
@ -507,6 +534,7 @@ class nsHtml5TreeOperation {
nsAHtml5TreeBuilderState* state;
int32_t integer;
nsresult result;
const Encoding* encoding;
} mOne, mTwo, mThree, mFour, mFive;
};

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

@ -19,15 +19,22 @@
#include "nsISupports.h"
#include "nsString.h"
#include "mozilla/FlushType.h"
#include "mozilla/NotNull.h"
#include "nsIDTD.h"
class nsParserBase;
namespace mozilla {
class Encoding;
}
#define NS_ICONTENT_SINK_IID \
{ 0xcf9a7cbb, 0xfcbc, 0x4e13, \
{ 0x8e, 0xf5, 0x18, 0xef, 0x2d, 0x3d, 0x58, 0x29 } }
class nsIContentSink : public nsISupports {
protected:
using Encoding = mozilla::Encoding;
template <typename T> using NotNull = mozilla::NotNull<T>;
public:
NS_DECLARE_STATIC_IID_ACCESSOR(NS_ICONTENT_SINK_IID)
@ -103,7 +110,7 @@ public:
* Set the document character set. This should be passed on to the
* document itself.
*/
NS_IMETHOD SetDocumentCharset(nsACString& aCharset)=0;
virtual void SetDocumentCharset(NotNull<const Encoding*> aEncoding) = 0;
/**
* Returns the target object (often a document object) into which

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

@ -25,6 +25,7 @@
#include "nsTArray.h"
#include "nsIAtom.h"
#include "nsParserBase.h"
#include "mozilla/NotNull.h"
#define NS_IPARSER_IID \
{ 0x2c4ad90a, 0x740e, 0x4212, \
@ -35,6 +36,9 @@ class nsIRequestObserver;
class nsString;
class nsIURI;
class nsIChannel;
namespace mozilla {
class Encoding;
}
enum eParserCommands {
eViewNormal,
@ -63,6 +67,9 @@ enum eStreamState {eNone,eOnStart,eOnDataAvail,eOnStop};
* likely than not that #including this header is the wrong thing to do.
*/
class nsIParser : public nsParserBase {
protected:
using Encoding = mozilla::Encoding;
template <typename T> using NotNull = mozilla::NotNull<T>;
public:
NS_DECLARE_STATIC_IID_ACCESSOR(NS_IPARSER_IID)
@ -105,8 +112,8 @@ class nsIParser : public nsParserBase {
* @param aCharsetSource- the soure of the chares
* @return nada
*/
NS_IMETHOD_(void) SetDocumentCharset(const nsACString& aCharset, int32_t aSource)=0;
NS_IMETHOD_(void) GetDocumentCharset(nsACString& oCharset, int32_t& oSource)=0;
virtual void SetDocumentCharset(NotNull<const Encoding*> aCharset,
int32_t aSource) = 0;
/**
* Get the channel associated with this parser

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

@ -126,6 +126,7 @@ public:
* default constructor
*/
nsParser::nsParser()
: mCharset(WINDOWS_1252_ENCODING)
{
Initialize(true);
}
@ -150,7 +151,7 @@ nsParser::Initialize(bool aConstructor)
mContinueEvent = nullptr;
mCharsetSource = kCharsetUninitialized;
mCharset.AssignLiteral("windows-1252");
mCharset = WINDOWS_1252_ENCODING;
mInternalState = NS_OK;
mStreamStatus = NS_OK;
mCommand = eViewNormal;
@ -283,8 +284,9 @@ nsParser::SetCommand(eParserCommands aParserCommand)
* @param aCharset- the charset of a document
* @param aCharsetSource- the source of the charset
*/
NS_IMETHODIMP_(void)
nsParser::SetDocumentCharset(const nsACString& aCharset, int32_t aCharsetSource)
void
nsParser::SetDocumentCharset(NotNull<const Encoding*> aCharset,
int32_t aCharsetSource)
{
mCharset = aCharset;
mCharsetSource = aCharsetSource;
@ -294,7 +296,7 @@ nsParser::SetDocumentCharset(const nsACString& aCharset, int32_t aCharsetSource)
}
void
nsParser::SetSinkCharset(nsACString& aCharset)
nsParser::SetSinkCharset(NotNull<const Encoding*> aCharset)
{
if (mSink) {
mSink->SetDocumentCharset(aCharset);
@ -1331,8 +1333,7 @@ ParserWriteFunc(nsIInputStream* in,
if (pws->mNeedCharsetCheck) {
pws->mNeedCharsetCheck = false;
int32_t source;
nsAutoCString preferred;
pws->mParser->GetDocumentCharset(preferred, source);
auto preferred = pws->mParser->GetDocumentCharset(source);
// This code was bogus when I found it. It expects the BOM or the XML
// declaration to be entirely in the first network buffer. -- hsivonen
@ -1344,7 +1345,7 @@ ParserWriteFunc(nsIInputStream* in,
// The decoder will swallow the BOM. The UTF-16 will re-sniff for
// endianness. The value of preferred is now "UTF-8", "UTF-16LE"
// or "UTF-16BE".
encoding->Name(preferred);
preferred = WrapNotNull(encoding);
source = kCharsetFromByteOrderMark;
} else if (source < kCharsetFromChannel) {
nsAutoCString declCharset;
@ -1352,7 +1353,7 @@ ParserWriteFunc(nsIInputStream* in,
if (ExtractCharsetFromXmlDeclaration(buf, count, declCharset)) {
encoding = Encoding::ForLabel(declCharset);
if (encoding) {
encoding->Name(preferred);
preferred = WrapNotNull(encoding);
source = kCharsetFromMetaTag;
}
}

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

@ -129,12 +129,13 @@ class nsParser final : public nsIParser,
* @param aCharsetSource- the source of the charset
* @return nada
*/
NS_IMETHOD_(void) SetDocumentCharset(const nsACString& aCharset, int32_t aSource) override;
virtual void SetDocumentCharset(NotNull<const Encoding*> aCharset,
int32_t aSource) override;
NS_IMETHOD_(void) GetDocumentCharset(nsACString& aCharset, int32_t& aSource) override
NotNull<const Encoding*> GetDocumentCharset(int32_t& aSource)
{
aCharset = mCharset;
aSource = mCharsetSource;
return mCharset;
}
/**
@ -241,7 +242,7 @@ class nsParser final : public nsIParser,
*/
virtual nsIStreamListener* GetStreamListener() override;
void SetSinkCharset(nsACString& aCharset);
void SetSinkCharset(NotNull<const Encoding*> aCharset);
/**
* Removes continue parsing events
@ -388,7 +389,7 @@ protected:
uint32_t mBlocked;
nsString mUnusedInput;
nsCString mCharset;
NotNull<const Encoding*> mCharset;
nsCString mCommandStr;
bool mProcessingNetworkData;

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

@ -93,26 +93,18 @@ nsScanner::nsScanner(nsString& aFilename, bool aCreateStream)
mUnicodeDecoder = nullptr;
mCharsetSource = kCharsetUninitialized;
// XML defaults to UTF-8 and about:blank is UTF-8, too.
SetDocumentCharset(NS_LITERAL_CSTRING("UTF-8"), kCharsetFromDocTypeDefault);
SetDocumentCharset(UTF_8_ENCODING, kCharsetFromDocTypeDefault);
}
nsresult nsScanner::SetDocumentCharset(const nsACString& aCharset , int32_t aSource)
nsresult nsScanner::SetDocumentCharset(NotNull<const Encoding*> aEncoding,
int32_t aSource)
{
if (aSource < mCharsetSource) // priority is lower than the current one
return NS_OK;
mCharsetSource = aSource;
const Encoding* encoding;
if (aCharset.EqualsLiteral("replacement")) {
encoding = REPLACEMENT_ENCODING;
} else {
encoding = Encoding::ForLabel(aCharset);
MOZ_ASSERT(encoding, "Should never call with a bogus aCharset.");
}
nsCString charsetName;
encoding->Name(charsetName);
aEncoding->Name(charsetName);
if (!mCharset.IsEmpty() && charsetName.Equals(mCharset)) {
return NS_OK; // no difference, don't change it
}
@ -121,7 +113,7 @@ nsresult nsScanner::SetDocumentCharset(const nsACString& aCharset , int32_t aSou
mCharset.Assign(charsetName);
mUnicodeDecoder = encoding->NewDecoderWithBOMRemoval();
mUnicodeDecoder = aEncoding->NewDecoderWithBOMRemoval();
return NS_OK;
}

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

@ -36,7 +36,9 @@ private:
void operator=(const nsReadEndCondition& aOther); // No assigning
};
class nsScanner {
class nsScanner final {
using Encoding = mozilla::Encoding;
template <typename T> using NotNull = mozilla::NotNull<T>;
public:
/**
@ -142,7 +144,8 @@ class nsScanner {
* @param aCharsetSource- where the charset info came from
* @return
*/
nsresult SetDocumentCharset(const nsACString& aCharset, int32_t aSource);
nsresult SetDocumentCharset(NotNull<const Encoding*> aEncoding,
int32_t aSource);
void BindSubstring(nsScannerSubstring& aSubstring, const nsScannerIterator& aStart, const nsScannerIterator& aEnd);
void CurrentPosition(nsScannerIterator& aPosition);

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

@ -20,6 +20,7 @@
#include "nsCharsetSource.h"
using mozilla::Encoding;
using mozilla::NotNull;
#define XMLNS_URI "http://www.w3.org/2000/xmlns/"
@ -628,9 +629,9 @@ nsSAXXMLReader::InitParser(nsIRequestObserver *aObserver, nsIChannel *aChannel)
parser->SetContentSink(this);
int32_t charsetSource = kCharsetFromDocTypeDefault;
nsAutoCString charset(NS_LITERAL_CSTRING("UTF-8"));
TryChannelCharset(aChannel, charsetSource, charset);
parser->SetDocumentCharset(charset, charsetSource);
auto encoding = UTF_8_ENCODING;
TryChannelCharset(aChannel, charsetSource, encoding);
parser->SetDocumentCharset(encoding, charsetSource);
rv = parser->Parse(mBaseURI, aObserver);
NS_ENSURE_SUCCESS(rv, rv);
@ -644,7 +645,7 @@ nsSAXXMLReader::InitParser(nsIRequestObserver *aObserver, nsIChannel *aChannel)
bool
nsSAXXMLReader::TryChannelCharset(nsIChannel *aChannel,
int32_t& aCharsetSource,
nsACString& aCharset)
NotNull<const Encoding*>& aEncoding)
{
if (aCharsetSource >= kCharsetFromChannel)
return true;
@ -657,7 +658,7 @@ nsSAXXMLReader::TryChannelCharset(nsIChannel *aChannel,
if (!preferred)
return false;
preferred->Name(aCharset);
aEncoding = WrapNotNull(preferred);
aCharsetSource = kCharsetFromChannel;
return true;
}

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

@ -19,6 +19,7 @@
#include "nsIMozSAXXMLDeclarationHandler.h"
#include "nsCycleCollectionParticipant.h"
#include "mozilla/Attributes.h"
#include "mozilla/NotNull.h"
#define NS_SAXXMLREADER_CONTRACTID "@mozilla.org/saxparser/xmlreader;1"
#define NS_SAXXMLREADER_CID \
@ -64,9 +65,8 @@ public:
{
}
NS_IMETHOD SetDocumentCharset(nsACString& aCharset) override
virtual void SetDocumentCharset(NotNull<const Encoding*> aEncoding) override
{
return NS_OK;
}
virtual nsISupports *GetTarget() override
@ -87,8 +87,8 @@ private:
nsCOMPtr<nsIRequestObserver> mParserObserver;
bool mIsAsyncParse;
static bool TryChannelCharset(nsIChannel *aChannel,
int32_t& aCharsetSource,
nsACString& aCharset);
int32_t& aCharsetSource,
NotNull<const Encoding*>& aEncoding);
nsresult EnsureBaseURI();
nsresult InitParser(nsIRequestObserver *aListener, nsIChannel *aChannel);
nsresult SplitExpatName(const char16_t *aExpatName,

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

@ -106,7 +106,8 @@ public:
NS_IMETHOD WillResume(void) override;
NS_IMETHOD SetParser(nsParserBase* aParser) override;
virtual void FlushPendingNotifications(mozilla::FlushType aType) override { }
NS_IMETHOD SetDocumentCharset(nsACString& aCharset) override { return NS_OK; }
virtual void SetDocumentCharset(NotNull<const Encoding*> aEncoding)
override { }
virtual nsISupports *GetTarget() override { return nullptr; }
// nsIRDFContentSink

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

@ -6,6 +6,7 @@
#include "nsRDFXMLParser.h"
#include "mozilla/Encoding.h"
#include "nsIComponentManager.h"
#include "nsIParser.h"
#include "nsCharsetSource.h"
@ -65,7 +66,7 @@ nsRDFXMLParser::ParseAsync(nsIRDFDataSource* aSink, nsIURI* aBaseURI, nsIStreamL
nsCOMPtr<nsIParser> parser = do_CreateInstance(kParserCID, &rv);
if (NS_FAILED(rv)) return rv;
parser->SetDocumentCharset(NS_LITERAL_CSTRING("UTF-8"),
parser->SetDocumentCharset(UTF_8_ENCODING,
kCharsetFromDocTypeDefault);
parser->SetContentSink(sink);
@ -96,7 +97,7 @@ nsRDFXMLParser::ParseString(nsIRDFDataSource* aSink, nsIURI* aBaseURI, const nsA
nsCOMPtr<nsIParser> parser = do_CreateInstance(kParserCID, &rv);
if (NS_FAILED(rv)) return rv;
parser->SetDocumentCharset(NS_LITERAL_CSTRING("UTF-8"),
parser->SetDocumentCharset(UTF_8_ENCODING,
kCharsetFromOtherComponent);
parser->SetContentSink(sink);