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
|
// 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/chromeos/app_mode/kiosk_web_app_install_util.h"
#include <optional>
#include <string_view>
#include <tuple>
#include <utility>
#include "base/check_op.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_functions.h"
#include "base/syslog_logging.h"
#include "chrome/browser/web_applications/external_install_options.h"
#include "chrome/browser/web_applications/mojom/user_display_mode.mojom-shared.h"
#include "chrome/browser/web_applications/web_app_constants.h"
#include "chrome/browser/web_applications/web_app_management_type.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#include "components/webapps/browser/install_result_code.h"
#include "url/gurl.h"
namespace {
// Histogram to log the installed web app is a placeholder.
constexpr std::string_view kWebAppIsPlaceholderUMA =
"Kiosk.AppService.WebApp.IsPlaceholder";
// Histogram to log the web app install result code.
constexpr std::string_view kWebAppInstallResultUMA =
"Kiosk.AppService.WebApp.InstallResult";
web_app::ExternalInstallOptions GetInstallOptions(GURL install_url) {
web_app::ExternalInstallOptions options(
install_url, web_app::mojom::UserDisplayMode::kStandalone,
web_app::ExternalInstallSource::kKiosk);
// When the install URL redirects to another URL a placeholder will be
// installed. This happens if a web app requires authentication.
options.install_placeholder = true;
return options;
}
web_app::WebAppProvider& WebAppProviderOf(Profile& profile) {
return CHECK_DEREF(web_app::WebAppProvider::GetForWebApps(&profile));
}
void OnExternalInstallCompleted(
const GURL& requested_install_url,
chromeos::InstallWebKioskCallback on_done,
const GURL& installed_url,
web_app::ExternallyManagedAppManager::InstallResult result) {
CHECK_EQ(installed_url, requested_install_url);
base::UmaHistogramEnumeration(kWebAppInstallResultUMA, result.code);
if (!webapps::IsSuccess(result.code)) {
SYSLOG(ERROR) << "Failed to install Kiosk web app, code " << result.code;
return std::move(on_done).Run(std::nullopt);
}
SYSLOG(INFO) << "Successfully installed Kiosk web app.";
std::move(on_done).Run(result.app_id);
}
} // namespace
namespace chromeos {
KioskWebAppInstallState GetKioskWebAppInstallState(Profile& profile,
const GURL& install_url) {
// If a web app `install_url` requires authentication, it will be assigned a
// temporary `app_id` which will be changed to the correct `app_id` once the
// authentication is done. The only key that is safe to be used as identifier
// for Kiosk web apps is `install_url`.
auto app_id =
WebAppProviderOf(profile).registrar_unsafe().LookUpAppIdByInstallUrl(
install_url);
if (!app_id || app_id->empty()) {
return std::make_tuple(WebKioskInstallState::kNotInstalled, std::nullopt);
}
// If the installed app is a placeholder (similar to failed installation in
// the old launcher), try to install again to replace it.
bool is_placeholder_app =
WebAppProviderOf(profile).registrar_unsafe().IsPlaceholderApp(
app_id.value(), web_app::WebAppManagement::Type::kKiosk);
base::UmaHistogramBoolean(kWebAppIsPlaceholderUMA, is_placeholder_app);
if (is_placeholder_app) {
SYSLOG(INFO) << "Placeholder app installed. Trying to reinstall...";
return std::make_tuple(WebKioskInstallState::kPlaceholderInstalled,
std::nullopt);
}
return std::make_tuple(WebKioskInstallState::kInstalled, app_id);
}
void InstallKioskWebApp(Profile& profile,
const GURL& install_url,
InstallWebKioskCallback on_done) {
WebAppProviderOf(profile).externally_managed_app_manager().Install(
GetInstallOptions(install_url),
base::BindOnce(&OnExternalInstallCompleted, install_url,
std::move(on_done)));
}
} // namespace chromeos
|