LCOV - app.info - dom/base/nsJSUtils.cpp

LCOV - code coverage report
| Current view: | directory - dom/base - nsJSUtils.cpp (source / functions) |  | Found | Hit | Coverage |
| Test: | app.info | Lines: | 60 | 24 | 40.0 %
| Date: | 2012-04-21 | Functions: | 6 | 2 | 33.3 %

           1                 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
           2                 : /* vim: set ts=2 sw=2 et tw=78: */
           3                 : /* ***** BEGIN LICENSE BLOCK *****
           4                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
           5                 :  *
           6                 :  * The contents of this file are subject to the Mozilla Public License Version
           7                 :  * 1.1 (the "License"); you may not use this file except in compliance with
           8                 :  * the License. You may obtain a copy of the License at
           9                 :  * http://www.mozilla.org/MPL/
          10                 :  *
          11                 :  * Software distributed under the License is distributed on an "AS IS" basis,
          12                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
          13                 :  * for the specific language governing rights and limitations under the
          14                 :  * License.
          15                 :  *
          16                 :  * The Original Code is mozilla.org code.
          17                 :  *
          18                 :  * The Initial Developer of the Original Code is
          19                 :  * Netscape Communications Corporation.
          20                 :  * Portions created by the Initial Developer are Copyright (C) 1998
          21                 :  * the Initial Developer. All Rights Reserved.
          22                 :  *
          23                 :  * Contributor(s):
          24                 :  *   Vidur Apparao <vidur@netscape.com>
          25                 :  *   L. David Baron <dbaron@mozillafoundation.org>
          26                 :  *
          27                 :  * Alternatively, the contents of this file may be used under the terms of
          28                 :  * either of the GNU General Public License Version 2 or later (the "GPL"),
          29                 :  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
          30                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
          31                 :  * of those above. If you wish to allow use of your version of this file only
          32                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
          33                 :  * use your version of this file under the terms of the MPL, indicate your
          34                 :  * decision by deleting the provisions above and replace them with the notice
          35                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
          36                 :  * the provisions above, a recipient may use your version of this file under
          37                 :  * the terms of any one of the MPL, the GPL or the LGPL.
          38                 :  *
          39                 :  * ***** END LICENSE BLOCK ***** */
          40                 :
          41                 : /**
          42                 :  * This is not a generated file. It contains common utility functions
          43                 :  * invoked from the JavaScript code generated from IDL interfaces.
          44                 :  * The goal of the utility functions is to cut down on the size of
          45                 :  * the generated code itself.
          46                 :  */
          47                 :
          48                 : #include "nsJSUtils.h"
          49                 : #include "jsapi.h"
          50                 : #include "jsdbgapi.h"
          51                 : #include "prprf.h"
          52                 : #include "nsIScriptContext.h"
          53                 : #include "nsIScriptObjectOwner.h"
          54                 : #include "nsIScriptGlobalObject.h"
          55                 : #include "nsIServiceManager.h"
          56                 : #include "nsIXPConnect.h"
          57                 : #include "nsCOMPtr.h"
          58                 : #include "nsContentUtils.h"
          59                 : #include "nsIScriptSecurityManager.h"
          60                 : #include "nsPIDOMWindow.h"
          61                 :
          62                 : #include "nsDOMJSUtils.h" // for GetScriptContextFromJSContext
          63                 :
          64                 : JSBool
          65               0 : nsJSUtils::GetCallingLocation(JSContext* aContext, const char* *aFilename,
          66                 :                               PRUint32* aLineno)
          67                 : {
          68                 :   // Get the current filename and line number
          69               0 :   JSStackFrame* frame = nsnull;
          70               0 :   JSScript* script = nsnull;
          71               0 :   do {
          72               0 :     frame = ::JS_FrameIterator(aContext, &frame);
          73                 :
          74               0 :     if (frame) {
          75               0 :       script = ::JS_GetFrameScript(aContext, frame);
          76                 :     }
          77                 :   } while (frame && !script);
          78                 :
          79               0 :   if (script) {
          80               0 :     const char* filename = ::JS_GetScriptFilename(aContext, script);
          81                 :
          82               0 :     if (filename) {
          83               0 :       PRUint32 lineno = 0;
          84               0 :       jsbytecode* bytecode = ::JS_GetFramePC(aContext, frame);
          85                 :
          86               0 :       if (bytecode) {
          87               0 :         lineno = ::JS_PCToLineNumber(aContext, script, bytecode);
          88                 :       }
          89                 :
          90               0 :       *aFilename = filename;
          91               0 :       *aLineno = lineno;
          92               0 :       return JS_TRUE;
          93                 :     }
          94                 :   }
          95                 :
          96               0 :   return JS_FALSE;
          97                 : }
          98                 :
          99                 : nsIScriptGlobalObject *
         100             406 : nsJSUtils::GetStaticScriptGlobal(JSContext* aContext, JSObject* aObj)
         101                 : {
         102                 :   nsISupports* supports;
         103                 :   JSClass* clazz;
         104             406 :   JSObject* glob = aObj; // starting point for search
         105                 :
         106             406 :   if (!glob)
         107               0 :     return nsnull;
         108                 :
         109             406 :   glob = JS_GetGlobalForObject(aContext, glob);
         110             406 :   NS_ABORT_IF_FALSE(glob, "Infallible returns null");
         111                 :
         112             406 :   clazz = JS_GetClass(glob);
         113                 :
         114            1624 :   if (!clazz ||
         115             406 :       !(clazz->flags & JSCLASS_HAS_PRIVATE) ||
         116             406 :       !(clazz->flags & JSCLASS_PRIVATE_IS_NSISUPPORTS) ||
         117             406 :       !(supports = (nsISupports*)::JS_GetPrivate(glob))) {
         118               0 :     return nsnull;
         119                 :   }
         120                 :
         121                 :   // We might either have a window directly (e.g. if the global is a
         122                 :   // sandbox whose script object principal pointer is a window), or an
         123                 :   // XPCWrappedNative for a window.  We could also have other
         124                 :   // sandbox-related script object principals, but we can't do much
         125                 :   // about those short of trying to walk the proto chain of |glob|
         126                 :   // looking for a window or something.
         127             812 :   nsCOMPtr<nsIScriptGlobalObject> sgo(do_QueryInterface(supports));
         128             406 :   if (!sgo) {
         129             812 :     nsCOMPtr<nsIXPConnectWrappedNative> wrapper(do_QueryInterface(supports));
         130             406 :     NS_ENSURE_TRUE(wrapper, nsnull);
         131             812 :     sgo = do_QueryWrappedNative(wrapper);
         132                 :   }
         133                 :
         134                 :   // We're returning a pointer to something that's about to be
         135                 :   // released, but that's ok here.
         136             406 :   return sgo;
         137                 : }
         138                 :
         139                 : nsIScriptContext *
         140               0 : nsJSUtils::GetStaticScriptContext(JSContext* aContext, JSObject* aObj)
         141                 : {
         142               0 :   nsIScriptGlobalObject *nativeGlobal = GetStaticScriptGlobal(aContext, aObj);
         143               0 :   if (!nativeGlobal)
         144               0 :     return nsnull;
         145                 :
         146               0 :   return nativeGlobal->GetScriptContext(nsIProgrammingLanguage::JAVASCRIPT);
         147                 : }
         148                 :
         149                 : nsIScriptGlobalObject *
         150               0 : nsJSUtils::GetDynamicScriptGlobal(JSContext* aContext)
         151                 : {
         152               0 :   nsIScriptContext *scriptCX = GetDynamicScriptContext(aContext);
         153               0 :   if (!scriptCX)
         154               0 :     return nsnull;
         155               0 :   return scriptCX->GetGlobalObject();
         156                 : }
         157                 :
         158                 : nsIScriptContext *
         159               0 : nsJSUtils::GetDynamicScriptContext(JSContext *aContext)
         160                 : {
         161               0 :   return GetScriptContextFromJSContext(aContext);
         162                 : }
         163                 :
         164                 : PRUint64
         165             406 : nsJSUtils::GetCurrentlyRunningCodeInnerWindowID(JSContext *aContext)
         166                 : {
         167             406 :   if (!aContext)
         168               0 :     return 0;
         169                 :
         170             406 :   PRUint64 innerWindowID = 0;
         171                 :
         172             406 :   JSObject *jsGlobal = JS_GetGlobalForScopeChain(aContext);
         173             406 :   if (jsGlobal) {
         174                 :     nsIScriptGlobalObject *scriptGlobal = GetStaticScriptGlobal(aContext,
         175             406 :                                                                 jsGlobal);
         176             406 :     if (scriptGlobal) {
         177               0 :       nsCOMPtr<nsPIDOMWindow> win = do_QueryInterface(scriptGlobal);
         178               0 :       if (win)
         179               0 :         innerWindowID = win->WindowID();
         180                 :     }
         181                 :   }
         182                 :
         183             406 :   return innerWindowID;
         184                 : }
         185                 :

Generated by: LCOV version 1.7