Skip to content

armgit / cra

Public Imported from GitHub
0

The open-source Arc replacement for macOS. Spaces, pinned tabs, split view, Little Windows and Arc's shortcuts on Chromium, with your Arc sidebar imported in one go.

Web Push, on-device speech recognition and Safe Browsing (#78)

Victor Kildahlb06edf98c41f
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index b9f8387..cc238ca 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -15,6 +15,8 @@ name: Release
# required: installed copies only accept updates it signed
# MACOS_PROVISIONING_PROFILE optional, base64 of the Developer ID provisioning profile
# for dk.kildahl.cra: turns on passkeys (docs/release.md)
+# CRA_GOOGLE_API_KEY optional, Google API key limited to the Safe Browsing API:
+# turns on Safe Browsing (docs/release.md)
on:
push:
@@ -86,6 +88,7 @@ jobs:
APPLE_TEAM_ID: ${{ secrets.APPLE_TEAM_ID }}
APPLE_APP_PASSWORD: ${{ secrets.APPLE_APP_PASSWORD }}
SPARKLE_PRIVATE_KEY: ${{ secrets.SPARKLE_PRIVATE_KEY }}
+ CRA_GOOGLE_API_KEY: ${{ secrets.CRA_GOOGLE_API_KEY }}
run: scripts/release.sh
- name: Upload artifacts
diff --git a/Sources/CraCEF/CraCEF.mm b/Sources/CraCEF/CraCEF.mm
index 6f0b0f4..8dc9c36 100644
--- a/Sources/CraCEF/CraCEF.mm
+++ b/Sources/CraCEF/CraCEF.mm
@@ -2252,6 +2252,12 @@ static void MarkProfileExitedCleanly(NSString* profileDir) {
// Make sure the CefAppProtocol NSApplication exists before CefInitialize.
[CraApplication sharedApplication];
+ // Safe Browsing's list updates need a Google API key. Chromium reads it from the
+ // environment (unbranded builds only); one already set there wins, for testing.
+ if (NSString* key = [NSBundle.mainBundle objectForInfoDictionaryKey:@"CraGoogleAPIKey"]; key.length) {
+ setenv("GOOGLE_API_KEY", key.UTF8String, 0);
+ }
+
CefMainArgs mainArgs(*_NSGetArgc(), *_NSGetArgv());
CefSettings settings;
// Renderer, GPU and utility processes run in Chromium's seatbelt sandbox
diff --git a/docs/release.md b/docs/release.md
index a469b40..792bd71 100644
--- a/docs/release.md
+++ b/docs/release.md
@@ -28,6 +28,7 @@ Everything is scripted; the only manual part is obtaining the certificate once.
| `APPLE_TEAM_ID` | the 10-character team id |
| `APPLE_APP_PASSWORD` | the app-specific password |
| `MACOS_PROVISIONING_PROFILE` | optional, for passkeys (see [Passkeys](#passkeys)) |
+ | `CRA_GOOGLE_API_KEY` | optional, for Safe Browsing (see [Safe Browsing](#safe-browsing)) |
Until these exist the Release workflow still runs: it publishes an ad-hoc build as a
regular release (pre-releases are invisible to `releases/latest`) with the install
@@ -159,6 +160,35 @@ One-time setup, as the Account Holder:
the keychain group, iCloud Keychain once approved), because macOS refuses to launch an
app that claims an entitlement its profile lacks. Ad-hoc builds never get them.
+## Safe Browsing
+
+Chromium's phishing and malware lists come from Google's Safe Browsing API, which
+needs an API key. Without one the lists never download and dangerous pages load
+without a warning.
+
+1. In the Google Cloud console, create a project, enable the **Safe Browsing API** and
+ create an API key. Restrict it to that API: it ships inside the app, so anyone can
+ read it.
+2. Locally: `CRA_GOOGLE_API_KEY=<key>` next to `CRA_SIGN_IDENTITY`. In CI: the
+ `CRA_GOOGLE_API_KEY` secret. Never commit it.
+
+`build.sh` writes it to Info.plist as `CraGoogleAPIKey`, and CraCEF sets
+`GOOGLE_API_KEY` from it before Chromium starts.
+
+The key lives in the Google Cloud project `cra-browser`, restricted to the Safe Browsing
+API. Every install shares its quota: 10,000 list updates and 10,000 lookups a day. A
+running Cra fetches list updates 1–5 minutes after launch and then once a day
+(`scripts/cef-patches/safe-browsing-update-interval.patch`; Chrome uses Google's ~30
+minutes), plus a full-hash lookup when a URL matches a list: about 1–3 updates per
+install per day. An alert policy in the project emails at 7,000 a day; ask for more on
+the API's Quotas page in the Cloud console (the Cloud Quotas API can't). Once the quota
+allows it, shorten the interval in the patch. A client over quota backs off (up to 24
+hours) and its lists go stale until an update succeeds.
+
+The Safe Browsing API is for non-commercial use. Google's commercial service, Web Risk,
+is not a drop-in: it has its own endpoints and request format, so Chromium would need
+a patch to its update and full-hash protocol.
+
## Updates
Cra updates itself the way Arc does, with [Sparkle 2](https://sparkle-project.org)
diff --git a/scripts/build.sh b/scripts/build.sh
index ce02a07..ea7a4f3 100755
--- a/scripts/build.sh
+++ b/scripts/build.sh
@@ -35,6 +35,8 @@ sed -e "s/__VERSION__/$VERSION/" -e "s/__BUILD__/$BUILD_NUMBER/" resources/Info.
# Only release builds (scripts/release.sh) check the update feed; a dev build would
# otherwise replace itself with the latest release.
[ "${CRA_RELEASE:-}" = 1 ] || /usr/libexec/PlistBuddy -c "Delete :SUFeedURL" "$APP/Contents/Info.plist"
+# Google API key for Safe Browsing (docs/release.md); CraCEF hands it to Chromium.
+[ -n "${CRA_GOOGLE_API_KEY:-}" ] && /usr/libexec/PlistBuddy -c "Add :CraGoogleAPIKey string $CRA_GOOGLE_API_KEY" "$APP/Contents/Info.plist"
echo -n "APPL????" > "$APP/Contents/PkgInfo"
[ -f resources/AppIcon.icns ] && cp resources/AppIcon.icns "$APP/Contents/Resources/AppIcon.icns"
[ -d resources/web ] && cp -R resources/web "$APP/Contents/Resources/web"
diff --git a/scripts/cef-patches/gcm-enabled.patch b/scripts/cef-patches/gcm-enabled.patch
new file mode 100644
index 0000000..8007544
--- /dev/null
+++ b/scripts/cef-patches/gcm-enabled.patch
@@ -0,0 +1,32 @@
+Cra: Web Push. CEF stubs out GCM (cef/patch/patches/components_gcm_disable.patch,
+CEF issue 4078) to silence DEPRECATED_ENDPOINT log lines, which also left
+PushManager.subscribe() pending forever. This puts Chromium's GCM client back:
+sites' push subscriptions work again (FCM endpoints, as in Chrome). The
+DEPRECATED_ENDPOINT lines come from Chrome's own com.google.android.gms
+registration, which gives up after a few retries (REACHED_MAX_RETRIES).
+
+diff --git a/components/gcm_driver/gcm_client_impl.cc b/components/gcm_driver/gcm_client_impl.cc
+index fcdae6ccac..2bd1a15816 100644
+--- a/components/gcm_driver/gcm_client_impl.cc
++++ b/components/gcm_driver/gcm_client_impl.cc
+@@ -296,9 +296,6 @@ void GCMClientImpl::Initialize(
+ }
+
+ void GCMClientImpl::Start(StartMode start_mode) {
+- // Disable GCM to avoid DEPRECATED_ENDPOINT errors.
+- return;
+-
+ DCHECK_NE(UNINITIALIZED, state_);
+ DCHECK(io_task_runner_->RunsTasksInCurrentSequence());
+
+@@ -795,10 +792,6 @@ void GCMClientImpl::ResetCache() {
+
+ void GCMClientImpl::Register(
+ scoped_refptr<RegistrationInfo> registration_info) {
+- // Disable GCM to avoid DEPRECATED_ENDPOINT errors.
+- delegate_->OnRegisterFinished(registration_info, std::string(), GCM_DISABLED);
+- return;
+-
+ DCHECK_EQ(state_, READY);
+ DCHECK(io_task_runner_->RunsTasksInCurrentSequence());
+
diff --git a/scripts/cef-patches/safe-browsing-update-interval.patch b/scripts/cef-patches/safe-browsing-update-interval.patch
new file mode 100644
index 0000000..53bc4c8
--- /dev/null
+++ b/scripts/cef-patches/safe-browsing-update-interval.patch
@@ -0,0 +1,29 @@
+Cra: Safe Browsing list updates at most once a day while Cra runs, plus the
+one shortly after launch. All of Cra shares one Google API key and its daily
+quota (10,000 list updates); Chrome's ~30 minute interval would cover a few
+hundred installs. A new phishing site can take until the next launch or day
+to be blocked.
+
+diff --git a/components/safe_browsing/core/browser/db/v4_update_protocol_manager.cc b/components/safe_browsing/core/browser/db/v4_update_protocol_manager.cc
+--- a/components/safe_browsing/core/browser/db/v4_update_protocol_manager.cc
++++ b/components/safe_browsing/core/browser/db/v4_update_protocol_manager.cc
+@@ -4,6 +4,8 @@
+
+ #include "components/safe_browsing/core/browser/db/v4_update_protocol_manager.h"
+
++#include <algorithm>
++
+ #include "base/base64url.h"
+ #include "base/metrics/histogram_functions.h"
+ #include "base/metrics/histogram_macros.h"
+@@ -185,6 +187,10 @@
+ }
+ next_update_interval_ = base::Seconds(minimum_wait_duration_seconds);
+ }
++ // Cra: every Cra shares one API key's daily quota, so after a successful
++ // update the next one waits at least 24 hours (Google asks for ~30 minutes,
++ // a minimum). The first update after launch still comes 1-5 minutes in.
++ next_update_interval_ = std::max(next_update_interval_, base::Hours(24));
+
+ for (ListUpdateResponse& list_update_response :
+ *response.mutable_list_update_responses()) {
diff --git a/scripts/cef-patches/speech-on-device.patch b/scripts/cef-patches/speech-on-device.patch
new file mode 100644
index 0000000..ffa8ee0
--- /dev/null
+++ b/scripts/cef-patches/speech-on-device.patch
@@ -0,0 +1,71 @@
+Cra: Web Speech recognition on this Mac. Chromium sends audio to Google's speech
+service, which needs an API key Google doesn't give other browsers, so every
+start() failed with "network". Pages now get Chromium's on-device recognizer
+(SODA, as with processLocally), and a missing language pack starts downloading
+on the first start().
+
+diff --git a/content/browser/speech/speech_recognition_dispatcher_host.cc b/content/browser/speech/speech_recognition_dispatcher_host.cc
+--- a/content/browser/speech/speech_recognition_dispatcher_host.cc
++++ b/content/browser/speech/speech_recognition_dispatcher_host.cc
+@@ -247,6 +247,18 @@
+ #else
+ bool on_device_available = false;
+ #endif // !BUILDFLAG(IS_ANDROID)
++
++#if !BUILDFLAG(IS_FUCHSIA) && !BUILDFLAG(IS_ANDROID)
++ // Cra: Google's speech service needs an API key Cra can't get, so pages
++ // recognize on this Mac (SODA), as if they had set processLocally, and no
++ // audio goes to Google. A language that isn't installed yet starts
++ // downloading in the availability check above (see
++ // chrome_content_browser_client.cc) and works from a later start().
++ if (can_render_frame_use_on_device && !params->on_device) {
++ params->on_device = true;
++ params->allow_cloud_fallback = false;
++ }
++#endif
+
+ bool use_on_device = false;
+ #if !BUILDFLAG(IS_FUCHSIA) && !BUILDFLAG(IS_ANDROID)
+diff --git a/chrome/browser/chrome_content_browser_client.cc b/chrome/browser/chrome_content_browser_client.cc
+--- a/chrome/browser/chrome_content_browser_client.cc
++++ b/chrome/browser/chrome_content_browser_client.cc
+@@ -616,6 +616,8 @@
+ #include "components/keep_alive_registry/keep_alive_registry.h"
+ #include "components/password_manager/content/common/web_ui_constants.h"
+ #include "components/password_manager/core/common/password_manager_features.h"
++#include "components/soda/constants.h"
++#include "components/soda/soda_installer.h"
+ #include "components/soda/soda_util.h"
+ #include "components/webapps/isolated_web_apps/url_loading/url_loader_factory.h"
+ #include "services/network/public/mojom/permissions_policy/permissions_policy_feature.mojom.h"
+@@ -4607,8 +4609,28 @@
+ content::BrowserContext* context,
+ const std::string& language,
+ media::mojom::SpeechRecognitionQuality quality) {
+- return speech::GetOnDeviceSpeechRecognitionAvailabilityStatus(
+- context, language, quality);
++ media::mojom::AvailabilityStatus status =
++ speech::GetOnDeviceSpeechRecognitionAvailabilityStatus(context, language,
++ quality);
++#if !BUILDFLAG(IS_ANDROID)
++ // Cra: only SpeechRecognition.start() asks here, and Cra always recognizes on
++ // device (speech_recognition_dispatcher_host.cc), so a missing SODA language
++ // pack starts downloading now and serves a later start().
++ std::optional<speech::SodaLanguagePackComponentConfig> config =
++ speech::GetLanguageComponentConfigMatchingLanguageSubtag(language);
++ if (quality == media::mojom::SpeechRecognitionQuality::kCommand &&
++ (status == media::mojom::AvailabilityStatus::kDownloadable ||
++ status == media::mojom::AvailabilityStatus::
++ kDownloadableWithoutTransientUserActivation) &&
++ config.has_value()) {
++ // Both install only what is missing.
++ speech::SodaInstaller::GetInstance()->InstallSoda(
++ g_browser_process->local_state());
++ speech::SodaInstaller::GetInstance()->InstallLanguage(
++ config->language_name, g_browser_process->local_state());
++ }
++#endif // !BUILDFLAG(IS_ANDROID)
++ return status;
+ }
+
+ #if BUILDFLAG(IS_CHROMEOS)
diff --git a/scripts/fetch-cef.sh b/scripts/fetch-cef.sh
index 3e685ae..36d0884 100755
--- a/scripts/fetch-cef.sh
+++ b/scripts/fetch-cef.sh
@@ -13,7 +13,7 @@ CEF_VERSION="${CEF_VERSION:-156.0.15+gcf671c0+chromium-156.0.8078.80}"
ARCH="${CEF_ARCH:-macosarm64}"
NAME="cef_binary_${CEF_VERSION}_${ARCH}_minimal"
# Bump the suffix (codecs2, …) when the build changes but CEF_VERSION does not.
-CEF_RELEASE="${CEF_RELEASE:-cef-${CEF_VERSION%%+*}-codecs2}"
+CEF_RELEASE="${CEF_RELEASE:-cef-${CEF_VERSION%%+*}-codecs5}"
URL="${CEF_URL:-https://github.com/VictorKildahl/cra/releases/download/${CEF_RELEASE}/cef_binary_${ARCH}_minimal.tar.bz2}"
mkdir -p vendor