Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/midi/MIDIPermissionRequest.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 file,
5
 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "mozilla/dom/MIDIPermissionRequest.h"
8
#include "mozilla/dom/MIDIAccessManager.h"
9
#include "nsIGlobalObject.h"
10
#include "mozilla/Preferences.h"
11
#include "nsContentUtils.h"
12
13
//-------------------------------------------------
14
// MIDI Permission Requests
15
//-------------------------------------------------
16
17
NS_IMPL_CYCLE_COLLECTION(MIDIPermissionRequest, mWindow, mPromise)
18
19
0
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MIDIPermissionRequest)
20
0
NS_INTERFACE_MAP_ENTRY(nsIContentPermissionRequest)
21
0
NS_INTERFACE_MAP_ENTRY(nsIRunnable)
22
0
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIContentPermissionRequest)
23
0
NS_INTERFACE_MAP_END
24
25
NS_IMPL_CYCLE_COLLECTING_ADDREF(MIDIPermissionRequest)
26
NS_IMPL_CYCLE_COLLECTING_RELEASE(MIDIPermissionRequest)
27
28
MIDIPermissionRequest::MIDIPermissionRequest(nsPIDOMWindowInner* aWindow,
29
                                             Promise* aPromise,
30
                                             const MIDIOptions& aOptions)
31
: mWindow(aWindow),
32
  mPromise(aPromise),
33
  mNeedsSysex(aOptions.mSysex),
34
  mRequester(new nsContentPermissionRequester(mWindow))
35
0
{
36
0
  MOZ_ASSERT(aWindow);
37
0
  MOZ_ASSERT(aPromise, "aPromise should not be null!");
38
0
  MOZ_ASSERT(aWindow->GetDoc());
39
0
  mPrincipal = aWindow->GetDoc()->NodePrincipal();
40
0
  MOZ_ASSERT(mPrincipal);
41
0
}
42
43
MIDIPermissionRequest::~MIDIPermissionRequest()
44
0
{
45
0
}
46
47
NS_IMETHODIMP
48
MIDIPermissionRequest::GetIsHandlingUserInput(bool* aHandlingInput)
49
0
{
50
0
  *aHandlingInput = true;
51
0
  return NS_OK;
52
0
}
53
54
NS_IMETHODIMP
55
MIDIPermissionRequest::GetRequester(nsIContentPermissionRequester** aRequester)
56
0
{
57
0
  NS_ENSURE_ARG_POINTER(aRequester);
58
0
  nsCOMPtr<nsIContentPermissionRequester> requester = mRequester;
59
0
  requester.forget(aRequester);
60
0
  return NS_OK;
61
0
}
62
63
NS_IMETHODIMP
64
MIDIPermissionRequest::GetTypes(nsIArray** aTypes)
65
0
{
66
0
  NS_ENSURE_ARG_POINTER(aTypes);
67
0
  nsTArray<nsString> options;
68
0
  if (mNeedsSysex) {
69
0
    options.AppendElement(NS_LITERAL_STRING("sysex"));
70
0
  }
71
0
  return nsContentPermissionUtils::CreatePermissionArray(NS_LITERAL_CSTRING("midi"),
72
0
                                                         NS_LITERAL_CSTRING("unused"),
73
0
                                                         options,
74
0
                                                         aTypes);
75
0
}
76
77
NS_IMETHODIMP
78
MIDIPermissionRequest::GetPrincipal(nsIPrincipal** aRequestingPrincipal)
79
0
{
80
0
  NS_ENSURE_ARG_POINTER(aRequestingPrincipal);
81
0
  NS_IF_ADDREF(*aRequestingPrincipal = mPrincipal);
82
0
  return NS_OK;
83
0
}
84
85
NS_IMETHODIMP
86
MIDIPermissionRequest::GetWindow(mozIDOMWindow** aRequestingWindow)
87
0
{
88
0
  NS_ENSURE_ARG_POINTER(aRequestingWindow);
89
0
  NS_IF_ADDREF(*aRequestingWindow = mWindow);
90
0
  return NS_OK;
91
0
}
92
93
NS_IMETHODIMP
94
MIDIPermissionRequest::GetElement(Element** aRequestingElement)
95
0
{
96
0
  NS_ENSURE_ARG_POINTER(aRequestingElement);
97
0
  *aRequestingElement = nullptr;
98
0
  return NS_OK;
99
0
}
100
101
NS_IMETHODIMP
102
MIDIPermissionRequest::Cancel()
103
0
{
104
0
  mPromise->MaybeReject(NS_ERROR_DOM_SECURITY_ERR);
105
0
  return NS_OK;
106
0
}
107
108
NS_IMETHODIMP
109
MIDIPermissionRequest::Allow(JS::HandleValue aChoices)
110
0
{
111
0
  MOZ_ASSERT(aChoices.isUndefined());
112
0
  MIDIAccessManager* mgr = MIDIAccessManager::Get();
113
0
  mgr->CreateMIDIAccess(mWindow, mNeedsSysex, mPromise);
114
0
  return NS_OK;
115
0
}
116
117
NS_IMETHODIMP
118
MIDIPermissionRequest::Run()
119
0
{
120
0
  // If the testing flag is true, skip dialog
121
0
  if (Preferences::GetBool("midi.prompt.testing", false)) {
122
0
    bool allow = Preferences::GetBool("media.navigator.permission.disabled", false);
123
0
    if (allow) {
124
0
      Allow(JS::UndefinedHandleValue);
125
0
    } else {
126
0
      Cancel();
127
0
    }
128
0
    return NS_OK;
129
0
  }
130
0
131
0
  // If we already have sysex perms, allow.
132
0
  if (nsContentUtils::IsExactSitePermAllow(mPrincipal, "midi-sysex")) {
133
0
    Allow(JS::UndefinedHandleValue);
134
0
    return NS_OK;
135
0
  }
136
0
137
0
  // If we have no perms, or only have midi and are asking for sysex, pop dialog
138
0
  if (NS_FAILED(nsContentPermissionUtils::AskPermission(this, mWindow))) {
139
0
    Cancel();
140
0
    return NS_ERROR_FAILURE;
141
0
  }
142
0
  return NS_OK;
143
0
}
144