Skip to content

Commit bb96c08

Browse files
committed
Add IPC overview
1 parent 099c6a9 commit bb96c08

File tree

1 file changed

+89
-4
lines changed

1 file changed

+89
-4
lines changed

docs/Deep Dive/Architecture/WebKit2.md

+89-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ WebKit’s Multi-Process Architecture
77
In order to safeguard the rest of the system and allow the application to remain responsive
88
even if the user had loaded web page that infinite loops or otherwise hangs,
99
the modern incarnation of WebKit uses multi-process architecture.
10-
Web pages are loaded in its own *WebContent* process.
10+
Web pages are loaded in its own _WebContent_ process.
1111
Multiple WebContent processes can share a browsing session, which lives in a shared network process.
1212
In addition to handling all network accesses,
1313
this process is also responsible for managing the disk cache and Web APIs that allow websites
@@ -17,10 +17,95 @@ and [IndexedDB API](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_A
1717
Because a WebContent process can Just-in-Time compile arbitrary JavaScript code loaded from the internet,
1818
meaning that it can write to memory that gets executed, this process is tightly sandboxed.
1919
It does not have access to any file system unless the user grants an access,
20-
and it does not have direct access to the underlying operating system’s [clipboard](https://en.wikipedia.org/wiki/Clipboard_(computing)),
20+
and it does not have direct access to the underlying operating system’s [clipboard](<https://en.wikipedia.org/wiki/Clipboard_(computing)>),
2121
microphone, or video camera even though there are Web APIs that grant access to those features.
2222
Instead, UI process brokers such requests.
2323

24-
FIXME: How is IPC setup
24+
## How is IPC Setup
2525

26-
FIXME: How to add / modify an IPC message
26+
In WebKit, Inter-Process Communication (IPC) enables communication between different processes. The IPC setup in WebKit involves several key components:
27+
28+
### Message Definitions
29+
30+
Messages are defined in `*.messages.in` files (e.g., `WebPage.messages.in`). These files specify the structure and types of messages exchanged between processes. Each `*.messages.in` file must map to a header file with the same name (e.g., `WebPage.messages.in` maps to `WebPage.h`). These `*.messages.in` files are processed at build time to generate source code for receiving IPC messages. They support both synchronous and asynchronous messages.
31+
32+
### Message Handlers
33+
34+
Each process has message handlers that process incoming messages and perform the necessary actions. The message definitions in the `*.messages.in` file need to have corresponding implementations in the related class (e.g., `WebPage` implements handlers for the corresponding messages defined in `WebPage.messages.in`). The processing of the `*.messages.in` files during build time generates source code that maps a message definition to its corresponding handler implementation, which will automatically call it for you when that message is received. The message handlers must adhere to the correct function signatures specified in the `*.messages.in` files, otherwise you will get a build error.
35+
36+
### IPC::MessageSender and IPC::MessageReceiver
37+
38+
Classes that send or receive IPC messages typically inherit from `IPC::MessageSender` and `IPC::MessageReceiver`. These base classes automatically handle the serialization and deserialization of messages, as well as the setup of message dispatching.
39+
40+
For a deeper dive, you can found WebKit's IPC code in the `Source/WebKit/Platform/IPC` directory. This directory contains the classes and utilities needed to manage IPC, including message sending and receiving, connection management, and the serialization and deserialization of messages.
41+
42+
## How to Add or Modify an IPC Message
43+
44+
Adding or modifying an IPC message in WebKit involves several steps to ensure proper communication between classes in different processes. Here is a step-by-step guide:
45+
46+
### 1. Define the Message
47+
48+
Locate the appropriate `*.messages.in` file (e.g., `WebPage.messages.in`). If a new message file is needed, it should be placed in the same directory as the class you are implementing it for and it should also be added to the directory's `Sources.txt` file, as mentioned [here](../Build/AddingNewFile.html).
49+
Now, define the new message in this file. For example, let's say we want to send a message called `LoadURL` from the UI process' `WebPageProxy` class to the WebContent process' `WebPage` class. In `WebPage.messages.in`, you would add an entry like:
50+
51+
```
52+
LoadURL(String url)
53+
```
54+
55+
### 2. Implement the Message Handler
56+
57+
In the class that will receive the message (e.g., `WebPage`), implement the handler method. This method will be called when the message is received. For example:
58+
59+
```
60+
void WebPage::loadURL(const String& url) {
61+
// Implementation code to load the URL
62+
}
63+
```
64+
65+
### 3. Send the Message
66+
67+
To send the message from another process, use the `send` method of the `IPC::MessageSender` class. For example, to send the `LoadURL` message from the UI process' `WebPageProxy` class to the WebContent's `WebPage` class:
68+
69+
```
70+
send(Messages::WebPage::LoadURL(url));
71+
```
72+
73+
Note, `WebPageProxy` inherits from `IPC::MessageSender`, which is what makes this possible.
74+
75+
## Replying to Messages
76+
77+
In addition to sending messages, WebKit's IPC mechanism supports replying to messages, which can be done synchronously or asynchronously.
78+
79+
### 1. Define the Message Reply
80+
81+
In the definition of the message in the `*.messages.in` file, specify that it expects a reply. For example, in `WebPage.messages.in`:
82+
83+
```
84+
GetTitle() -> (String title)
85+
```
86+
87+
By default, the reply is handled asynchronously. In order to make it synchronous, add `Synchronous` after the message definition, for example:
88+
89+
```
90+
GetTitle() -> (String title) Synchronous
91+
```
92+
93+
### 2. Implement the Message Handler with Reply
94+
95+
Implement the handler method in the receiving class that will process the message and send a reply. For example, in the WebContent process' `WebPage.h`:
96+
97+
```
98+
void WebPage::getTitle(CompletionHandler<void(String)>&& completionHandler) {
99+
completionHandler(m_title);
100+
}
101+
```
102+
103+
### 3. Send the Asynchronous Message and Handle Reply
104+
105+
To send the asynchronous message and handle the reply, use the `sendWithAsyncReply` method of the `IPC::MessageSender` class. For example, in the UI process' `WebPageProxy.cpp`:
106+
107+
```
108+
sendWithAsyncReply(Messages::WebPage::GetTitle(), [this, weakThis = WeakPtr { *this }](String title) {
109+
// do something with title...
110+
});
111+
```

0 commit comments

Comments
 (0)