Files
palemoon27/dom/base/nsPluginArray.cpp
T
roytam1 919a7bbc39 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1241764: Replace nsPIDOMWindow with nsPIDOMWindowInner/Outer. r=mrbkap,smaug (ed606e368d)
- Bug 1212720 - Part 1: Implement Document.getAnimations(). r=heycam, r=smaug (8c3951b318)
- Bug 1212720 - Part 2: Tests for document.getAnimations. r=heycam (8e6660abe1)
- Bug 1212720 - Part 3: Remove AnimationTimeline.getAnimations. r=heycam, r=smaug (a663e7e79b)
- Bug 1212720 - Part 4: Remove all cancelAllAnimationsOnEnd. It is not needed any more. r=bbirtles (8c9b92f2d4)
- Bug 1218258 - Fix -Wimplicit-fallthrough warnings in toolkit/components/downloads. r=paolo (1962eee4fc)
- Bug 1218258 - Fix -Wshadow warnings in toolkit/components/downloads. r=paolo (6cf1d464bb)
- Bug 1244726 - Include mozIDOMWindow.h for nsDownloadManager. r=khuey (b01f8d6e44)
- Bug 1212323: P1. Use a 640x360 SPS to test for HW decoding support. r=cpearce (ed67910d9c)
- Bug 1212323: P2. Have IsVideoAccelerated return a Promise. r=cpearce (824e543945)
- Bug 1241764: Merge bug 1212323. r=me CLOSED TREE (e5ed1ad65d)
- Bug 1240365 - FileReader should use the global scope correctly in ChromeWorkers, r=smaug, r=khuey (bf40380947)
- Bug 1239210 - Don't process WebIDL files when not compiling; r=glandium (5a55f23b2e)
- Bug 1242210 - Mark RootedTypedArray as final; r=baku (e9c594fbc8)
- more palemoon getSelection revert (443f0d9a7d)
- Bug 1213818 - Align document.title for SVG documents with HTML spec; r=bz (fb60e8c048)
- Bug 1245182: Move a few event handling functions to nsPIDOMWindowInner. r=smaug (02aadac27e)
- Bug 1245124 - window.open() should open a new tab in the same container, r=gijs, r=smaug (808bdd8f9f)
- Bug 1168161 - Mark nsPIDOMWindow::mDocShell as MOZ_NON_OWNING_REF. r=ehsan (0b36dbbcb2)
- Bug 1234170 - WebSocket should check if the channel has been opened before send the 'close' notification to the WebSocketEventService, r=jduell (4bfd6f3f3f)
- Bug 1245261 - Use an atomic to safely access gcTriggerBytes; r=jonco (f9c80d47e1)
- Bug 1243001 part 1. Remove the dead WrappedWorkerRunnable class from Promise code. r=peterv (9f8c758723)
- Bug 1243001 part 2. Make Promise an empty [NoInterfaceObject] interface when SPIDERMONKEY_PROMISE is defined. r=peterv (6be034ee59)
- Bug 1243001 part 3. Turn off the IDL bits of PromiseDebugging when SPIDERMONKEY_PROMISE is defined. r=peterv (114241ddd6)
- Bug 1243001 part 4. Switch to using MaybeResolve/MaybeReject instead of ResolveInternal/RejectInternal for PromiseWorkerProxy. r=peterv (ca8faf02f8)
- Bug 1243114 - Convert PromiseCapability::mPromise to a rooted JSObject* instead of a rooted JS::Value. r=bz (e4c907e5c2)
- Bug 1243001 part 5. Get rid of most of the dom::Promise methods when SPIDERMONKEY_PROMISE is defined, and reimplement the rest in terms of SpiderMonkey Promise. r=peterv (693a61e5a2)
- Bug 1242054. Get rid of AbortablePromise, so we can move Promise into SpiderMonkey more easily. r=khuey (6a0200e625)
- Bug 1243001 part 6. Implement Promise::AppendNativeHandler in the SPIDERMONKEY_PROMISE world. r=peterv (7c3a6390f9)
- Bug 1243001 part 7. Stop wrappercaching dom::Promise when SPIDERMONKEY_PROMISE is defined. r=peterv (be1bd9b33f)
- Bug 1243001 part 8. Tell SpiderMonkey to put its promise jobs into the CycleCollectedJSRuntime job queue. r=peterv (192e6a551c)
- Bug 1156880 - Null check the prescontext in nsDOMWindowUtils::AdvanceTimeAndRefresh; r=mstange (11f1a39f22)
- Bug 1191597 part 1 - Add head.js and dummy page for browser chrome test. r=smaug (5257870dd3)
- Bug 1191597 part 2 - Convert fullscreen-esc-context-menu to a browser chrome test. r=smaug (e1c0fe84a4)
2023-11-08 16:37:38 +08:00

476 lines
10 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 "nsPluginArray.h"
#include "mozilla/dom/PluginArrayBinding.h"
#include "mozilla/dom/PluginBinding.h"
#include "nsMimeTypeArray.h"
#include "Navigator.h"
#include "nsIDocShell.h"
#include "nsIWebNavigation.h"
#include "nsPluginHost.h"
#include "nsPluginTags.h"
#include "nsIObserverService.h"
#include "nsIWeakReference.h"
#include "mozilla/Services.h"
#include "nsIInterfaceRequestorUtils.h"
using namespace mozilla;
using namespace mozilla::dom;
nsPluginArray::nsPluginArray(nsPIDOMWindowInner* aWindow)
: mWindow(aWindow)
{
}
void
nsPluginArray::Init()
{
nsCOMPtr<nsIObserverService> obsService =
mozilla::services::GetObserverService();
if (obsService) {
obsService->AddObserver(this, "plugin-info-updated", true);
}
}
nsPluginArray::~nsPluginArray()
{
}
nsPIDOMWindowInner*
nsPluginArray::GetParentObject() const
{
MOZ_ASSERT(mWindow);
return mWindow;
}
JSObject*
nsPluginArray::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
{
return PluginArrayBinding::Wrap(aCx, this, aGivenProto);
}
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsPluginArray)
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsPluginArray)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsPluginArray)
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIObserver)
NS_INTERFACE_MAP_ENTRY(nsIObserver)
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(nsPluginArray,
mWindow,
mPlugins)
static void
GetPluginMimeTypes(const nsTArray<RefPtr<nsPluginElement> >& aPlugins,
nsTArray<RefPtr<nsMimeType> >& aMimeTypes)
{
for (uint32_t i = 0; i < aPlugins.Length(); ++i) {
nsPluginElement *plugin = aPlugins[i];
aMimeTypes.AppendElements(plugin->MimeTypes());
}
}
static bool
operator<(const RefPtr<nsMimeType>& lhs, const RefPtr<nsMimeType>& rhs)
{
// Sort MIME types alphabetically by type name.
return lhs->Type() < rhs->Type();
}
void
nsPluginArray::GetMimeTypes(nsTArray<RefPtr<nsMimeType>>& aMimeTypes)
{
aMimeTypes.Clear();
if (!AllowPlugins()) {
return;
}
EnsurePlugins();
GetPluginMimeTypes(mPlugins, aMimeTypes);
// Alphabetize the enumeration order of non-hidden MIME types to reduce
// fingerprintable entropy based on plugins' installation file times.
aMimeTypes.Sort();
}
nsPluginElement*
nsPluginArray::Item(uint32_t aIndex)
{
bool unused;
return IndexedGetter(aIndex, unused);
}
nsPluginElement*
nsPluginArray::NamedItem(const nsAString& aName)
{
bool unused;
return NamedGetter(aName, unused);
}
void
nsPluginArray::Refresh(bool aReloadDocuments)
{
RefPtr<nsPluginHost> pluginHost = nsPluginHost::GetInst();
if(!AllowPlugins() || !pluginHost) {
return;
}
// NS_ERROR_PLUGINS_PLUGINSNOTCHANGED on reloading plugins indicates
// that plugins did not change and was not reloaded
if (pluginHost->ReloadPlugins() ==
NS_ERROR_PLUGINS_PLUGINSNOTCHANGED) {
nsTArray<nsCOMPtr<nsIInternalPluginTag> > newPluginTags;
pluginHost->GetPlugins(newPluginTags);
// Check if the number of plugins we know about are different from
// the number of plugin tags the plugin host knows about. If the
// lengths are different, we refresh. This is safe because we're
// notified for every plugin enabling/disabling event that
// happens, and therefore the lengths will be in sync only when
// the both arrays contain the same plugin tags (though as
// different types).
if (newPluginTags.Length() == mPlugins.Length()) {
return;
}
}
mPlugins.Clear();
nsCOMPtr<nsIDOMNavigator> navigator = mWindow->GetNavigator();
if (!navigator) {
return;
}
static_cast<mozilla::dom::Navigator*>(navigator.get())->RefreshMIMEArray();
nsCOMPtr<nsIWebNavigation> webNav = do_GetInterface(mWindow);
if (aReloadDocuments && webNav) {
webNav->Reload(nsIWebNavigation::LOAD_FLAGS_NONE);
}
}
nsPluginElement*
nsPluginArray::IndexedGetter(uint32_t aIndex, bool &aFound)
{
aFound = false;
if (!AllowPlugins()) {
return nullptr;
}
EnsurePlugins();
aFound = aIndex < mPlugins.Length();
if (!aFound) {
return nullptr;
}
return mPlugins[aIndex];
}
void
nsPluginArray::Invalidate()
{
nsCOMPtr<nsIObserverService> obsService =
mozilla::services::GetObserverService();
if (obsService) {
obsService->RemoveObserver(this, "plugin-info-updated");
}
}
static nsPluginElement*
FindPlugin(const nsTArray<RefPtr<nsPluginElement> >& aPlugins,
const nsAString& aName)
{
for (uint32_t i = 0; i < aPlugins.Length(); ++i) {
nsAutoString pluginName;
nsPluginElement* plugin = aPlugins[i];
plugin->GetName(pluginName);
if (pluginName.Equals(aName)) {
return plugin;
}
}
return nullptr;
}
nsPluginElement*
nsPluginArray::NamedGetter(const nsAString& aName, bool &aFound)
{
aFound = false;
if (!AllowPlugins()) {
return nullptr;
}
EnsurePlugins();
nsPluginElement* plugin = FindPlugin(mPlugins, aName);
aFound = (plugin != nullptr);
return plugin;
}
bool
nsPluginArray::NameIsEnumerable(const nsAString& aName)
{
return true;
}
uint32_t
nsPluginArray::Length()
{
if (!AllowPlugins()) {
return 0;
}
EnsurePlugins();
return mPlugins.Length();
}
void
nsPluginArray::GetSupportedNames(unsigned, nsTArray<nsString>& aRetval)
{
aRetval.Clear();
if (!AllowPlugins()) {
return;
}
for (uint32_t i = 0; i < mPlugins.Length(); ++i) {
nsAutoString pluginName;
mPlugins[i]->GetName(pluginName);
aRetval.AppendElement(pluginName);
}
}
NS_IMETHODIMP
nsPluginArray::Observe(nsISupports *aSubject, const char *aTopic,
const char16_t *aData) {
if (!nsCRT::strcmp(aTopic, "plugin-info-updated")) {
Refresh(false);
}
return NS_OK;
}
bool
nsPluginArray::AllowPlugins() const
{
nsCOMPtr<nsIDocShell> docShell = mWindow ? mWindow->GetDocShell() : nullptr;
return docShell && docShell->PluginsAllowedInCurrentDoc();
}
static bool
operator<(const RefPtr<nsPluginElement>& lhs,
const RefPtr<nsPluginElement>& rhs)
{
// Sort plugins alphabetically by name.
return lhs->PluginTag()->Name() < rhs->PluginTag()->Name();
}
void
nsPluginArray::EnsurePlugins()
{
if (!mPlugins.IsEmpty()) {
// We already have an array of plugin elements.
return;
}
RefPtr<nsPluginHost> pluginHost = nsPluginHost::GetInst();
if (!pluginHost) {
// We have no plugin host.
return;
}
nsTArray<nsCOMPtr<nsIInternalPluginTag> > pluginTags;
pluginHost->GetPlugins(pluginTags);
// need to wrap each of these with a nsPluginElement, which is
// scriptable.
for (uint32_t i = 0; i < pluginTags.Length(); ++i) {
mPlugins.AppendElement(new nsPluginElement(mWindow, pluginTags[i]));
}
// Alphabetize the enumeration order of non-hidden plugins to reduce
// fingerprintable entropy based on plugins' installation file times.
mPlugins.Sort();
}
// nsPluginElement implementation.
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsPluginElement)
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsPluginElement)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsPluginElement)
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(nsPluginElement, mWindow, mMimeTypes)
nsPluginElement::nsPluginElement(nsPIDOMWindowInner* aWindow,
nsIInternalPluginTag* aPluginTag)
: mWindow(aWindow),
mPluginTag(aPluginTag)
{
}
nsPluginElement::~nsPluginElement()
{
}
nsPIDOMWindowInner*
nsPluginElement::GetParentObject() const
{
MOZ_ASSERT(mWindow);
return mWindow;
}
JSObject*
nsPluginElement::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
{
return PluginBinding::Wrap(aCx, this, aGivenProto);
}
void
nsPluginElement::GetDescription(nsString& retval) const
{
CopyUTF8toUTF16(mPluginTag->Description(), retval);
}
void
nsPluginElement::GetFilename(nsString& retval) const
{
CopyUTF8toUTF16(mPluginTag->FileName(), retval);
}
void
nsPluginElement::GetVersion(nsString& retval) const
{
CopyUTF8toUTF16(mPluginTag->Version(), retval);
}
void
nsPluginElement::GetName(nsString& retval) const
{
CopyUTF8toUTF16(mPluginTag->Name(), retval);
}
nsMimeType*
nsPluginElement::Item(uint32_t aIndex)
{
EnsurePluginMimeTypes();
return mMimeTypes.SafeElementAt(aIndex);
}
nsMimeType*
nsPluginElement::NamedItem(const nsAString& aName)
{
bool unused;
return NamedGetter(aName, unused);
}
nsMimeType*
nsPluginElement::IndexedGetter(uint32_t aIndex, bool &aFound)
{
EnsurePluginMimeTypes();
aFound = aIndex < mMimeTypes.Length();
if (!aFound) {
return nullptr;
}
return mMimeTypes[aIndex];
}
nsMimeType*
nsPluginElement::NamedGetter(const nsAString& aName, bool &aFound)
{
EnsurePluginMimeTypes();
aFound = false;
for (uint32_t i = 0; i < mMimeTypes.Length(); ++i) {
if (mMimeTypes[i]->Type().Equals(aName)) {
aFound = true;
return mMimeTypes[i];
}
}
return nullptr;
}
bool
nsPluginElement::NameIsEnumerable(const nsAString& aName)
{
return true;
}
uint32_t
nsPluginElement::Length()
{
EnsurePluginMimeTypes();
return mMimeTypes.Length();
}
void
nsPluginElement::GetSupportedNames(unsigned, nsTArray<nsString>& retval)
{
EnsurePluginMimeTypes();
for (uint32_t i = 0; i < mMimeTypes.Length(); ++i) {
retval.AppendElement(mMimeTypes[i]->Type());
}
}
nsTArray<RefPtr<nsMimeType> >&
nsPluginElement::MimeTypes()
{
EnsurePluginMimeTypes();
return mMimeTypes;
}
void
nsPluginElement::EnsurePluginMimeTypes()
{
if (!mMimeTypes.IsEmpty()) {
return;
}
if (mPluginTag->MimeTypes().Length() != mPluginTag->MimeDescriptions().Length() ||
mPluginTag->MimeTypes().Length() != mPluginTag->Extensions().Length()) {
MOZ_ASSERT(false, "mime type arrays expected to be the same length");
return;
}
for (uint32_t i = 0; i < mPluginTag->MimeTypes().Length(); ++i) {
NS_ConvertUTF8toUTF16 type(mPluginTag->MimeTypes()[i]);
NS_ConvertUTF8toUTF16 description(mPluginTag->MimeDescriptions()[i]);
NS_ConvertUTF8toUTF16 extension(mPluginTag->Extensions()[i]);
mMimeTypes.AppendElement(new nsMimeType(mWindow, this, type, description,
extension));
}
}