nsresult MediaDocument::StartDocumentLoad(const char* aCommand, nsIChannel* aChannel, nsILoadGroup* aLoadGroup, nsISupports* aContainer, nsIStreamListener** aDocListener, bool aReset, nsIContentSink* aSink) { nsresult rv = nsDocument::StartDocumentLoad(aCommand, aChannel, aLoadGroup, aContainer, aDocListener, aReset, aSink); if (NS_FAILED(rv)) { return rv; } // We try to set the charset of the current document to that of the // 'genuine' (as opposed to an intervening 'chrome') parent document // that may be in a different window/tab. Even if we fail here, // we just return NS_OK because another attempt is made in // |UpdateTitleAndCharset| and the worst thing possible is a mangled // filename in the titlebar and the file picker. // Note that we // exclude UTF-8 as 'invalid' because UTF-8 is likely to be the charset // of a chrome document that has nothing to do with the actual content // whose charset we want to know. Even if "the actual content" is indeed // in UTF-8, we don't lose anything because the default empty value is // considered synonymous with UTF-8. nsCOMPtr<nsIDocShell> docShell(do_QueryInterface(aContainer)); // not being able to set the charset is not critical. NS_ENSURE_TRUE(docShell, NS_OK); nsAutoCString charset; int32_t source; nsCOMPtr<nsIPrincipal> principal; // opening in a new tab docShell->GetParentCharset(charset, &source, getter_AddRefs(principal)); if (!charset.IsEmpty() && !charset.Equals("UTF-8") && NodePrincipal()->Equals(principal)) { SetDocumentCharacterSetSource(source); SetDocumentCharacterSet(charset); } return NS_OK; }
void MediaDocument::GetFileName(nsAString& aResult, nsIChannel* aChannel) { aResult.Truncate(); if (aChannel) { aChannel->GetContentDispositionFilename(aResult); if (!aResult.IsEmpty()) return; } nsCOMPtr<nsIURL> url = do_QueryInterface(mDocumentURI); if (!url) return; nsAutoCString fileName; url->GetFileName(fileName); if (fileName.IsEmpty()) return; nsAutoCString docCharset; // Now that the charset is set in |StartDocumentLoad| to the charset of // the document viewer instead of a bogus value ("ISO-8859-1" set in // |nsDocument|'s ctor), the priority is given to the current charset. // This is necessary to deal with a media document being opened in a new // window or a new tab, in which case |originCharset| of |nsIURI| is not // reliable. if (mCharacterSetSource != kCharsetUninitialized) { docCharset = mCharacterSet; } else { // resort to |originCharset| url->GetOriginCharset(docCharset); SetDocumentCharacterSet(docCharset); } nsresult rv; nsCOMPtr<nsITextToSubURI> textToSubURI = do_GetService(NS_ITEXTTOSUBURI_CONTRACTID, &rv); if (NS_SUCCEEDED(rv)) { // UnEscapeURIForUI always succeeds textToSubURI->UnEscapeURIForUI(docCharset, fileName, aResult); } else { CopyUTF8toUTF16(fileName, aResult); } }
nsresult MediaDocument::StartDocumentLoad(const char* aCommand, nsIChannel* aChannel, nsILoadGroup* aLoadGroup, nsISupports* aContainer, nsIStreamListener** aDocListener, bool aReset, nsIContentSink* aSink) { nsresult rv = nsDocument::StartDocumentLoad(aCommand, aChannel, aLoadGroup, aContainer, aDocListener, aReset, aSink); if (NS_FAILED(rv)) { return rv; } // We try to set the charset of the current document to that of the // 'genuine' (as opposed to an intervening 'chrome') parent document // that may be in a different window/tab. Even if we fail here, // we just return NS_OK because another attempt is made in // |UpdateTitleAndCharset| and the worst thing possible is a mangled // filename in the titlebar and the file picker. // When this document is opened in the window/tab of the referring // document (by a simple link-clicking), |prevDocCharacterSet| contains // the charset of the referring document. On the other hand, if the // document is opened in a new window, it is |defaultCharacterSet| of |muCV| // where the charset of our interest is stored. In case of openining // in a new tab, we get the charset from the docShell. Note that we // exclude UTF-8 as 'invalid' because UTF-8 is likely to be the charset // of a chrome document that has nothing to do with the actual content // whose charset we want to know. Even if "the actual content" is indeed // in UTF-8, we don't lose anything because the default empty value is // considered synonymous with UTF-8. nsCOMPtr<nsIDocShell> docShell(do_QueryInterface(aContainer)); // not being able to set the charset is not critical. NS_ENSURE_TRUE(docShell, NS_OK); nsAutoCString charset; // opening in a new tab docShell->GetParentCharset(charset); if (charset.IsEmpty() || charset.Equals("UTF-8")) { nsCOMPtr<nsIContentViewer> cv; docShell->GetContentViewer(getter_AddRefs(cv)); // not being able to set the charset is not critical. NS_ENSURE_TRUE(cv, NS_OK); nsCOMPtr<nsIMarkupDocumentViewer> muCV = do_QueryInterface(cv); if (muCV) { muCV->GetPrevDocCharacterSet(charset); // opening in the same window/tab if (charset.Equals("UTF-8") || charset.IsEmpty()) { muCV->GetDefaultCharacterSet(charset); // opening in a new window } } } if (!charset.IsEmpty() && !charset.Equals("UTF-8")) { SetDocumentCharacterSet(charset); mCharacterSetSource = kCharsetFromUserDefault; } return NS_OK; }
void nsMediaDocument::UpdateTitleAndCharset(const nsACString& aTypeStr, const char* const* aFormatNames, PRInt32 aWidth, PRInt32 aHeight, const nsAString& aStatus) { nsXPIDLString fileStr; if (mDocumentURI) { nsCAutoString fileName; nsCOMPtr<nsIURL> url = do_QueryInterface(mDocumentURI); if (url) url->GetFileName(fileName); nsCAutoString docCharset; // Now that the charset is set in |StartDocumentLoad| to the charset of // the document viewer instead of a bogus value ("ISO-8859-1" set in // |nsDocument|'s ctor), the priority is given to the current charset. // This is necessary to deal with a media document being opened in a new // window or a new tab, in which case |originCharset| of |nsIURI| is not // reliable. if (mCharacterSetSource != kCharsetUninitialized) { docCharset = mCharacterSet; } else { // resort to |originCharset| mDocumentURI->GetOriginCharset(docCharset); SetDocumentCharacterSet(docCharset); } if (!fileName.IsEmpty()) { nsresult rv; nsCOMPtr<nsITextToSubURI> textToSubURI = do_GetService(NS_ITEXTTOSUBURI_CONTRACTID, &rv); if (NS_SUCCEEDED(rv)) // UnEscapeURIForUI always succeeds textToSubURI->UnEscapeURIForUI(docCharset, fileName, fileStr); else CopyUTF8toUTF16(fileName, fileStr); } } NS_ConvertASCIItoUTF16 typeStr(aTypeStr); nsXPIDLString title; if (mStringBundle) { // if we got a valid size (not all media have a size) if (aWidth != 0 && aHeight != 0) { nsAutoString widthStr; nsAutoString heightStr; widthStr.AppendInt(aWidth); heightStr.AppendInt(aHeight); // If we got a filename, display it if (!fileStr.IsEmpty()) { const PRUnichar *formatStrings[4] = {fileStr.get(), typeStr.get(), widthStr.get(), heightStr.get()}; NS_ConvertASCIItoUTF16 fmtName(aFormatNames[eWithDimAndFile]); mStringBundle->FormatStringFromName(fmtName.get(), formatStrings, 4, getter_Copies(title)); } else { const PRUnichar *formatStrings[3] = {typeStr.get(), widthStr.get(), heightStr.get()}; NS_ConvertASCIItoUTF16 fmtName(aFormatNames[eWithDim]); mStringBundle->FormatStringFromName(fmtName.get(), formatStrings, 3, getter_Copies(title)); } } else { // If we got a filename, display it if (!fileStr.IsEmpty()) { const PRUnichar *formatStrings[2] = {fileStr.get(), typeStr.get()}; NS_ConvertASCIItoUTF16 fmtName(aFormatNames[eWithFile]); mStringBundle->FormatStringFromName(fmtName.get(), formatStrings, 2, getter_Copies(title)); } else { const PRUnichar *formatStrings[1] = {typeStr.get()}; NS_ConvertASCIItoUTF16 fmtName(aFormatNames[eWithNoInfo]); mStringBundle->FormatStringFromName(fmtName.get(), formatStrings, 1, getter_Copies(title)); } } } // set it on the document if (aStatus.IsEmpty()) { SetTitle(title); } else { nsXPIDLString titleWithStatus; const nsPromiseFlatString& status = PromiseFlatString(aStatus); const PRUnichar *formatStrings[2] = {title.get(), status.get()}; NS_NAMED_LITERAL_STRING(fmtName, "TitleWithStatus"); mStringBundle->FormatStringFromName(fmtName.get(), formatStrings, 2, getter_Copies(titleWithStatus)); SetTitle(titleWithStatus); } }
nsresult nsXMLDocument::StartDocumentLoad(const char* aCommand, nsIChannel* aChannel, nsILoadGroup* aLoadGroup, nsISupports* aContainer, nsIStreamListener **aDocListener, bool aReset, nsIContentSink* aSink) { nsresult rv = nsDocument::StartDocumentLoad(aCommand, aChannel, aLoadGroup, aContainer, aDocListener, aReset, aSink); if (NS_FAILED(rv)) return rv; if (nsCRT::strcmp("loadAsInteractiveData", aCommand) == 0) { mLoadedAsInteractiveData = true; aCommand = kLoadAsData; // XBL, for example, needs scripts and styles } int32_t charsetSource = kCharsetFromDocTypeDefault; nsAutoCString charset(NS_LITERAL_CSTRING("UTF-8")); TryChannelCharset(aChannel, charsetSource, charset, nullptr); nsCOMPtr<nsIURI> aUrl; rv = aChannel->GetURI(getter_AddRefs(aUrl)); if (NS_FAILED(rv)) return rv; static NS_DEFINE_CID(kCParserCID, NS_PARSER_CID); mParser = do_CreateInstance(kCParserCID, &rv); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIXMLContentSink> sink; if (aSink) { sink = do_QueryInterface(aSink); } else { nsCOMPtr<nsIDocShell> docShell; if (aContainer) { docShell = do_QueryInterface(aContainer); NS_ENSURE_TRUE(docShell, NS_ERROR_FAILURE); } rv = NS_NewXMLContentSink(getter_AddRefs(sink), this, aUrl, docShell, aChannel); NS_ENSURE_SUCCESS(rv, rv); } // Set the parser as the stream listener for the document loader... rv = CallQueryInterface(mParser, aDocListener); NS_ENSURE_SUCCESS(rv, rv); NS_ASSERTION(mChannel, "How can we not have a channel here?"); mChannelIsPending = true; SetDocumentCharacterSet(charset); mParser->SetDocumentCharset(charset, charsetSource); mParser->SetCommand(aCommand); mParser->SetContentSink(sink); mParser->Parse(aUrl, nullptr, (void *)this); return NS_OK; }