Commit 53b8892c authored by Christopher Cameron's avatar Christopher Cameron Committed by Commit Bot

MacPWAs: Add AppShimManager::LoadAndLaunchApp function

This is a no-functional-change refactor that changes the
function OnShimProcessConnectedForLaunch (which is used
to launch the app when an app shim connects) to be more
generically written.

This is towards unifying four code paths:
1. OnShimProcessConnectedForLaunch, when the user launches
   an app shim
2. OnShimSelectedProfile, when the user selects a profile
   from the Profile menu
3. OnShimOpenedFiles, when the user opens files (e.g, by
   opening in Finder)
4. OnShimReopen, which is the *new* behavior in this bug,
   which will open a new window when the shim is running
   but has no active profiles.

The next patch in this sequence will changes paths 2 and 3
to use this function.

Bug: 1094419
Change-Id: I05265b9923db0f811bed239f2cd4db34e4c6553c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2402164Reviewed-by: default avatarAlexey Baskakov <loyso@chromium.org>
Reviewed-by: default avatarDaniel Murphy <dmurph@chromium.org>
Commit-Queue: ccameron <ccameron@chromium.org>
Cr-Commit-Position: refs/heads/master@{#805796}
parent 4968cc66
...@@ -297,9 +297,18 @@ void AppShimManager::OnShimProcessConnected( ...@@ -297,9 +297,18 @@ void AppShimManager::OnShimProcessConnected(
std::unique_ptr<AppShimHostBootstrap> bootstrap) { std::unique_ptr<AppShimHostBootstrap> bootstrap) {
DCHECK(crx_file::id_util::IdIsValid(bootstrap->GetAppId())); DCHECK(crx_file::id_util::IdIsValid(bootstrap->GetAppId()));
switch (bootstrap->GetLaunchType()) { switch (bootstrap->GetLaunchType()) {
case chrome::mojom::AppShimLaunchType::kNormal: case chrome::mojom::AppShimLaunchType::kNormal: {
OnShimProcessConnectedForLaunch(std::move(bootstrap)); const web_app::AppId app_id = bootstrap->GetAppId();
const base::FilePath profile_path = bootstrap->GetProfilePath();
const std::vector<base::FilePath> launch_files =
bootstrap->GetLaunchFiles();
LoadAndLaunchAppCallback launch_callback = base::BindOnce(
&AppShimManager::OnShimProcessConnectedAndAllLaunchesDone,
weak_factory_.GetWeakPtr(), std::move(bootstrap));
LoadAndLaunchApp(app_id, profile_path, launch_files,
std::move(launch_callback));
break; break;
}
case chrome::mojom::AppShimLaunchType::kRegisterOnly: case chrome::mojom::AppShimLaunchType::kRegisterOnly:
OnShimProcessConnectedForRegisterOnly(std::move(bootstrap)); OnShimProcessConnectedForRegisterOnly(std::move(bootstrap));
break; break;
...@@ -339,18 +348,17 @@ void AppShimManager::OnShimProcessConnectedForRegisterOnly( ...@@ -339,18 +348,17 @@ void AppShimManager::OnShimProcessConnectedForRegisterOnly(
} }
OnShimProcessConnectedAndAllLaunchesDone( OnShimProcessConnectedAndAllLaunchesDone(
profile_state, std::move(bootstrap), profile_state,
profile_state ? chrome::mojom::AppShimLaunchResult::kSuccess profile_state
: chrome::mojom::AppShimLaunchResult::kSuccessAndDisconnect, ? chrome::mojom::AppShimLaunchResult::kSuccess
std::move(bootstrap)); : chrome::mojom::AppShimLaunchResult::kSuccessAndDisconnect);
} }
void AppShimManager::OnShimProcessConnectedForLaunch( void AppShimManager::LoadAndLaunchApp(
std::unique_ptr<AppShimHostBootstrap> bootstrap) { const web_app::AppId& app_id,
const web_app::AppId& app_id = bootstrap->GetAppId(); const base::FilePath& profile_path,
DCHECK_EQ(bootstrap->GetLaunchType(), std::vector<base::FilePath> launch_files,
chrome::mojom::AppShimLaunchType::kNormal); LoadAndLaunchAppCallback launch_callback) {
// Retrieve the list of last-active profiles. If there are no last-active // Retrieve the list of last-active profiles. If there are no last-active
// profiles (which is rare -- e.g, when the last-active profiles were // profiles (which is rare -- e.g, when the last-active profiles were
// removed), then use all profiles for which the app is installed. // removed), then use all profiles for which the app is installed.
...@@ -364,8 +372,7 @@ void AppShimManager::OnShimProcessConnectedForLaunch( ...@@ -364,8 +372,7 @@ void AppShimManager::OnShimProcessConnectedForLaunch(
// Construct |profile_paths_to_launch| to be the list of all profiles to // Construct |profile_paths_to_launch| to be the list of all profiles to
// attempt to launch, starting with the profile specified in |bootstrap|, // attempt to launch, starting with the profile specified in |bootstrap|,
// at the front of the list. // at the front of the list.
std::vector<base::FilePath> profile_paths_to_launch = { std::vector<base::FilePath> profile_paths_to_launch = {profile_path};
bootstrap->GetProfilePath()};
for (const auto& profile_path : last_active_profile_paths) for (const auto& profile_path : last_active_profile_paths)
profile_paths_to_launch.push_back(profile_path); profile_paths_to_launch.push_back(profile_path);
...@@ -373,9 +380,9 @@ void AppShimManager::OnShimProcessConnectedForLaunch( ...@@ -373,9 +380,9 @@ void AppShimManager::OnShimProcessConnectedForLaunch(
// they're loaded (or have failed to load), call // they're loaded (or have failed to load), call
// OnShimProcessConnectedAndProfilesToLaunchLoaded. // OnShimProcessConnectedAndProfilesToLaunchLoaded.
base::OnceClosure callback = base::BindOnce( base::OnceClosure callback = base::BindOnce(
&AppShimManager::OnShimProcessConnectedAndProfilesToLaunchLoaded, &AppShimManager::LoadAndLaunchApp_OnProfilesAndAppReady,
weak_factory_.GetWeakPtr(), std::move(bootstrap), weak_factory_.GetWeakPtr(), app_id, std::move(launch_files),
profile_paths_to_launch); profile_paths_to_launch, std::move(launch_callback));
{ {
// This will update |callback| to be a chain of callbacks that load the // This will update |callback| to be a chain of callbacks that load the
// profiles in |profile_paths_to_load|, one by one, using // profiles in |profile_paths_to_load|, one by one, using
...@@ -385,7 +392,7 @@ void AppShimManager::OnShimProcessConnectedForLaunch( ...@@ -385,7 +392,7 @@ void AppShimManager::OnShimProcessConnectedForLaunch(
for (const auto& profile_path : profile_paths_to_launch) { for (const auto& profile_path : profile_paths_to_launch) {
if (profile_path.empty()) if (profile_path.empty())
continue; continue;
LoadProfileAppCallback callback_wrapped = LoadProfileAndAppCallback callback_wrapped =
base::BindOnce([](base::OnceClosure callback_to_wrap, base::BindOnce([](base::OnceClosure callback_to_wrap,
Profile*) { std::move(callback_to_wrap).Run(); }, Profile*) { std::move(callback_to_wrap).Run(); },
std::move(callback)); std::move(callback));
...@@ -397,16 +404,11 @@ void AppShimManager::OnShimProcessConnectedForLaunch( ...@@ -397,16 +404,11 @@ void AppShimManager::OnShimProcessConnectedForLaunch(
std::move(callback).Run(); std::move(callback).Run();
} }
void AppShimManager::OnShimProcessConnectedAndProfilesToLaunchLoaded( void AppShimManager::LoadAndLaunchApp_OnProfilesAndAppReady(
std::unique_ptr<AppShimHostBootstrap> bootstrap, const web_app::AppId& app_id,
const std::vector<base::FilePath>& profile_paths_to_launch) { std::vector<base::FilePath> launch_files,
// The the profile specified in |bootstrap| (even if it's empty) should be the const std::vector<base::FilePath>& profile_paths_to_launch,
// first profile listed in |profile_paths_to_launch|. LoadAndLaunchAppCallback launch_callback) {
DCHECK_EQ(profile_paths_to_launch[0], bootstrap->GetProfilePath());
const auto& app_id = bootstrap->GetAppId();
auto launch_files = bootstrap->GetLaunchFiles();
// Launch all of the profiles in |profile_paths_to_launch|. Record the most // Launch all of the profiles in |profile_paths_to_launch|. Record the most
// profile successfully launched in |launched_profile_state|, and the most // profile successfully launched in |launched_profile_state|, and the most
// recent reason for a failure (if any) in |launch_result|. // recent reason for a failure (if any) in |launch_result|.
...@@ -472,14 +474,13 @@ void AppShimManager::OnShimProcessConnectedAndProfilesToLaunchLoaded( ...@@ -472,14 +474,13 @@ void AppShimManager::OnShimProcessConnectedAndProfilesToLaunchLoaded(
if (launched_profile_state) if (launched_profile_state)
launch_result = chrome::mojom::AppShimLaunchResult::kSuccess; launch_result = chrome::mojom::AppShimLaunchResult::kSuccess;
OnShimProcessConnectedAndAllLaunchesDone(launched_profile_state, std::move(launch_callback).Run(launched_profile_state, launch_result);
launch_result, std::move(bootstrap));
} }
void AppShimManager::OnShimProcessConnectedAndAllLaunchesDone( void AppShimManager::OnShimProcessConnectedAndAllLaunchesDone(
std::unique_ptr<AppShimHostBootstrap> bootstrap,
ProfileState* profile_state, ProfileState* profile_state,
chrome::mojom::AppShimLaunchResult result, chrome::mojom::AppShimLaunchResult result) {
std::unique_ptr<AppShimHostBootstrap> bootstrap) {
// If we failed because the profile was locked, launch the profile manager. // If we failed because the profile was locked, launch the profile manager.
if (result == chrome::mojom::AppShimLaunchResult::kProfileLocked) if (result == chrome::mojom::AppShimLaunchResult::kProfileLocked)
LaunchUserManager(); LaunchUserManager();
...@@ -565,22 +566,23 @@ void AppShimManager::CloseShimForApp(Profile* profile, ...@@ -565,22 +566,23 @@ void AppShimManager::CloseShimForApp(Profile* profile,
void AppShimManager::LoadProfileAndApp(const base::FilePath& profile_path, void AppShimManager::LoadProfileAndApp(const base::FilePath& profile_path,
const web_app::AppId& app_id, const web_app::AppId& app_id,
LoadProfileAppCallback callback) { LoadProfileAndAppCallback callback) {
// Run |profile_loaded_callback| when the profile is loaded (be that now, or // Run |profile_loaded_callback| when the profile is loaded (be that now, or
// after having to asynchronously load the profile). // after having to asynchronously load the profile).
auto profile_loaded_callback = base::BindOnce( auto profile_loaded_callback = base::BindOnce(
&AppShimManager::OnProfileLoaded, weak_factory_.GetWeakPtr(), &AppShimManager::LoadProfileAndApp_OnProfileLoaded,
profile_path, app_id, std::move(callback)); weak_factory_.GetWeakPtr(), profile_path, app_id, std::move(callback));
if (auto* profile = ProfileForPath(profile_path)) if (auto* profile = ProfileForPath(profile_path))
std::move(profile_loaded_callback).Run(profile); std::move(profile_loaded_callback).Run(profile);
else else
LoadProfileAsync(profile_path, std::move(profile_loaded_callback)); LoadProfileAsync(profile_path, std::move(profile_loaded_callback));
} }
void AppShimManager::OnProfileLoaded(const base::FilePath& profile_path, void AppShimManager::LoadProfileAndApp_OnProfileLoaded(
const web_app::AppId& app_id, const base::FilePath& profile_path,
LoadProfileAppCallback callback, const web_app::AppId& app_id,
Profile* profile) { LoadProfileAndAppCallback callback,
Profile* profile) {
// It may be that the profile fails to load. // It may be that the profile fails to load.
DCHECK_CURRENTLY_ON(content::BrowserThread::UI); DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (!profile) { if (!profile) {
...@@ -593,15 +595,15 @@ void AppShimManager::OnProfileLoaded(const base::FilePath& profile_path, ...@@ -593,15 +595,15 @@ void AppShimManager::OnProfileLoaded(const base::FilePath& profile_path,
// launching. // launching.
// https://crbug.com/1094419. // https://crbug.com/1094419.
auto registry_ready_callback = base::BindOnce( auto registry_ready_callback = base::BindOnce(
&AppShimManager::OnProfileAppRegistryReady, weak_factory_.GetWeakPtr(), &AppShimManager::LoadProfileAndApp_OnProfileAppRegistryReady,
profile_path, app_id, std::move(callback)); weak_factory_.GetWeakPtr(), profile_path, app_id, std::move(callback));
WaitForAppRegistryReadyAsync(profile, std::move(registry_ready_callback)); WaitForAppRegistryReadyAsync(profile, std::move(registry_ready_callback));
} }
void AppShimManager::OnProfileAppRegistryReady( void AppShimManager::LoadProfileAndApp_OnProfileAppRegistryReady(
const base::FilePath& profile_path, const base::FilePath& profile_path,
const web_app::AppId& app_id, const web_app::AppId& app_id,
LoadProfileAppCallback callback) { LoadProfileAndAppCallback callback) {
// It may be that the profile was destroyed while waiting for the callback to // It may be that the profile was destroyed while waiting for the callback to
// be issued. // be issued.
Profile* profile = ProfileForPath(profile_path); Profile* profile = ProfileForPath(profile_path);
...@@ -611,9 +613,9 @@ void AppShimManager::OnProfileAppRegistryReady( ...@@ -611,9 +613,9 @@ void AppShimManager::OnProfileAppRegistryReady(
} }
// Run |app_enabled_callback| once the app is enabled (now or async). Note // Run |app_enabled_callback| once the app is enabled (now or async). Note
// that this is only relevant for extension-based apps. // that this is only relevant for extension-based apps.
auto app_enabled_callback = auto app_enabled_callback = base::BindOnce(
base::BindOnce(&AppShimManager::OnAppEnabled, weak_factory_.GetWeakPtr(), &AppShimManager::LoadProfileAndApp_OnAppEnabled,
profile_path, app_id, std::move(callback)); weak_factory_.GetWeakPtr(), profile_path, app_id, std::move(callback));
if (delegate_->AppIsInstalled(profile, app_id)) { if (delegate_->AppIsInstalled(profile, app_id)) {
std::move(app_enabled_callback).Run(); std::move(app_enabled_callback).Run();
} else { } else {
...@@ -622,9 +624,10 @@ void AppShimManager::OnProfileAppRegistryReady( ...@@ -622,9 +624,10 @@ void AppShimManager::OnProfileAppRegistryReady(
} }
} }
void AppShimManager::OnAppEnabled(const base::FilePath& profile_path, void AppShimManager::LoadProfileAndApp_OnAppEnabled(
const web_app::AppId& app_id, const base::FilePath& profile_path,
LoadProfileAppCallback callback) { const web_app::AppId& app_id,
LoadProfileAndAppCallback callback) {
std::move(callback).Run(ProfileForPath(profile_path)); std::move(callback).Run(ProfileForPath(profile_path));
} }
......
...@@ -242,27 +242,39 @@ class AppShimManager : public AppShimHostBootstrap::Client, ...@@ -242,27 +242,39 @@ class AppShimManager : public AppShimHostBootstrap::Client,
void OnShimProcessConnectedForRegisterOnly( void OnShimProcessConnectedForRegisterOnly(
std::unique_ptr<AppShimHostBootstrap> bootstrap); std::unique_ptr<AppShimHostBootstrap> bootstrap);
// This is called by OnShimProcessConnected when the shim was was launched // The function LoadAndLaunchApp will:
// not by Chrome, and needs to launch the app (that is, open a new app // - Find the appropriate profiles for which |app_id| should be launched.
// window). // - Load the profiles and ensure the app is enabled (using
void OnShimProcessConnectedForLaunch( // LoadProfileAndApp).
std::unique_ptr<AppShimHostBootstrap> bootstrap); // - Launch the app, if appropriate.
// The "if appropriate" above is defined as:
// Continuation of OnShimProcessConnectedForLaunch, once all of the profiles // - If |launch_files| is non-empty, then will always launch the app
// to use have been loaded. The list of profiles to launch is in // - If |profile_path| is non-empty, then use that profile.
// |profile_paths_to_launch|. The first entry corresponds to the bootstrap- // - In the most recently used profile, otherwise
// specified profile, and may be a blank path. // - If |launch_files| is empty, then launch the app only if:
void OnShimProcessConnectedAndProfilesToLaunchLoaded( // - If |profile_path| is non-empty, then launch if the app is not running
std::unique_ptr<AppShimHostBootstrap> bootstrap, // in that profile.
const std::vector<base::FilePath>& profile_paths_to_launch); // - Otherwise, launch the app only if it is not running any profile.
using LoadAndLaunchAppCallback =
base::OnceCallback<void(ProfileState* profile_state,
chrome::mojom::AppShimLaunchResult result)>;
void LoadAndLaunchApp(const web_app::AppId& app_id,
const base::FilePath& profile_path,
std::vector<base::FilePath> launch_files,
LoadAndLaunchAppCallback launch_callback);
void LoadAndLaunchApp_OnProfilesAndAppReady(
const web_app::AppId& app_id,
std::vector<base::FilePath> launch_files,
const std::vector<base::FilePath>& profile_paths_to_launch,
LoadAndLaunchAppCallback launch_callback);
// The final step of both paths for OnShimProcessConnected. This will connect // The final step of both paths for OnShimProcessConnected. This will connect
// |bootstrap| to |profile_state|'s AppShimHost, if possible. The value of // |bootstrap| to |profile_state|'s AppShimHost, if possible. The value of
// |profile_state| is non-null if and only if |result| is success. // |profile_state| is non-null if and only if |result| is success.
void OnShimProcessConnectedAndAllLaunchesDone( void OnShimProcessConnectedAndAllLaunchesDone(
std::unique_ptr<AppShimHostBootstrap> bootstrap,
ProfileState* profile_state, ProfileState* profile_state,
chrome::mojom::AppShimLaunchResult result, chrome::mojom::AppShimLaunchResult result);
std::unique_ptr<AppShimHostBootstrap> bootstrap);
// Continuation of OnShimSelectedProfile, once the profile has loaded. // Continuation of OnShimSelectedProfile, once the profile has loaded.
void OnShimSelectedProfileAndAppLoaded(const web_app::AppId& app_id, void OnShimSelectedProfileAndAppLoaded(const web_app::AppId& app_id,
...@@ -270,20 +282,21 @@ class AppShimManager : public AppShimHostBootstrap::Client, ...@@ -270,20 +282,21 @@ class AppShimManager : public AppShimHostBootstrap::Client,
// Load the specified profile and extension, and run |callback| with // Load the specified profile and extension, and run |callback| with
// the result. The callback's arguments may be nullptr on failure. // the result. The callback's arguments may be nullptr on failure.
using LoadProfileAppCallback = base::OnceCallback<void(Profile*)>; using LoadProfileAndAppCallback = base::OnceCallback<void(Profile*)>;
void LoadProfileAndApp(const base::FilePath& profile_path, void LoadProfileAndApp(const base::FilePath& profile_path,
const web_app::AppId& app_id, const web_app::AppId& app_id,
LoadProfileAppCallback callback); LoadProfileAndAppCallback callback);
void OnProfileLoaded(const base::FilePath& profile_path, void LoadProfileAndApp_OnProfileLoaded(const base::FilePath& profile_path,
const web_app::AppId& app_id, const web_app::AppId& app_id,
LoadProfileAppCallback callback, LoadProfileAndAppCallback callback,
Profile* profile); Profile* profile);
void OnProfileAppRegistryReady(const base::FilePath& profile_path, void LoadProfileAndApp_OnProfileAppRegistryReady(
const web_app::AppId& app_id, const base::FilePath& profile_path,
LoadProfileAppCallback callback); const web_app::AppId& app_id,
void OnAppEnabled(const base::FilePath& profile_path, LoadProfileAndAppCallback callback);
const web_app::AppId& app_id, void LoadProfileAndApp_OnAppEnabled(const base::FilePath& profile_path,
LoadProfileAppCallback callback); const web_app::AppId& app_id,
LoadProfileAndAppCallback callback);
// Update the profiles menu for the specified host. // Update the profiles menu for the specified host.
void UpdateAppProfileMenu(AppState* app_state); void UpdateAppProfileMenu(AppState* app_state);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment