Bug 464858 - intermittent / recurring fail of test_db_update_v1.js. bustagefix.
[wine-gecko.git] / dom / src / base / nsPluginArray.cpp
blob80ef3b574c3daa0cccd5e97eae0c991b903d2291
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Pierre Phaneuf <pp@ludusdesign.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsPluginArray.h"
40 #include "nsMimeTypeArray.h"
41 #include "nsGlobalWindow.h"
42 #include "nsIScriptGlobalObject.h"
43 #include "nsIDOMNavigator.h"
44 #include "nsIDOMMimeType.h"
45 #include "nsIServiceManager.h"
46 #include "nsIPluginHost.h"
47 #include "nsIDocShell.h"
48 #include "nsIWebNavigation.h"
49 #include "nsDOMClassInfo.h"
50 #include "nsPluginError.h"
51 #include "nsIComponentRegistrar.h"
52 #include "nsContentUtils.h"
54 static NS_DEFINE_CID(kPluginManagerCID, NS_PLUGINMANAGER_CID);
56 nsPluginArray::nsPluginArray(nsNavigator* navigator,
57 nsIDocShell *aDocShell)
59 nsresult rv;
60 mNavigator = navigator; // don't ADDREF here, needed for parent of script object.
61 mPluginHost = do_GetService(kPluginManagerCID, &rv);
62 mPluginCount = 0;
63 mPluginArray = nsnull;
64 mDocShell = aDocShell;
67 nsPluginArray::~nsPluginArray()
69 if (mPluginArray != nsnull) {
70 for (PRUint32 i = 0; i < mPluginCount; i++) {
71 NS_IF_RELEASE(mPluginArray[i]);
73 delete[] mPluginArray;
77 // QueryInterface implementation for nsPluginArray
78 NS_INTERFACE_MAP_BEGIN(nsPluginArray)
79 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMPluginArray)
80 NS_INTERFACE_MAP_ENTRY(nsIDOMPluginArray)
81 NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(PluginArray)
82 NS_INTERFACE_MAP_END
84 NS_IMPL_ADDREF(nsPluginArray)
85 NS_IMPL_RELEASE(nsPluginArray)
87 NS_IMETHODIMP
88 nsPluginArray::GetLength(PRUint32* aLength)
90 if (AllowPlugins() && mPluginHost)
91 return mPluginHost->GetPluginCount(aLength);
93 *aLength = 0;
94 return NS_OK;
97 PRBool
98 nsPluginArray::AllowPlugins()
100 PRBool allowPlugins = PR_FALSE;
101 if (mDocShell)
102 if (NS_FAILED(mDocShell->GetAllowPlugins(&allowPlugins)))
103 allowPlugins = PR_FALSE;
105 return allowPlugins;
108 nsIDOMPlugin*
109 nsPluginArray::GetItemAt(PRUint32 aIndex, nsresult* aResult)
111 *aResult = NS_OK;
113 if (!AllowPlugins())
114 return nsnull;
116 if (mPluginArray == nsnull) {
117 *aResult = GetPlugins();
118 if (*aResult != NS_OK)
119 return nsnull;
122 return aIndex < mPluginCount ? mPluginArray[aIndex] : nsnull;
125 NS_IMETHODIMP
126 nsPluginArray::Item(PRUint32 aIndex, nsIDOMPlugin** aReturn)
128 nsresult rv;
130 NS_IF_ADDREF(*aReturn = GetItemAt(aIndex, &rv));
132 return rv;
135 nsIDOMPlugin*
136 nsPluginArray::GetNamedItem(const nsAString& aName, nsresult* aResult)
138 *aResult = NS_OK;
140 if (!AllowPlugins())
141 return nsnull;
143 if (mPluginArray == nsnull) {
144 *aResult = GetPlugins();
145 if (*aResult != NS_OK)
146 return nsnull;
149 for (PRUint32 i = 0; i < mPluginCount; i++) {
150 nsAutoString pluginName;
151 nsIDOMPlugin* plugin = mPluginArray[i];
152 if (plugin->GetName(pluginName) == NS_OK && pluginName.Equals(aName)) {
153 return plugin;
157 return nsnull;
160 NS_IMETHODIMP
161 nsPluginArray::NamedItem(const nsAString& aName, nsIDOMPlugin** aReturn)
163 NS_PRECONDITION(nsnull != aReturn, "null arg");
165 nsresult rv;
167 NS_IF_ADDREF(*aReturn = GetNamedItem(aName, &rv));
169 return rv;
172 nsresult
173 nsPluginArray::GetPluginHost(nsIPluginHost** aPluginHost)
175 NS_ENSURE_ARG_POINTER(aPluginHost);
177 nsresult rv = NS_OK;
179 if (!mPluginHost) {
180 mPluginHost = do_GetService(kPluginManagerCID, &rv);
182 if (NS_FAILED(rv)) {
183 return rv;
187 *aPluginHost = mPluginHost;
188 NS_IF_ADDREF(*aPluginHost);
190 return rv;
193 void
194 nsPluginArray::SetDocShell(nsIDocShell* aDocShell)
196 mDocShell = aDocShell;
199 NS_IMETHODIMP
200 nsPluginArray::Refresh(PRBool aReloadDocuments)
202 nsresult res = NS_OK;
203 if (!AllowPlugins())
204 return NS_SUCCESS_LOSS_OF_INSIGNIFICANT_DATA;
206 // refresh the component registry first, see bug 87913
207 nsCOMPtr<nsIServiceManager> servManager;
208 NS_GetServiceManager(getter_AddRefs(servManager));
209 nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servManager);
210 if (registrar)
211 registrar->AutoRegister(nsnull);
213 if (!mPluginHost) {
214 mPluginHost = do_GetService(kPluginManagerCID, &res);
217 if(NS_FAILED(res)) {
218 return res;
221 nsCOMPtr<nsIPluginManager> pm(do_QueryInterface(mPluginHost));
223 // NS_ERROR_PLUGINS_PLUGINSNOTCHANGED on reloading plugins indicates
224 // that plugins did not change and was not reloaded
225 PRBool pluginsNotChanged = PR_FALSE;
226 if(pm)
227 pluginsNotChanged = (NS_ERROR_PLUGINS_PLUGINSNOTCHANGED == pm->ReloadPlugins(aReloadDocuments));
229 // no need to reload the page if plugins have not been changed
230 // in fact, if we do reload we can hit recursive load problem, see bug 93351
231 if(pluginsNotChanged)
232 return res;
234 nsCOMPtr<nsIWebNavigation> webNav = do_QueryInterface(mDocShell);
236 if (mPluginArray != nsnull) {
237 for (PRUint32 i = 0; i < mPluginCount; i++)
238 NS_IF_RELEASE(mPluginArray[i]);
240 delete[] mPluginArray;
243 mPluginCount = 0;
244 mPluginArray = nsnull;
246 if (mNavigator)
247 mNavigator->RefreshMIMEArray();
249 if (aReloadDocuments && webNav)
250 webNav->Reload(nsIWebNavigation::LOAD_FLAGS_NONE);
252 return res;
255 nsresult
256 nsPluginArray::GetPlugins()
258 nsresult rv = GetLength(&mPluginCount);
259 if (NS_SUCCEEDED(rv)) {
260 mPluginArray = new nsIDOMPlugin*[mPluginCount];
261 if (!mPluginArray)
262 return NS_ERROR_OUT_OF_MEMORY;
264 if (!mPluginCount)
265 return NS_OK;
267 rv = mPluginHost->GetPlugins(mPluginCount, mPluginArray);
268 if (NS_SUCCEEDED(rv)) {
269 // need to wrap each of these with a nsPluginElement, which
270 // is scriptable.
271 for (PRUint32 i = 0; i < mPluginCount; i++) {
272 nsIDOMPlugin* wrapper = new nsPluginElement(mPluginArray[i]);
273 NS_IF_ADDREF(wrapper);
274 mPluginArray[i] = wrapper;
276 } else {
277 /* XXX this code is all broken. If GetPlugins fails, there's no contract
278 * explaining what should happen. Instead of deleting elements in an
279 * array of random pointers, we mark the array as 0 length.
281 mPluginCount = 0;
284 return rv;
289 nsPluginElement::nsPluginElement(nsIDOMPlugin* plugin)
291 mPlugin = plugin; // don't AddRef, see nsPluginArray::Item.
292 mMimeTypeCount = 0;
293 mMimeTypeArray = nsnull;
296 nsPluginElement::~nsPluginElement()
298 NS_IF_RELEASE(mPlugin);
300 if (mMimeTypeArray != nsnull) {
301 for (PRUint32 i = 0; i < mMimeTypeCount; i++)
302 NS_IF_RELEASE(mMimeTypeArray[i]);
303 delete[] mMimeTypeArray;
308 // QueryInterface implementation for nsPluginElement
309 NS_INTERFACE_MAP_BEGIN(nsPluginElement)
310 NS_INTERFACE_MAP_ENTRY(nsISupports)
311 NS_INTERFACE_MAP_ENTRY(nsIDOMPlugin)
312 NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(Plugin)
313 NS_INTERFACE_MAP_END
316 NS_IMPL_ADDREF(nsPluginElement)
317 NS_IMPL_RELEASE(nsPluginElement)
320 NS_IMETHODIMP
321 nsPluginElement::GetDescription(nsAString& aDescription)
323 return mPlugin->GetDescription(aDescription);
326 NS_IMETHODIMP
327 nsPluginElement::GetFilename(nsAString& aFilename)
329 return mPlugin->GetFilename(aFilename);
332 NS_IMETHODIMP
333 nsPluginElement::GetName(nsAString& aName)
335 return mPlugin->GetName(aName);
338 NS_IMETHODIMP
339 nsPluginElement::GetLength(PRUint32* aLength)
341 return mPlugin->GetLength(aLength);
344 nsIDOMMimeType*
345 nsPluginElement::GetItemAt(PRUint32 aIndex, nsresult *aResult)
347 if (mMimeTypeArray == nsnull) {
348 *aResult = GetMimeTypes();
349 if (*aResult != NS_OK)
350 return nsnull;
353 if (aIndex >= mMimeTypeCount) {
354 *aResult = NS_ERROR_FAILURE;
356 return nsnull;
359 *aResult = NS_OK;
361 return mMimeTypeArray[aIndex];
364 NS_IMETHODIMP
365 nsPluginElement::Item(PRUint32 aIndex, nsIDOMMimeType** aReturn)
367 nsresult rv;
369 NS_IF_ADDREF(*aReturn = GetItemAt(aIndex, &rv));
371 return rv;
374 nsIDOMMimeType*
375 nsPluginElement::GetNamedItem(const nsAString& aName, nsresult *aResult)
377 if (mMimeTypeArray == nsnull) {
378 *aResult = GetMimeTypes();
379 if (*aResult != NS_OK)
380 return nsnull;
383 *aResult = NS_OK;
384 for (PRUint32 i = 0; i < mMimeTypeCount; i++) {
385 nsAutoString type;
386 nsIDOMMimeType* mimeType = mMimeTypeArray[i];
387 if (mimeType->GetType(type) == NS_OK && type.Equals(aName)) {
388 return mimeType;
392 return nsnull;
395 NS_IMETHODIMP
396 nsPluginElement::NamedItem(const nsAString& aName, nsIDOMMimeType** aReturn)
398 nsresult rv;
400 NS_IF_ADDREF(*aReturn = GetNamedItem(aName, &rv));
402 return rv;
405 nsresult
406 nsPluginElement::GetMimeTypes()
408 nsresult rv = mPlugin->GetLength(&mMimeTypeCount);
409 if (rv == NS_OK) {
410 mMimeTypeArray = new nsIDOMMimeType*[mMimeTypeCount];
411 if (mMimeTypeArray == nsnull)
412 return NS_ERROR_OUT_OF_MEMORY;
413 for (PRUint32 i = 0; i < mMimeTypeCount; i++) {
414 nsCOMPtr<nsIDOMMimeType> mimeType;
415 rv = mPlugin->Item(i, getter_AddRefs(mimeType));
416 if (rv != NS_OK)
417 break;
418 mimeType = new nsMimeType(this, mimeType);
419 NS_IF_ADDREF(mMimeTypeArray[i] = mimeType);
422 return rv;