Files
palemoon27/dom/plugins/ipc/PluginScriptableObjectChild.cpp
roytam1 df2987f659 import changes from `dev' branch of rmottola/Arctic-Fox:
- bit of Bug 1219134: P2. Use LibAV/FFmpeg logic to detect invalid pts. r=edwin (34d2a7d442)
- Bug 1195018 - Support 'background_color' member in the manifest processor. r=marcosc (1451b14321)
- Bug 1240490 - Fix console prefix used by the App Manifest processor. r=marcosc (e3196b53c8)
- Bug 1086997 - Localize developer warnings issued by the manifest processor. r=baku (7659fc5dd6)
- Bug 1258899 - teach manifest processor about dir member. r=baku (00b40c51c2)
- Bug 1262739 - Remove support for splash_screens member in Manifest Processor r=mconley (bceb0d2517)
- Bug 1264813 - Remove image object's density member from Web Manifest processor. r=mconley (f34da7259e)
- Bug 1264816 - Drop background_color from Web manifest image object. r=mconley (40c55f7bf9)
- Bug 1186908 - Return manifest members to canonical form after processing. r=mconley. (59d9a12a17)
- Bug 293394 - javascript: should never execute with chrome privs. r=bz (0dcc8f146b)
- bit of Bug 1153267 - part 2 (a0c5f7fda8)
- Bug 1254320 - Remove SEC_NORMAL from dom/json/ . r=bz (cd068db51c)
- Bug 1262590 - [e10s] AppProcessChecker.cpp has DEUBG typo preventing DEBUG messages. r=mconley (f9ffab4e57)
- Bug 1263556 - Remove EmptyBlobImpl in dom/ipc/Blob.cpp, r=smaug (3f4fe63e05)
- Bug 1261072 - File::GetName() should be const, r=smaug (bccd7475f1)
- Bug 1257759 part.1 Use switch-case at the first message handling in PluginInstanceChild::PluginWindowProcInternal() r=m_kato (bc0d4c457e)
- Bug 1257759 part.2 Separate Windows' message and related definitions from nsWindowDefs.h to mozilla/widget/WinMessages.h r=jimm (f8b5cb62f8)
- Bug 1153829 - Don't use MOZ_ALWAYS_INLINE for non-inline functions. r=jimm (c0e5b26c35)
- Bug 1257759 part.3 ModifierKeyState should be available in plugin module r=jimm (aa3fc81f63)
- Bug 1257759 part.4 Rename WidgetGUIEvent::PluginEvent to NativeEventData for using this class to send native event from plugin process to content and/or chrome process r=smaug (49392203ad)
- Bug 1255968 - Part 1: Let the callback of PeekMessages() to return a boolean to be able to break out the loop. r=dvander (d7f926ee7e)
- Bug 1255968 - Part 2: Implement puppet widget's HasPendingInputEvent() for interruptible reflow to work in content process. r=bz (5a0915e650)
- Bug 1257759 part.5 PluginInstanceChild should post received native key event to chrome process if the key combination may be a shortcut key r=jimm (ad1e3ad1c0)
- Bug 1252152 - Make plugin instances destroyed while that instance is on the stack crash earlier and more usefully, r=jimm (2238cc2a79)
- Bug 1257759 part.6 Keep event order between keyboard events and IME events in a plugin process r=jimm (32cbe1b13e)
- Bug 1257759 part.7 Add new internal events which represent key events on plugin r=smaug (161725755a)
- Bug 1257759 part.8 nsXBLWindowKeyHandler should handle eKeyDownOnPlugin and eKeyUpOnPlugin events only with reserved shortcut key handlers r=smaug (aa7cd1d786)
- Bug 1257759 part.9 Implement nsWindow::OnKeyEventInPluginProcess() on Windows r=jimm (245b2709e4)
- Bug 1257759 part.10 PluginInstanceChild should consume WM_*CHAR messages which follow consumed WM_*KEYDOWN or WM_*KEYUP message r=jimm (db355e6a77)
- Bug 1261735 (part 1) - Overhaul the atom implementation. r=froydnj,erahm. (abbed483f7)
- Bug 1261735 (part 2) - Inline some {Dynamic,Static}Atom methods. r=erahm. (97b11fa656)
- Bug 1261735 (part 3) - De-virtualize nsIAtom::IsStaticAtom(). r=froydnj,erahm. (4e0465a84a)
- Bug 1257207 - Increase ATOM_HASHTABLE_INITIAL_LENGTH. r=froydnj. (fe663e9c77)
- Bug 1261735 (part 4) - Change StaticAtomEntry::mAtom to |StaticAtom*|. r=erahm. (999fff7b8c)
- Bug 1261744 - Add two missing null checks for nsStringBuffer::Alloc(). r=erahm. (dd9ef00b6a)
- Bug 1266295 - Remove unnecessary compiler version checks for gcc <= 4.8.0 in xpcom. r=froydnj (b2f17f8d1a)
- Bug 1259706: Add NS_INLINE_DECL_THREADSAFE_VIRTUAL_REFCOUNTING macro. r=froydnj (d56a5fd48b)
- just a space (1508ec768f)
- Bug 1236991 - part 1: allow forwarding label direction through nsITooltipTextProvider, r=enndeakin (8d49127ffa)
- Bug 1245649: Enable no-nested-ternary. r=mconley (43c7b99372)
- Bug 1236991 - part 2: implement a default tooltiptextprovider in toolkit, r=enndeakin (a0a378979f)
- Bug 1251032 - Send RenderFrame info down to child in BrowserFrameOpenWindow. r=kanru (aec01fcec9)
- Bug 1246327 - Remove dom.always_allow_move_resize_window preference. r=jst (d06d83d596)
- Bug 1158228 - merge github's readability code into m-c, rs=me (38da823ce7)
- Bug 1158184 - merge recent github readability changes into m-c, rs=me (3b634d63c8)
- Bug 1162917 - update readability from github repo, rs=me (29dd18a9f0)
- No bug - update readability from github repo, includes fix for bug 1230050, rs=uplift-with-r+-patches-from-github (fa04927c71)
- Bug 1265866 - update Readability to the latest version from github, rs=me (c89e4dbeac)
- bits of Bug 1158228 (950dd3d561)
- Bug 1249579 - part2 : audio competing suspend/resume methods. r=snorp, baku. (3b6ddc018d)
- Bug 1266221 P1 Get devtools http service worker testing option from top window. r=bz (e8492118f0)
- Bug 225910 - Use nsIURI's GetRef and GetHasRef in nsDocShell. r=bz (76da6473d4)
- Bug 1261471: Remove support for getting mozIDOMWindowProxy via GetInterface. r=mrbkap (04904ed142)
- Bug 1264725 - Isolate Troubleshoot.jsm (about:support) from addons with null names. r=felipe (4ebb240827)
- Bug 1227730 - Support closing libnotify alerts. r=karlt,MattN (73d8b583dc)
2024-05-23 23:25:42 +08:00

1299 lines
35 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: sw=2 ts=2 et :
* 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 "PluginScriptableObjectChild.h"
#include "PluginScriptableObjectUtils.h"
#include "mozilla/plugins/PluginTypes.h"
using namespace mozilla::plugins;
/**
* NPIdentifiers in the plugin process use a tagged representation. The low bit
* stores the tag. If it's zero, the identifier is a string, and the value is a
* pointer to a StoredIdentifier. If the tag bit is 1, then the rest of the
* NPIdentifier value is the integer itself. Like the JSAPI, we require that all
* integers stored in NPIdentifier be non-negative.
*
* String identifiers are stored in the sIdentifiers hashtable to ensure
* uniqueness. The lifetime of these identifiers is only as long as the incoming
* IPC call from the chrome process. If the plugin wants to retain an
* identifier, it needs to call NPN_GetStringIdentifier, which causes the
* mPermanent flag to be set on the identifier. When this flag is set, the
* identifier is saved until the plugin process exits.
*
* The StackIdentifier RAII class is used to manage ownership of
* identifiers. Any identifier obtained from this class should not be used
* outside its scope, except when the MakePermanent() method has been called on
* it.
*
* The lifetime of an NPIdentifier in the plugin process is totally divorced
* from the lifetime of an NPIdentifier in the chrome process (where an
* NPIdentifier is stored as a jsid). The JS GC in the chrome process is able to
* trace through the entire heap, unlike in the plugin process, so there is no
* reason to retain identifiers there.
*/
PluginScriptableObjectChild::IdentifierTable PluginScriptableObjectChild::sIdentifiers;
/* static */ PluginScriptableObjectChild::StoredIdentifier*
PluginScriptableObjectChild::HashIdentifier(const nsCString& aIdentifier)
{
StoredIdentifier* stored = sIdentifiers.Get(aIdentifier).get();
if (stored) {
return stored;
}
stored = new StoredIdentifier(aIdentifier);
sIdentifiers.Put(aIdentifier, stored);
return stored;
}
/* static */ void
PluginScriptableObjectChild::UnhashIdentifier(StoredIdentifier* aStored)
{
MOZ_ASSERT(sIdentifiers.Get(aStored->mIdentifier));
sIdentifiers.Remove(aStored->mIdentifier);
}
/* static */ void
PluginScriptableObjectChild::ClearIdentifiers()
{
sIdentifiers.Clear();
}
PluginScriptableObjectChild::StackIdentifier::StackIdentifier(const PluginIdentifier& aIdentifier)
: mIdentifier(aIdentifier),
mStored(nullptr)
{
if (aIdentifier.type() == PluginIdentifier::TnsCString) {
mStored = PluginScriptableObjectChild::HashIdentifier(mIdentifier.get_nsCString());
}
}
PluginScriptableObjectChild::StackIdentifier::StackIdentifier(NPIdentifier aIdentifier)
: mStored(nullptr)
{
uintptr_t bits = reinterpret_cast<uintptr_t>(aIdentifier);
if (bits & 1) {
int32_t num = int32_t(bits >> 1);
mIdentifier = PluginIdentifier(num);
} else {
mStored = static_cast<StoredIdentifier*>(aIdentifier);
mIdentifier = mStored->mIdentifier;
}
}
PluginScriptableObjectChild::StackIdentifier::~StackIdentifier()
{
if (!mStored) {
return;
}
// Each StackIdentifier owns one reference to its StoredIdentifier. In
// addition, the sIdentifiers table owns a reference. If mPermanent is false
// and sIdentifiers has the last reference, then we want to remove the
// StoredIdentifier from the table (and destroy it).
StoredIdentifier *stored = mStored;
mStored = nullptr;
if (stored->mRefCnt == 1 && !stored->mPermanent) {
PluginScriptableObjectChild::UnhashIdentifier(stored);
}
}
NPIdentifier
PluginScriptableObjectChild::StackIdentifier::ToNPIdentifier() const
{
if (mStored) {
MOZ_ASSERT(mIdentifier.type() == PluginIdentifier::TnsCString);
MOZ_ASSERT((reinterpret_cast<uintptr_t>(mStored.get()) & 1) == 0);
return mStored;
}
int32_t num = mIdentifier.get_int32_t();
// The JS engine imposes this condition on int32s in jsids, so we assume it.
MOZ_ASSERT(num >= 0);
return reinterpret_cast<NPIdentifier>((num << 1) | 1);
}
static PluginIdentifier
FromNPIdentifier(NPIdentifier aIdentifier)
{
PluginScriptableObjectChild::StackIdentifier stack(aIdentifier);
return stack.GetIdentifier();
}
// static
NPObject*
PluginScriptableObjectChild::ScriptableAllocate(NPP aInstance,
NPClass* aClass)
{
AssertPluginThread();
if (aClass != GetClass()) {
NS_RUNTIMEABORT("Huh?! Wrong class!");
}
return new ChildNPObject();
}
// static
void
PluginScriptableObjectChild::ScriptableInvalidate(NPObject* aObject)
{
AssertPluginThread();
if (aObject->_class != GetClass()) {
NS_RUNTIMEABORT("Don't know what kind of object this is!");
}
ChildNPObject* object = reinterpret_cast<ChildNPObject*>(aObject);
if (object->invalidated) {
// This can happen more than once, and is just fine.
return;
}
object->invalidated = true;
}
// static
void
PluginScriptableObjectChild::ScriptableDeallocate(NPObject* aObject)
{
AssertPluginThread();
if (aObject->_class != GetClass()) {
NS_RUNTIMEABORT("Don't know what kind of object this is!");
}
ChildNPObject* object = reinterpret_cast<ChildNPObject*>(aObject);
PluginScriptableObjectChild* actor = object->parent;
if (actor) {
NS_ASSERTION(actor->Type() == Proxy, "Bad type!");
actor->DropNPObject();
}
delete object;
}
// static
bool
PluginScriptableObjectChild::ScriptableHasMethod(NPObject* aObject,
NPIdentifier aName)
{
AssertPluginThread();
if (aObject->_class != GetClass()) {
NS_RUNTIMEABORT("Don't know what kind of object this is!");
}
ChildNPObject* object = reinterpret_cast<ChildNPObject*>(aObject);
if (object->invalidated) {
NS_WARNING("Calling method on an invalidated object!");
return false;
}
ProtectedActor<PluginScriptableObjectChild> actor(object->parent);
NS_ASSERTION(actor, "This shouldn't ever be null!");
NS_ASSERTION(actor->Type() == Proxy, "Bad type!");
bool result;
actor->CallHasMethod(FromNPIdentifier(aName), &result);
return result;
}
// static
bool
PluginScriptableObjectChild::ScriptableInvoke(NPObject* aObject,
NPIdentifier aName,
const NPVariant* aArgs,
uint32_t aArgCount,
NPVariant* aResult)
{
AssertPluginThread();
if (aObject->_class != GetClass()) {
NS_RUNTIMEABORT("Don't know what kind of object this is!");
}
ChildNPObject* object = reinterpret_cast<ChildNPObject*>(aObject);
if (object->invalidated) {
NS_WARNING("Calling method on an invalidated object!");
return false;
}
ProtectedActor<PluginScriptableObjectChild> actor(object->parent);
NS_ASSERTION(actor, "This shouldn't ever be null!");
NS_ASSERTION(actor->Type() == Proxy, "Bad type!");
ProtectedVariantArray args(aArgs, aArgCount, actor->GetInstance());
if (!args.IsOk()) {
NS_ERROR("Failed to convert arguments!");
return false;
}
Variant remoteResult;
bool success;
actor->CallInvoke(FromNPIdentifier(aName), args,
&remoteResult, &success);
if (!success) {
return false;
}
ConvertToVariant(remoteResult, *aResult);
return true;
}
// static
bool
PluginScriptableObjectChild::ScriptableInvokeDefault(NPObject* aObject,
const NPVariant* aArgs,
uint32_t aArgCount,
NPVariant* aResult)
{
AssertPluginThread();
if (aObject->_class != GetClass()) {
NS_RUNTIMEABORT("Don't know what kind of object this is!");
}
ChildNPObject* object = reinterpret_cast<ChildNPObject*>(aObject);
if (object->invalidated) {
NS_WARNING("Calling method on an invalidated object!");
return false;
}
ProtectedActor<PluginScriptableObjectChild> actor(object->parent);
NS_ASSERTION(actor, "This shouldn't ever be null!");
NS_ASSERTION(actor->Type() == Proxy, "Bad type!");
ProtectedVariantArray args(aArgs, aArgCount, actor->GetInstance());
if (!args.IsOk()) {
NS_ERROR("Failed to convert arguments!");
return false;
}
Variant remoteResult;
bool success;
actor->CallInvokeDefault(args, &remoteResult, &success);
if (!success) {
return false;
}
ConvertToVariant(remoteResult, *aResult);
return true;
}
// static
bool
PluginScriptableObjectChild::ScriptableHasProperty(NPObject* aObject,
NPIdentifier aName)
{
AssertPluginThread();
if (aObject->_class != GetClass()) {
NS_RUNTIMEABORT("Don't know what kind of object this is!");
}
ChildNPObject* object = reinterpret_cast<ChildNPObject*>(aObject);
if (object->invalidated) {
NS_WARNING("Calling method on an invalidated object!");
return false;
}
ProtectedActor<PluginScriptableObjectChild> actor(object->parent);
NS_ASSERTION(actor, "This shouldn't ever be null!");
NS_ASSERTION(actor->Type() == Proxy, "Bad type!");
bool result;
actor->CallHasProperty(FromNPIdentifier(aName), &result);
return result;
}
// static
bool
PluginScriptableObjectChild::ScriptableGetProperty(NPObject* aObject,
NPIdentifier aName,
NPVariant* aResult)
{
AssertPluginThread();
if (aObject->_class != GetClass()) {
NS_RUNTIMEABORT("Don't know what kind of object this is!");
}
ChildNPObject* object = reinterpret_cast<ChildNPObject*>(aObject);
if (object->invalidated) {
NS_WARNING("Calling method on an invalidated object!");
return false;
}
ProtectedActor<PluginScriptableObjectChild> actor(object->parent);
NS_ASSERTION(actor, "This shouldn't ever be null!");
NS_ASSERTION(actor->Type() == Proxy, "Bad type!");
PluginInstanceChild::AutoStackHelper guard(actor->mInstance);
Variant result;
bool success;
actor->CallGetParentProperty(FromNPIdentifier(aName),
&result, &success);
if (!success) {
return false;
}
ConvertToVariant(result, *aResult);
return true;
}
// static
bool
PluginScriptableObjectChild::ScriptableSetProperty(NPObject* aObject,
NPIdentifier aName,
const NPVariant* aValue)
{
AssertPluginThread();
if (aObject->_class != GetClass()) {
NS_RUNTIMEABORT("Don't know what kind of object this is!");
}
ChildNPObject* object = reinterpret_cast<ChildNPObject*>(aObject);
if (object->invalidated) {
NS_WARNING("Calling method on an invalidated object!");
return false;
}
ProtectedActor<PluginScriptableObjectChild> actor(object->parent);
NS_ASSERTION(actor, "This shouldn't ever be null!");
NS_ASSERTION(actor->Type() == Proxy, "Bad type!");
ProtectedVariant value(*aValue, actor->GetInstance());
if (!value.IsOk()) {
NS_WARNING("Failed to convert variant!");
return false;
}
bool success;
actor->CallSetProperty(FromNPIdentifier(aName), value,
&success);
return success;
}
// static
bool
PluginScriptableObjectChild::ScriptableRemoveProperty(NPObject* aObject,
NPIdentifier aName)
{
AssertPluginThread();
if (aObject->_class != GetClass()) {
NS_RUNTIMEABORT("Don't know what kind of object this is!");
}
ChildNPObject* object = reinterpret_cast<ChildNPObject*>(aObject);
if (object->invalidated) {
NS_WARNING("Calling method on an invalidated object!");
return false;
}
ProtectedActor<PluginScriptableObjectChild> actor(object->parent);
NS_ASSERTION(actor, "This shouldn't ever be null!");
NS_ASSERTION(actor->Type() == Proxy, "Bad type!");
bool success;
actor->CallRemoveProperty(FromNPIdentifier(aName),
&success);
return success;
}
// static
bool
PluginScriptableObjectChild::ScriptableEnumerate(NPObject* aObject,
NPIdentifier** aIdentifiers,
uint32_t* aCount)
{
AssertPluginThread();
if (aObject->_class != GetClass()) {
NS_RUNTIMEABORT("Don't know what kind of object this is!");
}
ChildNPObject* object = reinterpret_cast<ChildNPObject*>(aObject);
if (object->invalidated) {
NS_WARNING("Calling method on an invalidated object!");
return false;
}
ProtectedActor<PluginScriptableObjectChild> actor(object->parent);
NS_ASSERTION(actor, "This shouldn't ever be null!");
NS_ASSERTION(actor->Type() == Proxy, "Bad type!");
AutoTArray<PluginIdentifier, 10> identifiers;
bool success;
actor->CallEnumerate(&identifiers, &success);
if (!success) {
return false;
}
*aCount = identifiers.Length();
if (!*aCount) {
*aIdentifiers = nullptr;
return true;
}
*aIdentifiers = reinterpret_cast<NPIdentifier*>(
PluginModuleChild::sBrowserFuncs.memalloc(*aCount * sizeof(NPIdentifier)));
if (!*aIdentifiers) {
NS_ERROR("Out of memory!");
return false;
}
for (uint32_t index = 0; index < *aCount; index++) {
StackIdentifier id(identifiers[index]);
// Make the id permanent in case the plugin retains it.
id.MakePermanent();
(*aIdentifiers)[index] = id.ToNPIdentifier();
}
return true;
}
// static
bool
PluginScriptableObjectChild::ScriptableConstruct(NPObject* aObject,
const NPVariant* aArgs,
uint32_t aArgCount,
NPVariant* aResult)
{
AssertPluginThread();
if (aObject->_class != GetClass()) {
NS_RUNTIMEABORT("Don't know what kind of object this is!");
}
ChildNPObject* object = reinterpret_cast<ChildNPObject*>(aObject);
if (object->invalidated) {
NS_WARNING("Calling method on an invalidated object!");
return false;
}
ProtectedActor<PluginScriptableObjectChild> actor(object->parent);
NS_ASSERTION(actor, "This shouldn't ever be null!");
NS_ASSERTION(actor->Type() == Proxy, "Bad type!");
ProtectedVariantArray args(aArgs, aArgCount, actor->GetInstance());
if (!args.IsOk()) {
NS_ERROR("Failed to convert arguments!");
return false;
}
Variant remoteResult;
bool success;
actor->CallConstruct(args, &remoteResult, &success);
if (!success) {
return false;
}
ConvertToVariant(remoteResult, *aResult);
return true;
}
const NPClass PluginScriptableObjectChild::sNPClass = {
NP_CLASS_STRUCT_VERSION,
PluginScriptableObjectChild::ScriptableAllocate,
PluginScriptableObjectChild::ScriptableDeallocate,
PluginScriptableObjectChild::ScriptableInvalidate,
PluginScriptableObjectChild::ScriptableHasMethod,
PluginScriptableObjectChild::ScriptableInvoke,
PluginScriptableObjectChild::ScriptableInvokeDefault,
PluginScriptableObjectChild::ScriptableHasProperty,
PluginScriptableObjectChild::ScriptableGetProperty,
PluginScriptableObjectChild::ScriptableSetProperty,
PluginScriptableObjectChild::ScriptableRemoveProperty,
PluginScriptableObjectChild::ScriptableEnumerate,
PluginScriptableObjectChild::ScriptableConstruct
};
PluginScriptableObjectChild::PluginScriptableObjectChild(
ScriptableObjectType aType)
: mInstance(nullptr),
mObject(nullptr),
mInvalidated(false),
mProtectCount(0),
mType(aType)
{
AssertPluginThread();
}
PluginScriptableObjectChild::~PluginScriptableObjectChild()
{
AssertPluginThread();
if (mObject) {
UnregisterActor(mObject);
if (mObject->_class == GetClass()) {
NS_ASSERTION(mType == Proxy, "Wrong type!");
static_cast<ChildNPObject*>(mObject)->parent = nullptr;
}
else {
NS_ASSERTION(mType == LocalObject, "Wrong type!");
PluginModuleChild::sBrowserFuncs.releaseobject(mObject);
}
}
}
bool
PluginScriptableObjectChild::InitializeProxy()
{
AssertPluginThread();
NS_ASSERTION(mType == Proxy, "Bad type!");
NS_ASSERTION(!mObject, "Calling Initialize more than once!");
NS_ASSERTION(!mInvalidated, "Already invalidated?!");
mInstance = static_cast<PluginInstanceChild*>(Manager());
NS_ASSERTION(mInstance, "Null manager?!");
NPObject* object = CreateProxyObject();
if (!object) {
NS_ERROR("Failed to create object!");
return false;
}
if (!RegisterActor(object)) {
NS_ERROR("RegisterActor failed");
return false;
}
mObject = object;
return true;
}
void
PluginScriptableObjectChild::InitializeLocal(NPObject* aObject)
{
AssertPluginThread();
NS_ASSERTION(mType == LocalObject, "Bad type!");
NS_ASSERTION(!mObject, "Calling Initialize more than once!");
NS_ASSERTION(!mInvalidated, "Already invalidated?!");
mInstance = static_cast<PluginInstanceChild*>(Manager());
NS_ASSERTION(mInstance, "Null manager?!");
PluginModuleChild::sBrowserFuncs.retainobject(aObject);
NS_ASSERTION(!mProtectCount, "Should be zero!");
mProtectCount++;
if (!RegisterActor(aObject)) {
NS_ERROR("RegisterActor failed");
}
mObject = aObject;
}
NPObject*
PluginScriptableObjectChild::CreateProxyObject()
{
NS_ASSERTION(mInstance, "Must have an instance!");
NS_ASSERTION(mType == Proxy, "Shouldn't call this for non-proxy object!");
NPClass* proxyClass = const_cast<NPClass*>(GetClass());
NPObject* npobject =
PluginModuleChild::sBrowserFuncs.createobject(mInstance->GetNPP(),
proxyClass);
NS_ASSERTION(npobject, "Failed to create object?!");
NS_ASSERTION(npobject->_class == GetClass(), "Wrong kind of object!");
NS_ASSERTION(npobject->referenceCount == 1, "Some kind of live object!");
ChildNPObject* object = static_cast<ChildNPObject*>(npobject);
NS_ASSERTION(!object->invalidated, "Bad object!");
NS_ASSERTION(!object->parent, "Bad object!");
// We don't want to have the actor own this object but rather let the object
// own this actor. Set the reference count to 0 here so that when the object
// dies we will send the destructor message to the child.
object->referenceCount = 0;
NS_LOG_RELEASE(object, 0, "NPObject");
object->parent = const_cast<PluginScriptableObjectChild*>(this);
return object;
}
bool
PluginScriptableObjectChild::ResurrectProxyObject()
{
NS_ASSERTION(mInstance, "Must have an instance already!");
NS_ASSERTION(!mObject, "Should not have an object already!");
NS_ASSERTION(mType == Proxy, "Shouldn't call this for non-proxy object!");
if (!InitializeProxy()) {
NS_ERROR("Initialize failed!");
return false;
}
SendProtect();
return true;
}
NPObject*
PluginScriptableObjectChild::GetObject(bool aCanResurrect)
{
if (!mObject && aCanResurrect && !ResurrectProxyObject()) {
NS_ERROR("Null object!");
return nullptr;
}
return mObject;
}
void
PluginScriptableObjectChild::Protect()
{
NS_ASSERTION(mObject, "No object!");
NS_ASSERTION(mProtectCount >= 0, "Negative retain count?!");
if (mType == LocalObject) {
++mProtectCount;
}
}
void
PluginScriptableObjectChild::Unprotect()
{
NS_ASSERTION(mObject, "Bad state!");
NS_ASSERTION(mProtectCount >= 0, "Negative retain count?!");
if (mType == LocalObject) {
if (--mProtectCount == 0) {
PluginScriptableObjectChild::Send__delete__(this);
}
}
}
void
PluginScriptableObjectChild::DropNPObject()
{
NS_ASSERTION(mObject, "Invalidated object!");
NS_ASSERTION(mObject->_class == GetClass(), "Wrong type of object!");
NS_ASSERTION(mType == Proxy, "Shouldn't call this for non-proxy object!");
// We think we're about to be deleted, but we could be racing with the other
// process.
UnregisterActor(mObject);
mObject = nullptr;
SendUnprotect();
}
void
PluginScriptableObjectChild::NPObjectDestroyed()
{
NS_ASSERTION(LocalObject == mType,
"ScriptableDeallocate should have handled this for proxies");
mInvalidated = true;
mObject = nullptr;
}
bool
PluginScriptableObjectChild::AnswerInvalidate()
{
AssertPluginThread();
PluginInstanceChild::AutoStackHelper guard(mInstance);
if (mInvalidated) {
return true;
}
mInvalidated = true;
NS_ASSERTION(mObject->_class != GetClass(), "Bad object type!");
NS_ASSERTION(mType == LocalObject, "Bad type!");
if (mObject->_class && mObject->_class->invalidate) {
mObject->_class->invalidate(mObject);
}
Unprotect();
return true;
}
bool
PluginScriptableObjectChild::AnswerHasMethod(const PluginIdentifier& aId,
bool* aHasMethod)
{
AssertPluginThread();
PluginInstanceChild::AutoStackHelper guard(mInstance);
if (mInvalidated) {
NS_WARNING("Calling AnswerHasMethod with an invalidated object!");
*aHasMethod = false;
return true;
}
NS_ASSERTION(mObject->_class != GetClass(), "Bad object type!");
NS_ASSERTION(mType == LocalObject, "Bad type!");
if (!(mObject->_class && mObject->_class->hasMethod)) {
*aHasMethod = false;
return true;
}
StackIdentifier id(aId);
*aHasMethod = mObject->_class->hasMethod(mObject, id.ToNPIdentifier());
return true;
}
bool
PluginScriptableObjectChild::AnswerInvoke(const PluginIdentifier& aId,
InfallibleTArray<Variant>&& aArgs,
Variant* aResult,
bool* aSuccess)
{
AssertPluginThread();
PluginInstanceChild::AutoStackHelper guard(mInstance);
if (mInvalidated) {
NS_WARNING("Calling AnswerInvoke with an invalidated object!");
*aResult = void_t();
*aSuccess = false;
return true;
}
NS_ASSERTION(mObject->_class != GetClass(), "Bad object type!");
NS_ASSERTION(mType == LocalObject, "Bad type!");
if (!(mObject->_class && mObject->_class->invoke)) {
*aResult = void_t();
*aSuccess = false;
return true;
}
AutoTArray<NPVariant, 10> convertedArgs;
uint32_t argCount = aArgs.Length();
if (!convertedArgs.SetLength(argCount, mozilla::fallible)) {
*aResult = void_t();
*aSuccess = false;
return true;
}
for (uint32_t index = 0; index < argCount; index++) {
ConvertToVariant(aArgs[index], convertedArgs[index]);
}
NPVariant result;
VOID_TO_NPVARIANT(result);
StackIdentifier id(aId);
bool success = mObject->_class->invoke(mObject, id.ToNPIdentifier(),
convertedArgs.Elements(), argCount,
&result);
for (uint32_t index = 0; index < argCount; index++) {
PluginModuleChild::sBrowserFuncs.releasevariantvalue(&convertedArgs[index]);
}
if (!success) {
*aResult = void_t();
*aSuccess = false;
return true;
}
Variant convertedResult;
success = ConvertToRemoteVariant(result, convertedResult, GetInstance(),
false);
DeferNPVariantLastRelease(&PluginModuleChild::sBrowserFuncs, &result);
if (!success) {
*aResult = void_t();
*aSuccess = false;
return true;
}
*aSuccess = true;
*aResult = convertedResult;
return true;
}
bool
PluginScriptableObjectChild::AnswerInvokeDefault(InfallibleTArray<Variant>&& aArgs,
Variant* aResult,
bool* aSuccess)
{
AssertPluginThread();
PluginInstanceChild::AutoStackHelper guard(mInstance);
if (mInvalidated) {
NS_WARNING("Calling AnswerInvokeDefault with an invalidated object!");
*aResult = void_t();
*aSuccess = false;
return true;
}
NS_ASSERTION(mObject->_class != GetClass(), "Bad object type!");
NS_ASSERTION(mType == LocalObject, "Bad type!");
if (!(mObject->_class && mObject->_class->invokeDefault)) {
*aResult = void_t();
*aSuccess = false;
return true;
}
AutoTArray<NPVariant, 10> convertedArgs;
uint32_t argCount = aArgs.Length();
if (!convertedArgs.SetLength(argCount, mozilla::fallible)) {
*aResult = void_t();
*aSuccess = false;
return true;
}
for (uint32_t index = 0; index < argCount; index++) {
ConvertToVariant(aArgs[index], convertedArgs[index]);
}
NPVariant result;
VOID_TO_NPVARIANT(result);
bool success = mObject->_class->invokeDefault(mObject,
convertedArgs.Elements(),
argCount, &result);
for (uint32_t index = 0; index < argCount; index++) {
PluginModuleChild::sBrowserFuncs.releasevariantvalue(&convertedArgs[index]);
}
if (!success) {
*aResult = void_t();
*aSuccess = false;
return true;
}
Variant convertedResult;
success = ConvertToRemoteVariant(result, convertedResult, GetInstance(),
false);
DeferNPVariantLastRelease(&PluginModuleChild::sBrowserFuncs, &result);
if (!success) {
*aResult = void_t();
*aSuccess = false;
return true;
}
*aResult = convertedResult;
*aSuccess = true;
return true;
}
bool
PluginScriptableObjectChild::AnswerHasProperty(const PluginIdentifier& aId,
bool* aHasProperty)
{
AssertPluginThread();
PluginInstanceChild::AutoStackHelper guard(mInstance);
if (mInvalidated) {
NS_WARNING("Calling AnswerHasProperty with an invalidated object!");
*aHasProperty = false;
return true;
}
NS_ASSERTION(mObject->_class != GetClass(), "Bad object type!");
NS_ASSERTION(mType == LocalObject, "Bad type!");
if (!(mObject->_class && mObject->_class->hasProperty)) {
*aHasProperty = false;
return true;
}
StackIdentifier id(aId);
*aHasProperty = mObject->_class->hasProperty(mObject, id.ToNPIdentifier());
return true;
}
bool
PluginScriptableObjectChild::AnswerGetChildProperty(const PluginIdentifier& aId,
bool* aHasProperty,
bool* aHasMethod,
Variant* aResult,
bool* aSuccess)
{
AssertPluginThread();
PluginInstanceChild::AutoStackHelper guard(mInstance);
*aHasProperty = *aHasMethod = *aSuccess = false;
*aResult = void_t();
if (mInvalidated) {
NS_WARNING("Calling AnswerGetProperty with an invalidated object!");
return true;
}
NS_ASSERTION(mObject->_class != GetClass(), "Bad object type!");
NS_ASSERTION(mType == LocalObject, "Bad type!");
if (!(mObject->_class && mObject->_class->hasProperty &&
mObject->_class->hasMethod && mObject->_class->getProperty)) {
return true;
}
StackIdentifier stackID(aId);
NPIdentifier id = stackID.ToNPIdentifier();
*aHasProperty = mObject->_class->hasProperty(mObject, id);
*aHasMethod = mObject->_class->hasMethod(mObject, id);
if (*aHasProperty) {
NPVariant result;
VOID_TO_NPVARIANT(result);
if (!mObject->_class->getProperty(mObject, id, &result)) {
return true;
}
Variant converted;
if ((*aSuccess = ConvertToRemoteVariant(result, converted, GetInstance(),
false))) {
DeferNPVariantLastRelease(&PluginModuleChild::sBrowserFuncs, &result);
*aResult = converted;
}
}
return true;
}
bool
PluginScriptableObjectChild::AnswerSetProperty(const PluginIdentifier& aId,
const Variant& aValue,
bool* aSuccess)
{
AssertPluginThread();
PluginInstanceChild::AutoStackHelper guard(mInstance);
if (mInvalidated) {
NS_WARNING("Calling AnswerSetProperty with an invalidated object!");
*aSuccess = false;
return true;
}
NS_ASSERTION(mObject->_class != GetClass(), "Bad object type!");
NS_ASSERTION(mType == LocalObject, "Bad type!");
if (!(mObject->_class && mObject->_class->hasProperty &&
mObject->_class->setProperty)) {
*aSuccess = false;
return true;
}
StackIdentifier stackID(aId);
NPIdentifier id = stackID.ToNPIdentifier();
if (!mObject->_class->hasProperty(mObject, id)) {
*aSuccess = false;
return true;
}
NPVariant converted;
ConvertToVariant(aValue, converted);
if ((*aSuccess = mObject->_class->setProperty(mObject, id, &converted))) {
PluginModuleChild::sBrowserFuncs.releasevariantvalue(&converted);
}
return true;
}
bool
PluginScriptableObjectChild::AnswerRemoveProperty(const PluginIdentifier& aId,
bool* aSuccess)
{
AssertPluginThread();
PluginInstanceChild::AutoStackHelper guard(mInstance);
if (mInvalidated) {
NS_WARNING("Calling AnswerRemoveProperty with an invalidated object!");
*aSuccess = false;
return true;
}
NS_ASSERTION(mObject->_class != GetClass(), "Bad object type!");
NS_ASSERTION(mType == LocalObject, "Bad type!");
if (!(mObject->_class && mObject->_class->hasProperty &&
mObject->_class->removeProperty)) {
*aSuccess = false;
return true;
}
StackIdentifier stackID(aId);
NPIdentifier id = stackID.ToNPIdentifier();
*aSuccess = mObject->_class->hasProperty(mObject, id) ?
mObject->_class->removeProperty(mObject, id) :
true;
return true;
}
bool
PluginScriptableObjectChild::AnswerEnumerate(InfallibleTArray<PluginIdentifier>* aProperties,
bool* aSuccess)
{
AssertPluginThread();
PluginInstanceChild::AutoStackHelper guard(mInstance);
if (mInvalidated) {
NS_WARNING("Calling AnswerEnumerate with an invalidated object!");
*aSuccess = false;
return true;
}
NS_ASSERTION(mObject->_class != GetClass(), "Bad object type!");
NS_ASSERTION(mType == LocalObject, "Bad type!");
if (!(mObject->_class && mObject->_class->enumerate)) {
*aSuccess = false;
return true;
}
NPIdentifier* ids;
uint32_t idCount;
if (!mObject->_class->enumerate(mObject, &ids, &idCount)) {
*aSuccess = false;
return true;
}
aProperties->SetCapacity(idCount);
for (uint32_t index = 0; index < idCount; index++) {
aProperties->AppendElement(FromNPIdentifier(ids[index]));
}
PluginModuleChild::sBrowserFuncs.memfree(ids);
*aSuccess = true;
return true;
}
bool
PluginScriptableObjectChild::AnswerConstruct(InfallibleTArray<Variant>&& aArgs,
Variant* aResult,
bool* aSuccess)
{
AssertPluginThread();
PluginInstanceChild::AutoStackHelper guard(mInstance);
if (mInvalidated) {
NS_WARNING("Calling AnswerConstruct with an invalidated object!");
*aResult = void_t();
*aSuccess = false;
return true;
}
NS_ASSERTION(mObject->_class != GetClass(), "Bad object type!");
NS_ASSERTION(mType == LocalObject, "Bad type!");
if (!(mObject->_class && mObject->_class->construct)) {
*aResult = void_t();
*aSuccess = false;
return true;
}
AutoTArray<NPVariant, 10> convertedArgs;
uint32_t argCount = aArgs.Length();
if (!convertedArgs.SetLength(argCount, mozilla::fallible)) {
*aResult = void_t();
*aSuccess = false;
return true;
}
for (uint32_t index = 0; index < argCount; index++) {
ConvertToVariant(aArgs[index], convertedArgs[index]);
}
NPVariant result;
VOID_TO_NPVARIANT(result);
bool success = mObject->_class->construct(mObject, convertedArgs.Elements(),
argCount, &result);
for (uint32_t index = 0; index < argCount; index++) {
PluginModuleChild::sBrowserFuncs.releasevariantvalue(&convertedArgs[index]);
}
if (!success) {
*aResult = void_t();
*aSuccess = false;
return true;
}
Variant convertedResult;
success = ConvertToRemoteVariant(result, convertedResult, GetInstance(),
false);
DeferNPVariantLastRelease(&PluginModuleChild::sBrowserFuncs, &result);
if (!success) {
*aResult = void_t();
*aSuccess = false;
return true;
}
*aResult = convertedResult;
*aSuccess = true;
return true;
}
bool
PluginScriptableObjectChild::RecvProtect()
{
NS_ASSERTION(mObject->_class != GetClass(), "Bad object type!");
NS_ASSERTION(mType == LocalObject, "Bad type!");
Protect();
return true;
}
bool
PluginScriptableObjectChild::RecvUnprotect()
{
NS_ASSERTION(mObject->_class != GetClass(), "Bad object type!");
NS_ASSERTION(mType == LocalObject, "Bad type!");
Unprotect();
return true;
}
bool
PluginScriptableObjectChild::Evaluate(NPString* aScript,
NPVariant* aResult)
{
PluginInstanceChild::AutoStackHelper guard(mInstance);
nsDependentCString script("");
if (aScript->UTF8Characters && aScript->UTF8Length) {
script.Rebind(aScript->UTF8Characters, aScript->UTF8Length);
}
bool success;
Variant result;
CallNPN_Evaluate(script, &result, &success);
if (!success) {
return false;
}
ConvertToVariant(result, *aResult);
return true;
}
nsTHashtable<PluginScriptableObjectChild::NPObjectData>* PluginScriptableObjectChild::sObjectMap;
bool
PluginScriptableObjectChild::RegisterActor(NPObject* aObject)
{
AssertPluginThread();
MOZ_ASSERT(aObject, "Null pointer!");
NPObjectData* d = sObjectMap->GetEntry(aObject);
if (!d) {
NS_ERROR("NPObject not in object table");
return false;
}
d->actor = this;
return true;
}
void
PluginScriptableObjectChild::UnregisterActor(NPObject* aObject)
{
AssertPluginThread();
MOZ_ASSERT(aObject, "Null pointer!");
NPObjectData* d = sObjectMap->GetEntry(aObject);
MOZ_ASSERT(d, "NPObject not in object table");
if (d) {
d->actor = nullptr;
}
}
/* static */ PluginScriptableObjectChild*
PluginScriptableObjectChild::GetActorForNPObject(NPObject* aObject)
{
AssertPluginThread();
MOZ_ASSERT(aObject, "Null pointer!");
NPObjectData* d = sObjectMap->GetEntry(aObject);
if (!d) {
NS_ERROR("Plugin using object not created with NPN_CreateObject?");
return nullptr;
}
return d->actor;
}
/* static */ void
PluginScriptableObjectChild::RegisterObject(NPObject* aObject, PluginInstanceChild* aInstance)
{
AssertPluginThread();
if (!sObjectMap) {
sObjectMap = new nsTHashtable<PluginScriptableObjectChild::NPObjectData>();
}
NPObjectData* d = sObjectMap->PutEntry(aObject);
MOZ_ASSERT(!d->instance, "New NPObject already mapped?");
d->instance = aInstance;
}
/* static */ void
PluginScriptableObjectChild::UnregisterObject(NPObject* aObject)
{
AssertPluginThread();
sObjectMap->RemoveEntry(aObject);
if (!sObjectMap->Count()) {
delete sObjectMap;
sObjectMap = nullptr;
}
}
/* static */ PluginInstanceChild*
PluginScriptableObjectChild::GetInstanceForNPObject(NPObject* aObject)
{
AssertPluginThread();
NPObjectData* d = sObjectMap->GetEntry(aObject);
if (!d) {
return nullptr;
}
return d->instance;
}
/* static */ void
PluginScriptableObjectChild::NotifyOfInstanceShutdown(PluginInstanceChild* aInstance)
{
AssertPluginThread();
if (!sObjectMap) {
return;
}
for (auto iter = sObjectMap->Iter(); !iter.Done(); iter.Next()) {
NPObjectData* d = iter.Get();
if (d->instance == aInstance) {
NPObject* o = d->GetKey();
aInstance->mDeletingHash->PutEntry(o);
}
}
}