Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/html/AutoplayPermissionManager.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/AutoplayPermissionManager.h"
8
#include "mozilla/AutoplayPermissionRequest.h"
9
10
#include "nsGlobalWindowInner.h"
11
#include "nsISupportsImpl.h"
12
#include "mozilla/Logging.h"
13
#include "nsContentPermissionHelper.h"
14
15
extern mozilla::LazyLogModule gAutoplayPermissionLog;
16
17
#define PLAY_REQUEST_LOG(msg, ...)                                             \
18
0
  MOZ_LOG(gAutoplayPermissionLog, LogLevel::Debug, (msg, ##__VA_ARGS__))
19
20
namespace mozilla {
21
22
RefPtr<GenericPromise>
23
AutoplayPermissionManager::RequestWithPrompt()
24
0
{
25
0
  // If we've already requested permission, we'll just return the promise,
26
0
  // as we don't want to show multiple permission requests at once.
27
0
  // The promise is non-exclusive, so if the request has already completed,
28
0
  // the ThenValue will run immediately.
29
0
  if (mRequestDispatched) {
30
0
    PLAY_REQUEST_LOG("AutoplayPermissionManager %p RequestWithPrompt() request "
31
0
                     "already dispatched",
32
0
                     this);
33
0
    return mPromiseHolder.Ensure(__func__);
34
0
  }
35
0
36
0
  nsCOMPtr<nsPIDOMWindowInner> window = do_QueryReferent(mWindow);
37
0
  if (!window) {
38
0
    return GenericPromise::CreateAndReject(NS_ERROR_DOM_MEDIA_NOT_ALLOWED_ERR,
39
0
                                           __func__);
40
0
  }
41
0
42
0
  nsCOMPtr<nsIContentPermissionRequest> request =
43
0
    AutoplayPermissionRequest::Create(nsGlobalWindowInner::Cast(window), this);
44
0
  if (!request) {
45
0
    return GenericPromise::CreateAndReject(NS_ERROR_DOM_MEDIA_NOT_ALLOWED_ERR,
46
0
                                           __func__);
47
0
  }
48
0
49
0
  // Dispatch the request.
50
0
  nsCOMPtr<nsIRunnable> f = NS_NewRunnableFunction(
51
0
    "AutoplayPermissionManager::RequestWithPrompt", [window, request]() {
52
0
      dom::nsContentPermissionUtils::AskPermission(request, window);
53
0
    });
54
0
  window->EventTargetFor(TaskCategory::Other)->Dispatch(f, NS_DISPATCH_NORMAL);
55
0
56
0
  mRequestDispatched = true;
57
0
  return mPromiseHolder.Ensure(__func__);
58
0
}
59
60
AutoplayPermissionManager::AutoplayPermissionManager(
61
  nsGlobalWindowInner* aWindow)
62
  : mWindow(do_GetWeakReference(aWindow))
63
0
{
64
0
  PLAY_REQUEST_LOG("AutoplayPermissionManager %p Create()", this);
65
0
}
66
67
AutoplayPermissionManager::~AutoplayPermissionManager()
68
0
{
69
0
  // If we made a request, it should have been resolved.
70
0
  MOZ_ASSERT(!mRequestDispatched);
71
0
}
72
73
void
74
AutoplayPermissionManager::DenyPlayRequestIfExists()
75
0
{
76
0
  if (mRequestDispatched) {
77
0
    PLAY_REQUEST_LOG("AutoplayPermissionManager %p DenyPlayRequest()", this);
78
0
    mRequestDispatched = false;
79
0
    mPromiseHolder.RejectIfExists(NS_ERROR_DOM_MEDIA_NOT_ALLOWED_ERR, __func__);
80
0
  }
81
0
}
82
83
void
84
AutoplayPermissionManager::ApprovePlayRequestIfExists()
85
0
{
86
0
  if (mRequestDispatched) {
87
0
    PLAY_REQUEST_LOG("AutoplayPermissionManager %p ApprovePlayRequest()", this);
88
0
    mRequestDispatched = false;
89
0
    mPromiseHolder.ResolveIfExists(true, __func__);
90
0
  }
91
0
}
92
93
} // namespace mozilla