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
|
// Copyright 2023 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/arc/session/arc_vm_data_migration_necessity_checker.h"
#include <string>
#include <vector>
#include "base/feature_list.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
#include "chromeos/ash/components/cryptohome/cryptohome_parameters.h"
#include "chromeos/ash/components/dbus/arc/arcvm_data_migrator_client.h"
#include "chromeos/ash/components/dbus/arcvm_data_migrator/arcvm_data_migrator.pb.h"
#include "chromeos/ash/experiences/arc/arc_features.h"
#include "chromeos/ash/experiences/arc/arc_prefs.h"
#include "chromeos/ash/experiences/arc/arc_util.h"
#include "chromeos/ash/experiences/arc/session/arc_vm_client_adapter.h"
#include "components/account_id/account_id.h"
namespace arc {
namespace {
std::string GetChromeOsUser(Profile* profile) {
const AccountId account(multi_user_util::GetAccountIdFromProfile(profile));
return cryptohome::CreateAccountIdentifierFromAccountId(account).account_id();
}
} // namespace
ArcVmDataMigrationNecessityChecker::ArcVmDataMigrationNecessityChecker(
Profile* profile)
: profile_(profile) {
DCHECK(profile_);
}
ArcVmDataMigrationNecessityChecker::~ArcVmDataMigrationNecessityChecker() =
default;
void ArcVmDataMigrationNecessityChecker::Check(CheckCallback callback) {
DCHECK(base::FeatureList::IsEnabled(kEnableArcVmDataMigration));
if (base::FeatureList::IsEnabled(kEnableVirtioBlkForData)) {
// No migration needs to be performed if virtio-blk /data is forciblly
// enabled via a feature.
std::move(callback).Run(false);
return;
}
if (GetArcVmDataMigrationStatus(profile_->GetPrefs()) ==
ArcVmDataMigrationStatus::kFinished) {
// No migration needs to be performed if it's already finished.
std::move(callback).Run(false);
return;
}
std::vector<std::string> environment = {"CHROMEOS_USER=" +
GetChromeOsUser(profile_)};
std::deque<JobDesc> jobs{JobDesc{kArcVmDataMigratorJobName,
UpstartOperation::JOB_STOP_AND_START,
std::move(environment)}};
ConfigureUpstartJobs(
std::move(jobs),
base::BindOnce(
&ArcVmDataMigrationNecessityChecker::OnArcVmDataMigratorStarted,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
void ArcVmDataMigrationNecessityChecker::OnArcVmDataMigratorStarted(
CheckCallback callback,
bool result) {
if (!result) {
LOG(ERROR) << "Failed to start arcvm-data-migrator";
std::move(callback).Run(std::nullopt);
return;
}
DCHECK(ash::ArcVmDataMigratorClient::Get());
data_migrator::HasDataToMigrateRequest request;
request.set_username(GetChromeOsUser(profile_));
ash::ArcVmDataMigratorClient::Get()->HasDataToMigrate(
request,
base::BindOnce(
&ArcVmDataMigrationNecessityChecker::OnHasDataToMigrateResponse,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
void ArcVmDataMigrationNecessityChecker::OnHasDataToMigrateResponse(
CheckCallback callback,
std::optional<bool> response) {
if (!response.has_value()) {
LOG(ERROR) << "Failed to check whether /data has any content: "
<< "No valid D-Bus response";
}
std::move(callback).Run(response);
}
} // namespace arc
|