Back to index

lightning-sunbird  0.9+nobinonly
Classes | Functions | Variables
nsJSConfigTriggers.cpp File Reference
#include "jsapi.h"
#include "nsIXPCSecurityManager.h"
#include "nsIXPConnect.h"
#include "nsIJSRuntimeService.h"
#include "nsCOMPtr.h"
#include "nsIGenericFactory.h"
#include "nsIServiceManager.h"
#include "nsIComponentManager.h"
#include "nsString.h"
#include "nsIPrefService.h"
#include "nsIJSContextStack.h"
#include "nspr.h"

Go to the source code of this file.

Classes

class  AutoConfigSecMan

Functions

 autoConfigErrorReporter (JSContext *cx, const char *message, JSErrorReport *report)
nsresult CentralizedAdminPrefManagerInit ()
nsresult CentralizedAdminPrefManagerFinish ()
nsresult EvaluateAdminConfigScript (const char *js_buffer, size_t length, const char *filename, PRBool bGlobalContext, PRBool bCallbacks, PRBool skipFirstLine)

Variables

PRLogModuleInfoMCD
static JSContextautoconfig_cx = nsnull
static JSObjectautoconfig_glob
static JSClass global_class

Function Documentation

autoConfigErrorReporter ( JSContext cx,
const char *  message,
JSErrorReport report 
)

Definition at line 117 of file nsJSConfigTriggers.cpp.

{
    NS_ERROR(message);
    PR_LOG(MCD, PR_LOG_DEBUG, ("JS error in js from MCD server: %s\n", message));
} 

Here is the caller graph for this function:

Definition at line 178 of file nsJSConfigTriggers.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 124 of file nsJSConfigTriggers.cpp.

{
    nsresult rv;
    JSRuntime *rt;

    // If autoconfig_cx already created, no need to create it again
    if (autoconfig_cx) 
        return NS_OK;

    // We need the XPCONNECT service
    nsCOMPtr<nsIXPConnect> xpc = do_GetService(nsIXPConnect::GetCID(), &rv);
    if (NS_FAILED(rv)) {
        return rv;
    }

    // Get the JS RunTime
    nsCOMPtr<nsIJSRuntimeService> rtsvc = 
        do_GetService("@mozilla.org/js/xpc/RuntimeService;1", &rv);
    if (NS_SUCCEEDED(rv))
        rv = rtsvc->GetRuntime(&rt);

    if (NS_FAILED(rv)) {
        NS_ERROR("Couldn't get JS RunTime");
        return rv;
    }

    // Create a new JS context for autoconfig JS script
    autoconfig_cx = JS_NewContext(rt, 1024);
    if (!autoconfig_cx)
        return NS_ERROR_OUT_OF_MEMORY;

    JS_SetErrorReporter(autoconfig_cx, autoConfigErrorReporter);

    // Create a new Security Manger and set it for the new JS context
    nsCOMPtr<nsIXPCSecurityManager> secman =
        NS_STATIC_CAST(nsIXPCSecurityManager*, new AutoConfigSecMan());
    xpc->SetSecurityManagerForJSContext(autoconfig_cx, secman, 0);

    autoconfig_glob = JS_NewObject(autoconfig_cx, &global_class, NULL, NULL);
    if (autoconfig_glob) {
        if (JS_InitStandardClasses(autoconfig_cx, autoconfig_glob)) {
            // XPCONNECT enable this JS context
            rv = xpc->InitClasses(autoconfig_cx, autoconfig_glob);
            if (NS_SUCCEEDED(rv)) 
                return NS_OK;
        }
    }

    // failue exit... clean up the JS context
    JS_DestroyContext(autoconfig_cx);
    autoconfig_cx = nsnull;
    return NS_ERROR_FAILURE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult EvaluateAdminConfigScript ( const char *  js_buffer,
size_t  length,
const char *  filename,
PRBool  bGlobalContext,
PRBool  bCallbacks,
PRBool  skipFirstLine 
)

Definition at line 185 of file nsJSConfigTriggers.cpp.

{
    JSBool ok;
    jsval result;

    if (skipFirstLine) {
        /* In order to protect the privacy of the JavaScript preferences file 
         * from loading by the browser, we make the first line unparseable
         * by JavaScript. We must skip that line here before executing 
         * the JavaScript code.
         */
        unsigned int i = 0;
        while (i < length) {
            char c = js_buffer[i++];
            if (c == '\r') {
                if (js_buffer[i] == '\n')
                    i++;
                break;
            }
            if (c == '\n')
                break;
        }

        length -= i;
        js_buffer += i;
    }

    nsresult rv;
    nsCOMPtr<nsIJSContextStack> cxstack = 
        do_GetService("@mozilla.org/js/xpc/ContextStack;1");
    rv = cxstack->Push(autoconfig_cx);
    if (NS_FAILED(rv)) {
        NS_ERROR("coudn't push the context on the stack");
        return rv;
    }

    JS_BeginRequest(autoconfig_cx);
    ok = JS_EvaluateScript(autoconfig_cx, autoconfig_glob,
                           js_buffer, length, filename, 0, &result);
    JS_EndRequest(autoconfig_cx);

    JS_MaybeGC(autoconfig_cx);

    JSContext *cx;
    cxstack->Pop(&cx);
    NS_ASSERTION(cx == autoconfig_cx, "AutoConfig JS contexts didn't match");

    if (ok)
        return NS_OK;
    return NS_ERROR_FAILURE;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Definition at line 107 of file nsJSConfigTriggers.cpp.

Definition at line 108 of file nsJSConfigTriggers.cpp.

Definition at line 59 of file nsAutoConfig.cpp.