# -*- Mode:Python; tab-width:8; indent-tabs-mode:nil -*- */
# vim: set ts=8 sts=4 et sw=4 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/.

# WebExtension WebIDL API Bindings Configuration, used by the script
# `dom/extensions-webidl/GenerateWebIDLBindingsFromJSONSchema.py`
# to customize the WebIDL generated based on the WebExtensions API JSON Schemas.
#
# Generating the WebIDL definitions for some of the WebExtensions API does require
# some special handling, there are corresponding entries in the configuration tables
# below.
#

# Mapping table between the JSON Schema types (represented as keys of the map)
# and the related WebIDL type (represented by the value in the map).
# Any mapping missing from this table will fallback to use the "any" webidl type
# (See GenerateWebIDLBindings.py WebIDLHelpers.webidl_type_from_mapping method).
#
# NOTE: Please keep this table in alphabetic order (upper and lower case in two
# separate alphabetic orders, group of the upcase ones first).
WEBEXT_TYPES_MAPPING = {
    "ExpectedError": "any", # Only used in test.assertThrows/assertRejects
    "Port": "ExtensionPort",
    "Promise": "Promise<any>",
    "StreamFilter": "ExtensionStreamFilter",
    "any": "any",
    "boolean": "boolean",
    "number": "float",
    "function": "Function",
    "integer": "long",
    "object": "any", # TODO: as a follow up we may look into generating webidl dictionaries to achieve a more precise mapping
    "runtime.Port": "ExtensionPort",
    "string": "DOMString",
    "types.Setting": "ExtensionSetting",
}

# Set of the types from the WEBEXT_TYPES_MAPPING that will be threated as primitive
# types (e.g. used to omit optional attribute in the WebIDL methods type signatures).
#
# NOTE: Please keep this table in alphabetic order (upper and lower case in two
# separate alphabetic orders, group of the update ones first).
WEBIDL_PRIMITIVE_TYPES = set([
    "DOMString",
    "boolean",
    "float"
    "long",
])

# Mapping table for some APIs that do require special handling and a
# specific stub method should be set in the generated webidl extended
# attribute `WebExtensionStub`.
#
# The key in this map represent the API method name (including the
# API namespace that is part of), the value is the value to set on the
# `WebExtensionStub` webidl extended attribute:
#
#     "namespace.methodName": "WebExtensionStubName",
#
# NOTE: Please keep this table in alphabetic order.
WEBEXT_STUBS_MAPPING = {
    "dns.resolve": "AsyncAmbiguous",
    "runtime.connect": "ReturnsPort",
    "runtime.connectNative": "ReturnsPort",
    "runtime.getURL": "ReturnsString",
    # TODO: Bug 1782690 - This method accepts functions/args so we'll need to
    # serialize them.
    "scripting.executeScript": "NotImplementedAsync",
    "scripting.getRegisteredContentScripts": "AsyncAmbiguous",
    "scripting.unregisterContentScripts": "AsyncAmbiguous",
    "test.assertEq": "AssertEq",
    "test.assertRejects": False, # No WebExtensionStub attribute.
    "test.assertThrows": False, # No WebExtensionStub attribute.
    "test.withHandlingUserInput": "NotImplementedNoReturn",
}

WEBEXT_WORKER_HIDDEN_SET = set([
    "runtime.getFrameId",
    "runtime.getBackgroundPage",
])

# Mapping table for the directories where the JSON API schema will be loaded
# from.
WEBEXT_SCHEMADIRS_MAPPING = {
    "toolkit": ["toolkit", "components", "extensions", "schemas"],
    "browser": ["browser", "components", "extensions", "schemas"],
    "mobile": ["mobile", "shared", "components", "extensions", "schemas"],
}

# List of toolkit-level WebExtensions API namespaces that are not included
# in android builds.
#
# NOTE: keep this list in sync with the API namespaces excluded in
# - toolkit/components/extensions/jar.mn
# - toolkit/components/extensions/schemas/jar.mn
WEBEXT_ANDROID_EXCLUDED = [
    "captivePortal",
    "geckoProfiler",
    "identity"
]
