2004-04-19 02:14:17 +04:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla 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/MPL/
|
|
|
|
*
|
|
|
|
* 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-12-06 09:32:12 +03:00
|
|
|
* The Original Code is XMLterm.
|
2004-04-19 02:14:17 +04:00
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Ramalingam Saravanan.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1999
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
1999-12-06 09:32:12 +03:00
|
|
|
* Contributor(s):
|
2004-04-19 02:14:17 +04:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1999-12-06 09:32:12 +03:00
|
|
|
|
|
|
|
// mozXMLTermUtils.cpp: XMLTerm utilities implementation
|
|
|
|
|
|
|
|
#include "nscore.h"
|
|
|
|
#include "nspr.h"
|
|
|
|
#include "prlog.h"
|
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsString.h"
|
1999-12-26 18:19:45 +03:00
|
|
|
#include "nsIInterfaceRequestor.h"
|
2001-09-06 01:28:38 +04:00
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
1999-12-06 09:32:12 +03:00
|
|
|
|
|
|
|
#include "nsIContentViewer.h"
|
|
|
|
#include "nsIDocumentViewer.h"
|
2004-08-01 03:15:21 +04:00
|
|
|
#include "nsPresContext.h"
|
1999-12-06 09:32:12 +03:00
|
|
|
#include "nsIPresShell.h"
|
|
|
|
#include "nsIDeviceContext.h"
|
|
|
|
|
|
|
|
#include "nsIPrincipal.h"
|
|
|
|
#include "nsIScriptContext.h"
|
|
|
|
#include "nsIScriptGlobalObject.h"
|
|
|
|
#include "nsIDOMWindowCollection.h"
|
2005-11-29 04:01:01 +03:00
|
|
|
#include "nsPIDOMWindow.h"
|
2000-03-06 18:16:48 +03:00
|
|
|
#include "nsIDOMElement.h"
|
1999-12-06 09:32:12 +03:00
|
|
|
#include "nsIDocument.h"
|
|
|
|
|
|
|
|
#include "mozXMLT.h"
|
|
|
|
#include "mozXMLTermUtils.h"
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2000-02-14 04:17:55 +03:00
|
|
|
/** Gets DOM window for doc shell
|
|
|
|
* @param aDocShell doc shell
|
1999-12-06 09:32:12 +03:00
|
|
|
* @param aDOMWindow returned DOM window (frame)
|
|
|
|
* @return NS_OK on success
|
|
|
|
*/
|
|
|
|
NS_EXPORT nsresult
|
2000-02-14 04:17:55 +03:00
|
|
|
mozXMLTermUtils::ConvertDocShellToDOMWindow(nsIDocShell* aDocShell,
|
2000-09-01 05:54:35 +04:00
|
|
|
nsIDOMWindowInternal** aDOMWindow)
|
1999-12-06 09:32:12 +03:00
|
|
|
{
|
2000-02-14 04:17:55 +03:00
|
|
|
XMLT_LOG(mozXMLTermUtils::ConvertDocShellToDOMWindow,30,("\n"));
|
1999-12-06 09:32:12 +03:00
|
|
|
|
|
|
|
if (!aDOMWindow)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
*aDOMWindow = nsnull;
|
|
|
|
|
2005-11-29 02:56:44 +03:00
|
|
|
nsCOMPtr<nsIDOMWindowInternal> domWindow(do_GetInterface(aDocShell));
|
1999-12-06 09:32:12 +03:00
|
|
|
if (!domWindow)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2005-11-29 02:56:44 +03:00
|
|
|
domWindow.swap(*aDOMWindow);
|
1999-12-06 09:32:12 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-02-14 04:17:55 +03:00
|
|
|
/** Gets doc shell for DOM window
|
1999-12-06 09:32:12 +03:00
|
|
|
* @param aDOMWindow DOM window (frame)
|
2000-02-14 04:17:55 +03:00
|
|
|
* @param aDocShell returned doc shell
|
1999-12-06 09:32:12 +03:00
|
|
|
* @return NS_OK on success
|
|
|
|
*/
|
|
|
|
NS_EXPORT nsresult
|
2000-09-01 05:54:35 +04:00
|
|
|
mozXMLTermUtils::ConvertDOMWindowToDocShell(nsIDOMWindowInternal* aDOMWindow,
|
2000-02-14 04:17:55 +03:00
|
|
|
nsIDocShell** aDocShell)
|
1999-12-06 09:32:12 +03:00
|
|
|
{
|
2000-02-14 04:17:55 +03:00
|
|
|
XMLT_LOG(mozXMLTermUtils::ConvertDOMWindowToDocShell,30,("\n"));
|
1999-12-06 09:32:12 +03:00
|
|
|
|
2005-11-29 02:56:44 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aDOMWindow);
|
|
|
|
if (!window)
|
1999-12-06 09:32:12 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2005-11-29 02:56:44 +03:00
|
|
|
*aDocShell = window->GetDocShell();
|
2000-02-14 04:17:55 +03:00
|
|
|
if (!*aDocShell)
|
1999-12-06 09:32:12 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2004-02-10 03:51:35 +03:00
|
|
|
NS_ADDREF(*aDocShell);
|
|
|
|
|
1999-12-06 09:32:12 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Locates named inner DOM window (frame) inside outer DOM window
|
|
|
|
* @param outerDOMWindow outer DOM window (frame)
|
|
|
|
* @param innerFrameName name of inner frame to be returned
|
|
|
|
* @param innerDOMWindow returned inner DOM window (frame)
|
|
|
|
* @return NS_OK on success
|
|
|
|
*/
|
|
|
|
NS_EXPORT nsresult
|
2000-09-01 05:54:35 +04:00
|
|
|
mozXMLTermUtils::GetInnerDOMWindow(nsIDOMWindowInternal* outerDOMWindow,
|
1999-12-06 09:32:12 +03:00
|
|
|
const nsString& innerFrameName,
|
2000-09-01 05:54:35 +04:00
|
|
|
nsIDOMWindowInternal** innerDOMWindow)
|
1999-12-06 09:32:12 +03:00
|
|
|
{
|
|
|
|
nsresult result;
|
|
|
|
|
|
|
|
XMLT_LOG(mozXMLTermUtils::GetInnerDOMWindow,30,("\n"));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMWindowCollection> innerDOMWindowList;
|
|
|
|
result = outerDOMWindow->GetFrames(getter_AddRefs(innerDOMWindowList));
|
|
|
|
if (NS_FAILED(result) || !innerDOMWindowList)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
PRUint32 frameCount = 0;
|
|
|
|
result = innerDOMWindowList->GetLength(&frameCount);
|
|
|
|
XMLT_LOG(mozXMLTermUtils::GetInnerDOMWindow,31,("frameCount=%d\n",
|
|
|
|
frameCount));
|
|
|
|
|
2000-09-01 11:41:46 +04:00
|
|
|
result = innerDOMWindowList->NamedItem(innerFrameName, (nsIDOMWindow **)innerDOMWindow);
|
1999-12-06 09:32:12 +03:00
|
|
|
if (NS_FAILED(result) || !*innerDOMWindow)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Gets the scrollable view for presentation context
|
|
|
|
* @param aPresContext presentation context
|
|
|
|
* @param aScrollableView returned scrollable view
|
|
|
|
* @return NS_OK on success
|
|
|
|
*/
|
|
|
|
NS_EXPORT nsresult
|
2004-08-01 03:15:21 +04:00
|
|
|
mozXMLTermUtils::GetPresContextScrollableView(nsPresContext* aPresContext,
|
1999-12-06 09:32:12 +03:00
|
|
|
nsIScrollableView** aScrollableView)
|
|
|
|
{
|
|
|
|
XMLT_LOG(mozXMLTermUtils::GetPresContextScrollableView,30,("\n"));
|
|
|
|
|
|
|
|
if (!aScrollableView)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
*aScrollableView = nsnull;
|
|
|
|
|
2003-12-21 08:36:36 +03:00
|
|
|
nsIPresShell *presShell = aPresContext->GetPresShell();
|
|
|
|
if (!presShell)
|
1999-12-06 09:32:12 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2003-09-23 21:05:29 +04:00
|
|
|
nsIViewManager* viewManager = presShell->GetViewManager();
|
|
|
|
if (!viewManager)
|
1999-12-06 09:32:12 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
return viewManager->GetRootScrollableView(aScrollableView);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Gets the device context for presentation context
|
|
|
|
* @param aPresContext presentation context
|
|
|
|
* @param aDeviceContext returned device context
|
|
|
|
* @return NS_OK on success
|
|
|
|
*/
|
|
|
|
NS_EXPORT nsresult
|
2004-08-01 03:15:21 +04:00
|
|
|
mozXMLTermUtils::GetPresContextDeviceContext(nsPresContext* aPresContext,
|
1999-12-06 09:32:12 +03:00
|
|
|
nsIDeviceContext** aDeviceContext)
|
|
|
|
{
|
|
|
|
nsresult result;
|
|
|
|
|
|
|
|
XMLT_LOG(mozXMLTermUtils::GetPresContextScrollableView,30,("\n"));
|
|
|
|
|
|
|
|
if (!aDeviceContext)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
*aDeviceContext = nsnull;
|
|
|
|
|
2003-09-23 21:05:29 +04:00
|
|
|
nsIViewManager* viewManager = aPresContext->GetViewManager();
|
|
|
|
if (!viewManager)
|
1999-12-06 09:32:12 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDeviceContext> deviceContext;
|
|
|
|
result = viewManager->GetDeviceContext(*getter_AddRefs(deviceContext));
|
|
|
|
if (NS_FAILED(result) || !deviceContext)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
*aDeviceContext = deviceContext.get();
|
|
|
|
NS_ADDREF(*aDeviceContext);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Gets the script context for document
|
|
|
|
* @param aDOMDocument document providing context
|
|
|
|
* @param aScriptContext returned script context
|
|
|
|
* @return NS_OK on success
|
|
|
|
*/
|
|
|
|
NS_EXPORT nsresult
|
|
|
|
mozXMLTermUtils::GetScriptContext(nsIDOMDocument* aDOMDocument,
|
|
|
|
nsIScriptContext** aScriptContext)
|
|
|
|
{
|
|
|
|
XMLT_LOG(mozXMLTermUtils::GetScriptContext,20,("\n"));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> doc ( do_QueryInterface(aDOMDocument) );
|
|
|
|
if (!doc)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2003-10-22 11:01:13 +04:00
|
|
|
nsIScriptGlobalObject *scriptGlobalObject = doc->GetScriptGlobalObject();
|
1999-12-06 09:32:12 +03:00
|
|
|
|
2003-10-22 11:01:13 +04:00
|
|
|
if (!scriptGlobalObject)
|
1999-12-06 09:32:12 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2004-02-10 03:51:35 +03:00
|
|
|
*aScriptContext = scriptGlobalObject->GetContext();
|
|
|
|
NS_IF_ADDREF(*aScriptContext);
|
|
|
|
|
|
|
|
return NS_OK;
|
1999-12-06 09:32:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Executes script in specified document's context.
|
|
|
|
* @param aDOMDocument document providing context for script execution
|
|
|
|
* @param aScript string to be executed
|
|
|
|
* @param aOutput output string produced by script execution
|
|
|
|
* @return NS_OK if script was valid and got executed properly
|
|
|
|
*/
|
|
|
|
NS_EXPORT nsresult
|
|
|
|
mozXMLTermUtils::ExecuteScript(nsIDOMDocument* aDOMDocument,
|
|
|
|
const nsString& aScript,
|
|
|
|
nsString& aOutput)
|
|
|
|
{
|
|
|
|
nsresult result;
|
|
|
|
|
|
|
|
XMLT_LOG(mozXMLTermUtils::ExecuteScript,20,("\n"));
|
|
|
|
|
|
|
|
// Get document principal
|
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryInterface(aDOMDocument);
|
|
|
|
if (!doc)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2006-04-27 22:21:11 +04:00
|
|
|
nsIPrincipal *docPrincipal = doc->NodePrincipal();
|
1999-12-06 09:32:12 +03:00
|
|
|
|
|
|
|
// Get document script context
|
|
|
|
nsCOMPtr<nsIScriptContext> scriptContext;
|
|
|
|
result = GetScriptContext(aDOMDocument, getter_AddRefs(scriptContext));
|
|
|
|
if (NS_FAILED(result) || !scriptContext)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// Execute script
|
|
|
|
PRBool isUndefined = PR_FALSE;
|
|
|
|
const char* URL = "";
|
1999-12-26 18:19:45 +03:00
|
|
|
result = scriptContext->EvaluateString(aScript, (void *) nsnull,
|
|
|
|
docPrincipal, URL, 0, nsnull,
|
2004-06-11 04:48:00 +04:00
|
|
|
&aOutput, &isUndefined);
|
1999-12-26 18:19:45 +03:00
|
|
|
|
2000-11-28 00:38:14 +03:00
|
|
|
XMLT_LOG(mozXMLTermUtils::ExecuteScript,21,("result=0x%x,isUndefined=0x%x\n",
|
1999-12-26 18:19:45 +03:00
|
|
|
result, isUndefined));
|
|
|
|
|
1999-12-06 09:32:12 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-06 18:16:48 +03:00
|
|
|
/** Returns the specified attribute value associated with a DOM node,
|
|
|
|
* or a null string if the attribute is not defined, or if the DOM node
|
|
|
|
* is not an element.
|
|
|
|
* @param aDOMNode node whose attribute is to be determined
|
2004-08-03 17:46:52 +04:00
|
|
|
* @param aAttName attribute to be determined. Must be ASCII.
|
2000-03-06 18:16:48 +03:00
|
|
|
* @param aAttValue output attribute value
|
|
|
|
* @return NS_OK if no errors occurred
|
|
|
|
*/
|
|
|
|
NS_EXPORT nsresult
|
|
|
|
mozXMLTermUtils::GetNodeAttribute(nsIDOMNode* aDOMNode,
|
|
|
|
const char* aAttName,
|
|
|
|
nsString& aAttValue)
|
|
|
|
{
|
|
|
|
XMLT_LOG(mozXMLTermUtils::GetNodeAttribute,20,("aAttName=%s\n", aAttName));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMElement> domElement = do_QueryInterface(aDOMNode);
|
|
|
|
if (!domElement) {
|
2000-04-28 14:55:20 +04:00
|
|
|
aAttValue.SetLength(0);
|
2000-03-06 18:16:48 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-04-28 14:55:20 +04:00
|
|
|
nsAutoString attName;
|
2004-08-03 17:46:52 +04:00
|
|
|
attName.AssignASCII(aAttName);
|
2000-03-06 18:16:48 +03:00
|
|
|
return domElement->GetAttribute(attName, aAttValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-12-06 09:32:12 +03:00
|
|
|
/** Returns a timestamp string containing the local time, if at least
|
|
|
|
* deltaSec seconds have elapsed since the last timestamp. Otherwise,
|
|
|
|
* a null string is returned.
|
|
|
|
* @param deltaSec minimum elapsed seconds since last timestamp (>=0)
|
|
|
|
* @param lastTime in/out parameter containing time of last timestamp
|
|
|
|
* @param aTimeStamp returned timestamp string
|
|
|
|
* @return NS_OK on success
|
|
|
|
*/
|
|
|
|
NS_IMETHODIMP mozXMLTermUtils::TimeStamp(PRInt32 deltaSec, PRTime& lastTime,
|
|
|
|
nsString& aTimeStamp)
|
|
|
|
{
|
|
|
|
static const PRInt32 DATE_LEN = 19;
|
|
|
|
PRTime deltaTime ;
|
|
|
|
char dateStr[DATE_LEN+1];
|
|
|
|
PRTime curTime, difTime;
|
|
|
|
|
|
|
|
curTime = PR_Now();
|
|
|
|
LL_SUB(difTime, curTime, lastTime);
|
|
|
|
|
|
|
|
LL_I2L(deltaTime, deltaSec*1000000);
|
|
|
|
if (LL_CMP(difTime, <, deltaTime)) {
|
|
|
|
// Not enough time has elapsed for a new time stamp
|
2000-04-28 14:55:20 +04:00
|
|
|
aTimeStamp.SetLength(0);
|
1999-12-06 09:32:12 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
lastTime = curTime;
|
|
|
|
|
|
|
|
// Current local time
|
|
|
|
PRExplodedTime localTime;
|
|
|
|
PR_ExplodeTime(curTime, PR_LocalTimeParameters, &localTime);
|
|
|
|
|
|
|
|
PRInt32 nWritten = PR_snprintf(dateStr, DATE_LEN+1,
|
2004-08-03 17:46:52 +04:00
|
|
|
"%02d:%02d:%02d %02d/%02d/%04d", // XXX i18n!
|
1999-12-06 09:32:12 +03:00
|
|
|
localTime.tm_hour, localTime.tm_min, localTime.tm_sec,
|
|
|
|
localTime.tm_mday, localTime.tm_month+1, localTime.tm_year);
|
|
|
|
|
|
|
|
if (nWritten != DATE_LEN)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
XMLT_LOG(mozXMLTermUtils::LocalTime,99,("localTime=%s\n", dateStr));
|
|
|
|
|
2004-08-03 17:46:52 +04:00
|
|
|
aTimeStamp.AssignASCII(dateStr);
|
1999-12-06 09:32:12 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Returns a string containing a 11-digit random cookie based upon the
|
|
|
|
* current local time and the elapsed execution of the program.
|
|
|
|
* @param aCookie returned cookie string
|
|
|
|
* @return NS_OK on success
|
|
|
|
*/
|
|
|
|
NS_IMETHODIMP mozXMLTermUtils::RandomCookie(nsString& aCookie)
|
|
|
|
{
|
|
|
|
// Current local time
|
|
|
|
PRExplodedTime localTime;
|
|
|
|
PR_ExplodeTime(PR_Now(), PR_LocalTimeParameters, &localTime);
|
|
|
|
|
|
|
|
PRInt32 randomNumberA = localTime.tm_sec*1000000+localTime.tm_usec;
|
|
|
|
PRIntervalTime randomNumberB = PR_IntervalNow();
|
|
|
|
|
|
|
|
XMLT_LOG(mozXMLTermUtils::RandomCookie,30,("ranA=0x%x, ranB=0x%x\n",
|
|
|
|
randomNumberA, randomNumberB));
|
|
|
|
PR_ASSERT(randomNumberA >= 0);
|
|
|
|
PR_ASSERT(randomNumberB >= 0);
|
|
|
|
|
|
|
|
static const char cookieDigits[17] = "0123456789abcdef";
|
|
|
|
char cookie[12];
|
|
|
|
int j;
|
|
|
|
|
|
|
|
for (j=0; j<6; j++) {
|
|
|
|
cookie[j] = cookieDigits[randomNumberA%16];
|
|
|
|
randomNumberA = randomNumberA/16;
|
|
|
|
}
|
|
|
|
for (j=6; j<11; j++) {
|
|
|
|
cookie[j] = cookieDigits[randomNumberB%16];
|
|
|
|
randomNumberB = randomNumberB/16;
|
|
|
|
}
|
|
|
|
cookie[11] = '\0';
|
|
|
|
|
2004-08-03 17:46:52 +04:00
|
|
|
aCookie.AssignASCII(cookie);
|
1999-12-06 09:32:12 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|