Commit 9647a6ed authored by Anand K. Mistry's avatar Anand K. Mistry Committed by Commit Bot

Return unmount error from CrosDisksClient instead of success/fail.

Receiving the unmount error code will let Chrome properly handle various
types of errors, and produce better logging.

BUG=873903

Change-Id: I792a806440be6725ceba0663afc4b4ee3069d9bd
Reviewed-on: https://chromium-review.googlesource.com/1179109Reviewed-by: default avatarNoel Gordon <noel@chromium.org>
Reviewed-by: default avatarSteven Bennetts <stevenjb@chromium.org>
Commit-Queue: Anand Mistry <amistry@chromium.org>
Cr-Commit-Position: refs/heads/master@{#584348}
parent f4d9a36a
...@@ -177,7 +177,7 @@ class CrosDisksClientImpl : public CrosDisksClient { ...@@ -177,7 +177,7 @@ class CrosDisksClientImpl : public CrosDisksClient {
// CrosDisksClient override. // CrosDisksClient override.
void Unmount(const std::string& device_path, void Unmount(const std::string& device_path,
UnmountOptions options, UnmountOptions options,
VoidDBusMethodCallback callback) override { UnmountCallback callback) override {
dbus::MethodCall method_call(cros_disks::kCrosDisksInterface, dbus::MethodCall method_call(cros_disks::kCrosDisksInterface,
cros_disks::kUnmount); cros_disks::kUnmount);
dbus::MessageWriter writer(&method_call); dbus::MessageWriter writer(&method_call);
...@@ -324,9 +324,9 @@ class CrosDisksClientImpl : public CrosDisksClient { ...@@ -324,9 +324,9 @@ class CrosDisksClientImpl : public CrosDisksClient {
} }
// Handles the result of Unmount and calls |callback| or |error_callback|. // Handles the result of Unmount and calls |callback| or |error_callback|.
void OnUnmount(VoidDBusMethodCallback callback, dbus::Response* response) { void OnUnmount(UnmountCallback callback, dbus::Response* response) {
if (!response) { if (!response) {
std::move(callback).Run(false); std::move(callback).Run(MOUNT_ERROR_UNKNOWN);
return; return;
} }
...@@ -340,15 +340,12 @@ class CrosDisksClientImpl : public CrosDisksClient { ...@@ -340,15 +340,12 @@ class CrosDisksClientImpl : public CrosDisksClient {
// the response. // the response.
dbus::MessageReader reader(response); dbus::MessageReader reader(response);
uint32_t error_code = 0; uint32_t error_code = 0;
if (reader.PopUint32(&error_code) && MountError mount_error = MOUNT_ERROR_NONE;
CrosDisksMountErrorToChromeMountError( if (reader.PopUint32(&error_code)) {
static_cast<cros_disks::MountErrorType>(error_code)) != mount_error = CrosDisksMountErrorToChromeMountError(
MOUNT_ERROR_NONE) { static_cast<cros_disks::MountErrorType>(error_code));
std::move(callback).Run(false);
return;
} }
std::move(callback).Run(mount_error);
std::move(callback).Run(true);
} }
// Handles the result of EnumerateDevices and EnumarateAutoMountableDevices. // Handles the result of EnumerateDevices and EnumarateAutoMountableDevices.
......
...@@ -283,6 +283,10 @@ class CHROMEOS_EXPORT CrosDisksClient : public DBusClient { ...@@ -283,6 +283,10 @@ class CHROMEOS_EXPORT CrosDisksClient : public DBusClient {
typedef base::Callback<void(const DiskInfo& disk_info)> typedef base::Callback<void(const DiskInfo& disk_info)>
GetDevicePropertiesCallback; GetDevicePropertiesCallback;
// A callback to handle the result of Unmount.
// The argument is the unmount error code.
typedef base::OnceCallback<void(MountError error_code)> UnmountCallback;
class Observer { class Observer {
public: public:
// Called when a mount event signal is received. // Called when a mount event signal is received.
...@@ -330,10 +334,10 @@ class CHROMEOS_EXPORT CrosDisksClient : public DBusClient { ...@@ -330,10 +334,10 @@ class CHROMEOS_EXPORT CrosDisksClient : public DBusClient {
VoidDBusMethodCallback callback) = 0; VoidDBusMethodCallback callback) = 0;
// Calls Unmount method. On method call completion, |callback| is called // Calls Unmount method. On method call completion, |callback| is called
// with |true| on success, or with |false| otherwise. // with the error code.
virtual void Unmount(const std::string& device_path, virtual void Unmount(const std::string& device_path,
UnmountOptions options, UnmountOptions options,
VoidDBusMethodCallback callback) = 0; UnmountCallback callback) = 0;
// Calls EnumerateDevices method. |callback| is called after the // Calls EnumerateDevices method. |callback| is called after the
// method call succeeds, otherwise, |error_callback| is called. // method call succeeds, otherwise, |error_callback| is called.
......
...@@ -56,7 +56,7 @@ MountError PerformFakeMount(const std::string& source_path, ...@@ -56,7 +56,7 @@ MountError PerformFakeMount(const std::string& source_path,
FakeCrosDisksClient::FakeCrosDisksClient() FakeCrosDisksClient::FakeCrosDisksClient()
: unmount_call_count_(0), : unmount_call_count_(0),
unmount_success_(true), unmount_error_(MOUNT_ERROR_NONE),
format_call_count_(0), format_call_count_(0),
format_success_(true), format_success_(true),
rename_call_count_(0), rename_call_count_(0),
...@@ -144,7 +144,7 @@ void FakeCrosDisksClient::DidMount(const std::string& source_path, ...@@ -144,7 +144,7 @@ void FakeCrosDisksClient::DidMount(const std::string& source_path,
void FakeCrosDisksClient::Unmount(const std::string& device_path, void FakeCrosDisksClient::Unmount(const std::string& device_path,
UnmountOptions options, UnmountOptions options,
VoidDBusMethodCallback callback) { UnmountCallback callback) {
DCHECK(!callback.is_null()); DCHECK(!callback.is_null());
unmount_call_count_++; unmount_call_count_++;
...@@ -160,10 +160,10 @@ void FakeCrosDisksClient::Unmount(const std::string& device_path, ...@@ -160,10 +160,10 @@ void FakeCrosDisksClient::Unmount(const std::string& device_path,
base::BindOnce(base::IgnoreResult(&base::DeleteFile), base::BindOnce(base::IgnoreResult(&base::DeleteFile),
base::FilePath::FromUTF8Unsafe(device_path), base::FilePath::FromUTF8Unsafe(device_path),
true /* recursive */), true /* recursive */),
base::BindOnce(std::move(callback), unmount_success_)); base::BindOnce(std::move(callback), unmount_error_));
} else { } else {
base::ThreadTaskRunnerHandle::Get()->PostTask( base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), unmount_success_)); FROM_HERE, base::BindOnce(std::move(callback), unmount_error_));
} }
if (!unmount_listener_.is_null()) if (!unmount_listener_.is_null())
unmount_listener_.Run(); unmount_listener_.Run();
......
...@@ -46,7 +46,7 @@ class CHROMEOS_EXPORT FakeCrosDisksClient : public CrosDisksClient { ...@@ -46,7 +46,7 @@ class CHROMEOS_EXPORT FakeCrosDisksClient : public CrosDisksClient {
// Deletes the directory created in Mount(). // Deletes the directory created in Mount().
void Unmount(const std::string& device_path, void Unmount(const std::string& device_path,
UnmountOptions options, UnmountOptions options,
VoidDBusMethodCallback callback) override; UnmountCallback callback) override;
void EnumerateDevices(const EnumerateDevicesCallback& callback, void EnumerateDevices(const EnumerateDevicesCallback& callback,
const base::Closure& error_callback) override; const base::Closure& error_callback) override;
void EnumerateMountEntries(const EnumerateMountEntriesCallback& callback, void EnumerateMountEntries(const EnumerateMountEntriesCallback& callback,
...@@ -94,9 +94,7 @@ class CHROMEOS_EXPORT FakeCrosDisksClient : public CrosDisksClient { ...@@ -94,9 +94,7 @@ class CHROMEOS_EXPORT FakeCrosDisksClient : public CrosDisksClient {
} }
// Makes the subsequent Unmount() calls fail. Unmount() succeeds by default. // Makes the subsequent Unmount() calls fail. Unmount() succeeds by default.
void MakeUnmountFail() { void MakeUnmountFail(MountError error_code) { unmount_error_ = error_code; }
unmount_success_ = false;
}
// Sets a listener callbackif the following Unmount() call is success or not. // Sets a listener callbackif the following Unmount() call is success or not.
// Unmount() calls the corresponding callback given as a parameter. // Unmount() calls the corresponding callback given as a parameter.
...@@ -152,7 +150,7 @@ class CHROMEOS_EXPORT FakeCrosDisksClient : public CrosDisksClient { ...@@ -152,7 +150,7 @@ class CHROMEOS_EXPORT FakeCrosDisksClient : public CrosDisksClient {
int unmount_call_count_; int unmount_call_count_;
std::string last_unmount_device_path_; std::string last_unmount_device_path_;
UnmountOptions last_unmount_options_; UnmountOptions last_unmount_options_;
bool unmount_success_; MountError unmount_error_;
base::Closure unmount_listener_; base::Closure unmount_listener_;
int format_call_count_; int format_call_count_;
std::string last_format_device_path_; std::string last_format_device_path_;
......
...@@ -388,10 +388,11 @@ class DiskMountManagerImpl : public DiskMountManager, ...@@ -388,10 +388,11 @@ class DiskMountManagerImpl : public DiskMountManager,
// Callback for UnmountDeviceRecursively. // Callback for UnmountDeviceRecursively.
void OnUnmountDeviceRecursively(UnmountDeviceRecursivelyCallbackData* cb_data, void OnUnmountDeviceRecursively(UnmountDeviceRecursivelyCallbackData* cb_data,
const std::string& mount_path, const std::string& mount_path,
bool success) { MountError error_code) {
bool success = error_code == MOUNT_ERROR_NONE;
if (success) { if (success) {
// Do standard processing for Unmount event. // Do standard processing for Unmount event.
OnUnmountPath(UnmountPathCallback(), mount_path, true /* success */); OnUnmountPath(UnmountPathCallback(), mount_path, MOUNT_ERROR_NONE);
VLOG(1) << mount_path << " unmounted."; VLOG(1) << mount_path << " unmounted.";
} }
// This is safe as long as all callbacks are called on the same thread as // This is safe as long as all callbacks are called on the same thread as
...@@ -464,7 +465,7 @@ class DiskMountManagerImpl : public DiskMountManager, ...@@ -464,7 +465,7 @@ class DiskMountManagerImpl : public DiskMountManager,
// Callback for UnmountPath. // Callback for UnmountPath.
void OnUnmountPath(const UnmountPathCallback& callback, void OnUnmountPath(const UnmountPathCallback& callback,
const std::string& mount_path, const std::string& mount_path,
bool success) { MountError error_code) {
MountPointMap::iterator mount_points_it = mount_points_.find(mount_path); MountPointMap::iterator mount_points_it = mount_points_.find(mount_path);
if (mount_points_it == mount_points_.end()) { if (mount_points_it == mount_points_.end()) {
// The path was unmounted, but not as a result of this unmount request, // The path was unmounted, but not as a result of this unmount request,
...@@ -475,26 +476,25 @@ class DiskMountManagerImpl : public DiskMountManager, ...@@ -475,26 +476,25 @@ class DiskMountManagerImpl : public DiskMountManager,
} }
NotifyMountStatusUpdate( NotifyMountStatusUpdate(
UNMOUNTING, UNMOUNTING, error_code,
success ? MOUNT_ERROR_NONE : MOUNT_ERROR_INTERNAL,
MountPointInfo(mount_points_it->second.source_path, MountPointInfo(mount_points_it->second.source_path,
mount_points_it->second.mount_path, mount_points_it->second.mount_path,
mount_points_it->second.mount_type, mount_points_it->second.mount_type,
mount_points_it->second.mount_condition)); mount_points_it->second.mount_condition));
std::string path(mount_points_it->second.source_path); std::string path(mount_points_it->second.source_path);
if (success) if (error_code == MOUNT_ERROR_NONE)
mount_points_.erase(mount_points_it); mount_points_.erase(mount_points_it);
DiskMap::iterator disk_iter = disks_.find(path); DiskMap::iterator disk_iter = disks_.find(path);
if (disk_iter != disks_.end()) { if (disk_iter != disks_.end()) {
DCHECK(disk_iter->second); DCHECK(disk_iter->second);
if (success) if (error_code == MOUNT_ERROR_NONE)
disk_iter->second->clear_mount_path(); disk_iter->second->clear_mount_path();
} }
if (!callback.is_null()) if (!callback.is_null())
callback.Run(success ? MOUNT_ERROR_NONE : MOUNT_ERROR_INTERNAL); callback.Run(error_code);
} }
void OnUnmountPathForFormat(const std::string& device_path, void OnUnmountPathForFormat(const std::string& device_path,
......
...@@ -620,7 +620,8 @@ TEST_F(DiskMountManagerTest, Format_FailToUnmount) { ...@@ -620,7 +620,8 @@ TEST_F(DiskMountManagerTest, Format_FailToUnmount) {
// In this test unmount will fail, and there should be no attempt to // In this test unmount will fail, and there should be no attempt to
// format the device. // format the device.
fake_cros_disks_client_->MakeUnmountFail(); fake_cros_disks_client_->MakeUnmountFail(
chromeos::MOUNT_ERROR_PATH_NOT_MOUNTED);
// Start test. // Start test.
DiskMountManager::GetInstance()->FormatMountedDevice(kDevice1MountPath); DiskMountManager::GetInstance()->FormatMountedDevice(kDevice1MountPath);
...@@ -632,7 +633,7 @@ TEST_F(DiskMountManagerTest, Format_FailToUnmount) { ...@@ -632,7 +633,7 @@ TEST_F(DiskMountManagerTest, Format_FailToUnmount) {
ASSERT_EQ(2U, observer_->GetEventCount()); ASSERT_EQ(2U, observer_->GetEventCount());
const MountEvent& mount_event = observer_->GetMountEvent(0); const MountEvent& mount_event = observer_->GetMountEvent(0);
EXPECT_EQ(DiskMountManager::UNMOUNTING, mount_event.event); EXPECT_EQ(DiskMountManager::UNMOUNTING, mount_event.event);
EXPECT_EQ(chromeos::MOUNT_ERROR_INTERNAL, mount_event.error_code); EXPECT_EQ(chromeos::MOUNT_ERROR_PATH_NOT_MOUNTED, mount_event.error_code);
EXPECT_EQ(kDevice1MountPath, mount_event.mount_point.mount_path); EXPECT_EQ(kDevice1MountPath, mount_event.mount_point.mount_path);
EXPECT_EQ(FormatEvent(DiskMountManager::FORMAT_COMPLETED, EXPECT_EQ(FormatEvent(DiskMountManager::FORMAT_COMPLETED,
...@@ -698,7 +699,8 @@ TEST_F(DiskMountManagerTest, Format_ConcurrentFormatCalls) { ...@@ -698,7 +699,8 @@ TEST_F(DiskMountManagerTest, Format_ConcurrentFormatCalls) {
fake_cros_disks_client_->set_unmount_listener( fake_cros_disks_client_->set_unmount_listener(
base::Bind(&FakeCrosDisksClient::MakeUnmountFail, base::Bind(&FakeCrosDisksClient::MakeUnmountFail,
base::Unretained(fake_cros_disks_client_))); base::Unretained(fake_cros_disks_client_),
chromeos::MOUNT_ERROR_INVALID_UNMOUNT_OPTIONS));
// Start the test. // Start the test.
DiskMountManager::GetInstance()->FormatMountedDevice(kDevice1MountPath); DiskMountManager::GetInstance()->FormatMountedDevice(kDevice1MountPath);
DiskMountManager::GetInstance()->FormatMountedDevice(kDevice1MountPath); DiskMountManager::GetInstance()->FormatMountedDevice(kDevice1MountPath);
...@@ -1081,7 +1083,7 @@ TEST_F(DiskMountManagerTest, Rename_FailToUnmount) { ...@@ -1081,7 +1083,7 @@ TEST_F(DiskMountManagerTest, Rename_FailToUnmount) {
// In this test unmount will fail, and there should be no attempt to // In this test unmount will fail, and there should be no attempt to
// rename the device. // rename the device.
fake_cros_disks_client_->MakeUnmountFail(); fake_cros_disks_client_->MakeUnmountFail(chromeos::MOUNT_ERROR_UNKNOWN);
// Start test. // Start test.
DiskMountManager::GetInstance()->RenameMountedDevice(kDevice1MountPath, DiskMountManager::GetInstance()->RenameMountedDevice(kDevice1MountPath,
"MYUSB"); "MYUSB");
...@@ -1094,7 +1096,7 @@ TEST_F(DiskMountManagerTest, Rename_FailToUnmount) { ...@@ -1094,7 +1096,7 @@ TEST_F(DiskMountManagerTest, Rename_FailToUnmount) {
ASSERT_EQ(2U, observer_->GetEventCount()); ASSERT_EQ(2U, observer_->GetEventCount());
const MountEvent& mount_event = observer_->GetMountEvent(0); const MountEvent& mount_event = observer_->GetMountEvent(0);
EXPECT_EQ(DiskMountManager::UNMOUNTING, mount_event.event); EXPECT_EQ(DiskMountManager::UNMOUNTING, mount_event.event);
EXPECT_EQ(chromeos::MOUNT_ERROR_INTERNAL, mount_event.error_code); EXPECT_EQ(chromeos::MOUNT_ERROR_UNKNOWN, mount_event.error_code);
EXPECT_EQ(kDevice1MountPath, mount_event.mount_point.mount_path); EXPECT_EQ(kDevice1MountPath, mount_event.mount_point.mount_path);
EXPECT_EQ(RenameEvent(DiskMountManager::RENAME_COMPLETED, EXPECT_EQ(RenameEvent(DiskMountManager::RENAME_COMPLETED,
...@@ -1161,7 +1163,8 @@ TEST_F(DiskMountManagerTest, Rename_ConcurrentRenameCalls) { ...@@ -1161,7 +1163,8 @@ TEST_F(DiskMountManagerTest, Rename_ConcurrentRenameCalls) {
fake_cros_disks_client_->set_unmount_listener( fake_cros_disks_client_->set_unmount_listener(
base::Bind(&FakeCrosDisksClient::MakeUnmountFail, base::Bind(&FakeCrosDisksClient::MakeUnmountFail,
base::Unretained(fake_cros_disks_client_))); base::Unretained(fake_cros_disks_client_),
chromeos::MOUNT_ERROR_INTERNAL));
// Start the test. // Start the test.
DiskMountManager::GetInstance()->RenameMountedDevice(kDevice1MountPath, DiskMountManager::GetInstance()->RenameMountedDevice(kDevice1MountPath,
"MYUSB1"); "MYUSB1");
......
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