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 "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
)
60 mNavigator
= navigator
; // don't ADDREF here, needed for parent of script object.
61 mPluginHost
= do_GetService(kPluginManagerCID
, &rv
);
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
)
84 NS_IMPL_ADDREF(nsPluginArray
)
85 NS_IMPL_RELEASE(nsPluginArray
)
88 nsPluginArray::GetLength(PRUint32
* aLength
)
90 if (AllowPlugins() && mPluginHost
)
91 return mPluginHost
->GetPluginCount(aLength
);
98 nsPluginArray::AllowPlugins()
100 PRBool allowPlugins
= PR_FALSE
;
102 if (NS_FAILED(mDocShell
->GetAllowPlugins(&allowPlugins
)))
103 allowPlugins
= PR_FALSE
;
109 nsPluginArray::GetItemAt(PRUint32 aIndex
, nsresult
* aResult
)
116 if (mPluginArray
== nsnull
) {
117 *aResult
= GetPlugins();
118 if (*aResult
!= NS_OK
)
122 return aIndex
< mPluginCount
? mPluginArray
[aIndex
] : nsnull
;
126 nsPluginArray::Item(PRUint32 aIndex
, nsIDOMPlugin
** aReturn
)
130 NS_IF_ADDREF(*aReturn
= GetItemAt(aIndex
, &rv
));
136 nsPluginArray::GetNamedItem(const nsAString
& aName
, nsresult
* aResult
)
143 if (mPluginArray
== nsnull
) {
144 *aResult
= GetPlugins();
145 if (*aResult
!= NS_OK
)
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
)) {
161 nsPluginArray::NamedItem(const nsAString
& aName
, nsIDOMPlugin
** aReturn
)
163 NS_PRECONDITION(nsnull
!= aReturn
, "null arg");
167 NS_IF_ADDREF(*aReturn
= GetNamedItem(aName
, &rv
));
173 nsPluginArray::GetPluginHost(nsIPluginHost
** aPluginHost
)
175 NS_ENSURE_ARG_POINTER(aPluginHost
);
180 mPluginHost
= do_GetService(kPluginManagerCID
, &rv
);
187 *aPluginHost
= mPluginHost
;
188 NS_IF_ADDREF(*aPluginHost
);
194 nsPluginArray::SetDocShell(nsIDocShell
* aDocShell
)
196 mDocShell
= aDocShell
;
200 nsPluginArray::Refresh(PRBool aReloadDocuments
)
202 nsresult res
= NS_OK
;
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
);
211 registrar
->AutoRegister(nsnull
);
214 mPluginHost
= do_GetService(kPluginManagerCID
, &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
;
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
)
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
;
244 mPluginArray
= nsnull
;
247 mNavigator
->RefreshMIMEArray();
249 if (aReloadDocuments
&& webNav
)
250 webNav
->Reload(nsIWebNavigation::LOAD_FLAGS_NONE
);
256 nsPluginArray::GetPlugins()
258 nsresult rv
= GetLength(&mPluginCount
);
259 if (NS_SUCCEEDED(rv
)) {
260 mPluginArray
= new nsIDOMPlugin
*[mPluginCount
];
262 return NS_ERROR_OUT_OF_MEMORY
;
267 rv
= mPluginHost
->GetPlugins(mPluginCount
, mPluginArray
);
268 if (NS_SUCCEEDED(rv
)) {
269 // need to wrap each of these with a nsPluginElement, which
271 for (PRUint32 i
= 0; i
< mPluginCount
; i
++) {
272 nsIDOMPlugin
* wrapper
= new nsPluginElement(mPluginArray
[i
]);
273 NS_IF_ADDREF(wrapper
);
274 mPluginArray
[i
] = wrapper
;
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.
289 nsPluginElement::nsPluginElement(nsIDOMPlugin
* plugin
)
291 mPlugin
= plugin
; // don't AddRef, see nsPluginArray::Item.
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
)
316 NS_IMPL_ADDREF(nsPluginElement
)
317 NS_IMPL_RELEASE(nsPluginElement
)
321 nsPluginElement::GetDescription(nsAString
& aDescription
)
323 return mPlugin
->GetDescription(aDescription
);
327 nsPluginElement::GetFilename(nsAString
& aFilename
)
329 return mPlugin
->GetFilename(aFilename
);
333 nsPluginElement::GetName(nsAString
& aName
)
335 return mPlugin
->GetName(aName
);
339 nsPluginElement::GetLength(PRUint32
* aLength
)
341 return mPlugin
->GetLength(aLength
);
345 nsPluginElement::GetItemAt(PRUint32 aIndex
, nsresult
*aResult
)
347 if (mMimeTypeArray
== nsnull
) {
348 *aResult
= GetMimeTypes();
349 if (*aResult
!= NS_OK
)
353 if (aIndex
>= mMimeTypeCount
) {
354 *aResult
= NS_ERROR_FAILURE
;
361 return mMimeTypeArray
[aIndex
];
365 nsPluginElement::Item(PRUint32 aIndex
, nsIDOMMimeType
** aReturn
)
369 NS_IF_ADDREF(*aReturn
= GetItemAt(aIndex
, &rv
));
375 nsPluginElement::GetNamedItem(const nsAString
& aName
, nsresult
*aResult
)
377 if (mMimeTypeArray
== nsnull
) {
378 *aResult
= GetMimeTypes();
379 if (*aResult
!= NS_OK
)
384 for (PRUint32 i
= 0; i
< mMimeTypeCount
; i
++) {
386 nsIDOMMimeType
* mimeType
= mMimeTypeArray
[i
];
387 if (mimeType
->GetType(type
) == NS_OK
&& type
.Equals(aName
)) {
396 nsPluginElement::NamedItem(const nsAString
& aName
, nsIDOMMimeType
** aReturn
)
400 NS_IF_ADDREF(*aReturn
= GetNamedItem(aName
, &rv
));
406 nsPluginElement::GetMimeTypes()
408 nsresult rv
= mPlugin
->GetLength(&mMimeTypeCount
);
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
));
418 mimeType
= new nsMimeType(this, mimeType
);
419 NS_IF_ADDREF(mMimeTypeArray
[i
] = mimeType
);