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