Back to index

lightning-sunbird  0.9+nobinonly
mozXMLTermUtils.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is XMLterm.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Ramalingam Saravanan.
00018  * Portions created by the Initial Developer are Copyright (C) 1999
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 // mozXMLTermUtils.cpp: XMLTerm utilities implementation
00038 
00039 #include "nscore.h"
00040 #include "nspr.h"
00041 #include "prlog.h"
00042 
00043 #include "nsCOMPtr.h"
00044 #include "nsString.h"
00045 #include "nsIInterfaceRequestor.h"
00046 #include "nsIInterfaceRequestorUtils.h"
00047 
00048 #include "nsIContentViewer.h"
00049 #include "nsIDocumentViewer.h"
00050 #include "nsPresContext.h"
00051 #include "nsIPresShell.h"
00052 #include "nsIDeviceContext.h"
00053 
00054 #include "nsIPrincipal.h"
00055 #include "nsIScriptContext.h"
00056 #include "nsIScriptGlobalObject.h"
00057 #include "nsIDOMWindowCollection.h"
00058 #include "nsIDOMElement.h"
00059 #include "nsIDocument.h"
00060 
00061 #include "mozXMLT.h"
00062 #include "mozXMLTermUtils.h"
00063 
00065 
00071 NS_EXPORT nsresult
00072 mozXMLTermUtils::ConvertDocShellToDOMWindow(nsIDocShell* aDocShell,
00073                                     nsIDOMWindowInternal** aDOMWindow)
00074 {
00075   XMLT_LOG(mozXMLTermUtils::ConvertDocShellToDOMWindow,30,("\n"));
00076 
00077   if (!aDOMWindow)
00078     return NS_ERROR_FAILURE;
00079 
00080   *aDOMWindow = nsnull;
00081 
00082   nsCOMPtr<nsIScriptGlobalObject> scriptGlobalObject(do_GetInterface(aDocShell));
00083 
00084   nsCOMPtr<nsIDOMWindowInternal> domWindow(do_QueryInterface(scriptGlobalObject));
00085   if (!domWindow)
00086     return NS_ERROR_FAILURE;
00087 
00088   *aDOMWindow = domWindow.get();
00089   NS_ADDREF(*aDOMWindow);
00090 
00091   return NS_OK;
00092 }
00093 
00094 
00100 NS_EXPORT nsresult
00101 mozXMLTermUtils::ConvertDOMWindowToDocShell(nsIDOMWindowInternal* aDOMWindow,
00102                                              nsIDocShell** aDocShell)
00103 {
00104   XMLT_LOG(mozXMLTermUtils::ConvertDOMWindowToDocShell,30,("\n"));
00105 
00106   nsCOMPtr<nsIScriptGlobalObject> globalObject = do_QueryInterface(aDOMWindow);
00107   if (!globalObject)
00108     return NS_ERROR_FAILURE;
00109 
00110   *aDocShell = globalObject->GetDocShell();
00111   if (!*aDocShell)
00112     return NS_ERROR_FAILURE;
00113 
00114   NS_ADDREF(*aDocShell);
00115 
00116   return NS_OK;
00117 }
00118 
00119 
00126 NS_EXPORT nsresult
00127 mozXMLTermUtils::GetInnerDOMWindow(nsIDOMWindowInternal* outerDOMWindow,
00128                                     const nsString& innerFrameName,
00129                                     nsIDOMWindowInternal** innerDOMWindow)
00130 {
00131   nsresult result;
00132 
00133   XMLT_LOG(mozXMLTermUtils::GetInnerDOMWindow,30,("\n"));
00134 
00135   nsCOMPtr<nsIDOMWindowCollection> innerDOMWindowList;
00136   result = outerDOMWindow->GetFrames(getter_AddRefs(innerDOMWindowList));
00137   if (NS_FAILED(result) || !innerDOMWindowList)
00138     return NS_ERROR_FAILURE;
00139 
00140   PRUint32 frameCount = 0;
00141   result = innerDOMWindowList->GetLength(&frameCount);
00142   XMLT_LOG(mozXMLTermUtils::GetInnerDOMWindow,31,("frameCount=%d\n",
00143                                                    frameCount));
00144 
00145   result = innerDOMWindowList->NamedItem(innerFrameName, (nsIDOMWindow **)innerDOMWindow);
00146   if (NS_FAILED(result) || !*innerDOMWindow)
00147     return NS_ERROR_FAILURE;
00148 
00149   return NS_OK;
00150 }
00151 
00152 
00158 NS_EXPORT nsresult
00159 mozXMLTermUtils::GetPresContextScrollableView(nsPresContext* aPresContext,
00160                                          nsIScrollableView** aScrollableView)
00161 {
00162   XMLT_LOG(mozXMLTermUtils::GetPresContextScrollableView,30,("\n"));
00163 
00164   if (!aScrollableView)
00165     return NS_ERROR_FAILURE;
00166 
00167   *aScrollableView = nsnull;
00168 
00169   nsIPresShell *presShell = aPresContext->GetPresShell();
00170   if (!presShell)
00171     return NS_ERROR_FAILURE;
00172 
00173   nsIViewManager* viewManager = presShell->GetViewManager();
00174   if (!viewManager)
00175     return NS_ERROR_FAILURE;
00176 
00177   return viewManager->GetRootScrollableView(aScrollableView);
00178 }
00179 
00180 
00186 NS_EXPORT nsresult
00187 mozXMLTermUtils::GetPresContextDeviceContext(nsPresContext* aPresContext,
00188                                          nsIDeviceContext** aDeviceContext)
00189 {
00190   nsresult result;
00191 
00192   XMLT_LOG(mozXMLTermUtils::GetPresContextScrollableView,30,("\n"));
00193 
00194   if (!aDeviceContext)
00195     return NS_ERROR_FAILURE;
00196 
00197   *aDeviceContext = nsnull;
00198 
00199   nsIViewManager* viewManager = aPresContext->GetViewManager();
00200   if (!viewManager)
00201     return NS_ERROR_FAILURE;
00202 
00203   nsCOMPtr<nsIDeviceContext> deviceContext;
00204   result = viewManager->GetDeviceContext(*getter_AddRefs(deviceContext));
00205   if (NS_FAILED(result) || !deviceContext)
00206     return NS_ERROR_FAILURE;
00207 
00208   *aDeviceContext = deviceContext.get();
00209   NS_ADDREF(*aDeviceContext);
00210 
00211   return NS_OK;
00212 }
00213 
00214 
00220 NS_EXPORT nsresult
00221 mozXMLTermUtils::GetScriptContext(nsIDOMDocument* aDOMDocument,
00222                                   nsIScriptContext** aScriptContext)
00223 {
00224   XMLT_LOG(mozXMLTermUtils::GetScriptContext,20,("\n"));
00225 
00226   nsCOMPtr<nsIDocument> doc ( do_QueryInterface(aDOMDocument) );
00227   if (!doc)
00228     return NS_ERROR_FAILURE;
00229 
00230   nsIScriptGlobalObject *scriptGlobalObject = doc->GetScriptGlobalObject();
00231 
00232   if (!scriptGlobalObject)
00233     return NS_ERROR_FAILURE;
00234 
00235   *aScriptContext = scriptGlobalObject->GetContext();
00236   NS_IF_ADDREF(*aScriptContext);
00237 
00238   return NS_OK;
00239 }
00240 
00241 
00248 NS_EXPORT nsresult
00249 mozXMLTermUtils::ExecuteScript(nsIDOMDocument* aDOMDocument,
00250                                const nsString& aScript,
00251                                nsString& aOutput)
00252 {
00253   nsresult result;
00254 
00255   XMLT_LOG(mozXMLTermUtils::ExecuteScript,20,("\n"));
00256 
00257   // Get document principal
00258   nsCOMPtr<nsIDocument> doc = do_QueryInterface(aDOMDocument);
00259   if (!doc)
00260     return NS_ERROR_FAILURE;
00261 
00262   nsIPrincipal *docPrincipal = doc->GetPrincipal();
00263   if (!docPrincipal) 
00264     return NS_ERROR_FAILURE;
00265 
00266   // Get document script context
00267   nsCOMPtr<nsIScriptContext> scriptContext;
00268   result = GetScriptContext(aDOMDocument, getter_AddRefs(scriptContext));
00269   if (NS_FAILED(result) || !scriptContext)
00270     return NS_ERROR_FAILURE;
00271 
00272   // Execute script
00273   PRBool isUndefined = PR_FALSE;
00274   const char* URL = "";
00275   result = scriptContext->EvaluateString(aScript, (void *) nsnull,
00276                                          docPrincipal, URL, 0, nsnull,
00277                                          &aOutput, &isUndefined);
00278 
00279   XMLT_LOG(mozXMLTermUtils::ExecuteScript,21,("result=0x%x,isUndefined=0x%x\n",
00280                                              result, isUndefined));
00281 
00282   return result;
00283 }
00284 
00285 
00294 NS_EXPORT nsresult
00295 mozXMLTermUtils::GetNodeAttribute(nsIDOMNode* aDOMNode,
00296                                   const char* aAttName,
00297                                   nsString& aAttValue)
00298 {
00299   XMLT_LOG(mozXMLTermUtils::GetNodeAttribute,20,("aAttName=%s\n", aAttName));
00300 
00301   nsCOMPtr<nsIDOMElement> domElement = do_QueryInterface(aDOMNode);
00302   if (!domElement) {
00303     aAttValue.SetLength(0);
00304     return NS_OK;
00305   }
00306 
00307   nsAutoString attName;
00308   attName.AssignASCII(aAttName);
00309   return domElement->GetAttribute(attName, aAttValue);
00310 }
00311 
00312 
00321 NS_IMETHODIMP mozXMLTermUtils::TimeStamp(PRInt32 deltaSec, PRTime& lastTime,
00322                                      nsString& aTimeStamp)
00323 {
00324   static const PRInt32 DATE_LEN = 19;
00325   PRTime deltaTime ;
00326   char dateStr[DATE_LEN+1];
00327   PRTime curTime, difTime;
00328 
00329   curTime = PR_Now();
00330   LL_SUB(difTime, curTime, lastTime);
00331 
00332   LL_I2L(deltaTime, deltaSec*1000000);
00333   if (LL_CMP(difTime, <, deltaTime)) {
00334     // Not enough time has elapsed for a new time stamp
00335     aTimeStamp.SetLength(0);
00336     return NS_OK;
00337   }
00338 
00339   lastTime = curTime;
00340 
00341   // Current local time
00342   PRExplodedTime localTime;
00343   PR_ExplodeTime(curTime, PR_LocalTimeParameters, &localTime);
00344 
00345   PRInt32 nWritten = PR_snprintf(dateStr, DATE_LEN+1,
00346                      "%02d:%02d:%02d %02d/%02d/%04d", // XXX i18n!
00347                    localTime.tm_hour, localTime.tm_min, localTime.tm_sec,
00348                    localTime.tm_mday, localTime.tm_month+1, localTime.tm_year);
00349 
00350   if (nWritten != DATE_LEN)
00351     return NS_ERROR_FAILURE;
00352 
00353   XMLT_LOG(mozXMLTermUtils::LocalTime,99,("localTime=%s\n", dateStr));
00354 
00355   aTimeStamp.AssignASCII(dateStr);
00356   return NS_OK;
00357 }
00358 
00359 
00365 NS_IMETHODIMP mozXMLTermUtils::RandomCookie(nsString& aCookie)
00366 {
00367   // Current local time
00368   PRExplodedTime localTime;
00369   PR_ExplodeTime(PR_Now(), PR_LocalTimeParameters, &localTime);
00370 
00371   PRInt32        randomNumberA = localTime.tm_sec*1000000+localTime.tm_usec;
00372   PRIntervalTime randomNumberB = PR_IntervalNow();
00373 
00374   XMLT_LOG(mozXMLTermUtils::RandomCookie,30,("ranA=0x%x, ranB=0x%x\n",
00375                                         randomNumberA, randomNumberB));
00376   PR_ASSERT(randomNumberA >= 0);
00377   PR_ASSERT(randomNumberB >= 0);
00378 
00379   static const char cookieDigits[17] = "0123456789abcdef";
00380   char cookie[12];
00381   int j;
00382 
00383   for (j=0; j<6; j++) {
00384     cookie[j] = cookieDigits[randomNumberA%16];
00385     randomNumberA = randomNumberA/16;
00386   }
00387   for (j=6; j<11; j++) {
00388     cookie[j] = cookieDigits[randomNumberB%16];
00389     randomNumberB = randomNumberB/16;
00390   }
00391   cookie[11] = '\0';
00392 
00393   aCookie.AssignASCII(cookie);
00394 
00395   return NS_OK;
00396 }