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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
// 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/ui/webui/ash/cloud_upload/one_drive_upload_handler.h"
#include "ash/constants/ash_features.h"
#include "base/check_op.h"
#include "base/debug/dump_without_crashing.h"
#include "base/files/file.h"
#include "base/functional/bind.h"
#include "base/i18n/message_formatter.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/timer/elapsed_timer.h"
#include "chrome/browser/ash/file_manager/copy_or_move_io_task.h"
#include "chrome/browser/ash/file_manager/file_tasks.h"
#include "chrome/browser/ash/file_manager/fileapi_util.h"
#include "chrome/browser/ash/file_manager/io_task.h"
#include "chrome/browser/ash/file_manager/volume_manager.h"
#include "chrome/browser/ash/file_system_provider/provided_file_system_info.h"
#include "chrome/browser/ash/file_system_provider/service.h"
#include "chrome/browser/ui/webui/ash/cloud_upload/cloud_upload_util.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/grit/generated_resources.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "google_apis/common/task_util.h"
#include "ui/base/l10n/l10n_util.h"
using ash::file_system_provider::ProvidedFileSystemInfo;
using ash::file_system_provider::ProviderId;
using ash::file_system_provider::Service;
using storage::FileSystemURL;
namespace ash::cloud_upload {
OneDriveUploadHandler::OneDriveUploadHandler(
Profile* profile,
const storage::FileSystemURL& source_url,
UploadType upload_type,
UploadCallback callback,
base::SafeRef<CloudOpenMetrics> cloud_open_metrics)
: profile_(profile),
file_system_context_(
file_manager::util::GetFileManagerFileSystemContext(profile)),
notification_manager_(
base::MakeRefCounted<CloudUploadNotificationManager>(
profile,
l10n_util::GetStringUTF8(IDS_OFFICE_CLOUD_PROVIDER_ONEDRIVE),
l10n_util::GetStringUTF8(IDS_OFFICE_FILE_HANDLER_APP_MICROSOFT),
// TODO(b/242685536) Update when support for multi-files is added.
/*num_files=*/1,
upload_type)),
source_url_(source_url),
operation_type_(upload_type == UploadType::kCopy
? file_manager::io_task::OperationType::kCopy
: file_manager::io_task::OperationType::kMove),
callback_(std::move(callback)),
cloud_open_metrics_(cloud_open_metrics) {
observed_task_id_ = -1;
}
OneDriveUploadHandler::~OneDriveUploadHandler() {
// Stop observing IO task updates.
if (io_task_controller_) {
io_task_controller_->RemoveObserver(this);
}
}
void OneDriveUploadHandler::Run() {
DCHECK(callback_);
if (!profile_) {
LOG(ERROR) << "No profile";
OnFailedUpload(OfficeFilesUploadResult::kOtherError);
return;
}
file_manager::VolumeManager* volume_manager =
(file_manager::VolumeManager::Get(profile_));
if (!volume_manager) {
LOG(ERROR) << "No volume manager";
OnFailedUpload(OfficeFilesUploadResult::kOtherError);
return;
}
io_task_controller_ = volume_manager->io_task_controller();
if (!io_task_controller_) {
LOG(ERROR) << "No task_controller";
OnFailedUpload(OfficeFilesUploadResult::kOtherError);
return;
}
// Observe IO tasks updates.
io_task_controller_->AddObserver(this);
GetODFSMetadataAndStartIOTask();
}
void OneDriveUploadHandler::GetODFSMetadataAndStartIOTask() {
file_system_provider::ProvidedFileSystemInterface* file_system =
GetODFS(profile_);
if (!file_system) {
LOG(ERROR) << "ODFS not found";
// TODO(b/293363474): Remove when the underlying cause is diagnosed.
base::debug::DumpWithoutCrashing(FROM_HERE);
OnFailedUpload(OfficeFilesUploadResult::kFileSystemNotFound);
return;
}
FileSystemURL destination_folder_url =
GetDestinationFolderUrl(file_system->GetFileSystemInfo());
if (!destination_folder_url.is_valid()) {
LOG(ERROR) << "Unable to generate destination folder ODFS URL";
// TODO(b/293363474): Remove when the underlying cause is diagnosed.
base::debug::DumpWithoutCrashing(FROM_HERE);
OnFailedUpload(OfficeFilesUploadResult::kDestinationUrlError);
return;
}
// First check that ODFS is not in the "ReauthenticationRequired" state.
GetODFSMetadata(
file_system,
base::BindOnce(
&OneDriveUploadHandler::CheckReauthenticationAndStartIOTask,
weak_ptr_factory_.GetWeakPtr(), destination_folder_url));
}
void OneDriveUploadHandler::CheckReauthenticationAndStartIOTask(
const FileSystemURL& destination_folder_url,
base::expected<ODFSMetadata, base::File::Error> metadata_or_error) {
if (!metadata_or_error.has_value()) {
// Try the copy/move anyway.
LOG(ERROR) << "Failed to get reauthentication required state: "
<< metadata_or_error.error();
} else if (metadata_or_error->reauthentication_required ||
(metadata_or_error->account_state.has_value() &&
metadata_or_error->account_state.value() ==
OdfsAccountState::kReauthenticationRequired)) {
// TODO(b/330786891): Only query account_state once
// reauthentication_required is no longer needed for backwards compatibility
// with ODFS.
if (tried_reauth_ || !base::FeatureList::IsEnabled(
ash::features::kOneDriveUploadImmediateReauth)) {
// We do not expect this failure because it would mean we became de-auth'd
// right after auth. Except when the immediate-reauth feature is on, then
// it just means reauthentication is required and we have to ask the user.
OnFailedUpload(
OfficeFilesUploadResult::kUploadNotStartedReauthenticationRequired,
GetReauthenticationRequiredMessage());
} else {
// Try to reauth immediately and then try the upload again.
RequestODFSMount(profile_,
base::BindOnce(&OneDriveUploadHandler::OnMountResponse,
weak_ptr_factory_.GetWeakPtr()));
}
return;
}
std::vector<FileSystemURL> source_urls{source_url_};
std::unique_ptr<file_manager::io_task::IOTask> task =
std::make_unique<file_manager::io_task::CopyOrMoveIOTask>(
operation_type_, std::move(source_urls),
std::move(destination_folder_url), profile_, file_system_context_,
/*show_notification=*/false);
observed_task_id_ = io_task_controller_->Add(std::move(task));
}
void OneDriveUploadHandler::OnMountResponse(base::File::Error result) {
if (result != base::File::FILE_OK) {
OnFailedUpload(
OfficeFilesUploadResult::kUploadNotStartedReauthenticationRequired,
GetReauthenticationRequiredMessage());
return;
}
tried_reauth_ = true;
GetODFSMetadataAndStartIOTask();
}
FileSystemURL OneDriveUploadHandler::GetDestinationFolderUrl(
file_system_provider::ProvidedFileSystemInfo odfs_info) {
destination_folder_path_ = odfs_info.mount_path();
return FilePathToFileSystemURL(profile_, file_system_context_,
destination_folder_path_);
}
void OneDriveUploadHandler::OnSuccessfulUpload(
OfficeFilesUploadResult result_metric,
storage::FileSystemURL url) {
cloud_open_metrics_->LogUploadResult(result_metric);
// Show complete notification.
if (notification_manager_) {
notification_manager_->MarkUploadComplete();
}
const OfficeTaskResult task_result =
operation_type_ == file_manager::io_task::OperationType::kCopy
? OfficeTaskResult::kCopied
: OfficeTaskResult::kMoved;
std::move(callback_).Run(task_result, url, upload_size_);
}
void OneDriveUploadHandler::OnFailedUpload(
OfficeFilesUploadResult result_metric,
std::string error_message) {
cloud_open_metrics_->LogUploadResult(result_metric);
// Show error notification.
if (notification_manager_) {
LOG(ERROR) << "Upload to OneDrive: " << error_message;
notification_manager_->ShowUploadError(error_message);
}
std::move(callback_).Run(OfficeTaskResult::kFailedToUpload, std::nullopt, 0);
}
void OneDriveUploadHandler::OnIOTaskStatus(
const file_manager::io_task::ProgressStatus& status) {
if (status.task_id != observed_task_id_) {
return;
}
switch (status.state) {
case file_manager::io_task::State::kScanning:
// TODO(crbug.com/1361915): Potentially adapt to show scanning.
case file_manager::io_task::State::kQueued:
return;
case file_manager::io_task::State::kInProgress:
if (status.total_bytes > 0) {
upload_size_ = status.total_bytes;
notification_manager_->ShowUploadProgress(
100 * status.bytes_transferred / status.total_bytes);
}
return;
case file_manager::io_task::State::kPaused:
return;
case file_manager::io_task::State::kSuccess:
notification_manager_->SetDestinationPath(status.outputs[0].url.path());
notification_manager_->ShowUploadProgress(100);
DCHECK_EQ(status.outputs.size(), 1u);
if (tried_reauth_) {
OnSuccessfulUpload(OfficeFilesUploadResult::kSuccessAfterReauth,
status.outputs[0].url);
} else {
OnSuccessfulUpload(OfficeFilesUploadResult::kSuccess,
status.outputs[0].url);
}
return;
case file_manager::io_task::State::kCancelled:
if (status.type == file_manager::io_task::OperationType::kCopy) {
OnFailedUpload(OfficeFilesUploadResult::kCopyOperationCancelled);
} else {
OnFailedUpload(OfficeFilesUploadResult::kMoveOperationCancelled);
}
return;
case file_manager::io_task::State::kError:
ShowIOTaskError(status);
return;
case file_manager::io_task::State::kNeedPassword:
NOTREACHED() << "Encrypted file should not need password to be copied or "
"moved. Case should not be reached.";
}
}
void OneDriveUploadHandler::OnGetReauthenticationRequired(
base::expected<ODFSMetadata, base::File::Error> metadata_or_error) {
std::string error_message = GetGenericErrorMessage();
OfficeFilesUploadResult upload_result =
OfficeFilesUploadResult::kCloudAccessDenied;
if (!metadata_or_error.has_value()) {
LOG(ERROR) << "Failed to get reauthentication required state: "
<< metadata_or_error.error();
} else if (metadata_or_error->reauthentication_required ||
(metadata_or_error->account_state.has_value() &&
metadata_or_error->account_state.value() ==
OdfsAccountState::kReauthenticationRequired)) {
// TODO(b/330786891): Only query account_state once
// reauthentication_required is no longer needed for backwards compatibility
// with ODFS. Show the reauthentication required error notification.
error_message = GetReauthenticationRequiredMessage();
upload_result = OfficeFilesUploadResult::kCloudReauthRequired;
}
OnFailedUpload(upload_result, error_message);
}
void OneDriveUploadHandler::ShowAccessDeniedError() {
file_system_provider::ProvidedFileSystemInterface* file_system =
GetODFS(profile_);
if (!file_system) {
LOG(ERROR) << "ODFS not found";
OnFailedUpload(OfficeFilesUploadResult::kCloudAccessDenied);
return;
}
GetODFSMetadata(
file_system,
base::BindOnce(&OneDriveUploadHandler::OnGetReauthenticationRequired,
weak_ptr_factory_.GetWeakPtr()));
}
void OneDriveUploadHandler::ShowIOTaskError(
const file_manager::io_task::ProgressStatus& status) {
OfficeFilesUploadResult upload_result;
std::string error_message;
bool copy = status.type == file_manager::io_task::OperationType::kCopy;
// TODO(b/242685536) Find most relevant error in a multi-file upload when
// support for multi-files is added.
base::File::Error file_error =
GetFirstTaskError(status).value_or(base::File::FILE_ERROR_FAILED);
if (copy) {
cloud_open_metrics_->LogCopyError(file_error);
} else {
cloud_open_metrics_->LogMoveError(file_error);
}
switch (file_error) {
case base::File::FILE_ERROR_ACCESS_DENIED:
ShowAccessDeniedError();
return;
case base::File::FILE_ERROR_NO_SPACE:
upload_result = OfficeFilesUploadResult::kCloudQuotaFull;
// TODO(b/242685536) Use "these files" for multi-files when support for
// multi-files is added.
error_message = base::UTF16ToUTF8(
base::i18n::MessageFormatter::FormatWithNumberedArgs(
l10n_util::GetStringUTF16(
copy ? IDS_OFFICE_UPLOAD_ERROR_FREE_UP_SPACE_TO_COPY
: IDS_OFFICE_UPLOAD_ERROR_FREE_UP_SPACE_TO_MOVE),
// TODO(b/242685536) Update when support for multi-files is added.
1,
l10n_util::GetStringUTF16(
IDS_OFFICE_CLOUD_PROVIDER_ONEDRIVE_SHORT)));
break;
case base::File::FILE_ERROR_NOT_FOUND:
if (copy) {
upload_result = OfficeFilesUploadResult::kCopyOperationError;
} else {
upload_result = OfficeFilesUploadResult::kMoveOperationError;
}
error_message = l10n_util::GetStringUTF8(
copy ? IDS_OFFICE_UPLOAD_ERROR_FILE_NOT_EXIST_TO_COPY
: IDS_OFFICE_UPLOAD_ERROR_FILE_NOT_EXIST_TO_MOVE);
break;
case base::File::FILE_ERROR_INVALID_URL:
if (copy) {
upload_result = OfficeFilesUploadResult::kCopyOperationError;
} else {
upload_result = OfficeFilesUploadResult::kMoveOperationError;
}
error_message =
l10n_util::GetStringUTF8(IDS_OFFICE_UPLOAD_ERROR_REJECTED);
break;
default:
if (copy) {
upload_result = OfficeFilesUploadResult::kCopyOperationError;
} else {
upload_result = OfficeFilesUploadResult::kMoveOperationError;
}
error_message = GetGenericErrorMessage();
}
OnFailedUpload(upload_result, error_message);
}
} // namespace ash::cloud_upload
|