result = NS_ERROR_NOT_AVAILABLE; if (! strHTTPCharset.IsEmpty()) { result = mLoader->SetCharset(strHTTPCharset); } if (NS_FAILED(result)) {   result = mLoader->SetCharset(aString, aStringLen); } if (NS_FAILED(result)) {   nsCOMPtr<nsIStyleSheetLinkingElement> element(do_QueryInterface(mOwningElement)); if (element) { nsAutoString linkCharset; element->GetCharset(linkCharset); if (! linkCharset.IsEmpty()) { result = mLoader->SetCharset(linkCharset);
if (mLoader->mNavQuirkMode || Compare(contentType, NS_LITERAL_CSTRING("text/css"), nsCaseInsensitiveCStringComparator()) == 0 || contentType.IsEmpty()) {  nsAutoString strHTTPCharset; nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(request); if (httpChannel) { nsXPIDLCString httpCharset; httpChannel->GetCharset(getter_Copies(httpCharset)); CopyASCIItoUCS2(httpCharset, strHTTPCharset); } result = NS_ERROR_NOT_AVAILABLE; if (! strHTTPCharset.IsEmpty()) { result = mLoader->SetCharset(strHTTPCharset); } if (NS_FAILED(result)) {   result = mLoader->SetCharset(aString, aStringLen); } if (NS_FAILED(result)) {   nsCOMPtr<nsIStyleSheetLinkingElement> element(do_QueryInterface(mOwningElement)); if (element) { nsAutoString linkCharset; element->GetCharset(linkCharset); if (! linkCharset.IsEmpty()) { result = mLoader->SetCharset(linkCharset); }
SheetLoadData::OnStreamComplete(nsIStreamLoader* aLoader,                                nsISupports* aContext,                                nsresult aStatus,                                PRUint32 aStringLen,                                const char* aString){  NS_TIMELINE_OUTDENT();  NS_TIMELINE_MARK_LOADER("SheetLoadData::OnStreamComplete(%s)", aLoader);  nsresult result = NS_OK;  nsString *strUnicodeBuffer = nsnull;  if (aString && aStringLen>0) {    /*     * First determine the charset (if one is indicated)     * 1)  Check HTTP charset     * 2)  Check @charset rules     * 3)  Check "charset" attribute of the <LINK> or <?xml-stylesheet?>     *     * If all these fail to give us a charset, fall back on our     * default (document charset or ISO-8859-1 if we have no document     * charset)     */    nsAutoString strHTTPCharset;    nsCOMPtr<nsIRequest> request;    aLoader->GetRequest(getter_AddRefs(request));    nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(request);    if (httpChannel) {      nsXPIDLCString httpCharset;      httpChannel->GetCharset(getter_Copies(httpCharset));      CopyASCIItoUCS2(httpCharset, strHTTPCharset);    }    result = NS_ERROR_NOT_AVAILABLE;    if (! strHTTPCharset.IsEmpty()) {      result = mLoader->SetCharset(strHTTPCharset);    }    if (NS_FAILED(result)) {      //  We have no charset or the HTTP charset is not recognized.      //  Try @charset rules      result = mLoader->SetCharset(aString, aStringLen);    }    if (NS_FAILED(result)) {      // Now try the charset on the <link> or processing instruction      // that loaded us      nsCOMPtr<nsIStyleSheetLinkingElement>                    element(do_QueryInterface(mOwningElement));      if (element) {        nsAutoString linkCharset;        element->GetCharset(linkCharset);        if (! linkCharset.IsEmpty()) {          result = mLoader->SetCharset(linkCharset);        }      }    }    if (NS_FAILED(result)) {      // no useful data on charset.  Just set to empty string, and let      // SetCharset pick a sane default      mLoader->SetCharset(NS_LITERAL_STRING(""));    }          {      // now get the decoder      nsCOMPtr<nsICharsetConverterManager> ccm =                do_GetService(kCharsetConverterManagerCID, &result);      if (NS_SUCCEEDED(result) && ccm) {        nsString charset;        mLoader->GetCharset(charset);        nsIUnicodeDecoder *decoder = nsnull;        ccm->GetUnicodeDecoder(&charset,&decoder);        if (decoder) {          PRInt32 unicodeLength=0;          if (NS_SUCCEEDED(decoder->GetMaxLength(aString,aStringLen,&unicodeLength))) {            PRUnichar *unicodeString = nsnull;            strUnicodeBuffer = new nsString;            if (nsnull == strUnicodeBuffer) {              result = NS_ERROR_OUT_OF_MEMORY;            } else {              // make space for the decoding              strUnicodeBuffer->SetCapacity(unicodeLength);              unicodeString = (PRUnichar *) strUnicodeBuffer->get();              result = decoder->Convert(aString, (PRInt32 *) &aStringLen, unicodeString, &unicodeLength);              if (NS_SUCCEEDED(result)) {                strUnicodeBuffer->SetLength(unicodeLength);              } else {                strUnicodeBuffer->SetLength(0);              }            }          }          NS_RELEASE(decoder);        }      }    }  }  mLoader->DidLoadStyle(aLoader, strUnicodeBuffer, this, aStatus);  // NOTE: passed ownership of strUnicodeBuffer to mLoader in the call,   //       so nulling it out for clarity / safety  strUnicodeBuffer = nsnull;  // We added a reference when the loader was created. This  // release should destroy it.  NS_RELEASE(aLoader);  return result;}