Compare commits

...

7 Commits

Author SHA1 Message Date
deepak1556
45ba741580 chore: remove interface bound checks 2025-06-17 18:05:09 +09:00
Robo
ef040ffad6 chore: update spec/api-utility-process-spec.ts
Co-authored-by: David Sanders <dsanders11@ucsbalum.com>
2025-06-17 17:27:54 +09:00
deepak1556
95033edacd chore: update spec 2025-06-16 12:31:22 +09:00
Niklas Wenzel
dd4b11d89f docs: update breaking changes 2025-06-15 23:16:22 +02:00
deepak1556
2fdf78e4e6 fix: utilityProcess running user script after process.exit is called 2025-06-15 19:26:06 +09:00
Charles Kerr
6f915e85e2 refactor: have ShowSaveDialogSync() return a std::optional<base::FilePath> (#47428)
* refactor: have ShowSaveDialogSync() return a std::optional<base::FilePath>

* fixup! refactor: have ShowSaveDialogSync() return a std::optional<base::FilePath>
2025-06-12 12:25:40 +02:00
Anny Yang
0259abe920 docs: update remaining references to electron-quick-start (#47405)
* chore: udpate remaining references to electron-quick-start

* chore: Update docs/tutorial/tutorial-1-prerequisites.md

Co-authored-by: Niklas Wenzel <dev@nikwen.de>

* chore: Update docs/tutorial/tutorial-3-preload.md

Co-authored-by: Niklas Wenzel <dev@nikwen.de>

* chore: Update docs/tutorial/tutorial-2-first-app.md

Co-authored-by: Niklas Wenzel <dev@nikwen.de>

* chore: linebreak

* chore: swap minimal-repro for npx create-electron-app

* chore: add back code commands

* chore: add whitespace

* chore: remove reference to repo containing old quick start

---------

Co-authored-by: Niklas Wenzel <dev@nikwen.de>
2025-06-12 10:32:17 +02:00
15 changed files with 148 additions and 83 deletions

View File

@@ -27,6 +27,16 @@ process.on('unhandledRejection', () => {
})
```
### Behavior Changed: `process.exit()` kills utility process synchronously
Calling `process.exit()` in a utility process will now kill the utility process synchronously.
This brings the behavior of `process.exit()` in line with Node.js behavior.
Please refer to the
[Node.js docs](https://nodejs.org/docs/latest-v22.x/api/process.html#processexitcode) and
[PR #45690](https://github.com/electron/electron/pull/45690) to understand the potential
implications of that, e.g., when calling `console.log()` before `process.exit()`.
### Behavior Changed: WebUSB and WebSerial Blocklist Support
[WebUSB](https://developer.mozilla.org/en-US/docs/Web/API/WebUSB_API) and [Web Serial](https://developer.mozilla.org/en-US/docs/Web/API/Web_Serial_API) now support the [WebUSB Blocklist](https://wicg.github.io/webusb/#blocklist) and [Web Serial Blocklist](https://wicg.github.io/serial/#blocklist) used by Chromium and outlined in their respective specifications.

View File

@@ -1,6 +1,7 @@
# Debugging in VSCode
This guide goes over how to set up VSCode debugging for both your own Electron project as well as the native Electron codebase.
This guide goes over how to set up VSCode debugging for both your own Electron
project as well as the native Electron codebase.
## Debugging your Electron app
@@ -9,8 +10,8 @@ This guide goes over how to set up VSCode debugging for both your own Electron p
#### 1. Open an Electron project in VSCode.
```sh
$ git clone git@github.com:electron/electron-quick-start.git
$ code electron-quick-start
$ npx create-electron-app@latest my-app
$ code my-app
```
#### 2. Add a file `.vscode/launch.json` with the following configuration:
@@ -37,23 +38,27 @@ $ code electron-quick-start
#### 3. Debugging
Set some breakpoints in `main.js`, and start debugging in the [Debug View](https://code.visualstudio.com/docs/editor/debugging). You should be able to hit the breakpoints.
Here is a pre-configured project that you can download and directly debug in VSCode: https://github.com/octref/vscode-electron-debug/tree/master/electron-quick-start
Set some breakpoints in `main.js`, and start debugging in the
[Debug View](https://code.visualstudio.com/docs/editor/debugging). You should
be able to hit the breakpoints.
## Debugging the Electron codebase
If you want to build Electron from source and modify the native Electron codebase, this section will help you in testing your modifications.
If you want to build Electron from source and modify the native Electron codebase,
this section will help you in testing your modifications.
For those unsure where to acquire this code or how to build it, [Electron's Build Tools](https://github.com/electron/build-tools) automates and explains most of this process. If you wish to manually set up the environment, you can instead use these [build instructions](../development/build-instructions-gn.md).
For those unsure where to acquire this code or how to build it,
[Electron's Build Tools](https://github.com/electron/build-tools) automates and
explains most of this process. If you wish to manually set up the environment,
you can instead use these [build instructions](../development/build-instructions-gn.md).
### Windows (C++)
#### 1. Open an Electron project in VSCode.
```sh
$ git clone git@github.com:electron/electron-quick-start.git
$ code electron-quick-start
$ npx create-electron-app@latest my-app
$ code my-app
```
#### 2. Add a file `.vscode/launch.json` with the following configuration:
@@ -86,14 +91,22 @@ $ code electron-quick-start
**Configuration Notes**
* `cppvsdbg` requires the [built-in C/C++ extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools) be enabled.
* `cppvsdbg` requires the
[built-in C/C++ extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools)
be enabled.
* `${workspaceFolder}` is the full path to Chromium's `src` directory.
* `your-executable-location` will be one of the following depending on a few items:
* `Testing`: If you are using the default settings of [Electron's Build-Tools](https://github.com/electron/build-tools) or the default instructions when [building from source](../development/build-instructions-gn.md#building).
* `Testing`: If you are using the default settings of
[Electron's Build-Tools](https://github.com/electron/build-tools) or the default
instructions when [building from source](../development/build-instructions-gn.md#building).
* `Release`: If you built a Release build rather than a Testing build.
* `your-directory-name`: If you modified this during your build process from the default, this will be whatever you specified.
* The `args` array string `"your-electron-project-path"` should be the absolute path to either the directory or `main.js` file of the Electron project you are using for testing. In this example, it should be your path to `electron-quick-start`.
* `your-directory-name`: If you modified this during your build process from
the default, this will be whatever you specified.
* The `args` array string `"your-electron-project-path"` should be the absolute
path to either the directory or `main.js` file of the Electron project you are
using for testing. In this example, it should be your path to `my-app`.
#### 3. Debugging
Set some breakpoints in the .cc files of your choosing in the native Electron C++ code, and start debugging in the [Debug View](https://code.visualstudio.com/docs/editor/debugging).
Set some breakpoints in the .cc files of your choosing in the native Electron C++
code, and start debugging in the [Debug View](https://code.visualstudio.com/docs/editor/debugging).

View File

@@ -5,8 +5,8 @@ for any Snapcraft environment, including the Ubuntu Software Center.
## Background and Requirements
Together with the broader Linux community, Canonical aims to fix many of the
common software installation problems with the [`snapcraft`](https://snapcraft.io/)
Together with the broader Linux community, Canonical aims to address common
software installation issues through the [`snapcraft`](https://snapcraft.io/)
project. Snaps are containerized software packages that include required
dependencies, auto-update, and work on all major Linux distributions without
system modification.
@@ -83,7 +83,14 @@ snap(options)
### Step 1: Create Sample Snapcraft Project
Create your project directory and add the following to `snap/snapcraft.yaml`:
```sh
$ npx create-electron-app@latest my-app
```
### Step 2: Create Sample Snapcraft Project
Create a `snap` directory in your project root and add the following to
`snap/snapcraft.yaml`:
```yaml
name: electron-packager-hello-world
@@ -97,7 +104,7 @@ grade: stable
apps:
electron-packager-hello-world:
command: electron-quick-start/electron-quick-start --no-sandbox
command: my-app/my-app --no-sandbox
extensions: [gnome]
plugs:
- browser-support
@@ -109,13 +116,13 @@ apps:
TMPDIR: $XDG_RUNTIME_DIR
parts:
electron-quick-start:
my-app:
plugin: nil
source: https://github.com/electron/electron-quick-start.git
source: .
override-build: |
npm install electron @electron/packager
npx electron-packager . --overwrite --platform=linux --output=release-build --prune=true
cp -rv ./electron-quick-start-linux-* $SNAPCRAFT_PART_INSTALL/electron-quick-start
cp -rv ./my-app-linux-* $SNAPCRAFT_PART_INSTALL/my-app
build-snaps:
- node/14/stable
build-packages:
@@ -125,12 +132,10 @@ parts:
- libnspr4
```
If you want to apply this example to an existing project:
If you want to apply this example to an existing project, replace all instances
of `my-app` with your project's name.
- Replace `source: https://github.com/electron/electron-quick-start.git` with `source: .`.
- Replace all instances of `electron-quick-start` with your project's name.
### Step 2: Build the snap
### Step 3: Build the snap
```sh
$ snapcraft
@@ -139,13 +144,13 @@ $ snapcraft
Snapped electron-packager-hello-world_0.1_amd64.snap
```
### Step 3: Install the snap
### Step 4: Install the snap
```sh
sudo snap install electron-packager-hello-world_0.1_amd64.snap --dangerous
```
### Step 4: Run the snap
### Step 5: Run the snap
```sh
electron-packager-hello-world

View File

@@ -1,6 +1,6 @@
---
title: 'Prerequisites'
description: 'This guide will step you through the process of creating a barebones Hello World app in Electron, similar to electron/electron-quick-start.'
description: 'This guide will step you through the process of creating a barebones Hello World app in Electron.'
slug: tutorial-prerequisites
hide_title: false
---

View File

@@ -1,6 +1,6 @@
---
title: 'Building your First App'
description: 'This guide will step you through the process of creating a barebones Hello World app in Electron, similar to electron/electron-quick-start.'
description: 'This guide will step you through the process of creating a barebones Hello World app in Electron.'
slug: tutorial-first-app
hide_title: false
---

View File

@@ -1,6 +1,6 @@
---
title: 'Using Preload Scripts'
description: 'This guide will step you through the process of creating a barebones Hello World app in Electron, similar to electron/electron-quick-start.'
description: 'This guide will step you through the process of creating a barebones Hello World app in Electron.'
slug: tutorial-preload
hide_title: false
---

View File

@@ -71,9 +71,8 @@ v8::Local<v8::Promise> ShowOpenDialog(
void ShowSaveDialogSync(const file_dialog::DialogSettings& settings,
gin::Arguments* args) {
base::FilePath path;
if (file_dialog::ShowSaveDialogSync(settings, &path))
args->Return(path);
if (const auto path = file_dialog::ShowSaveDialogSync(settings))
args->Return(*path);
}
v8::Local<v8::Promise> ShowSaveDialog(

View File

@@ -18,6 +18,7 @@
#include "base/containers/fixed_flat_map.h"
#include "base/containers/flat_set.h"
#include "base/containers/id_map.h"
#include "base/containers/map_util.h"
#include "base/files/file_util.h"
#include "base/json/json_reader.h"
#include "base/no_destructor.h"
@@ -4008,30 +4009,35 @@ void WebContents::DevToolsSaveToFile(const std::string& url,
const std::string& content,
bool save_as,
bool is_base64) {
base::FilePath path;
auto it = saved_files_.find(url);
if (it != saved_files_.end() && !save_as) {
path = it->second;
} else {
const base::FilePath* path = nullptr;
if (!save_as)
base::FindOrNull(saved_files_, url);
if (path == nullptr) {
file_dialog::DialogSettings settings;
settings.parent_window = owner_window();
settings.force_detached = offscreen_;
settings.title = url;
settings.default_path = base::FilePath::FromUTF8Unsafe(url);
if (!file_dialog::ShowSaveDialogSync(settings, &path)) {
inspectable_web_contents_->CallClientFunction(
"DevToolsAPI", "canceledSaveURL", base::Value(url));
return;
if (auto new_path = file_dialog::ShowSaveDialogSync(settings)) {
auto [iter, _] = saved_files_.try_emplace(url, std::move(*new_path));
path = &iter->second;
}
}
saved_files_[url] = path;
if (path == nullptr) {
inspectable_web_contents_->CallClientFunction(
"DevToolsAPI", "canceledSaveURL", base::Value{url});
return;
}
// Notify DevTools.
inspectable_web_contents_->CallClientFunction(
"DevToolsAPI", "savedURL", base::Value(url),
base::Value(path.AsUTF8Unsafe()));
"DevToolsAPI", "savedURL", base::Value{url},
base::Value{path->AsUTF8Unsafe()});
file_task_runner_->PostTask(
FROM_HERE, base::BindOnce(&WriteToFile, path, content, is_base64));
FROM_HERE, base::BindOnce(&WriteToFile, *path, content, is_base64));
}
void WebContents::DevToolsAppendToFile(const std::string& url,

View File

@@ -5,6 +5,7 @@
#ifndef ELECTRON_SHELL_BROWSER_UI_FILE_DIALOG_H_
#define ELECTRON_SHELL_BROWSER_UI_FILE_DIALOG_H_
#include <optional>
#include <string>
#include <utility>
#include <vector>
@@ -72,7 +73,8 @@ bool ShowOpenDialogSync(const DialogSettings& settings,
void ShowOpenDialog(const DialogSettings& settings,
gin_helper::Promise<gin_helper::Dictionary> promise);
bool ShowSaveDialogSync(const DialogSettings& settings, base::FilePath* path);
std::optional<base::FilePath> ShowSaveDialogSync(
const DialogSettings& settings);
void ShowSaveDialog(const DialogSettings& settings,
gin_helper::Promise<gin_helper::Dictionary> promise);

View File

@@ -233,20 +233,25 @@ void ShowOpenDialog(const DialogSettings& settings,
dialog->RunOpenDialog(std::move(promise), settings);
}
bool ShowSaveDialogSync(const DialogSettings& settings, base::FilePath* path) {
base::RunLoop run_loop(base::RunLoop::Type::kNestableTasksAllowed);
auto cb = base::BindOnce(
[](base::RepeatingClosure cb, base::FilePath* file_path,
gin_helper::Dictionary result) {
result.Get("filePath", file_path);
std::move(cb).Run();
},
run_loop.QuitClosure(), path);
std::optional<base::FilePath> ShowSaveDialogSync(
const DialogSettings& settings) {
std::optional<base::FilePath> path;
FileChooserDialog* dialog = new FileChooserDialog();
dialog->RunSaveDialog(std::move(cb), settings);
base::RunLoop run_loop{base::RunLoop::Type::kNestableTasksAllowed};
auto on_chooser_dialog_done = base::BindOnce(
[](base::RepeatingClosure run_loop_closure,
std::optional<base::FilePath>* path, gin_helper::Dictionary result) {
if (base::FilePath val; result.Get("filePath", &val))
*path = std::move(val);
std::move(run_loop_closure).Run();
},
run_loop.QuitClosure(), &path);
auto* const dialog = new FileChooserDialog{};
dialog->RunSaveDialog(std::move(on_chooser_dialog_done), settings);
run_loop.Run();
return !path->empty();
return path;
}
void ShowSaveDialog(const DialogSettings& settings,

View File

@@ -436,19 +436,18 @@ void ShowOpenDialog(const DialogSettings& settings,
}
}
bool ShowSaveDialogSync(const DialogSettings& settings, base::FilePath* path) {
DCHECK(path);
std::optional<base::FilePath> ShowSaveDialogSync(
const DialogSettings& settings) {
NSSavePanel* dialog = [NSSavePanel savePanel];
SetupDialog(dialog, settings);
SetupSaveDialogForProperties(dialog, settings.properties);
int chosen = RunModalDialog(dialog, settings);
const int chosen = RunModalDialog(dialog, settings);
if (chosen == NSModalResponseCancel || ![[dialog URL] isFileURL])
return false;
return {};
*path = base::FilePath(base::SysNSStringToUTF8([[dialog URL] path]));
return true;
return base::FilePath{base::SysNSStringToUTF8([[dialog URL] path])};
}
void SaveDialogCompletion(int chosen,

View File

@@ -219,11 +219,12 @@ void ShowOpenDialog(const DialogSettings& settings,
base::BindOnce(done, std::move(promise)));
}
bool ShowSaveDialogSync(const DialogSettings& settings, base::FilePath* path) {
std::optional<base::FilePath> ShowSaveDialogSync(
const DialogSettings& settings) {
ATL::CComPtr<IFileSaveDialog> file_save_dialog;
HRESULT hr = file_save_dialog.CoCreateInstance(CLSID_FileSaveDialog);
if (FAILED(hr))
return false;
return {};
DWORD options = FOS_FORCEFILESYSTEM | FOS_PATHMUSTEXIST | FOS_OVERWRITEPROMPT;
if (settings.properties & SAVE_DIALOG_SHOW_HIDDEN_FILES)
@@ -236,32 +237,31 @@ bool ShowSaveDialogSync(const DialogSettings& settings, base::FilePath* path) {
hr = ShowFileDialog(file_save_dialog, settings);
if (FAILED(hr))
return false;
return {};
CComPtr<IShellItem> pItem;
hr = file_save_dialog->GetResult(&pItem);
if (FAILED(hr))
return false;
return {};
PWSTR result_path = nullptr;
hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &result_path);
if (!SUCCEEDED(hr))
return false;
return {};
*path = base::FilePath(result_path);
auto path = base::FilePath{result_path};
CoTaskMemFree(result_path);
return true;
return path;
}
void ShowSaveDialog(const DialogSettings& settings,
gin_helper::Promise<gin_helper::Dictionary> promise) {
auto done = [](gin_helper::Promise<gin_helper::Dictionary> promise,
bool success, base::FilePath result) {
std::optional<base::FilePath> result) {
v8::HandleScope handle_scope(promise.isolate());
auto dict = gin::Dictionary::CreateEmpty(promise.isolate());
dict.Set("canceled", !success);
dict.Set("filePath", result);
dict.Set("canceled", !result.has_value());
dict.Set("filePath", result.value_or(base::FilePath{}));
promise.Resolve(dict);
};
dialog_thread::Run(base::BindOnce(ShowSaveDialogSync, settings),

View File

@@ -9,6 +9,7 @@
#include "base/command_line.h"
#include "base/no_destructor.h"
#include "base/process/process.h"
#include "base/strings/utf_string_conversions.h"
#include "electron/mas.h"
#include "services/network/public/cpp/wrapper_shared_url_loader_factory.h"
@@ -99,8 +100,6 @@ NodeService::~NodeService() {
ParentPort::GetInstance()->Close();
js_env_->DestroyMicrotasksRunner();
node::Stop(node_env_.get(), node::StopFlags::kDoNotTerminateIsolate);
}
if (g_client_remote.is_bound()) {
g_client_remote.reset();
}
}
@@ -147,12 +146,12 @@ void NodeService::Initialize(
node::SetProcessExitHandler(
node_env_.get(), [this](node::Environment* env, int exit_code) {
// Destroy node platform.
env->set_trace_sync_io(false);
node_env_stopped_ = true;
ParentPort::GetInstance()->Close();
js_env_->DestroyMicrotasksRunner();
node::Stop(env, node::StopFlags::kDoNotTerminateIsolate);
node_env_stopped_ = true;
g_client_remote.reset();
receiver_.ResetWithReason(exit_code, "process_exit_termination");
node::DefaultProcessExitHandler(env, exit_code);
});
node_env_->set_trace_sync_io(node_env_->options()->trace_sync_io);

View File

@@ -129,6 +129,26 @@ describe('utilityProcess module', () => {
expect(code).to.equal(exitCode);
});
it('does not run JS after process.exit is called', async () => {
const file = path.join(os.tmpdir(), `no-js-after-exit-log-${Math.random()}`);
const child = utilityProcess.fork(path.join(fixturesPath, 'no-js-after-exit.js'), [`--testPath=${file}`]);
const [code] = await once(child, 'exit');
expect(code).to.equal(1);
let handle = null;
const lines = [];
try {
handle = await fs.open(file);
for await (const line of handle.readLines()) {
lines.push(line);
}
} finally {
await handle?.close();
await fs.rm(file, { force: true });
}
expect(lines.length).to.equal(1);
expect(lines[0]).to.equal('before exit');
});
// 32-bit system will not have V8 Sandbox enabled.
// WoA testing does not have VS toolchain configured to build native addons.
ifit(process.arch !== 'ia32' && process.arch !== 'arm' && !isWindowsOnArm)('emits \'error\' when fatal error is triggered from V8', async () => {

View File

@@ -0,0 +1,7 @@
const { writeFileSync } = require('node:fs');
const arg = process.argv[2];
const file = arg.split('=')[1];
writeFileSync(file, 'before exit');
process.exit(1);
writeFileSync(file, 'after exit');