Bug 1684463 - [devtools] Part 1: Shorten the _createAttribute function by refactoring...
[gecko.git] / image / ImageBlocker.cpp
blobb3e8df22350f926ef2a8e2bea086023465ab4c74
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 const nsACString& aMimeGuess, int16_t* aShouldLoad) {
19 nsContentPolicyType contentType = aLoadInfo->GetExternalContentPolicyType();
20 MOZ_ASSERT(contentType == nsContentUtils::InternalContentPolicyTypeToExternal(
21 contentType),
22 "We should only see external content policy types here.");
24 *aShouldLoad = nsIContentPolicy::ACCEPT;
26 // we only want to check http, https, ftp
27 // for chrome:// and resources and others, no need to check.
28 nsAutoCString scheme;
29 aContentLocation->GetScheme(scheme);
30 if (!scheme.LowerCaseEqualsLiteral("ftp") &&
31 !scheme.LowerCaseEqualsLiteral("http") &&
32 !scheme.LowerCaseEqualsLiteral("https")) {
33 return NS_OK;
36 // Block loading images depending on the permissions.default.image pref.
37 if ((contentType == nsIContentPolicy::TYPE_IMAGE ||
38 contentType == nsIContentPolicy::TYPE_IMAGESET) &&
39 StaticPrefs::permissions_default_image() ==
40 nsIPermissionManager::DENY_ACTION) {
41 NS_SetRequestBlockingReason(
42 aLoadInfo, nsILoadInfo::BLOCKING_REASON_CONTENT_POLICY_CONTENT_BLOCKED);
43 *aShouldLoad = nsIContentPolicy::REJECT_TYPE;
46 return NS_OK;
49 NS_IMETHODIMP
50 ImageBlocker::ShouldProcess(nsIURI* aContentLocation, nsILoadInfo* aLoadInfo,
51 const nsACString& aMimeGuess,
52 int16_t* aShouldProcess) {
53 #ifdef DEBUG
54 nsContentPolicyType contentType = aLoadInfo->GetExternalContentPolicyType();
55 MOZ_ASSERT(contentType == nsContentUtils::InternalContentPolicyTypeToExternal(
56 contentType),
57 "We should only see external content policy types here.");
58 #endif
60 // We block images at load level already, so those should not end up here.
61 *aShouldProcess = nsIContentPolicy::ACCEPT;
62 return NS_OK;