DEBUG_PRINTF( PR_STDOUT, "%s %d: GetNativeContext failed\n", (char*)__FILE__, (int)__LINE__ );
PRINTF("%s %d: GetNativeContext failed\n", (char*)__FILE__, (int)__LINE__ );
nsUnknownContentTypeHandler::HandleUnknownContentType( nsIChannel *aChannel,                                                       const char *aContentType,                                                       nsIDOMWindowInternal *aWindow ) {    nsresult rv = NS_OK;    nsCOMPtr<nsISupports> channel;    nsCAutoString         contentDisp;    if ( aChannel ) {        // Need root nsISupports for later JS_PushArguments call.        channel = do_QueryInterface( aChannel );        // Try to get HTTP channel.        nsCOMPtr<nsIHTTPChannel> httpChannel = do_QueryInterface( aChannel );        if ( httpChannel ) {            // Get content-disposition response header.            nsCOMPtr<nsIAtom> atom = NS_NewAtom( "content-disposition" );            if ( atom ) {                nsXPIDLCString disp;                 rv = httpChannel->GetResponseHeader( atom, getter_Copies( disp ) );                if ( NS_SUCCEEDED( rv ) && disp ) {                    contentDisp = disp; // Save the response header to pass to dialog.                }            }        }        // Cancel input channel now.        rv = aChannel->Cancel(NS_BINDING_ABORTED);        if ( NS_FAILED( rv ) ) {            DEBUG_PRINTF( PR_STDOUT, "%s %d: Cancel failed, rv=0x%08X\n",                          (char*)__FILE__, (int)__LINE__, (int)rv );        }    }    if ( NS_SUCCEEDED( rv ) && channel && aContentType && aWindow ) {        // Open "Unknown content type" dialog.        // We pass in the channel, the content type, and the content disposition.        // Note that the "parent" browser window will be window.opener within the        // new dialog.            // Get JS context from parent window.        nsCOMPtr<nsIScriptGlobalObject> sgo = do_QueryInterface( aWindow, &rv );        if ( NS_SUCCEEDED( rv ) && sgo ) {            nsCOMPtr<nsIScriptContext> context;            sgo->GetContext( getter_AddRefs( context ) );            if ( context ) {                JSContext *jsContext = (JSContext*)context->GetNativeContext();                if ( jsContext ) {                    void *stackPtr;                    jsval *argv = JS_PushArguments( jsContext,                                                    &stackPtr,                                                    "sss%ipss",                                                    "chrome://global/content/unknownContent.xul",                                                    "_blank",                                                    "chrome,titlebar",                                                    (const nsIID*)(&NS_GET_IID(nsIChannel)),                                                    (nsISupports*)channel.get(),                                                    aContentType,                                                    (const char*)contentDisp.GetBuffer() );                    if ( argv ) {                        nsCOMPtr<nsIDOMWindowInternal> newWindow;                        rv = aWindow->OpenDialog( jsContext, argv, 6, getter_AddRefs( newWindow ) );                        if ( NS_FAILED( rv ) ) {                            DEBUG_PRINTF( PR_STDOUT, "%s %d: OpenDialog failed, rv=0x%08X\n",                                          (char*)__FILE__, (int)__LINE__, (int)rv );                        }                        JS_PopArguments( jsContext, stackPtr );                    } else {                        DEBUG_PRINTF( PR_STDOUT, "%s %d: JS_PushArguments failed\n",                                      (char*)__FILE__, (int)__LINE__ );                        rv = NS_ERROR_FAILURE;                    }                } else {                    DEBUG_PRINTF( PR_STDOUT, "%s %d: GetNativeContext failed\n",                                  (char*)__FILE__, (int)__LINE__ );                    rv = NS_ERROR_FAILURE;                }            } else {                DEBUG_PRINTF( PR_STDOUT, "%s %d: GetContext failed\n",                              (char*)__FILE__, (int)__LINE__ );                rv = NS_ERROR_FAILURE;            }        } else {            DEBUG_PRINTF( PR_STDOUT, "%s %d: QueryInterface (for nsIScriptGlobalObject) failed, rv=0x%08X\n",                          (char*)__FILE__, (int)__LINE__, (int)rv );        }    } else {        // If no error recorded so far, set one now.        if ( NS_SUCCEEDED( rv ) ) {            rv = NS_ERROR_NULL_POINTER;        }    }    return rv;}