the Chromium logo

The Chromium Projects

Web IDL interfaces

Web interfaces – exposed as JavaScript objects – are generally specified in Web IDL (Interface Definition Language), a declarative language (sometimes written without the space as WebIDL). This is the language used in standard specifications, and Blink uses IDL files to specify the interface and generate JavaScript bindings (formally, C++ code that the V8 JavaScript virtual machine uses to call Blink itself). Web IDL in Blink is close to the standard, and the resulting bindings use standard conventions to call Blink code, but there are additional features to specify implementation details, primarily Blink IDL extended attributes.

To implement a new Web IDL interface in Blink:

The bulk of the work is the implementation, secondarily tests. Writing an IDL file should require minimal work (ideally just copy-and-paste the spec), assuming nothing unusual is being done, and the build can be forgotten about once you've set it up. Details follow.

If you do not intend to expose the IDL to mobile platforms e.g. android you can exclude the source files and the idl files by checking for the target_os buildflag. Example idl_in_modules.gni.

If excluding Android, also update not-webview-exposed.txt with the excluded IDLs.

Web IDL

The initial IDL file should contain:

See Blink IDL: Style for style guide.

IDL files contain two types of data:

Note that if Blink behavior differs from the spec, the Blink IDL file should reflect Blink behavior. This makes interface differences visible, rather than hiding them in the C++ implementation or bindings generator.

Also as a rule, nop data should not be included: if Blink (bindings generator) ignores an IDL keyword or extended attribute, do not include it, as it suggests a difference in behavior when there is none. If this results in a difference from the spec, this is good, as it makes the difference in behavior visible.

Initially you likely want to comment out all attributes and operations, uncommenting them as you implement them.

Nulls and non-finite numbers

Two points to be careful of, and which are often incorrect in specs, particularly older specs, are nullability and non-finite values (infinities and NaN). These are both to ensure correct type checking. If these are incorrect in the spec – for example, a prose specifying behavior on non-finite values, but the IDL not reflecting this – please file a spec bug upstream, and link to it in the IDL file.

If null values are valid (for attributes, argument types, or method return values), the type MUST be marked with a ? to indicate nullable, as in attribute Foo? foo;

Similarly, IEEE floating point allows non-finite numbers (infinities and NaN); if these are valid, the floating point type – float or double – MUST be marked as unrestricted as in unrestricted float or unrestricted double – the bare float or double means finite floating point.

Ref: double, unrestricted double, Type mapping: double, Type mapping: unrestricted double

Union types

Many older specs use overloading when a union type argument would be clearer. Please match spec, but file a spec bug for these and link to it. For example:

// FIXME: should be void bar((long or Foo) foo); https://www.w3.org/Bugs/Public/show_bug.cgi?id=123
void bar(long foo);
void bar(Foo foo);

Also, beware that you can't have multiple nullable arguments in the distinguishing position in an overload, as these are not distinguishing (what does null resolve to?). This is best resolved by using a union type if possible; otherwise make sure to mark only one overload as having a nullable argument in that position.

Don't do this:

void zork(Foo? x);
void zork(Bar? x); // What does zork(null) resolve to?

Instead do this:

void zork(Foo? x);
void zork(Bar x);

...but preferably this:

void zork((Foo or Bar)? x);

Extended attributes

You will often need to add Blink-specific extended attributes to specify implementation details.

Please comment extended attributes – why do you need special behavior?

Bindings

See Web IDL in Blink.

C++

Bindings code assumes that a C++ class exists for each interface, with methods for each attribute or operation (with some exceptions). Attributes are implemented as properties, meaning that while in the JavaScript interface these are read and written as attributes, in C++ these are read and written by getter and setter methods.

Names

The class and methods have default names, which can be overridden by the [ImplementedAs] extended attribute; this is strongly discouraged, and method names should align with the spec unless there is very good reason for them to differ (this is sometimes necessary when there is a conflict, say when inheriting another interface).

Given an IDL file foo.idl:

interface Foo {
    attribute long a;
    attribute DOMString cssText;
    void f();
    void f(long arg);
    void g(optional long arg);
};

...a minimal header file foo.h illustrating this is:

class Foo {
 public:
  int32_t a();
  void setA(int32_t value);
  String cssText();
  void setCSSText(const String&);
  void f();
  void f(int32_t arg);
  void g();
  void g(int32_t arg);
};

Type information ("ScriptWrappable")

Blink objects that are visible in JavaScript need type information, fundamentally because JavaScript is dynamically typed (so values have type), concretely because the bindings code uses type introspection for dynamic dispatch (function resolution of bindings functions): given a C++ object (representing the implementation of a JavaScript object), accessing it from V8 requires calling the correct C++ binding methods, which requires knowing its JavaScript type (i.e., the IDL interface type).

Blink does not use C++ run-time type information (RTTI), and thus the type information must be stored separately.

There are various ways this is done, most simply (for Blink developers) by the C++ class inheriting ScriptWrappable and placing DEFINE_WRAPPERTYPEINFO in the class declaration. Stylistically ScriptWrappable or its inheritance MUST be the first class.

Explicitly:

foo.h:

#ifndef third_party_blink_renderer_core_foo_foo_h
#define third_party_blink_renderer_core_foo_foo_h
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
namespace blink {
class Foo : public ScriptWrappable /* maybe others */ {
  DEFINE_WRAPPERTYPEINFO();
 public:
  // ...
};
} // namespace blink
#endif third_party_blink_renderer_core_foo_foo_h

Garbage Collection

reference](https://chromium.googlesource.com/chromium/src/+/HEAD/third_party/blink/renderer/platform/heap/BlinkGCAPIReference.md)

Build

You need to list the .idl file and .h/.cc files in the correct GN variable so that they will be built (bindings generated, Blink code compiled.)

Now we are replacing bindings code generators from version 1 to 2, and thus you have to work for both generators.

There are 3 dichotomies in these .idl files, which affect where you list them in the build:

For the code generator v1, see comments in core_idl_files.gni and/or modules_idl_files.gni in which GN variable your IDL file should be listed. And if your IDL file defines a callback function or a new union type, see generated.gni for core component or generated.gni for modules component in which GN variable generated files should be listed.

For the code generator v2, see idl_in_core.gni or idl_in_modules.gni for IDL files, and generated_in_core.gni or generated_in_modules.gni for generated files.

Tests

Make sure to test:

Subtyping

There are three mechanisms for subtyping in IDL:

The corresponding C++ implementations are as follows, here illustrated for attribute T foo;

IDL files SHOULD agree with spec, and almost always MUST do so. It is not ok to change the kind of subtyping or move members between interfaces, and violations SHOULD or MUST be fixed:

Technical details

While members of an interface definition, members of interface mixin, and members of partial interfaces are identical for JavaScript, partial interface members – and members of certain interface mixin, namely those with the [TreatAsPartial] extended attribute – are treated differently internally in Blink (see below).

Inheritance and implements are both interface inheritance. JavaScript has single inheritance, and IDL inheritance corresponds to JavaScript inheritance, while IDL includes provides multiple inheritance in IDL, which does not correspond to inheritance in JavaScript.

In both cases, by spec, members of the inherited interface or interface mixin must be implemented on the JavaScript object implementing the interface. Concretely, members of inherited interfaces are implemented as properties on the prototype object of the parent interface, while members of interface mixins are implemented as properties of the implementing interface.

In C++, members of an interface definition and members of implemented interfaces are implemented on the C++ object (referred to as the parameter or variable impl) implementing the JavaScript object. Specifically this is done in the Blink class corresponding to the IDL interface or a base class – the C++ hierarchy is invisible to both JavaScript and the bindings.

Implementation-wise, inheritance and implements differ in two ways:

For simplicity, in the wrapper (used by V8 to call Blink) the bindings just treat members of implemented interfaces and partial interfaces as if they were part of the main interface: there is no multiple inheritance in the bindings implementation.

If (IDL) interface A inherits from interface B, then usually (C++) class A inherits from class B, meaning that:

interface A : B { /* ... */ };

is usually implemented as:

class A : B { /* ... */ };

...or perhaps:

class A : C { /* ... */ };
class C : B { /* ... */ };

However, the bindings are agnostic about this, and simply set the prototype in the wrapper object to be the inherited interface (concretely, sets the parentClass attribute in the WrapperTypeInfo of the class's bindings). Dispatch is thus done in JavaScript.

"A includes B;" should mean that members declared in (IDL) interface mixin B are members of (C++) classes implementing A.

Partial interfaces formally are type extension (external type extension, since specified in a separate place from the original definition), and in principle are simply part of the interface, just defined separately, as a convenience for spec authors. However in practice, members of partial interfaces are not assumed to be implemented on the C++ object (impl), and are not defined in the Blink class implementing the interface. Instead, they are implemented as static members of a separate class, which take impl as their first argument. This is done because in practice, partial interfaces are type extensions, which often only used in subtypes or are deactivated (via conditionals or as runtime enabled features), and we do not want to bloat the main Blink class to include these.

Further, in some cases we must use type extension (static methods) for implemented interfaces as well. This is due to componentization in Blink (see Browser Components), currently core versus modules. Code in core cannot inherit from code in modules, and thus if an interface in core implements an interface in modules, this must be implemented via type extension (static methods in modules). This is an exceptional case, and indicates that Blink's internal layering (componentization) disagrees with the layering implied by the IDL specs, and formally should be resolved by moving the relevant interface from modules to core. This is not always possible or desirable (for internal implementation reasons), and thus static methods can be specified via the [TreatAsPartial] extended attribute on the implemented interface.

Inheritance and code reuse

IDL has single inheritance, which maps directly to JavaScript inheritance (prototype chain). C++ has multiple inheritance, and the two hierarchies need not be related.

IDL has 3 mechanisms for combining interfaces:

See also

Other Blink interfaces, not standard Web IDL interfaces:

For reference, documentation by other projects.