summaryrefslogtreecommitdiffstats
path: root/xpcom/docs/writing-xpcom-interface.rst
blob: 9eeb1c72a2569d06719b455f5b4e3e30e0b6fa55 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
.. _writing_xpcom_interface:

Tutorial for Writing a New XPCOM Interface
==========================================

High Level Overview
-------------------

In order to write code that works in native code (C++, Rust), and JavaScript contexts, it's necessary to have a mechanism to do so. For chrome privileged contexts, this is the XPCOM Interface Class.

This mechanism starts with an :ref:`XPIDL` file to define the shape of the interface. In the `build system`_, this file is processed, and `Rust`_ and `C++`_ code is automatically generated.

.. _build system: https://searchfox.org/mozilla-central/source/xpcom/idl-parser/xpidl
.. _Rust: https://searchfox.org/mozilla-central/source/__GENERATED__/dist/xpcrs/rt
.. _C++: https://searchfox.org/mozilla-central/source/__GENERATED__/dist/include

Next, the interface's methods and attributes must be implemented. This can be done through either a JSM module, or through a C++ interface class. Once these steps are done, the new files must be added to the appropriate :code:`moz.build` files to ensure the build system knows how to find them and process them.

Often these XPCOM components are wired into the :code:`Services` JavaScript object to allow for ergonomic access to the interface. For example, open the `Browser Console`_ and type :code:`Services.` to interactively access these components.

.. _Browser Console: https://developer.mozilla.org/en-US/docs/Tools/Browser_Console

From C++, components can be accessed via :code:`mozilla::components::ComponentName::Create()` using the :code:`name` option in the :code:`components.conf`.

While :code:`Services` and :code:`mozilla::components` are the preferred means of accessing components, many are accessed through the historical (and somewhat arcane) :code:`createInstance` mechanism. New usage of these mechanisms should be avoided if possible.

.. code:: javascript

    let component = Cc["@mozilla.org/component-name;1"].createInstance(
      Ci.nsIComponentName
    );

.. code:: c++

    nsCOMPtr<nsIComponentName> component = do_CreateInstance(
      "@mozilla.org/component-name;1");

Writing an XPIDL
----------------

First decide on a name. Conventionally the interfaces are prefixed with :code:`nsI` (historically Netscape) or :code:`mozI` as they are defined in the global namespace. While the interface is global, the implementation of an interface can be defined in a namespace with no prefix. Historically many component implementations still use the :code:`ns` prefixes (notice that the :code:`I` was dropped), but this convention is no longer needed.

This tutorial assumes the component is located at :code:`path/to` with the name :code:`ComponentName`. The interface name will be :code:`nsIComponentName`, while the implementation will be :code:`mozilla::ComponentName`.

To start, create an :ref:`XPIDL` file:

.. code:: bash

    touch path/to/nsIComponentName.idl

And hook it up to the :code:`path/to/moz.build`

.. code:: python

    XPIDL_SOURCES += [
        "nsIComponentName.idl",
    ]

Next write the initial :code:`.idl` file: :code:`path/to/nsIComponentName.idl`

.. _contract_ids:
.. code:: c++

    /* 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/. */

    // This is the base include which defines nsISupports. This class defines
    // the QueryInterface method.
    #include "nsISupports.idl"

    // `scriptable` designates that this object will be used with JavaScript
    // `uuid`       The example below uses a UUID with all Xs. Replace the Xs with
    //              your own UUID generated here:
    //              http://mozilla.pettay.fi/cgi-bin/mozuuid.pl

    /**
     * Make sure to document your interface.
     */
    [scriptable, uuid(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)]
    interface nsIComponentName : nsISupports {

      // Fill out your definition here. This example attribute only returns a bool.

      /**
       * Make sure to document your attributes.
       */
      readonly attribute bool isAlive;
    };

This definition only includes one attribute, :code:`isAlive`, which will demonstrate that we've done our work correctly at the end. For a more comprehensive guide for this syntax, see the :ref:`XPIDL` docs.

Once :code:`./mach build` is run, the XPIDL parser will read this file, and give any warnings if the syntax is wrong. It will then auto-generate the C++ (or Rust) code for us. For this example the generated :code:`nsIComponentName` class will be located in:

:code:`{obj-directory}/dist/include/nsIComponentName.h`

It might be useful to check out what was automatically generated here, or see the existing `generated C++ header files on SearchFox <https://searchfox.org/mozilla-central/source/__GENERATED__/dist/>`_.

Writing the C++ implementation
------------------------------

Now we have a definition for an interface, but no implementation. The interface could be backed by a JavaScript implementation using a JSM, but for this example we'll use a C++ implementation.

Add the C++ sources to :code:`path/to/moz.build`

.. code:: python

    EXPORTS.mozilla += [
        "ComponentName.h",
    ]

    UNIFIED_SOURCES += [
        "ComponentName.cpp",
    ]

Now write the header: :code:`path/to/ComponentName.h`

.. code:: c++

    /* 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/. */
    #ifndef mozilla_nsComponentName_h__
    #define mozilla_nsComponentName_h__

    // This will pull in the header auto-generated by the .idl file:
    // {obj-directory}/dist/include/nsIComponentName.h
    #include "nsIComponentName.h"

    // The implementation can be namespaced, while the XPCOM interface is globally namespaced.
    namespace mozilla {

    // Notice how the class name does not need to be prefixed, as it is defined in the
    // `mozilla` namespace.
    class ComponentName final : public nsIComponentName {
      // This first macro includes the necessary information to use the base nsISupports.
      // This includes the QueryInterface method.
      NS_DECL_ISUPPORTS

      // This second macro includes the declarations for the attributes. There is
      // no need to duplicate these declarations.
      //
      // In our case it includes a declaration for the isAlive attribute:
      //   GetIsAlive(bool *aIsAlive)
      NS_DECL_NSICOMPONENTNAME

     public:
      ComponentName() = default;

     private:
      // A private destructor must be declared.
      ~ComponentName() = default;
    };

    }  // namespace mozilla

    #endif

Now write the definitions: :code:`path/to/ComponentName.cpp`

.. code:: c++

    /* 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 "ComponentName.h"

    namespace mozilla {

    // Use the macro to inject all of the definitions for nsISupports.
    NS_IMPL_ISUPPORTS(ComponentName, nsIComponentName)

    // This is the actual implementation of the `isAlive` attribute. Note that the
    // method name is somewhat different than the attribute. We specified "read-only"
    // in the attribute, so only a getter, not a setter was defined for us. Here
    // the name was adjusted to be `GetIsAlive`.
    //
    // Another common detail of implementing an XPIDL interface is that the return values
    // are passed as out parameters. The methods are treated as fallible, and the return
    // value is an `nsresult`. See the XPIDL documentation for the full nitty gritty
    // details.
    //
    // A common way to know the exact function signature for a method implementation is
    // to copy and paste from existing examples, or inspecting the generated file
    // directly: {obj-directory}/dist/include/nsIComponentName.h
    NS_IMETHODIMP
    ComponentName::GetIsAlive(bool* aIsAlive) {
      *aIsAlive = true;
      return NS_OK;
    }

    } // namespace: mozilla

Registering the component
-------------------------

At this point, the component should be correctly written, but it's not registered with the component system. In order to this, we'll need to create or modify the :code:`components.conf`.

.. code:: bash

    touch path/to/components.conf


Now update the :code:`moz.build` to point to it.

.. code:: python

    XPCOM_MANIFESTS += [
        "components.conf",
    ]

It is probably worth reading over :ref:`defining_xpcom_components`, but the following config will be sufficient to hook up our component to the :code:`Services` object.
Services should also be added to ``tools/lint/eslint/eslint-plugin-mozilla/lib/services.json``.
The easiest way to do that is to copy from ``<objdir>/xpcom/components/services.json``.

.. code:: python

    Classes = [
        {
            # This CID is the ID for component entries, and needs a separate UUID from
            # the .idl file. Replace the Xs with a uuid from:
            # http://mozilla.pettay.fi/cgi-bin/mozuuid.pl
            'cid': '{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}',
            'interfaces': ['nsIComponentName'],

            # A contract ID is a human-readable identifier for an _implementation_ of
            # an XPCOM interface.
            #
            # "@mozilla.org/process/environment;1"
            #  ^^^^^^^^^^^^ ^^^^^^^ ^^^^^^^^^^^ ^
            #  |            |       |           |
            #  |            |       |           The version number, usually just 1.
            #  |            |       Component name
            #  |            Module
            #  Domain
            #
            # This design goes back to a time when XPCOM was intended to be a generalized
            # solution for the Gecko Runtime Environment (GRE). At this point most (if
            # not all) of mozilla-central has an @mozilla domain.
            'contract_ids': ['@mozilla.org/component-name;1'],

            # This is the name of the C++ type that implements the interface.
            'type': 'mozilla::ComponentName',

            # The header file to pull in for the implementation of the interface.
            'headers': ['path/to/ComponentName.h'],

            # In order to hook up this interface to the `Services` object, we can
            # provide the "js_name" parameter. This is an ergonomic way to access
            # the component.
            'js_name': 'componentName',
        },
    ]

At this point the full :code:`moz.build` file should look like:

.. code:: python

    # -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
    # vim: set filetype=python:
    # 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/.

    XPIDL_SOURCES += [
        "nsIComponentName.idl",
    ]

    XPCOM_MANIFESTS += [
        "components.conf",
    ]

    EXPORTS.mozilla += [
        "ComponentName.h",
    ]

    UNIFIED_SOURCES += [
        "ComponentName.cpp",
    ]

This completes the implementation of a basic XPCOM Interface using C++. The component should be available via the `Browser Console`_ or other chrome contexts.

.. code:: javascript

    console.log(Services.componentName.isAlive);
    > true