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 119 120 121 122 123 124 125 126 127
|
// Copyright 2024 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/ash/policy/skyvault/policy_utils.h"
#include <optional>
#include "ash/constants/ash_pref_names.h"
#include "base/check_is_test.h"
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/time/time.h"
#include "chrome/browser/ash/policy/skyvault/file_location_utils.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/pref_service.h"
namespace policy::local_user_files {
namespace {
FileSaveDestination GetDestinationForPref(Profile* profile,
const std::string& pref_name) {
DCHECK(profile);
auto* pref = profile->GetPrefs()->FindPreference(pref_name);
if (!pref || !pref->GetValue() || !pref->IsManaged()) {
return FileSaveDestination::kNotSpecified;
}
const auto value_str = pref->GetValue()->GetString();
if (!IsValidLocationString(value_str)) {
return FileSaveDestination::kNotSpecified;
}
if (value_str.find(kGoogleDrivePolicyVariableName) != std::string::npos) {
return FileSaveDestination::kGoogleDrive;
}
if (value_str.find(kOneDrivePolicyVariableName) != std::string::npos) {
return FileSaveDestination::kOneDrive;
}
return FileSaveDestination::kDownloads;
}
} // namespace
constexpr char kGoogleDrivePolicyVariableName[] = "${google_drive}";
constexpr char kOneDrivePolicyVariableName[] = "${microsoft_onedrive}";
constexpr char kMigrationDestinationGoogleDrive[] = "google_drive";
constexpr char kMigrationDestinationOneDrive[] = "microsoft_onedrive";
constexpr char kMigrationDestinationDelete[] = "delete";
bool LocalUserFilesAllowed() {
// If the flag is disabled, ignore the policy value and allow local storage.
if (!base::FeatureList::IsEnabled(features::kSkyVault)) {
return true;
}
// In tests, `g_browser_process` is null.
if (!g_browser_process || !g_browser_process->local_state()) {
CHECK_IS_TEST();
return true;
}
return g_browser_process->local_state()->GetBoolean(
prefs::kLocalUserFilesAllowed);
}
MigrationDestination GetMigrationDestination() {
if (!base::FeatureList::IsEnabled(features::kSkyVault) ||
!base::FeatureList::IsEnabled(features::kSkyVaultV2)) {
return MigrationDestination::kNotSpecified;
}
const std::string destination = g_browser_process->local_state()->GetString(
prefs::kLocalUserFilesMigrationDestination);
if (destination == kMigrationDestinationGoogleDrive) {
return MigrationDestination::kGoogleDrive;
}
if (destination == kMigrationDestinationOneDrive) {
return MigrationDestination::kOneDrive;
}
if (base::FeatureList::IsEnabled(features::kSkyVaultV3) &&
destination == kMigrationDestinationDelete) {
return MigrationDestination::kDelete;
}
return MigrationDestination::kNotSpecified;
}
bool IsCloudDestination(MigrationDestination destination) {
return destination == MigrationDestination::kGoogleDrive ||
destination == MigrationDestination::kOneDrive;
}
FileSaveDestination GetDownloadsDestination(Profile* profile) {
return GetDestinationForPref(profile, prefs::kDownloadDefaultDirectory);
}
FileSaveDestination GetScreenCaptureDestination(Profile* profile) {
return GetDestinationForPref(profile, ash::prefs::kCaptureModePolicySavePath);
}
bool DownloadToTemp(Profile* profile) {
return base::FeatureList::IsEnabled(features::kSkyVault) &&
GetDownloadsDestination(profile) == FileSaveDestination::kOneDrive;
}
base::FilePath GetMyFilesPath(Profile* profile) {
return profile->GetPath().Append("MyFiles");
}
std::optional<base::Time> GetMigrationStartTime(Profile* profile) {
if (!base::FeatureList::IsEnabled(features::kSkyVaultV3)) {
return std::nullopt;
}
PrefService* pref_service = profile->GetPrefs();
base::Time scheduled_start_time =
pref_service->GetTime(prefs::kSkyVaultMigrationScheduledStartTime);
if (scheduled_start_time.is_null()) {
LOG(ERROR) << "Migration/deletion start time cannot be determined.";
return std::nullopt;
}
return scheduled_start_time;
}
} // namespace policy::local_user_files
|