chrome: Add callback for already running app relaunch (fixes #3609)

Adds a new CefBrowserProcessHandler::OnAlreadyRunningAppRelaunch
callback for when an already running app is relaunched with the
same CefSettings.root_cache_path.

Client apps should check the CefInitialize() return value for early
exit of the relaunch source process.
This commit is contained in:
Marshall Greenblatt
2023-11-28 20:33:44 -05:00
parent d6af79e7a6
commit a25f89f9e4
45 changed files with 553 additions and 178 deletions

View File

@ -1,5 +1,5 @@
diff --git chrome/browser/BUILD.gn chrome/browser/BUILD.gn
index bce36d7ffb408..33fdb680fb20a 100644
index bce36d7ffb408..3e8fd566a375c 100644
--- chrome/browser/BUILD.gn
+++ chrome/browser/BUILD.gn
@@ -11,6 +11,7 @@ import("//build/config/compiler/pgo/pgo.gni")
@ -10,17 +10,7 @@ index bce36d7ffb408..33fdb680fb20a 100644
import("//chrome/browser/buildflags.gni")
import("//chrome/browser/downgrade/buildflags.gni")
import("//chrome/common/features.gni")
@@ -110,7 +111,8 @@ buildflag_header("buildflags") {
# Android and ChromeOS don't support multiple browser processes, so they don't
# employ ProcessSingleton.
- if (is_android || is_chromeos) {
+ # Also disable for CEF (see issue #3609).
+ if (is_android || is_chromeos || enable_cef) {
flags += [ "ENABLE_PROCESS_SINGLETON=0" ]
} else {
flags += [ "ENABLE_PROCESS_SINGLETON=1" ]
@@ -2073,6 +2075,7 @@ static_library("browser") {
@@ -2073,6 +2074,7 @@ static_library("browser") {
"//build/config/chromebox_for_meetings:buildflags",
"//build/config/compiler:compiler_buildflags",
"//cc",
@ -28,7 +18,7 @@ index bce36d7ffb408..33fdb680fb20a 100644
"//chrome:extra_resources",
"//chrome:resources",
"//chrome:strings",
@@ -2729,6 +2732,10 @@ static_library("browser") {
@@ -2729,6 +2731,10 @@ static_library("browser") {
]
}
@ -39,37 +29,3 @@ index bce36d7ffb408..33fdb680fb20a 100644
if (is_android) {
sources += [
"accessibility/accessibility_prefs/android/accessibility_prefs_controller.cc",
diff --git chrome/browser/browser_process_platform_part_win.cc chrome/browser/browser_process_platform_part_win.cc
index 66fa1aa8b10b3..9b506f6d153ae 100644
--- chrome/browser/browser_process_platform_part_win.cc
+++ chrome/browser/browser_process_platform_part_win.cc
@@ -9,6 +9,7 @@
BrowserProcessPlatformPart::BrowserProcessPlatformPart() = default;
BrowserProcessPlatformPart::~BrowserProcessPlatformPart() = default;
+#if BUILDFLAG(ENABLE_PROCESS_SINGLETON)
void BrowserProcessPlatformPart::OnBrowserLaunch() {
if constexpr (kShouldRecordActiveUse) {
if (!did_run_updater_) {
@@ -16,3 +17,4 @@ void BrowserProcessPlatformPart::OnBrowserLaunch() {
}
}
}
+#endif // BUILDFLAG(ENABLE_PROCESS_SINGLETON)
diff --git chrome/browser/browser_process_platform_part_win.h chrome/browser/browser_process_platform_part_win.h
index fc4b9808f3f6e..92cef9459811f 100644
--- chrome/browser/browser_process_platform_part_win.h
+++ chrome/browser/browser_process_platform_part_win.h
@@ -18,10 +18,12 @@ class BrowserProcessPlatformPart : public BrowserProcessPlatformPartBase {
~BrowserProcessPlatformPart() override;
// BrowserProcessPlatformPartBase:
+#if BUILDFLAG(ENABLE_PROCESS_SINGLETON)
void OnBrowserLaunch() override;
private:
absl::optional<DidRunUpdater> did_run_updater_;
+#endif // BUILDFLAG(ENABLE_PROCESS_SINGLETON)
};
#endif // CHROME_BROWSER_BROWSER_PROCESS_PLATFORM_PART_WIN_H_

View File

@ -0,0 +1,70 @@
diff --git chrome/browser/ui/startup/startup_browser_creator.cc chrome/browser/ui/startup/startup_browser_creator.cc
index cef85a137d3e2..985d716ec7549 100644
--- chrome/browser/ui/startup/startup_browser_creator.cc
+++ chrome/browser/ui/startup/startup_browser_creator.cc
@@ -578,6 +578,14 @@ void OpenNewWindowForFirstRun(
is_first_run, std::move(launch_mode_recorder));
}
#endif // BUILDFLAG(IS_CHROMEOS_LACROS) || BUILDFLAG(ENABLE_DICE_SUPPORT)
+
+StartupBrowserCreator::ProcessCommandLineCallback*
+GetProcessCommandLineCallback() {
+ static base::NoDestructor<StartupBrowserCreator::ProcessCommandLineCallback>
+ callback;
+ return callback.get();
+}
+
} // namespace
StartupProfileMode StartupProfileModeFromReason(
@@ -1441,6 +1449,12 @@ void StartupBrowserCreator::ProcessCommandLineWithProfile(
{profile, mode}, last_opened_profiles);
}
+// static
+void StartupBrowserCreator::RegisterProcessCommandLineCallback(
+ ProcessCommandLineCallback cb) {
+ *GetProcessCommandLineCallback() = cb;
+}
+
// static
void StartupBrowserCreator::ProcessCommandLineAlreadyRunning(
const base::CommandLine& command_line,
@@ -1450,6 +1464,11 @@ void StartupBrowserCreator::ProcessCommandLineAlreadyRunning(
return;
}
+ auto* cb = GetProcessCommandLineCallback();
+ if (!cb->is_null() && cb->Run(command_line, cur_dir)) {
+ return;
+ }
+
Profile* profile = nullptr;
StartupProfileMode mode =
StartupProfileModeFromReason(profile_path_info.reason);
diff --git chrome/browser/ui/startup/startup_browser_creator.h chrome/browser/ui/startup/startup_browser_creator.h
index e22ea9b782910..68724228e79f5 100644
--- chrome/browser/ui/startup/startup_browser_creator.h
+++ chrome/browser/ui/startup/startup_browser_creator.h
@@ -9,6 +9,7 @@
#include <vector>
#include "base/files/file_path.h"
+#include "base/functional/callback.h"
#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "build/build_config.h"
@@ -136,6 +137,13 @@ class StartupBrowserCreator {
StartupProfileInfo profile_info,
const Profiles& last_opened_profiles);
+ // Registers a callback that will be executed each time
+ // ProcessCommandLineAlreadyRunning is called.
+ using ProcessCommandLineCallback = base::RepeatingCallback<bool(
+ const base::CommandLine& command_line,
+ const base::FilePath& cur_dir)>;
+ static void RegisterProcessCommandLineCallback(ProcessCommandLineCallback cb);
+
// This function performs command-line handling and is invoked only after
// start up (for example when we get a start request for another process).
// |command_line| holds the command line we need to process.

View File

@ -1,5 +1,5 @@
diff --git chrome/app/chrome_main_delegate.cc chrome/app/chrome_main_delegate.cc
index dc84cbd4c97d1..b8a1dd5a19be1 100644
index dc84cbd4c97d1..696af79e6cec5 100644
--- chrome/app/chrome_main_delegate.cc
+++ chrome/app/chrome_main_delegate.cc
@@ -41,6 +41,7 @@
@ -30,6 +30,15 @@ index dc84cbd4c97d1..b8a1dd5a19be1 100644
absl::optional<int> ChromeMainDelegate::PostEarlyInitialization(
InvokedIn invoked_in) {
DCHECK(base::ThreadPoolInstance::Get());
@@ -782,7 +789,7 @@ absl::optional<int> ChromeMainDelegate::PostEarlyInitialization(
// future session's metrics.
DeferBrowserMetrics(user_data_dir);
-#if BUILDFLAG(IS_WIN)
+#if BUILDFLAG(IS_WIN) && !BUILDFLAG(ENABLE_CEF)
// In the case the process is not the singleton process, the uninstall tasks
// need to be executed here. A window will be displayed asking to close all
// running instances.
@@ -946,7 +953,8 @@ absl::optional<int> ChromeMainDelegate::PostEarlyInitialization(
if (base::FeatureList::IsEnabled(
@ -126,7 +135,7 @@ index 176b6248f18e8..1360e8c209eba 100644
#if BUILDFLAG(IS_CHROMEOS_LACROS)
std::unique_ptr<chromeos::LacrosService> lacros_service_;
diff --git chrome/browser/chrome_browser_main.cc chrome/browser/chrome_browser_main.cc
index e819129536dc0..d0f1e684ae019 100644
index e819129536dc0..e9287f6e03a88 100644
--- chrome/browser/chrome_browser_main.cc
+++ chrome/browser/chrome_browser_main.cc
@@ -52,6 +52,7 @@
@ -137,6 +146,15 @@ index e819129536dc0..d0f1e684ae019 100644
#include "chrome/browser/about_flags.h"
#include "chrome/browser/active_use_util.h"
#include "chrome/browser/after_startup_task_utils.h"
@@ -486,7 +487,7 @@ void ProcessSingletonNotificationCallbackImpl(
return;
}
-#if BUILDFLAG(IS_WIN)
+#if BUILDFLAG(IS_WIN) && !BUILDFLAG(ENABLE_CEF)
// The uninstall command-line switch is handled by the origin process; see
// ChromeMainDelegate::PostEarlyInitialization(...). The other process won't
// be able to become the singleton process and will display a window asking
@@ -774,7 +775,7 @@ int ChromeBrowserMainParts::PreEarlyInitialization() {
return content::RESULT_CODE_NORMAL_EXIT;
}