Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FilesTrash] Extract common validation logic for .trashinfo files
The validation of .trashinfo files will be shared between the fileManagerPrivate API and the RestoreIOTask. This extracts the centralises the validation logic into a single class that also manages the launching and lifetime of the TrashInfoParser. Bug: b:238946031 Test: unit_tests --gtest_filter=*RestoreIOTask* Change-Id: I87ff2e963de199720a3e69034a543801a14e0761 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3818750 Commit-Queue: Ben Reich <benreich@chromium.org> Reviewed-by: Luciano Pacheco <lucmult@chromium.org> Cr-Commit-Position: refs/heads/main@{#1033299}
- Loading branch information
Showing
7 changed files
with
288 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
chrome/browser/ash/file_manager/trash_info_validator.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
// Copyright 2022 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/ash/file_manager/trash_info_validator.h" | ||
|
||
#include "base/files/file.h" | ||
#include "base/files/file_util.h" | ||
#include "base/strings/string_piece.h" | ||
#include "base/task/bind_post_task.h" | ||
#include "base/task/task_traits.h" | ||
#include "base/task/thread_pool.h" | ||
#include "base/threading/sequenced_task_runner_handle.h" | ||
|
||
class Profile; | ||
|
||
namespace file_manager { | ||
|
||
namespace { | ||
|
||
void RunCallbackWithError(base::File::Error error, | ||
ValidateAndParseTrashInfoCallback callback) { | ||
std::move(callback).Run(base::FileErrorOr<ParsedTrashInfoData>(error)); | ||
} | ||
|
||
} // namespace | ||
|
||
TrashInfoValidator::TrashInfoValidator(Profile* profile, | ||
const base::FilePath& base_path) { | ||
enabled_trash_locations_ = | ||
io_task::GenerateEnabledTrashLocationsForProfile(profile, base_path); | ||
|
||
parser_ = std::make_unique<chromeos::trash_service::TrashInfoParser>(); | ||
} | ||
|
||
TrashInfoValidator::~TrashInfoValidator() = default; | ||
|
||
void TrashInfoValidator::SetDisconnectHandler( | ||
base::OnceCallback<void()> disconnect_callback) { | ||
DCHECK(parser_) << "Parser should not be null here"; | ||
if (parser_) { | ||
parser_->SetDisconnectHandler(std::move(disconnect_callback)); | ||
} | ||
} | ||
|
||
void TrashInfoValidator::ValidateAndParseTrashInfo( | ||
const base::FilePath& trash_info_path, | ||
ValidateAndParseTrashInfoCallback callback) { | ||
// Validates the supplied file ends in a .trashinfo extension. | ||
if (trash_info_path.FinalExtension() != io_task::kTrashInfoExtension) { | ||
RunCallbackWithError(base::File::FILE_ERROR_INVALID_URL, | ||
std::move(callback)); | ||
return; | ||
} | ||
|
||
// Validate the .trashinfo file belongs in an enabled trash location. | ||
base::FilePath trash_folder_location; | ||
base::FilePath mount_point_path; | ||
for (const auto& [parent_path, info] : enabled_trash_locations_) { | ||
if (parent_path.Append(info.relative_folder_path) | ||
.IsParent(trash_info_path)) { | ||
trash_folder_location = parent_path.Append(info.relative_folder_path); | ||
mount_point_path = info.mount_point_path; | ||
break; | ||
} | ||
} | ||
|
||
if (mount_point_path.empty() || trash_folder_location.empty()) { | ||
RunCallbackWithError(base::File::FILE_ERROR_INVALID_OPERATION, | ||
std::move(callback)); | ||
return; | ||
} | ||
|
||
// Ensure the corresponding file that this metadata file refers to actually | ||
// exists. | ||
base::FilePath trashed_file_location = | ||
trash_folder_location.Append(io_task::kFilesFolderName) | ||
.Append(trash_info_path.BaseName().RemoveFinalExtension()); | ||
|
||
base::ThreadPool::PostTaskAndReplyWithResult( | ||
FROM_HERE, {base::MayBlock()}, | ||
base::BindOnce(&base::PathExists, trashed_file_location), | ||
base::BindOnce(&TrashInfoValidator::OnTrashedFileExists, | ||
weak_ptr_factory_.GetWeakPtr(), mount_point_path, | ||
trashed_file_location, std::move(trash_info_path), | ||
std::move(callback))); | ||
} | ||
|
||
void TrashInfoValidator::OnTrashedFileExists( | ||
const base::FilePath& mount_point_path, | ||
const base::FilePath& trashed_file_location, | ||
const base::FilePath& trash_info_path, | ||
ValidateAndParseTrashInfoCallback callback, | ||
bool exists) { | ||
if (!exists) { | ||
RunCallbackWithError(base::File::FILE_ERROR_NOT_FOUND, std::move(callback)); | ||
return; | ||
} | ||
|
||
auto complete_callback = base::BindPostTask( | ||
base::SequencedTaskRunnerHandle::Get(), | ||
base::BindOnce(&TrashInfoValidator::OnTrashInfoParsed, | ||
weak_ptr_factory_.GetWeakPtr(), mount_point_path, | ||
trashed_file_location, std::move(callback))); | ||
|
||
parser_->ParseTrashInfoFile(std::move(trash_info_path), | ||
std::move(complete_callback)); | ||
} | ||
|
||
void TrashInfoValidator::OnTrashInfoParsed( | ||
const base::FilePath& mount_point_path, | ||
const base::FilePath& trashed_file_location, | ||
ValidateAndParseTrashInfoCallback callback, | ||
base::File::Error status, | ||
const base::FilePath& restore_path, | ||
base::Time deletion_date) { | ||
if (status != base::File::FILE_OK) { | ||
RunCallbackWithError(status, std::move(callback)); | ||
return; | ||
} | ||
|
||
// The restore path that was parsed could be empty or not have a leading "/". | ||
if (restore_path.empty() || | ||
restore_path.value()[0] != base::FilePath::kSeparators[0]) { | ||
RunCallbackWithError(base::File::FILE_ERROR_INVALID_URL, | ||
std::move(callback)); | ||
return; | ||
} | ||
|
||
// Remove the leading "/" character to make the restore path relative from the | ||
// known trash parent path. | ||
base::StringPiece relative_path = | ||
base::StringPiece(restore_path.value()).substr(1); | ||
base::FilePath absolute_restore_path = mount_point_path.Append(relative_path); | ||
|
||
ParsedTrashInfoData parsed_data = { | ||
.trashed_file_path = std::move(trashed_file_location), | ||
.absolute_restore_path = std::move(absolute_restore_path), | ||
.deletion_date = std::move(deletion_date), | ||
}; | ||
|
||
std::move(callback).Run( | ||
base::FileErrorOr<ParsedTrashInfoData>(std::move(parsed_data))); | ||
} | ||
|
||
} // namespace file_manager |
Oops, something went wrong.