Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "nsWebNavigationInfo.h"
8 : #include "nsIWebNavigation.h"
9 : #include "nsServiceManagerUtils.h"
10 : #include "nsIDocumentLoaderFactory.h"
11 : #include "nsIPluginHost.h"
12 : #include "nsIDocShell.h"
13 : #include "nsContentUtils.h"
14 : #include "imgLoader.h"
15 : #include "nsPluginHost.h"
16 :
17 22 : NS_IMPL_ISUPPORTS(nsWebNavigationInfo, nsIWebNavigationInfo)
18 :
19 : nsresult
20 2 : nsWebNavigationInfo::Init()
21 : {
22 : nsresult rv;
23 2 : mCategoryManager = do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);
24 2 : NS_ENSURE_SUCCESS(rv, rv);
25 :
26 2 : return NS_OK;
27 : }
28 :
29 : NS_IMETHODIMP
30 4 : nsWebNavigationInfo::IsTypeSupported(const nsACString& aType,
31 : nsIWebNavigation* aWebNav,
32 : uint32_t* aIsTypeSupported)
33 : {
34 4 : NS_PRECONDITION(aIsTypeSupported, "null out param?");
35 :
36 : // Note to self: aWebNav could be an nsWebBrowser or an nsDocShell here (or
37 : // an nsSHistory, but not much we can do with that). So if we start using
38 : // it here, we need to be careful to get to the docshell correctly.
39 :
40 : // For now just report what the Gecko-Content-Viewers category has
41 : // to say for itself.
42 4 : *aIsTypeSupported = nsIWebNavigationInfo::UNSUPPORTED;
43 :
44 : // We want to claim that the type for PDF documents is unsupported,
45 : // so that the internal PDF viewer's stream converted will get used.
46 4 : if (aType.LowerCaseEqualsLiteral("application/pdf") &&
47 0 : nsContentUtils::IsPDFJSEnabled()) {
48 0 : return NS_OK;
49 : }
50 :
51 8 : const nsCString& flatType = PromiseFlatCString(aType);
52 4 : nsresult rv = IsTypeSupportedInternal(flatType, aIsTypeSupported);
53 4 : NS_ENSURE_SUCCESS(rv, rv);
54 :
55 4 : if (*aIsTypeSupported) {
56 4 : return rv;
57 : }
58 :
59 : // As of FF 52, we only support flash and test plugins, so if the mime types
60 : // don't match for that, exit before we start loading plugins.
61 0 : if (!nsPluginHost::CanUsePluginForMIMEType(aType)) {
62 0 : return NS_OK;
63 : }
64 :
65 : // If this request is for a docShell that isn't going to allow plugins,
66 : // there's no need to try and find a plugin to handle it.
67 0 : nsCOMPtr<nsIDocShell> docShell(do_QueryInterface(aWebNav));
68 : bool allowed;
69 0 : if (docShell &&
70 0 : NS_SUCCEEDED(docShell->GetAllowPlugins(&allowed)) && !allowed) {
71 0 : return NS_OK;
72 : }
73 :
74 : // Try reloading plugins in case they've changed.
75 : nsCOMPtr<nsIPluginHost> pluginHost =
76 0 : do_GetService(MOZ_PLUGIN_HOST_CONTRACTID);
77 0 : if (pluginHost) {
78 : // false will ensure that currently running plugins will not
79 : // be shut down
80 0 : rv = pluginHost->ReloadPlugins();
81 0 : if (NS_SUCCEEDED(rv)) {
82 : // OK, we reloaded plugins and there were new ones
83 : // (otherwise NS_ERROR_PLUGINS_PLUGINSNOTCHANGED would have
84 : // been returned). Try checking whether we can handle the
85 : // content now.
86 0 : return IsTypeSupportedInternal(flatType, aIsTypeSupported);
87 : }
88 : }
89 :
90 0 : return NS_OK;
91 : }
92 :
93 : nsresult
94 4 : nsWebNavigationInfo::IsTypeSupportedInternal(const nsCString& aType,
95 : uint32_t* aIsSupported)
96 : {
97 4 : NS_PRECONDITION(aIsSupported, "Null out param?");
98 :
99 4 : nsContentUtils::ContentViewerType vtype = nsContentUtils::TYPE_UNSUPPORTED;
100 :
101 : nsCOMPtr<nsIDocumentLoaderFactory> docLoaderFactory =
102 8 : nsContentUtils::FindInternalContentViewer(aType, &vtype);
103 :
104 4 : switch (vtype) {
105 : case nsContentUtils::TYPE_UNSUPPORTED:
106 0 : *aIsSupported = nsIWebNavigationInfo::UNSUPPORTED;
107 0 : break;
108 :
109 : case nsContentUtils::TYPE_PLUGIN:
110 0 : *aIsSupported = nsIWebNavigationInfo::PLUGIN;
111 0 : break;
112 :
113 : case nsContentUtils::TYPE_UNKNOWN:
114 0 : *aIsSupported = nsIWebNavigationInfo::OTHER;
115 0 : break;
116 :
117 : case nsContentUtils::TYPE_CONTENT:
118 : // XXXbz we only need this because images register for the same
119 : // contractid as documents, so we can't tell them apart based on
120 : // contractid.
121 4 : if (imgLoader::SupportImageWithMimeType(aType.get())) {
122 0 : *aIsSupported = nsIWebNavigationInfo::IMAGE;
123 : } else {
124 4 : *aIsSupported = nsIWebNavigationInfo::OTHER;
125 : }
126 4 : break;
127 : }
128 :
129 8 : return NS_OK;
130 : }
|