2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2010-08-12 21:05:05 -07:00
|
|
|
/* vim: set ts=2 sw=2 et tw=79: */
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsMimeTypeArray.h"
|
2013-08-13 23:56:21 -07:00
|
|
|
|
2013-07-09 06:58:21 -07:00
|
|
|
#include "mozilla/dom/MimeTypeArrayBinding.h"
|
|
|
|
#include "mozilla/dom/MimeTypeBinding.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIDOMNavigator.h"
|
2013-07-09 06:58:21 -07:00
|
|
|
#include "nsPluginArray.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIMIMEService.h"
|
|
|
|
#include "nsIMIMEInfo.h"
|
2013-07-17 20:23:52 -07:00
|
|
|
#include "Navigator.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-07-09 06:58:21 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-07-09 06:58:21 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsMimeTypeArray)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsMimeTypeArray)
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsMimeTypeArray)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2013-08-28 13:55:29 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_2(nsMimeTypeArray,
|
|
|
|
mWindow,
|
2013-07-09 06:58:21 -07:00
|
|
|
mMimeTypes)
|
|
|
|
|
2013-08-28 13:55:29 -07:00
|
|
|
nsMimeTypeArray::nsMimeTypeArray(nsPIDOMWindow* aWindow)
|
2013-07-09 06:58:21 -07:00
|
|
|
: mWindow(aWindow),
|
|
|
|
mPluginMimeTypeCount(0)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-07-09 06:58:21 -07:00
|
|
|
SetIsDOMBinding();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsMimeTypeArray::~nsMimeTypeArray()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-07-09 06:58:21 -07:00
|
|
|
JSObject*
|
|
|
|
nsMimeTypeArray::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
|
|
|
|
{
|
|
|
|
return MimeTypeArrayBinding::Wrap(aCx, aScope, this);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-07-09 06:58:21 -07:00
|
|
|
void
|
|
|
|
nsMimeTypeArray::Refresh()
|
|
|
|
{
|
|
|
|
mMimeTypes.Clear();
|
|
|
|
mPluginMimeTypeCount = 0;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-28 13:55:29 -07:00
|
|
|
nsPIDOMWindow*
|
2013-07-09 06:58:21 -07:00
|
|
|
nsMimeTypeArray::GetParentObject() const
|
|
|
|
{
|
2013-08-28 13:55:29 -07:00
|
|
|
MOZ_ASSERT(mWindow);
|
|
|
|
return mWindow;
|
2013-07-09 06:58:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-07-09 06:58:21 -07:00
|
|
|
nsMimeType*
|
|
|
|
nsMimeTypeArray::Item(uint32_t aIndex)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-07-09 06:58:21 -07:00
|
|
|
bool unused;
|
|
|
|
return IndexedGetter(aIndex, unused);
|
|
|
|
}
|
2008-10-31 14:40:35 -07:00
|
|
|
|
2013-07-09 06:58:21 -07:00
|
|
|
nsMimeType*
|
|
|
|
nsMimeTypeArray::NamedItem(const nsAString& aName)
|
|
|
|
{
|
|
|
|
bool unused;
|
|
|
|
return NamedGetter(aName, unused);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-07-09 06:58:21 -07:00
|
|
|
nsMimeType*
|
|
|
|
nsMimeTypeArray::IndexedGetter(uint32_t aIndex, bool &aFound)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-07-09 06:58:21 -07:00
|
|
|
aFound = false;
|
|
|
|
|
|
|
|
if (mMimeTypes.IsEmpty()) {
|
|
|
|
EnsureMimeTypes();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-10-22 07:31:14 -07:00
|
|
|
|
2013-07-09 06:58:21 -07:00
|
|
|
MOZ_ASSERT(mMimeTypes.Length() >= mPluginMimeTypeCount);
|
2008-10-31 14:40:35 -07:00
|
|
|
|
|
|
|
if (aIndex >= mPluginMimeTypeCount) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-10-22 07:31:14 -07:00
|
|
|
|
2013-07-09 06:58:21 -07:00
|
|
|
aFound = true;
|
2008-10-22 07:31:14 -07:00
|
|
|
|
2013-07-09 06:58:21 -07:00
|
|
|
return mMimeTypes[aIndex];
|
2008-10-22 07:31:14 -07:00
|
|
|
}
|
|
|
|
|
2013-07-09 06:58:21 -07:00
|
|
|
nsMimeType*
|
|
|
|
nsMimeTypeArray::NamedGetter(const nsAString& aName, bool &aFound)
|
2008-10-22 07:31:14 -07:00
|
|
|
{
|
2013-07-09 06:58:21 -07:00
|
|
|
aFound = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-07-09 06:58:21 -07:00
|
|
|
if (mMimeTypes.IsEmpty()) {
|
|
|
|
EnsureMimeTypes();
|
2008-10-31 11:07:26 -07:00
|
|
|
}
|
2008-10-31 08:55:59 -07:00
|
|
|
|
2013-07-09 06:58:21 -07:00
|
|
|
for (uint32_t i = 0; i < mMimeTypes.Length(); ++i) {
|
|
|
|
if (aName.Equals(mMimeTypes[i]->Type())) {
|
|
|
|
aFound = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-07-09 06:58:21 -07:00
|
|
|
return mMimeTypes[i];
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now let's check with the MIME service.
|
|
|
|
nsCOMPtr<nsIMIMEService> mimeSrv = do_GetService("@mozilla.org/mime;1");
|
2013-07-09 06:58:21 -07:00
|
|
|
if (!mimeSrv) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-07-09 06:58:21 -07:00
|
|
|
nsCOMPtr<nsIMIMEInfo> mimeInfo;
|
|
|
|
mimeSrv->GetFromTypeAndExtension(NS_ConvertUTF16toUTF8(aName), EmptyCString(),
|
|
|
|
getter_AddRefs(mimeInfo));
|
|
|
|
if (!mimeInfo) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2008-10-31 14:40:35 -07:00
|
|
|
|
2013-07-09 06:58:21 -07:00
|
|
|
// Now we check whether we can really claim to support this type
|
|
|
|
nsHandlerInfoAction action = nsIHandlerInfo::saveToDisk;
|
|
|
|
mimeInfo->GetPreferredAction(&action);
|
|
|
|
if (action != nsIMIMEInfo::handleInternally) {
|
|
|
|
bool hasHelper = false;
|
|
|
|
mimeInfo->GetHasDefaultHandler(&hasHelper);
|
|
|
|
|
|
|
|
if (!hasHelper) {
|
|
|
|
nsCOMPtr<nsIHandlerApp> helper;
|
|
|
|
mimeInfo->GetPreferredApplicationHandler(getter_AddRefs(helper));
|
|
|
|
|
|
|
|
if (!helper) {
|
|
|
|
// mime info from the OS may not have a PreferredApplicationHandler
|
|
|
|
// so just check for an empty default description
|
|
|
|
nsAutoString defaultDescription;
|
|
|
|
mimeInfo->GetDefaultDescription(defaultDescription);
|
|
|
|
|
|
|
|
if (defaultDescription.IsEmpty()) {
|
|
|
|
// no support; just leave
|
|
|
|
return nullptr;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-09 06:58:21 -07:00
|
|
|
// If we got here, we support this type! Say so.
|
|
|
|
aFound = true;
|
|
|
|
|
|
|
|
nsMimeType *mt = new nsMimeType(mWindow, aName);
|
|
|
|
mMimeTypes.AppendElement(mt);
|
|
|
|
|
|
|
|
return mt;
|
2008-10-31 14:40:35 -07:00
|
|
|
}
|
|
|
|
|
2013-07-09 06:58:21 -07:00
|
|
|
uint32_t
|
|
|
|
nsMimeTypeArray::Length()
|
2008-10-31 14:40:35 -07:00
|
|
|
{
|
2013-07-09 06:58:21 -07:00
|
|
|
if (mMimeTypes.IsEmpty()) {
|
|
|
|
EnsureMimeTypes();
|
|
|
|
}
|
2008-10-31 14:40:35 -07:00
|
|
|
|
2013-07-09 06:58:21 -07:00
|
|
|
MOZ_ASSERT(mMimeTypes.Length() >= mPluginMimeTypeCount);
|
2008-10-31 14:40:35 -07:00
|
|
|
|
2013-07-09 06:58:21 -07:00
|
|
|
return mPluginMimeTypeCount;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-12-01 00:28:16 -08:00
|
|
|
void
|
2013-07-09 06:58:21 -07:00
|
|
|
nsMimeTypeArray::GetSupportedNames(nsTArray< nsString >& aRetval)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-07-09 06:58:21 -07:00
|
|
|
if (mMimeTypes.IsEmpty()) {
|
|
|
|
EnsureMimeTypes();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-07-09 06:58:21 -07:00
|
|
|
for (uint32_t i = 0; i < mMimeTypes.Length(); ++i) {
|
|
|
|
aRetval.AppendElement(mMimeTypes[i]->Type());
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-07-09 06:58:21 -07:00
|
|
|
void
|
|
|
|
nsMimeTypeArray::EnsureMimeTypes()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-08-28 13:55:29 -07:00
|
|
|
if (!mMimeTypes.IsEmpty() || !mWindow) {
|
2013-07-09 06:58:21 -07:00
|
|
|
return;
|
2010-08-12 21:05:05 -07:00
|
|
|
}
|
|
|
|
|
2013-07-09 06:58:21 -07:00
|
|
|
nsCOMPtr<nsIDOMNavigator> navigator;
|
2013-08-28 13:55:29 -07:00
|
|
|
mWindow->GetNavigator(getter_AddRefs(navigator));
|
2013-07-09 06:58:21 -07:00
|
|
|
|
|
|
|
if (!navigator) {
|
|
|
|
return;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-07-17 20:23:52 -07:00
|
|
|
ErrorResult rv;
|
2013-07-09 06:58:21 -07:00
|
|
|
nsPluginArray *pluginArray =
|
2013-07-17 20:23:52 -07:00
|
|
|
static_cast<Navigator*>(navigator.get())->GetPlugins(rv);
|
|
|
|
if (!pluginArray) {
|
|
|
|
return;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-07-09 06:58:21 -07:00
|
|
|
nsTArray<nsRefPtr<nsPluginElement> > plugins;
|
|
|
|
pluginArray->GetPlugins(plugins);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-07-09 06:58:21 -07:00
|
|
|
for (uint32_t i = 0; i < plugins.Length(); ++i) {
|
|
|
|
nsPluginElement *plugin = plugins[i];
|
2010-01-12 05:08:43 -08:00
|
|
|
|
2013-07-09 06:58:21 -07:00
|
|
|
mMimeTypes.AppendElements(plugin->MimeTypes());
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-07-09 06:58:21 -07:00
|
|
|
mPluginMimeTypeCount = mMimeTypes.Length();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-07-09 06:58:21 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(nsMimeType, AddRef)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(nsMimeType, Release)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-28 13:55:29 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_2(nsMimeType, mWindow, mPluginElement)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-28 13:55:29 -07:00
|
|
|
nsMimeType::nsMimeType(nsPIDOMWindow* aWindow, nsPluginElement* aPluginElement,
|
2013-07-09 06:58:21 -07:00
|
|
|
uint32_t aPluginTagMimeIndex, const nsAString& aType)
|
|
|
|
: mWindow(aWindow),
|
|
|
|
mPluginElement(aPluginElement),
|
|
|
|
mPluginTagMimeIndex(aPluginTagMimeIndex),
|
|
|
|
mType(aType)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-07-09 06:58:21 -07:00
|
|
|
SetIsDOMBinding();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-08-28 13:55:29 -07:00
|
|
|
nsMimeType::nsMimeType(nsPIDOMWindow* aWindow, const nsAString& aType)
|
2013-07-09 06:58:21 -07:00
|
|
|
: mWindow(aWindow),
|
|
|
|
mPluginElement(nullptr),
|
|
|
|
mPluginTagMimeIndex(0),
|
|
|
|
mType(aType)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-07-09 06:58:21 -07:00
|
|
|
SetIsDOMBinding();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-07-09 06:58:21 -07:00
|
|
|
nsMimeType::~nsMimeType()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-08-28 13:55:29 -07:00
|
|
|
nsPIDOMWindow*
|
2013-07-09 06:58:21 -07:00
|
|
|
nsMimeType::GetParentObject() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-08-28 13:55:29 -07:00
|
|
|
MOZ_ASSERT(mWindow);
|
|
|
|
return mWindow;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-07-09 06:58:21 -07:00
|
|
|
JSObject*
|
|
|
|
nsMimeType::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-07-09 06:58:21 -07:00
|
|
|
return MimeTypeBinding::Wrap(aCx, aScope, this);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-07-09 06:58:21 -07:00
|
|
|
void
|
|
|
|
nsMimeType::GetDescription(nsString& retval) const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-07-09 06:58:21 -07:00
|
|
|
retval.Truncate();
|
|
|
|
|
|
|
|
if (mPluginElement) {
|
|
|
|
CopyUTF8toUTF16(mPluginElement->PluginTag()->
|
|
|
|
mMimeDescriptions[mPluginTagMimeIndex], retval);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-07-09 06:58:21 -07:00
|
|
|
nsPluginElement*
|
|
|
|
nsMimeType::GetEnabledPlugin() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-07-09 06:58:21 -07:00
|
|
|
return (mPluginElement && mPluginElement->PluginTag()->IsEnabled()) ?
|
|
|
|
mPluginElement : nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-07-09 06:58:21 -07:00
|
|
|
void
|
|
|
|
nsMimeType::GetSuffixes(nsString& retval) const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-07-09 06:58:21 -07:00
|
|
|
retval.Truncate();
|
|
|
|
|
|
|
|
if (mPluginElement) {
|
|
|
|
CopyUTF8toUTF16(mPluginElement->PluginTag()->
|
|
|
|
mExtensions[mPluginTagMimeIndex], retval);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-07-09 06:58:21 -07:00
|
|
|
void
|
|
|
|
nsMimeType::GetType(nsString& aRetval) const
|
|
|
|
{
|
|
|
|
aRetval = mType;
|
|
|
|
}
|