Files
electron/shell/common/extensions/electron_extensions_client.cc
T
Sam MaddockandClaude bc658b2924 fix: extension code drift (#55155)
* fix: report tab ids to extension webRequest and declarativeNetRequest

ElectronExtensionsBrowserClient did not override
GetTabAndWindowIdForWebContents, so the default of -1 reached every
caller: chrome.webRequest reported details.tabId as -1 and
declarativeNetRequest rules with a tabIds condition never matched.
Return the WebContents id, the same id chrome.tabs uses.

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

* fix: keep extension service workers registered when clearing storage

Chrome's embedder refuses to delete or web-update the registration of
an extension's background service worker and only lets the manifest's
worker script register at the extension's root scope. Electron used
the content defaults, so session.clearStorageData() deleted the
registration and the extension stopped receiving events until it was
reloaded.

Override MayDeleteServiceWorkerRegistration,
ShouldTryToUpdateServiceWorkerRegistration and AllowServiceWorker to
match.

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

* fix: crash in chrome.cookies.getAllCookieStores

The cookies API now lives in //extensions, so extensions with the
"cookies" permission can call it in Electron. Core code dereferences
ExtensionsAPIClient::GetCookiesApiDelegate(), which Electron did not
provide, crashing the browser process, and the CookiesAPI factory that
dispatches cookies.onChanged is only built by embedders.

Provide a delegate that lists the session as its own cookie store and
build the factory.

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

* fix: restore the management permission feature

The "management" entry was dropped from Electron's permission features
when the file was resynced with Chrome's in #44653. The management API
feature depends on it, so a manifest requesting the permission lost it
and chrome.management.getAll, get, onEnabled and onDisabled were
unavailable.

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

* fix: localize messages in extension stylesheets

Chromium substitutes __MSG_*__ placeholders in extension CSS with a
renderer URL loader throttle that only Chrome's throttle provider
installs. Electron had no provider, so placeholders such as
__MSG_@@extension_id__ were left as literal text. Install the throttle
from RendererClientBase.

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

* fix: let WebSocket activity keep extension service workers alive

Chrome forwards ShouldNotifyServiceWorkerOnWebSocketActivity to the
extensions dispatcher so traffic on a WebSocket resets an extension
service worker's idle timer. Electron did not, so a worker holding an
open socket was still stopped after 30 seconds.

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

* fix: keep sandboxed extension frames out of the extension process map

Chrome isolates only manifest-sandboxed extension pages (and sandboxed
about:srcdoc and data: documents) in a separate process and leaves that
process out of the extension ProcessMap, so it cannot be granted
extension API access. Electron isolated every sandboxed extension frame
and recorded each of those processes as a privileged extension process.

Override ShouldAllowCrossProcessSandboxedFrameForPrecursor and skip
sandboxed principals in SiteInstanceGotProcessAndSite to match.

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

* fix: support navigator.clipboard in extension service workers

Chromium exposes navigator.clipboard to extension service workers, but
every piece that enables it lives in Chrome's embedder. Register
chrome-extension as an extension scheme with Blink, pass
--extension-process to extension renderers and turn the runtime feature
on there, and answer IsClipboardAllowedForServiceWorker and
AllowWorkerWriteToClipboard in the browser. Writes still require the
clipboardWrite permission.

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

* fix: run the extension service worker script on disk after a restart

A service worker registration outlives the app, and Chromium trusts a
stored registration as current while the extension's version is
unchanged. Electron loads unpacked extensions on every launch, so an
extension whose files changed without a version bump kept running the
worker script from the launch that first registered it.

Unregister a worker left by a previous launch before adding the
extension so it is registered again from the files being loaded.

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

* fix: persist extension alarms and dynamic content scripts

ElectronExtensionSystem returned null for the state, rules and dynamic
user scripts stores, so chrome.alarms and content scripts registered
with persistAcrossSessions were forgotten when the app quit. Create the
three StateStores as Chrome's extension system does.

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

* fix: report webRequest proxying to the service worker loader

content asks HasWebRequestAPIProxy before starting a service worker
AutoPreload request, because a preload that is raced and dropped is
reported to webRequest listeners as a failed request. Answer for both
Electron's webRequest module and extensions that proxy requests.

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

* fix: use the app name in extension warnings

ElectronExtensionsClient::GetProductName still returned "app_shell",
the name of the Chromium embedder it was copied from, which extension
warning messages interpolate.

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

* chore: remove stale extension feature entries

These keys name permissions, manifest keys and API functions that
Chromium has removed or that Electron's schemas do not define, so they
matched nothing.

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

* docs: update extension support notes

The background key is supported for Manifest V3 service workers too,
and chrome.management needs the management permission.

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

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-23 13:41:53 -04:00

141 lines
4.4 KiB
C++

// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "shell/common/extensions/electron_extensions_client.h"
#include <memory>
#include <string>
#include "base/no_destructor.h"
#include "base/notimplemented.h"
#include "components/version_info/version_info.h"
#include "extensions/common/core_extensions_api_provider.h"
#include "extensions/common/extension_urls.h"
#include "extensions/common/features/simple_feature.h"
#include "extensions/common/permissions/permission_message_provider.h"
#include "extensions/common/url_pattern_set.h"
#include "shell/common/application_info.h"
#include "shell/common/extensions/electron_extensions_api_provider.h"
using extensions::ExtensionsClient;
namespace electron {
namespace {
// TODO(jamescook): Refactor ChromePermissionsMessageProvider so we can share
// code. For now, this implementation does nothing.
class ElectronPermissionMessageProvider
: public extensions::PermissionMessageProvider {
public:
ElectronPermissionMessageProvider() = default;
~ElectronPermissionMessageProvider() override = default;
// disable copy
ElectronPermissionMessageProvider(const ElectronPermissionMessageProvider&) =
delete;
ElectronPermissionMessageProvider& operator=(
const ElectronPermissionMessageProvider&) = delete;
// PermissionMessageProvider implementation.
[[nodiscard]] extensions::PermissionMessages GetPermissionMessages(
const extensions::PermissionIDSet& permissions) const override {
return extensions::PermissionMessages();
}
[[nodiscard]] bool IsPrivilegeIncrease(
const extensions::PermissionSet& granted_permissions,
const extensions::PermissionSet& requested_permissions,
extensions::Manifest::Type extension_type) const override {
// Ensure we implement this before shipping.
NOTREACHED();
}
[[nodiscard]] extensions::PermissionIDSet GetAllPermissionIDs(
const extensions::PermissionSet& permissions,
extensions::Manifest::Type extension_type) const override {
return {};
}
};
} // namespace
ElectronExtensionsClient::ElectronExtensionsClient()
: webstore_base_url_(extension_urls::kChromeWebstoreBaseURL),
new_webstore_base_url_(extension_urls::kNewChromeWebstoreBaseURL),
webstore_update_url_(extension_urls::kChromeWebstoreUpdateURL) {
AddAPIProvider(std::make_unique<extensions::CoreExtensionsAPIProvider>());
AddAPIProvider(std::make_unique<ElectronExtensionsAPIProvider>());
}
ElectronExtensionsClient::~ElectronExtensionsClient() = default;
void ElectronExtensionsClient::Initialize() {
// TODO(jamescook): Do we need to whitelist any extensions?
}
void ElectronExtensionsClient::InitializeWebStoreUrls(
base::CommandLine* command_line) {}
const extensions::PermissionMessageProvider&
ElectronExtensionsClient::GetPermissionMessageProvider() const {
NOTIMPLEMENTED();
static base::NoDestructor<ElectronPermissionMessageProvider> instance;
return *instance;
}
const std::string ElectronExtensionsClient::GetProductName() {
return GetApplicationName();
}
void ElectronExtensionsClient::FilterHostPermissions(
const extensions::URLPatternSet& hosts,
extensions::URLPatternSet* new_hosts,
extensions::PermissionIDSet* permissions) const {
NOTIMPLEMENTED();
}
void ElectronExtensionsClient::SetScriptingAllowlist(
const ExtensionsClient::ScriptingAllowlist& allowlist) {
scripting_allowlist_ = allowlist;
}
const ExtensionsClient::ScriptingAllowlist&
ElectronExtensionsClient::GetScriptingAllowlist() const {
// TODO(jamescook): Real whitelist.
return scripting_allowlist_;
}
extensions::URLPatternSet
ElectronExtensionsClient::GetPermittedChromeSchemeHosts(
const extensions::Extension* extension,
const extensions::APIPermissionSet& api_permissions) const {
return {};
}
bool ElectronExtensionsClient::IsScriptableURL(const GURL& url,
std::string* error) const {
// No restrictions on URLs.
return true;
}
const GURL& ElectronExtensionsClient::GetWebstoreBaseURL() const {
return webstore_base_url_;
}
const GURL& ElectronExtensionsClient::GetNewWebstoreBaseURL() const {
return new_webstore_base_url_;
}
const GURL& ElectronExtensionsClient::GetWebstoreUpdateURL() const {
return webstore_update_url_;
}
bool ElectronExtensionsClient::IsBlocklistUpdateURL(const GURL& url) const {
return false;
}
} // namespace electron