Backed out 3 changesets (bug 1892041) for causing SM failures in test262. CLOSED...
[gecko.git] / image / ImageBlocker.cpp
blobc8ff1baaafb0069414b4ea9844aa452c003d4141
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "ImageBlocker.h"
6 #include "nsIPermissionManager.h"
7 #include "nsContentUtils.h"
8 #include "mozilla/StaticPrefs_permissions.h"
9 #include "nsNetUtil.h"
11 using namespace mozilla;
12 using namespace mozilla::image;
14 NS_IMPL_ISUPPORTS(ImageBlocker, nsIContentPolicy)
16 NS_IMETHODIMP
17 ImageBlocker::ShouldLoad(nsIURI* aContentLocation, nsILoadInfo* aLoadInfo,
18 int16_t* aShouldLoad) {
19 *aShouldLoad = nsIContentPolicy::ACCEPT;
21 if (!aContentLocation) {
22 // Bug 1720280: Ideally we should block the load, but to avoid a potential
23 // null pointer deref, we return early in this case. Please note that
24 // the ImageBlocker only applies about http/https loads anyway.
25 return NS_OK;
28 ExtContentPolicyType contentType = aLoadInfo->GetExternalContentPolicyType();
29 if (contentType != ExtContentPolicy::TYPE_IMAGE &&
30 contentType != ExtContentPolicy::TYPE_IMAGESET) {
31 return NS_OK;
34 if (ImageBlocker::ShouldBlock(aContentLocation)) {
35 NS_SetRequestBlockingReason(
36 aLoadInfo, nsILoadInfo::BLOCKING_REASON_CONTENT_POLICY_CONTENT_BLOCKED);
37 *aShouldLoad = nsIContentPolicy::REJECT_TYPE;
40 return NS_OK;
43 NS_IMETHODIMP
44 ImageBlocker::ShouldProcess(nsIURI* aContentLocation, nsILoadInfo* aLoadInfo,
45 int16_t* aShouldProcess) {
46 // We block images at load level already, so those should not end up here.
47 *aShouldProcess = nsIContentPolicy::ACCEPT;
48 return NS_OK;
51 /* static */
52 bool ImageBlocker::ShouldBlock(nsIURI* aContentLocation) {
53 // Block loading images depending on the permissions.default.image pref.
54 if (StaticPrefs::permissions_default_image() !=
55 nsIPermissionManager::DENY_ACTION) {
56 return false;
59 // we only want to check http, https
60 // for chrome:// and resources and others, no need to check.
61 return aContentLocation->SchemeIs("http") ||
62 aContentLocation->SchemeIs("https");