1/*
2 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
3 * Copyright (C) 2008, 2015 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/IsoMallocInlines.h>
28#include <wtf/text/AtomString.h>
29
30namespace WebCore {
31
32WTF_MAKE_ISO_ALLOCATED_IMPL(DOMPluginArray);
33
34DOMPluginArray::DOMPluginArray(DOMWindow* window)
35 : DOMWindowProperty(window)
36{
37}
38
39DOMPluginArray::~DOMPluginArray() = default;
40
41unsigned DOMPluginArray::length() const
42{
43 PluginData* data = pluginData();
44 if (!data)
45 return 0;
46
47 return data->publiclyVisiblePlugins().size();
48}
49
50RefPtr<DOMPlugin> DOMPluginArray::item(unsigned index)
51{
52 PluginData* data = pluginData();
53 if (!data)
54 return nullptr;
55
56 const Vector<PluginInfo>& plugins = data->publiclyVisiblePlugins();
57 if (index >= plugins.size())
58 return nullptr;
59 return DOMPlugin::create(data, frame(), plugins[index]);
60}
61
62RefPtr<DOMPlugin> DOMPluginArray::namedItem(const AtomString& propertyName)
63{
64 PluginData* data = pluginData();
65 if (!data)
66 return nullptr;
67
68 for (auto& plugin : data->webVisiblePlugins()) {
69 if (plugin.name == propertyName)
70 return DOMPlugin::create(data, frame(), plugin);
71 }
72 return nullptr;
73}
74
75Vector<AtomString> DOMPluginArray::supportedPropertyNames()
76{
77 PluginData* data = pluginData();
78 if (!data)
79 return { };
80
81 const auto& plugins = data->publiclyVisiblePlugins();
82
83 Vector<AtomString> result;
84 result.reserveInitialCapacity(plugins.size());
85 for (auto& plugin : plugins)
86 result.uncheckedAppend(plugin.name);
87
88 return result;
89}
90
91void DOMPluginArray::refresh(bool reloadPages)
92{
93 auto* frame = this->frame();
94 if (!frame)
95 return;
96
97 if (!frame->page())
98 return;
99
100 Page::refreshPlugins(reloadPages);
101}
102
103PluginData* DOMPluginArray::pluginData() const
104{
105 auto* frame = this->frame();
106 if (!frame)
107 return nullptr;
108
109 Page* page = frame->page();
110 if (!page)
111 return nullptr;
112
113 return &page->pluginData();
114}
115
116} // namespace WebCore
117