Source code

Revision control

Other Tools

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
NS_IMPL_ISUPPORTS(nsWebNavigationInfo, nsIWebNavigationInfo)
18
19
NS_IMETHODIMP
20
nsWebNavigationInfo::IsTypeSupported(const nsACString& aType,
21
nsIWebNavigation* aWebNav,
22
uint32_t* aIsTypeSupported) {
23
MOZ_ASSERT(aIsTypeSupported, "null out param?");
24
25
*aIsTypeSupported = IsTypeSupported(aType, aWebNav);
26
return NS_OK;
27
}
28
29
uint32_t nsWebNavigationInfo::IsTypeSupported(const nsACString& aType,
30
nsIWebNavigation* aWebNav) {
31
// Note to self: aWebNav could be an nsWebBrowser or an nsDocShell here (or
32
// an nsSHistory, but not much we can do with that). So if we start using
33
// it here, we need to be careful to get to the docshell correctly.
34
nsCOMPtr<nsIDocShell> docShell(do_QueryInterface(aWebNav));
35
auto* browsingContext = docShell ? docShell->GetBrowsingContext() : nullptr;
36
bool pluginsAllowed =
37
browsingContext ? browsingContext->GetAllowPlugins() : true;
38
39
return IsTypeSupported(aType, pluginsAllowed);
40
}
41
42
uint32_t nsWebNavigationInfo::IsTypeSupported(const nsACString& aType,
43
bool aPluginsAllowed) {
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
if (aType.LowerCaseEqualsLiteral("application/pdf") &&
47
nsContentUtils::IsPDFJSEnabled()) {
48
return nsIWebNavigationInfo::UNSUPPORTED;
49
;
50
}
51
52
const nsCString& flatType = PromiseFlatCString(aType);
53
uint32_t result = IsTypeSupportedInternal(flatType);
54
if (result != nsIWebNavigationInfo::UNSUPPORTED) {
55
return result;
56
}
57
58
// As of FF 52, we only support flash and test plugins, so if the mime types
59
// don't match for that, exit before we start loading plugins.
60
if (!nsPluginHost::CanUsePluginForMIMEType(aType)) {
61
return nsIWebNavigationInfo::UNSUPPORTED;
62
}
63
64
// If this request is for a docShell that isn't going to allow plugins,
65
// there's no need to try and find a plugin to handle it.
66
if (!aPluginsAllowed) {
67
return nsIWebNavigationInfo::UNSUPPORTED;
68
}
69
70
// Try reloading plugins in case they've changed.
71
nsCOMPtr<nsIPluginHost> pluginHost =
72
do_GetService(MOZ_PLUGIN_HOST_CONTRACTID);
73
if (pluginHost) {
74
// false will ensure that currently running plugins will not
75
// be shut down
76
nsresult rv = pluginHost->ReloadPlugins();
77
if (NS_SUCCEEDED(rv)) {
78
// OK, we reloaded plugins and there were new ones
79
// (otherwise NS_ERROR_PLUGINS_PLUGINSNOTCHANGED would have
80
// been returned). Try checking whether we can handle the
81
// content now.
82
return IsTypeSupportedInternal(flatType);
83
}
84
}
85
86
return nsIWebNavigationInfo::UNSUPPORTED;
87
}
88
89
uint32_t nsWebNavigationInfo::IsTypeSupportedInternal(const nsCString& aType) {
90
nsContentUtils::ContentViewerType vtype = nsContentUtils::TYPE_UNSUPPORTED;
91
92
nsCOMPtr<nsIDocumentLoaderFactory> docLoaderFactory =
93
nsContentUtils::FindInternalContentViewer(aType, &vtype);
94
95
switch (vtype) {
96
case nsContentUtils::TYPE_UNSUPPORTED:
97
return nsIWebNavigationInfo::UNSUPPORTED;
98
99
case nsContentUtils::TYPE_PLUGIN:
100
return nsIWebNavigationInfo::PLUGIN;
101
102
case nsContentUtils::TYPE_UNKNOWN:
103
return nsIWebNavigationInfo::OTHER;
104
105
case nsContentUtils::TYPE_CONTENT:
106
// XXXbz we only need this because images register for the same
107
// contractid as documents, so we can't tell them apart based on
108
// contractid.
109
if (imgLoader::SupportImageWithMimeType(aType.get())) {
110
return nsIWebNavigationInfo::IMAGE;
111
} else {
112
return nsIWebNavigationInfo::OTHER;
113
}
114
}
115
116
return nsIWebNavigationInfo::UNSUPPORTED;
117
}