Files
UXP-Jihad/dom/plugins/base/nsNPAPIPluginInstance.cpp
athenian200 b787af5702 Issue #1638 - Part 1: Put NPAPI code behind MOZ_ENABLE_NPAPI.
Adds --disable-npapi as an additional build option. The default is still to build in NPAPI compatibility, but with this flag, it's finally possible to build without pulling in GTK2 on systems that don't have the package.

This essentially ifdefs everything associated with NPAPI, following various Mozilla removal bugs.

Ref: BZ 1682030, BZ 1251202, BZ 1217665, BZ 1687239
2024-01-10 04:03:35 -06:00

1664 lines
40 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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 "mozilla/DebugOnly.h"
#include "mozilla/Logging.h"
#include "prmem.h"
#include "nscore.h"
#include "prenv.h"
#include "nsNPAPIPluginInstance.h"
#ifdef MOZ_ENABLE_NPAPI
#include "nsNPAPIPlugin.h"
#include "nsNPAPIPluginStreamListener.h"
#endif
#include "nsPluginHost.h"
#include "nsPluginLogging.h"
#include "nsContentUtils.h"
#ifdef MOZ_ENABLE_NPAPI
#include "nsPluginInstanceOwner.h"
#endif
#include "nsThreadUtils.h"
#include "nsIDOMElement.h"
#include "nsIDocument.h"
#include "nsIDocShell.h"
#include "nsIScriptGlobalObject.h"
#include "nsIScriptContext.h"
#include "nsDirectoryServiceDefs.h"
#ifdef MOZ_ENABLE_NPAPI
#include "nsJSNPRuntime.h"
#include "nsPluginStreamListenerPeer.h"
#endif
#include "nsSize.h"
#include "nsNetCID.h"
#include "nsIContent.h"
#include "nsVersionComparator.h"
#include "mozilla/Preferences.h"
#include "mozilla/Unused.h"
#include "nsILoadContext.h"
#include "mozilla/dom/HTMLObjectElementBinding.h"
#include "AudioChannelService.h"
#include "GeckoProfiler.h"
using namespace mozilla;
using namespace mozilla::dom;
using namespace mozilla;
#ifdef MOZ_ENABLE_NPAPI
using namespace mozilla::plugins::parent;
#endif
using namespace mozilla::layers;
#ifdef MOZ_ENABLE_NPAPI
static NS_DEFINE_IID(kIOutputStreamIID, NS_IOUTPUTSTREAM_IID);
#endif
NS_IMPL_ISUPPORTS(nsNPAPIPluginInstance, nsIAudioChannelAgentCallback)
nsNPAPIPluginInstance::nsNPAPIPluginInstance()
: mDrawingModel(kDefaultDrawingModel)
, mRunning(NOT_STARTED)
, mWindowless(false)
, mTransparent(false)
, mCached(false)
, mUsesDOMForCursor(false)
, mInPluginInitCall(false)
#ifdef MOZ_ENABLE_NPAPI
, mPlugin(nullptr)
#endif
, mMIMEType(nullptr)
#ifdef MOZ_ENABLE_NPAPI
, mOwner(nullptr)
#endif
#ifdef XP_MACOSX
, mCurrentPluginEvent(nullptr)
#endif
, mHaveJavaC2PJSObjectQuirk(false)
, mCachedParamLength(0)
, mCachedParamNames(nullptr)
, mCachedParamValues(nullptr)
, mMuted(false)
{
mNPP.pdata = nullptr;
mNPP.ndata = this;
PLUGIN_LOG(PLUGIN_LOG_BASIC, ("nsNPAPIPluginInstance ctor: this=%p\n",this));
}
nsNPAPIPluginInstance::~nsNPAPIPluginInstance()
{
PLUGIN_LOG(PLUGIN_LOG_BASIC, ("nsNPAPIPluginInstance dtor: this=%p\n",this));
if (mMIMEType) {
PR_Free((void *)mMIMEType);
mMIMEType = nullptr;
}
if (!mCachedParamValues || !mCachedParamNames) {
return;
}
MOZ_ASSERT(mCachedParamValues && mCachedParamNames);
for (uint32_t i = 0; i < mCachedParamLength; i++) {
if (mCachedParamNames[i]) {
free(mCachedParamNames[i]);
mCachedParamNames[i] = nullptr;
}
if (mCachedParamValues[i]) {
free(mCachedParamValues[i]);
mCachedParamValues[i] = nullptr;
}
}
free(mCachedParamNames);
mCachedParamNames = nullptr;
free(mCachedParamValues);
mCachedParamValues = nullptr;
}
uint32_t nsNPAPIPluginInstance::gInUnsafePluginCalls = 0;
void
nsNPAPIPluginInstance::Destroy()
{
Stop();
#ifdef MOZ_ENABLE_NPAPI
mPlugin = nullptr;
#endif
mAudioChannelAgent = nullptr;
}
TimeStamp
nsNPAPIPluginInstance::StopTime()
{
return mStopTime;
}
#ifdef MOZ_ENABLE_NPAPI
nsresult nsNPAPIPluginInstance::Initialize(nsNPAPIPlugin *aPlugin, nsPluginInstanceOwner* aOwner, const nsACString& aMIMEType)
{
PROFILER_LABEL_FUNC(js::ProfileEntry::Category::OTHER);
PLUGIN_LOG(PLUGIN_LOG_NORMAL, ("nsNPAPIPluginInstance::Initialize this=%p\n",this));
NS_ENSURE_ARG_POINTER(aPlugin);
NS_ENSURE_ARG_POINTER(aOwner);
mPlugin = aPlugin;
mOwner = aOwner;
if (!aMIMEType.IsEmpty()) {
mMIMEType = ToNewCString(aMIMEType);
}
return Start();
}
#endif
nsresult nsNPAPIPluginInstance::Stop()
{
#ifdef MOZ_ENABLE_NPAPI
PLUGIN_LOG(PLUGIN_LOG_NORMAL, ("nsNPAPIPluginInstance::Stop this=%p\n",this));
// Make sure the plugin didn't leave popups enabled.
if (mPopupStates.Length() > 0) {
nsCOMPtr<nsPIDOMWindowOuter> window = GetDOMWindow();
if (window) {
window->PopPopupControlState(openAbused);
}
}
if (RUNNING != mRunning) {
return NS_OK;
}
// clean up all outstanding timers
for (uint32_t i = mTimers.Length(); i > 0; i--)
UnscheduleTimer(mTimers[i - 1]->id);
// If there's code from this plugin instance on the stack, delay the
// destroy.
if (PluginDestructionGuard::DelayDestroy(this)) {
return NS_OK;
}
// Make sure we lock while we're writing to mRunning after we've
// started as other threads might be checking that inside a lock.
{
AsyncCallbackAutoLock lock;
mRunning = DESTROYING;
mStopTime = TimeStamp::Now();
}
OnPluginDestroy(&mNPP);
// clean up open streams
while (mStreamListeners.Length() > 0) {
RefPtr<nsNPAPIPluginStreamListener> currentListener(mStreamListeners[0]);
currentListener->CleanUpStream(NPRES_USER_BREAK);
mStreamListeners.RemoveElement(currentListener);
}
if (!mPlugin || !mPlugin->GetLibrary())
#endif
return NS_ERROR_FAILURE;
#ifdef MOZ_ENABLE_NPAPI
NPPluginFuncs* pluginFunctions = mPlugin->PluginFuncs();
NPError error = NPERR_GENERIC_ERROR;
if (pluginFunctions->destroy) {
NPSavedData *sdata = 0;
NS_TRY_SAFE_CALL_RETURN(error, (*pluginFunctions->destroy)(&mNPP, &sdata), this,
NS_PLUGIN_CALL_UNSAFE_TO_REENTER_GECKO);
NPP_PLUGIN_LOG(PLUGIN_LOG_NORMAL,
("NPP Destroy called: this=%p, npp=%p, return=%d\n", this, &mNPP, error));
}
mRunning = DESTROYED;
nsJSNPRuntime::OnPluginDestroy(&mNPP);
if (error != NPERR_NO_ERROR)
return NS_ERROR_FAILURE;
else
return NS_OK;
#endif
}
already_AddRefed<nsPIDOMWindowOuter>
nsNPAPIPluginInstance::GetDOMWindow()
{
#ifdef MOZ_ENABLE_NPAPI
if (!mOwner)
return nullptr;
RefPtr<nsPluginInstanceOwner> kungFuDeathGrip(mOwner);
nsCOMPtr<nsIDocument> doc;
kungFuDeathGrip->GetDocument(getter_AddRefs(doc));
if (!doc)
#endif
return nullptr;
#ifdef MOZ_ENABLE_NPAPI
RefPtr<nsPIDOMWindowOuter> window = doc->GetWindow();
return window.forget();
#endif
}
#ifdef MOZ_ENABLE_NPAPI
nsresult
nsNPAPIPluginInstance::GetTagType(nsPluginTagType *result)
{
if (!mOwner) {
return NS_ERROR_FAILURE;
}
return mOwner->GetTagType(result);
}
nsresult
nsNPAPIPluginInstance::GetMode(int32_t *result)
{
if (mOwner)
return mOwner->GetMode(result);
else
return NS_ERROR_FAILURE;
}
#endif
#ifdef MOZ_ENABLE_NPAPI
nsTArray<nsNPAPIPluginStreamListener*>*
nsNPAPIPluginInstance::StreamListeners()
{
return &mStreamListeners;
}
nsTArray<nsPluginStreamListenerPeer*>*
nsNPAPIPluginInstance::FileCachedStreamListeners()
{
return &mFileCachedStreamListeners;
}
#endif
nsresult
nsNPAPIPluginInstance::Start()
{
#ifdef MOZ_ENABLE_NPAPI
if (mRunning == RUNNING) {
return NS_OK;
}
if (!mOwner) {
MOZ_ASSERT(false, "Should not be calling Start() on unowned plugin.");
#endif
return NS_ERROR_FAILURE;
#ifdef MOZ_ENABLE_NPAPI
}
PluginDestructionGuard guard(this);
nsTArray<MozPluginParameter> attributes;
nsTArray<MozPluginParameter> params;
nsPluginTagType tagtype;
nsresult rv = GetTagType(&tagtype);
if (NS_SUCCEEDED(rv)) {
mOwner->GetAttributes(attributes);
mOwner->GetParameters(params);
} else {
MOZ_ASSERT(false, "Failed to get tag type.");
}
mCachedParamLength = attributes.Length() + 1 + params.Length();
// We add an extra entry "PARAM" as a separator between the attribute
// and param values, but we don't count it if there are no <param> entries.
// Legacy behavior quirk.
uint32_t quirkParamLength = params.Length() ?
mCachedParamLength : attributes.Length();
mCachedParamNames = (char**)moz_xmalloc(sizeof(char*) * mCachedParamLength);
mCachedParamValues = (char**)moz_xmalloc(sizeof(char*) * mCachedParamLength);
for (uint32_t i = 0; i < attributes.Length(); i++) {
mCachedParamNames[i] = ToNewUTF8String(attributes[i].mName);
mCachedParamValues[i] = ToNewUTF8String(attributes[i].mValue);
}
mCachedParamNames[attributes.Length()] = ToNewUTF8String(NS_LITERAL_STRING("PARAM"));
mCachedParamValues[attributes.Length()] = nullptr;
for (uint32_t i = 0, pos = attributes.Length() + 1; i < params.Length(); i ++) {
mCachedParamNames[pos] = ToNewUTF8String(params[i].mName);
mCachedParamValues[pos] = ToNewUTF8String(params[i].mValue);
pos++;
}
int32_t mode;
const char* mimetype;
NPError error = NPERR_GENERIC_ERROR;
GetMode(&mode);
GetMIMEType(&mimetype);
CheckJavaC2PJSObjectQuirk(quirkParamLength, mCachedParamNames, mCachedParamValues);
bool oldVal = mInPluginInitCall;
mInPluginInitCall = true;
// Need this on the stack before calling NPP_New otherwise some callbacks that
// the plugin may make could fail (NPN_HasProperty, for example).
NPPAutoPusher autopush(&mNPP);
if (!mPlugin)
return NS_ERROR_FAILURE;
PluginLibrary* library = mPlugin->GetLibrary();
if (!library)
return NS_ERROR_FAILURE;
// Mark this instance as running before calling NPP_New because the plugin may
// call other NPAPI functions, like NPN_GetURLNotify, that assume this is set
// before returning. If the plugin returns failure, we'll clear it out below.
mRunning = RUNNING;
nsresult newResult = library->NPP_New((char*)mimetype, &mNPP, (uint16_t)mode,
quirkParamLength, mCachedParamNames,
mCachedParamValues, nullptr, &error);
mInPluginInitCall = oldVal;
NPP_PLUGIN_LOG(PLUGIN_LOG_NORMAL,
("NPP New called: this=%p, npp=%p, mime=%s, mode=%d, argc=%d, return=%d\n",
this, &mNPP, mimetype, mode, quirkParamLength, error));
if (NS_FAILED(newResult) || error != NPERR_NO_ERROR) {
mRunning = DESTROYED;
nsJSNPRuntime::OnPluginDestroy(&mNPP);
return NS_ERROR_FAILURE;
}
return newResult;
#endif // MOZ_ENABLE_NPAPI
}
nsresult nsNPAPIPluginInstance::SetWindow(NPWindow* window)
{
#ifdef MOZ_ENABLE_NPAPI
// NPAPI plugins don't want a SetWindow(nullptr).
if (!window || RUNNING != mRunning)
return NS_OK;
#if MOZ_WIDGET_GTK
// bug 108347, flash plugin on linux doesn't like window->width <=
// 0, but Java needs wants this call.
if (window && window->type == NPWindowTypeWindow &&
(window->width <= 0 || window->height <= 0) &&
(nsPluginHost::GetSpecialType(nsDependentCString(mMIMEType)) !=
nsPluginHost::eSpecialType_Java)) {
return NS_OK;
}
#endif
if (!mPlugin || !mPlugin->GetLibrary())
return NS_ERROR_FAILURE;
NPPluginFuncs* pluginFunctions = mPlugin->PluginFuncs();
if (pluginFunctions->setwindow) {
PluginDestructionGuard guard(this);
// XXX Turns out that NPPluginWindow and NPWindow are structurally
// identical (on purpose!), so there's no need to make a copy.
PLUGIN_LOG(PLUGIN_LOG_NORMAL, ("nsNPAPIPluginInstance::SetWindow (about to call it) this=%p\n",this));
bool oldVal = mInPluginInitCall;
mInPluginInitCall = true;
NPPAutoPusher nppPusher(&mNPP);
NPError error;
NS_TRY_SAFE_CALL_RETURN(error, (*pluginFunctions->setwindow)(&mNPP, (NPWindow*)window), this,
NS_PLUGIN_CALL_UNSAFE_TO_REENTER_GECKO);
// 'error' is only used if this is a logging-enabled build.
// That is somewhat complex to check, so we just use "unused"
// to suppress any compiler warnings in build configurations
// where the logging is a no-op.
mozilla::Unused << error;
mInPluginInitCall = oldVal;
NPP_PLUGIN_LOG(PLUGIN_LOG_NORMAL,
("NPP SetWindow called: this=%p, [x=%d,y=%d,w=%d,h=%d], clip[t=%d,b=%d,l=%d,r=%d], return=%d\n",
this, window->x, window->y, window->width, window->height,
window->clipRect.top, window->clipRect.bottom, window->clipRect.left, window->clipRect.right, error));
}
return NS_OK;
#else
return NS_ERROR_FAILURE;
#endif // MOZ_ENABLE_NPAPI
}
nsresult
nsNPAPIPluginInstance::NewStreamFromPlugin(const char* type, const char* target,
nsIOutputStream* *result)
{
#ifdef MOZ_ENABLE_NPAPI
nsPluginStreamToFile* stream = new nsPluginStreamToFile(target, mOwner);
return stream->QueryInterface(kIOutputStreamIID, (void**)result);
#else
return NS_ERROR_FAILURE;
#endif
}
#ifdef MOZ_ENABLE_NPAPI
nsresult
nsNPAPIPluginInstance::NewStreamListener(const char* aURL, void* notifyData,
nsNPAPIPluginStreamListener** listener)
{
RefPtr<nsNPAPIPluginStreamListener> sl = new nsNPAPIPluginStreamListener(this, notifyData, aURL);
mStreamListeners.AppendElement(sl);
sl.forget(listener);
return NS_OK;
}
#endif
nsresult nsNPAPIPluginInstance::Print(NPPrint* platformPrint)
{
#ifdef MOZ_ENABLE_NPAPI
NS_ENSURE_TRUE(platformPrint, NS_ERROR_NULL_POINTER);
PluginDestructionGuard guard(this);
if (!mPlugin || !mPlugin->GetLibrary())
return NS_ERROR_FAILURE;
NPPluginFuncs* pluginFunctions = mPlugin->PluginFuncs();
NPPrint* thePrint = (NPPrint *)platformPrint;
// to be compatible with the older SDK versions and to match what
// NPAPI and other browsers do, overwrite |window.type| field with one
// more copy of |platformPrint|. See bug 113264
uint16_t sdkmajorversion = (pluginFunctions->version & 0xff00)>>8;
uint16_t sdkminorversion = pluginFunctions->version & 0x00ff;
if ((sdkmajorversion == 0) && (sdkminorversion < 11)) {
// Let's copy platformPrint bytes over to where it was supposed to be
// in older versions -- four bytes towards the beginning of the struct
// but we should be careful about possible misalignments
if (sizeof(NPWindowType) >= sizeof(void *)) {
void* source = thePrint->print.embedPrint.platformPrint;
void** destination = (void **)&(thePrint->print.embedPrint.window.type);
*destination = source;
} else {
NS_ERROR("Incompatible OS for assignment");
}
}
if (pluginFunctions->print)
NS_TRY_SAFE_CALL_VOID((*pluginFunctions->print)(&mNPP, thePrint), this,
NS_PLUGIN_CALL_UNSAFE_TO_REENTER_GECKO);
NPP_PLUGIN_LOG(PLUGIN_LOG_NORMAL,
("NPP PrintProc called: this=%p, pDC=%p, [x=%d,y=%d,w=%d,h=%d], clip[t=%d,b=%d,l=%d,r=%d]\n",
this,
platformPrint->print.embedPrint.platformPrint,
platformPrint->print.embedPrint.window.x,
platformPrint->print.embedPrint.window.y,
platformPrint->print.embedPrint.window.width,
platformPrint->print.embedPrint.window.height,
platformPrint->print.embedPrint.window.clipRect.top,
platformPrint->print.embedPrint.window.clipRect.bottom,
platformPrint->print.embedPrint.window.clipRect.left,
platformPrint->print.embedPrint.window.clipRect.right));
return NS_OK;
#else
return NS_ERROR_FAILURE;
#endif
}
nsresult nsNPAPIPluginInstance::HandleEvent(void* event, int16_t* result,
NSPluginCallReentry aSafeToReenterGecko)
{
#ifdef MOZ_ENABLE_NPAPI
if (RUNNING != mRunning)
return NS_OK;
PROFILER_LABEL_FUNC(js::ProfileEntry::Category::OTHER);
if (!event)
return NS_ERROR_FAILURE;
PluginDestructionGuard guard(this);
if (!mPlugin || !mPlugin->GetLibrary())
return NS_ERROR_FAILURE;
NPPluginFuncs* pluginFunctions = mPlugin->PluginFuncs();
int16_t tmpResult = kNPEventNotHandled;
if (pluginFunctions->event) {
#ifdef XP_MACOSX
mCurrentPluginEvent = event;
#endif
#if defined(XP_WIN)
NS_TRY_SAFE_CALL_RETURN(tmpResult, (*pluginFunctions->event)(&mNPP, event), this,
aSafeToReenterGecko);
#else
MAIN_THREAD_JNI_REF_GUARD;
tmpResult = (*pluginFunctions->event)(&mNPP, event);
#endif
NPP_PLUGIN_LOG(PLUGIN_LOG_NOISY,
("NPP HandleEvent called: this=%p, npp=%p, event=%p, return=%d\n",
this, &mNPP, event, tmpResult));
if (result)
*result = tmpResult;
#ifdef XP_MACOSX
mCurrentPluginEvent = nullptr;
#endif
}
return NS_OK;
#else
return NS_ERROR_FAILURE;
#endif // MOZ_ENABLE_NPAPI
}
nsresult nsNPAPIPluginInstance::GetValueFromPlugin(NPPVariable variable, void* value)
{
#ifdef MOZ_ENABLE_NPAPI
if (!mPlugin || !mPlugin->GetLibrary())
#endif
return NS_ERROR_FAILURE;
#ifdef MOZ_ENABLE_NPAPI
NPPluginFuncs* pluginFunctions = mPlugin->PluginFuncs();
nsresult rv = NS_ERROR_FAILURE;
if (pluginFunctions->getvalue && RUNNING == mRunning) {
PluginDestructionGuard guard(this);
NPError pluginError = NPERR_GENERIC_ERROR;
NS_TRY_SAFE_CALL_RETURN(pluginError, (*pluginFunctions->getvalue)(&mNPP, variable, value), this,
NS_PLUGIN_CALL_UNSAFE_TO_REENTER_GECKO);
NPP_PLUGIN_LOG(PLUGIN_LOG_NORMAL,
("NPP GetValue called: this=%p, npp=%p, var=%d, value=%d, return=%d\n",
this, &mNPP, variable, value, pluginError));
if (pluginError == NPERR_NO_ERROR) {
rv = NS_OK;
}
}
return rv;
#endif
}
#ifdef MOZ_ENABLE_NPAPI
nsNPAPIPlugin* nsNPAPIPluginInstance::GetPlugin()
{
return mPlugin;
}
#endif
nsresult nsNPAPIPluginInstance::GetNPP(NPP* aNPP)
{
if (aNPP)
*aNPP = &mNPP;
else
return NS_ERROR_NULL_POINTER;
return NS_OK;
}
NPError nsNPAPIPluginInstance::SetWindowless(bool aWindowless)
{
mWindowless = aWindowless;
if (mMIMEType) {
// bug 558434 - Prior to 3.6.4, we assumed windowless was transparent.
// Silverlight apparently relied on this quirk, so we default to
// transparent unless they specify otherwise after setting the windowless
// property. (Last tested version: sl 4.0).
// Changes to this code should be matched with changes in
// PluginInstanceChild::InitQuirksMode.
if (nsPluginHost::GetSpecialType(nsDependentCString(mMIMEType)) ==
nsPluginHost::eSpecialType_Silverlight) {
mTransparent = true;
}
}
return NPERR_NO_ERROR;
}
NPError nsNPAPIPluginInstance::SetTransparent(bool aTransparent)
{
mTransparent = aTransparent;
return NPERR_NO_ERROR;
}
NPError nsNPAPIPluginInstance::SetUsesDOMForCursor(bool aUsesDOMForCursor)
{
mUsesDOMForCursor = aUsesDOMForCursor;
return NPERR_NO_ERROR;
}
bool
nsNPAPIPluginInstance::UsesDOMForCursor()
{
return mUsesDOMForCursor;
}
void nsNPAPIPluginInstance::SetDrawingModel(NPDrawingModel aModel)
{
mDrawingModel = aModel;
}
void nsNPAPIPluginInstance::RedrawPlugin()
{
#ifdef MOZ_ENABLE_NPAPI
mOwner->RedrawPlugin();
#endif
}
#if defined(XP_MACOSX)
void nsNPAPIPluginInstance::SetEventModel(NPEventModel aModel)
{
#ifdef MOZ_ENABLE_NPAPI
// the event model needs to be set for the object frame immediately
if (!mOwner) {
NS_WARNING("Trying to set event model without a plugin instance owner!");
return;
}
mOwner->SetEventModel(aModel);
#endif // MOZ_ENABLE_NPAPI
}
#endif // XP_MACOSX
nsresult nsNPAPIPluginInstance::GetDrawingModel(int32_t* aModel)
{
*aModel = (int32_t)mDrawingModel;
return NS_OK;
}
nsresult nsNPAPIPluginInstance::IsRemoteDrawingCoreAnimation(bool* aDrawing)
{
#if defined(XP_MACOSX) && defined(MOZ_ENABLE_NPAPI)
if (!mPlugin)
return NS_ERROR_FAILURE;
PluginLibrary* library = mPlugin->GetLibrary();
if (!library)
return NS_ERROR_FAILURE;
return library->IsRemoteDrawingCoreAnimation(&mNPP, aDrawing);
#else
return NS_ERROR_FAILURE;
#endif
}
nsresult
nsNPAPIPluginInstance::ContentsScaleFactorChanged(double aContentsScaleFactor)
{
#if (defined(XP_MACOSX) || defined(XP_WIN)) && defined(MOZ_ENABLE_NPAPI)
if (!mPlugin)
return NS_ERROR_FAILURE;
PluginLibrary* library = mPlugin->GetLibrary();
if (!library)
return NS_ERROR_FAILURE;
// We only need to call this if the plugin is running OOP.
if (!library->IsOOP())
return NS_OK;
return library->ContentsScaleFactorChanged(&mNPP, aContentsScaleFactor);
#else
return NS_ERROR_FAILURE;
#endif
}
nsresult
nsNPAPIPluginInstance::CSSZoomFactorChanged(float aCSSZoomFactor)
{
#ifdef MOZ_ENABLE_NPAPI
if (RUNNING != mRunning)
return NS_OK;
PLUGIN_LOG(PLUGIN_LOG_NORMAL, ("nsNPAPIPluginInstance informing plugin of CSS Zoom Factor change this=%p\n",this));
if (!mPlugin || !mPlugin->GetLibrary())
#endif
return NS_ERROR_FAILURE;
#ifdef MOZ_ENABLE_NPAPI
NPPluginFuncs* pluginFunctions = mPlugin->PluginFuncs();
if (!pluginFunctions->setvalue)
return NS_ERROR_FAILURE;
PluginDestructionGuard guard(this);
NPError error;
double value = static_cast<double>(aCSSZoomFactor);
NS_TRY_SAFE_CALL_RETURN(error, (*pluginFunctions->setvalue)(&mNPP, NPNVCSSZoomFactor, &value), this,
NS_PLUGIN_CALL_UNSAFE_TO_REENTER_GECKO);
return (error == NPERR_NO_ERROR) ? NS_OK : NS_ERROR_FAILURE;
#endif
}
nsresult
nsNPAPIPluginInstance::GetJSObject(JSContext *cx, JSObject** outObject)
{
#ifdef MOZ_ENABLE_NPAPI
if (mHaveJavaC2PJSObjectQuirk) {
#endif
return NS_ERROR_FAILURE;
#ifdef MOZ_ENABLE_NPAPI
}
NPObject *npobj = nullptr;
nsresult rv = GetValueFromPlugin(NPPVpluginScriptableNPObject, &npobj);
if (NS_FAILED(rv) || !npobj)
return NS_ERROR_FAILURE;
*outObject = nsNPObjWrapper::GetNewOrUsed(&mNPP, cx, npobj);
_releaseobject(npobj);
return NS_OK;
#endif
}
void
nsNPAPIPluginInstance::SetCached(bool aCache)
{
mCached = aCache;
}
bool
nsNPAPIPluginInstance::ShouldCache()
{
return mCached;
}
nsresult
nsNPAPIPluginInstance::IsWindowless(bool* isWindowless)
{
#ifdef XP_MACOSX
// All OS X plugins are windowless.
*isWindowless = true;
#else
*isWindowless = mWindowless;
#endif
return NS_OK;
}
#ifdef MOZ_ENABLE_NPAPI
class MOZ_STACK_CLASS AutoPluginLibraryCall
{
public:
explicit AutoPluginLibraryCall(nsNPAPIPluginInstance* aThis)
: mThis(aThis), mGuard(aThis), mLibrary(nullptr)
{
nsNPAPIPlugin* plugin = mThis->GetPlugin();
if (plugin)
mLibrary = plugin->GetLibrary();
}
explicit operator bool() { return !!mLibrary; }
PluginLibrary* operator->() { return mLibrary; }
private:
nsNPAPIPluginInstance* mThis;
PluginDestructionGuard mGuard;
PluginLibrary* mLibrary;
};
#endif
nsresult
nsNPAPIPluginInstance::AsyncSetWindow(NPWindow* window)
{
#ifdef MOZ_ENABLE_NPAPI
if (RUNNING != mRunning)
return NS_OK;
AutoPluginLibraryCall library(this);
if (!library)
#endif
return NS_ERROR_FAILURE;
#ifdef MOZ_ENABLE_NPAPI
return library->AsyncSetWindow(&mNPP, window);
#endif
}
nsresult
nsNPAPIPluginInstance::GetImageContainer(ImageContainer**aContainer)
{
#ifdef MOZ_ENABLE_NPAPI
*aContainer = nullptr;
if (RUNNING != mRunning)
return NS_OK;
AutoPluginLibraryCall library(this);
return !library ? NS_ERROR_FAILURE : library->GetImageContainer(&mNPP, aContainer);
#else
return NS_ERROR_FAILURE;
#endif
}
nsresult
nsNPAPIPluginInstance::GetImageSize(nsIntSize* aSize)
{
#ifdef MOZ_ENABLE_NPAPI
*aSize = nsIntSize(0, 0);
if (RUNNING != mRunning)
return NS_OK;
AutoPluginLibraryCall library(this);
return !library ? NS_ERROR_FAILURE : library->GetImageSize(&mNPP, aSize);
#else
return NS_ERROR_FAILURE;
#endif
}
#if defined(XP_WIN)
nsresult
nsNPAPIPluginInstance::GetScrollCaptureContainer(ImageContainer**aContainer)
{
#ifdef MOZ_ENABLE_NPAPI
*aContainer = nullptr;
if (RUNNING != mRunning)
return NS_OK;
AutoPluginLibraryCall library(this);
return !library ? NS_ERROR_FAILURE : library->GetScrollCaptureContainer(&mNPP, aContainer);
#else
return NS_ERROR_FAILURE;
#endif // MOZ_ENABLE_NPAPI
}
#endif
nsresult
nsNPAPIPluginInstance::HandledWindowedPluginKeyEvent(
const NativeEventData& aKeyEventData,
bool aIsConsumed)
{
#ifdef MOZ_ENABLE_NPAPI
if (NS_WARN_IF(!mPlugin)) {
return NS_ERROR_FAILURE;
}
PluginLibrary* library = mPlugin->GetLibrary();
if (NS_WARN_IF(!library)) {
return NS_ERROR_FAILURE;
}
return library->HandledWindowedPluginKeyEvent(&mNPP, aKeyEventData,
aIsConsumed);
#else
return NS_ERROR_FAILURE;
#endif
}
void
nsNPAPIPluginInstance::DidComposite()
{
#ifdef MOZ_ENABLE_NPAPI
if (RUNNING != mRunning)
return;
AutoPluginLibraryCall library(this);
library->DidComposite(&mNPP);
#endif
}
nsresult
nsNPAPIPluginInstance::NotifyPainted(void)
{
NS_NOTREACHED("Dead code, shouldn't be called.");
return NS_ERROR_NOT_IMPLEMENTED;
}
nsresult
nsNPAPIPluginInstance::GetIsOOP(bool* aIsAsync)
{
#ifdef MOZ_ENABLE_NPAPI
AutoPluginLibraryCall library(this);
if (!library)
#endif
return NS_ERROR_FAILURE;
#ifdef MOZ_ENABLE_NPAPI
*aIsAsync = library->IsOOP();
return NS_OK;
#endif
}
nsresult
nsNPAPIPluginInstance::SetBackgroundUnknown()
{
#ifdef MOZ_ENABLE_NPAPI
if (RUNNING != mRunning)
return NS_OK;
AutoPluginLibraryCall library(this);
if (!library)
#endif
return NS_ERROR_FAILURE;
#ifdef MOZ_ENABLE_NPAPI
return library->SetBackgroundUnknown(&mNPP);
#endif
}
nsresult
nsNPAPIPluginInstance::BeginUpdateBackground(nsIntRect* aRect,
DrawTarget** aDrawTarget)
{
#ifdef MOZ_ENABLE_NPAPI
if (RUNNING != mRunning)
return NS_OK;
AutoPluginLibraryCall library(this);
if (!library)
#endif
return NS_ERROR_FAILURE;
#ifdef MOZ_ENABLE_NPAPI
return library->BeginUpdateBackground(&mNPP, *aRect, aDrawTarget);
#endif
}
nsresult
nsNPAPIPluginInstance::EndUpdateBackground(nsIntRect* aRect)
{
#ifdef MOZ_ENABLE_NPAPI
if (RUNNING != mRunning)
return NS_OK;
AutoPluginLibraryCall library(this);
if (!library)
#endif
return NS_ERROR_FAILURE;
#ifdef MOZ_ENABLE_NPAPI
return library->EndUpdateBackground(&mNPP, *aRect);
#endif
}
nsresult
nsNPAPIPluginInstance::IsTransparent(bool* isTransparent)
{
*isTransparent = mTransparent;
return NS_OK;
}
nsresult
nsNPAPIPluginInstance::GetFormValue(nsAString& aValue)
{
aValue.Truncate();
char *value = nullptr;
nsresult rv = GetValueFromPlugin(NPPVformValue, &value);
if (NS_FAILED(rv) || !value)
return NS_ERROR_FAILURE;
CopyUTF8toUTF16(value, aValue);
// NPPVformValue allocates with NPN_MemAlloc(), which uses
// nsMemory.
free(value);
return NS_OK;
}
nsresult
nsNPAPIPluginInstance::PushPopupsEnabledState(bool aEnabled)
{
nsCOMPtr<nsPIDOMWindowOuter> window = GetDOMWindow();
if (!window)
return NS_ERROR_FAILURE;
PopupControlState oldState =
window->PushPopupControlState(aEnabled ? openAllowed : openAbused,
true);
if (!mPopupStates.AppendElement(oldState)) {
// Appending to our state stack failed, pop what we just pushed.
window->PopPopupControlState(oldState);
return NS_ERROR_FAILURE;
}
return NS_OK;
}
nsresult
nsNPAPIPluginInstance::PopPopupsEnabledState()
{
int32_t last = mPopupStates.Length() - 1;
if (last < 0) {
// Nothing to pop.
return NS_OK;
}
nsCOMPtr<nsPIDOMWindowOuter> window = GetDOMWindow();
if (!window)
return NS_ERROR_FAILURE;
PopupControlState &oldState = mPopupStates[last];
window->PopPopupControlState(oldState);
mPopupStates.RemoveElementAt(last);
return NS_OK;
}
nsresult
nsNPAPIPluginInstance::GetPluginAPIVersion(uint16_t* version)
{
#ifdef MOZ_ENABLE_NPAPI
NS_ENSURE_ARG_POINTER(version);
if (!mPlugin)
#endif
return NS_ERROR_FAILURE;
#ifdef MOZ_ENABLE_NPAPI
if (!mPlugin->GetLibrary())
return NS_ERROR_FAILURE;
NPPluginFuncs* pluginFunctions = mPlugin->PluginFuncs();
*version = pluginFunctions->version;
return NS_OK;
#endif
}
nsresult
nsNPAPIPluginInstance::PrivateModeStateChanged(bool enabled)
{
#ifdef MOZ_ENABLE_NPAPI
if (RUNNING != mRunning)
return NS_OK;
PLUGIN_LOG(PLUGIN_LOG_NORMAL, ("nsNPAPIPluginInstance informing plugin of private mode state change this=%p\n",this));
if (!mPlugin || !mPlugin->GetLibrary())
#endif
return NS_ERROR_FAILURE;
#ifdef MOZ_ENABLE_NPAPI
NPPluginFuncs* pluginFunctions = mPlugin->PluginFuncs();
if (!pluginFunctions->setvalue)
return NS_ERROR_FAILURE;
PluginDestructionGuard guard(this);
NPError error;
NPBool value = static_cast<NPBool>(enabled);
NS_TRY_SAFE_CALL_RETURN(error, (*pluginFunctions->setvalue)(&mNPP, NPNVprivateModeBool, &value), this,
NS_PLUGIN_CALL_UNSAFE_TO_REENTER_GECKO);
return (error == NPERR_NO_ERROR) ? NS_OK : NS_ERROR_FAILURE;
#endif
}
nsresult
nsNPAPIPluginInstance::IsPrivateBrowsing(bool* aEnabled)
{
#ifdef MOZ_ENABLE_NPAPI
if (!mOwner)
#endif
return NS_ERROR_FAILURE;
#ifdef MOZ_ENABLE_NPAPI
nsCOMPtr<nsIDocument> doc;
mOwner->GetDocument(getter_AddRefs(doc));
NS_ENSURE_TRUE(doc, NS_ERROR_FAILURE);
nsCOMPtr<nsPIDOMWindowOuter> domwindow = doc->GetWindow();
NS_ENSURE_TRUE(domwindow, NS_ERROR_FAILURE);
nsCOMPtr<nsIDocShell> docShell = domwindow->GetDocShell();
nsCOMPtr<nsILoadContext> loadContext = do_QueryInterface(docShell);
*aEnabled = (loadContext && loadContext->UsePrivateBrowsing());
return NS_OK;
#endif
}
static void
PluginTimerCallback(nsITimer *aTimer, void *aClosure)
{
nsNPAPITimer* t = (nsNPAPITimer*)aClosure;
NPP npp = t->npp;
uint32_t id = t->id;
PLUGIN_LOG(PLUGIN_LOG_NOISY, ("nsNPAPIPluginInstance running plugin timer callback this=%p\n", npp->ndata));
MAIN_THREAD_JNI_REF_GUARD;
// Some plugins call unscheduletimer from this callback. Use a guard.
t->inCallback = true;
(*(t->callback))(npp, id);
t->inCallback = false;
// Make sure we still have an instance and the timer is still alive
// after the callback.
nsNPAPIPluginInstance *inst = (nsNPAPIPluginInstance*)npp->ndata;
if (!inst || !inst->TimerWithID(id, nullptr))
return;
// use UnscheduleTimer to clean up if this is a one-shot timer
uint32_t timerType;
t->timer->GetType(&timerType);
if (t->needUnschedule || timerType == nsITimer::TYPE_ONE_SHOT)
inst->UnscheduleTimer(id);
}
nsNPAPITimer*
nsNPAPIPluginInstance::TimerWithID(uint32_t id, uint32_t* index)
{
uint32_t len = mTimers.Length();
for (uint32_t i = 0; i < len; i++) {
if (mTimers[i]->id == id) {
if (index)
*index = i;
return mTimers[i];
}
}
return nullptr;
}
uint32_t
nsNPAPIPluginInstance::ScheduleTimer(uint32_t interval, NPBool repeat, void (*timerFunc)(NPP npp, uint32_t timerID))
{
if (RUNNING != mRunning)
return 0;
nsNPAPITimer *newTimer = new nsNPAPITimer();
newTimer->inCallback = newTimer->needUnschedule = false;
newTimer->npp = &mNPP;
// generate ID that is unique to this instance
uint32_t uniqueID = mTimers.Length();
while ((uniqueID == 0) || TimerWithID(uniqueID, nullptr))
uniqueID++;
newTimer->id = uniqueID;
// create new xpcom timer, scheduled correctly
nsresult rv;
nsCOMPtr<nsITimer> xpcomTimer = do_CreateInstance(NS_TIMER_CONTRACTID, &rv);
if (NS_FAILED(rv)) {
delete newTimer;
return 0;
}
const short timerType = (repeat ? (short)nsITimer::TYPE_REPEATING_SLACK : (short)nsITimer::TYPE_ONE_SHOT);
xpcomTimer->InitWithFuncCallback(PluginTimerCallback, newTimer, interval, timerType);
newTimer->timer = xpcomTimer;
// save callback function
newTimer->callback = timerFunc;
// add timer to timers array
mTimers.AppendElement(newTimer);
return newTimer->id;
}
void
nsNPAPIPluginInstance::UnscheduleTimer(uint32_t timerID)
{
// find the timer struct by ID
uint32_t index;
nsNPAPITimer* t = TimerWithID(timerID, &index);
if (!t)
return;
if (t->inCallback) {
t->needUnschedule = true;
return;
}
// cancel the timer
t->timer->Cancel();
// remove timer struct from array
mTimers.RemoveElementAt(index);
// delete timer
delete t;
}
NPBool
nsNPAPIPluginInstance::ConvertPoint(double sourceX, double sourceY, NPCoordinateSpace sourceSpace,
double *destX, double *destY, NPCoordinateSpace destSpace)
{
#ifdef MOZ_ENABLE_NPAPI
if (mOwner) {
return mOwner->ConvertPoint(sourceX, sourceY, sourceSpace, destX, destY, destSpace);
}
#endif
return false;
}
nsresult
nsNPAPIPluginInstance::GetDOMElement(nsIDOMElement* *result)
{
#ifdef MOZ_ENABLE_NPAPI
if (!mOwner) {
*result = nullptr;
#endif
return NS_ERROR_FAILURE;
#ifdef MOZ_ENABLE_NPAPI
}
return mOwner->GetDOMElement(result);
#endif
}
nsresult
nsNPAPIPluginInstance::InvalidateRect(NPRect *invalidRect)
{
#ifdef MOZ_ENABLE_NPAPI
if (RUNNING != mRunning)
return NS_OK;
if (!mOwner)
#endif
return NS_ERROR_FAILURE;
#ifdef MOZ_ENABLE_NPAPI
return mOwner->InvalidateRect(invalidRect);
#endif
}
nsresult
nsNPAPIPluginInstance::InvalidateRegion(NPRegion invalidRegion)
{
#ifdef MOZ_ENABLE_NPAPI
if (RUNNING != mRunning)
return NS_OK;
if (!mOwner)
#endif
return NS_ERROR_FAILURE;
#ifdef MOZ_ENABLE_NPAPI
return mOwner->InvalidateRegion(invalidRegion);
#endif
}
nsresult
nsNPAPIPluginInstance::GetMIMEType(const char* *result)
{
if (!mMIMEType)
*result = "";
else
*result = mMIMEType;
return NS_OK;
}
#ifdef MOZ_ENABLE_NPAPI
nsPluginInstanceOwner*
nsNPAPIPluginInstance::GetOwner()
{
return mOwner;
}
void
nsNPAPIPluginInstance::SetOwner(nsPluginInstanceOwner *aOwner)
{
mOwner = aOwner;
}
#endif
nsresult
nsNPAPIPluginInstance::AsyncSetWindow(NPWindow& window)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
void
nsNPAPIPluginInstance::URLRedirectResponse(void* notifyData, NPBool allow)
{
#ifdef MOZ_ENABLE_NPAPI
if (!notifyData) {
return;
}
uint32_t listenerCount = mStreamListeners.Length();
for (uint32_t i = 0; i < listenerCount; i++) {
nsNPAPIPluginStreamListener* currentListener = mStreamListeners[i];
if (currentListener->GetNotifyData() == notifyData) {
currentListener->URLRedirectResponse(allow);
}
}
#endif
}
NPError
nsNPAPIPluginInstance::InitAsyncSurface(NPSize *size, NPImageFormat format,
void *initData, NPAsyncSurface *surface)
{
#ifdef MOZ_ENABLE_NPAPI
if (mOwner) {
return mOwner->InitAsyncSurface(size, format, initData, surface);
}
#endif
return NPERR_GENERIC_ERROR;
}
NPError
nsNPAPIPluginInstance::FinalizeAsyncSurface(NPAsyncSurface *surface)
{
#ifdef MOZ_ENABLE_NPAPI
if (mOwner) {
return mOwner->FinalizeAsyncSurface(surface);
}
#endif
return NPERR_GENERIC_ERROR;
}
void
nsNPAPIPluginInstance::SetCurrentAsyncSurface(NPAsyncSurface *surface, NPRect *changed)
{
#ifdef MOZ_ENABLE_NPAPI
if (mOwner) {
mOwner->SetCurrentAsyncSurface(surface, changed);
}
#endif
}
class CarbonEventModelFailureEvent : public Runnable {
public:
nsCOMPtr<nsIContent> mContent;
explicit CarbonEventModelFailureEvent(nsIContent* aContent)
: mContent(aContent)
{}
~CarbonEventModelFailureEvent() {}
NS_IMETHOD Run();
};
NS_IMETHODIMP
CarbonEventModelFailureEvent::Run()
{
nsString type = NS_LITERAL_STRING("npapi-carbon-event-model-failure");
nsContentUtils::DispatchTrustedEvent(mContent->GetComposedDoc(), mContent,
type, true, true);
return NS_OK;
}
void
nsNPAPIPluginInstance::CarbonNPAPIFailure()
{
nsCOMPtr<nsIDOMElement> element;
GetDOMElement(getter_AddRefs(element));
if (!element) {
return;
}
nsCOMPtr<nsIContent> content(do_QueryInterface(element));
if (!content) {
return;
}
nsCOMPtr<nsIRunnable> e = new CarbonEventModelFailureEvent(content);
nsresult rv = NS_DispatchToCurrentThread(e);
if (NS_FAILED(rv)) {
NS_WARNING("Failed to dispatch CarbonEventModelFailureEvent.");
}
}
static bool
GetJavaVersionFromMimetype(nsPluginTag* pluginTag, nsCString& version)
{
for (uint32_t i = 0; i < pluginTag->MimeTypes().Length(); ++i) {
nsCString type = pluginTag->MimeTypes()[i];
nsAutoCString jpi("application/x-java-applet;jpi-version=");
int32_t idx = type.Find(jpi, false, 0, -1);
if (idx != 0) {
continue;
}
type.Cut(0, jpi.Length());
if (type.IsEmpty()) {
continue;
}
type.ReplaceChar('_', '.');
version = type;
return true;
}
return false;
}
#ifdef MOZ_ENABLE_NPAPI
void
nsNPAPIPluginInstance::CheckJavaC2PJSObjectQuirk(uint16_t paramCount,
const char* const* paramNames,
const char* const* paramValues)
{
if (!mMIMEType || !mPlugin) {
return;
}
nsPluginTagType tagtype;
nsresult rv = GetTagType(&tagtype);
if (NS_FAILED(rv) ||
(tagtype != nsPluginTagType_Applet)) {
return;
}
RefPtr<nsPluginHost> pluginHost = nsPluginHost::GetInst();
if (!pluginHost) {
return;
}
nsPluginTag* pluginTag = pluginHost->TagForPlugin(mPlugin);
if (!pluginTag ||
!pluginTag->mIsJavaPlugin) {
return;
}
// check the params for "code" being present and non-empty
bool haveCodeParam = false;
bool isCodeParamEmpty = true;
for (uint16_t i = paramCount; i > 0; --i) {
if (PL_strcasecmp(paramNames[i - 1], "code") == 0) {
haveCodeParam = true;
if (strlen(paramValues[i - 1]) > 0) {
isCodeParamEmpty = false;
}
break;
}
}
// Due to the Java version being specified inconsistently across platforms
// check the version via the mimetype for choosing specific Java versions
nsCString javaVersion;
if (!GetJavaVersionFromMimetype(pluginTag, javaVersion)) {
return;
}
mozilla::Version version(javaVersion.get());
if (version >= "1.7.0.4") {
return;
}
if (!haveCodeParam && version >= "1.6.0.34" && version < "1.7") {
return;
}
if (haveCodeParam && !isCodeParamEmpty) {
return;
}
mHaveJavaC2PJSObjectQuirk = true;
}
#endif
double
nsNPAPIPluginInstance::GetContentsScaleFactor()
{
#ifdef MOZ_ENABLE_NPAPI
double scaleFactor = 1.0;
if (mOwner) {
mOwner->GetContentsScaleFactor(&scaleFactor);
}
return scaleFactor;
#else
return -1.0;
#endif
}
float
nsNPAPIPluginInstance::GetCSSZoomFactor()
{
#ifdef MOZ_ENABLE_NPAPI
float zoomFactor = 1.0;
if (mOwner) {
mOwner->GetCSSZoomFactor(&zoomFactor);
}
return zoomFactor;
#else
return -1.0f;
#endif
}
nsresult
nsNPAPIPluginInstance::GetRunID(uint32_t* aRunID)
{
#ifdef MOZ_ENABLE_NPAPI
if (NS_WARN_IF(!aRunID)) {
return NS_ERROR_INVALID_POINTER;
}
if (NS_WARN_IF(!mPlugin)) {
return NS_ERROR_FAILURE;
}
PluginLibrary* library = mPlugin->GetLibrary();
if (!library) {
return NS_ERROR_FAILURE;
}
return library->GetRunID(aRunID);
#else
return NS_ERROR_FAILURE;
#endif
}
nsresult
nsNPAPIPluginInstance::GetOrCreateAudioChannelAgent(nsIAudioChannelAgent** aAgent)
{
if (!mAudioChannelAgent) {
nsresult rv;
mAudioChannelAgent = do_CreateInstance("@mozilla.org/audiochannelagent;1", &rv);
if (NS_WARN_IF(!mAudioChannelAgent)) {
return NS_ERROR_FAILURE;
}
nsCOMPtr<nsPIDOMWindowOuter> window = GetDOMWindow();
if (NS_WARN_IF(!window)) {
return NS_ERROR_FAILURE;
}
rv = mAudioChannelAgent->Init(window->GetCurrentInnerWindow(),
(int32_t)AudioChannelService::GetDefaultAudioChannel(),
this);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
}
nsCOMPtr<nsIAudioChannelAgent> agent = mAudioChannelAgent;
agent.forget(aAgent);
return NS_OK;
}
NS_IMETHODIMP
nsNPAPIPluginInstance::WindowVolumeChanged(float aVolume, bool aMuted)
{
// We just support mute/unmute
nsresult rv = SetMuted(aMuted);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "SetMuted failed");
if (mMuted != aMuted) {
mMuted = aMuted;
AudioChannelService::AudibleState audible = aMuted ?
AudioChannelService::AudibleState::eNotAudible :
AudioChannelService::AudibleState::eAudible;
mAudioChannelAgent->NotifyStartedAudible(audible,
AudioChannelService::AudibleChangedReasons::eVolumeChanged);
}
return rv;
}
NS_IMETHODIMP
nsNPAPIPluginInstance::WindowSuspendChanged(nsSuspendedTypes aSuspend)
{
// It doesn't support suspended, so we just do something like mute/unmute.
WindowVolumeChanged(1.0, /* useless */
aSuspend != nsISuspendedTypes::NONE_SUSPENDED);
return NS_OK;
}
NS_IMETHODIMP
nsNPAPIPluginInstance::WindowAudioCaptureChanged(bool aCapture)
{
return NS_OK;
}
nsresult
nsNPAPIPluginInstance::SetMuted(bool aIsMuted)
{
#ifdef MOZ_ENABLE_NPAPI
if (RUNNING != mRunning)
return NS_OK;
PLUGIN_LOG(PLUGIN_LOG_NORMAL, ("nsNPAPIPluginInstance informing plugin of mute state change this=%p\n",this));
if (!mPlugin || !mPlugin->GetLibrary())
return NS_ERROR_FAILURE;
NPPluginFuncs* pluginFunctions = mPlugin->PluginFuncs();
if (!pluginFunctions->setvalue)
return NS_ERROR_FAILURE;
PluginDestructionGuard guard(this);
NPError error;
NPBool value = static_cast<NPBool>(aIsMuted);
NS_TRY_SAFE_CALL_RETURN(error, (*pluginFunctions->setvalue)(&mNPP, NPNVmuteAudioBool, &value), this,
NS_PLUGIN_CALL_UNSAFE_TO_REENTER_GECKO);
return (error == NPERR_NO_ERROR) ? NS_OK : NS_ERROR_FAILURE;
#else
return NS_ERROR_FAILURE;
#endif
}