| // Copyright 2022 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_preload_service/preload_app_definition.h" |
| |
| #include "base/strings/string_util.h" |
| #include "chrome/browser/apps/app_preload_service/proto/app_preload.pb.h" |
| #include "chrome/browser/web_applications/web_app_helpers.h" |
| #include "components/services/app_service/public/cpp/package_id.h" |
| #include "url/gurl.h" |
| |
| namespace apps { |
| |
| PreloadAppDefinition::PreloadAppDefinition( |
| proto::AppPreloadListResponse_App app_proto) |
| : app_proto_(app_proto), |
| package_id_(PackageId::FromString(app_proto_.package_id())) {} |
| |
| PreloadAppDefinition::PreloadAppDefinition(const PreloadAppDefinition&) = |
| default; |
| PreloadAppDefinition& PreloadAppDefinition::operator=( |
| const PreloadAppDefinition&) = default; |
| PreloadAppDefinition::~PreloadAppDefinition() = default; |
| |
| std::string PreloadAppDefinition::GetName() const { |
| return app_proto_.name(); |
| } |
| |
| AppType PreloadAppDefinition::GetPlatform() const { |
| if (package_id_.has_value()) { |
| return package_id_->app_type(); |
| } |
| return AppType::kUnknown; |
| } |
| |
| bool PreloadAppDefinition::IsDefaultApp() const { |
| return app_proto_.install_reason() == |
| proto::AppPreloadListResponse::INSTALL_REASON_DEFAULT; |
| } |
| |
| bool PreloadAppDefinition::IsOemApp() const { |
| return app_proto_.install_reason() == |
| proto::AppPreloadListResponse::INSTALL_REASON_OEM; |
| } |
| |
| bool PreloadAppDefinition::IsTestApp() const { |
| return app_proto_.install_reason() == |
| proto::AppPreloadListResponse::INSTALL_REASON_TEST; |
| } |
| |
| AppInstallSurface PreloadAppDefinition::GetInstallSurface() const { |
| return IsDefaultApp() ? AppInstallSurface::kAppPreloadServiceDefault |
| : AppInstallSurface::kAppPreloadServiceOem; |
| } |
| |
| std::string PreloadAppDefinition::GetAndroidPackageName() const { |
| DCHECK_EQ(GetPlatform(), AppType::kArc); |
| DCHECK(package_id_.has_value()); |
| |
| return package_id_->identifier(); |
| } |
| |
| GURL PreloadAppDefinition::GetWebAppManifestUrl() const { |
| DCHECK_EQ(GetPlatform(), AppType::kWeb); |
| |
| return GURL(app_proto_.web_extras().manifest_url()); |
| } |
| |
| GURL PreloadAppDefinition::GetWebAppOriginalManifestUrl() const { |
| DCHECK_EQ(GetPlatform(), AppType::kWeb); |
| |
| return GURL(app_proto_.web_extras().original_manifest_url()); |
| } |
| |
| GURL PreloadAppDefinition::GetWebAppManifestId() const { |
| DCHECK_EQ(GetPlatform(), AppType::kWeb); |
| DCHECK(package_id_.has_value()); |
| |
| return GURL(package_id_->identifier()); |
| } |
| |
| std::string PreloadAppDefinition::GetWebAppId() const { |
| DCHECK_EQ(GetPlatform(), AppType::kWeb); |
| return web_app::GenerateAppIdFromManifestId(GetWebAppManifestId()); |
| } |
| |
| AppInstallData PreloadAppDefinition::ToAppInstallData() const { |
| AppInstallData result(package_id_.value()); |
| result.name = GetName(); |
| if (GetPlatform() == AppType::kArc) { |
| // nothing. |
| } else if (GetPlatform() == AppType::kWeb) { |
| auto& web_app_data = result.app_type_data.emplace<WebAppInstallData>(); |
| web_app_data.original_manifest_url = GetWebAppOriginalManifestUrl(); |
| web_app_data.proxied_manifest_url = GetWebAppManifestUrl(); |
| web_app_data.document_url = GetWebAppManifestId().GetWithEmptyPath(); |
| } else { |
| NOTREACHED(); |
| } |
| return result; |
| } |
| |
| std::ostream& operator<<(std::ostream& os, const PreloadAppDefinition& app) { |
| os << std::boolalpha; |
| os << "- Name: " << app.GetName() << std::endl; |
| os << "- Platform: " << EnumToString(app.GetPlatform()) << std::endl; |
| os << "- OEM: " << app.IsOemApp() << std::endl; |
| os << "- Default: " << app.IsDefaultApp() << std::endl; |
| |
| if (app.GetPlatform() == AppType::kArc) { |
| os << "- Android Extras:" << std::endl; |
| os << " - Package Name: " << app.GetAndroidPackageName() << std::endl; |
| } else if (app.GetPlatform() == AppType::kWeb) { |
| os << "- Web Extras:" << std::endl; |
| os << " - Manifest URL: " << app.GetWebAppManifestUrl() << std::endl; |
| os << " - Original Manifest URL: " << app.GetWebAppOriginalManifestUrl() |
| << std::endl; |
| os << " - Manifest ID: " << app.GetWebAppManifestId() << std::endl; |
| } |
| |
| os << std::noboolalpha; |
| return os; |
| } |
| |
| } // namespace apps |