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
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.
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 "nsIPluginHost.h"
46 #include "nsIDocShell.h"
47 #include "nsIWebNavigation.h"
48 #include "nsDOMClassInfo.h"
49 #include "nsPluginError.h"
50 #include "nsContentUtils.h"
52 nsPluginArray::nsPluginArray(nsNavigator
* navigator
,
53 nsIDocShell
*aDocShell
)
56 mNavigator
= navigator
; // don't ADDREF here, needed for parent of script object.
57 mPluginHost
= do_GetService(MOZ_PLUGIN_HOST_CONTRACTID
, &rv
);
59 mPluginArray
= nsnull
;
60 mDocShell
= aDocShell
;
63 nsPluginArray::~nsPluginArray()
65 if (mPluginArray
!= nsnull
) {
66 for (PRUint32 i
= 0; i
< mPluginCount
; i
++) {
67 NS_IF_RELEASE(mPluginArray
[i
]);
69 delete[] mPluginArray
;
73 DOMCI_DATA(PluginArray
, nsPluginArray
)
75 // QueryInterface implementation for nsPluginArray
76 NS_INTERFACE_MAP_BEGIN(nsPluginArray
)
77 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports
, nsIDOMPluginArray
)
78 NS_INTERFACE_MAP_ENTRY(nsIDOMPluginArray
)
79 NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(PluginArray
)
82 NS_IMPL_ADDREF(nsPluginArray
)
83 NS_IMPL_RELEASE(nsPluginArray
)
86 nsPluginArray::GetLength(PRUint32
* aLength
)
88 if (AllowPlugins() && mPluginHost
)
89 return mPluginHost
->GetPluginCount(aLength
);
96 nsPluginArray::AllowPlugins()
98 PRBool allowPlugins
= PR_FALSE
;
100 if (NS_FAILED(mDocShell
->GetAllowPlugins(&allowPlugins
)))
101 allowPlugins
= PR_FALSE
;
107 nsPluginArray::GetItemAt(PRUint32 aIndex
, nsresult
* aResult
)
114 if (mPluginArray
== nsnull
) {
115 *aResult
= GetPlugins();
116 if (*aResult
!= NS_OK
)
120 return aIndex
< mPluginCount
? mPluginArray
[aIndex
] : nsnull
;
124 nsPluginArray::Item(PRUint32 aIndex
, nsIDOMPlugin
** aReturn
)
128 NS_IF_ADDREF(*aReturn
= GetItemAt(aIndex
, &rv
));
134 nsPluginArray::GetNamedItem(const nsAString
& aName
, nsresult
* aResult
)
141 if (mPluginArray
== nsnull
) {
142 *aResult
= GetPlugins();
143 if (*aResult
!= NS_OK
)
147 for (PRUint32 i
= 0; i
< mPluginCount
; i
++) {
148 nsAutoString pluginName
;
149 nsIDOMPlugin
* plugin
= mPluginArray
[i
];
150 if (plugin
->GetName(pluginName
) == NS_OK
&& pluginName
.Equals(aName
)) {
159 nsPluginArray::NamedItem(const nsAString
& aName
, nsIDOMPlugin
** aReturn
)
161 NS_PRECONDITION(nsnull
!= aReturn
, "null arg");
165 NS_IF_ADDREF(*aReturn
= GetNamedItem(aName
, &rv
));
171 nsPluginArray::GetPluginHost(nsIPluginHost
** aPluginHost
)
173 NS_ENSURE_ARG_POINTER(aPluginHost
);
178 mPluginHost
= do_GetService(MOZ_PLUGIN_HOST_CONTRACTID
, &rv
);
185 *aPluginHost
= mPluginHost
;
186 NS_IF_ADDREF(*aPluginHost
);
192 nsPluginArray::SetDocShell(nsIDocShell
*aDocShell
)
194 mDocShell
= aDocShell
;
198 nsPluginArray::Invalidate()
205 nsPluginArray::Refresh(PRBool aReloadDocuments
)
207 nsresult res
= NS_OK
;
209 return NS_SUCCESS_LOSS_OF_INSIGNIFICANT_DATA
;
212 mPluginHost
= do_GetService(MOZ_PLUGIN_HOST_CONTRACTID
, &res
);
219 // NS_ERROR_PLUGINS_PLUGINSNOTCHANGED on reloading plugins indicates
220 // that plugins did not change and was not reloaded
221 PRBool pluginsNotChanged
= PR_FALSE
;
223 pluginsNotChanged
= (NS_ERROR_PLUGINS_PLUGINSNOTCHANGED
== mPluginHost
->ReloadPlugins(aReloadDocuments
));
225 // no need to reload the page if plugins have not been changed
226 // in fact, if we do reload we can hit recursive load problem, see bug 93351
227 if(pluginsNotChanged
)
230 nsCOMPtr
<nsIWebNavigation
> webNav
= do_QueryInterface(mDocShell
);
232 if (mPluginArray
!= nsnull
) {
233 for (PRUint32 i
= 0; i
< mPluginCount
; i
++)
234 NS_IF_RELEASE(mPluginArray
[i
]);
236 delete[] mPluginArray
;
240 mPluginArray
= nsnull
;
243 mNavigator
->RefreshMIMEArray();
245 if (aReloadDocuments
&& webNav
)
246 webNav
->Reload(nsIWebNavigation::LOAD_FLAGS_NONE
);
252 nsPluginArray::GetPlugins()
254 nsresult rv
= GetLength(&mPluginCount
);
255 if (NS_SUCCEEDED(rv
)) {
256 mPluginArray
= new nsIDOMPlugin
*[mPluginCount
];
258 return NS_ERROR_OUT_OF_MEMORY
;
263 rv
= mPluginHost
->GetPlugins(mPluginCount
, mPluginArray
);
264 if (NS_SUCCEEDED(rv
)) {
265 // need to wrap each of these with a nsPluginElement, which
267 for (PRUint32 i
= 0; i
< mPluginCount
; i
++) {
268 nsIDOMPlugin
* wrapper
= new nsPluginElement(mPluginArray
[i
]);
269 NS_IF_ADDREF(wrapper
);
270 mPluginArray
[i
] = wrapper
;
273 /* XXX this code is all broken. If GetPlugins fails, there's no contract
274 * explaining what should happen. Instead of deleting elements in an
275 * array of random pointers, we mark the array as 0 length.
285 nsPluginElement::nsPluginElement(nsIDOMPlugin
* plugin
)
287 mPlugin
= plugin
; // don't AddRef, see nsPluginArray::Item.
289 mMimeTypeArray
= nsnull
;
292 nsPluginElement::~nsPluginElement()
294 NS_IF_RELEASE(mPlugin
);
296 if (mMimeTypeArray
!= nsnull
) {
297 for (PRUint32 i
= 0; i
< mMimeTypeCount
; i
++) {
298 nsMimeType
* mt
= static_cast<nsMimeType
*>(mMimeTypeArray
[i
]);
304 delete[] mMimeTypeArray
;
309 DOMCI_DATA(Plugin
, nsPluginElement
)
311 // QueryInterface implementation for nsPluginElement
312 NS_INTERFACE_MAP_BEGIN(nsPluginElement
)
313 NS_INTERFACE_MAP_ENTRY(nsISupports
)
314 NS_INTERFACE_MAP_ENTRY(nsIDOMPlugin
)
315 NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(Plugin
)
319 NS_IMPL_ADDREF(nsPluginElement
)
320 NS_IMPL_RELEASE(nsPluginElement
)
324 nsPluginElement::GetDescription(nsAString
& aDescription
)
326 return mPlugin
->GetDescription(aDescription
);
330 nsPluginElement::GetFilename(nsAString
& aFilename
)
332 return mPlugin
->GetFilename(aFilename
);
336 nsPluginElement::GetVersion(nsAString
& aVersion
)
338 return mPlugin
->GetVersion(aVersion
);
342 nsPluginElement::GetName(nsAString
& aName
)
344 return mPlugin
->GetName(aName
);
348 nsPluginElement::GetLength(PRUint32
* aLength
)
350 return mPlugin
->GetLength(aLength
);
354 nsPluginElement::GetItemAt(PRUint32 aIndex
, nsresult
*aResult
)
356 if (mMimeTypeArray
== nsnull
) {
357 *aResult
= GetMimeTypes();
358 if (*aResult
!= NS_OK
)
362 if (aIndex
>= mMimeTypeCount
) {
363 *aResult
= NS_ERROR_FAILURE
;
370 return mMimeTypeArray
[aIndex
];
374 nsPluginElement::Item(PRUint32 aIndex
, nsIDOMMimeType
** aReturn
)
378 NS_IF_ADDREF(*aReturn
= GetItemAt(aIndex
, &rv
));
384 nsPluginElement::GetNamedItem(const nsAString
& aName
, nsresult
*aResult
)
386 if (mMimeTypeArray
== nsnull
) {
387 *aResult
= GetMimeTypes();
388 if (*aResult
!= NS_OK
)
393 for (PRUint32 i
= 0; i
< mMimeTypeCount
; i
++) {
395 nsIDOMMimeType
* mimeType
= mMimeTypeArray
[i
];
396 if (mimeType
->GetType(type
) == NS_OK
&& type
.Equals(aName
)) {
405 nsPluginElement::NamedItem(const nsAString
& aName
, nsIDOMMimeType
** aReturn
)
409 NS_IF_ADDREF(*aReturn
= GetNamedItem(aName
, &rv
));
415 nsPluginElement::GetMimeTypes()
417 nsresult rv
= mPlugin
->GetLength(&mMimeTypeCount
);
419 mMimeTypeArray
= new nsIDOMMimeType
*[mMimeTypeCount
];
420 if (mMimeTypeArray
== nsnull
)
421 return NS_ERROR_OUT_OF_MEMORY
;
422 for (PRUint32 i
= 0; i
< mMimeTypeCount
; i
++) {
423 nsCOMPtr
<nsIDOMMimeType
> mimeType
;
424 rv
= mPlugin
->Item(i
, getter_AddRefs(mimeType
));
427 mimeType
= new nsMimeType(this, mimeType
);
428 NS_IF_ADDREF(mMimeTypeArray
[i
] = mimeType
);