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 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
|
// Copyright 2023 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/apps/app_service/app_install/app_install_service_ash.h"
#include <algorithm>
#include <variant>
#include "ash/constants/ash_features.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "chrome/browser/apps/app_service/app_install/app_install.pb.h"
#include "chrome/browser/apps/app_service/app_install/app_install_discovery_metrics.h"
#include "chrome/browser/apps/app_service/app_install/app_install_types.h"
#include "chrome/browser/apps/app_service/launch_utils.h"
#include "chrome/browser/ash/borealis/borealis_game_install_flow.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/webui/ash/app_install/app_install.mojom.h"
// TODO(crbug.com/40283709): Remove circular dependency.
#include "chrome/browser/ui/webui/ash/app_install/app_install_dialog.h" // nogncheck
#include "chrome/browser/ui/webui/ash/app_install/app_install_page_handler.h" // nogncheck
#include "chromeos/ash/components/browser_context_helper/browser_context_types.h"
#include "chromeos/constants/chromeos_features.h"
#include "components/services/app_service/public/cpp/app_registry_cache.h"
#include "components/services/app_service/public/cpp/app_types.h"
#include "components/services/app_service/public/cpp/package_id.h"
#include "components/services/app_service/public/cpp/types_util.h"
#include "components/user_manager/user_manager.h"
#include "net/base/url_util.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "ui/gfx/native_widget_types.h"
namespace apps {
namespace {
std::optional<QueryError::Type> VerifyAppInstallData(
const base::expected<AppInstallData, QueryError>& data,
const PackageId& expected_package_id) {
if (data.has_value()) {
if (data->package_id != expected_package_id) {
return QueryError::kBadResponse;
}
if (!data->IsValidForInstallation()) {
return QueryError::kBadResponse;
}
return std::nullopt;
}
return data.error().type;
}
AppInstallResult AppInstallResultFromQueryError(
QueryError::Type query_error_type) {
switch (query_error_type) {
case QueryError::kConnectionError:
return AppInstallResult::kAlmanacFetchFailed;
case QueryError::kBadRequest:
return AppInstallResult::kBadAppRequest;
case QueryError::kBadResponse:
return AppInstallResult::kAppDataCorrupted;
}
}
void RecordInstallResult(base::OnceClosure callback,
AppInstallSurface surface,
AppInstallResult result) {
base::UmaHistogramEnumeration("Apps.AppInstallService.AppInstallResult",
result);
base::UmaHistogramEnumeration(
base::StrCat({"Apps.AppInstallService.AppInstallResult.",
base::ToString(surface)}),
result);
std::move(callback).Run();
}
} // namespace
base::OnceCallback<void(PackageId)>&
AppInstallServiceAsh::InstallAppCallbackForTesting() {
static base::NoDestructor<base::OnceCallback<void(PackageId)>> callback;
return *callback;
}
AppInstallServiceAsh::AppInstallServiceAsh(Profile& profile)
: profile_(profile),
arc_app_installer_(&*profile_),
web_app_installer_(&*profile_) {}
AppInstallServiceAsh::~AppInstallServiceAsh() = default;
void AppInstallServiceAsh::InstallAppWithFallback(
AppInstallSurface surface,
std::string serialized_package_id,
std::optional<WindowIdentifier> anchor_window,
base::OnceClosure callback) {
if (std::optional<PackageId> package_id =
PackageId::FromString(serialized_package_id)) {
InstallApp(surface, std::move(package_id).value(), anchor_window,
std::move(callback));
return;
}
base::OnceCallback<void(AppInstallResult)> result_callback =
base::BindOnce(&RecordInstallResult, std::move(callback), surface);
FetchAppInstallUrl(
std::move(serialized_package_id),
base::BindOnce(&AppInstallServiceAsh::MaybeLaunchAppInstallUrl,
weak_ptr_factory_.GetWeakPtr(),
std::move(result_callback)));
}
void AppInstallServiceAsh::InstallApp(
AppInstallSurface surface,
PackageId package_id,
std::optional<gfx::NativeWindow> anchor_window,
base::OnceClosure callback) {
if (InstallAppCallbackForTesting()) {
std::move(InstallAppCallbackForTesting()).Run(package_id);
}
RecordAppDiscoveryMetricForInstallRequest(&profile_.get(), surface,
package_id);
base::OnceCallback<void(AppInstallResult)> result_callback =
base::BindOnce(&RecordInstallResult, std::move(callback), surface);
if (!CanUserInstall()) {
std::move(result_callback).Run(AppInstallResult::kUserTypeNotPermitted);
return;
}
switch (package_id.package_type()) {
case PackageType::kArc:
case PackageType::kGeForceNow:
case PackageType::kWeb:
case PackageType::kWebsite: {
// Observe for `anchor_window` being destroyed during async work.
std::unique_ptr<views::NativeWindowTracker> anchor_window_tracker;
if (anchor_window) {
anchor_window_tracker =
views::NativeWindowTracker::Create(*anchor_window);
}
FetchAppInstallData(
package_id,
base::BindOnce(&AppInstallServiceAsh::ShowDialogAndInstall,
weak_ptr_factory_.GetWeakPtr(), surface, package_id,
anchor_window, std::move(anchor_window_tracker),
std::move(result_callback)));
return;
}
case PackageType::kBorealis:
case PackageType::kChromeApp:
case PackageType::kSystem:
case PackageType::kUnknown:
std::move(result_callback).Run(AppInstallResult::kAppTypeNotSupported);
return;
}
}
void AppInstallServiceAsh::InstallAppHeadless(
AppInstallSurface surface,
PackageId package_id,
base::OnceCallback<void(bool success)> callback) {
FetchAppInstallData(
package_id, base::BindOnce(&AppInstallServiceAsh::PerformInstallHeadless,
weak_ptr_factory_.GetWeakPtr(), surface,
package_id, std::move(callback)));
}
void AppInstallServiceAsh::InstallAppHeadless(
AppInstallSurface surface,
AppInstallData data,
base::OnceCallback<void(bool success)> callback) {
PerformInstallHeadless(surface, data.package_id, std::move(callback), data);
}
bool AppInstallServiceAsh::CanUserInstall() const {
if (profile_->IsSystemProfile()) {
return false;
}
if (!ash::IsUserBrowserContext(&*profile_)) {
return false;
}
auto* user_manager = user_manager::UserManager::Get();
if (!user_manager) {
return false;
}
if (user_manager->IsLoggedInAsManagedGuestSession() ||
user_manager->IsLoggedInAsGuest() ||
user_manager->IsLoggedInAsAnyKioskApp()) {
return false;
}
return true;
}
void AppInstallServiceAsh::FetchAppInstallData(
PackageId package_id,
app_install_almanac_endpoint::GetAppInstallInfoCallback data_callback) {
app_install_almanac_endpoint::GetAppInstallInfo(&profile_.get(), package_id,
std::move(data_callback));
}
void AppInstallServiceAsh::PerformInstallHeadless(
AppInstallSurface surface,
PackageId expected_package_id,
base::OnceCallback<void(bool success)> callback,
base::expected<AppInstallData, QueryError> data) {
// TODO(b/327535848): Record metrics for headless installs.
if (!data.has_value()) {
std::move(callback).Run(false);
return;
}
RecordAppDiscoveryMetricForInstallRequest(&profile_.get(), surface,
expected_package_id);
PerformInstall(surface, *data, std::move(callback));
}
void AppInstallServiceAsh::ShowDialogAndInstall(
AppInstallSurface surface,
PackageId expected_package_id,
std::optional<gfx::NativeWindow> anchor_window,
std::unique_ptr<views::NativeWindowTracker> anchor_window_tracker,
base::OnceCallback<void(AppInstallResult)> callback,
base::expected<AppInstallData, QueryError> data) {
gfx::NativeWindow parent =
anchor_window.has_value() &&
!anchor_window_tracker->WasNativeWindowDestroyed()
? anchor_window.value()
: nullptr;
if (std::optional<QueryError::Type> query_error =
VerifyAppInstallData(data, expected_package_id)) {
base::WeakPtr<ash::app_install::AppInstallDialog> dialog =
ash::app_install::AppInstallDialog::CreateDialog();
switch (query_error.value()) {
case QueryError::kConnectionError:
dialog->ShowConnectionError(
parent, base::BindOnce(&AppInstallServiceAsh::InstallApp,
weak_ptr_factory_.GetWeakPtr(), surface,
expected_package_id, anchor_window,
base::DoNothing()));
break;
case QueryError::kBadRequest:
case QueryError::kBadResponse:
dialog->ShowNoAppError(parent);
break;
}
std::move(callback).Run(
AppInstallResultFromQueryError(query_error.value()));
return;
}
bool show_install_dialog =
expected_package_id.package_type() == PackageType::kWeb ||
expected_package_id.package_type() == PackageType::kWebsite;
// If we can't show the install dialog, we must have an install URL to open.
if (!show_install_dialog) {
// This is checked by VerifyAppInstallData:
CHECK(data->install_url.is_valid());
LaunchUrlInInstalledAppOrBrowser(&*profile_, data->install_url,
LaunchSource::kFromInstaller);
std::move(callback).Run(AppInstallResult::kUnknown);
return;
}
// The install dialog is only used for web apps currently.
CHECK(std::holds_alternative<WebAppInstallData>(data->app_type_data));
const WebAppInstallData& web_app_data =
std::get<WebAppInstallData>(data->app_type_data);
if (expected_package_id.package_type() == PackageType::kWebsite) {
// kWebsite packages will end up installed as a regular kWeb app. Pass a
// kWeb package ID to the Install Dialog so that it can look for the correct
// installed app.
// An alternative would be to set the installer_package_id for shortcut web
// apps as kWebsite in App Service. However, this is difficult to manage
// correctly, as the user could already have a non-shortcut web app
// installed with the same identifier.
expected_package_id =
PackageId(PackageType::kWeb, expected_package_id.identifier());
}
std::vector<ash::app_install::mojom::ScreenshotPtr> screenshots;
for (auto& screenshot : data->screenshots) {
auto dialog_screenshot = ash::app_install::mojom::Screenshot::New();
dialog_screenshot->url = screenshot.url;
dialog_screenshot->size =
gfx::Size(screenshot.width_in_pixels, screenshot.height_in_pixels);
screenshots.push_back(std::move(dialog_screenshot));
}
base::WeakPtr<ash::app_install::AppInstallDialog> dialog =
ash::app_install::AppInstallDialog::CreateDialog();
dialog->ShowApp(&*profile_, parent, expected_package_id, data->name,
web_app_data.document_url, data->description, data->icon,
std::move(screenshots),
base::BindOnce(&AppInstallServiceAsh::InstallIfDialogAccepted,
weak_ptr_factory_.GetWeakPtr(), surface,
data.value(), dialog, std::move(callback)));
}
void AppInstallServiceAsh::InstallIfDialogAccepted(
AppInstallSurface surface,
AppInstallData data,
base::WeakPtr<ash::app_install::AppInstallDialog> dialog,
base::OnceCallback<void(AppInstallResult)> callback,
bool dialog_accepted) {
if (!dialog_accepted) {
std::move(callback).Run(AppInstallResult::kInstallDialogNotAccepted);
return;
}
PerformInstall(surface, data,
base::BindOnce(&AppInstallServiceAsh::ProcessInstallResult,
weak_ptr_factory_.GetWeakPtr(), surface, data,
dialog, std::move(callback)));
}
void AppInstallServiceAsh::ProcessInstallResult(
AppInstallSurface surface,
AppInstallData data,
base::WeakPtr<ash::app_install::AppInstallDialog> dialog,
base::OnceCallback<void(AppInstallResult)> callback,
bool install_success) {
if (!dialog) {
std::move(callback).Run(install_success
? AppInstallResult::kSuccess
: AppInstallResult::kAppTypeInstallFailed);
return;
}
if (install_success) {
dialog->SetInstallSucceeded();
std::move(callback).Run(AppInstallResult::kSuccess);
return;
}
dialog->SetInstallFailed(
base::BindOnce(&AppInstallServiceAsh::InstallIfDialogAccepted,
weak_ptr_factory_.GetWeakPtr(), surface, std::move(data),
dialog, std::move(callback)));
}
void AppInstallServiceAsh::PerformInstall(
AppInstallSurface surface,
AppInstallData data,
base::OnceCallback<void(bool)> install_callback) {
if (std::holds_alternative<AndroidAppInstallData>(data.app_type_data)) {
arc_app_installer_.InstallApp(surface, std::move(data),
std::move(install_callback));
} else if (std::holds_alternative<WebAppInstallData>(data.app_type_data)) {
web_app_installer_.InstallApp(surface, std::move(data),
std::move(install_callback));
} else {
LOG(ERROR) << "Unsupported AppInstallData type";
std::move(install_callback).Run(false);
}
}
void AppInstallServiceAsh::FetchAppInstallUrl(
std::string serialized_package_id,
base::OnceCallback<void(base::expected<GURL, QueryError>)> callback) {
app_install_almanac_endpoint::GetAppInstallUrl(
&profile_.get(), serialized_package_id, std::move(callback));
}
void AppInstallServiceAsh::MaybeLaunchAppInstallUrl(
base::OnceCallback<void(AppInstallResult)> callback,
base::expected<GURL, QueryError> install_url) {
if (install_url.has_value()) {
LaunchUrlInInstalledAppOrBrowser(&*profile_, install_url.value(),
LaunchSource::kFromInstaller);
std::move(callback).Run(AppInstallResult::kInstallUrlFallback);
return;
}
base::WeakPtr<ash::app_install::AppInstallDialog> dialog =
ash::app_install::AppInstallDialog::CreateDialog();
switch (install_url.error().type) {
case QueryError::kConnectionError:
// TODO(b/339548810): Show connection error dialog instead, this needs
// the parameters necessary for a retry_callback to be plumbed through
// to here.
case QueryError::kBadRequest:
case QueryError::kBadResponse:
// TODO(b/339548810): Plumb the parent window through to here.
dialog->ShowNoAppError(/*parent=*/nullptr);
break;
}
std::move(callback).Run(
AppInstallResultFromQueryError(install_url.error().type));
}
} // namespace apps
|