View Javadoc
1   /*
2    * Copyright (c) 2002-2026 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.javascript.host;
16  
17  import java.util.ArrayList;
18  import java.util.List;
19  
20  import org.htmlunit.corejs.javascript.Scriptable;
21  import org.htmlunit.javascript.HtmlUnitScriptable;
22  import org.htmlunit.javascript.JavaScriptEngine;
23  import org.htmlunit.javascript.configuration.JsxClass;
24  import org.htmlunit.javascript.configuration.JsxConstructor;
25  import org.htmlunit.javascript.configuration.JsxFunction;
26  import org.htmlunit.javascript.configuration.JsxGetter;
27  import org.htmlunit.javascript.configuration.JsxSymbol;
28  
29  /**
30   * JavaScript host object for {@code Plugin}.
31   *
32   * @author Marc Guillemot
33   * @author Ahmed Ashour
34   * @author Ronald Brill
35   *
36   * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/Plugin">MDN Documentation</a>
37   */
38  @JsxClass
39  public class Plugin extends HtmlUnitScriptable {
40      private String description_;
41      private String filename_;
42      private String name_;
43  
44      private final List<MimeType> elements_ = new ArrayList<>();
45  
46      /**
47       * Creates an instance.
48       */
49      public Plugin() {
50          super();
51      }
52  
53      /**
54       * Creates an instance of this object.
55       */
56      @JsxConstructor
57      public void jsConstructor() {
58          // nothing to do
59      }
60  
61      /**
62       * Creates an instance with the given properties.
63       *
64       * @param name the plugin name
65       * @param description the plugin description
66       * @param filename the plugin filename
67       */
68      public Plugin(final String name, final String description, final String filename) {
69          super();
70          name_ = name;
71          description_ = description;
72          filename_ = filename;
73      }
74  
75      /**
76       * Returns the {@link MimeType} at the given index.
77       *
78       * @param index the index
79       * @return the {@link MimeType} at the given position, or {@code null} if out of range
80       */
81      @JsxFunction
82      public MimeType item(final int index) {
83          if (index >= 0 && index < elements_.size()) {
84              return elements_.get(index);
85          }
86          return null;
87      }
88  
89      /**
90       * {@inheritDoc}
91       */
92      @Override
93      protected Object getWithPreemption(final String name) {
94          final MimeType response = namedItem(name);
95          if (response != null) {
96              return response;
97          }
98          return NOT_FOUND;
99      }
100 
101     /**
102      * Returns the element at the specified index, or {@link #NOT_FOUND} if the index is invalid.
103      * {@inheritDoc}
104      */
105     @Override
106     public final Object get(final int index, final Scriptable start) {
107         final Plugin plugin = (Plugin) start;
108         final List<MimeType> elements = plugin.elements_;
109 
110         if (index >= 0 && index < elements.size()) {
111             return elements.get(index);
112         }
113         return NOT_FOUND;
114     }
115 
116     /**
117      * Returns the {@link MimeType} with the given MIME type string.
118      *
119      * @param name the MIME type string to look up
120      * @return the matching {@link MimeType}, or {@code null} if not found
121      */
122     @JsxFunction
123     public MimeType namedItem(final String name) {
124         for (final MimeType element : elements_) {
125             if (name.equals(element.getType())) {
126                 return element;
127             }
128         }
129         return null;
130     }
131 
132     /**
133      * Returns the number of MIME types supported by this plugin.
134      *
135      * @return the number of elements
136      */
137     @JsxGetter
138     public int getLength() {
139         return elements_.size();
140     }
141 
142     /**
143      * Adds a {@link MimeType} to this plugin.
144      *
145      * @param element the element to add
146      */
147     void add(final MimeType element) {
148         elements_.add(element);
149     }
150 
151     /**
152      * Returns the plugin description.
153      *
154      * @return the description
155      */
156     @JsxGetter
157     public String getDescription() {
158         return description_;
159     }
160 
161     /**
162      * Returns the plugin filename.
163      *
164      * @return the filename
165      */
166     @JsxGetter
167     public String getFilename() {
168         return filename_;
169     }
170 
171     /**
172      * Returns the plugin name.
173      *
174      * @return the name
175      */
176     @JsxGetter
177     public String getName() {
178         return name_;
179     }
180 
181     /**
182      * Returns an iterator over the values in this plugin.
183      *
184      * @return the iterator
185      */
186     @JsxSymbol
187     public Scriptable iterator() {
188         return JavaScriptEngine.newArrayIteratorTypeValues(getParentScope(), this);
189     }
190 }