nsCOMPtr<nsIScriptGlobalObject> global; rv = globalOwner->GetScriptGlobalObject(getter_AddRefs(global));
nsIScriptGlobalObject* global = globalOwner->GetScriptGlobalObject();
nsresult nsJSThunk::EvaluateScript(nsIChannel *aChannel){    nsresult rv;    NS_ENSURE_ARG_POINTER(aChannel);    // Get the script string to evaluate...    nsCAutoString script;    rv = mURI->GetPath(script);    if (NS_FAILED(rv)) return rv;    // The the global object owner from the channel    nsCOMPtr<nsIScriptGlobalObjectOwner> globalOwner;    GetInterfaceFromChannel(aChannel, NS_GET_IID(nsIScriptGlobalObjectOwner),                            getter_AddRefs(globalOwner));    NS_ASSERTION(globalOwner,                  "Unable to get an nsIScriptGlobalObjectOwner from the "                 "channel!");    if (!globalOwner) {        return NS_ERROR_FAILURE;    }    // So far so good: get the script context from its owner.    nsCOMPtr<nsIScriptGlobalObject> global;    rv = globalOwner->GetScriptGlobalObject(getter_AddRefs(global));    NS_ASSERTION(NS_SUCCEEDED(rv) && global,                 "Unable to get an nsIScriptGlobalObject from the "                 "ScriptGlobalObjectOwner!");    if (NS_FAILED(rv) || !global) {        return NS_ERROR_FAILURE;    }    JSObject *globalJSObject = global->GetGlobalJSObject();    nsCOMPtr<nsIDOMWindow> domWindow(do_QueryInterface(global, &rv));    if (NS_FAILED(rv)) {        return NS_ERROR_FAILURE;    }    // If mURI is just "javascript:", we bring up the JavaScript console    // and return NS_ERROR_DOM_RETVAL_UNDEFINED.    if (script.IsEmpty()) {        rv = BringUpConsole(domWindow);        if (NS_FAILED(rv)) return NS_ERROR_FAILURE;        return NS_ERROR_DOM_RETVAL_UNDEFINED;    }    // Now get the DOM Document.  Accessing the document will create one    // if necessary.  So, basically, this call ensures that a document gets    // created -- if necessary.    nsCOMPtr<nsIDOMDocument> doc;    rv = domWindow->GetDocument(getter_AddRefs(doc));    NS_ASSERTION(doc, "No DOMDocument!");    if (NS_FAILED(rv)) {        return NS_ERROR_FAILURE;    }    nsCOMPtr<nsIScriptContext> scriptContext = global->GetContext();    if (!scriptContext)        return NS_ERROR_FAILURE;    // Unescape the script    NS_UnescapeURL(script);    // Get the url.    nsCAutoString url;    rv = mURI->GetSpec(url);    if (NS_FAILED(rv)) return rv;    // Get principal of code for execution    nsCOMPtr<nsISupports> owner;    rv = aChannel->GetOwner(getter_AddRefs(owner));    nsCOMPtr<nsIPrincipal> principal;    if (NS_FAILED(rv))        return rv;    nsCOMPtr<nsIScriptSecurityManager> securityManager;    securityManager = do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv);    if (NS_FAILED(rv))        return rv;    if (owner) {        principal = do_QueryInterface(owner, &rv);        NS_ASSERTION(principal, "Channel's owner is not a principal");        if (!principal)            return NS_ERROR_FAILURE;        //-- Don't run if the script principal is different from the        //   principal of the context, with two exceptions: we allow        //   the script to run if the script has the system principal        //   or the context is about:blank.        nsCOMPtr<nsIPrincipal> objectPrincipal;        rv = securityManager->GetObjectPrincipal(                                (JSContext*)scriptContext->GetNativeContext(),                                globalJSObject,                                getter_AddRefs(objectPrincipal));        if (NS_FAILED(rv))            return rv;        nsCOMPtr<nsIPrincipal> systemPrincipal;        securityManager->GetSystemPrincipal(getter_AddRefs(systemPrincipal));        if (principal != systemPrincipal) {            rv = securityManager->CheckSameOriginPrincipal(principal,                                                           objectPrincipal);            if (NS_FAILED(rv)) {                nsCOMPtr<nsIConsoleService> console =                    do_GetService("@mozilla.org/consoleservice;1");                if (console) {                    // XXX Localize me!                    console->LogStringMessage(                        NS_LITERAL_STRING("Attempt to load a javascript: URL from one host\nin a window displaying content from another host\nwas blocked by the security manager.").get());                }                return NS_ERROR_DOM_RETVAL_UNDEFINED;            }        }    }    else {        // No owner from channel, use the current URI to generate a principal        rv = securityManager->GetCodebasePrincipal(mURI,                                                    getter_AddRefs(principal));        if (NS_FAILED(rv) || !principal) {            return NS_ERROR_FAILURE;        }    }    // Finally, we have everything needed to evaluate the expression.    nsString result;    PRBool bIsUndefined;    rv = scriptContext->EvaluateString(NS_ConvertUTF8toUCS2(script),                                       globalJSObject, // obj                                       principal,                                       url.get(),      // url                                       1,              // line no                                       nsnull,                                       &result,                                       &bIsUndefined);    if (NS_FAILED(rv)) {        rv = NS_ERROR_MALFORMED_URI;    }    else if (bIsUndefined) {        rv = NS_ERROR_DOM_RETVAL_UNDEFINED;    }    else {        // NS_NewStringInputStream calls ToNewCString        // XXXbe this should not decimate! pass back UCS-2 to necko        rv = NS_NewStringInputStream(getter_AddRefs(mInnerStream), result);    }    return rv;}