rv = mDocument->GetDocumentCharacterSet(characterSet);
nsCOMPtr<nsIChannel> channel; rv = aLoader->GetChannel(getter_AddRefs(channel));  if (channel) { httpChannel = do_QueryInterface(channel); if (httpChannel) { rv = httpChannel->GetResponseHeader(contentTypeKey, getter_Copies(contenttypeheader)); NS_RELEASE(contentTypeKey); } }  if (NS_SUCCEEDED(rv)) {  nsAutoString contentType(contenttypeheader); PRInt32 start = contentType.RFind("charset=", PR_TRUE ) ;  if(kNotFound != start) { start += 8; PRInt32 end = contentType.FindCharInSet(";\n\r ", start  ); if(kNotFound == end ) end = contentType.Length();  contentType.Mid(characterSet, start, end - start); NS_WITH_SERVICE(nsICharsetAlias, calias, kCharsetAliasCID, &rv); if(NS_SUCCEEDED(rv) && (nsnull != calias) ) { nsAutoString preferred; rv = calias->GetPreferred(characterSet, preferred); if(NS_SUCCEEDED(rv)) { characterSet = preferred; } } } }  if (NS_FAILED(rv) || (characterSet.Length() == 0)) {  characterSet = mScriptCharset; }  if (NS_FAILED(rv) || (characterSet.Length() == 0) ) {  rv = mDocument->GetDocumentCharacterSet(characterSet); }
HTMLContentSink::OnStreamComplete(nsIStreamLoader* aLoader,                                  nsISupports* aContext,                                  nsresult aStatus,                                  PRUint32 stringLen,                                  const char* string){  nsresult rv = NS_OK;    if (stringLen) {    PRUnichar *unicodeString = nsnull;    PRInt32 unicodeLength = 0;    nsAutoString characterSet;    nsICharsetConverterManager  *charsetConv = nsnull;    nsCOMPtr<nsIUnicodeDecoder> unicodeDecoder;    // charset from document default    rv = mDocument->GetDocumentCharacterSet(characterSet);    NS_ASSERTION(NS_SUCCEEDED(rv), "Could not get document charset!");    rv = nsServiceManager::GetService(kCharsetConverterManagerCID,                    NS_GET_IID(nsICharsetConverterManager),                    (nsISupports**)&charsetConv);    if (NS_SUCCEEDED(rv) && (charsetConv))    {       rv = charsetConv->GetUnicodeDecoder(&characterSet,           getter_AddRefs(unicodeDecoder));       NS_RELEASE(charsetConv);    }    // converts from the charset to unicode    if (NS_SUCCEEDED(rv)) {      rv = unicodeDecoder->GetMaxLength(string, stringLen, &unicodeLength);      if (NS_SUCCEEDED(rv)) {          mUnicodeXferBuf.SetCapacity(unicodeLength);          unicodeString = (PRUnichar *) mUnicodeXferBuf.GetUnicode();          rv = unicodeDecoder->Convert(string, (PRInt32 *) &stringLen, unicodeString, &unicodeLength);          if (NS_SUCCEEDED(rv)) {            mUnicodeXferBuf.SetLength(unicodeLength);          } else {            mUnicodeXferBuf.SetLength(0);          }      }    }    NS_ASSERTION(NS_SUCCEEDED(rv), "Could not convert Script input to Unicode!");    if ((NS_OK == aStatus) && (NS_SUCCEEDED(rv))) {      nsAutoString jsUnicodeBuffer(CBufDescriptor(unicodeString, PR_TRUE, unicodeLength+1, unicodeLength));      PRBool bodyPresent = PreEvaluateScript();      //-- Merge the principal of the script file with that of the document      nsCOMPtr<nsISupports> owner;      aLoader->GetOwner(getter_AddRefs(owner));      if (owner)      {        nsCOMPtr<nsIPrincipal> prin = do_QueryInterface(owner, &rv);        if (NS_FAILED(rv)) return rv;        rv = mDocument->AddPrincipal(prin);        if (NS_FAILED(rv)) return rv;      }      rv = EvaluateScript(jsUnicodeBuffer, mScriptURI, 1, mScriptLanguageVersion);      if (NS_FAILED(rv)) return rv;      PostEvaluateScript(bodyPresent);    }  }  rv = ResumeParsing();  if (NS_FAILED(rv)) return rv;  // We added a reference when the loader was created. This  // release should destroy it.  NS_RELEASE(aLoader);  //invalidate Xfer buffer content  mUnicodeXferBuf.SetLength(0);   return rv;}