1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/ash/desks/desks_templates_app_launch_handler.h"
#include <string>
#include "ash/public/cpp/desk_template.h"
#include "ash/wm/desks/desks_controller.h"
#include "base/metrics/histogram_macros.h"
#include "base/notreached.h"
#include "base/numerics/safe_conversions.h"
#include "chrome/browser/apps/app_service/app_service_proxy.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/ash/app_restore/app_launch_handler.h"
#include "chrome/browser/ash/app_restore/app_restore_arc_task_handler.h"
#include "chrome/browser/ash/app_restore/app_restore_arc_task_handler_factory.h"
#include "chrome/browser/ash/app_restore/arc_app_queue_restore_handler.h"
#include "chrome/browser/ash/system_web_apps/system_web_app_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/ash/desks/chrome_desks_util.h"
#include "chrome/browser/ui/ash/desks/desks_client.h"
#include "chrome/browser/ui/ash/shelf/chrome_shelf_controller_util.h"
#include "chrome/browser/ui/ash/system_web_apps/system_web_app_ui_utils.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_group_model.h"
#include "components/app_constants/constants.h"
#include "components/app_restore/app_restore_data.h"
#include "components/app_restore/app_restore_utils.h"
#include "components/app_restore/desk_template_read_handler.h"
#include "components/app_restore/restore_data.h"
#include "components/app_restore/window_info.h"
#include "components/services/app_service/public/cpp/app_types.h"
#include "components/services/app_service/public/cpp/types_util.h"
#include "components/tab_groups/tab_group_info.h"
#include "extensions/common/extension.h"
namespace {
// Used to generate unique IDs for desk launches.
int32_t g_launch_id = 0;
// Returns the browser app name if it's an app type browser. Returns an empty
// string otherwise.
std::string GetBrowserAppName(
const std::unique_ptr<app_restore::AppRestoreData>& app_restore_data,
const std::string& app_id) {
const bool app_type_browser =
app_restore_data->browser_extra_info.app_type_browser.value_or(false);
if (!app_type_browser) {
return std::string();
}
const std::optional<std::string>& maybe_app_name =
app_restore_data->browser_extra_info.app_name;
return maybe_app_name.has_value() && !maybe_app_name.value().empty()
? maybe_app_name.value()
: app_id;
}
} // namespace
DesksTemplatesAppLaunchHandler::DesksTemplatesAppLaunchHandler(Profile* profile,
Type type)
: ash::AppLaunchHandler(profile),
type_(type),
read_handler_(app_restore::DeskTemplateReadHandler::Get()) {}
DesksTemplatesAppLaunchHandler::~DesksTemplatesAppLaunchHandler() {
if (launch_id_) {
read_handler_->ClearRestoreData(launch_id_);
if (auto* arc_task_handler =
ash::app_restore::AppRestoreArcTaskHandlerFactory::GetForProfile(
profile())) {
arc_task_handler->ClearDeskTemplateArcAppQueueRestoreHandler(launch_id_);
}
}
}
// static
int32_t DesksTemplatesAppLaunchHandler::GetNextLaunchId() {
return ++g_launch_id;
}
void DesksTemplatesAppLaunchHandler::LaunchTemplate(
const ash::DeskTemplate& desk_template,
int32_t launch_id) {
// Ensure that the handler isn't re-used.
DCHECK_EQ(launch_id_, 0);
launch_id_ = launch_id;
DCHECK(desk_template.desk_restore_data());
auto restore_data = desk_template.desk_restore_data()->Clone();
DCHECK(!restore_data->app_id_to_launch_list().empty());
// Note: Keep this until we have an OS Stepping Stone that performs the final
// Lacros cleanup. See b/380780352.
restore_data->RemoveApp(app_constants::kLacrosAppId);
read_handler_->SetRestoreData(launch_id_, restore_data->Clone());
set_restore_data(std::move(restore_data));
// Launch the different types of apps. They can be done in any order.
MaybeLaunchArcApps();
LaunchApps();
LaunchBrowsers();
}
void DesksTemplatesAppLaunchHandler::LaunchCoralGroup(
std::unique_ptr<app_restore::RestoreData> restore_data,
int32_t launch_id) {
// Ensure that the handler isn't re-used.
CHECK_EQ(launch_id_, 0);
launch_id_ = launch_id;
read_handler_->SetRestoreData(launch_id_, restore_data->Clone());
set_restore_data(std::move(restore_data));
LaunchBrowsers();
LaunchApps();
MaybeLaunchArcApps();
}
void DesksTemplatesAppLaunchHandler::RecordRestoredAppLaunch(
apps::AppTypeName app_type_name) {
// TODO: Add UMA Histogram.
NOTIMPLEMENTED();
}
bool DesksTemplatesAppLaunchHandler::ShouldLaunchSystemWebAppOrChromeApp(
const std::string& app_id,
const app_restore::RestoreData::LaunchList& launch_list) {
// Launched coral groups are intended to be done in the post-login screen. At
// this point, the assumption is that there are no apps, so we should always
// launch.
if (type_ == Type::kCoral) {
return true;
}
// Find out if the app can have multiple instances. Apps that can have
// multiple instances are:
// 1) System web apps which can open multiple windows
// 2) Non platform app type Chrome apps
// TODO(crbug.com/1239089): Investigate if we can have a way to handle moving
// single instance windows without all these heuristics.
bool is_multi_instance_window = false;
// Check the app registry cache to see if the app is a system web app.
bool is_system_web_app = false;
apps::AppServiceProxyFactory::GetForProfile(profile())
->AppRegistryCache()
.ForOneApp(app_id, [&is_system_web_app](const apps::AppUpdate& update) {
if (update.AppType() == apps::AppType::kWeb ||
update.AppType() == apps::AppType::kSystemWeb) {
is_system_web_app = true;
}
});
// A SWA can handle multiple instances if it can open multiple windows.
if (is_system_web_app) {
std::optional<ash::SystemWebAppType> swa_type =
ash::GetSystemWebAppTypeForAppId(profile(), app_id);
if (swa_type.has_value()) {
auto* swa_manager = ash::SystemWebAppManager::Get(profile());
DCHECK(swa_manager);
auto* system_app = swa_manager->GetSystemApp(*swa_type);
DCHECK(system_app);
is_multi_instance_window = system_app->ShouldShowNewWindowMenuOption();
}
} else {
// Check the extensions registry to see if the app is a platform app. No
// need to do this check if the app is a system web app.
DCHECK(!is_multi_instance_window);
const extensions::Extension* extension =
GetExtensionForAppID(app_id, profile());
is_multi_instance_window = extension && !extension->is_platform_app();
}
// Do not try sending an existing window to the active desk and launch a new
// instance.
if (is_multi_instance_window) {
return true;
}
const bool should_launch =
ash::DesksController::Get()->OnSingleInstanceAppLaunchingFromSavedDesk(
app_id, launch_list);
// Notify performance tracker that some tracked windows will be moving.
if (!should_launch) {
for (const auto& window : launch_list) {
NotifyMovedSingleInstanceApp(window.first);
}
}
return should_launch;
}
void DesksTemplatesAppLaunchHandler::OnExtensionLaunching(
const std::string& app_id) {
read_handler_->SetNextRestoreWindowIdForChromeApp(app_id);
}
base::WeakPtr<ash::AppLaunchHandler>
DesksTemplatesAppLaunchHandler::GetWeakPtrAppLaunchHandler() {
return weak_ptr_factory_.GetWeakPtr();
}
void DesksTemplatesAppLaunchHandler::LaunchBrowsers() {
DCHECK(restore_data());
const auto& launch_list = restore_data()->app_id_to_launch_list();
for (const auto& iter : launch_list) {
const std::string& app_id = iter.first;
if (app_id != app_constants::kChromeAppId) {
continue;
}
for (const auto& window_iter : iter.second) {
const std::unique_ptr<app_restore::AppRestoreData>& app_restore_data =
window_iter.second;
const app_restore::BrowserExtraInfo browser_extra_info =
app_restore_data->browser_extra_info;
const std::vector<GURL>& urls = browser_extra_info.urls;
if (urls.empty()) {
continue;
}
const gfx::Rect current_bounds =
app_restore_data->window_info.current_bounds.value_or(gfx::Rect());
const std::string app_name = GetBrowserAppName(app_restore_data, app_id);
if (!app_name.empty() && !IsBrowserAppInstalled(app_name)) {
continue;
}
Browser::CreateParams create_params =
!app_name.empty()
? Browser::CreateParams::CreateForApp(app_name,
/*trusted_source=*/true,
current_bounds, profile(),
/*user_gesture=*/false)
: Browser::CreateParams(Browser::TYPE_NORMAL, profile(),
/*user_gesture=*/false);
create_params.restore_id = window_iter.first;
create_params.creation_source = Browser::CreationSource::kDeskTemplate;
const std::optional<chromeos::WindowStateType>& window_state_type =
app_restore_data->window_info.window_state_type;
if (window_state_type) {
create_params.initial_show_state =
chromeos::ToWindowShowState(*window_state_type);
}
if (!current_bounds.IsEmpty()) {
create_params.initial_bounds = current_bounds;
}
if (type_ == Type::kCoral) {
create_params.should_trigger_session_restore = false;
}
Browser* browser = Browser::Create(create_params);
std::optional<int32_t> active_tab_index =
browser_extra_info.active_tab_index;
for (size_t i = 0; i < urls.size(); i++) {
chrome::AddTabAt(browser, urls[i], /*index=*/-1,
/*foreground=*/
(active_tab_index &&
base::checked_cast<int32_t>(i) == *active_tab_index));
}
if (!browser_extra_info.tab_group_infos.empty()) {
chrome_desks_util::AttachTabGroupsToBrowserInstance(
browser_extra_info.tab_group_infos, browser);
}
if (browser_extra_info.first_non_pinned_tab_index.has_value() &&
browser_extra_info.first_non_pinned_tab_index.value() <=
static_cast<int>(urls.size())) {
chrome_desks_util::SetBrowserPinnedTabs(
browser_extra_info.first_non_pinned_tab_index.value(), browser);
}
// We need to handle minimized windows separately since unlike other
// window types, it's not shown.
if (window_state_type &&
*window_state_type == chromeos::WindowStateType::kMinimized) {
browser->window()->Minimize();
continue;
}
browser->window()->ShowInactive();
}
}
restore_data()->RemoveApp(app_constants::kChromeAppId);
}
void DesksTemplatesAppLaunchHandler::MaybeLaunchArcApps() {
apps::AppRegistryCache& cache =
apps::AppServiceProxyFactory::GetForProfile(profile())
->AppRegistryCache();
const auto& app_id_to_launch_list = restore_data()->app_id_to_launch_list();
// Add the ready ARC apps in `app_id_to_launch_list` to `app_ids`.
std::set<std::string> app_ids;
cache.ForEachApp(
[&app_ids, &app_id_to_launch_list](const apps::AppUpdate& update) {
if (update.Readiness() == apps::Readiness::kReady &&
update.AppType() == apps::AppType::kArc &&
base::Contains(app_id_to_launch_list, update.AppId())) {
app_ids.insert(update.AppId());
}
});
// For each ARC app, check and see if there is an existing instance. We will
// move this instance over instead of launching a new one. Remove the app
// from the restore data if it was successfully moved so that the ARC launch
// handler does not try to launch it later.
if (type_ == Type::kTemplate) {
for (const std::string& app_id : app_ids) {
auto it = app_id_to_launch_list.find(app_id);
DCHECK(it != app_id_to_launch_list.end());
if (!ash::DesksController::Get()
->OnSingleInstanceAppLaunchingFromSavedDesk(app_id,
it->second)) {
for (auto& window : it->second) {
NotifyMovedSingleInstanceApp(window.first);
}
restore_data()->RemoveApp(app_id);
}
}
}
auto* arc_task_handler =
ash::app_restore::AppRestoreArcTaskHandlerFactory::GetForProfile(
profile());
if (!arc_task_handler) {
return;
}
if (auto* launch_handler =
arc_task_handler->GetDeskTemplateArcAppQueueRestoreHandler(
launch_id_)) {
launch_handler->set_desk_template_launch_id(launch_id_);
launch_handler->RestoreArcApps(this);
}
}
void DesksTemplatesAppLaunchHandler::NotifyMovedSingleInstanceApp(
int32_t window_id) {
CHECK_EQ(Type::kTemplate, type_);
DesksClient::Get()->NotifyMovedSingleInstanceApp(window_id);
}
bool DesksTemplatesAppLaunchHandler::IsBrowserAppInstalled(
const std::string& app_name) {
apps::AppRegistryCache& cache =
apps::AppServiceProxyFactory::GetForProfile(profile())
->AppRegistryCache();
std::string app_id = app_restore::GetAppIdFromAppName(app_name);
bool result = false;
cache.ForOneApp(app_id, [&result](const apps::AppUpdate& update) {
if (apps_util::IsInstalled(update.Readiness()) &&
update.AppType() != apps::AppType::kUnknown) {
result = true;
}
});
return result;
}
|