Creating a New Electron Browser Module

Welcome to the Electron API guide! If you are unfamiliar with creating a new Electron API module within the browser directory, this guide serves as a checklist for some of the necessary steps that you will need to implement.

This is not a comprehensive end-all guide to creating an Electron Browser API, rather an outline documenting some of the more unintuitive steps.

Add your files to Electron’s project configuration

Electron uses GN as a meta build system to generate files for its compiler, Ninja. This means that in order to tell Electron to compile your code, we have to add your API’s code and header file names into filenames.gni.

You will need to append your API file names alphabetically into the appropriate files like so:

filenames.gni

  1. lib_sources = [
  2. "path/to/api/api_name.cc",
  3. "path/to/api/api_name.h",
  4. ]
  5. lib_sources_mac = [
  6. "path/to/api/api_name_mac.h",
  7. "path/to/api/api_name_mac.mm",
  8. ]
  9. lib_sources_win = [
  10. "path/to/api/api_name_win.cc",
  11. "path/to/api/api_name_win.h",
  12. ]
  13. lib_sources_linux = [
  14. "path/to/api/api_name_linux.cc",
  15. "path/to/api/api_name_linux.h",
  16. ]

Note that the Windows, macOS and Linux array additions are optional and should only be added if your API has specific platform implementations.

Create API documentation

Type definitions are generated by Electron using @electron/docs-parser and @electron/typescript-definitions. This step is necessary to ensure consistency across Electron’s API documentation. This means that for your API type definition to appear in the electron.d.ts file, we must create a .md file. Examples can be found in this folder.

Set up ObjectTemplateBuilder and Wrappable

Electron constructs its modules using object_template_builder.

wrappable is a base class for C++ objects that have corresponding v8 wrapper objects.

Here is a basic example of code that you may need to add, in order to incorporate object_template_builder and wrappable into your API. For further reference, you can find more implementations here.

In your api_name.h file:

api_name.h

  1. #ifndef ELECTRON_SHELL_BROWSER_API_ELECTRON_API_{API_NAME}_H_
  2. #define ELECTRON_SHELL_BROWSER_API_ELECTRON_API_{API_NAME}_H_
  3. #include "gin/handle.h"
  4. #include "gin/wrappable.h"
  5. namespace electron {
  6. namespace api {
  7. class ApiName : public gin::Wrappable<ApiName> {
  8. public:
  9. static gin::Handle<ApiName> Create(v8::Isolate* isolate);
  10. // gin::Wrappable
  11. static gin::WrapperInfo kWrapperInfo;
  12. gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
  13. v8::Isolate* isolate) override;
  14. const char* GetTypeName() override;
  15. } // namespace api
  16. } // namespace electron

In your api_name.cc file:

api_name.cc

  1. #include "shell/browser/api/electron_api_safe_storage.h"
  2. #include "shell/browser/browser.h"
  3. #include "shell/common/gin_converters/base_converter.h"
  4. #include "shell/common/gin_converters/callback_converter.h"
  5. #include "shell/common/gin_helper/dictionary.h"
  6. #include "shell/common/gin_helper/object_template_builder.h"
  7. #include "shell/common/node_includes.h"
  8. #include "shell/common/platform_util.h"
  9. namespace electron {
  10. namespace api {
  11. gin::WrapperInfo ApiName::kWrapperInfo = {gin::kEmbedderNativeGin};
  12. gin::ObjectTemplateBuilder ApiName::GetObjectTemplateBuilder(
  13. v8::Isolate* isolate) {
  14. return gin::ObjectTemplateBuilder(isolate)
  15. .SetMethod("methodName", &ApiName::methodName);
  16. }
  17. const char* ApiName::GetTypeName() {
  18. return "ApiName";
  19. }
  20. // static
  21. gin::Handle<ApiName> ApiName::Create(v8::Isolate* isolate) {
  22. return gin::CreateHandle(isolate, new ApiName());
  23. }
  24. } // namespace api
  25. } // namespace electron
  26. namespace {
  27. void Initialize(v8::Local<v8::Object> exports,
  28. v8::Local<v8::Value> unused,
  29. v8::Local<v8::Context> context,
  30. void* priv) {
  31. v8::Isolate* isolate = context->GetIsolate();
  32. gin_helper::Dictionary dict(isolate, exports);
  33. dict.Set("apiName", electron::api::ApiName::Create(isolate));
  34. }
  35. } // namespace

In the typings/internal-ambient.d.ts file, we need to append a new property onto the Process interface like so:

typings/internal-ambient.d.ts

  1. interface Process {
  2. _linkedBinding(name: 'electron_browser_{api_name}'): Electron.ApiName;
  3. }

At the very bottom of your api_name.cc file:

api_name.cc

  1. NODE_LINKED_BINDING_CONTEXT_AWARE(electron_browser_{api_name},Initialize)

In your shell/common/node_bindings.cc file, add your node binding name to Electron’s built-in modules.

shell/common/node_bindings.cc

  1. #define ELECTRON_BROWSER_MODULES(V) \
  2. V(electron_browser_{api_name})

Note: More technical details on how Node links with Electron can be found on our blog.

Expose your API to TypeScript

Export your API as a module

We will need to create a new TypeScript file in the path that follows:

"lib/browser/api/{electron_browser_{api_name}}.ts"

An example of the contents of this file can be found here.

Expose your module to TypeScript

Add your module to the module list found at "lib/browser/api/module-list.ts" like so:

lib/browser/api/module-list.ts

  1. export const browserModuleList: ElectronInternal.ModuleEntry[] = [
  2. { name: 'apiName', loader: () => require('./api-name') },
  3. ];