From d6019634f9ac1d30a8beb55390a45a94881df60c Mon Sep 17 00:00:00 2001 From: Samuel Attard Date: Fri, 13 Nov 2020 12:53:32 -0800 Subject: [PATCH] feat: add app.runningUnderRosettaTranslation to detect running under rosetta (#26444) * feat: add app.isRunningUnderRosettaTranslation to detect running under rosetta * chore: fixup * chore: make const * chore: add missing import --- docs/api/app.md | 9 +++++++++ shell/browser/api/electron_api_app.cc | 2 ++ shell/browser/api/electron_api_app.h | 1 + shell/browser/api/electron_api_app_mac.mm | 11 +++++++++++ 4 files changed, 23 insertions(+) diff --git a/docs/api/app.md b/docs/api/app.md index aef3250d2ed5..b485b339683c 100644 --- a/docs/api/app.md +++ b/docs/api/app.md @@ -1489,3 +1489,12 @@ which native modules you can use in the renderer process. For more information on the direction Electron is going with renderer process restarts and usage of native modules in the renderer process please check out this [Tracking Issue](https://github.com/electron/electron/issues/18397). + +### `app.runningUnderRosettaTranslation` _macOS_ _Readonly_ + +A `Boolean` which when `true` indicates that the app is currently running +under the [Rosetta Translator Environment](https://en.wikipedia.org/wiki/Rosetta_(software)). + +You can use this property to prompt users to download the arm64 version of +your application when they are running the x64 version under Rosetta +incorrectly. diff --git a/shell/browser/api/electron_api_app.cc b/shell/browser/api/electron_api_app.cc index cee2493f821f..4e4cf7bae53b 100644 --- a/shell/browser/api/electron_api_app.cc +++ b/shell/browser/api/electron_api_app.cc @@ -1666,6 +1666,8 @@ gin::ObjectTemplateBuilder App::GetObjectTemplateBuilder(v8::Isolate* isolate) { #endif #if defined(OS_MAC) .SetProperty("dock", &App::GetDockAPI) + .SetProperty("runningUnderRosettaTranslation", + &App::IsRunningUnderRosettaTranslation) #endif .SetProperty("userAgentFallback", &App::GetUserAgentFallback, &App::SetUserAgentFallback) diff --git a/shell/browser/api/electron_api_app.h b/shell/browser/api/electron_api_app.h index 10e5961166fa..096cc919f8fe 100644 --- a/shell/browser/api/electron_api_app.h +++ b/shell/browser/api/electron_api_app.h @@ -221,6 +221,7 @@ class App : public ElectronBrowserClient::Delegate, bool MoveToApplicationsFolder(gin_helper::ErrorThrower, gin::Arguments* args); bool IsInApplicationsFolder(); v8::Local GetDockAPI(v8::Isolate* isolate); + bool IsRunningUnderRosettaTranslation() const; v8::Global dock_; #endif diff --git a/shell/browser/api/electron_api_app_mac.mm b/shell/browser/api/electron_api_app_mac.mm index c317e9d40a29..8a4458f9aed2 100644 --- a/shell/browser/api/electron_api_app_mac.mm +++ b/shell/browser/api/electron_api_app_mac.mm @@ -9,6 +9,7 @@ #include "shell/common/electron_paths.h" #import +#import namespace electron { @@ -58,6 +59,16 @@ void App::SetActivationPolicy(gin_helper::ErrorThrower thrower, [NSApp setActivationPolicy:activation_policy]; } +bool App::IsRunningUnderRosettaTranslation() const { + int proc_translated = 0; + size_t size = sizeof(proc_translated); + if (sysctlbyname("sysctl.proc_translated", &proc_translated, &size, NULL, + 0) == -1) { + return false; + } + return proc_translated == 1; +} + } // namespace api } // namespace electron