Automated checkin: version bump remove "pre" from version number for firefox 3.7a1...
[mozilla-central.git] / docshell / base / nsWebNavigationInfo.cpp
blobd1b6667ded18ea2babcfe1917fdc3da552c199eb
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 * Boris Zbarsky <bzbarsky@mit.edu>.
19 * Portions created by the Initial Developer are Copyright (C) 2005
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "nsWebNavigationInfo.h"
39 #include "nsIWebNavigation.h"
40 #include "nsString.h"
41 #include "nsServiceManagerUtils.h"
42 #include "nsIDocumentLoaderFactory.h"
43 #include "nsIPluginHost.h"
45 NS_IMPL_ISUPPORTS1(nsWebNavigationInfo, nsIWebNavigationInfo)
47 #define CONTENT_DLF_CONTRACT "@mozilla.org/content/document-loader-factory;1"
48 #define PLUGIN_DLF_CONTRACT \
49 "@mozilla.org/content/plugin/document-loader-factory;1"
51 nsresult
52 nsWebNavigationInfo::Init()
54 nsresult rv;
55 mCategoryManager = do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);
56 NS_ENSURE_SUCCESS(rv, rv);
58 mImgLoader = do_GetService("@mozilla.org/image/loader;1", &rv);
60 return rv;
63 NS_IMETHODIMP
64 nsWebNavigationInfo::IsTypeSupported(const nsACString& aType,
65 nsIWebNavigation* aWebNav,
66 PRUint32* aIsTypeSupported)
68 NS_PRECONDITION(aIsTypeSupported, "null out param?");
70 // Note to self: aWebNav could be an nsWebBrowser or an nsDocShell here (or
71 // an nsSHistory, but not much we can do with that). So if we start using
72 // it here, we need to be careful to get to the docshell correctly.
74 // For now just report what the Gecko-Content-Viewers category has
75 // to say for itself.
76 *aIsTypeSupported = nsIWebNavigationInfo::UNSUPPORTED;
78 const nsCString& flatType = PromiseFlatCString(aType);
79 nsresult rv = IsTypeSupportedInternal(flatType, aIsTypeSupported);
80 NS_ENSURE_SUCCESS(rv, rv);
82 if (*aIsTypeSupported) {
83 return rv;
86 // Try reloading plugins in case they've changed.
87 nsCOMPtr<nsIPluginHost> pluginHost =
88 do_GetService(MOZ_PLUGIN_HOST_CONTRACTID);
89 if (pluginHost) {
90 // PR_FALSE will ensure that currently running plugins will not
91 // be shut down
92 rv = pluginHost->ReloadPlugins(PR_FALSE);
93 if (NS_SUCCEEDED(rv)) {
94 // OK, we reloaded plugins and there were new ones
95 // (otherwise NS_ERROR_PLUGINS_PLUGINSNOTCHANGED would have
96 // been returned). Try checking whether we can handle the
97 // content now.
98 return IsTypeSupportedInternal(flatType, aIsTypeSupported);
102 return NS_OK;
105 nsresult
106 nsWebNavigationInfo::IsTypeSupportedInternal(const nsCString& aType,
107 PRUint32* aIsSupported)
109 NS_PRECONDITION(mCategoryManager, "Must have category manager");
110 NS_PRECONDITION(aIsSupported, "Null out param?");
112 nsXPIDLCString value;
113 nsresult rv = mCategoryManager->GetCategoryEntry("Gecko-Content-Viewers",
114 aType.get(),
115 getter_Copies(value));
117 // If the category manager can't find what we're looking for
118 // it returns NS_ERROR_NOT_AVAILABLE, we don't want to propagate
119 // that to the caller since it's really not a failure
121 if (NS_FAILED(rv) && rv != NS_ERROR_NOT_AVAILABLE)
122 return rv;
124 // Now try to get an actual document loader factory for this contractid. If
125 // there is no contractid, don't try and just return false for *aIsSupported.
126 nsCOMPtr<nsIDocumentLoaderFactory> docLoaderFactory;
127 if (!value.IsEmpty()) {
128 docLoaderFactory = do_GetService(value.get());
131 // If we got a factory, we should be able to handle this type
132 if (!docLoaderFactory) {
133 *aIsSupported = nsIWebNavigationInfo::UNSUPPORTED;
135 else if (value.EqualsLiteral(CONTENT_DLF_CONTRACT)) {
136 PRBool isImage = PR_FALSE;
137 mImgLoader->SupportImageWithMimeType(aType.get(), &isImage);
138 if (isImage) {
139 *aIsSupported = nsIWebNavigationInfo::IMAGE;
141 else {
142 *aIsSupported = nsIWebNavigationInfo::OTHER;
145 else if (value.EqualsLiteral(PLUGIN_DLF_CONTRACT)) {
146 *aIsSupported = nsIWebNavigationInfo::PLUGIN;
148 else {
149 *aIsSupported = nsIWebNavigationInfo::OTHER;
152 return NS_OK;