LCOV - code coverage report
Current view: directory - extensions/pref/autoconfig/src - nsJSConfigTriggers.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 77 0 0.0 %
Date: 2012-06-02 Functions: 12 0 0.0 %

       1                 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2                 : /* ***** BEGIN LICENSE BLOCK *****
       3                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       4                 :  *
       5                 :  * The contents of this file are subject to the Mozilla Public License Version
       6                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       7                 :  * the License. You may obtain a copy of the License at
       8                 :  * http://www.mozilla.org/MPL/
       9                 :  *
      10                 :  * Software distributed under the License is distributed on an "AS IS" basis,
      11                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      12                 :  * for the specific language governing rights and limitations under the
      13                 :  * License.
      14                 :  *
      15                 :  * The Original Code is mozilla.org code.
      16                 :  *
      17                 :  * The Initial Developer of the Original Code is
      18                 :  * Netscape Communications Corporation.
      19                 :  * Portions created by the Initial Developer are Copyright (C) 1998
      20                 :  * the Initial Developer. All Rights Reserved.
      21                 :  *
      22                 :  * Contributor(s):
      23                 :  *   Mitesh Shah <mitesh@netscape.com>
      24                 :  *
      25                 :  * Alternatively, the contents of this file may be used under the terms of
      26                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
      27                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      28                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      29                 :  * of those above. If you wish to allow use of your version of this file only
      30                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      31                 :  * use your version of this file under the terms of the MPL, indicate your
      32                 :  * decision by deleting the provisions above and replace them with the notice
      33                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      34                 :  * the provisions above, a recipient may use your version of this file under
      35                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      36                 :  *
      37                 :  * ***** END LICENSE BLOCK ***** */
      38                 : 
      39                 : #ifdef MOZ_LOGGING
      40                 : // sorry, this has to be before the pre-compiled header
      41                 : #define FORCE_PR_LOG /* Allow logging in the release build */
      42                 : #endif
      43                 : #include "jsapi.h"
      44                 : #include "nsIXPCSecurityManager.h"
      45                 : #include "nsIXPConnect.h"
      46                 : #include "nsIJSRuntimeService.h"
      47                 : #include "nsCOMPtr.h"
      48                 : #include "nsIServiceManager.h"
      49                 : #include "nsIComponentManager.h"
      50                 : #include "nsString.h"
      51                 : #include "nsIPrefService.h"
      52                 : #include "nsIJSContextStack.h"
      53                 : #include "nspr.h"
      54                 : 
      55                 : extern PRLogModuleInfo *MCD;
      56                 : 
      57                 : // Security Manager for new XPCONNECT enabled JS Context
      58                 : // Right now it allows all access
      59                 : 
      60                 : class AutoConfigSecMan : public nsIXPCSecurityManager
      61                 : {
      62                 : public:
      63                 :     NS_DECL_ISUPPORTS
      64                 :     NS_DECL_NSIXPCSECURITYMANAGER
      65                 :     AutoConfigSecMan();
      66                 : };
      67                 : 
      68               0 : NS_IMPL_ISUPPORTS1(AutoConfigSecMan, nsIXPCSecurityManager)
      69                 : 
      70               0 : AutoConfigSecMan::AutoConfigSecMan()
      71                 : {
      72               0 : }
      73                 : 
      74                 : NS_IMETHODIMP
      75               0 : AutoConfigSecMan::CanCreateWrapper(JSContext *aJSContext, const nsIID & aIID, 
      76                 :                                   nsISupports *aObj, nsIClassInfo *aClassInfo, 
      77                 :                                   void **aPolicy)
      78                 : {
      79               0 :     return NS_OK;
      80                 : }
      81                 : 
      82                 : NS_IMETHODIMP
      83               0 : AutoConfigSecMan::CanCreateInstance(JSContext *aJSContext, const nsCID & aCID)
      84                 : {
      85               0 :     return NS_OK;
      86                 : }
      87                 : 
      88                 : NS_IMETHODIMP
      89               0 : AutoConfigSecMan::CanGetService(JSContext *aJSContext, const nsCID & aCID)
      90                 : {
      91               0 :     return NS_OK;
      92                 : }
      93                 : 
      94                 : NS_IMETHODIMP 
      95               0 : AutoConfigSecMan::CanAccess(PRUint32 aAction, 
      96                 :                             nsAXPCNativeCallContext *aCallContext, 
      97                 :                             JSContext *aJSContext, JSObject *aJSObject, 
      98                 :                             nsISupports *aObj, nsIClassInfo *aClassInfo, 
      99                 :                             jsid aName, void **aPolicy)
     100                 : {
     101               0 :     return NS_OK;
     102                 : }
     103                 : 
     104                 : //*****************************************************************************
     105                 : 
     106                 : static  JSContext *autoconfig_cx = nsnull;
     107                 : static  JSObject *autoconfig_glob;
     108                 : 
     109                 : static JSClass global_class = {
     110                 :     "autoconfig_global", JSCLASS_GLOBAL_FLAGS,
     111                 :     JS_PropertyStub,  JS_PropertyStub,  JS_PropertyStub,  JS_StrictPropertyStub,
     112                 :     JS_EnumerateStub, JS_ResolveStub,   JS_ConvertStub,   nsnull
     113                 : };
     114                 : 
     115                 : static void
     116               0 : autoConfigErrorReporter(JSContext *cx, const char *message, 
     117                 :                         JSErrorReport *report)
     118                 : {
     119               0 :     NS_ERROR(message);
     120               0 :     PR_LOG(MCD, PR_LOG_DEBUG, ("JS error in js from MCD server: %s\n", message));
     121               0 : } 
     122                 : 
     123               0 : nsresult CentralizedAdminPrefManagerInit()
     124                 : {
     125                 :     nsresult rv;
     126                 :     JSRuntime *rt;
     127                 : 
     128                 :     // If autoconfig_cx already created, no need to create it again
     129               0 :     if (autoconfig_cx) 
     130               0 :         return NS_OK;
     131                 : 
     132                 :     // We need the XPCONNECT service
     133               0 :     nsCOMPtr<nsIXPConnect> xpc = do_GetService(nsIXPConnect::GetCID(), &rv);
     134               0 :     if (NS_FAILED(rv)) {
     135               0 :         return rv;
     136                 :     }
     137                 : 
     138                 :     // Get the JS RunTime
     139                 :     nsCOMPtr<nsIJSRuntimeService> rtsvc = 
     140               0 :         do_GetService("@mozilla.org/js/xpc/RuntimeService;1", &rv);
     141               0 :     if (NS_SUCCEEDED(rv))
     142               0 :         rv = rtsvc->GetRuntime(&rt);
     143                 : 
     144               0 :     if (NS_FAILED(rv)) {
     145               0 :         NS_ERROR("Couldn't get JS RunTime");
     146               0 :         return rv;
     147                 :     }
     148                 : 
     149                 :     // Create a new JS context for autoconfig JS script
     150               0 :     autoconfig_cx = JS_NewContext(rt, 1024);
     151               0 :     if (!autoconfig_cx)
     152               0 :         return NS_ERROR_OUT_OF_MEMORY;
     153                 : 
     154               0 :     JSAutoRequest ar(autoconfig_cx);
     155                 : 
     156               0 :     JS_SetErrorReporter(autoconfig_cx, autoConfigErrorReporter);
     157                 : 
     158                 :     // Create a new Security Manger and set it for the new JS context
     159                 :     nsCOMPtr<nsIXPCSecurityManager> secman =
     160               0 :         static_cast<nsIXPCSecurityManager*>(new AutoConfigSecMan());
     161               0 :     xpc->SetSecurityManagerForJSContext(autoconfig_cx, secman, 0);
     162                 : 
     163               0 :     autoconfig_glob = JS_NewCompartmentAndGlobalObject(autoconfig_cx, &global_class, NULL);
     164               0 :     if (autoconfig_glob) {
     165               0 :         JSAutoEnterCompartment ac;
     166               0 :         if(!ac.enter(autoconfig_cx, autoconfig_glob))
     167               0 :             return NS_ERROR_FAILURE;
     168               0 :         if (JS_InitStandardClasses(autoconfig_cx, autoconfig_glob)) {
     169                 :             // XPCONNECT enable this JS context
     170               0 :             rv = xpc->InitClasses(autoconfig_cx, autoconfig_glob);
     171               0 :             if (NS_SUCCEEDED(rv)) 
     172               0 :                 return NS_OK;
     173                 :         }
     174                 :     }
     175                 : 
     176                 :     // failue exit... clean up the JS context
     177               0 :     JS_DestroyContext(autoconfig_cx);
     178               0 :     autoconfig_cx = nsnull;
     179               0 :     return NS_ERROR_FAILURE;
     180                 : }
     181                 : 
     182               0 : nsresult CentralizedAdminPrefManagerFinish()
     183                 : {
     184               0 :     if (autoconfig_cx)
     185               0 :         JS_DestroyContext(autoconfig_cx);
     186               0 :     return NS_OK;
     187                 : }
     188                 : 
     189               0 : nsresult EvaluateAdminConfigScript(const char *js_buffer, size_t length,
     190                 :                                    const char *filename, bool bGlobalContext, 
     191                 :                                    bool bCallbacks, bool skipFirstLine)
     192                 : {
     193                 :     JSBool ok;
     194                 : 
     195               0 :     if (skipFirstLine) {
     196                 :         /* In order to protect the privacy of the JavaScript preferences file 
     197                 :          * from loading by the browser, we make the first line unparseable
     198                 :          * by JavaScript. We must skip that line here before executing 
     199                 :          * the JavaScript code.
     200                 :          */
     201               0 :         unsigned int i = 0;
     202               0 :         while (i < length) {
     203               0 :             char c = js_buffer[i++];
     204               0 :             if (c == '\r') {
     205               0 :                 if (js_buffer[i] == '\n')
     206               0 :                     i++;
     207               0 :                 break;
     208                 :             }
     209               0 :             if (c == '\n')
     210               0 :                 break;
     211                 :         }
     212                 : 
     213               0 :         length -= i;
     214               0 :         js_buffer += i;
     215                 :     }
     216                 : 
     217                 :     nsresult rv;
     218                 :     nsCOMPtr<nsIJSContextStack> cxstack = 
     219               0 :         do_GetService("@mozilla.org/js/xpc/ContextStack;1");
     220               0 :     rv = cxstack->Push(autoconfig_cx);
     221               0 :     if (NS_FAILED(rv)) {
     222               0 :         NS_ERROR("coudn't push the context on the stack");
     223               0 :         return rv;
     224                 :     }
     225                 : 
     226               0 :     JS_BeginRequest(autoconfig_cx);
     227                 :     ok = JS_EvaluateScript(autoconfig_cx, autoconfig_glob,
     228               0 :                            js_buffer, length, filename, 0, nsnull);
     229               0 :     JS_EndRequest(autoconfig_cx);
     230                 : 
     231               0 :     JS_MaybeGC(autoconfig_cx);
     232                 : 
     233                 :     JSContext *cx;
     234               0 :     cxstack->Pop(&cx);
     235               0 :     NS_ASSERTION(cx == autoconfig_cx, "AutoConfig JS contexts didn't match");
     236                 : 
     237               0 :     if (ok)
     238               0 :         return NS_OK;
     239               0 :     return NS_ERROR_FAILURE;
     240                 : }
     241                 : 

Generated by: LCOV version 1.7