gecko-dev/dom/html/PluginDocument.cpp

308 lines
9.0 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
2012-05-21 11:12:37 +00:00
/* 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 "nsIPluginDocument.h"
#include "nsGkAtoms.h"
#include "nsIPresShell.h"
#include "nsIObjectFrame.h"
#include "nsNPAPIPluginInstance.h"
#include "nsIDocumentInlines.h"
#include "nsIDocShellTreeItem.h"
#include "nsNodeInfoManager.h"
#include "nsContentCreatorFunctions.h"
#include "nsContentPolicyUtils.h"
#include "nsIPropertyBag2.h"
#include "mozilla/dom/Element.h"
#include "nsObjectLoadingContent.h"
#include "GeckoProfiler.h"
namespace mozilla {
namespace dom {
class PluginDocument final : public MediaDocument
, public nsIPluginDocument
{
public:
PluginDocument();
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_NSIPLUGINDOCUMENT
enum MediaDocumentKind MediaDocumentKind() const override
{
return MediaDocumentKind::Plugin;
}
nsresult StartDocumentLoad(const char* aCommand,
nsIChannel* aChannel,
nsILoadGroup* aLoadGroup,
nsISupports* aContainer,
nsIStreamListener** aDocListener,
bool aReset = true,
nsIContentSink* aSink = nullptr) override;
void SetScriptGlobalObject(nsIScriptGlobalObject* aScriptGlobalObject) override;
bool CanSavePresentation(nsIRequest *aNewRequest) override;
const nsCString& GetType() const { return mMimeType; }
Element* GetPluginContent() { return mPluginContent; }
void StartLayout() { MediaDocument::StartLayout(); }
virtual void Destroy() override
{
if (mStreamListener) {
mStreamListener->DropDocumentRef();
}
MediaDocument::Destroy();
}
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(PluginDocument, MediaDocument)
protected:
~PluginDocument() override;
nsresult CreateSyntheticPluginDocument();
nsCOMPtr<Element> mPluginContent;
Bug 1207245 - part 6 - rename nsRefPtr<T> to RefPtr<T>; r=ehsan; a=Tomcat The bulk of this commit was generated with a script, executed at the top level of a typical source code checkout. The only non-machine-generated part was modifying MFBT's moz.build to reflect the new naming. CLOSED TREE makes big refactorings like this a piece of cake. # The main substitution. find . -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.mm' -o -name '*.idl'| \ xargs perl -p -i -e ' s/nsRefPtr\.h/RefPtr\.h/g; # handle includes s/nsRefPtr ?</RefPtr</g; # handle declarations and variables ' # Handle a special friend declaration in gfx/layers/AtomicRefCountedWithFinalize.h. perl -p -i -e 's/::nsRefPtr;/::RefPtr;/' gfx/layers/AtomicRefCountedWithFinalize.h # Handle nsRefPtr.h itself, a couple places that define constructors # from nsRefPtr, and code generators specially. We do this here, rather # than indiscriminantly s/nsRefPtr/RefPtr/, because that would rename # things like nsRefPtrHashtable. perl -p -i -e 's/nsRefPtr/RefPtr/g' \ mfbt/nsRefPtr.h \ xpcom/glue/nsCOMPtr.h \ xpcom/base/OwningNonNull.h \ ipc/ipdl/ipdl/lower.py \ ipc/ipdl/ipdl/builtin.py \ dom/bindings/Codegen.py \ python/lldbutils/lldbutils/utils.py # In our indiscriminate substitution above, we renamed # nsRefPtrGetterAddRefs, the class behind getter_AddRefs. Fix that up. find . -name '*.cpp' -o -name '*.h' -o -name '*.idl' | \ xargs perl -p -i -e 's/nsRefPtrGetterAddRefs/RefPtrGetterAddRefs/g' if [ -d .git ]; then git mv mfbt/nsRefPtr.h mfbt/RefPtr.h else hg mv mfbt/nsRefPtr.h mfbt/RefPtr.h fi --HG-- rename : mfbt/nsRefPtr.h => mfbt/RefPtr.h
2015-10-18 05:24:48 +00:00
RefPtr<MediaDocumentStreamListener> mStreamListener;
nsCString mMimeType;
};
class PluginStreamListener : public MediaDocumentStreamListener
{
public:
explicit PluginStreamListener(PluginDocument* aDoc)
: MediaDocumentStreamListener(aDoc)
, mPluginDoc(aDoc)
{}
NS_IMETHOD OnStartRequest(nsIRequest* request, nsISupports *ctxt) override;
private:
Bug 1207245 - part 6 - rename nsRefPtr<T> to RefPtr<T>; r=ehsan; a=Tomcat The bulk of this commit was generated with a script, executed at the top level of a typical source code checkout. The only non-machine-generated part was modifying MFBT's moz.build to reflect the new naming. CLOSED TREE makes big refactorings like this a piece of cake. # The main substitution. find . -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.mm' -o -name '*.idl'| \ xargs perl -p -i -e ' s/nsRefPtr\.h/RefPtr\.h/g; # handle includes s/nsRefPtr ?</RefPtr</g; # handle declarations and variables ' # Handle a special friend declaration in gfx/layers/AtomicRefCountedWithFinalize.h. perl -p -i -e 's/::nsRefPtr;/::RefPtr;/' gfx/layers/AtomicRefCountedWithFinalize.h # Handle nsRefPtr.h itself, a couple places that define constructors # from nsRefPtr, and code generators specially. We do this here, rather # than indiscriminantly s/nsRefPtr/RefPtr/, because that would rename # things like nsRefPtrHashtable. perl -p -i -e 's/nsRefPtr/RefPtr/g' \ mfbt/nsRefPtr.h \ xpcom/glue/nsCOMPtr.h \ xpcom/base/OwningNonNull.h \ ipc/ipdl/ipdl/lower.py \ ipc/ipdl/ipdl/builtin.py \ dom/bindings/Codegen.py \ python/lldbutils/lldbutils/utils.py # In our indiscriminate substitution above, we renamed # nsRefPtrGetterAddRefs, the class behind getter_AddRefs. Fix that up. find . -name '*.cpp' -o -name '*.h' -o -name '*.idl' | \ xargs perl -p -i -e 's/nsRefPtrGetterAddRefs/RefPtrGetterAddRefs/g' if [ -d .git ]; then git mv mfbt/nsRefPtr.h mfbt/RefPtr.h else hg mv mfbt/nsRefPtr.h mfbt/RefPtr.h fi --HG-- rename : mfbt/nsRefPtr.h => mfbt/RefPtr.h
2015-10-18 05:24:48 +00:00
RefPtr<PluginDocument> mPluginDoc;
};
NS_IMETHODIMP
PluginStreamListener::OnStartRequest(nsIRequest* request, nsISupports *ctxt)
{
Bug 1375392 - Tweak the PROFILER_LABEL* macros. r=mstange. This patch makes the following changes to the macros. - Removes PROFILER_LABEL_FUNC. It's only suitable for use in functions outside classes, due to PROFILER_FUNCTION_NAME not getting class names, and it was mostly misused. - Removes PROFILER_FUNCTION_NAME. It's no longer used, and __func__ is universally available now anyway. - Combines the first two string literal arguments of PROFILER_LABEL and PROFILER_LABEL_DYNAMIC into a single argument. There was no good reason for them to be separate, and it forced a '::' in the label, which isn't always appropriate. Also, the meaning of the "name_space" argument was interpreted in an interesting variety of ways. - Adds an "AUTO_" prefix to PROFILER_LABEL and PROFILER_LABEL_DYNAMIC, to make it clearer they construct RAII objects rather than just being function calls. (I myself have screwed up the scoping because of this in the past.) - Fills in the 'js::ProfileEntry::Category::' qualifier within the macro, so the caller doesn't need to. This makes a *lot* more of the uses fit onto a single line. The patch also makes the following changes to the macro uses (beyond those required by the changes described above). - Fixes a bunch of labels that had gotten out of sync with the name of the class and/or function that encloses them. - Removes a useless PROFILER_LABEL use within a trivial scope in EventStateManager::DispatchMouseOrPointerEvent(). It clearly wasn't serving any useful purpose. It also serves as extra evidence that the AUTO_ prefix is a good idea. - Tweaks DecodePool::SyncRunIf{Preferred,Possible} so that the labelling is done within them, instead of at their callsites, because that's a more standard way of doing things. --HG-- extra : rebase_source : 318d1bc6fc1425a94aacbf489dd46e4f83211de4
2017-06-22 07:08:53 +00:00
AUTO_PROFILER_LABEL("PluginStreamListener::OnStartRequest", NETWORK);
nsCOMPtr<nsIContent> embed = mPluginDoc->GetPluginContent();
nsCOMPtr<nsIObjectLoadingContent> objlc = do_QueryInterface(embed);
nsCOMPtr<nsIStreamListener> objListener = do_QueryInterface(objlc);
if (!objListener) {
MOZ_ASSERT_UNREACHABLE("PluginStreamListener without appropriate content "
"node");
return NS_BINDING_ABORTED;
}
SetStreamListener(objListener);
// Sets up the ObjectLoadingContent tag as if it is waiting for a
// channel, so it can proceed with a load normally once it gets OnStartRequest
nsresult rv = objlc->InitializeFromChannel(request);
if (NS_FAILED(rv)) {
MOZ_ASSERT_UNREACHABLE("InitializeFromChannel failed");
return rv;
}
// Note that because we're now hooked up to a plugin listener, this will
// likely spawn a plugin, which may re-enter.
return MediaDocumentStreamListener::OnStartRequest(request, ctxt);
}
PluginDocument::PluginDocument()
{}
PluginDocument::~PluginDocument() = default;
NS_IMPL_CYCLE_COLLECTION_INHERITED(PluginDocument, MediaDocument,
mPluginContent)
NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED(PluginDocument,
MediaDocument,
nsIPluginDocument)
void
PluginDocument::SetScriptGlobalObject(nsIScriptGlobalObject* aScriptGlobalObject)
{
// Set the script global object on the superclass before doing
// anything that might require it....
MediaDocument::SetScriptGlobalObject(aScriptGlobalObject);
if (aScriptGlobalObject) {
if (!InitialSetupHasBeenDone()) {
// Create synthetic document
#ifdef DEBUG
nsresult rv =
#endif
CreateSyntheticPluginDocument();
NS_ASSERTION(NS_SUCCEEDED(rv), "failed to create synthetic document");
InitialSetupDone();
}
} else {
mStreamListener = nullptr;
}
}
bool
PluginDocument::CanSavePresentation(nsIRequest* aNewRequest)
{
// Full-page plugins cannot be cached, currently, because we don't have
// the stream listener data to feed to the plugin instance.
return false;
}
nsresult
PluginDocument::StartDocumentLoad(const char* aCommand,
nsIChannel* aChannel,
nsILoadGroup* aLoadGroup,
nsISupports* aContainer,
nsIStreamListener** aDocListener,
bool aReset,
nsIContentSink* aSink)
{
// do not allow message panes to host full-page plugins
// returning an error causes helper apps to take over
nsCOMPtr<nsIDocShellTreeItem> dsti (do_QueryInterface(aContainer));
if (dsti) {
bool isMsgPane = false;
dsti->NameEquals(NS_LITERAL_STRING("messagepane"), &isMsgPane);
if (isMsgPane) {
return NS_ERROR_FAILURE;
}
}
nsresult rv =
MediaDocument::StartDocumentLoad(aCommand, aChannel, aLoadGroup, aContainer,
aDocListener, aReset, aSink);
if (NS_FAILED(rv)) {
return rv;
}
rv = aChannel->GetContentType(mMimeType);
if (NS_FAILED(rv)) {
return rv;
}
MediaDocument::UpdateTitleAndCharset(mMimeType, aChannel);
mStreamListener = new PluginStreamListener(this);
NS_ASSERTION(aDocListener, "null aDocListener");
NS_ADDREF(*aDocListener = mStreamListener);
return rv;
}
nsresult
PluginDocument::CreateSyntheticPluginDocument()
{
NS_ASSERTION(!GetShell() || !GetShell()->DidInitialize(),
"Creating synthetic plugin document content too late");
// make our generic document
nsresult rv = MediaDocument::CreateSyntheticDocument();
NS_ENSURE_SUCCESS(rv, rv);
// then attach our plugin
Element* body = GetBodyElement();
if (!body) {
NS_WARNING("no body on plugin document!");
return NS_ERROR_FAILURE;
}
// remove margins from body
NS_NAMED_LITERAL_STRING(zero, "0");
body->SetAttr(kNameSpaceID_None, nsGkAtoms::marginwidth, zero, false);
body->SetAttr(kNameSpaceID_None, nsGkAtoms::marginheight, zero, false);
// make plugin content
Bug 1207245 - part 6 - rename nsRefPtr<T> to RefPtr<T>; r=ehsan; a=Tomcat The bulk of this commit was generated with a script, executed at the top level of a typical source code checkout. The only non-machine-generated part was modifying MFBT's moz.build to reflect the new naming. CLOSED TREE makes big refactorings like this a piece of cake. # The main substitution. find . -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.mm' -o -name '*.idl'| \ xargs perl -p -i -e ' s/nsRefPtr\.h/RefPtr\.h/g; # handle includes s/nsRefPtr ?</RefPtr</g; # handle declarations and variables ' # Handle a special friend declaration in gfx/layers/AtomicRefCountedWithFinalize.h. perl -p -i -e 's/::nsRefPtr;/::RefPtr;/' gfx/layers/AtomicRefCountedWithFinalize.h # Handle nsRefPtr.h itself, a couple places that define constructors # from nsRefPtr, and code generators specially. We do this here, rather # than indiscriminantly s/nsRefPtr/RefPtr/, because that would rename # things like nsRefPtrHashtable. perl -p -i -e 's/nsRefPtr/RefPtr/g' \ mfbt/nsRefPtr.h \ xpcom/glue/nsCOMPtr.h \ xpcom/base/OwningNonNull.h \ ipc/ipdl/ipdl/lower.py \ ipc/ipdl/ipdl/builtin.py \ dom/bindings/Codegen.py \ python/lldbutils/lldbutils/utils.py # In our indiscriminate substitution above, we renamed # nsRefPtrGetterAddRefs, the class behind getter_AddRefs. Fix that up. find . -name '*.cpp' -o -name '*.h' -o -name '*.idl' | \ xargs perl -p -i -e 's/nsRefPtrGetterAddRefs/RefPtrGetterAddRefs/g' if [ -d .git ]; then git mv mfbt/nsRefPtr.h mfbt/RefPtr.h else hg mv mfbt/nsRefPtr.h mfbt/RefPtr.h fi --HG-- rename : mfbt/nsRefPtr.h => mfbt/RefPtr.h
2015-10-18 05:24:48 +00:00
RefPtr<mozilla::dom::NodeInfo> nodeInfo;
nodeInfo = mNodeInfoManager->GetNodeInfo(nsGkAtoms::embed, nullptr,
kNameSpaceID_XHTML,
nsINode::ELEMENT_NODE);
rv = NS_NewHTMLElement(getter_AddRefs(mPluginContent), nodeInfo.forget(),
NOT_FROM_PARSER);
NS_ENSURE_SUCCESS(rv, rv);
// make it a named element
mPluginContent->SetAttr(kNameSpaceID_None, nsGkAtoms::name,
NS_LITERAL_STRING("plugin"), false);
// fill viewport and auto-resize
NS_NAMED_LITERAL_STRING(percent100, "100%");
mPluginContent->SetAttr(kNameSpaceID_None, nsGkAtoms::width, percent100,
false);
mPluginContent->SetAttr(kNameSpaceID_None, nsGkAtoms::height, percent100,
false);
// set URL
nsAutoCString src;
mDocumentURI->GetSpec(src);
mPluginContent->SetAttr(kNameSpaceID_None, nsGkAtoms::src,
NS_ConvertUTF8toUTF16(src), false);
// set mime type
mPluginContent->SetAttr(kNameSpaceID_None, nsGkAtoms::type,
NS_ConvertUTF8toUTF16(mMimeType), false);
// nsHTML(Shared)ObjectElement does not kick off a load on BindToTree if it is
// to a PluginDocument
body->AppendChildTo(mPluginContent, false);
return NS_OK;
}
NS_IMETHODIMP
PluginDocument::Print()
{
NS_ENSURE_TRUE(mPluginContent, NS_ERROR_FAILURE);
nsIObjectFrame* objectFrame =
do_QueryFrame(mPluginContent->GetPrimaryFrame());
if (objectFrame) {
RefPtr<nsNPAPIPluginInstance> pi = objectFrame->GetPluginInstance();
if (pi) {
NPPrint npprint;
npprint.mode = NP_FULL;
npprint.print.fullPrint.pluginPrinted = false;
npprint.print.fullPrint.printOne = false;
npprint.print.fullPrint.platformPrint = nullptr;
pi->Print(&npprint);
}
}
return NS_OK;
}
} // namespace dom
} // namespace mozilla
nsresult
NS_NewPluginDocument(nsIDocument** aResult)
{
auto* doc = new mozilla::dom::PluginDocument();
NS_ADDREF(doc);
nsresult rv = doc->Init();
if (NS_FAILED(rv)) {
NS_RELEASE(doc);
}
*aResult = doc;
return rv;
}