Skip to content

Commit

Permalink
Bug 1373984 - Turn nsIDocument::mCharacterSet into mozilla::NotNull<c…
Browse files Browse the repository at this point in the history
…onst mozilla::Encoding*>. r=hsivonen

MozReview-Commit-ID: GF0YXDwfA14

--HG--
extra : rebase_source : fdae0046f882d47fb539a7f882364e5c5caafdcd
extra : source : 49249788c0dee331ac2989dc39f0505d965a7bd8
  • Loading branch information
vyv03354 committed Jun 18, 2017
1 parent acba848 commit b515c9c
Show file tree
Hide file tree
Showing 81 changed files with 582 additions and 448 deletions.
2 changes: 1 addition & 1 deletion accessible/xul/XULElementAccessibles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
24 changes: 14 additions & 10 deletions docshell/base/nsDocShell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand All @@ -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)
{
Expand All @@ -2150,7 +2153,7 @@ nsDocShell::SetParentCharset(const nsACString& aCharset,
}

void
nsDocShell::GetParentCharset(nsACString& aCharset,
nsDocShell::GetParentCharset(const Encoding*& aCharset,
int32_t* aCharsetSource,
nsIPrincipal** aPrincipal)
{
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 6 additions & 2 deletions docshell/base/nsDocShell.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
#include "nsIDeprecationWarner.h"

namespace mozilla {
class Encoding;
enum class TaskCategory;
namespace dom {
class EventTarget;
Expand Down Expand Up @@ -155,6 +156,7 @@ class nsDocShell final
{
friend class nsDSURIContentListener;
friend class FramingChecker;
using Encoding = mozilla::Encoding;

public:
MOZ_DECLARE_WEAKREFERENCE_TYPENAME(nsDocShell)
Expand Down Expand Up @@ -274,6 +276,8 @@ class nsDocShell final
}
bool InFrameSwap();

const Encoding* GetForcedCharset() { return mForcedCharset; }

private:
bool CanSetOriginAttributes();

Expand Down Expand Up @@ -1036,8 +1040,8 @@ class nsDocShell final
nsString mInterceptedDocumentId;

private:
nsCString mForcedCharset;
nsCString mParentCharset;
const Encoding* mForcedCharset;
const Encoding* mParentCharset;
int32_t mParentCharsetSource;
nsCOMPtr<nsIPrincipal> mParentCharsetPrincipal;
nsTObserverArray<nsWeakPtr> mPrivacyObservers;
Expand Down
9 changes: 7 additions & 2 deletions docshell/base/nsIDocShell.idl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
%}

/**
Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion dom/base/FragmentOrElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
21 changes: 5 additions & 16 deletions dom/base/Location.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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.
Expand Down
10 changes: 4 additions & 6 deletions dom/base/nsContentSink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 6 additions & 3 deletions dom/base/nsContentUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion dom/base/nsDOMSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "nsDOMSerializer.h"

#include "mozilla/Encoding.h"
#include "nsIDocument.h"
#include "nsIDocumentEncoder.h"
#include "nsIDOMDocument.h"
Expand Down Expand Up @@ -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))
Expand Down
32 changes: 16 additions & 16 deletions dom/base/nsDocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -3678,21 +3678,22 @@ 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();

for (int32_t i = 0; i < n; i++) {
nsIObserver* observer = mCharSetObservers.ElementAt(i);

observer->Observe(static_cast<nsIDocument *>(this), "charset",
NS_ConvertASCIItoUTF16(aCharSetID).get());
charset16.get());
}
}
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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()) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);

Expand Down
7 changes: 4 additions & 3 deletions dom/base/nsDocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -424,10 +424,11 @@ class nsDocument : public nsIDocument,
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.
Expand Down Expand Up @@ -1048,7 +1049,7 @@ class nsDocument : public nsIDocument,

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.
Expand Down
Loading

0 comments on commit b515c9c

Please sign in to comment.