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"
|
2013-09-22 20:35:05 -07:00
|
|
|
#include "nsServiceManagerUtils.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-10-31 22:19:09 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_3(nsMimeTypeArray,
|
2013-08-28 13:55:29 -07:00
|
|
|
mWindow,
|
2013-10-31 22:19:09 -07:00
|
|
|
mMimeTypes,
|
|
|
|
mHiddenMimeTypes)
|
2013-07-09 06:58:21 -07:00
|
|
|
|
2013-08-28 13:55:29 -07:00
|
|
|
nsMimeTypeArray::nsMimeTypeArray(nsPIDOMWindow* aWindow)
|
2013-10-31 22:19:09 -07:00
|
|
|
: mWindow(aWindow)
|
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*
|
2014-04-08 15:27:18 -07:00
|
|
|
nsMimeTypeArray::WrapObject(JSContext* aCx)
|
2013-07-09 06:58:21 -07:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-08 15:27:17 -07:00
|
|
|
return MimeTypeArrayBinding::Wrap(aCx, this);
|
2013-07-09 06:58:21 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-07-09 06:58:21 -07:00
|
|
|
void
|
|
|
|
nsMimeTypeArray::Refresh()
|
|
|
|
{
|
|
|
|
mMimeTypes.Clear();
|
2013-10-31 22:19:09 -07:00
|
|
|
mHiddenMimeTypes.Clear();
|
2013-07-09 06:58:21 -07:00
|
|
|
}
|
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;
|
|
|
|
|
2013-10-20 00:05:05 -07:00
|
|
|
EnsurePluginMimeTypes();
|
2008-10-22 07:31:14 -07:00
|
|
|
|
2013-10-31 22:19:09 -07:00
|
|
|
if (aIndex >= mMimeTypes.Length()) {
|
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-10-31 22:19:09 -07:00
|
|
|
static nsMimeType*
|
|
|
|
FindMimeType(const nsTArray<nsRefPtr<nsMimeType> >& aMimeTypes,
|
|
|
|
const nsAString& aType)
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < aMimeTypes.Length(); ++i) {
|
|
|
|
nsMimeType* mimeType = aMimeTypes[i];
|
|
|
|
if (aType.Equals(mimeType->Type())) {
|
|
|
|
return mimeType;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
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-10-20 00:05:05 -07:00
|
|
|
EnsurePluginMimeTypes();
|
2008-10-31 08:55:59 -07:00
|
|
|
|
2014-03-20 15:41:51 -07:00
|
|
|
nsString lowerName(aName);
|
|
|
|
ToLowerCase(lowerName);
|
|
|
|
|
|
|
|
nsMimeType* mimeType = FindMimeType(mMimeTypes, lowerName);
|
2013-10-31 22:19:09 -07:00
|
|
|
if (!mimeType) {
|
2014-03-20 15:41:51 -07:00
|
|
|
mimeType = FindMimeType(mHiddenMimeTypes, lowerName);
|
2013-10-31 22:19:09 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-10-31 22:19:09 -07:00
|
|
|
if (mimeType) {
|
|
|
|
aFound = true;
|
|
|
|
return mimeType;
|
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;
|
2014-03-20 15:41:51 -07:00
|
|
|
mimeSrv->GetFromTypeAndExtension(NS_ConvertUTF16toUTF8(lowerName),
|
|
|
|
EmptyCString(), getter_AddRefs(mimeInfo));
|
2013-07-09 06:58:21 -07:00
|
|
|
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;
|
|
|
|
|
2013-10-31 22:19:09 -07:00
|
|
|
// We don't want navigator.mimeTypes enumeration to expose MIME types with
|
|
|
|
// application handlers, so add them to the list of hidden MIME types.
|
2014-03-20 15:41:51 -07:00
|
|
|
nsMimeType *mt = new nsMimeType(mWindow, lowerName);
|
2013-10-31 22:19:09 -07:00
|
|
|
mHiddenMimeTypes.AppendElement(mt);
|
2013-07-09 06:58:21 -07:00
|
|
|
|
|
|
|
return mt;
|
2008-10-31 14:40:35 -07:00
|
|
|
}
|
|
|
|
|
2014-04-15 19:58:44 -07:00
|
|
|
bool
|
|
|
|
nsMimeTypeArray::NameIsEnumerable(const nsAString& aName)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-09 06:58:21 -07:00
|
|
|
uint32_t
|
|
|
|
nsMimeTypeArray::Length()
|
2008-10-31 14:40:35 -07:00
|
|
|
{
|
2013-10-20 00:05:05 -07:00
|
|
|
EnsurePluginMimeTypes();
|
2008-10-31 14:40:35 -07:00
|
|
|
|
2013-10-31 22:19:09 -07:00
|
|
|
return mMimeTypes.Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-12-01 00:28:16 -08:00
|
|
|
void
|
2014-04-15 19:58:44 -07:00
|
|
|
nsMimeTypeArray::GetSupportedNames(unsigned, nsTArray< nsString >& aRetval)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-10-20 00:05:05 -07:00
|
|
|
EnsurePluginMimeTypes();
|
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
|
2013-10-20 00:05:05 -07:00
|
|
|
nsMimeTypeArray::EnsurePluginMimeTypes()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-10-31 22:19:09 -07:00
|
|
|
if (!mMimeTypes.IsEmpty() || !mHiddenMimeTypes.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-10-31 22:19:09 -07:00
|
|
|
pluginArray->GetMimeTypes(mMimeTypes, mHiddenMimeTypes);
|
2013-07-09 06:58:21 -07:00
|
|
|
}
|
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*
|
2014-04-08 15:27:18 -07:00
|
|
|
nsMimeType::WrapObject(JSContext* aCx)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-08 15:27:17 -07:00
|
|
|
return MimeTypeBinding::Wrap(aCx, 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;
|
|
|
|
}
|