Commit 1f5967ed authored by Hesen Zhang's avatar Hesen Zhang Committed by Commit Bot

[Download-Rename] Fix rename unavailable failure in Android Q.

- Rename validation check whether original path exist in disk, in
Android Q path is using ContentUri, Failure of unavailable returned.

- Android Q uses display_name column in media storage, we should
SetDisplayName to update UI before run the callback.

- Fix a missing rename dialog UI state machine transition case.

Bug: 1007503
Change-Id: I2051d99449f39f6910a2ecd70250d6308e731917
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1834949Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Reviewed-by: default avatarShakti Sahu <shaktisahu@chromium.org>
Commit-Queue: Hesen Zhang <hesen@chromium.org>
Auto-Submit: Hesen Zhang <hesen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#702914}
parent 13b66bdf
...@@ -116,9 +116,11 @@ public class RenameDialogManager { ...@@ -116,9 +116,11 @@ public class RenameDialogManager {
private void processDialogState(@RenameDialogState int nextState, int dismissalCause) { private void processDialogState(@RenameDialogState int nextState, int dismissalCause) {
switch (nextState) { switch (nextState) {
case RenameDialogState.NO_DIALOG: case RenameDialogState.NO_DIALOG:
if (mCurState == RenameDialogState.RENAME_EXTENSION_DIALOG_DEFAULT) { if (mCurState == RenameDialogState.RENAME_EXTENSION_DIALOG_DEFAULT
|| mCurState == RenameDialogState.RENAME_EXTENSION_DIALOG_COMMIT_ERROR) {
mRenameExtensionDialogCoordinator.dismissDialog(dismissalCause); mRenameExtensionDialogCoordinator.dismissDialog(dismissalCause);
} else if (mCurState == RenameDialogState.RENAME_DIALOG_DEFAULT) { } else if (mCurState == RenameDialogState.RENAME_DIALOG_DEFAULT
|| mCurState == RenameDialogState.RENAME_DIALOG_COMMIT_ERROR) {
mRenameDialogCoordinator.dismissDialog(dismissalCause); mRenameDialogCoordinator.dismissDialog(dismissalCause);
} }
break; break;
......
...@@ -677,22 +677,33 @@ void DownloadItemImpl::ShowDownloadInShell() { ...@@ -677,22 +677,33 @@ void DownloadItemImpl::ShowDownloadInShell() {
delegate_->ShowDownloadInShell(this); delegate_->ShowDownloadInShell(this);
} }
void DownloadItemImpl::RenameDownloadedFileDone(RenameDownloadCallback callback, void DownloadItemImpl::RenameDownloadedFileDone(
const base::FilePath& new_path, RenameDownloadCallback callback,
DownloadRenameResult result) { const base::FilePath& display_name,
DownloadRenameResult result) {
if (result == DownloadRenameResult::SUCCESS) { if (result == DownloadRenameResult::SUCCESS) {
destination_info_.target_path = new_path; bool is_content_uri = false;
destination_info_.current_path = new_path; #if defined(OS_ANDROID)
is_content_uri = GetFullPath().IsContentUri();
#endif // defined(OS_ANDROID)
if (is_content_uri) {
SetDisplayName(display_name);
} else {
auto new_full_path =
base::FilePath(GetFullPath().DirName()).Append(display_name);
destination_info_.target_path = new_full_path;
destination_info_.current_path = new_full_path;
}
UpdateObservers(); UpdateObservers();
} }
std::move(callback).Run(result); std::move(callback).Run(result);
} }
void DownloadItemImpl::Rename(const base::FilePath& name, void DownloadItemImpl::Rename(const base::FilePath& display_name,
DownloadItem::RenameDownloadCallback callback) { DownloadItem::RenameDownloadCallback callback) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
if (name.IsAbsolute()) { if (display_name.IsAbsolute()) {
base::SequencedTaskRunnerHandle::Get()->PostTask( base::SequencedTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(&DownloadItemImpl::RenameDownloadedFileDone, FROM_HERE, base::BindOnce(&DownloadItemImpl::RenameDownloadedFileDone,
weak_ptr_factory_.GetWeakPtr(), weak_ptr_factory_.GetWeakPtr(),
...@@ -701,14 +712,13 @@ void DownloadItemImpl::Rename(const base::FilePath& name, ...@@ -701,14 +712,13 @@ void DownloadItemImpl::Rename(const base::FilePath& name,
return; return;
} }
auto full_path = base::FilePath(GetFullPath().DirName()).Append(name);
base::PostTaskAndReplyWithResult( base::PostTaskAndReplyWithResult(
GetDownloadTaskRunner().get(), FROM_HERE, GetDownloadTaskRunner().get(), FROM_HERE,
base::BindOnce(&download::RenameDownloadedFile, GetFullPath(), full_path), base::BindOnce(&download::RenameDownloadedFile, GetFullPath(),
display_name),
base::BindOnce(&DownloadItemImpl::RenameDownloadedFileDone, base::BindOnce(&DownloadItemImpl::RenameDownloadedFileDone,
weak_ptr_factory_.GetWeakPtr(), std::move(callback), weak_ptr_factory_.GetWeakPtr(), std::move(callback),
full_path)); display_name));
} }
uint32_t DownloadItemImpl::GetId() const { uint32_t DownloadItemImpl::GetId() const {
......
...@@ -45,6 +45,27 @@ const int64_t kInvalidFileWriteOffset = -1; ...@@ -45,6 +45,27 @@ const int64_t kInvalidFileWriteOffset = -1;
// downloads will be deleted after expiration. // downloads will be deleted after expiration.
const int kDefaultDownloadExpiredTimeInDays = 90; const int kDefaultDownloadExpiredTimeInDays = 90;
#if defined(OS_ANDROID)
// Default maximum length of a downloaded file name on Android.
const int kDefaultMaxFileNameLengthOnAndroid = 127;
DownloadItem::DownloadRenameResult RenameDownloadedFileForContentUri(
const base::FilePath& from_path,
const base::FilePath& display_name) {
if (static_cast<int>(display_name.value().length()) >
kDefaultMaxFileNameLengthOnAndroid) {
return DownloadItem::DownloadRenameResult::FAILURE_NAME_TOO_LONG;
}
if (DownloadCollectionBridge::FileNameExists(display_name))
return DownloadItem::DownloadRenameResult::FAILURE_NAME_CONFLICT;
return DownloadCollectionBridge::RenameDownloadUri(from_path, display_name)
? DownloadItem::DownloadRenameResult::SUCCESS
: DownloadItem::DownloadRenameResult::FAILURE_NAME_INVALID;
}
#endif // defined(OS_ANDROID)
void AppendExtraHeaders(net::HttpRequestHeaders* headers, void AppendExtraHeaders(net::HttpRequestHeaders* headers,
DownloadUrlParameters* params) { DownloadUrlParameters* params) {
for (const auto& header : params->request_headers()) for (const auto& header : params->request_headers())
...@@ -569,9 +590,14 @@ bool DeleteDownloadedFile(const base::FilePath& path) { ...@@ -569,9 +590,14 @@ bool DeleteDownloadedFile(const base::FilePath& path) {
return base::DeleteFile(path, false); return base::DeleteFile(path, false);
} }
download::DownloadItem::DownloadRenameResult RenameDownloadedFile( DownloadItem::DownloadRenameResult RenameDownloadedFile(
const base::FilePath& from_path, const base::FilePath& from_path,
const base::FilePath& to_path) { const base::FilePath& display_name) {
#if defined(OS_ANDROID)
if (from_path.IsContentUri())
return RenameDownloadedFileForContentUri(from_path, display_name);
#endif // defined(OS_ANDROID)
auto to_path = base::FilePath(from_path.DirName()).Append(display_name);
if (!base::PathExists(from_path) || if (!base::PathExists(from_path) ||
!base::DirectoryExists(from_path.DirName())) !base::DirectoryExists(from_path.DirName()))
return DownloadItem::DownloadRenameResult::FAILURE_UNAVAILABLE; return DownloadItem::DownloadRenameResult::FAILURE_UNAVAILABLE;
...@@ -590,20 +616,9 @@ download::DownloadItem::DownloadRenameResult RenameDownloadedFile( ...@@ -590,20 +616,9 @@ download::DownloadItem::DownloadRenameResult RenameDownloadedFile(
return DownloadItem::DownloadRenameResult::FAILURE_NAME_TOO_LONG; return DownloadItem::DownloadRenameResult::FAILURE_NAME_TOO_LONG;
} }
} }
#if defined(OS_ANDROID)
if (from_path.IsContentUri()) {
return DownloadCollectionBridge::RenameDownloadUri(from_path,
to_path.BaseName())
? download::DownloadItem::DownloadRenameResult::SUCCESS
: download::DownloadItem::DownloadRenameResult::
FAILURE_NAME_INVALID;
}
#endif
return base::Move(from_path, to_path) return base::Move(from_path, to_path)
? download::DownloadItem::DownloadRenameResult::SUCCESS ? DownloadItem::DownloadRenameResult::SUCCESS
: download::DownloadItem::DownloadRenameResult:: : DownloadItem::DownloadRenameResult::FAILURE_NAME_INVALID;
FAILURE_NAME_INVALID;
} }
int64_t GetDownloadValidationLengthConfig() { int64_t GetDownloadValidationLengthConfig() {
......
...@@ -98,10 +98,10 @@ COMPONENTS_DOWNLOAD_EXPORT bool IsDownloadDone( ...@@ -98,10 +98,10 @@ COMPONENTS_DOWNLOAD_EXPORT bool IsDownloadDone(
COMPONENTS_DOWNLOAD_EXPORT bool DeleteDownloadedFile( COMPONENTS_DOWNLOAD_EXPORT bool DeleteDownloadedFile(
const base::FilePath& path); const base::FilePath& path);
// Rename downloaded file from |oldpath| to newname. // Rename downloaded file |from_path| to a new |display_name|.
COMPONENTS_DOWNLOAD_EXPORT download::DownloadItem::DownloadRenameResult COMPONENTS_DOWNLOAD_EXPORT DownloadItem::DownloadRenameResult
RenameDownloadedFile(const base::FilePath& from_path, RenameDownloadedFile(const base::FilePath& from_path,
const base::FilePath& to_path); const base::FilePath& display_name);
// Finch parameter key value for number of bytes used for content validation // Finch parameter key value for number of bytes used for content validation
// during resumption. // during resumption.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment