| // Copyright 2018 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #include "chrome/browser/web_applications/components/web_app_utils.h" |
| |
| #include "base/files/file_path.h" |
| #include "build/chromeos_buildflags.h" |
| #include "chrome/browser/profiles/profile.h" |
| #include "chrome/common/chrome_constants.h" |
| #include "components/site_engagement/content/site_engagement_service.h" |
| |
| #if BUILDFLAG(IS_CHROMEOS_ASH) |
| #include "base/feature_list.h" |
| #include "chrome/browser/ash/profiles/profile_helper.h" |
| #include "chrome/common/chrome_features.h" |
| #include "components/user_manager/user_manager.h" |
| #endif // BUILDFLAG(IS_CHROMEOS_ASH) |
| |
| namespace web_app { |
| |
| constexpr base::FilePath::CharType kManifestResourcesDirectoryName[] = |
| FILE_PATH_LITERAL("Manifest Resources"); |
| |
| constexpr base::FilePath::CharType kTempDirectoryName[] = |
| FILE_PATH_LITERAL("Temp"); |
| |
| bool AreWebAppsEnabled(const Profile* profile) { |
| if (!profile || profile->IsSystemProfile()) |
| return false; |
| |
| const Profile* original_profile = profile->GetOriginalProfile(); |
| DCHECK(!original_profile->IsOffTheRecord()); |
| |
| #if BUILDFLAG(IS_CHROMEOS_ASH) |
| // Web Apps should not be installed to the ChromeOS system profiles. |
| if (!chromeos::ProfileHelper::IsRegularProfile(original_profile)) { |
| return false; |
| } |
| // Disable Web Apps if running any kiosk app. |
| auto* user_manager = user_manager::UserManager::Get(); |
| if (user_manager && user_manager->IsLoggedInAsAnyKioskApp()) { |
| return false; |
| } |
| #endif // BUILDFLAG(IS_CHROMEOS_ASH) |
| |
| return true; |
| } |
| |
| bool AreWebAppsUserInstallable(Profile* profile) { |
| #if BUILDFLAG(IS_CHROMEOS_ASH) |
| // With Lacros, web apps are not installed using the Ash browser. |
| if (base::FeatureList::IsEnabled(features::kWebAppsCrosapi)) |
| return false; |
| #endif |
| return AreWebAppsEnabled(profile) && !profile->IsGuestSession() && |
| !profile->IsEphemeralGuestProfile() && !profile->IsOffTheRecord(); |
| } |
| |
| content::BrowserContext* GetBrowserContextForWebApps( |
| content::BrowserContext* context) { |
| // Use original profile to create only one KeyedService instance. |
| Profile* original_profile = |
| Profile::FromBrowserContext(context)->GetOriginalProfile(); |
| return AreWebAppsEnabled(original_profile) ? original_profile : nullptr; |
| } |
| |
| content::BrowserContext* GetBrowserContextForWebAppMetrics( |
| content::BrowserContext* context) { |
| // Use original profile to create only one KeyedService instance. |
| Profile* original_profile = |
| Profile::FromBrowserContext(context)->GetOriginalProfile(); |
| const bool is_web_app_metrics_enabled = |
| site_engagement::SiteEngagementService::IsEnabled() && |
| AreWebAppsEnabled(original_profile) && |
| !original_profile->IsGuestSession() && |
| !original_profile->IsEphemeralGuestProfile(); |
| return is_web_app_metrics_enabled ? original_profile : nullptr; |
| } |
| |
| base::FilePath GetWebAppsRootDirectory(Profile* profile) { |
| return profile->GetPath().Append(chrome::kWebAppDirname); |
| } |
| |
| base::FilePath GetManifestResourcesDirectory( |
| const base::FilePath& web_apps_root_directory) { |
| return web_apps_root_directory.Append(kManifestResourcesDirectoryName); |
| } |
| |
| base::FilePath GetManifestResourcesDirectory(Profile* profile) { |
| return GetManifestResourcesDirectory(GetWebAppsRootDirectory(profile)); |
| } |
| |
| base::FilePath GetManifestResourcesDirectoryForApp( |
| const base::FilePath& web_apps_root_directory, |
| const AppId& app_id) { |
| return GetManifestResourcesDirectory(web_apps_root_directory) |
| .AppendASCII(app_id); |
| } |
| |
| base::FilePath GetWebAppsTempDirectory( |
| const base::FilePath& web_apps_root_directory) { |
| return web_apps_root_directory.Append(kTempDirectoryName); |
| } |
| |
| std::string GetProfileCategoryForLogging(Profile* profile) { |
| #if BUILDFLAG(IS_CHROMEOS_ASH) |
| if (!chromeos::ProfileHelper::IsRegularProfile(profile)) { |
| return "SigninOrLockScreen"; |
| } else if (user_manager::UserManager::Get()->IsLoggedInAsAnyKioskApp()) { |
| return "Kiosk"; |
| } else if (chromeos::ProfileHelper::IsEphemeralUserProfile(profile)) { |
| return "Ephemeral"; |
| } else if (chromeos::ProfileHelper::IsPrimaryProfile(profile)) { |
| return "Primary"; |
| } else { |
| return "Other"; |
| } |
| #else |
| // Chrome OS profiles are different from non-ChromeOS ones. Because System Web |
| // Apps are not installed on non Chrome OS, "Other" is returned here. |
| return "Other"; |
| #endif |
| } |
| |
| bool IsChromeOs() { |
| #if BUILDFLAG(IS_CHROMEOS_ASH) |
| return true; |
| #else |
| return false; |
| #endif |
| } |
| |
| } // namespace web_app |