Files
palemoon27/dom/html/MediaDocument.cpp
roytam1 7e7ba08548 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1119049 - Keyboard shortcuts should work in MediaDocuments without explicitly focusing the media element. r=bz (46be3d5343)
- fix (3ade1708af)
- Bug 967982 - Show findbar after unsuccessful Find Again [r=mikedeboer] (8f228f14a1)
- Bug 1071631 - fix findbar re-filling in last character, r=mikedeboer (1dcea48088)
- Bug 1214249 - Set MOZ_FLOAT_ABI in armv6 case r=glandium (8c53df59f6)
- Bug 1223093 - Bump required rustc version to 1.4. r=mshal (2e116f0dc6)
- Bug 1208566 - require Rust 1.5 for --enable-rust builds; r=mshal (091c824772)
- bump gcc version to 4.4 and use e.g. MacPorts gcc for safety (1af814d7bd)
- reorder and fix (c74b24dfbf)
- Bug 1043112 - Allow b2g to core dump when signal permits. r=jld (f5afeb0051)
- Bug 1190965 - Restrict syscalls in nsProfileLock to Linux. r=jld (9ac892b4eb)
- Bug 1227569 - Remove unsupported OpenVMS code. r=mccr8 (ea080668c9)
- Bug 1227248 - Part 1: Allow extending StrictOrderingOnAppendListWithFlags. r=gps (52260fd46d)
- Bug 1163358 - Add "psm" tag to PSM xpcshell and mochitest manifests. r=dkeeler (aa31fa4e2f)
- Bug 1167254 - Convert test_bug234856.html mochitest to an xpcshell test. r=keeler (1f20c9f997)
- Bug 1169195 - Convert test_bug644006.html mochitest to an xpcshell test. r=keeler (f391df2ed5)
- bug 1181376 - convert test_bug480619.html to an xpcshell test r=mgoodwin (3bb1a3b762)
- Bug 1167866 - Add result strings to PSM test_cert* xpcshell tests. r=keeler (74743ab236)
- Bug 1171819 - Convert test_cert_eku-*.js to generate certificates at build time. r=keeler (9aea62b0dc)
- bug 1203312 - split tlsserver certificates into ocsp_certs and bad_certs r=mgoodwin (599437ece7)
- bug 1209695 - fold mochitest test_bug413909.html into xpcshell test_cert_overrides.js r=mgoodwin (cd5769201b)
- bug 1187029 - convert test_bug480509.html to an xpcshell test r=jcj (966cb02692)
- bug 1174292 - convert test_cert_version.js to generate certificates at build time r=Cykesiopka (7acb99f5ee)
- bug 1173565 - convert test_pinning_dynamic.js to generate certificates at build time r=Cykesiopka (cf5844376a)
- bug 1178988 - convert test_ocsp_url to generate certificates at build time r=Cykesiopka (de5554cb7b)
- bug 1189427 - convert test_ocsp_fetch_method.js to generate certificates at build time r=mgoodwin (9955a340fa)
- bug 1194013 - convert test_name_constraints.js to generate certificates at build time r=Cykesiopka,mgoodwin (0060e4232f)
- bug 1196853 - convert test_cert_signatures.js to generate certificat s at build time r=jcj (ce0ab9e63a)
- bug 1190603 - convert test_keysize.js to generate certificates at build time r=Cykesiopka (3dd9972c8f)
- Bug 1228794 - Convert test_getchain.js to generate certificates at build time. r=keeler (cf6b4d3c47)
- Bug 901698 - Some tests for OCSP-must-staple; r=keeler (4f51b34bee)
- Bug 1179678 - Add result strings to misc PSM xpcshell tests. r=keeler (a2142e0ca1)
- Bug 1205962 - Address some pylint complaints about pycert.py and pykey.py, r=keeler (190cdb0e4a)
- Bug 1218999 - Back out changeset 5f32b2bcfa43 (bug 1188468) in favor of a more efficient solution. r=glandium (195e060c30)
- Bug 1222903 - Reject EV status for EV EE certs that are valid for longer than 27 months as well. r=keeler (811b0bcc46)
- Bug 1231315 - Build CONFIGURE_DEFINE_FILES at build time instead of during configure. r=gps (5242734649)
- gradle remove (b745c7b2e4)
- Bug 1232421 - force matching the start of the line for comments, r=glandium (de93f61f91)
- Bug 1232421 - followup: fix test to not use 'inline comments' which are not supported elsewhere, rs=bustage on a CLOSED TREE (9e34a9d721)
- Bug 1172645 - Make mozbuild's setup.py work; r=glandium (b73b656574)
- bug 1166976 - add pyasn1-modules python library r=ted,gerv (24c7f3253f)
- bug 1166976 - add Python-RSA python library r=ted,gerv (9628d1583f)
- bug 1166976 - generate some PSM xpcshell test certificates at build time r=Cykesiopka,mgoodwin,froydnj (af4744b19d)
- Bug 1224478 - Replace do_check_* calls with their Assert.jsm equivalents in PSM xpcshell tests. r=keeler (6846617e65)
- bug 975763 - move test_certificate_overrides.html to test_cert_override_bits_mismatches.js r=mgoodwin (4ba930e064)
- Bug 1171820 - Convert test_bug483440.html mochitest to an xpcshell test. r=keeler (d41a63cd13)
- Bug 1147726: Disable test_keysize_ev.js on slow B2G Emulator debug builds. r=dkeeler (df54c1bbc8)
- Bug 1147725 - Disable test_ocsp_fetch_method.js and test_ocsp_url.js on slow B2G Emulator debug builds. r=keeler (56e71d8d7f)
- bug 1081128 - test_pinning.js takes ~300 seconds on b2g debug emulator - request a longer timeout for it r=Cykesiopka (20ecf7ca8b)
- Bug 1145679 - Part 2 - Tests. r=keeler (046b88cd37)
- Bug 1227248 - Part 2: Add GeneratedTest{Certificate,Key} mozbuild templates. r=gps (810848b3bc)
- Bug 1227248 - Part 3: Make GeneratedTest{Certificate,Key} no-op when --disable-compile-environment. r=gps (563c26d64b)
- Bug 1130413 - Remove unused nsITokenPasswordDialogs::GetPassword() function. r=jjones (0cff903788)
- Bug 1157515 - CipherSuiteChangeObserver should clean itself up. r=keeler (e9175e8f89)
- Bug 1215779 - Remove broken (non-EC) DSA keygen code. r=keeler (98a09304c1)
- bug 1215270 - remove some unused functions from nsNSSShutDown.h r=Cykesiopka (ea0e9566f2)
- bug 1215690 - remove nsPSMUITracker r=Cykesiopka r=mgoodwin (98ea9b8481)
- bug 1205767 - prevent memory leak when generating an EC key with <keygen> r=ttaubert (3be4b5f545)
- Bug 488480 - Correct documentation about the function hasMatchingOverride() in nsICertOverrideService.idl. Original patch by Johnathan Nightingale. r=keeler (bf2b16f2c0)
- Bug 629558 - Pref to make Intermediate Cert Store memory-only. r=keeler (19632e2fd6)
- typo (86093804e3)
- Bug 278689 - Multiple Certificates with the same subject are not shown in the digital signature select cert combo (only one is shown) r=dkeeler (c5984b6857)
2023-07-06 10:17:46 +08:00

448 lines
14 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "MediaDocument.h"
#include "nsGkAtoms.h"
#include "nsRect.h"
#include "nsPresContext.h"
#include "nsIPresShell.h"
#include "nsIScrollable.h"
#include "nsViewManager.h"
#include "nsITextToSubURI.h"
#include "nsIURL.h"
#include "nsIContentViewer.h"
#include "nsIDocShell.h"
#include "nsCharsetSource.h" // kCharsetFrom* macro definition
#include "nsNodeInfoManager.h"
#include "nsContentUtils.h"
#include "nsDocElementCreatedNotificationRunner.h"
#include "mozilla/Services.h"
#include "nsServiceManagerUtils.h"
#include "nsIPrincipal.h"
#include "nsIMultiPartChannel.h"
namespace mozilla {
namespace dom {
MediaDocumentStreamListener::MediaDocumentStreamListener(MediaDocument *aDocument)
{
mDocument = aDocument;
}
MediaDocumentStreamListener::~MediaDocumentStreamListener()
{
}
NS_IMPL_ISUPPORTS(MediaDocumentStreamListener,
nsIRequestObserver,
nsIStreamListener)
void
MediaDocumentStreamListener::SetStreamListener(nsIStreamListener *aListener)
{
mNextStream = aListener;
}
NS_IMETHODIMP
MediaDocumentStreamListener::OnStartRequest(nsIRequest* request, nsISupports *ctxt)
{
NS_ENSURE_TRUE(mDocument, NS_ERROR_FAILURE);
mDocument->StartLayout();
if (mNextStream) {
return mNextStream->OnStartRequest(request, ctxt);
}
return NS_ERROR_PARSED_DATA_CACHED;
}
NS_IMETHODIMP
MediaDocumentStreamListener::OnStopRequest(nsIRequest* request,
nsISupports *ctxt,
nsresult status)
{
nsresult rv = NS_OK;
if (mNextStream) {
rv = mNextStream->OnStopRequest(request, ctxt, status);
}
// Don't release mDocument here if we're in the middle of a multipart response.
bool lastPart = true;
nsCOMPtr<nsIMultiPartChannel> mpchan(do_QueryInterface(request));
if (mpchan) {
mpchan->GetIsLastPart(&lastPart);
}
if (lastPart) {
mDocument = nullptr;
}
return rv;
}
NS_IMETHODIMP
MediaDocumentStreamListener::OnDataAvailable(nsIRequest* request,
nsISupports *ctxt,
nsIInputStream *inStr,
uint64_t sourceOffset,
uint32_t count)
{
if (mNextStream) {
return mNextStream->OnDataAvailable(request, ctxt, inStr, sourceOffset, count);
}
return NS_OK;
}
// default format names for MediaDocument.
const char* const MediaDocument::sFormatNames[4] =
{
"MediaTitleWithNoInfo", // eWithNoInfo
"MediaTitleWithFile", // eWithFile
"", // eWithDim
"" // eWithDimAndFile
};
MediaDocument::MediaDocument()
: nsHTMLDocument(),
mDocumentElementInserted(false)
{
}
MediaDocument::~MediaDocument()
{
}
nsresult
MediaDocument::Init()
{
nsresult rv = nsHTMLDocument::Init();
NS_ENSURE_SUCCESS(rv, rv);
// Create a bundle for the localization
nsCOMPtr<nsIStringBundleService> stringService =
mozilla::services::GetStringBundleService();
if (stringService) {
stringService->CreateBundle(NSMEDIADOCUMENT_PROPERTIES_URI,
getter_AddRefs(mStringBundle));
}
mIsSyntheticDocument = true;
return NS_OK;
}
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.EqualsLiteral("UTF-8") &&
NodePrincipal()->Equals(principal)) {
SetDocumentCharacterSetSource(source);
SetDocumentCharacterSet(charset);
}
return NS_OK;
}
void
MediaDocument::BecomeInteractive()
{
// In principle, if we knew the readyState code to work, we could infer
// restoration from GetReadyStateEnum() == nsIDocument::READYSTATE_COMPLETE.
bool restoring = false;
nsPIDOMWindow* window = GetWindow();
if (window) {
nsIDocShell* docShell = window->GetDocShell();
if (docShell) {
docShell->GetRestoringDocument(&restoring);
}
}
if (!restoring) {
MOZ_ASSERT(GetReadyStateEnum() == nsIDocument::READYSTATE_LOADING,
"Bad readyState");
SetReadyStateInternal(nsIDocument::READYSTATE_INTERACTIVE);
}
}
nsresult
MediaDocument::CreateSyntheticDocument()
{
// Synthesize an empty html document
nsresult rv;
RefPtr<mozilla::dom::NodeInfo> nodeInfo;
nodeInfo = mNodeInfoManager->GetNodeInfo(nsGkAtoms::html, nullptr,
kNameSpaceID_XHTML,
nsIDOMNode::ELEMENT_NODE);
RefPtr<nsGenericHTMLElement> root = NS_NewHTMLHtmlElement(nodeInfo.forget());
NS_ENSURE_TRUE(root, NS_ERROR_OUT_OF_MEMORY);
NS_ASSERTION(GetChildCount() == 0, "Shouldn't have any kids");
rv = AppendChildTo(root, false);
NS_ENSURE_SUCCESS(rv, rv);
nodeInfo = mNodeInfoManager->GetNodeInfo(nsGkAtoms::head, nullptr,
kNameSpaceID_XHTML,
nsIDOMNode::ELEMENT_NODE);
// Create a <head> so our title has somewhere to live
RefPtr<nsGenericHTMLElement> head = NS_NewHTMLHeadElement(nodeInfo.forget());
NS_ENSURE_TRUE(head, NS_ERROR_OUT_OF_MEMORY);
nodeInfo = mNodeInfoManager->GetNodeInfo(nsGkAtoms::meta, nullptr,
kNameSpaceID_XHTML,
nsIDOMNode::ELEMENT_NODE);
RefPtr<nsGenericHTMLElement> metaContent = NS_NewHTMLMetaElement(nodeInfo.forget());
NS_ENSURE_TRUE(metaContent, NS_ERROR_OUT_OF_MEMORY);
metaContent->SetAttr(kNameSpaceID_None, nsGkAtoms::name,
NS_LITERAL_STRING("viewport"),
true);
metaContent->SetAttr(kNameSpaceID_None, nsGkAtoms::content,
NS_LITERAL_STRING("width=device-width; height=device-height;"),
true);
head->AppendChildTo(metaContent, false);
root->AppendChildTo(head, false);
nodeInfo = mNodeInfoManager->GetNodeInfo(nsGkAtoms::body, nullptr,
kNameSpaceID_XHTML,
nsIDOMNode::ELEMENT_NODE);
RefPtr<nsGenericHTMLElement> body = NS_NewHTMLBodyElement(nodeInfo.forget());
NS_ENSURE_TRUE(body, NS_ERROR_OUT_OF_MEMORY);
root->AppendChildTo(body, false);
return NS_OK;
}
nsresult
MediaDocument::StartLayout()
{
mMayStartLayout = true;
nsCOMPtr<nsIPresShell> shell = GetShell();
// Don't mess with the presshell if someone has already handled
// its initial reflow.
if (shell && !shell->DidInitialize()) {
nsRect visibleArea = shell->GetPresContext()->GetVisibleArea();
nsresult rv = shell->Initialize(visibleArea.width, visibleArea.height);
NS_ENSURE_SUCCESS(rv, rv);
}
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::LinkStylesheet(const nsAString& aStylesheet)
{
RefPtr<mozilla::dom::NodeInfo> nodeInfo;
nodeInfo = mNodeInfoManager->GetNodeInfo(nsGkAtoms::link, nullptr,
kNameSpaceID_XHTML,
nsIDOMNode::ELEMENT_NODE);
RefPtr<nsGenericHTMLElement> link = NS_NewHTMLLinkElement(nodeInfo.forget());
NS_ENSURE_TRUE(link, NS_ERROR_OUT_OF_MEMORY);
link->SetAttr(kNameSpaceID_None, nsGkAtoms::rel,
NS_LITERAL_STRING("stylesheet"), true);
link->SetAttr(kNameSpaceID_None, nsGkAtoms::href, aStylesheet, true);
Element* head = GetHeadElement();
return head->AppendChildTo(link, false);
}
nsresult
MediaDocument::LinkScript(const nsAString& aScript)
{
RefPtr<mozilla::dom::NodeInfo> nodeInfo;
nodeInfo = mNodeInfoManager->GetNodeInfo(nsGkAtoms::script, nullptr,
kNameSpaceID_XHTML,
nsIDOMNode::ELEMENT_NODE);
RefPtr<nsGenericHTMLElement> script = NS_NewHTMLScriptElement(nodeInfo.forget());
NS_ENSURE_TRUE(script, NS_ERROR_OUT_OF_MEMORY);
script->SetAttr(kNameSpaceID_None, nsGkAtoms::type,
NS_LITERAL_STRING("text/javascript;version=1.8"), true);
script->SetAttr(kNameSpaceID_None, nsGkAtoms::src, aScript, true);
Element* head = GetHeadElement();
return head->AppendChildTo(script, false);
}
void
MediaDocument::UpdateTitleAndCharset(const nsACString& aTypeStr,
nsIChannel* aChannel,
const char* const* aFormatNames,
int32_t aWidth, int32_t aHeight,
const nsAString& aStatus)
{
nsXPIDLString fileStr;
GetFileName(fileStr, aChannel);
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 char16_t *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 char16_t *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 char16_t *formatStrings[2] = {fileStr.get(), typeStr.get()};
NS_ConvertASCIItoUTF16 fmtName(aFormatNames[eWithFile]);
mStringBundle->FormatStringFromName(fmtName.get(), formatStrings, 2,
getter_Copies(title));
}
else {
const char16_t *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 char16_t *formatStrings[2] = {title.get(), status.get()};
NS_NAMED_LITERAL_STRING(fmtName, "TitleWithStatus");
mStringBundle->FormatStringFromName(fmtName.get(), formatStrings, 2,
getter_Copies(titleWithStatus));
SetTitle(titleWithStatus);
}
}
void
MediaDocument::SetScriptGlobalObject(nsIScriptGlobalObject* aGlobalObject)
{
nsHTMLDocument::SetScriptGlobalObject(aGlobalObject);
if (!mDocumentElementInserted && aGlobalObject) {
mDocumentElementInserted = true;
nsContentUtils::AddScriptRunner(
new nsDocElementCreatedNotificationRunner(this));
}
}
} // namespace dom
} // namespace mozilla