Commit dc5d9051 authored by reillyg's avatar reillyg Committed by Commit bot

Add PermissionDescriptor to the permissions Mojo interface.

A permission descriptor is a structure describing a permission that an
origin may request. Most permissions are only identified by their names
but some have additional parameters. For example, a Bluetooth device
permission may include the MAC address of the device (or other unique
identifier) so that the permission applies to only the device selected
by the user through some sort of chooser interface.

BUG=638721

Review-Url: https://codereview.chromium.org/2255933002
Cr-Commit-Position: refs/heads/master@{#422744}
parent cac47f30
......@@ -12,6 +12,7 @@
#include "content/public/browser/permission_manager.h"
#include "content/public/browser/permission_type.h"
using blink::mojom::PermissionDescriptorPtr;
using blink::mojom::PermissionName;
using blink::mojom::PermissionStatus;
......@@ -19,18 +20,22 @@ namespace content {
namespace {
PermissionType PermissionNameToPermissionType(PermissionName name) {
switch(name) {
PermissionType PermissionDescriptorToPermissionType(
const PermissionDescriptorPtr& descriptor) {
switch (descriptor->name) {
case PermissionName::GEOLOCATION:
return PermissionType::GEOLOCATION;
case PermissionName::NOTIFICATIONS:
return PermissionType::NOTIFICATIONS;
case PermissionName::PUSH_NOTIFICATIONS:
return PermissionType::PUSH_MESSAGING;
case PermissionName::MIDI:
case PermissionName::MIDI: {
if (descriptor->extension && descriptor->extension->is_midi() &&
descriptor->extension->get_midi()->sysex) {
return PermissionType::MIDI_SYSEX;
}
return PermissionType::MIDI;
case PermissionName::MIDI_SYSEX:
return PermissionType::MIDI_SYSEX;
}
case PermissionName::PROTECTED_MEDIA_IDENTIFIER:
return PermissionType::PROTECTED_MEDIA_IDENTIFIER;
case PermissionName::DURABLE_STORAGE:
......@@ -106,7 +111,7 @@ void PermissionServiceImpl::OnConnectionError() {
}
void PermissionServiceImpl::RequestPermission(
PermissionName permission,
PermissionDescriptorPtr permission,
const url::Origin& origin,
bool user_gesture,
const PermissionStatusCallback& callback) {
......@@ -121,15 +126,15 @@ void PermissionServiceImpl::RequestPermission(
DCHECK(browser_context);
if (!context_->render_frame_host() ||
!browser_context->GetPermissionManager()) {
callback.Run(GetPermissionStatusFromName(permission, origin));
callback.Run(GetPermissionStatus(permission, origin));
return;
}
int pending_request_id = pending_requests_.Add(new PendingRequest(
base::Bind(&PermissionRequestResponseCallbackWrapper, callback), 1));
int id = browser_context->GetPermissionManager()->RequestPermission(
PermissionNameToPermissionType(permission), context_->render_frame_host(),
GURL(origin.Serialize()), user_gesture,
PermissionDescriptorToPermissionType(permission),
context_->render_frame_host(), GURL(origin.Serialize()), user_gesture,
base::Bind(&PermissionServiceImpl::OnRequestPermissionResponse,
weak_factory_.GetWeakPtr(), pending_request_id));
......@@ -150,7 +155,7 @@ void PermissionServiceImpl::OnRequestPermissionResponse(
}
void PermissionServiceImpl::RequestPermissions(
const std::vector<PermissionName>& permissions,
std::vector<PermissionDescriptorPtr> permissions,
const url::Origin& origin,
bool user_gesture,
const RequestPermissionsCallback& callback) {
......@@ -166,16 +171,15 @@ void PermissionServiceImpl::RequestPermissions(
if (!context_->render_frame_host() ||
!browser_context->GetPermissionManager()) {
std::vector<PermissionStatus> result(permissions.size());
for (size_t i = 0; i < permissions.size(); ++i) {
result[i] = GetPermissionStatusFromName(permissions[i], origin);
}
for (size_t i = 0; i < permissions.size(); ++i)
result[i] = GetPermissionStatus(permissions[i], origin);
callback.Run(result);
return;
}
std::vector<PermissionType> types(permissions.size());
for (size_t i = 0; i < types.size(); ++i)
types[i] = PermissionNameToPermissionType(permissions[i]);
types[i] = PermissionDescriptorToPermissionType(permissions[i]);
int pending_request_id = pending_requests_.Add(
new PendingRequest(callback, permissions.size()));
......@@ -233,17 +237,18 @@ void PermissionServiceImpl::CancelPendingOperations() {
}
void PermissionServiceImpl::HasPermission(
PermissionName permission,
PermissionDescriptorPtr permission,
const url::Origin& origin,
const PermissionStatusCallback& callback) {
callback.Run(GetPermissionStatusFromName(permission, origin));
callback.Run(GetPermissionStatus(permission, origin));
}
void PermissionServiceImpl::RevokePermission(
PermissionName permission,
PermissionDescriptorPtr permission,
const url::Origin& origin,
const PermissionStatusCallback& callback) {
PermissionType permission_type = PermissionNameToPermissionType(permission);
PermissionType permission_type =
PermissionDescriptorToPermissionType(permission);
PermissionStatus status =
GetPermissionStatusFromType(permission_type, origin);
......@@ -260,12 +265,11 @@ void PermissionServiceImpl::RevokePermission(
}
void PermissionServiceImpl::GetNextPermissionChange(
PermissionName permission,
PermissionDescriptorPtr permission,
const url::Origin& origin,
PermissionStatus last_known_status,
const PermissionStatusCallback& callback) {
PermissionStatus current_status =
GetPermissionStatusFromName(permission, origin);
PermissionStatus current_status = GetPermissionStatus(permission, origin);
if (current_status != last_known_status) {
callback.Run(current_status);
return;
......@@ -278,7 +282,8 @@ void PermissionServiceImpl::GetNextPermissionChange(
return;
}
PermissionType permission_type = PermissionNameToPermissionType(permission);
PermissionType permission_type =
PermissionDescriptorToPermissionType(permission);
// We need to pass the id of PendingSubscription in pending_subscriptions_
// to the callback but SubscribePermissionStatusChange() will also return an
......@@ -298,11 +303,11 @@ void PermissionServiceImpl::GetNextPermissionChange(
weak_factory_.GetWeakPtr(), pending_subscription_id));
}
PermissionStatus PermissionServiceImpl::GetPermissionStatusFromName(
PermissionName permission,
PermissionStatus PermissionServiceImpl::GetPermissionStatus(
const PermissionDescriptorPtr& permission,
const url::Origin& origin) {
return GetPermissionStatusFromType(PermissionNameToPermissionType(permission),
origin);
return GetPermissionStatusFromType(
PermissionDescriptorToPermissionType(permission), origin);
}
PermissionStatus PermissionServiceImpl::GetPermissionStatusFromType(
......
......@@ -72,23 +72,23 @@ class PermissionServiceImpl : public blink::mojom::PermissionService {
using SubscriptionsMap = IDMap<PendingSubscription, IDMapOwnPointer>;
// blink::mojom::PermissionService.
void HasPermission(blink::mojom::PermissionName permission,
void HasPermission(blink::mojom::PermissionDescriptorPtr permission,
const url::Origin& origin,
const PermissionStatusCallback& callback) override;
void RequestPermission(blink::mojom::PermissionName permission,
void RequestPermission(blink::mojom::PermissionDescriptorPtr permission,
const url::Origin& origin,
bool user_gesture,
const PermissionStatusCallback& callback) override;
void RequestPermissions(
const std::vector<blink::mojom::PermissionName>& permissions,
std::vector<blink::mojom::PermissionDescriptorPtr> permissions,
const url::Origin& origin,
bool user_gesture,
const RequestPermissionsCallback& callback) override;
void RevokePermission(blink::mojom::PermissionName permission,
void RevokePermission(blink::mojom::PermissionDescriptorPtr permission,
const url::Origin& origin,
const PermissionStatusCallback& callback) override;
void GetNextPermissionChange(
blink::mojom::PermissionName permission,
blink::mojom::PermissionDescriptorPtr permission,
const url::Origin& origin,
blink::mojom::PermissionStatus last_known_status,
const PermissionStatusCallback& callback) override;
......@@ -101,8 +101,8 @@ class PermissionServiceImpl : public blink::mojom::PermissionService {
int pending_request_id,
const std::vector<blink::mojom::PermissionStatus>& result);
blink::mojom::PermissionStatus GetPermissionStatusFromName(
blink::mojom::PermissionName permission,
blink::mojom::PermissionStatus GetPermissionStatus(
const blink::mojom::PermissionDescriptorPtr& permission,
const url::Origin& origin);
blink::mojom::PermissionStatus GetPermissionStatusFromType(
PermissionType type,
......
......@@ -16,17 +16,26 @@ namespace {
using Type = media::MediaPermission::Type;
blink::mojom::PermissionName MediaPermissionTypeToPermissionName(Type type) {
blink::mojom::PermissionDescriptorPtr MediaPermissionTypeToPermissionDescriptor(
Type type) {
auto descriptor = blink::mojom::PermissionDescriptor::New();
switch (type) {
case Type::PROTECTED_MEDIA_IDENTIFIER:
return blink::mojom::PermissionName::PROTECTED_MEDIA_IDENTIFIER;
descriptor->name =
blink::mojom::PermissionName::PROTECTED_MEDIA_IDENTIFIER;
break;
case Type::AUDIO_CAPTURE:
return blink::mojom::PermissionName::AUDIO_CAPTURE;
descriptor->name = blink::mojom::PermissionName::AUDIO_CAPTURE;
break;
case Type::VIDEO_CAPTURE:
return blink::mojom::PermissionName::VIDEO_CAPTURE;
descriptor->name = blink::mojom::PermissionName::VIDEO_CAPTURE;
break;
default:
NOTREACHED() << type;
descriptor->name =
blink::mojom::PermissionName::PROTECTED_MEDIA_IDENTIFIER;
}
NOTREACHED();
return blink::mojom::PermissionName::PROTECTED_MEDIA_IDENTIFIER;
return descriptor;
}
} // namespace
......@@ -72,7 +81,8 @@ void MediaPermissionDispatcher::HasPermission(
DVLOG(2) << __func__ << ": request ID " << request_id;
permission_service_->HasPermission(
MediaPermissionTypeToPermissionName(type), url::Origin(security_origin),
MediaPermissionTypeToPermissionDescriptor(type),
url::Origin(security_origin),
base::Bind(&MediaPermissionDispatcher::OnPermissionStatus, weak_ptr_,
request_id));
}
......@@ -98,7 +108,8 @@ void MediaPermissionDispatcher::RequestPermission(
DVLOG(2) << __func__ << ": request ID " << request_id;
permission_service_->RequestPermission(
MediaPermissionTypeToPermissionName(type), url::Origin(security_origin),
MediaPermissionTypeToPermissionDescriptor(type),
url::Origin(security_origin),
blink::WebUserGestureIndicator::isProcessingUserGesture(),
base::Bind(&MediaPermissionDispatcher::OnPermissionStatus, weak_ptr_,
request_id));
......
......@@ -149,8 +149,8 @@ let geolocationServiceMock = loadMojoModules(
* for a call if necessary. Any permission request that is not for
* geolocation is always denied.
*/
requestPermission(permissionName) {
if (permissionName != permission.PermissionName.GEOLOCATION)
requestPermission(permissionDescriptor) {
if (permissionDescriptor.name != permission.PermissionName.GEOLOCATION)
return Promise.resolve(permissionStatus.PermissionStatus.DENIED);
return new Promise(resolve => {
......
......@@ -7,6 +7,7 @@ include_rules = [
"+modules/EventModules.h",
"+modules/ModulesExport.h",
"+modules/geolocation",
"+modules/permissions",
"+platform",
"+public/platform",
"-web",
......
......@@ -33,8 +33,8 @@
#include "core/frame/Settings.h"
#include "modules/geolocation/Coordinates.h"
#include "modules/geolocation/GeolocationError.h"
#include "modules/permissions/PermissionUtils.h"
#include "platform/UserGestureIndicator.h"
#include "platform/mojo/MojoHelper.h"
#include "public/platform/InterfaceProvider.h"
#include "public/platform/Platform.h"
#include "wtf/Assertions.h"
......@@ -432,7 +432,7 @@ void Geolocation::requestPermission() {
// Ask the embedder: it maintains the geolocation challenge policy itself.
m_permissionService->RequestPermission(
mojom::blink::PermissionName::GEOLOCATION,
createPermissionDescriptor(mojom::blink::PermissionName::GEOLOCATION),
getExecutionContext()->getSecurityOrigin(),
UserGestureIndicator::processingUserGesture(),
convertToBaseCallback(WTF::bind(
......
......@@ -8,7 +8,7 @@
#include "bindings/core/v8/ScriptState.h"
#include "modules/notifications/Notification.h"
#include "modules/notifications/NotificationPermissionCallback.h"
#include "modules/permissions/Permissions.h"
#include "modules/permissions/PermissionUtils.h"
#include "platform/UserGestureIndicator.h"
#include "platform/weborigin/SecurityOrigin.h"
#include "public/platform/InterfaceProvider.h"
......@@ -66,8 +66,7 @@ ScriptPromise NotificationManager::requestPermission(
ExecutionContext* context = scriptState->getExecutionContext();
if (!m_permissionService) {
Permissions::connectToService(context,
mojo::GetProxy(&m_permissionService));
connectToPermissionService(context, mojo::GetProxy(&m_permissionService));
m_permissionService.set_connection_error_handler(convertToBaseCallback(
WTF::bind(&NotificationManager::onPermissionServiceConnectionError,
wrapWeakPersistent(this))));
......@@ -77,7 +76,8 @@ ScriptPromise NotificationManager::requestPermission(
ScriptPromise promise = resolver->promise();
m_permissionService->RequestPermission(
mojom::blink::PermissionName::NOTIFICATIONS, context->getSecurityOrigin(),
createPermissionDescriptor(mojom::blink::PermissionName::NOTIFICATIONS),
context->getSecurityOrigin(),
UserGestureIndicator::processingUserGesture(),
convertToBaseCallback(
WTF::bind(&NotificationManager::onPermissionRequestComplete,
......
......@@ -10,6 +10,8 @@ blink_modules_sources("permissions") {
"NavigatorPermissions.h",
"PermissionStatus.cpp",
"PermissionStatus.h",
"PermissionUtils.cpp",
"PermissionUtils.h",
"Permissions.cpp",
"Permissions.h",
"WorkerNavigatorPermissions.cpp",
......
......@@ -8,7 +8,7 @@
#include "core/dom/Document.h"
#include "core/events/Event.h"
#include "modules/EventTargetModulesNames.h"
#include "modules/permissions/Permissions.h"
#include "modules/permissions/PermissionUtils.h"
#include "public/platform/Platform.h"
#include "wtf/Functional.h"
......@@ -17,17 +17,17 @@ namespace blink {
// static
PermissionStatus* PermissionStatus::take(ScriptPromiseResolver* resolver,
MojoPermissionStatus status,
MojoPermissionName name) {
MojoPermissionDescriptor descriptor) {
return PermissionStatus::createAndListen(resolver->getExecutionContext(),
status, name);
status, std::move(descriptor));
}
PermissionStatus* PermissionStatus::createAndListen(
ExecutionContext* executionContext,
MojoPermissionStatus status,
MojoPermissionName name) {
MojoPermissionDescriptor descriptor) {
PermissionStatus* permissionStatus =
new PermissionStatus(executionContext, status, name);
new PermissionStatus(executionContext, status, std::move(descriptor));
permissionStatus->suspendIfNeeded();
permissionStatus->startListening();
return permissionStatus;
......@@ -35,11 +35,11 @@ PermissionStatus* PermissionStatus::createAndListen(
PermissionStatus::PermissionStatus(ExecutionContext* executionContext,
MojoPermissionStatus status,
MojoPermissionName name)
MojoPermissionDescriptor descriptor)
: ActiveScriptWrappable(this),
ActiveDOMObject(executionContext),
m_status(status),
m_name(name) {}
m_descriptor(std::move(descriptor)) {}
PermissionStatus::~PermissionStatus() {
stopListening();
......@@ -59,8 +59,10 @@ void PermissionStatus::permissionChanged(MojoPermissionStatus status) {
m_status = status;
dispatchEvent(Event::create(EventTypeNames::change));
m_service->GetNextPermissionChange(
m_name, getExecutionContext()->getSecurityOrigin(), m_status,
m_descriptor->Clone(), getExecutionContext()->getSecurityOrigin(),
m_status,
convertToBaseCallback(WTF::bind(&PermissionStatus::permissionChanged,
wrapWeakPersistent(this))));
}
......@@ -83,10 +85,10 @@ void PermissionStatus::stop() {
void PermissionStatus::startListening() {
DCHECK(!m_service);
Permissions::connectToService(getExecutionContext(),
mojo::GetProxy(&m_service));
connectToPermissionService(getExecutionContext(), mojo::GetProxy(&m_service));
m_service->GetNextPermissionChange(
m_name, getExecutionContext()->getSecurityOrigin(), m_status,
m_descriptor->Clone(), getExecutionContext()->getSecurityOrigin(),
m_status,
convertToBaseCallback(WTF::bind(&PermissionStatus::permissionChanged,
wrapWeakPersistent(this))));
}
......
......@@ -26,17 +26,17 @@ class PermissionStatus final : public EventTargetWithInlineData,
USING_GARBAGE_COLLECTED_MIXIN(PermissionStatus);
DEFINE_WRAPPERTYPEINFO();
using MojoPermissionName = mojom::blink::PermissionName;
using MojoPermissionDescriptor = mojom::blink::PermissionDescriptorPtr;
using MojoPermissionStatus = mojom::blink::PermissionStatus;
public:
static PermissionStatus* take(ScriptPromiseResolver*,
MojoPermissionStatus,
MojoPermissionName);
MojoPermissionDescriptor);
static PermissionStatus* createAndListen(ExecutionContext*,
MojoPermissionStatus,
MojoPermissionName);
MojoPermissionDescriptor);
~PermissionStatus() override;
// EventTarget implementation.
......@@ -52,20 +52,22 @@ class PermissionStatus final : public EventTargetWithInlineData,
void stop() override;
String state() const;
void permissionChanged(mojom::blink::PermissionStatus);
void permissionChanged(MojoPermissionStatus);
DEFINE_ATTRIBUTE_EVENT_LISTENER(change);
DECLARE_VIRTUAL_TRACE();
private:
PermissionStatus(ExecutionContext*, MojoPermissionStatus, MojoPermissionName);
PermissionStatus(ExecutionContext*,
MojoPermissionStatus,
MojoPermissionDescriptor);
void startListening();
void stopListening();
MojoPermissionStatus m_status;
MojoPermissionName m_name;
MojoPermissionDescriptor m_descriptor;
mojom::blink::PermissionServicePtr m_service;
};
......
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "modules/permissions/PermissionUtils.h"
#include "core/dom/Document.h"
#include "core/dom/ExecutionContext.h"
#include "core/frame/LocalFrame.h"
#include "public/platform/InterfaceProvider.h"
#include "public/platform/Platform.h"
namespace blink {
using mojom::blink::PermissionDescriptor;
using mojom::blink::PermissionDescriptorPtr;
using mojom::blink::PermissionName;
bool connectToPermissionService(
ExecutionContext* executionContext,
mojom::blink::PermissionServiceRequest request) {
InterfaceProvider* interfaceProvider = nullptr;
if (executionContext->isDocument()) {
Document* document = toDocument(executionContext);
if (document->frame())
interfaceProvider = document->frame()->interfaceProvider();
} else {
interfaceProvider = Platform::current()->interfaceProvider();
}
if (interfaceProvider)
interfaceProvider->getInterface(std::move(request));
return interfaceProvider;
}
PermissionDescriptorPtr createPermissionDescriptor(PermissionName name) {
auto descriptor = PermissionDescriptor::New();
descriptor->name = name;
return descriptor;
}
PermissionDescriptorPtr createMidiPermissionDescriptor(bool sysex) {
auto descriptor =
createPermissionDescriptor(mojom::blink::PermissionName::MIDI);
auto midiExtension = mojom::blink::MidiPermissionDescriptor::New();
midiExtension->sysex = sysex;
descriptor->extension = mojom::blink::PermissionDescriptorExtension::New();
descriptor->extension->set_midi(std::move(midiExtension));
return descriptor;
}
} // namespace blink
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef PermissionUtils_h
#define PermissionUtils_h
#include "public/platform/modules/permissions/permission.mojom-blink.h"
namespace blink {
class ExecutionContext;
bool connectToPermissionService(ExecutionContext*,
mojom::blink::PermissionServiceRequest);
mojom::blink::PermissionDescriptorPtr createPermissionDescriptor(
mojom::blink::PermissionName);
mojom::blink::PermissionDescriptorPtr createMidiPermissionDescriptor(
bool sysex);
} // namespace blink
#endif // PermissionUtils_h
......@@ -25,10 +25,6 @@ class Permissions final : public GarbageCollectedFinalized<Permissions>,
public:
DEFINE_INLINE_TRACE() {}
// TODO(mlamouri): Find better place for this. https://crbug.com/510948
static bool connectToService(ExecutionContext*,
mojom::blink::PermissionServiceRequest);
ScriptPromise query(ScriptState*, const Dictionary&);
ScriptPromise request(ScriptState*, const Dictionary&);
ScriptPromise revoke(ScriptState*, const Dictionary&);
......@@ -38,10 +34,10 @@ class Permissions final : public GarbageCollectedFinalized<Permissions>,
mojom::blink::PermissionService* getService(ExecutionContext*);
void serviceConnectionError();
void taskComplete(ScriptPromiseResolver*,
mojom::blink::PermissionName,
mojom::blink::PermissionDescriptorPtr,
mojom::blink::PermissionStatus);
void batchTaskComplete(ScriptPromiseResolver*,
Vector<mojom::blink::PermissionName>,
Vector<mojom::blink::PermissionDescriptorPtr>,
Vector<int>,
const Vector<mojom::blink::PermissionStatus>&);
......
......@@ -9,7 +9,7 @@
#include "core/dom/DOMException.h"
#include "core/dom/Document.h"
#include "core/dom/ExceptionCode.h"
#include "modules/permissions/Permissions.h"
#include "modules/permissions/PermissionUtils.h"
#include "modules/quota/StorageEstimate.h"
#include "platform/StorageQuotaCallbacks.h"
#include "platform/UserGestureIndicator.h"
......@@ -83,7 +83,7 @@ ScriptPromise StorageManager::persist(ScriptState* scriptState) {
return promise;
}
permissionService->RequestPermission(
PermissionName::DURABLE_STORAGE,
createPermissionDescriptor(PermissionName::DURABLE_STORAGE),
scriptState->getExecutionContext()->getSecurityOrigin(),
UserGestureIndicator::processingUserGesture(),
convertToBaseCallback(
......@@ -105,7 +105,7 @@ ScriptPromise StorageManager::persisted(ScriptState* scriptState) {
return promise;
}
permissionService->HasPermission(
PermissionName::DURABLE_STORAGE,
createPermissionDescriptor(PermissionName::DURABLE_STORAGE),
scriptState->getExecutionContext()->getSecurityOrigin(),
convertToBaseCallback(
WTF::bind(&StorageManager::permissionRequestComplete,
......@@ -138,11 +138,11 @@ ScriptPromise StorageManager::estimate(ScriptState* scriptState) {
DEFINE_TRACE(StorageManager) {}
mojom::blink::PermissionService* StorageManager::getPermissionService(
PermissionService* StorageManager::getPermissionService(
ExecutionContext* executionContext) {
if (!m_permissionService &&
Permissions::connectToService(executionContext,
mojo::GetProxy(&m_permissionService)))
connectToPermissionService(executionContext,
mojo::GetProxy(&m_permissionService)))
m_permissionService.set_connection_error_handler(convertToBaseCallback(
WTF::bind(&StorageManager::permissionServiceConnectionError,
wrapWeakPersistent(this))));
......
......@@ -6,6 +6,7 @@ include_rules = [
"+modules/EventModules.h",
"+modules/EventTargetModules.h",
"+modules/ModulesExport.h",
"+modules/permissions",
"+modules/webmidi",
"+platform",
"+public/platform",
......
......@@ -11,19 +11,19 @@
#include "core/dom/ExceptionCode.h"
#include "core/frame/LocalFrame.h"
#include "core/frame/Navigator.h"
#include "modules/permissions/PermissionUtils.h"
#include "modules/webmidi/MIDIAccess.h"
#include "modules/webmidi/MIDIOptions.h"
#include "modules/webmidi/MIDIPort.h"
#include "platform/UserGestureIndicator.h"
#include "platform/mojo/MojoHelper.h"
#include "public/platform/InterfaceProvider.h"
#include "third_party/WebKit/public/platform/modules/permissions/permission.mojom-blink.h"
#include "public/platform/modules/permissions/permission.mojom-blink.h"
namespace blink {
using PortState = WebMIDIAccessorClient::MIDIPortState;
using mojom::blink::PermissionName;
using mojom::blink::PermissionStatus;
MIDIAccessInitializer::MIDIAccessInitializer(ScriptState* scriptState,
......@@ -39,22 +39,11 @@ ScriptPromise MIDIAccessInitializer::start() {
ScriptPromise promise = this->promise();
m_accessor = MIDIAccessor::create(this);
Document* document = toDocument(getExecutionContext());
DCHECK(document);
document->frame()->interfaceProvider()->getInterface(
mojo::GetProxy(&m_permissionService));
bool requestSysEx = m_options.hasSysex() && m_options.sysex();
Vector<PermissionName> permissions;
permissions.resize(requestSysEx ? 2 : 1);
permissions[0] = PermissionName::MIDI;
if (requestSysEx)
permissions[1] = PermissionName::MIDI_SYSEX;
m_permissionService->RequestPermissions(
permissions, getExecutionContext()->getSecurityOrigin(),
connectToPermissionService(getExecutionContext(),
mojo::GetProxy(&m_permissionService));
m_permissionService->RequestPermission(
createMidiPermissionDescriptor(m_options.hasSysex() && m_options.sysex()),
getExecutionContext()->getSecurityOrigin(),
UserGestureIndicator::processingUserGesture(),
convertToBaseCallback(WTF::bind(
&MIDIAccessInitializer::onPermissionsUpdated, wrapPersistent(this))));
......@@ -130,17 +119,9 @@ ExecutionContext* MIDIAccessInitializer::getExecutionContext() const {
return getScriptState()->getExecutionContext();
}
void MIDIAccessInitializer::onPermissionsUpdated(
const Vector<PermissionStatus>& statusArray) {
bool allowed = true;
for (const auto status : statusArray) {
if (status != PermissionStatus::GRANTED) {
allowed = false;
break;
}
}
void MIDIAccessInitializer::onPermissionsUpdated(PermissionStatus status) {
m_permissionService.reset();
if (allowed)
if (status == PermissionStatus::GRANTED)
m_accessor->startSession();
else
reject(DOMException::create(SecurityError));
......
......@@ -93,7 +93,7 @@ class MODULES_EXPORT MIDIAccessInitializer : public ScriptPromiseResolver,
void contextDestroyed() override;
void onPermissionsUpdated(const Vector<mojom::blink::PermissionStatus>&);
void onPermissionsUpdated(mojom::blink::PermissionStatus);
void onPermissionUpdated(mojom::blink::PermissionStatus);
std::unique_ptr<MIDIAccessor> m_accessor;
......
......@@ -12,7 +12,6 @@ enum PermissionName {
NOTIFICATIONS,
PUSH_NOTIFICATIONS,
MIDI,
MIDI_SYSEX,
PROTECTED_MEDIA_IDENTIFIER,
DURABLE_STORAGE,
AUDIO_CAPTURE,
......@@ -20,26 +19,42 @@ enum PermissionName {
BACKGROUND_SYNC,
};
struct MidiPermissionDescriptor {
bool sysex;
};
// Union of possible extensions to the base PermissionDescriptor type.
union PermissionDescriptorExtension {
MidiPermissionDescriptor midi;
};
// This struct roughly corresponds to the PermissionDescriptor dictionary as
// defined by the Permissions API.
struct PermissionDescriptor {
PermissionName name;
PermissionDescriptorExtension? extension;
};
// The Permission service provides permission handling capabilities by exposing
// methods to check, request, and revoke permissions. It also allows a client to
// start listening to permission changes.
interface PermissionService {
HasPermission(PermissionName permission, url.mojom.Origin origin)
HasPermission(PermissionDescriptor permission, url.mojom.Origin origin)
=> (PermissionStatus status);
RequestPermission(PermissionName permission, url.mojom.Origin origin,
RequestPermission(PermissionDescriptor permission, url.mojom.Origin origin,
bool user_gesture)
=> (PermissionStatus status);
RequestPermissions(array<PermissionName> permission, url.mojom.Origin origin,
RequestPermissions(array<PermissionDescriptor> permission, url.mojom.Origin origin,
bool user_gesture)
=> (array<PermissionStatus> statuses);
RevokePermission(PermissionName permission, url.mojom.Origin origin)
RevokePermission(PermissionDescriptor permission, url.mojom.Origin origin)
=> (PermissionStatus status);
// Runs the callback next time there is a permission status change for the
// given { permission, origin }. Callers of this method will have to call it
// again if they want to keep listening to the changes. To prevent race
// conditions, the caller must pass the last known value.
GetNextPermissionChange(PermissionName permission,
GetNextPermissionChange(PermissionDescriptor permission,
url.mojom.Origin origin,
PermissionStatus last_known_status)
=> (PermissionStatus status);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment