chromium/third_party/blink/public/common/permissions/permission_utils.h

// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_PUBLIC_COMMON_PERMISSIONS_PERMISSION_UTILS_H_
#define THIRD_PARTY_BLINK_PUBLIC_COMMON_PERMISSIONS_PERMISSION_UTILS_H_

#include <optional>
#include <string>

#include "third_party/blink/public/common/common_export.h"
#include "third_party/blink/public/mojom/permissions/permission.mojom-forward.h"
#include "third_party/blink/public/mojom/permissions/permission_status.mojom-shared.h"
#include "third_party/blink/public/mojom/permissions_policy/permissions_policy_feature.mojom-forward.h"

namespace blink {

// This enum is also used for UMA purposes, so it needs to adhere to
// the UMA guidelines.
// Make sure you update enums.xml and GetAllPermissionTypes if you add new
// or deprecate permission types.
// Never delete or reorder an entry; only add new entries
// immediately before PermissionType::NUM
enum class PermissionType {};

// Converts a permission string ("granted", "denied", "prompt") into a
// PermissionStatus.
BLINK_COMMON_EXPORT mojom::PermissionStatus ToPermissionStatus(
    const std::string& status);

// Converts `PermissionType` into a string.
BLINK_COMMON_EXPORT std::string GetPermissionString(PermissionType permission);

// Get a list of all permission types.
BLINK_COMMON_EXPORT const std::vector<PermissionType>& GetAllPermissionTypes();

// Given |descriptor|, set |permission_type| to a corresponding PermissionType.
BLINK_COMMON_EXPORT std::optional<PermissionType>
PermissionDescriptorToPermissionType(
    const mojom::PermissionDescriptorPtr& descriptor);

// Ideally this would be an equivalent function to
// |PermissionDescriptorToPermissionType| but for a
// `mojom::blink::PermissionDescriptorPtr` descriptor. But unfortunately mojo
// blink headers depend on blink/common so we can't introduce the reverse
// dependency. Instead we provide this function that requires the relevant
// information for making the decision and the caller needs to extract it from
// the descriptor and provide it.
BLINK_COMMON_EXPORT std::optional<PermissionType>
PermissionDescriptorInfoToPermissionType(
    mojom::PermissionName name,
    bool midi_sysex,
    bool camera_ptz,
    bool clipboard_will_be_sanitized,
    bool clipboard_has_user_gesture,
    bool fullscreen_allow_without_user_gesture);

// Converts `permission` type into the corresponding permission policy feature.
// If there is no, returns nullopt.
BLINK_COMMON_EXPORT std::optional<mojom::PermissionsPolicyFeature>
PermissionTypeToPermissionsPolicyFeature(PermissionType permission);

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_PUBLIC_COMMON_PERMISSIONS_PERMISSION_UTILS_H_