refactor: eliminate duplicate C++ / JavaScript implementation of app.isPackaged (#29464)

This commit is contained in:
Milan Burda 2021-06-02 21:17:08 +02:00 committed by GitHub
parent f0d3e1d1cf
commit 44491b023a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 22 deletions

View file

@ -55,14 +55,6 @@ Object.defineProperty(app, 'applicationMenu', {
}
});
(app as any).isPackaged = (() => {
const execFile = path.basename(process.execPath).toLowerCase();
if (process.platform === 'win32') {
return execFile !== 'electron.exe';
}
return execFile !== 'electron';
})();
// The native implementation is not provided on non-windows platforms
app.setAppUserModelId = app.setAppUserModelId || (() => {});

View file

@ -936,6 +936,20 @@ void App::SetAppLogsPath(gin_helper::ErrorThrower thrower,
}
#endif
// static
bool App::IsPackaged() {
base::FilePath exe_path;
base::PathService::Get(base::FILE_EXE, &exe_path);
base::FilePath::StringType base_name =
base::ToLowerASCII(exe_path.BaseName().value());
#if defined(OS_WIN)
return base_name != FILE_PATH_LITERAL("electron.exe");
#else
return base_name != FILE_PATH_LITERAL("electron");
#endif
}
base::FilePath App::GetPath(gin_helper::ErrorThrower thrower,
const std::string& name) {
bool succeed = false;
@ -1628,6 +1642,7 @@ gin::ObjectTemplateBuilder App::GetObjectTemplateBuilder(v8::Isolate* isolate) {
.SetMethod("isUnityRunning",
base::BindRepeating(&Browser::IsUnityRunning, browser))
#endif
.SetProperty("isPackaged", &App::IsPackaged)
.SetMethod("setAppPath", &App::SetAppPath)
.SetMethod("getAppPath", &App::GetAppPath)
.SetMethod("setPath", &App::SetPath)

View file

@ -75,6 +75,8 @@ class App : public ElectronBrowserClient::Delegate,
void RenderProcessReady(content::RenderProcessHost* host);
void RenderProcessExited(content::RenderProcessHost* host);
static bool IsPackaged();
App();
private:

View file

@ -24,6 +24,7 @@
#include "content/public/browser/browser_thread.h"
#include "content/public/common/content_paths.h"
#include "electron/buildflags/buildflags.h"
#include "shell/browser/api/electron_api_app.h"
#include "shell/common/api/electron_bindings.h"
#include "shell/common/electron_command_line.h"
#include "shell/common/gin_converters/file_path_converter.h"
@ -130,19 +131,6 @@ void stop_and_close_uv_loop(uv_loop_t* loop) {
bool g_is_initialized = false;
bool IsPackagedApp() {
base::FilePath exe_path;
base::PathService::Get(base::FILE_EXE, &exe_path);
base::FilePath::StringType base_name =
base::ToLowerASCII(exe_path.BaseName().value());
#if defined(OS_WIN)
return base_name != FILE_PATH_LITERAL("electron.exe");
#else
return base_name != FILE_PATH_LITERAL("electron");
#endif
}
void V8FatalErrorCallback(const char* location, const char* message) {
LOG(ERROR) << "Fatal error in V8: " << location << " " << message;
@ -257,7 +245,7 @@ void SetNodeOptions(base::Environment* env) {
std::vector<std::string> parts = base::SplitString(
options, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
bool is_packaged_app = IsPackagedApp();
bool is_packaged_app = electron::api::App::IsPackaged();
for (const auto& part : parts) {
// Strip off values passed to individual NODE_OPTIONs