Fix register allocation bug in return values (bug 604996, r=dmandelin).
[mozilla-central.git] / dom / base / nsPluginArray.cpp
blob6fe539e0af9544e8823f30cfd94c29ac9bd2fb42
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 "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)
55 nsresult rv;
56 mNavigator = navigator; // don't ADDREF here, needed for parent of script object.
57 mPluginHost = do_GetService(MOZ_PLUGIN_HOST_CONTRACTID, &rv);
58 mPluginCount = 0;
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)
80 NS_INTERFACE_MAP_END
82 NS_IMPL_ADDREF(nsPluginArray)
83 NS_IMPL_RELEASE(nsPluginArray)
85 NS_IMETHODIMP
86 nsPluginArray::GetLength(PRUint32* aLength)
88 if (AllowPlugins() && mPluginHost)
89 return mPluginHost->GetPluginCount(aLength);
91 *aLength = 0;
92 return NS_OK;
95 PRBool
96 nsPluginArray::AllowPlugins()
98 PRBool allowPlugins = PR_FALSE;
99 if (mDocShell)
100 if (NS_FAILED(mDocShell->GetAllowPlugins(&allowPlugins)))
101 allowPlugins = PR_FALSE;
103 return allowPlugins;
106 nsIDOMPlugin*
107 nsPluginArray::GetItemAt(PRUint32 aIndex, nsresult* aResult)
109 *aResult = NS_OK;
111 if (!AllowPlugins())
112 return nsnull;
114 if (mPluginArray == nsnull) {
115 *aResult = GetPlugins();
116 if (*aResult != NS_OK)
117 return nsnull;
120 return aIndex < mPluginCount ? mPluginArray[aIndex] : nsnull;
123 NS_IMETHODIMP
124 nsPluginArray::Item(PRUint32 aIndex, nsIDOMPlugin** aReturn)
126 nsresult rv;
128 NS_IF_ADDREF(*aReturn = GetItemAt(aIndex, &rv));
130 return rv;
133 nsIDOMPlugin*
134 nsPluginArray::GetNamedItem(const nsAString& aName, nsresult* aResult)
136 *aResult = NS_OK;
138 if (!AllowPlugins())
139 return nsnull;
141 if (mPluginArray == nsnull) {
142 *aResult = GetPlugins();
143 if (*aResult != NS_OK)
144 return nsnull;
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)) {
151 return plugin;
155 return nsnull;
158 NS_IMETHODIMP
159 nsPluginArray::NamedItem(const nsAString& aName, nsIDOMPlugin** aReturn)
161 NS_PRECONDITION(nsnull != aReturn, "null arg");
163 nsresult rv;
165 NS_IF_ADDREF(*aReturn = GetNamedItem(aName, &rv));
167 return rv;
170 nsresult
171 nsPluginArray::GetPluginHost(nsIPluginHost** aPluginHost)
173 NS_ENSURE_ARG_POINTER(aPluginHost);
175 nsresult rv = NS_OK;
177 if (!mPluginHost) {
178 mPluginHost = do_GetService(MOZ_PLUGIN_HOST_CONTRACTID, &rv);
180 if (NS_FAILED(rv)) {
181 return rv;
185 *aPluginHost = mPluginHost;
186 NS_IF_ADDREF(*aPluginHost);
188 return rv;
191 void
192 nsPluginArray::SetDocShell(nsIDocShell *aDocShell)
194 mDocShell = aDocShell;
197 void
198 nsPluginArray::Invalidate()
200 mDocShell = nsnull;
201 mNavigator = nsnull;
204 NS_IMETHODIMP
205 nsPluginArray::Refresh(PRBool aReloadDocuments)
207 nsresult res = NS_OK;
208 if (!AllowPlugins())
209 return NS_SUCCESS_LOSS_OF_INSIGNIFICANT_DATA;
211 if (!mPluginHost) {
212 mPluginHost = do_GetService(MOZ_PLUGIN_HOST_CONTRACTID, &res);
215 if(NS_FAILED(res)) {
216 return 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;
222 if(mPluginHost)
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)
228 return res;
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;
239 mPluginCount = 0;
240 mPluginArray = nsnull;
242 if (mNavigator)
243 mNavigator->RefreshMIMEArray();
245 if (aReloadDocuments && webNav)
246 webNav->Reload(nsIWebNavigation::LOAD_FLAGS_NONE);
248 return res;
251 nsresult
252 nsPluginArray::GetPlugins()
254 nsresult rv = GetLength(&mPluginCount);
255 if (NS_SUCCEEDED(rv)) {
256 mPluginArray = new nsIDOMPlugin*[mPluginCount];
257 if (!mPluginArray)
258 return NS_ERROR_OUT_OF_MEMORY;
260 if (!mPluginCount)
261 return NS_OK;
263 rv = mPluginHost->GetPlugins(mPluginCount, mPluginArray);
264 if (NS_SUCCEEDED(rv)) {
265 // need to wrap each of these with a nsPluginElement, which
266 // is scriptable.
267 for (PRUint32 i = 0; i < mPluginCount; i++) {
268 nsIDOMPlugin* wrapper = new nsPluginElement(mPluginArray[i]);
269 NS_IF_ADDREF(wrapper);
270 mPluginArray[i] = wrapper;
272 } else {
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.
277 mPluginCount = 0;
280 return rv;
285 nsPluginElement::nsPluginElement(nsIDOMPlugin* plugin)
287 mPlugin = plugin; // don't AddRef, see nsPluginArray::Item.
288 mMimeTypeCount = 0;
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]);
299 if (mt) {
300 mt->DetachPlugin();
301 NS_RELEASE(mt);
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)
316 NS_INTERFACE_MAP_END
319 NS_IMPL_ADDREF(nsPluginElement)
320 NS_IMPL_RELEASE(nsPluginElement)
323 NS_IMETHODIMP
324 nsPluginElement::GetDescription(nsAString& aDescription)
326 return mPlugin->GetDescription(aDescription);
329 NS_IMETHODIMP
330 nsPluginElement::GetFilename(nsAString& aFilename)
332 return mPlugin->GetFilename(aFilename);
335 NS_IMETHODIMP
336 nsPluginElement::GetVersion(nsAString& aVersion)
338 return mPlugin->GetVersion(aVersion);
341 NS_IMETHODIMP
342 nsPluginElement::GetName(nsAString& aName)
344 return mPlugin->GetName(aName);
347 NS_IMETHODIMP
348 nsPluginElement::GetLength(PRUint32* aLength)
350 return mPlugin->GetLength(aLength);
353 nsIDOMMimeType*
354 nsPluginElement::GetItemAt(PRUint32 aIndex, nsresult *aResult)
356 if (mMimeTypeArray == nsnull) {
357 *aResult = GetMimeTypes();
358 if (*aResult != NS_OK)
359 return nsnull;
362 if (aIndex >= mMimeTypeCount) {
363 *aResult = NS_ERROR_FAILURE;
365 return nsnull;
368 *aResult = NS_OK;
370 return mMimeTypeArray[aIndex];
373 NS_IMETHODIMP
374 nsPluginElement::Item(PRUint32 aIndex, nsIDOMMimeType** aReturn)
376 nsresult rv;
378 NS_IF_ADDREF(*aReturn = GetItemAt(aIndex, &rv));
380 return rv;
383 nsIDOMMimeType*
384 nsPluginElement::GetNamedItem(const nsAString& aName, nsresult *aResult)
386 if (mMimeTypeArray == nsnull) {
387 *aResult = GetMimeTypes();
388 if (*aResult != NS_OK)
389 return nsnull;
392 *aResult = NS_OK;
393 for (PRUint32 i = 0; i < mMimeTypeCount; i++) {
394 nsAutoString type;
395 nsIDOMMimeType* mimeType = mMimeTypeArray[i];
396 if (mimeType->GetType(type) == NS_OK && type.Equals(aName)) {
397 return mimeType;
401 return nsnull;
404 NS_IMETHODIMP
405 nsPluginElement::NamedItem(const nsAString& aName, nsIDOMMimeType** aReturn)
407 nsresult rv;
409 NS_IF_ADDREF(*aReturn = GetNamedItem(aName, &rv));
411 return rv;
414 nsresult
415 nsPluginElement::GetMimeTypes()
417 nsresult rv = mPlugin->GetLength(&mMimeTypeCount);
418 if (rv == NS_OK) {
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));
425 if (rv != NS_OK)
426 break;
427 mimeType = new nsMimeType(this, mimeType);
428 NS_IF_ADDREF(mMimeTypeArray[i] = mimeType);
431 return rv;