refactor: Convert ProcessSingleton changes to patch (#30594)

* Convert ProcessSingleton changes to patch

* Update patch

* Polish

* Add sandbox check to patch

* Add missing includes

* Fix linking error

* Fix compile error

* Apply PR feedback

* Fix compile fails

* Fix tests

* Remove extra patch

* Update test
This commit is contained in:
Raymond Zhao 2021-09-03 14:16:33 -07:00 committed by GitHub
parent b8372f20a0
commit e6f781f403
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 349 additions and 1621 deletions

View file

@ -3,8 +3,12 @@
// found in the LICENSE file.
#include "shell/app/command_line_args.h"
#include <locale>
#include "sandbox/policy/switches.h"
#include "shell/common/options_switches.h"
namespace {
bool IsUrlArg(const base::CommandLine::CharType* arg) {
@ -51,4 +55,9 @@ bool CheckCommandLineArguments(int argc, base::CommandLine::CharType** argv) {
return true;
}
bool IsSandboxEnabled(base::CommandLine* command_line) {
return command_line->HasSwitch(switches::kEnableSandbox) ||
!command_line->HasSwitch(sandbox::policy::switches::kNoSandbox);
}
} // namespace electron

View file

@ -10,6 +10,7 @@
namespace electron {
bool CheckCommandLineArguments(int argc, base::CommandLine::CharType** argv);
bool IsSandboxEnabled(base::CommandLine* command_line);
} // namespace electron

View file

@ -25,6 +25,7 @@
#include "ipc/ipc_buildflags.h"
#include "sandbox/policy/switches.h"
#include "services/tracing/public/cpp/stack_sampling/tracing_sampler_profiler.h"
#include "shell/app/command_line_args.h"
#include "shell/app/electron_content_client.h"
#include "shell/browser/electron_browser_client.h"
#include "shell/browser/electron_gpu_client.h"
@ -82,11 +83,6 @@ bool IsBrowserProcess(base::CommandLine* cmd) {
return process_type.empty();
}
bool IsSandboxEnabled(base::CommandLine* command_line) {
return command_line->HasSwitch(switches::kEnableSandbox) ||
!command_line->HasSwitch(sandbox::policy::switches::kNoSandbox);
}
// Returns true if this subprocess type needs the ResourceBundle initialized
// and resources loaded.
bool SubprocessNeedsResourceBundle(const std::string& process_type) {

View file

@ -38,6 +38,7 @@
#include "net/ssl/ssl_private_key.h"
#include "sandbox/policy/switches.h"
#include "services/network/network_service.h"
#include "shell/app/command_line_args.h"
#include "shell/browser/api/electron_api_menu.h"
#include "shell/browser/api/electron_api_session.h"
#include "shell/browser/api/electron_api_web_contents.h"
@ -511,9 +512,9 @@ int GetPathConstant(const std::string& name) {
bool NotificationCallbackWrapper(
const base::RepeatingCallback<
void(const base::CommandLine::StringVector& command_line,
void(const base::CommandLine& command_line,
const base::FilePath& current_directory)>& callback,
const base::CommandLine::StringVector& cmd,
const base::CommandLine& cmd,
const base::FilePath& cwd) {
// Make sure the callback is called after app gets ready.
if (Browser::Get()->is_ready()) {
@ -1067,9 +1068,9 @@ std::string App::GetLocaleCountryCode() {
return region.size() == 2 ? region : std::string();
}
void App::OnSecondInstance(const base::CommandLine::StringVector& cmd,
void App::OnSecondInstance(const base::CommandLine& cmd,
const base::FilePath& cwd) {
Emit("second-instance", cmd, cwd);
Emit("second-instance", cmd.argv(), cwd);
}
bool App::HasSingleInstanceLock() const {
@ -1082,13 +1083,23 @@ bool App::RequestSingleInstanceLock() {
if (HasSingleInstanceLock())
return true;
std::string program_name = electron::Browser::Get()->GetName();
base::FilePath user_dir;
base::PathService::Get(chrome::DIR_USER_DATA, &user_dir);
auto cb = base::BindRepeating(&App::OnSecondInstance, base::Unretained(this));
#if defined(OS_WIN)
bool app_is_sandboxed =
IsSandboxEnabled(base::CommandLine::ForCurrentProcess());
process_singleton_ = std::make_unique<ProcessSingleton>(
program_name, user_dir, app_is_sandboxed,
base::BindRepeating(NotificationCallbackWrapper, cb));
#else
process_singleton_ = std::make_unique<ProcessSingleton>(
user_dir, base::BindRepeating(NotificationCallbackWrapper, cb));
#endif
switch (process_singleton_->NotifyOtherProcessOrCreate()) {
case ProcessSingleton::NotifyResult::LOCK_ERROR:

View file

@ -188,7 +188,7 @@ class App : public ElectronBrowserClient::Delegate,
void SetDesktopName(const std::string& desktop_name);
std::string GetLocale();
std::string GetLocaleCountryCode();
void OnSecondInstance(const base::CommandLine::StringVector& cmd,
void OnSecondInstance(const base::CommandLine& cmd,
const base::FilePath& cwd);
bool HasSingleInstanceLock() const;
bool RequestSingleInstanceLock();