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
|
// 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/web_applications/preinstalled_web_app_config_utils.h"
#include <optional>
#include "base/auto_reset.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "base/no_destructor.h"
#include "base/path_service.h"
#include "build/build_config.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "ash/constants/ash_switches.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#endif // BUILDFLAG(IS_CHROMEOS)
namespace web_app {
namespace {
std::optional<base::FilePath>&
GetPreinstalledWebAppConfigDirMutableForTesting() {
static base::NoDestructor<std::optional<base::FilePath>>
g_config_dir_for_testing(std::nullopt);
return *g_config_dir_for_testing.get();
}
#if BUILDFLAG(IS_CHROMEOS)
// The sub-directory of the extensions directory in which to scan for external
// web apps (as opposed to external extensions or external ARC apps).
const base::FilePath::CharType kWebAppsSubDirectory[] =
FILE_PATH_LITERAL("web_apps");
#endif // BUILDFLAG(IS_CHROMEOS)
} // namespace
namespace test {
std::optional<base::FilePath> GetPreinstalledWebAppConfigDirForTesting() {
return GetPreinstalledWebAppConfigDirMutableForTesting();
}
base::AutoReset<std::optional<base::FilePath>>
SetPreinstalledWebAppConfigDirForTesting(const base::FilePath& config_dir) {
return {&GetPreinstalledWebAppConfigDirMutableForTesting(), // IN-TEST
config_dir};
}
} // namespace test
base::FilePath GetPreinstalledWebAppConfigDirFromCommandLine(Profile* profile) {
std::string command_line_directory =
base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
switches::kPreinstalledWebAppsDir);
if (!command_line_directory.empty())
return base::FilePath::FromUTF8Unsafe(command_line_directory);
#if BUILDFLAG(IS_CHROMEOS)
// As of mid 2018, only Chrome OS has default/external web apps, and
// chrome::DIR_STANDALONE_EXTERNAL_EXTENSIONS is only defined for OS_LINUX,
// which includes OS_CHROMEOS.
// Exclude sign-in and lock screen profiles.
if (!ash::ProfileHelper::IsUserProfile(profile)) {
return {};
}
if (test::GetPreinstalledWebAppConfigDirForTesting()) { // IN-TEST
return *test::GetPreinstalledWebAppConfigDirForTesting(); // IN-TEST
}
// For manual testing, you can change s/STANDALONE/USER/, as writing to
// "$HOME/.config/chromium/test-user/.config/chromium/External
// Extensions/web_apps" does not require root ACLs, unlike
// "/usr/share/chromium/extensions/web_apps".
base::FilePath dir;
if (base::PathService::Get(chrome::DIR_STANDALONE_EXTERNAL_EXTENSIONS,
&dir)) {
return dir.Append(kWebAppsSubDirectory);
}
LOG(ERROR) << "base::PathService::Get failed";
#endif // BUILDFLAG(IS_CHROMEOS)
return {};
}
base::FilePath GetPreinstalledWebAppExtraConfigDirFromCommandLine(
Profile* profile) {
#if BUILDFLAG(IS_CHROMEOS)
base::FilePath config_dir =
GetPreinstalledWebAppConfigDirFromCommandLine(profile);
std::string extra_config_subdir =
base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
ash::switches::kExtraWebAppsDir);
if (config_dir.empty() || extra_config_subdir.empty())
return base::FilePath();
return config_dir.AppendASCII(extra_config_subdir);
#else
return base::FilePath();
#endif // BUILDFLAG(IS_CHROMEOS)
}
base::FilePath GetPreinstalledWebAppConfigDir(Profile* profile) {
return GetPreinstalledWebAppConfigDirFromCommandLine(profile);
}
base::FilePath GetPreinstalledWebAppExtraConfigDir(Profile* profile) {
return GetPreinstalledWebAppExtraConfigDirFromCommandLine(profile);
}
} // namespace web_app
|