understanding-tauri-process-model

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Tauri Process Model

Tauri 进程模型

Tauri implements a multi-process architecture similar to Electron and modern web browsers. Understanding this model is essential for building secure, performant Tauri applications.
Tauri 采用了与 Electron 和现代浏览器类似的多进程架构。理解该模型是构建安全、高性能 Tauri 应用的关键。

Architecture Overview

架构概述

+------------------------------------------------------------------+
|                        TAURI APPLICATION                          |
+------------------------------------------------------------------+
|                                                                   |
|  +-----------------------------+                                  |
|  |       CORE PROCESS          |                                  |
|  |         (Rust)              |                                  |
|  |                             |                                  |
|  |  +----------------------+   |                                  |
|  |  | Window Manager       |   |                                  |
|  |  +----------------------+   |                                  |
|  |  | System Tray          |   |                                  |
|  |  +----------------------+   |                                  |
|  |  | Global State         |   |                                  |
|  |  +----------------------+   |                                  |
|  |  | IPC Router           |   |                                  |
|  |  +----------------------+   |                                  |
|  |  | OS Abstractions      |   |                                  |
|  +-------------+---------------+                                  |
|                |                                                  |
|                | IPC (Inter-Process Communication)                |
|                |                                                  |
|     +----------+----------+----------+                            |
|     |          |          |          |                            |
|     v          v          v          v                            |
|  +------+  +------+  +------+  +------+                           |
|  |WebView|  |WebView|  |WebView|  |WebView|                       |
|  |  #1   |  |  #2   |  |  #3   |  |  #N   |                       |
|  +------+  +------+  +------+  +------+                           |
|  | HTML |  | HTML |  | HTML |  | HTML |                           |
|  | CSS  |  | CSS  |  | CSS  |  | CSS  |                           |
|  | JS   |  | JS   |  | JS   |  | JS   |                           |
|  +------+  +------+  +------+  +------+                           |
|                                                                   |
+------------------------------------------------------------------+
+------------------------------------------------------------------+
|                        TAURI APPLICATION                          |
+------------------------------------------------------------------+
|                                                                   |
|  +-----------------------------+                                  |
|  |       CORE PROCESS          |                                  |
|  |         (Rust)              |                                  |
|  |                             |                                  |
|  |  +----------------------+   |                                  |
|  |  | Window Manager       |   |                                  |
|  |  +----------------------+   |                                  |
|  |  | System Tray          |   |                                  |
|  |  +----------------------+   |                                  |
|  |  | Global State         |   |                                  |
|  |  +----------------------+   |                                  |
|  |  | IPC Router           |   |                                  |
|  |  +----------------------+   |                                  |
|  |  | OS Abstractions      |   |                                  |
|  +-------------+---------------+                                  |
|                |                                                  |
|                | IPC (Inter-Process Communication)                |
|                |                                                  |
|     +----------+----------+----------+                            |
|     |          |          |          |                            |
|     v          v          v          v                            |
|  +------+  +------+  +------+  +------+                           |
|  |WebView|  |WebView|  |WebView|  |WebView|                       |
|  |  #1   |  |  #2   |  |  #3   |  |  #N   |                       |
|  +------+  +------+  +------+  +------+                           |
|  | HTML |  | HTML |  | HTML |  | HTML |                           |
|  | CSS  |  | CSS  |  | CSS  |  | CSS  |                           |
|  | JS   |  | JS   |  | JS   |  | JS   |                           |
|  +------+  +------+  +------+  +------+                           |
|                                                                   |
+------------------------------------------------------------------+

The Core Process

Core 进程

The Core process is the application's entry point and central hub. It runs Rust code and has exclusive access to operating system capabilities.
Core 进程是应用的入口点和中心枢纽,运行 Rust 代码并拥有操作系统功能的独家访问权限。

Responsibilities

职责

ResponsibilityDescription
Window ManagementCreates and orchestrates application windows
System IntegrationManages system tray menus and notifications
IPC RoutingHandles all inter-process communication
Global StateManages application-wide settings and database connections
OS AbstractionsProvides cross-platform APIs
职责描述
窗口管理创建并协调应用窗口
系统集成管理系统托盘菜单和通知
IPC 路由处理所有进程间通信
全局状态管理应用级设置和数据库连接
操作系统抽象提供跨平台 API

Why Rust for the Core Process

为何选择 Rust 作为 Core 进程开发语言

Rust powers the Core process for its memory-safety guarantees. The ownership system prevents:
  • Null pointer dereferences
  • Buffer overflows
  • Data races
  • Use-after-free bugs
This is critical because the Core process has full system access.
+------------------------------------------+
|            CORE PROCESS                   |
|                                          |
|  Memory Safety via Rust Ownership:       |
|  - No null pointers                      |
|  - No buffer overflows                   |
|  - No data races                         |
|  - No use-after-free                     |
|                                          |
|  Full OS Access:                         |
|  - File system                           |
|  - Network                               |
|  - System APIs                           |
|  - Hardware interfaces                   |
+------------------------------------------+
Rust 为 Core 进程提供了内存安全保障,其所有权系统可防止:
  • 空指针解引用
  • 缓冲区溢出
  • 数据竞争
  • 悬垂引用
这一点至关重要,因为 Core 进程拥有完整的系统访问权限。
+------------------------------------------+
|            CORE PROCESS                   |
|                                          |
|  Memory Safety via Rust Ownership:       |
|  - No null pointers                      |
|  - No buffer overflows                   |
|  - No data races                         |
|  - No use-after-free                     |
|                                          |
|  Full OS Access:                         |
|  - File system                           |
|  - Network                               |
|  - System APIs                           |
|  - Hardware interfaces                   |
+------------------------------------------+

The WebView Process

WebView 进程

WebView processes render the user interface using the operating system's native WebView library.
WebView 进程使用操作系统的原生 WebView 库渲染用户界面。

Platform-Specific WebViews

平台专属 WebView

+------------------+------------------+------------------+
|     WINDOWS      |      MACOS       |      LINUX       |
+------------------+------------------+------------------+
|                  |                  |                  |
|  Microsoft Edge  |    WKWebView     |    webkitgtk     |
|    WebView2      |                  |                  |
|                  |                  |                  |
|  Chromium-based  |  Safari engine   |  WebKit engine   |
|                  |                  |                  |
+------------------+------------------+------------------+
         |                  |                  |
         +------------------+------------------+
                           |
                    Dynamic Linking
                    (Not bundled)
                           |
                    Smaller executables
+------------------+------------------+------------------+
|     WINDOWS      |      MACOS       |      LINUX       |
+------------------+------------------+------------------+
|                  |                  |                  |
|  Microsoft Edge  |    WKWebView     |    webkitgtk     |
|    WebView2      |                  |                  |
|                  |                  |                  |
|  Chromium-based  |  Safari engine   |  WebKit engine   |
|                  |                  |                  |
+------------------+------------------+------------------+
         |                  |                  |
         +------------------+------------------+
                           |
                    Dynamic Linking
                    (Not bundled)
                           |
                    Smaller executables

Key Characteristics

核心特性

  1. Dynamic Linking: WebView libraries are linked at runtime, not bundled
  2. Web Technologies: Execute HTML, CSS, and JavaScript
  3. Framework Support: Works with React, Vue, Svelte, Solid, etc.
  4. Isolation: Each WebView runs in its own process space
  1. 动态链接:WebView 库在运行时链接,而非打包进应用
  2. Web 技术栈:执行 HTML、CSS 和 JavaScript
  3. 框架支持:兼容 React、Vue、Svelte、Solid 等框架
  4. 隔离性:每个 WebView 运行在独立的进程空间中

Process Communication (IPC)

进程间通信(IPC)

All communication between processes flows through the Core process.
+----------------+                              +----------------+
|   WebView A    |                              |   WebView B    |
|                |                              |                |
|  invoke()  ----+---->+----------------+<------+---- invoke()   |
|                |     |  CORE PROCESS  |       |                |
|  <---- listen  |<----+                +------>|  listen ---->  |
|                |     |  - Validates   |       |                |
+----------------+     |  - Routes      |       +----------------+
                       |  - Filters     |
                       |  - Transforms  |
                       +----------------+
                              |
                              v
                       +----------------+
                       |   OS / System  |
                       |   Resources    |
                       +----------------+
进程间的所有通信均通过 Core 进程中转。
+----------------+                              +----------------+
|   WebView A    |                              |   WebView B    |
|                |                              |                |
|  invoke()  ----+---->+----------------+<------+---- invoke()   |
|                |     |  CORE PROCESS  |       |                |
|  <---- listen  |<----+                +------>|  listen ---->  |
|                |     |  - Validates   |       |                |
+----------------+     |  - Routes      |       +----------------+
                       |  - Filters     |
                       |  - Transforms  |
                       +----------------+
                              |
                              v
                       +----------------+
                       |   OS / System  |
                       |   Resources    |
                       +----------------+

IPC Flow

IPC 流程

  1. WebView calls
    invoke()
    with a command name and payload
  2. Core process receives the message
  3. Core process validates and processes the request
  4. Core process may interact with OS resources
  5. Core process sends response back to WebView
  1. WebView 调用
    invoke()
    并传入命令名称和负载
  2. Core 进程接收消息
  3. Core 进程验证并处理请求
  4. Core 进程可能与操作系统资源交互
  5. Core 进程将响应返回给 WebView

Example: Basic IPC

示例:基础 IPC

Frontend (JavaScript)
javascript
import { invoke } from '@tauri-apps/api/core';

// Call a Rust command
const result = await invoke('greet', { name: 'World' });
Backend (Rust)
rust
#[tauri::command]
fn greet(name: &str) -> String {
    format!("Hello, {}!", name)
}
前端(JavaScript)
javascript
import { invoke } from '@tauri-apps/api/core';

// Call a Rust command
const result = await invoke('greet', { name: 'World' });
后端(Rust)
rust
#[tauri::command]
fn greet(name: &str) -> String {
    format!("Hello, {}!", name)
}

Multiwindow Handling

多窗口处理

A single Core process manages multiple WebView processes.
                    +-------------------+
                    |   CORE PROCESS    |
                    |                   |
                    |  Shared State:    |
                    |  - User session   |
                    |  - App config     |
                    |  - DB connection  |
                    +-------------------+
                           /|\
                          / | \
                         /  |  \
                        /   |   \
                       /    |    \
                      v     v     v
               +------+ +------+ +------+
               |Main  | |Settings| |About|
               |Window| |Window | |Window|
               +------+ +------+ +------+
单个 Core 进程可管理多个 WebView 进程。
                    +-------------------+
                    |   CORE PROCESS    |
                    |                   |
                    |  Shared State:    |
                    |  - User session   |
                    |  - App config     |
                    |  - DB connection  |
                    +-------------------+
                           /|\
                          / | \
                         /  |  \
                        /   |   \
                       /    |    \
                      v     v     v
               +------+ +------+ +------+
               |Main  | |Settings| |About|
               |Window| |Window | |Window|
               +------+ +------+ +------+

Window Management Patterns

窗口管理模式

Creating Windows
rust
use tauri::Manager;

#[tauri::command]
fn open_settings(app: tauri::AppHandle) {
    tauri::WebviewWindowBuilder::new(
        &app,
        "settings",
        tauri::WebviewUrl::App("settings.html".into())
    )
    .title("Settings")
    .build()
    .unwrap();
}
Cross-Window Communication
rust
use tauri::Manager;

#[tauri::command]
fn broadcast_update(app: tauri::AppHandle, data: String) {
    // Emit to all windows
    app.emit("data-updated", data).unwrap();
}
Window-Specific Events
rust
use tauri::Manager;

#[tauri::command]
fn notify_window(app: tauri::AppHandle, window_label: String, data: String) {
    if let Some(window) = app.get_webview_window(&window_label) {
        window.emit("notification", data).unwrap();
    }
}
创建窗口
rust
use tauri::Manager;

#[tauri::command]
fn open_settings(app: tauri::AppHandle) {
    tauri::WebviewWindowBuilder::new(
        &app,
        "settings",
        tauri::WebviewUrl::App("settings.html".into())
    )
    .title("Settings")
    .build()
    .unwrap();
}
跨窗口通信
rust
use tauri::Manager;

#[tauri::command]
fn broadcast_update(app: tauri::AppHandle, data: String) {
    // Emit to all windows
    app.emit("data-updated", data).unwrap();
}
窗口专属事件
rust
use tauri::Manager;

#[tauri::command]
fn notify_window(app: tauri::AppHandle, window_label: String, data: String) {
    if let Some(window) = app.get_webview_window(&window_label) {
        window.emit("notification", data).unwrap();
    }
}

Process Isolation and Security

进程隔离与安全

The Principle of Least Privilege

最小权限原则

"If you have a gardener coming over to trim your hedge, you give them the key to your garden. You would not give them the keys to your house."
+------------------------------------------------------------------+
|                     SECURITY BOUNDARIES                           |
+------------------------------------------------------------------+
|                                                                   |
|  +---------------------------+   +---------------------------+    |
|  |      CORE PROCESS         |   |     WEBVIEW PROCESS       |    |
|  |      (Trusted Zone)       |   |    (Untrusted Zone)       |    |
|  +---------------------------+   +---------------------------+    |
|  |                           |   |                           |    |
|  |  - File system access     |   |  - Render UI only         |    |
|  |  - Database connections   |   |  - User input handling    |    |
|  |  - Network requests       |   |  - Display data           |    |
|  |  - Crypto operations      |   |  - Call allowed commands  |    |
|  |  - Secrets management     |   |                           |    |
|  |  - Business logic         |   |  NO DIRECT ACCESS TO:     |    |
|  |                           |   |  - File system            |    |
|  |                           |   |  - Network (direct)       |    |
|  |                           |   |  - System APIs            |    |
|  +---------------------------+   +---------------------------+    |
|                                                                   |
+------------------------------------------------------------------+
"如果请园丁来修剪篱笆,你只会给他们花园的钥匙,而不会把家里的钥匙也交给他们。"
+------------------------------------------------------------------+
|                     SECURITY BOUNDARIES                           |
+------------------------------------------------------------------+
|                                                                   |
|  +---------------------------+   +---------------------------+    |
|  |      CORE PROCESS         |   |     WEBVIEW PROCESS       |    |
|  |      (Trusted Zone)       |   |    (Untrusted Zone)       |    |
|  +---------------------------+   +---------------------------+    |
|  |                           |   |                           |    |
|  |  - File system access     |   |  - Render UI only         |    |
|  |  - Database connections   |   |  - User input handling    |    |
|  |  - Network requests       |   |  - Display data           |    |
|  |  - Crypto operations      |   |  - Call allowed commands  |    |
|  |  - Secrets management     |   |                           |    |
|  |  - Business logic         |   |  NO DIRECT ACCESS TO:     |    |
|  |                           |   |  - File system            |    |
|  |                           |   |  - Network (direct)       |    |
|  |                           |   |  - System APIs            |    |
|  +---------------------------+   +---------------------------+    |
|                                                                   |
+------------------------------------------------------------------+

Security Benefits of Process Isolation

进程隔离的安全优势

BenefitDescription
Crash ContainmentFailures in one process don't crash the entire app
State RecoveryInvalid processes can be restarted independently
Attack Surface ReductionCompromised WebView has limited capabilities
Resource ProtectionSensitive data stays in Core process
优势描述
崩溃隔离单个进程故障不会导致整个应用崩溃
状态恢复异常进程可独立重启
攻击面缩减被攻陷的 WebView 能力受限
资源保护敏感数据仅存于 Core 进程

Security Best Practices

安全最佳实践

In the Frontend (WebView)
  1. Sanitize all user input
  2. Never handle secrets
  3. Defer business logic to Core process
  4. Implement Content Security Policy (CSP)
In the Backend (Core Process)
  1. Validate all IPC inputs
  2. Use the capability system to restrict commands
  3. Apply principle of least privilege to each window
前端(WebView)
  1. 对所有用户输入进行 sanitize 处理
  2. 绝不处理敏感信息
  3. 将业务逻辑委托给 Core 进程
  4. 实现内容安全策略(CSP)
后端(Core 进程)
  1. 验证所有 IPC 输入
  2. 使用能力系统限制命令访问
  3. 为每个窗口应用最小权限原则

Capability-Based Security

基于能力的安全机制

Tauri uses a capability system to control what each window can access.
+------------------+     +------------------+     +------------------+
|   Main Window    |     | Settings Window  |     |  Viewer Window   |
+------------------+     +------------------+     +------------------+
| Capabilities:    |     | Capabilities:    |     | Capabilities:    |
| - read_file      |     | - read_config    |     | - read_file      |
| - write_file     |     | - write_config   |     | (read only)      |
| - network        |     |                  |     |                  |
| - notifications  |     |                  |     |                  |
+------------------+     +------------------+     +------------------+
Example: Capability Configuration
json
{
  "identifier": "main-capability",
  "description": "Capability for the main window",
  "windows": ["main"],
  "permissions": [
    "core:default",
    "fs:read-files",
    "fs:write-files",
    "http:default"
  ]
}
Tauri 使用能力系统控制每个窗口的访问权限。
+------------------+     +------------------+     +------------------+
|   Main Window    |     | Settings Window  |     |  Viewer Window   |
+------------------+     +------------------+     +------------------+
| Capabilities:    |     | Capabilities:    |     | Capabilities:    |
| - read_file      |     | - read_config    |     | - read_file      |
| - write_file     |     | - write_config   |     | (read only)      |
| - network        |     |                  |     |                  |
| - notifications  |     |                  |     |                  |
+------------------+     +------------------+     +------------------+
示例:能力配置
json
{
  "identifier": "main-capability",
  "description": "Capability for the main window",
  "windows": ["main"],
  "permissions": [
    "core:default",
    "fs:read-files",
    "fs:write-files",
    "http:default"
  ]
}

Process Lifecycle

进程生命周期

+------------------------------------------------------------------+
|                      APPLICATION LIFECYCLE                        |
+------------------------------------------------------------------+
|                                                                   |
|  1. App Launch                                                    |
|     +------------------+                                          |
|     | Core Process     |  <-- Starts first                        |
|     | Initializes      |                                          |
|     +------------------+                                          |
|              |                                                    |
|              v                                                    |
|  2. Window Creation                                               |
|     +------------------+                                          |
|     | WebView Process  |  <-- Core creates WebViews               |
|     | Spawned          |                                          |
|     +------------------+                                          |
|              |                                                    |
|              v                                                    |
|  3. Running                                                       |
|     +--------+    IPC    +----------+                             |
|     | Core   |<--------->| WebViews |                             |
|     +--------+           +----------+                             |
|              |                                                    |
|              v                                                    |
|  4. Shutdown                                                      |
|     +------------------+                                          |
|     | WebViews close   |  <-- WebViews terminate first            |
|     | Core cleans up   |  <-- Core process exits last             |
|     +------------------+                                          |
|                                                                   |
+------------------------------------------------------------------+
+------------------------------------------------------------------+
|                      APPLICATION LIFECYCLE                        |
+------------------------------------------------------------------+
|                                                                   |
|  1. App Launch                                                    |
|     +------------------+                                          |
|     | Core Process     |  <-- Starts first                        |
|     | Initializes      |                                          |
|     +------------------+                                          |
|              |                                                    |
|              v                                                    |
|  2. Window Creation                                               |
|     +------------------+                                          |
|     | WebView Process  |  <-- Core creates WebViews               |
|     | Spawned          |                                          |
|     +------------------+                                          |
|              |                                                    |
|              v                                                    |
|  3. Running                                                       |
|     +--------+    IPC    +----------+                             |
|     | Core   |<--------->| WebViews |                             |
|     +--------+           +----------+                             |
|              |                                                    |
|              v                                                    |
|  4. Shutdown                                                      |
|     +------------------+                                          |
|     | WebViews close   |  <-- WebViews terminate first            |
|     | Core cleans up   |  <-- Core process exits last             |
|     +------------------+                                          |
|                                                                   |
+------------------------------------------------------------------+

Summary

总结

AspectCore ProcessWebView Process
LanguageRustJavaScript/TypeScript
QuantityOne per appOne or more per app
OS AccessFullNone (via IPC only)
RoleBackend, orchestrationUI rendering
SecurityTrustedUntrusted
Crash ImpactApp terminatesWindow closes
The Tauri process model provides a secure foundation for building desktop applications by maintaining strict separation between the trusted Core process and the potentially vulnerable WebView processes. All sensitive operations should be implemented in the Core process, with the WebView serving only as a presentation layer.
维度Core 进程WebView 进程
开发语言RustJavaScript/TypeScript
进程数量每个应用一个每个应用一个或多个
系统访问权限完整权限无直接权限(仅通过 IPC)
角色后端、协调管控UI 渲染
安全级别可信不可信
崩溃影响整个应用终止仅对应窗口关闭
Tauri 进程模型通过严格区分可信的 Core 进程和潜在易受攻击的 WebView 进程,为桌面应用提供了安全基础。所有敏感操作都应在 Core 进程中实现,WebView 仅作为展示层。