创建新的 Electron 浏览器模块¶
欢迎阅读 Electron API 指南!如果您不熟悉在 browser 目录中创建新的 Electron API 模块,本指南可用作您需要实现的一些必要步骤的清单。
这并不是一份创建 Electron 浏览器 API 的详尽无遗的指南,而是一份记录其中一些较不直观步骤的提纲。
将您的文件添加到 Electron 的项目配置¶
Electron 使用 GN 作为元构建系统,为其编译器 Ninja 生成文件。这意味着,为了让 Electron 编译您的代码,我们必须将您 API 的代码和头文件名添加到 filenames.gni 中。
您需要将您的 API 文件名按字母顺序追加到相应的文件中,如下所示:
lib_sources = [
"path/to/api/api_name.cc",
"path/to/api/api_name.h",
]
lib_sources_mac = [
"path/to/api/api_name_mac.h",
"path/to/api/api_name_mac.mm",
]
lib_sources_win = [
"path/to/api/api_name_win.cc",
"path/to/api/api_name_win.h",
]
lib_sources_linux = [
"path/to/api/api_name_linux.cc",
"path/to/api/api_name_linux.h",
]
请注意,Windows、macOS 和 Linux 数组的添加项是可选的,仅当您的 API 具有特定平台实现时才应添加。
创建 API 文档¶
类型定义由 Electron 使用 @electron/docs-parser 和 @electron/typescript-definitions 生成。此步骤对于确保 Electron API 文档的一致性必不可少。这意味着,为了让您的 API 类型定义出现在 electron.d.ts 文件中,我们必须创建一个 .md 文件。示例可在此文件夹中找到。
设置 ObjectTemplateBuilder 和 Wrappable¶
Electron 使用 object_template_builder 构造其模块。
wrappable 是拥有相应 v8 包装对象的 C++ 对象的基类。
以下是一个您可能需要添加的基本代码示例,以将 object_template_builder 和 wrappable 集成到您的 API 中。如需进一步参考,您可以在此处找到更多实现。
在您的 api_name.h 文件中:
#ifndef ELECTRON_SHELL_BROWSER_API_ELECTRON_API_{API_NAME}_H_
#define ELECTRON_SHELL_BROWSER_API_ELECTRON_API_{API_NAME}_H_
#include "gin/wrappable.h"
namespace electron {
namespace api {
class ApiName final : public gin::Wrappable<ApiName> {
public:
static ApiName* Create(v8::Isolate* isolate);
// gin::Wrappable
static const gin::WrapperInfo kWrapperInfo;
void Trace(cppgc::Visitor* visitor) const override;
const gin::WrapperInfo* wrapper_info() const override;
gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
v8::Isolate* isolate) override;
const char* GetHumanReadableName() const override;
// 公有,供 cppgc::MakeGarbageCollected 使用。
ApiName();
~ApiName() override;
void MethodName();
};
} // namespace api
} // namespace electron
在您的 api_name.cc 文件中:
#include "shell/browser/api/electron_api_api_name.h"
#include "gin/object_template_builder.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/gin_helper/wrappable_pointer_tags.h"
#include "shell/common/node_includes.h"
#include "v8/include/cppgc/allocation.h"
#include "v8/include/v8-cppgc.h"
namespace electron {
namespace api {
const gin::WrapperInfo ApiName::kWrapperInfo =
electron::MakeWrapperInfo(electron::kElectronApiName);
ApiName::ApiName() = default;
ApiName::~ApiName() = default;
// static
ApiName* ApiName::Create(v8::Isolate* isolate) {
return cppgc::MakeGarbageCollected<ApiName>(
isolate->GetCppHeap()->GetAllocationHandle());
}
void ApiName::Trace(cppgc::Visitor* visitor) const {
gin::Wrappable<ApiName>::Trace(visitor);
}
const gin::WrapperInfo* ApiName::wrapper_info() const {
return &kWrapperInfo;
}
gin::ObjectTemplateBuilder ApiName::GetObjectTemplateBuilder(
v8::Isolate* isolate) {
return gin::Wrappable<ApiName>::GetObjectTemplateBuilder(isolate)
.SetMethod("methodName", &ApiName::MethodName);
}
const char* ApiName::GetHumanReadableName() const {
return "Electron / ApiName";
}
void ApiName::MethodName() {}
} // namespace api
} // namespace electron
namespace {
void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Value> unused,
v8::Local<v8::Context> context,
void* priv) {
v8::Isolate* const isolate = v8::Isolate::GetCurrent();
gin_helper::Dictionary dict(isolate, exports);
dict.Set("apiName", electron::api::ApiName::Create(isolate));
}
} // namespace
在 shell/common/gin_helper/wrappable_pointer_tags.h 中,为 API 的 gin::WrapperInfo 添加一个唯一的 kElectronApiName 值。
将您的 Electron API 与 Node 链接¶
在 typings/internal-ambient.d.ts 文件中,我们需要向 Process 接口追加一个新属性,如下所示:
```ts title='typings/internal-ambient.d.ts' @ts-nocheck @noformat interface Process { linkedBinding(name: 'electron_browser'): Electron.ApiName; }
在您的 `api_name.cc` 文件的最底部:
```cpp title='api_name.cc'
NODE_LINKED_BINDING_CONTEXT_AWARE(electron_browser_{api_name},Initialize)
在您的 shell/common/node_bindings.cc 文件中,将您的 Node 绑定名称添加到 Electron 的内置模块中。
[!NOTE] 有关 Node 如何与 Electron 链接的更多技术细节,请参见我们的博客。
向 TypeScript 公开您的 API¶
将您的 API 导出为模块¶
我们需要在以下路径创建一个新的 TypeScript 文件:
"lib/browser/api/{electron_browser_{api_name}}.ts"
有关此文件内容的示例,请参见此处。
向 TypeScript 公开您的模块¶
将您的模块添加到 "lib/browser/api/module-list.ts" 中的模块列表,如下所示:
ts title='lib/browser/api/module-list.ts' @ts-nocheck @noformat
export const browserModuleList: ElectronInternal.ModuleEntry[] = [
{ name: 'apiName', loader: () => require('./api-name') }
];
本页原文 Markdown:在 AtomGit 查看·内容源自开源项目 el/electron