ImageBlocker.cpp (2087B)
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/. */ 4 5 #include "ImageBlocker.h" 6 #include "nsIPermissionManager.h" 7 #include "nsContentUtils.h" 8 #include "mozilla/StaticPrefs_permissions.h" 9 #include "nsNetUtil.h" 10 11 using namespace mozilla; 12 using namespace mozilla::image; 13 14 NS_IMPL_ISUPPORTS(ImageBlocker, nsIContentPolicy) 15 16 NS_IMETHODIMP 17 ImageBlocker::ShouldLoad(nsIURI* aContentLocation, nsILoadInfo* aLoadInfo, 18 int16_t* aShouldLoad) { 19 *aShouldLoad = nsIContentPolicy::ACCEPT; 20 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; 26 } 27 28 ExtContentPolicyType contentType = aLoadInfo->GetExternalContentPolicyType(); 29 if (contentType != ExtContentPolicy::TYPE_IMAGE && 30 contentType != ExtContentPolicy::TYPE_IMAGESET) { 31 return NS_OK; 32 } 33 34 if (ImageBlocker::ShouldBlock(aContentLocation)) { 35 NS_SetRequestBlockingReason( 36 aLoadInfo, nsILoadInfo::BLOCKING_REASON_CONTENT_POLICY_CONTENT_BLOCKED); 37 *aShouldLoad = nsIContentPolicy::REJECT_TYPE; 38 } 39 40 return NS_OK; 41 } 42 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; 49 } 50 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; 57 } 58 59 // we only want to check http, https 60 // for chrome:// and resources and others, no need to check. 61 return net::SchemeIsHttpOrHttps(aContentLocation); 62 }