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/text/AtomicString.h>
28
29#if ENABLE(WEB_REPLAY)
30#include "Document.h"
31#include "WebReplayInputs.h"
32#include <replay/InputCursor.h>
33#endif
34
35namespace WebCore {
36
37DOMMimeTypeArray::DOMMimeTypeArray(Frame* frame)
38    : DOMWindowProperty(frame)
39{
40}
41
42DOMMimeTypeArray::~DOMMimeTypeArray()
43{
44}
45
46unsigned DOMMimeTypeArray::length() const
47{
48    PluginData* data = getPluginData();
49    if (!data)
50        return 0;
51    return data->mimes().size();
52}
53
54PassRefPtr<DOMMimeType> DOMMimeTypeArray::item(unsigned index)
55{
56    PluginData* data = getPluginData();
57    if (!data)
58        return 0;
59    const Vector<MimeClassInfo>& mimes = data->mimes();
60    if (index >= mimes.size())
61        return 0;
62    return DOMMimeType::create(data, m_frame, index).get();
63}
64
65bool DOMMimeTypeArray::canGetItemsForName(const AtomicString& propertyName)
66{
67    PluginData *data = getPluginData();
68    if (!data)
69        return 0;
70    const Vector<MimeClassInfo>& mimes = data->mimes();
71    for (unsigned i = 0; i < mimes.size(); ++i) {
72        if (mimes[i].type == propertyName)
73            return true;
74    }
75    return false;
76}
77
78PassRefPtr<DOMMimeType> DOMMimeTypeArray::namedItem(const AtomicString& propertyName)
79{
80    PluginData *data = getPluginData();
81    if (!data)
82        return 0;
83    const Vector<MimeClassInfo>& mimes = data->mimes();
84    for (unsigned i = 0; i < mimes.size(); ++i) {
85        if (mimes[i].type == propertyName)
86            return DOMMimeType::create(data, m_frame, i).get();
87    }
88    return 0;
89}
90
91PluginData* DOMMimeTypeArray::getPluginData() const
92{
93    if (!m_frame)
94        return nullptr;
95
96    Page* page = m_frame->page();
97    if (!page)
98        return nullptr;
99
100    PluginData* pluginData = &page->pluginData();
101
102#if ENABLE(WEB_REPLAY)
103    if (!m_frame->document())
104        return pluginData;
105
106    InputCursor& cursor = m_frame->document()->inputCursor();
107    if (cursor.isCapturing())
108        cursor.appendInput<FetchPluginData>(pluginData);
109    else if (cursor.isReplaying()) {
110        if (FetchPluginData* input = cursor.fetchInput<FetchPluginData>())
111            pluginData = input->pluginData().get();
112    }
113#endif
114
115    return pluginData;
116}
117
118} // namespace WebCore
119