summaryrefslogtreecommitdiffstats
path: root/dom/permission/PermissionStatus.cpp
blob: fd3d2ebcacad3d23af8e3997ba9782b280cff34c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/dom/PermissionStatus.h"
#include "mozilla/PermissionDelegateHandler.h"

#include "mozilla/AsyncEventDispatcher.h"
#include "mozilla/Permission.h"
#include "mozilla/Services.h"
#include "nsIPermissionManager.h"
#include "PermissionObserver.h"
#include "PermissionUtils.h"

namespace mozilla::dom {

PermissionStatus::PermissionStatus(nsPIDOMWindowInner* aWindow,
                                   PermissionName aName)
    : DOMEventTargetHelper(aWindow),
      mName(aName),
      mState(PermissionState::Denied) {
  KeepAliveIfHasListenersFor(nsGkAtoms::onchange);
}

// https://w3c.github.io/permissions/#onchange-attribute and
// https://w3c.github.io/permissions/#query-method
RefPtr<PermissionStatus::SimplePromise> PermissionStatus::Init() {
  // Covers the onchange part
  // Whenever the user agent is aware that the state of a PermissionStatus
  // instance status has changed: ...
  // (The observer calls PermissionChanged() to do the steps)
  mObserver = PermissionObserver::GetInstance();
  if (NS_WARN_IF(!mObserver)) {
    return SimplePromise::CreateAndReject(NS_ERROR_FAILURE, __func__);
  }

  mObserver->AddSink(this);

  // Covers the query part (Step 8.2 - 8.4)
  return UpdateState();
}

PermissionStatus::~PermissionStatus() {
  if (mObserver) {
    mObserver->RemoveSink(this);
  }
}

JSObject* PermissionStatus::WrapObject(JSContext* aCx,
                                       JS::Handle<JSObject*> aGivenProto) {
  return PermissionStatus_Binding::Wrap(aCx, this, aGivenProto);
}

nsLiteralCString PermissionStatus::GetPermissionType() const {
  return PermissionNameToType(mName);
}

// Covers the calling part of "permission query algorithm" of query() method and
// update steps, which calls
// https://w3c.github.io/permissions/#dfn-default-permission-query-algorithm
// and then https://w3c.github.io/permissions/#dfn-permission-state
RefPtr<PermissionStatus::SimplePromise> PermissionStatus::UpdateState() {
  // Step 1: If settings wasn't passed, set it to the current settings object.
  // Step 2: If settings is a non-secure context, return "denied".
  // XXX(krosylight): No such steps here, and no WPT coverage?

  // The permission handler covers the rest of the steps, although the model
  // does not exactly match what the spec has. (Not passing "permission key" for
  // example)

  nsCOMPtr<nsPIDOMWindowInner> window = GetOwner();
  if (NS_WARN_IF(!window)) {
    return SimplePromise::CreateAndReject(NS_ERROR_FAILURE, __func__);
  }

  RefPtr<Document> document = window->GetExtantDoc();
  if (NS_WARN_IF(!document)) {
    return SimplePromise::CreateAndReject(NS_ERROR_FAILURE, __func__);
  }

  uint32_t action = nsIPermissionManager::DENY_ACTION;

  PermissionDelegateHandler* permissionHandler =
      document->GetPermissionDelegateHandler();
  if (NS_WARN_IF(!permissionHandler)) {
    return SimplePromise::CreateAndReject(NS_ERROR_FAILURE, __func__);
  }

  nsresult rv = permissionHandler->GetPermissionForPermissionsAPI(
      GetPermissionType(), &action);
  if (NS_WARN_IF(NS_FAILED(rv))) {
    return SimplePromise::CreateAndReject(rv, __func__);
  }

  mState = ActionToPermissionState(action);
  return SimplePromise::CreateAndResolve(NS_OK, __func__);
}

bool PermissionStatus::MaybeUpdatedBy(nsIPermission* aPermission) const {
  NS_ENSURE_TRUE(aPermission, false);
  nsCOMPtr<nsPIDOMWindowInner> window = GetOwner();
  if (NS_WARN_IF(!window)) {
    return false;
  }

  Document* doc = window->GetExtantDoc();
  if (NS_WARN_IF(!doc)) {
    return false;
  }

  nsCOMPtr<nsIPrincipal> principal =
      Permission::ClonePrincipalForPermission(doc->NodePrincipal());
  NS_ENSURE_TRUE(principal, false);
  nsCOMPtr<nsIPrincipal> permissionPrincipal;
  aPermission->GetPrincipal(getter_AddRefs(permissionPrincipal));
  if (!permissionPrincipal) {
    return false;
  }
  return permissionPrincipal->Equals(principal);
}

bool PermissionStatus::MaybeUpdatedByNotifyOnly(
    nsPIDOMWindowInner* aInnerWindow) const {
  return false;
}

// https://w3c.github.io/permissions/#dfn-permissionstatus-update-steps
void PermissionStatus::PermissionChanged() {
  auto oldState = mState;
  RefPtr<PermissionStatus> self(this);
  // Step 1: If this's relevant global object is a Window object, then:
  // Step 1.1: Let document be status's relevant global object's associated
  // Document.
  // Step 1.2: If document is null or document is not fully active,
  // terminate this algorithm.
  // TODO(krosylight): WPT /permissions/non-fully-active.https.html fails
  // because we don't do this. See bug 1876470.

  // Step 2 - 3 is covered by UpdateState()
  UpdateState()->Then(
      GetMainThreadSerialEventTarget(), __func__,
      [self, oldState]() {
        if (self->mState != oldState) {
          // Step 4: Queue a task on the permissions task source to fire an
          // event named change at status.
          RefPtr<AsyncEventDispatcher> eventDispatcher =
              new AsyncEventDispatcher(self.get(), u"change"_ns,
                                       CanBubble::eNo);
          eventDispatcher->PostDOMEvent();
        }
      },
      []() {

      });
}

void PermissionStatus::DisconnectFromOwner() {
  IgnoreKeepAliveIfHasListenersFor(nsGkAtoms::onchange);

  if (mObserver) {
    mObserver->RemoveSink(this);
    mObserver = nullptr;
  }

  DOMEventTargetHelper::DisconnectFromOwner();
}

void PermissionStatus::GetType(nsACString& aName) const {
  aName.Assign(GetPermissionType());
}

}  // namespace mozilla::dom