nsWebBrowserContentPolicy.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "nsWebBrowserContentPolicy.h"
  6. #include "nsIDocShell.h"
  7. #include "nsCOMPtr.h"
  8. #include "nsContentPolicyUtils.h"
  9. #include "nsIContentViewer.h"
  10. nsWebBrowserContentPolicy::nsWebBrowserContentPolicy()
  11. {
  12. MOZ_COUNT_CTOR(nsWebBrowserContentPolicy);
  13. }
  14. nsWebBrowserContentPolicy::~nsWebBrowserContentPolicy()
  15. {
  16. MOZ_COUNT_DTOR(nsWebBrowserContentPolicy);
  17. }
  18. NS_IMPL_ISUPPORTS(nsWebBrowserContentPolicy, nsIContentPolicy)
  19. NS_IMETHODIMP
  20. nsWebBrowserContentPolicy::ShouldLoad(uint32_t aContentType,
  21. nsIURI* aContentLocation,
  22. nsIURI* aRequestingLocation,
  23. nsISupports* aRequestingContext,
  24. const nsACString& aMimeGuess,
  25. nsISupports* aExtra,
  26. nsIPrincipal* aRequestPrincipal,
  27. int16_t* aShouldLoad)
  28. {
  29. NS_PRECONDITION(aShouldLoad, "Null out param");
  30. MOZ_ASSERT(aContentType == nsContentUtils::InternalContentPolicyTypeToExternal(aContentType),
  31. "We should only see external content policy types here.");
  32. *aShouldLoad = nsIContentPolicy::ACCEPT;
  33. nsIDocShell* shell = NS_CP_GetDocShellFromContext(aRequestingContext);
  34. /* We're going to dereference shell, so make sure it isn't null */
  35. if (!shell) {
  36. return NS_OK;
  37. }
  38. nsresult rv;
  39. bool allowed = true;
  40. switch (aContentType) {
  41. case nsIContentPolicy::TYPE_SCRIPT:
  42. rv = shell->GetAllowJavascript(&allowed);
  43. break;
  44. case nsIContentPolicy::TYPE_SUBDOCUMENT:
  45. rv = shell->GetAllowSubframes(&allowed);
  46. break;
  47. #if 0
  48. /* XXXtw: commented out in old code; add during conpol phase 2 */
  49. case nsIContentPolicy::TYPE_REFRESH:
  50. rv = shell->GetAllowMetaRedirects(&allowed); /* meta _refresh_ */
  51. break;
  52. #endif
  53. case nsIContentPolicy::TYPE_IMAGE:
  54. case nsIContentPolicy::TYPE_IMAGESET:
  55. rv = shell->GetAllowImages(&allowed);
  56. break;
  57. default:
  58. return NS_OK;
  59. }
  60. if (NS_SUCCEEDED(rv) && !allowed) {
  61. *aShouldLoad = nsIContentPolicy::REJECT_TYPE;
  62. }
  63. return rv;
  64. }
  65. NS_IMETHODIMP
  66. nsWebBrowserContentPolicy::ShouldProcess(uint32_t aContentType,
  67. nsIURI* aContentLocation,
  68. nsIURI* aRequestingLocation,
  69. nsISupports* aRequestingContext,
  70. const nsACString& aMimeGuess,
  71. nsISupports* aExtra,
  72. nsIPrincipal* aRequestPrincipal,
  73. int16_t* aShouldProcess)
  74. {
  75. NS_PRECONDITION(aShouldProcess, "Null out param");
  76. MOZ_ASSERT(aContentType == nsContentUtils::InternalContentPolicyTypeToExternal(aContentType),
  77. "We should only see external content policy types here.");
  78. *aShouldProcess = nsIContentPolicy::ACCEPT;
  79. // Object tags will always open channels with TYPE_OBJECT, but may end up
  80. // loading with TYPE_IMAGE or TYPE_DOCUMENT as their final type, so we block
  81. // actual-plugins at the process stage
  82. if (aContentType != nsIContentPolicy::TYPE_OBJECT) {
  83. return NS_OK;
  84. }
  85. nsIDocShell* shell = NS_CP_GetDocShellFromContext(aRequestingContext);
  86. if (shell && (!shell->PluginsAllowedInCurrentDoc())) {
  87. *aShouldProcess = nsIContentPolicy::REJECT_TYPE;
  88. }
  89. return NS_OK;
  90. }