| 1 | /* |
| 2 | * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies) |
| 3 | * Copyright (C) 2008 Apple Inc. All rights reserved. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include "config.h" |
| 21 | #include "DOMMimeTypeArray.h" |
| 22 | |
| 23 | #include "DOMPlugin.h" |
| 24 | #include "Frame.h" |
| 25 | #include "Page.h" |
| 26 | #include "PluginData.h" |
| 27 | #include <wtf/IsoMallocInlines.h> |
| 28 | #include <wtf/text/AtomicString.h> |
| 29 | |
| 30 | namespace WebCore { |
| 31 | |
| 32 | WTF_MAKE_ISO_ALLOCATED_IMPL(DOMMimeTypeArray); |
| 33 | |
| 34 | DOMMimeTypeArray::DOMMimeTypeArray(DOMWindow* window) |
| 35 | : DOMWindowProperty(window) |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | DOMMimeTypeArray::~DOMMimeTypeArray() = default; |
| 40 | |
| 41 | unsigned DOMMimeTypeArray::length() const |
| 42 | { |
| 43 | PluginData* data = getPluginData(); |
| 44 | if (!data) |
| 45 | return 0; |
| 46 | |
| 47 | Vector<MimeClassInfo> mimes; |
| 48 | Vector<size_t> mimePluginIndices; |
| 49 | data->getWebVisibleMimesAndPluginIndices(mimes, mimePluginIndices); |
| 50 | return mimes.size(); |
| 51 | } |
| 52 | |
| 53 | RefPtr<DOMMimeType> DOMMimeTypeArray::item(unsigned index) |
| 54 | { |
| 55 | PluginData* data = getPluginData(); |
| 56 | if (!data) |
| 57 | return nullptr; |
| 58 | |
| 59 | Vector<MimeClassInfo> mimes; |
| 60 | Vector<size_t> mimePluginIndices; |
| 61 | data->getWebVisibleMimesAndPluginIndices(mimes, mimePluginIndices); |
| 62 | |
| 63 | if (index >= mimes.size()) |
| 64 | return nullptr; |
| 65 | return DOMMimeType::create(data, frame(), index); |
| 66 | } |
| 67 | |
| 68 | RefPtr<DOMMimeType> DOMMimeTypeArray::namedItem(const AtomicString& propertyName) |
| 69 | { |
| 70 | PluginData* data = getPluginData(); |
| 71 | if (!data) |
| 72 | return nullptr; |
| 73 | |
| 74 | Vector<MimeClassInfo> mimes; |
| 75 | Vector<size_t> mimePluginIndices; |
| 76 | data->getWebVisibleMimesAndPluginIndices(mimes, mimePluginIndices); |
| 77 | for (unsigned i = 0; i < mimes.size(); ++i) { |
| 78 | if (mimes[i].type == propertyName) |
| 79 | return DOMMimeType::create(data, frame(), i); |
| 80 | } |
| 81 | return nullptr; |
| 82 | } |
| 83 | |
| 84 | Vector<AtomicString> DOMMimeTypeArray::supportedPropertyNames() |
| 85 | { |
| 86 | PluginData* data = getPluginData(); |
| 87 | if (!data) |
| 88 | return { }; |
| 89 | |
| 90 | Vector<MimeClassInfo> mimes; |
| 91 | Vector<size_t> mimePluginIndices; |
| 92 | data->getWebVisibleMimesAndPluginIndices(mimes, mimePluginIndices); |
| 93 | |
| 94 | Vector<AtomicString> result; |
| 95 | result.reserveInitialCapacity(mimes.size()); |
| 96 | for (auto& info : mimes) |
| 97 | result.uncheckedAppend(WTFMove(info.type)); |
| 98 | |
| 99 | return result; |
| 100 | } |
| 101 | |
| 102 | PluginData* DOMMimeTypeArray::getPluginData() const |
| 103 | { |
| 104 | auto* frame = this->frame(); |
| 105 | if (!frame) |
| 106 | return nullptr; |
| 107 | |
| 108 | auto* page = frame->page(); |
| 109 | if (!page) |
| 110 | return nullptr; |
| 111 | |
| 112 | return &page->pluginData(); |
| 113 | } |
| 114 | |
| 115 | } // namespace WebCore |
| 116 | |