Files
electron/shell/browser/api/electron_api_app_shim_host.h
T
Samuel AttardandClaude 0edcdc47c0 feat: add app shims on macOS (#54932)
* feat: add app shims on macOS

An app shim is a small app, in a bundle of its own, that shows windows on
behalf of a host Electron app. The host still creates each window, loads
its page and handles its events, but the NSWindow belongs to the shim's
process, so the Dock, the app switcher, Mission Control and Stage Manager
attribute the window to the shim and not to the host.

- `app.startAppShimServer()` / `app.stopAppShimServer()` and the
  `app-shim-connected` event, which hands over an `AppShimHost`.
- A `BaseWindow` / `BrowserWindow` created with the `appShim` option has
  its native window made in that shim's process, through Chromium's
  remote_cocoa. Such a window is a new NativeWindow implementation that
  only goes through views::Widget; what remote_cocoa has no call for is
  a no-op (see docs/api/app-shim-host.md).
- The shim's executable is `Helpers/app_shim_loader` from the framework
  bundle. It links nothing from Electron: it loads the host's framework
  and calls the new `ElectronAppShimMain` export, which runs an
  `app-shim` process that connects back to the host over a Mach
  bootstrap server.
- Electron Framework gains an rpath to the folder it is in, so that it
  finds its sibling frameworks when an executable in another bundle
  loads it.

Not available in Mac App Store builds.

Co-Authored-By: Claude <noreply@anthropic.com>

* feat: app shim menus, dialogs, window chrome and review fixes

Follow-up to the first app shim commit.

Fixes from review:
- A window torn down from one of its own listeners (shim.quit() or
  win.destroy() in `show`) no longer frees the native host that is
  notifying.
- The native close button no longer leaves the shim waiting on an answer
  that was dropped with the window.
- Keys the page does not handle go back to the shim, whose menu they are
  shortcuts of, and not to the host's menu.
- stopAppShimServer() drops shims that have connected and not said hello.
- Quitting a shim asks the host without blocking and without cancelling a
  logout; quit-requested can now be answered with "no".
- Size limits and useContentSize are in content sizes; restore() and
  unmaximize() each do one thing; webContents.isFocused() works.

New:
- shim.setMenu(): a menu bar of the shim's own, built in the shim from a
  description of the Menu, with clicks sent back.
- Message boxes and open and save dialogs are sheets of the window in the
  shim's process.
- titleBarStyle, traffic light position and visibility, vibrancy, tabs and
  Quick Look, applied in the shim to the NSWindow that remote_cocoa made.
- A Chromium patch for what remote_cocoa has no call for: movable,
  closable, minimizable and fullscreenable, clearing an aspect ratio, and
  yielding activation to the hosting process by pid.
- A shim that loaded another build of Electron than the running host
  tells the host and quits: app-shim-version-mismatch.

The API is marked experimental in the docs.

Co-Authored-By: Claude <noreply@anthropic.com>

* fix: address review of app shims

- A window in an app shim can't be given a parent, at construction or
  with setParentWindow(), as the docs say.
- shim.setMenu() is now shim.setApplicationMenu().
- A message box that is showing under an id makes way for a new one with
  that id, whichever process shows it.
- Mac App Store builds no longer export the shim's entry point; they
  already had no loader.
- setProgressBar() and setHasShadow() are listed among what does nothing
  on a window in a shim.
- The spec no longer asserts inside an event listener.

Co-Authored-By: Claude <noreply@anthropic.com>

* fix: clang-tidy findings in app shim code

* docs: reword the app shim docs for clarity

* chore: update patches

Re-exported after rebasing onto Chromium 158.0.8105.0; only line offsets
changed.

Co-Authored-By: Claude <noreply@anthropic.com>

* test: use vitest matchers in the app shim spec

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-16 14:03:19 -07:00

118 lines
3.7 KiB
C++

// Copyright (c) 2026 Anthropic, PBC.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ELECTRON_SHELL_BROWSER_API_ELECTRON_API_APP_SHIM_HOST_H_
#define ELECTRON_SHELL_BROWSER_API_ELECTRON_API_APP_SHIM_HOST_H_
#include <mach/message.h>
#include <memory>
#include <string>
#include <vector>
#include "base/files/file_path.h"
#include "electron/shell/common/app_shim.mojom.h"
#include "gin/wrappable.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "shell/browser/event_emitter_mixin.h"
#include "shell/browser/native_peer.h"
class GURL;
namespace gin {
class Arguments;
} // namespace gin
namespace gin_helper {
class ErrorThrower;
} // namespace gin_helper
namespace remote_cocoa {
class ApplicationHost;
} // namespace remote_cocoa
namespace electron {
class AppShimHostBootstrap;
namespace api {
class Menu;
// A connected app shim: another app, with a Dock tile of its own, that shows
// windows on this app's behalf. Windows created with it as their `appShim`
// option are NSWindows of the shim's process.
class AppShimHost final : public gin::Wrappable<AppShimHost>,
public gin_helper::EventEmitterMixin<AppShimHost> {
public:
// Takes over a shim's connection once its hello has been answered.
static AppShimHost* Create(
v8::Isolate* isolate,
std::unique_ptr<AppShimHostBootstrap> bootstrap,
mojo::PendingReceiver<mojom::AppShimHost> host_receiver,
mojo::PendingRemote<mojom::AppShim> app_shim);
// Public for cppgc::MakeGarbageCollected.
AppShimHost(std::unique_ptr<AppShimHostBootstrap> bootstrap,
mojo::PendingReceiver<mojom::AppShimHost> host_receiver,
mojo::PendingRemote<mojom::AppShim> app_shim);
~AppShimHost() override;
AppShimHost(const AppShimHost&) = delete;
AppShimHost& operator=(const AppShimHost&) = delete;
// The bundle that the process an audit token names runs from; empty when
// the process is gone.
static base::FilePath BundlePathForAuditToken(const audit_token_t& token);
// What creates this shim's NSWindows; null once the shim has disconnected.
remote_cocoa::ApplicationHost* GetRemoteCocoaApplicationHost() const;
// The shim's process; null once the shim has disconnected.
mojom::AppShim* GetAppShim() const;
// A window was made in the shim's process.
void DidCreateWindow();
// gin::Wrappable
static gin::WrapperInfo kWrapperInfo;
gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
v8::Isolate* isolate) override;
const gin::WrapperInfo* wrapper_info() const override;
const char* GetHumanReadableName() const override;
const char* GetClassName() const { return "AppShimHost"; }
private:
class Connection;
// From the connection.
void OnReopen(bool has_visible_windows);
void OnFilesOpened(const std::vector<base::FilePath>& files);
void OnUrlsOpened(const std::vector<GURL>& urls);
// False when a listener prevented the default, which is to quit the shim.
bool OnQuitRequested();
void OnDisconnected();
// JS API.
int GetPid() const;
base::FilePath GetBundlePath() const;
std::string GetBundleId() const;
bool IsConnected() const;
bool SatisfiesCodeRequirement(gin_helper::ErrorThrower thrower,
const std::string& requirement);
void SetBadge(const std::string& label);
void Bounce(gin::Arguments* args);
void CancelBounce();
void Quit();
void SetApplicationMenu(gin::Arguments* args);
std::unique_ptr<Connection, NativePeerBase::Deleter> connection_;
};
} // namespace api
} // namespace electron
#endif // ELECTRON_SHELL_BROWSER_API_ELECTRON_API_APP_SHIM_HOST_H_