#include "chrome/browser/web_applications/web_app_install_utils.h"
#include <array>
#include <iterator>
#include <map>
#include <optional>
#include <ostream>
#include <set>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
#include <vector>
#include "base/check.h"
#include "base/check_op.h"
#include "base/containers/contains.h"
#include "base/containers/extend.h"
#include "base/containers/flat_map.h"
#include "base/containers/flat_set.h"
#include "base/containers/flat_tree.h"
#include "base/feature_list.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/metrics/histogram.h"
#include "base/metrics/histogram_base.h"
#include "base/metrics/histogram_functions.h"
#include "base/not_fatal_until.h"
#include "base/notreached.h"
#include "base/ranges/algorithm.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "build/buildflag.h"
#include "chrome/browser/favicon/favicon_utils.h"
#include "chrome/browser/shortcuts/shortcut_icon_generator.h"
#include "chrome/browser/ssl/chrome_security_state_tab_helper.h"
#include "chrome/browser/web_applications/os_integration/os_integration_manager.h"
#include "chrome/browser/web_applications/os_integration/web_app_file_handler_manager.h"
#include "chrome/browser/web_applications/policy/pre_redirection_url_observer.h"
#include "chrome/browser/web_applications/scope_extension_info.h"
#include "chrome/browser/web_applications/web_app.h"
#include "chrome/browser/web_applications/web_app_chromeos_data.h"
#include "chrome/browser/web_applications/web_app_constants.h"
#include "chrome/browser/web_applications/web_app_helpers.h"
#include "chrome/browser/web_applications/web_app_icon_generator.h"
#include "chrome/browser/web_applications/web_app_install_info.h"
#include "chrome/browser/web_applications/web_app_install_params.h"
#include "chrome/browser/web_applications/web_app_proto_utils.h"
#include "chrome/browser/web_applications/web_app_utils.h"
#include "chrome/common/chrome_features.h"
#include "components/services/app_service/public/cpp/file_handler.h"
#include "components/services/app_service/public/cpp/icon_info.h"
#include "components/services/app_service/public/cpp/protocol_handler_info.h"
#include "components/services/app_service/public/cpp/share_target.h"
#include "components/services/app_service/public/cpp/url_handler_info.h"
#include "components/sync/protocol/web_app_specifics.pb.h"
#include "components/webapps/browser/banners/app_banner_settings_helper.h"
#include "components/webapps/browser/installable/installable_evaluator.h"
#include "components/webapps/browser/installable/installable_manager.h"
#include "components/webapps/browser/installable/installable_metrics.h"
#include "content/public/common/content_features.h"
#include "mojo/public/cpp/bindings/struct_ptr.h"
#include "net/http/http_util.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/manifest/manifest.h"
#include "third_party/blink/public/common/permissions_policy/permissions_policy.h"
#include "third_party/blink/public/mojom/manifest/display_mode.mojom-shared.h"
#include "third_party/blink/public/mojom/manifest/manifest.mojom-shared.h"
#include "third_party/blink/public/mojom/manifest/manifest.mojom.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/gfx/geometry/size.h"
#include "url/gurl.h"
#include "url/origin.h"
#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "chrome/browser/ash/system_web_apps/types/system_web_app_data.h"
#endif
namespace web_app {
class WebAppRegistrar;
namespace {
constexpr int kMaxIcons = …;
constexpr SquareSizePx kMaxIconSize = …;
void AddSquareIconsFromMap(std::vector<SkBitmap>* square_icons,
const IconsMap& icons_map) { … }
void AddSquareIconsFromMapMatchingIconInfos(
std::vector<SkBitmap>* square_icons,
const std::vector<apps::IconInfo>& manifest_icons,
const IconsMap& icons_map) { … }
void AddSquareIconsFromBitmaps(
std::vector<SkBitmap>* square_icons,
const std::map<SquareSizePx, SkBitmap>& bitmaps) { … }
void PopulateWebAppShortcutsMenuItemInfos(
const std::vector<blink::Manifest::ShortcutItem>& shortcuts,
WebAppInstallInfo* web_app_info) { … }
std::vector<SquareSizePx> GetSquareSizePxs(
const std::map<SquareSizePx, SkBitmap>& icon_bitmaps) { … }
std::vector<SquareSizePx> GetSquareSizePxs(const IconBitmaps& icon_bitmaps,
IconPurpose purpose) { … }
std::vector<WebAppShortcutsMenuItemInfo> GetShortcutsMenuInfoWithIconSizes(
const std::vector<WebAppShortcutsMenuItemInfo>& shortcuts_menu_items,
const ShortcutsMenuIconBitmaps& shortcuts_menu_icon_bitmaps) { … }
apps::ShareTarget::Method ToAppsShareTargetMethod(
blink::mojom::ManifestShareTarget_Method method) { … }
apps::ShareTarget::Enctype ToAppsShareTargetEnctype(
blink::mojom::ManifestShareTarget_Enctype enctype) { … }
std::optional<apps::ShareTarget> ToWebAppShareTarget(
const std::optional<blink::Manifest::ShareTarget>& share_target) { … }
apps::UrlHandlers ToWebAppUrlHandlers(
const std::vector<blink::mojom::ManifestUrlHandlerPtr>& url_handlers) { … }
ScopeExtensions ToWebAppScopeExtensions(
const std::vector<blink::mojom::ManifestScopeExtensionPtr>&
scope_extensions) { … }
std::vector<apps::ProtocolHandlerInfo> ToWebAppProtocolHandlers(
const std::vector<blink::mojom::ManifestProtocolHandlerPtr>&
manifest_protocol_handlers) { … }
void PopulateShortcutItemIcons(WebAppInstallInfo* web_app_info,
const IconsMap& icons_map) { … }
void PopulateFileHandlingIcons(WebAppInstallInfo* web_app_info,
const IconsMap& icons_map,
IconsMap& other_icon_bitmaps) { … }
void PopulateHomeTabIcons(WebAppInstallInfo* web_app_info,
const IconsMap& icons_map,
IconsMap& other_icon_bitmaps) { … }
apps::FileHandler::LaunchType ToFileHandlerLaunchType(
blink::mojom::ManifestFileHandler::LaunchType launch_type) { … }
base::flat_map<std::string, blink::Manifest::TranslationItem>
ToWebAppTranslations(
const base::flat_map<std::u16string, blink::Manifest::TranslationItem>&
manifest_translations) { … }
}
void PopulateFileHandlerInfoFromManifest(
const std::vector<blink::mojom::ManifestFileHandlerPtr>&
manifest_file_handlers,
const GURL& app_scope,
WebAppInstallInfo* web_app_info) { … }
void UpdateWebAppInstallInfoIconsFromManifestIfNeeded(
const std::vector<blink::Manifest::ImageResource> icons,
WebAppInstallInfo* web_app_info) { … }
void PopulateHomeTabIconsFromHomeTabManifestParams(
WebAppInstallInfo* web_app_info) { … }
void UpdateWebAppInfoFromManifest(const blink::mojom::Manifest& manifest,
WebAppInstallInfo* web_app_info) { … }
WebAppInstallInfo CreateWebAppInfoFromManifest(
const blink::mojom::Manifest& manifest) { … }
void PopulateOtherIcons(WebAppInstallInfo* web_app_info,
const IconsMap& icons_map) { … }
void PopulateProductIcons(WebAppInstallInfo* web_app_info,
const IconsMap* icons_map) { … }
void RecordDownloadedIconsResultAndHttpStatusCodes(
IconsDownloadedResult result,
const DownloadedIconsHttpResults& icons_http_results) { … }
void RecordDownloadedIconsHttpResultsCodeClass(
std::string_view histogram_name,
IconsDownloadedResult result,
const DownloadedIconsHttpResults& icons_http_results) { … }
void RecordDownloadedIconHttpStatusCodes(
std::string_view histogram_name,
const DownloadedIconsHttpResults& icons_http_results) { … }
WebAppManagement::Type ConvertExternalInstallSourceToSource(
ExternalInstallSource external_install_source) { … }
webapps::WebappInstallSource ConvertExternalInstallSourceToInstallSource(
ExternalInstallSource external_install_source) { … }
webapps::WebappUninstallSource ConvertExternalInstallSourceToUninstallSource(
ExternalInstallSource external_install_source) { … }
WebAppManagement::Type ConvertInstallSurfaceToWebAppSource(
webapps::WebappInstallSource install_source) { … }
void CreateWebAppInstallTabHelpers(content::WebContents* web_contents) { … }
void SetWebAppManifestFields(const WebAppInstallInfo& web_app_info,
WebApp& web_app,
bool skip_icons_on_download_failure) { … }
void SetWebAppProductIconFields(const WebAppInstallInfo& web_app_info,
WebApp& web_app) { … }
bool CanWebAppUpdateIdentity(const WebApp* web_app) { … }
void ApplyParamsToWebAppInstallInfo(const WebAppInstallParams& install_params,
WebAppInstallInfo& web_app_info) { … }
void ApplyParamsToFinalizeOptions(
const WebAppInstallParams& install_params,
WebAppInstallFinalizer::FinalizeOptions& options) { … }
bool HomeTabIconsExistInTabStrip(const WebAppInstallInfo& web_app_info) { … }
}