dom/base/nsMimeTypeArray.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=2 sw=2 et tw=79: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "nsMimeTypeArray.h"
michael@0 8
michael@0 9 #include "mozilla/dom/MimeTypeArrayBinding.h"
michael@0 10 #include "mozilla/dom/MimeTypeBinding.h"
michael@0 11 #include "nsIDOMNavigator.h"
michael@0 12 #include "nsPluginArray.h"
michael@0 13 #include "nsIMIMEService.h"
michael@0 14 #include "nsIMIMEInfo.h"
michael@0 15 #include "Navigator.h"
michael@0 16 #include "nsServiceManagerUtils.h"
michael@0 17
michael@0 18 using namespace mozilla;
michael@0 19 using namespace mozilla::dom;
michael@0 20
michael@0 21 NS_IMPL_CYCLE_COLLECTING_ADDREF(nsMimeTypeArray)
michael@0 22 NS_IMPL_CYCLE_COLLECTING_RELEASE(nsMimeTypeArray)
michael@0 23 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsMimeTypeArray)
michael@0 24 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
michael@0 25 NS_INTERFACE_MAP_ENTRY(nsISupports)
michael@0 26 NS_INTERFACE_MAP_END
michael@0 27
michael@0 28 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_3(nsMimeTypeArray,
michael@0 29 mWindow,
michael@0 30 mMimeTypes,
michael@0 31 mHiddenMimeTypes)
michael@0 32
michael@0 33 nsMimeTypeArray::nsMimeTypeArray(nsPIDOMWindow* aWindow)
michael@0 34 : mWindow(aWindow)
michael@0 35 {
michael@0 36 SetIsDOMBinding();
michael@0 37 }
michael@0 38
michael@0 39 nsMimeTypeArray::~nsMimeTypeArray()
michael@0 40 {
michael@0 41 }
michael@0 42
michael@0 43 JSObject*
michael@0 44 nsMimeTypeArray::WrapObject(JSContext* aCx)
michael@0 45 {
michael@0 46 return MimeTypeArrayBinding::Wrap(aCx, this);
michael@0 47 }
michael@0 48
michael@0 49 void
michael@0 50 nsMimeTypeArray::Refresh()
michael@0 51 {
michael@0 52 mMimeTypes.Clear();
michael@0 53 mHiddenMimeTypes.Clear();
michael@0 54 }
michael@0 55
michael@0 56 nsPIDOMWindow*
michael@0 57 nsMimeTypeArray::GetParentObject() const
michael@0 58 {
michael@0 59 MOZ_ASSERT(mWindow);
michael@0 60 return mWindow;
michael@0 61 }
michael@0 62
michael@0 63 nsMimeType*
michael@0 64 nsMimeTypeArray::Item(uint32_t aIndex)
michael@0 65 {
michael@0 66 bool unused;
michael@0 67 return IndexedGetter(aIndex, unused);
michael@0 68 }
michael@0 69
michael@0 70 nsMimeType*
michael@0 71 nsMimeTypeArray::NamedItem(const nsAString& aName)
michael@0 72 {
michael@0 73 bool unused;
michael@0 74 return NamedGetter(aName, unused);
michael@0 75 }
michael@0 76
michael@0 77 nsMimeType*
michael@0 78 nsMimeTypeArray::IndexedGetter(uint32_t aIndex, bool &aFound)
michael@0 79 {
michael@0 80 aFound = false;
michael@0 81
michael@0 82 EnsurePluginMimeTypes();
michael@0 83
michael@0 84 if (aIndex >= mMimeTypes.Length()) {
michael@0 85 return nullptr;
michael@0 86 }
michael@0 87
michael@0 88 aFound = true;
michael@0 89
michael@0 90 return mMimeTypes[aIndex];
michael@0 91 }
michael@0 92
michael@0 93 static nsMimeType*
michael@0 94 FindMimeType(const nsTArray<nsRefPtr<nsMimeType> >& aMimeTypes,
michael@0 95 const nsAString& aType)
michael@0 96 {
michael@0 97 for (uint32_t i = 0; i < aMimeTypes.Length(); ++i) {
michael@0 98 nsMimeType* mimeType = aMimeTypes[i];
michael@0 99 if (aType.Equals(mimeType->Type())) {
michael@0 100 return mimeType;
michael@0 101 }
michael@0 102 }
michael@0 103
michael@0 104 return nullptr;
michael@0 105 }
michael@0 106
michael@0 107 nsMimeType*
michael@0 108 nsMimeTypeArray::NamedGetter(const nsAString& aName, bool &aFound)
michael@0 109 {
michael@0 110 aFound = false;
michael@0 111
michael@0 112 EnsurePluginMimeTypes();
michael@0 113
michael@0 114 nsString lowerName(aName);
michael@0 115 ToLowerCase(lowerName);
michael@0 116
michael@0 117 nsMimeType* mimeType = FindMimeType(mMimeTypes, lowerName);
michael@0 118 if (!mimeType) {
michael@0 119 mimeType = FindMimeType(mHiddenMimeTypes, lowerName);
michael@0 120 }
michael@0 121
michael@0 122 if (mimeType) {
michael@0 123 aFound = true;
michael@0 124 return mimeType;
michael@0 125 }
michael@0 126
michael@0 127 // Now let's check with the MIME service.
michael@0 128 nsCOMPtr<nsIMIMEService> mimeSrv = do_GetService("@mozilla.org/mime;1");
michael@0 129 if (!mimeSrv) {
michael@0 130 return nullptr;
michael@0 131 }
michael@0 132
michael@0 133 nsCOMPtr<nsIMIMEInfo> mimeInfo;
michael@0 134 mimeSrv->GetFromTypeAndExtension(NS_ConvertUTF16toUTF8(lowerName),
michael@0 135 EmptyCString(), getter_AddRefs(mimeInfo));
michael@0 136 if (!mimeInfo) {
michael@0 137 return nullptr;
michael@0 138 }
michael@0 139
michael@0 140 // Now we check whether we can really claim to support this type
michael@0 141 nsHandlerInfoAction action = nsIHandlerInfo::saveToDisk;
michael@0 142 mimeInfo->GetPreferredAction(&action);
michael@0 143 if (action != nsIMIMEInfo::handleInternally) {
michael@0 144 bool hasHelper = false;
michael@0 145 mimeInfo->GetHasDefaultHandler(&hasHelper);
michael@0 146
michael@0 147 if (!hasHelper) {
michael@0 148 nsCOMPtr<nsIHandlerApp> helper;
michael@0 149 mimeInfo->GetPreferredApplicationHandler(getter_AddRefs(helper));
michael@0 150
michael@0 151 if (!helper) {
michael@0 152 // mime info from the OS may not have a PreferredApplicationHandler
michael@0 153 // so just check for an empty default description
michael@0 154 nsAutoString defaultDescription;
michael@0 155 mimeInfo->GetDefaultDescription(defaultDescription);
michael@0 156
michael@0 157 if (defaultDescription.IsEmpty()) {
michael@0 158 // no support; just leave
michael@0 159 return nullptr;
michael@0 160 }
michael@0 161 }
michael@0 162 }
michael@0 163 }
michael@0 164
michael@0 165 // If we got here, we support this type! Say so.
michael@0 166 aFound = true;
michael@0 167
michael@0 168 // We don't want navigator.mimeTypes enumeration to expose MIME types with
michael@0 169 // application handlers, so add them to the list of hidden MIME types.
michael@0 170 nsMimeType *mt = new nsMimeType(mWindow, lowerName);
michael@0 171 mHiddenMimeTypes.AppendElement(mt);
michael@0 172
michael@0 173 return mt;
michael@0 174 }
michael@0 175
michael@0 176 bool
michael@0 177 nsMimeTypeArray::NameIsEnumerable(const nsAString& aName)
michael@0 178 {
michael@0 179 return true;
michael@0 180 }
michael@0 181
michael@0 182 uint32_t
michael@0 183 nsMimeTypeArray::Length()
michael@0 184 {
michael@0 185 EnsurePluginMimeTypes();
michael@0 186
michael@0 187 return mMimeTypes.Length();
michael@0 188 }
michael@0 189
michael@0 190 void
michael@0 191 nsMimeTypeArray::GetSupportedNames(unsigned, nsTArray< nsString >& aRetval)
michael@0 192 {
michael@0 193 EnsurePluginMimeTypes();
michael@0 194
michael@0 195 for (uint32_t i = 0; i < mMimeTypes.Length(); ++i) {
michael@0 196 aRetval.AppendElement(mMimeTypes[i]->Type());
michael@0 197 }
michael@0 198 }
michael@0 199
michael@0 200 void
michael@0 201 nsMimeTypeArray::EnsurePluginMimeTypes()
michael@0 202 {
michael@0 203 if (!mMimeTypes.IsEmpty() || !mHiddenMimeTypes.IsEmpty() || !mWindow) {
michael@0 204 return;
michael@0 205 }
michael@0 206
michael@0 207 nsCOMPtr<nsIDOMNavigator> navigator;
michael@0 208 mWindow->GetNavigator(getter_AddRefs(navigator));
michael@0 209
michael@0 210 if (!navigator) {
michael@0 211 return;
michael@0 212 }
michael@0 213
michael@0 214 ErrorResult rv;
michael@0 215 nsPluginArray *pluginArray =
michael@0 216 static_cast<Navigator*>(navigator.get())->GetPlugins(rv);
michael@0 217 if (!pluginArray) {
michael@0 218 return;
michael@0 219 }
michael@0 220
michael@0 221 pluginArray->GetMimeTypes(mMimeTypes, mHiddenMimeTypes);
michael@0 222 }
michael@0 223
michael@0 224 NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(nsMimeType, AddRef)
michael@0 225 NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(nsMimeType, Release)
michael@0 226
michael@0 227 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_2(nsMimeType, mWindow, mPluginElement)
michael@0 228
michael@0 229 nsMimeType::nsMimeType(nsPIDOMWindow* aWindow, nsPluginElement* aPluginElement,
michael@0 230 uint32_t aPluginTagMimeIndex, const nsAString& aType)
michael@0 231 : mWindow(aWindow),
michael@0 232 mPluginElement(aPluginElement),
michael@0 233 mPluginTagMimeIndex(aPluginTagMimeIndex),
michael@0 234 mType(aType)
michael@0 235 {
michael@0 236 SetIsDOMBinding();
michael@0 237 }
michael@0 238
michael@0 239 nsMimeType::nsMimeType(nsPIDOMWindow* aWindow, const nsAString& aType)
michael@0 240 : mWindow(aWindow),
michael@0 241 mPluginElement(nullptr),
michael@0 242 mPluginTagMimeIndex(0),
michael@0 243 mType(aType)
michael@0 244 {
michael@0 245 SetIsDOMBinding();
michael@0 246 }
michael@0 247
michael@0 248 nsMimeType::~nsMimeType()
michael@0 249 {
michael@0 250 }
michael@0 251
michael@0 252 nsPIDOMWindow*
michael@0 253 nsMimeType::GetParentObject() const
michael@0 254 {
michael@0 255 MOZ_ASSERT(mWindow);
michael@0 256 return mWindow;
michael@0 257 }
michael@0 258
michael@0 259 JSObject*
michael@0 260 nsMimeType::WrapObject(JSContext* aCx)
michael@0 261 {
michael@0 262 return MimeTypeBinding::Wrap(aCx, this);
michael@0 263 }
michael@0 264
michael@0 265 void
michael@0 266 nsMimeType::GetDescription(nsString& retval) const
michael@0 267 {
michael@0 268 retval.Truncate();
michael@0 269
michael@0 270 if (mPluginElement) {
michael@0 271 CopyUTF8toUTF16(mPluginElement->PluginTag()->
michael@0 272 mMimeDescriptions[mPluginTagMimeIndex], retval);
michael@0 273 }
michael@0 274 }
michael@0 275
michael@0 276 nsPluginElement*
michael@0 277 nsMimeType::GetEnabledPlugin() const
michael@0 278 {
michael@0 279 return (mPluginElement && mPluginElement->PluginTag()->IsEnabled()) ?
michael@0 280 mPluginElement : nullptr;
michael@0 281 }
michael@0 282
michael@0 283 void
michael@0 284 nsMimeType::GetSuffixes(nsString& retval) const
michael@0 285 {
michael@0 286 retval.Truncate();
michael@0 287
michael@0 288 if (mPluginElement) {
michael@0 289 CopyUTF8toUTF16(mPluginElement->PluginTag()->
michael@0 290 mExtensions[mPluginTagMimeIndex], retval);
michael@0 291 }
michael@0 292 }
michael@0 293
michael@0 294 void
michael@0 295 nsMimeType::GetType(nsString& aRetval) const
michael@0 296 {
michael@0 297 aRetval = mType;
michael@0 298 }

mercurial