Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/toolkit/components/browser/nsWebBrowserContentPolicy.cpp
Line
Count
Source (jump to first uncovered line)
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 "nsWebBrowserContentPolicy.h"
8
#include "nsIDocShell.h"
9
#include "nsCOMPtr.h"
10
#include "nsContentPolicyUtils.h"
11
#include "nsIContentViewer.h"
12
13
nsWebBrowserContentPolicy::nsWebBrowserContentPolicy()
14
0
{
15
0
}
16
17
nsWebBrowserContentPolicy::~nsWebBrowserContentPolicy()
18
0
{
19
0
}
20
21
NS_IMPL_ISUPPORTS(nsWebBrowserContentPolicy, nsIContentPolicy)
22
23
NS_IMETHODIMP
24
nsWebBrowserContentPolicy::ShouldLoad(nsIURI* aContentLocation,
25
                                      nsILoadInfo* aLoadInfo,
26
                                      const nsACString& aMimeGuess,
27
                                      int16_t* aShouldLoad)
28
0
{
29
0
  MOZ_ASSERT(aShouldLoad, "Null out param");
30
0
31
0
  uint32_t contentType = aLoadInfo->GetExternalContentPolicyType();
32
0
  MOZ_ASSERT(contentType == nsContentUtils::InternalContentPolicyTypeToExternal(contentType),
33
0
             "We should only see external content policy types here.");
34
0
35
0
  *aShouldLoad = nsIContentPolicy::ACCEPT;
36
0
37
0
  nsCOMPtr<nsISupports> context = aLoadInfo->GetLoadingContext();
38
0
  nsIDocShell* shell = NS_CP_GetDocShellFromContext(context);
39
0
  /* We're going to dereference shell, so make sure it isn't null */
40
0
  if (!shell) {
41
0
    return NS_OK;
42
0
  }
43
0
44
0
  nsresult rv;
45
0
  bool allowed = true;
46
0
47
0
  switch (contentType) {
48
0
    case nsIContentPolicy::TYPE_SCRIPT:
49
0
      rv = shell->GetAllowJavascript(&allowed);
50
0
      break;
51
0
    case nsIContentPolicy::TYPE_SUBDOCUMENT:
52
0
      rv = shell->GetAllowSubframes(&allowed);
53
0
      break;
54
#if 0
55
    /* XXXtw: commented out in old code; add during conpol phase 2 */
56
    case nsIContentPolicy::TYPE_REFRESH:
57
      rv = shell->GetAllowMetaRedirects(&allowed); /* meta _refresh_ */
58
      break;
59
#endif
60
0
    case nsIContentPolicy::TYPE_IMAGE:
61
0
    case nsIContentPolicy::TYPE_IMAGESET:
62
0
      rv = shell->GetAllowImages(&allowed);
63
0
      break;
64
0
    default:
65
0
      return NS_OK;
66
0
  }
67
0
68
0
  if (NS_SUCCEEDED(rv) && !allowed) {
69
0
    *aShouldLoad = nsIContentPolicy::REJECT_TYPE;
70
0
  }
71
0
  return rv;
72
0
}
73
74
NS_IMETHODIMP
75
nsWebBrowserContentPolicy::ShouldProcess(nsIURI* aContentLocation,
76
                                         nsILoadInfo* aLoadInfo,
77
                                         const nsACString& aMimeGuess,
78
                                         int16_t* aShouldProcess)
79
0
{
80
0
  MOZ_ASSERT(aShouldProcess, "Null out param");
81
0
82
0
  uint32_t contentType = aLoadInfo->GetExternalContentPolicyType();
83
0
  MOZ_ASSERT(contentType == nsContentUtils::InternalContentPolicyTypeToExternal(contentType),
84
0
             "We should only see external content policy types here.");
85
0
86
0
  *aShouldProcess = nsIContentPolicy::ACCEPT;
87
0
88
0
  // Object tags will always open channels with TYPE_OBJECT, but may end up
89
0
  // loading with TYPE_IMAGE or TYPE_DOCUMENT as their final type, so we block
90
0
  // actual-plugins at the process stage
91
0
  if (contentType != nsIContentPolicy::TYPE_OBJECT) {
92
0
    return NS_OK;
93
0
  }
94
0
95
0
  nsCOMPtr<nsISupports> context = aLoadInfo->GetLoadingContext();
96
0
  nsIDocShell* shell = NS_CP_GetDocShellFromContext(context);
97
0
  if (shell && (!shell->PluginsAllowedInCurrentDoc())) {
98
0
    *aShouldProcess = nsIContentPolicy::REJECT_TYPE;
99
0
  }
100
0
101
0
  return NS_OK;
102
0
}