gecko-dev/dom/src/base/nsDOMWindowList.cpp

177 lines
4.6 KiB
C++
Raw Normal View History

1999-02-01 17:16:17 +00:00
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
1999-02-01 17:16:17 +00:00
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
1999-02-01 17:16:17 +00:00
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Netscape
1999-02-01 17:16:17 +00:00
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
* travis@netscape.com
1999-02-01 17:16:17 +00:00
*/
#include "nsCOMPtr.h"
1999-02-01 17:16:17 +00:00
#include "nsDOMWindowList.h"
#include "nsIDocShell.h"
#include "nsIDocShellTreeNode.h"
#include "nsIDocShellTreeItem.h"
1999-02-01 17:16:17 +00:00
#include "nsIScriptGlobalObject.h"
#include "nsIDOMDocument.h"
1999-02-01 17:16:17 +00:00
#include "nsIDOMWindow.h"
#include "nsIDocument.h"
#include "nsIInterfaceRequestor.h"
1999-02-01 17:16:17 +00:00
nsDOMWindowList::nsDOMWindowList(nsIDocShell *aDocShell)
1999-02-01 17:16:17 +00:00
{
NS_INIT_REFCNT();
mScriptObject = nsnull;
SetDocShell(aDocShell);
1999-02-01 17:16:17 +00:00
}
nsDOMWindowList::~nsDOMWindowList()
{
}
NS_IMPL_ADDREF(nsDOMWindowList)
NS_IMPL_RELEASE(nsDOMWindowList)
NS_INTERFACE_MAP_BEGIN(nsDOMWindowList)
NS_INTERFACE_MAP_ENTRY(nsIDOMWindowCollection)
NS_INTERFACE_MAP_ENTRY(nsIScriptObjectOwner)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMWindowCollection)
NS_INTERFACE_MAP_END
1999-02-01 17:16:17 +00:00
NS_IMETHODIMP
nsDOMWindowList::SetDocShell(nsIDocShell* aDocShell)
1999-02-01 17:16:17 +00:00
{
nsCOMPtr<nsIDocShellTreeNode> docShellAsNode(do_QueryInterface(aDocShell));
mDocShellNode = docShellAsNode.get(); // Weak Reference
1999-02-01 17:16:17 +00:00
return NS_OK;
}
NS_IMETHODIMP
nsDOMWindowList::GetLength(PRUint32* aLength)
{
nsresult ret;
PRInt32 length;
1999-02-01 17:16:17 +00:00
*aLength = 0;
if (mDocShellNode) {
nsCOMPtr<nsIDocShell> shell = do_QueryInterface(mDocShellNode);
if (shell) {
nsCOMPtr<nsIDOMDocument> domdoc;
shell->GetDocument(getter_AddRefs(domdoc));
if (domdoc) {
nsCOMPtr<nsIDocument> doc = do_QueryInterface(domdoc);
if (doc) {
doc->FlushPendingNotifications();
}
}
}
ret = mDocShellNode->GetChildCount(&length);
*aLength = length;
}
1999-02-01 17:16:17 +00:00
return ret;
}
NS_IMETHODIMP
nsDOMWindowList::Item(PRUint32 aIndex, nsIDOMWindow** aReturn)
{
nsCOMPtr<nsIDocShellTreeItem> item;
*aReturn = nsnull;
if (mDocShellNode) {
nsCOMPtr<nsIDocShell> shell = do_QueryInterface(mDocShellNode);
if (shell) {
nsCOMPtr<nsIDOMDocument> domdoc;
shell->GetDocument(getter_AddRefs(domdoc));
if (domdoc) {
nsCOMPtr<nsIDocument> doc = do_QueryInterface(domdoc);
if (doc) {
doc->FlushPendingNotifications();
}
}
}
mDocShellNode->GetChildAt(aIndex, getter_AddRefs(item));
nsCOMPtr<nsIScriptGlobalObject> globalObject(do_GetInterface(item));
NS_ASSERTION(globalObject, "Couldn't get to the globalObject");
if (globalObject) {
CallQueryInterface(globalObject.get(), aReturn);
}
1999-02-01 17:16:17 +00:00
}
return NS_OK;
}
NS_IMETHODIMP
nsDOMWindowList::NamedItem(const nsString& aName, nsIDOMWindow** aReturn)
{
nsCOMPtr<nsIDocShellTreeItem> item;
*aReturn = nsnull;
if (mDocShellNode) {
nsCOMPtr<nsIDocShell> shell = do_QueryInterface(mDocShellNode);
if (shell) {
nsCOMPtr<nsIDOMDocument> domdoc;
shell->GetDocument(getter_AddRefs(domdoc));
if (domdoc) {
nsCOMPtr<nsIDocument> doc = do_QueryInterface(domdoc);
if (doc) {
doc->FlushPendingNotifications();
}
}
}
mDocShellNode->FindChildWithName(aName.GetUnicode(), PR_FALSE,
nsnull, getter_AddRefs(item));
nsCOMPtr<nsIScriptGlobalObject> globalObject(do_GetInterface(item));
NS_ASSERTION(globalObject, "Couldn't get to the globalObject");
if (globalObject) {
CallQueryInterface(globalObject.get(), aReturn);
}
1999-02-01 17:16:17 +00:00
}
1999-02-01 17:16:17 +00:00
return NS_OK;
}
NS_IMETHODIMP
nsDOMWindowList::GetScriptObject(nsIScriptContext *aContext, void** aScriptObject)
{
nsresult res = NS_OK;
nsIScriptGlobalObject *global = aContext->GetGlobalObject();
if (nsnull == mScriptObject) {
res = NS_NewScriptWindowCollection(aContext, (nsISupports *)(nsIDOMWindowCollection *)this, global, (void**)&mScriptObject);
}
*aScriptObject = mScriptObject;
NS_RELEASE(global);
return res;
}
NS_IMETHODIMP
nsDOMWindowList::SetScriptObject(void *aScriptObject)
{
mScriptObject = aScriptObject;
return NS_OK;
}