Skip to content

Commit

Permalink
Bug 1516366 - Move base classes from nsDocument to nsIDocument. r=smaug
Browse files Browse the repository at this point in the history
This is a big step in order to merge both.

Also allows to remove some very silly casts, though it causes us to add some
ToSupports around to deal with ambiguity of casts from nsIDocument to
nsISupports, and add a dummy nsISupports implementation that will go away later
in the series.

Differential Revision: https://phabricator.services.mozilla.com/D15352
  • Loading branch information
emilio committed Dec 29, 2018
1 parent ecc88f2 commit f131713
Show file tree
Hide file tree
Showing 36 changed files with 185 additions and 195 deletions.
9 changes: 1 addition & 8 deletions docshell/base/nsDocShell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11066,14 +11066,7 @@ nsDocShell::AddState(JS::Handle<JS::Value> aData, const nsAString& aTitle,
}
} else {
// It's a file:// URI
nsCOMPtr<nsIScriptObjectPrincipal> docScriptObj =
do_QueryInterface(document);

if (!docScriptObj) {
return NS_ERROR_DOM_SECURITY_ERR;
}

nsCOMPtr<nsIPrincipal> principal = docScriptObj->GetPrincipal();
nsCOMPtr<nsIPrincipal> principal = document->GetPrincipal();

if (!principal ||
NS_FAILED(principal->CheckMayLoad(newURI, true, false))) {
Expand Down
2 changes: 1 addition & 1 deletion dom/animation/AnimationEffect.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class AnimationEffect : public nsISupports, public nsWrapperCache {
return nullptr;
}

nsISupports* GetParentObject() const { return mDocument; }
nsISupports* GetParentObject() const { return ToSupports(mDocument); }

bool IsCurrent() const;
bool IsInEffect() const;
Expand Down
22 changes: 6 additions & 16 deletions dom/base/nsContentSink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -911,11 +911,6 @@ nsresult nsContentSink::SelectDocAppCache(

*aAction = CACHE_SELECTION_NONE;

nsCOMPtr<nsIApplicationCacheContainer> applicationCacheDocument =
do_QueryInterface(mDocument);
NS_ASSERTION(applicationCacheDocument,
"mDocument must implement nsIApplicationCacheContainer.");

if (aLoadApplicationCache) {
nsCOMPtr<nsIURI> groupURI;
rv = aLoadApplicationCache->GetManifestURI(getter_AddRefs(groupURI));
Expand Down Expand Up @@ -944,7 +939,7 @@ nsresult nsContentSink::SelectDocAppCache(
clientID.get(), docURISpec.get()));
#endif

rv = applicationCacheDocument->SetApplicationCache(aLoadApplicationCache);
rv = mDocument->SetApplicationCache(aLoadApplicationCache);
NS_ENSURE_SUCCESS(rv, rv);

// Document will be added as implicit entry to the cache as part of
Expand Down Expand Up @@ -981,11 +976,6 @@ nsresult nsContentSink::SelectDocAppCacheNoManifest(
if (aLoadApplicationCache) {
// The document was loaded from an application cache, use that
// application cache as the document's application cache.
nsCOMPtr<nsIApplicationCacheContainer> applicationCacheDocument =
do_QueryInterface(mDocument);
NS_ASSERTION(applicationCacheDocument,
"mDocument must implement nsIApplicationCacheContainer.");

#ifdef DEBUG
nsAutoCString docURISpec, clientID;
mDocumentURI->GetAsciiSpec(docURISpec);
Expand All @@ -996,7 +986,7 @@ nsresult nsContentSink::SelectDocAppCacheNoManifest(
clientID.get(), docURISpec.get()));
#endif

rv = applicationCacheDocument->SetApplicationCache(aLoadApplicationCache);
rv = mDocument->SetApplicationCache(aLoadApplicationCache);
NS_ENSURE_SUCCESS(rv, rv);

// Return the uri and invoke the update process for the selected
Expand Down Expand Up @@ -1563,13 +1553,13 @@ void nsContentSink::NotifyDocElementCreated(nsIDocument* aDoc) {
nsCOMPtr<nsIObserverService> observerService =
mozilla::services::GetObserverService();
if (observerService) {
observerService->NotifyObservers(aDoc, "document-element-inserted",
EmptyString().get());
observerService->NotifyObservers(
ToSupports(aDoc), "document-element-inserted", EmptyString().get());
}

nsContentUtils::DispatchChromeEvent(
aDoc, aDoc, NS_LITERAL_STRING("DOMDocElementInserted"), CanBubble::eYes,
Cancelable::eNo);
aDoc, ToSupports(aDoc), NS_LITERAL_STRING("DOMDocElementInserted"),
CanBubble::eYes, Cancelable::eNo);
}

NS_IMETHODIMP
Expand Down
64 changes: 33 additions & 31 deletions dom/base/nsDocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ void nsExternalResourceMap::Traverse(
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(*aCallback,
"mExternalResourceMap.mMap entry"
"->mDocument");
aCallback->NoteXPCOMChild(resource->mDocument);
aCallback->NoteXPCOMChild(ToSupports(resource->mDocument));

NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(*aCallback,
"mExternalResourceMap.mMap entry"
Expand Down Expand Up @@ -899,7 +899,8 @@ nsresult nsExternalResourceMap::AddExternalResource(

const nsTArray<nsCOMPtr<nsIObserver>>& obs = load->Observers();
for (uint32_t i = 0; i < obs.Length(); ++i) {
obs[i]->Observe(doc, "external-resource-document-created", nullptr);
obs[i]->Observe(ToSupports(doc), "external-resource-document-created",
nullptr);
}

return rv;
Expand Down Expand Up @@ -1624,6 +1625,20 @@ nsDocument::~nsDocument() {
mPlugins.Clear();
}

// In practice these three are always overriden by the nsDocument version, we
// just need them to avoid making nsIDocument::AddRef / Release ambiguous.
//
// We can get rid of these once we merge nsIDocument and nsDocument.
NS_IMETHODIMP_(MozExternalRefCountType) nsIDocument::Release() {
MOZ_CRASH("Should never be reachable");
}
NS_IMETHODIMP_(MozExternalRefCountType) nsIDocument::AddRef() {
MOZ_CRASH("Should never be reachable");
}
NS_IMETHODIMP nsIDocument::QueryInterface(REFNSIID aIID, void** aInstancePtr) {
MOZ_CRASH("Should never be reachable");
}

NS_INTERFACE_TABLE_HEAD(nsDocument)
NS_WRAPPERCACHE_INTERFACE_TABLE_ENTRY
NS_INTERFACE_TABLE_BEGIN
Expand Down Expand Up @@ -1804,7 +1819,7 @@ NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INTERNAL(nsDocument)
cb.NoteXPCOMChild(entry->mKey);
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb,
"mSubDocuments entry->mSubDocument");
cb.NoteXPCOMChild(entry->mSubDocument);
cb.NoteXPCOMChild(ToSupports(entry->mSubDocument));
}
}

Expand Down Expand Up @@ -3008,8 +3023,6 @@ void nsIDocument::RemoveFromIdTable(Element* aElement, nsAtom* aId) {
}
}

nsIPrincipal* nsDocument::GetPrincipal() { return NodePrincipal(); }

extern bool sDisablePrefetchHTTPSPref;

void nsIDocument::SetPrincipal(nsIPrincipal* aNewPrincipal) {
Expand Down Expand Up @@ -3096,16 +3109,14 @@ bool nsIDocument::IsScriptTracking(const nsACString& aURL) const {
}

NS_IMETHODIMP
nsDocument::GetApplicationCache(nsIApplicationCache** aApplicationCache) {
nsIDocument::GetApplicationCache(nsIApplicationCache** aApplicationCache) {
NS_IF_ADDREF(*aApplicationCache = mApplicationCache);

return NS_OK;
}

NS_IMETHODIMP
nsDocument::SetApplicationCache(nsIApplicationCache* aApplicationCache) {
nsIDocument::SetApplicationCache(nsIApplicationCache* aApplicationCache) {
mApplicationCache = aApplicationCache;

return NS_OK;
}

Expand Down Expand Up @@ -4094,7 +4105,7 @@ void nsIDocument::NotifyStyleSheetApplicableStateChanged() {
mozilla::services::GetObserverService();
if (observerService) {
observerService->NotifyObservers(
this, "style-sheet-applicable-state-changed", nullptr);
ToSupports(this), "style-sheet-applicable-state-changed", nullptr);
}
}

Expand Down Expand Up @@ -4706,7 +4717,7 @@ void nsIDocument::DispatchContentLoadedEvents() {
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
if (os) {
nsIPrincipal* principal = NodePrincipal();
os->NotifyObservers(this,
os->NotifyObservers(ToSupports(this),
nsContentUtils::IsSystemPrincipal(principal)
? "chrome-document-interactive"
: "content-document-interactive",
Expand All @@ -4716,7 +4727,7 @@ void nsIDocument::DispatchContentLoadedEvents() {
// Fire a DOM event notifying listeners that this document has been
// loaded (excluding images and other loads initiated by this
// document).
nsContentUtils::DispatchTrustedEvent(this, this,
nsContentUtils::DispatchTrustedEvent(this, ToSupports(this),
NS_LITERAL_STRING("DOMContentLoaded"),
CanBubble::eYes, Cancelable::eNo);

Expand Down Expand Up @@ -4787,11 +4798,9 @@ void nsIDocument::DispatchContentLoadedEvents() {
if (innerEvent) {
nsEventStatus status = nsEventStatus_eIgnore;

RefPtr<nsPresContext> context = parent->GetPresContext();

if (context) {
EventDispatcher::Dispatch(parent, context, innerEvent, event,
&status);
if (RefPtr<nsPresContext> context = parent->GetPresContext()) {
EventDispatcher::Dispatch(ToSupports(parent), context, innerEvent,
event, &status);
}
}
}
Expand All @@ -4805,7 +4814,7 @@ void nsIDocument::DispatchContentLoadedEvents() {
Element* root = GetRootElement();
if (root && root->HasAttr(kNameSpaceID_None, nsGkAtoms::manifest)) {
nsContentUtils::DispatchChromeEvent(
this, this, NS_LITERAL_STRING("MozApplicationManifest"),
this, ToSupports(this), NS_LITERAL_STRING("MozApplicationManifest"),
CanBubble::eYes, Cancelable::eYes);
}

Expand Down Expand Up @@ -5920,7 +5929,7 @@ void nsIDocument::DoNotifyPossibleTitleChange() {
}

// Fire a DOM event for the title change.
nsContentUtils::DispatchChromeEvent(this, static_cast<nsIDocument*>(this),
nsContentUtils::DispatchChromeEvent(this, ToSupports(this),
NS_LITERAL_STRING("DOMTitleChanged"),
CanBubble::eYes, Cancelable::eYes);
}
Expand Down Expand Up @@ -6491,7 +6500,7 @@ nsINode* nsIDocument::AdoptNode(nsINode& aAdoptedNode, ErrorResult& rv) {
// canonical scope. But we try to pass something sane anyway.
JSAutoRealm ar(cx, GetScopeObject()->GetGlobalJSObject());
JS::Rooted<JS::Value> v(cx);
rv = nsContentUtils::WrapNative(cx, this, this, &v,
rv = nsContentUtils::WrapNative(cx, ToSupports(this), this, &v,
/* aAllowWrapping = */ false);
if (rv.Failed()) return nullptr;
newScope = &v.toObject();
Expand Down Expand Up @@ -7823,7 +7832,7 @@ void nsIDocument::OnPageShow(bool aPersisted, EventTarget* aDispatchStartTarget,
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
if (os) {
nsIPrincipal* principal = NodePrincipal();
os->NotifyObservers(this,
os->NotifyObservers(ToSupports(this),
nsContentUtils::IsSystemPrincipal(principal)
? "chrome-page-shown"
: "content-page-shown",
Expand Down Expand Up @@ -7920,7 +7929,7 @@ void nsIDocument::OnPageHide(bool aPersisted, EventTarget* aDispatchStartTarget,
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
if (os) {
nsIPrincipal* principal = NodePrincipal();
os->NotifyObservers(this,
os->NotifyObservers(ToSupports(this),
nsContentUtils::IsSystemPrincipal(principal)
? "chrome-page-hidden"
: "content-page-hidden",
Expand Down Expand Up @@ -10886,7 +10895,7 @@ void nsIDocument::UpdateVisibilityState() {
dom::VisibilityState oldState = mVisibilityState;
mVisibilityState = ComputeVisibilityState();
if (oldState != mVisibilityState) {
nsContentUtils::DispatchTrustedEvent(this, static_cast<nsIDocument*>(this),
nsContentUtils::DispatchTrustedEvent(this, ToSupports(this),
NS_LITERAL_STRING("visibilitychange"),
CanBubble::eYes, Cancelable::eNo);
EnumerateActivityObservers(NotifyActivityChanged, nullptr);
Expand Down Expand Up @@ -12531,14 +12540,7 @@ bool nsIDocument::IsThirdParty() {
}

nsCOMPtr<nsIPrincipal> principal = NodePrincipal();
nsCOMPtr<nsIScriptObjectPrincipal> sop =
do_QueryInterface(parentDocument, &rv);
if (NS_WARN_IF(NS_FAILED(rv) || !sop)) {
// Failure
mIsThirdParty.emplace(true);
return mIsThirdParty.value();
}
nsCOMPtr<nsIPrincipal> parentPrincipal = sop->GetPrincipal();
nsCOMPtr<nsIPrincipal> parentPrincipal = parentDocument->GetPrincipal();

bool principalsMatch = false;
rv = principal->Equals(parentPrincipal, &principalsMatch);
Expand Down
70 changes: 3 additions & 67 deletions dom/base/nsDocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include "nsCOMPtr.h"
#include "nsAutoPtr.h"
#include "nsCRT.h"
#include "nsIWeakReferenceUtils.h"
#include "nsTArray.h"
#include "nsIdentifierMapEntry.h"
#include "nsStubDocumentObserver.h"
Expand All @@ -28,8 +27,6 @@
#include "nsBindingManager.h"
#include "nsRefPtrHashtable.h"
#include "nsJSThingHashtable.h"
#include "nsIScriptObjectPrincipal.h"
#include "nsIRadioGroupContainer.h"
#include "nsILayoutHistoryState.h"
#include "nsIRequest.h"
#include "nsILoadGroup.h"
Expand Down Expand Up @@ -95,12 +92,7 @@ class nsOnloadBlocker final : public nsIRequest {
};

// Base class for our document implementations.
class nsDocument : public nsIDocument,
public nsSupportsWeakReference,
public nsIScriptObjectPrincipal,
public nsIRadioGroupContainer,
public nsIApplicationCacheContainer,
public nsStubMutationObserver {
class nsDocument : public nsIDocument {
friend class nsIDocument;

public:
Expand Down Expand Up @@ -135,53 +127,7 @@ class nsDocument : public nsIDocument,
virtual void BeginLoad() override;
virtual void EndLoad() override;

// nsIRadioGroupContainer
NS_IMETHOD WalkRadioGroup(const nsAString& aName, nsIRadioVisitor* aVisitor,
bool aFlushContent) override {
return DocumentOrShadowRoot::WalkRadioGroup(aName, aVisitor, aFlushContent);
}
virtual void SetCurrentRadioButton(
const nsAString& aName, mozilla::dom::HTMLInputElement* aRadio) override {
DocumentOrShadowRoot::SetCurrentRadioButton(aName, aRadio);
}
virtual mozilla::dom::HTMLInputElement* GetCurrentRadioButton(
const nsAString& aName) override {
return DocumentOrShadowRoot::GetCurrentRadioButton(aName);
}
NS_IMETHOD
GetNextRadioButton(const nsAString& aName, const bool aPrevious,
mozilla::dom::HTMLInputElement* aFocusedRadio,
mozilla::dom::HTMLInputElement** aRadioOut) override {
return DocumentOrShadowRoot::GetNextRadioButton(aName, aPrevious,
aFocusedRadio, aRadioOut);
}
virtual void AddToRadioGroup(
const nsAString& aName, mozilla::dom::HTMLInputElement* aRadio) override {
DocumentOrShadowRoot::AddToRadioGroup(aName, aRadio);
}
virtual void RemoveFromRadioGroup(
const nsAString& aName, mozilla::dom::HTMLInputElement* aRadio) override {
DocumentOrShadowRoot::RemoveFromRadioGroup(aName, aRadio);
}
virtual uint32_t GetRequiredRadioCount(
const nsAString& aName) const override {
return DocumentOrShadowRoot::GetRequiredRadioCount(aName);
}
virtual void RadioRequiredWillChange(const nsAString& aName,
bool aRequiredAdded) override {
DocumentOrShadowRoot::RadioRequiredWillChange(aName, aRequiredAdded);
}
virtual bool GetValueMissingState(const nsAString& aName) const override {
return DocumentOrShadowRoot::GetValueMissingState(aName);
}
virtual void SetValueMissingState(const nsAString& aName,
bool aValue) override {
return DocumentOrShadowRoot::SetValueMissingState(aName, aValue);
}

// Check whether shadow DOM is enabled for the document this node belongs to.
// Same as above, but also checks that the caller is either chrome or some
// addon.
// Checks that the caller is either chrome or some addon.
static bool IsCallerChromeOrAddon(JSContext* aCx, JSObject* aObject);

public:
Expand All @@ -196,12 +142,6 @@ class nsDocument : public nsIDocument,
virtual mozilla::EventListenerManager* GetExistingListenerManager()
const override;

// nsIScriptObjectPrincipal
virtual nsIPrincipal* GetPrincipal() override;

// nsIApplicationCacheContainer
NS_DECL_NSIAPPLICATIONCACHECONTAINER

virtual nsresult Init();

virtual void Destroy() override;
Expand All @@ -211,7 +151,7 @@ class nsDocument : public nsIDocument,
virtual void UnblockOnload(bool aFireSync) override;

NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS_AMBIGUOUS(nsDocument,
nsIDocument)
nsINode)

void SetLoadedAsData(bool aLoadedAsData) { mLoadedAsData = aLoadedAsData; }
void SetLoadedAsInteractiveData(bool aLoadedAsInteractiveData) {
Expand Down Expand Up @@ -268,10 +208,6 @@ class nsDocument : public nsIDocument,

friend class nsCallRequestFullscreen;

// The application cache that this document is associated with, if
// any. This can change during the lifetime of the document.
nsCOMPtr<nsIApplicationCache> mApplicationCache;

nsCOMPtr<nsIContent> mFirstBaseNodeWithHref;

private:
Expand Down
Loading

0 comments on commit f131713

Please sign in to comment.