> For AI agents: the complete documentation index is available at /next/llms.txt, the full documentation bundle is available at /next/llms-full.txt.

# LynxExtensionModule

APISummary
`LynxExtensionModule` is the Desktop-only extension entry in the public embedder API. It lets the host hook into `LynxView` and BTS runtime lifecycle, expose NAPI exports, schedule BTS tasks, observe VSync, and register native views.

:::info

This API is Desktop only. It is supported on macOS and Windows, and is not part of the Android, iOS, Harmony, or Web native API surfaces.

:::

## Quick Access

Register an extension module globally through `LynxEnv::RegisterExtensionModule(...)`:

```cpp title="register_extension_module.cc"
#include "lynx_env.h"
#include "lynx_extension_module.h"
#include "lynx_view.h"

class DemoExtensionModule : public lynx::pub::LynxExtensionModule {
 public:
  void OnRuntimeAttach(
      Napi::Env env,
      std::unique_ptr<lynx::pub::VSyncObserver> vsync_observer) override {
    vsync_observer_ = std::move(vsync_observer);
  }

 private:
  std::unique_ptr<lynx::pub::VSyncObserver> vsync_observer_;
};

void RegisterExtensionModule() {
  auto& env = lynx::pub::LynxEnv::GetInstance();
  env.RegisterExtensionModule("DemoExtensionModule",
                              &CreateDemoExtensionModule,
                              false,
                              nullptr);
}
```

## Lifecycle

`LynxExtensionModule` exposes lifecycle callbacks across the UI thread and BTS thread:

- [`OnLynxViewCreate(...)`](/next/api/lynx-native-api/lynx-extension-module/on-lynx-view-create.md): called on the UI thread when a `LynxView` instance is created.
- [`OnLynxViewDestroy()`](/next/api/lynx-native-api/lynx-extension-module/on-lynx-view-destroy.md): called on the UI thread before the `LynxView` instance is destroyed.
- [`OnRuntimeInit()`](/next/api/lynx-native-api/lynx-extension-module/on-runtime-init.md): called on the UI thread when the BTS runtime instance is created.
- [`OnRuntimeAttach(Napi::Env env, std::unique_ptr<VSyncObserver> vsync_observer)`](/next/api/lynx-native-api/lynx-extension-module/on-runtime-attach.md): called on the BTS thread when the runtime is attached.
- [`OnRuntimeReady(Napi::Env env, Napi::Value lynx, const char* url)`](/next/api/lynx-native-api/lynx-extension-module/on-runtime-ready.md): called on the BTS thread when the runtime is ready.
- [`OnRuntimeDetach()`](/next/api/lynx-native-api/lynx-extension-module/on-runtime-detach.md): called on the BTS thread when the runtime is detached.
- [`OnEnterForeground()`](/next/api/lynx-native-api/lynx-extension-module/on-enter-foreground.md): called on the UI thread when the application enters foreground.
- [`OnEnterBackground()`](/next/api/lynx-native-api/lynx-extension-module/on-enter-background.md): called on the UI thread when the application enters background.
- [`Destroy()`](/next/api/lynx-native-api/lynx-extension-module/destroy.md): called on the BTS thread before the extension module instance is released.

## Runtime Utilities

`LynxExtensionModule` also exposes Desktop runtime helpers:

- [`PostTaskToRuntime(...)`](/next/api/lynx-native-api/lynx-extension-module/post-task-to-runtime.md): posts work to the BTS thread.
- [`IsRunningTasksOnBTSThread()`](/next/api/lynx-native-api/lynx-extension-module/is-running-tasks-on-bts-thread.md): checks whether the current execution is already on the BTS thread.
- [`SetCModule(...)`](/next/api/lynx-native-api/lynx-extension-module/set-c-module.md): associates the module with its C API counterpart.
- [`SetNapiModuleCreator(...)`](/next/api/lynx-native-api/lynx-extension-module/set-napi-module-creator.md): installs the NAPI exports creator for the module.
- `SetNapiInstanceData(...)` / `GetNapiInstanceData(...)`: stores and reads keyed NAPI instance data.
- [`Retain()`](/next/api/lynx-native-api/lynx-extension-module/retain.md) / [`Release()`](/next/api/lynx-native-api/lynx-extension-module/release.md): adjusts the lifetime of the module instance.

`VSyncObserver`, received in [`OnRuntimeAttach(...)`](/next/api/lynx-native-api/lynx-extension-module/on-runtime-attach.md), provides:

- [`RequestAnimationFrame(...)`](/next/api/lynx-native-api/lynx-extension-module/request-animation-frame.md)
- [`RequestBeforeAnimationFrame(...)`](/next/api/lynx-native-api/lynx-extension-module/request-before-animation-frame.md)
- [`RegisterAfterAnimationFrameListener(...)`](/next/api/lynx-native-api/lynx-extension-module/register-after-animation-frame-listener.md)

## Native View as Extension Capability

Native view capability belongs to the Desktop extension surface rather than a separate native API owner.

Use `LynxNativeView` when you need to embed a platform view or external texture inside the Lynx view tree. The C++ base class exposes these callbacks:

- `OnCreate()`
- `OnAttach()`
- `OnDetach()`
- `OnDestroy()`
- `OnLayoutChanged(...)`
- `OnPropertiesChanged(...)`
- `OnMotionEvent(...)`
- `OnFocusChanged(...)`
- `OnMethodInvoked(...)`

It also exposes helper methods such as:

- `TriggerEvent(...)`
- `RequestFocus()`
- `MarkDirty()`
- `PresentSurface(...)`
- `AcquireSurface(...)`
- `SwapBack()`

### Register Native View

You can register a native view in three common places:

- Globally through `LynxView::Builder::RegisterNativeView<T>(...)`
- Per view through `LynxView::RegisterNativeView<T>(...)`
- Inside an extension module during [`OnLynxViewCreate(...)`](/next/api/lynx-native-api/lynx-extension-module/on-lynx-view-create.md)

The extension-module pattern is useful when a native view should always be installed together with the extension itself. In practice, this registration is usually performed from [`OnLynxViewCreate(...)`](/next/api/lynx-native-api/lynx-extension-module/on-lynx-view-create.md):

```cpp title="cef_extension_module.cc"
#include "lynx_view.h"

class CEFExtensionModule : public lynx::pub::LynxExtensionModule {
 public:
  void InstallNativeView(lynx::pub::LynxView& view) {
    view.RegisterNativeView<CEFWebView>("webview");
  }
};
```

A standalone native view implementation typically subclasses `LynxNativeView`:

```cpp title="fake_view.h"
#include "lynx_native_view.h"

class FakeView : public lynx::pub::LynxNativeView {
 public:
  bool OnCreate() override { return true; }

  void OnLayoutChanged(float left,
                       float top,
                       float width,
                       float height,
                       float pixel_ratio) override {
    lynx::pub::LynxValue detail(lynx::pub::LynxValue::kCreateAsMapTag);
    detail.SetProperty("width", lynx::pub::LynxValue(width));
    detail.SetProperty("height", lynx::pub::LynxValue(height));
    TriggerEvent("resize", std::move(detail));
  }

  void OnMethodInvoked(const char* method,
                       const lynx::pub::LynxValue& attrs,
                       std::function<void(int, lynx::pub::LynxValue&&)> callback) override {
    callback(kSuccess, lynx::pub::LynxValue(lynx::pub::LynxValue::kCreateAsNullTag));
  }
};
```

## Compatibility

**Compatibility Table**

**Query:** `lynx-native-api.lynx-extension-module`

[Source](https://github.com/lynx-family/lynx-website/tree/main/packages/lynx-compat-data/lynx-native-api/lynx-extension-module.json)

| Feature | Android | HarmonyOS | iOS | macOS | Windows | Web |
| --- | --- | --- | --- | --- | --- | --- |
| lynx-extension-module | No | No | No | Yes (3.7) | Yes (3.7) | No |

**`lynx-extension-module`**

Desktop-only extension module entry for lifecycle hooks, NAPI binding, VSync, and native view registration.

**Specifications:**

