Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/html/AutoplayPermissionRequest.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim:set ts=2 sw=2 sts=2 et cindent: */
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 "mozilla/AutoplayPermissionRequest.h"
8
#include "mozilla/AutoplayPermissionManager.h"
9
10
#include "mozilla/Logging.h"
11
12
extern mozilla::LazyLogModule gAutoplayPermissionLog;
13
14
#define PLAY_REQUEST_LOG(msg, ...)                                             \
15
0
  MOZ_LOG(gAutoplayPermissionLog, LogLevel::Debug, (msg, ##__VA_ARGS__))
16
17
namespace mozilla {
18
19
NS_IMPL_ISUPPORTS(AutoplayPermissionRequest, nsIContentPermissionRequest)
20
21
AutoplayPermissionRequest::AutoplayPermissionRequest(
22
  AutoplayPermissionManager* aManager,
23
  nsGlobalWindowInner* aWindow,
24
  nsIPrincipal* aNodePrincipal,
25
  nsIEventTarget* aMainThreadTarget)
26
  : mManager(aManager)
27
  , mWindow(do_GetWeakReference(aWindow))
28
  , mNodePrincipal(aNodePrincipal)
29
  , mMainThreadTarget(aMainThreadTarget)
30
  , mRequester(new dom::nsContentPermissionRequester(aWindow))
31
0
{
32
0
  MOZ_ASSERT(mNodePrincipal);
33
0
}
34
35
AutoplayPermissionRequest::~AutoplayPermissionRequest()
36
0
{
37
0
  Cancel();
38
0
}
39
40
NS_IMETHODIMP
41
AutoplayPermissionRequest::GetPrincipal(nsIPrincipal** aRequestingPrincipal)
42
0
{
43
0
  NS_ENSURE_ARG_POINTER(aRequestingPrincipal);
44
0
45
0
  nsCOMPtr<nsIPrincipal> principal = mNodePrincipal;
46
0
  principal.forget(aRequestingPrincipal);
47
0
48
0
  return NS_OK;
49
0
}
50
51
NS_IMETHODIMP
52
AutoplayPermissionRequest::GetTypes(nsIArray** aTypes)
53
0
{
54
0
  NS_ENSURE_ARG_POINTER(aTypes);
55
0
56
0
  nsTArray<nsString> emptyOptions;
57
0
  return dom::nsContentPermissionUtils::CreatePermissionArray(
58
0
    NS_LITERAL_CSTRING("autoplay-media"),
59
0
    NS_LITERAL_CSTRING("unused"),
60
0
    emptyOptions,
61
0
    aTypes);
62
0
}
63
64
NS_IMETHODIMP
65
AutoplayPermissionRequest::GetWindow(mozIDOMWindow** aRequestingWindow)
66
0
{
67
0
  NS_ENSURE_ARG_POINTER(aRequestingWindow);
68
0
69
0
  nsCOMPtr<nsPIDOMWindowInner> window = do_QueryReferent(mWindow);
70
0
  if (!window) {
71
0
    return NS_ERROR_FAILURE;
72
0
  }
73
0
  window.forget(aRequestingWindow);
74
0
75
0
  return NS_OK;
76
0
}
77
78
NS_IMETHODIMP
79
AutoplayPermissionRequest::GetElement(dom::Element** aRequestingElement)
80
0
{
81
0
  NS_ENSURE_ARG_POINTER(aRequestingElement);
82
0
  *aRequestingElement = nullptr;
83
0
  return NS_OK;
84
0
}
85
86
NS_IMETHODIMP
87
AutoplayPermissionRequest::GetIsHandlingUserInput(bool* aIsHandlingUserInput)
88
0
{
89
0
  NS_ENSURE_ARG_POINTER(aIsHandlingUserInput);
90
0
  *aIsHandlingUserInput = false;
91
0
  return NS_OK;
92
0
}
93
94
NS_IMETHODIMP
95
AutoplayPermissionRequest::Cancel()
96
0
{
97
0
  if (mManager) {
98
0
    mManager->DenyPlayRequestIfExists();
99
0
    // Clear reference to manager, so we can't double report a result.
100
0
    // This could happen in particular if we call Cancel() in the destructor.
101
0
    mManager = nullptr;
102
0
  }
103
0
  return NS_OK;
104
0
}
105
106
NS_IMETHODIMP
107
AutoplayPermissionRequest::Allow(JS::HandleValue aChoices)
108
0
{
109
0
  if (mManager) {
110
0
    mManager->ApprovePlayRequestIfExists();
111
0
    // Clear reference to manager, so we can't double report a result.
112
0
    // This could happen in particular if we call Cancel() in the destructor.
113
0
    mManager = nullptr;
114
0
  }
115
0
  return NS_OK;
116
0
}
117
118
NS_IMETHODIMP
119
AutoplayPermissionRequest::GetRequester(
120
  nsIContentPermissionRequester** aRequester)
121
0
{
122
0
  NS_ENSURE_ARG_POINTER(aRequester);
123
0
124
0
  nsCOMPtr<nsIContentPermissionRequester> requester = mRequester;
125
0
  requester.forget(aRequester);
126
0
127
0
  return NS_OK;
128
0
}
129
130
already_AddRefed<AutoplayPermissionRequest>
131
AutoplayPermissionRequest::Create(nsGlobalWindowInner* aWindow,
132
                                  AutoplayPermissionManager* aManager)
133
0
{
134
0
  if (!aWindow || !aWindow->GetPrincipal() ||
135
0
      !aWindow->EventTargetFor(TaskCategory::Other)) {
136
0
    return nullptr;
137
0
  }
138
0
  RefPtr<AutoplayPermissionRequest> request =
139
0
    new AutoplayPermissionRequest(aManager,
140
0
                                  aWindow,
141
0
                                  aWindow->GetPrincipal(),
142
0
                                  aWindow->EventTargetFor(TaskCategory::Other));
143
0
  PLAY_REQUEST_LOG("AutoplayPermissionRequest %p Create()", request.get());
144
0
  return request.forget();
145
0
}
146
147
} // namespace mozilla