Skip to content

Commit

Permalink
Merge CrosDisksClientStubImpl into FakeCrosDisksClient
Browse files Browse the repository at this point in the history
Move the fake mounting logic from CrosDisksClientStubImpl
to FakeCrosDsisksClient, with some simplification.

The fake behaviors are used in FileManagerBrowserTest's
OpenZipFiles test. The test is currently disabled for
a performance reason, but I've confirmed that the test
passes with --gtest_also_run_disabled_tests.

TEST=out/Release/browser_tests --gtest_filter='*OpenZipFiles*' --gtest_also_run_disabled_tests
BUG=309506

Review URL: https://codereview.chromium.org/676423004

Cr-Commit-Position: refs/heads/master@{#302053}
  • Loading branch information
satorux authored and Commit bot committed Oct 30, 2014
1 parent e91d66a commit 8fd2938
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 186 deletions.
4 changes: 2 additions & 2 deletions chromeos/chromeos.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@
'dbus/fake_bluetooth_profile_service_provider.h',
'dbus/fake_cras_audio_client.cc',
'dbus/fake_cras_audio_client.h',
'dbus/fake_cros_disks_client.cc',
'dbus/fake_cros_disks_client.h',
'dbus/fake_cryptohome_client.cc',
'dbus/fake_cryptohome_client.h',
'dbus/fake_debug_daemon_client.cc',
Expand Down Expand Up @@ -574,8 +576,6 @@
],
# If you edit the file list of this target, please edit BUILD.gn as well.
'sources': [
'dbus/fake_cros_disks_client.cc',
'dbus/fake_cros_disks_client.h',
'dbus/fake_power_manager_client.cc',
'dbus/fake_power_manager_client.h',
'dbus/fake_session_manager_client.cc',
Expand Down
185 changes: 2 additions & 183 deletions chromeos/dbus/cros_disks_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "base/task_runner_util.h"
#include "base/threading/worker_pool.h"
#include "base/values.h"
#include "chromeos/dbus/fake_cros_disks_client.h"
#include "dbus/bus.h"
#include "dbus/message.h"
#include "dbus/object_path.h"
Expand Down Expand Up @@ -454,188 +455,6 @@ class CrosDisksClientImpl : public CrosDisksClient {
DISALLOW_COPY_AND_ASSIGN(CrosDisksClientImpl);
};

// A stub implementaion of CrosDisksClient.
class CrosDisksClientStubImpl : public CrosDisksClient {
public:
CrosDisksClientStubImpl()
: weak_ptr_factory_(this) {}

virtual ~CrosDisksClientStubImpl() {}

// CrosDisksClient overrides:
virtual void Init(dbus::Bus* bus) override {}
virtual void Mount(const std::string& source_path,
const std::string& source_format,
const std::string& mount_label,
const base::Closure& callback,
const base::Closure& error_callback) override {
// This stub implementation only accepts archive mount requests.
const MountType type = MOUNT_TYPE_ARCHIVE;

const base::FilePath mounted_path = GetArchiveMountPoint().Append(
base::FilePath::FromUTF8Unsafe(mount_label));

// Already mounted path.
if (mounted_to_source_path_map_.count(mounted_path.value()) != 0) {
FinishMount(MOUNT_ERROR_PATH_ALREADY_MOUNTED, source_path, type,
std::string(), callback);
return;
}

// Perform fake mount.
base::PostTaskAndReplyWithResult(
base::WorkerPool::GetTaskRunner(true /* task_is_slow */).get(),
FROM_HERE,
base::Bind(&PerformFakeMount, source_path, mounted_path),
base::Bind(&CrosDisksClientStubImpl::ContinueMount,
weak_ptr_factory_.GetWeakPtr(),
source_path,
type,
callback,
mounted_path));
}

virtual void Unmount(const std::string& device_path,
UnmountOptions options,
const base::Closure& callback,
const base::Closure& error_callback) override {
// Not mounted.
if (mounted_to_source_path_map_.count(device_path) == 0) {
base::MessageLoopProxy::current()->PostTask(FROM_HERE, error_callback);
return;
}

mounted_to_source_path_map_.erase(device_path);

// Remove the directory created in Mount().
base::WorkerPool::PostTaskAndReply(
FROM_HERE,
base::Bind(base::IgnoreResult(&base::DeleteFile),
base::FilePath::FromUTF8Unsafe(device_path),
true /* recursive */),
callback,
true /* task_is_slow */);
}

virtual void EnumerateAutoMountableDevices(
const EnumerateAutoMountableDevicesCallback& callback,
const base::Closure& error_callback) override {
std::vector<std::string> device_paths;
base::MessageLoopProxy::current()->PostTask(
FROM_HERE, base::Bind(callback, device_paths));
}

virtual void EnumerateMountEntries(
const EnumerateMountEntriesCallback& callback,
const base::Closure& error_callback) override {
std::vector<MountEntry> entries;
base::MessageLoopProxy::current()->PostTask(
FROM_HERE, base::Bind(callback, entries));
}

virtual void Format(const std::string& device_path,
const std::string& filesystem,
const base::Closure& callback,
const base::Closure& error_callback) override {
base::MessageLoopProxy::current()->PostTask(FROM_HERE, error_callback);
}

virtual void GetDeviceProperties(
const std::string& device_path,
const GetDevicePropertiesCallback& callback,
const base::Closure& error_callback) override {
base::MessageLoopProxy::current()->PostTask(FROM_HERE, error_callback);
}

virtual void SetMountEventHandler(
const MountEventHandler& mount_event_handler) override {
mount_event_handler_ = mount_event_handler;
}

virtual void SetMountCompletedHandler(
const MountCompletedHandler& mount_completed_handler) override {
mount_completed_handler_ = mount_completed_handler;
}

virtual void SetFormatCompletedHandler(
const FormatCompletedHandler& format_completed_handler) override {
format_completed_handler_ = format_completed_handler;
}

private:
// Performs file actions for Mount().
static MountError PerformFakeMount(const std::string& source_path,
const base::FilePath& mounted_path) {
// Check the source path exists.
if (!base::PathExists(base::FilePath::FromUTF8Unsafe(source_path))) {
DLOG(ERROR) << "Source does not exist at " << source_path;
return MOUNT_ERROR_INVALID_PATH;
}

// Just create an empty directory and shows it as the mounted directory.
if (!base::CreateDirectory(mounted_path)) {
DLOG(ERROR) << "Failed to create directory at " << mounted_path.value();
return MOUNT_ERROR_DIRECTORY_CREATION_FAILED;
}

// Put a dummy file.
const base::FilePath dummy_file_path =
mounted_path.Append("SUCCESSFULLY_PERFORMED_FAKE_MOUNT.txt");
const std::string dummy_file_content = "This is a dummy file.";
const int write_result = base::WriteFile(
dummy_file_path, dummy_file_content.data(), dummy_file_content.size());
if (write_result != static_cast<int>(dummy_file_content.size())) {
DLOG(ERROR) << "Failed to put a dummy file at "
<< dummy_file_path.value();
return MOUNT_ERROR_MOUNT_PROGRAM_FAILED;
}

return MOUNT_ERROR_NONE;
}

// Part of Mount() implementation.
void ContinueMount(const std::string& source_path,
MountType type,
const base::Closure& callback,
const base::FilePath& mounted_path,
MountError mount_error) {
if (mount_error != MOUNT_ERROR_NONE) {
FinishMount(mount_error, source_path, type, std::string(), callback);
return;
}
mounted_to_source_path_map_[mounted_path.value()] = source_path;
FinishMount(MOUNT_ERROR_NONE, source_path, type,
mounted_path.AsUTF8Unsafe(), callback);
}

// Runs |callback| and sends MountCompleted signal.
// Part of Mount() implementation.
void FinishMount(MountError error,
const std::string& source_path,
MountType type,
const std::string& mounted_path,
const base::Closure& callback) {
base::MessageLoopProxy::current()->PostTask(FROM_HERE, callback);
if (!mount_completed_handler_.is_null()) {
base::MessageLoopProxy::current()->PostTask(
FROM_HERE,
base::Bind(mount_completed_handler_,
MountEntry(error, source_path, type, mounted_path)));
}
}

// Mounted path to source path map.
std::map<std::string, std::string> mounted_to_source_path_map_;

MountEventHandler mount_event_handler_;
MountCompletedHandler mount_completed_handler_;
FormatCompletedHandler format_completed_handler_;

base::WeakPtrFactory<CrosDisksClientStubImpl> weak_ptr_factory_;

DISALLOW_COPY_AND_ASSIGN(CrosDisksClientStubImpl);
};

} // namespace

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -822,7 +641,7 @@ CrosDisksClient* CrosDisksClient::Create(DBusClientImplementationType type) {
if (type == REAL_DBUS_CLIENT_IMPLEMENTATION)
return new CrosDisksClientImpl();
DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type);
return new CrosDisksClientStubImpl();
return new FakeCrosDisksClient();
}

// static
Expand Down
81 changes: 81 additions & 0 deletions chromeos/dbus/fake_cros_disks_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,63 @@
#include "chromeos/dbus/fake_cros_disks_client.h"

#include "base/bind.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "base/task_runner_util.h"
#include "base/threading/worker_pool.h"

namespace chromeos {

namespace {

// Performs fake mounting by creating a directory with a dummy file.
MountError PerformFakeMount(const std::string& source_path,
const base::FilePath& mounted_path) {
// Just create an empty directory and shows it as the mounted directory.
if (!base::CreateDirectory(mounted_path)) {
DLOG(ERROR) << "Failed to create directory at " << mounted_path.value();
return MOUNT_ERROR_DIRECTORY_CREATION_FAILED;
}

// Put a dummy file.
const base::FilePath dummy_file_path =
mounted_path.Append("SUCCESSFULLY_PERFORMED_FAKE_MOUNT.txt");
const std::string dummy_file_content = "This is a dummy file.";
const int write_result = base::WriteFile(
dummy_file_path, dummy_file_content.data(), dummy_file_content.size());
if (write_result != static_cast<int>(dummy_file_content.size())) {
DLOG(ERROR) << "Failed to put a dummy file at "
<< dummy_file_path.value();
return MOUNT_ERROR_MOUNT_PROGRAM_FAILED;
}

return MOUNT_ERROR_NONE;
}

// Continuation of Mount().
void DidMount(const CrosDisksClient::MountCompletedHandler&
mount_completed_handler,
const std::string& source_path,
MountType type,
const base::Closure& callback,
const base::FilePath& mounted_path,
MountError mount_error) {
// Tell the caller of Mount() that the mount request was accepted.
base::MessageLoopProxy::current()->PostTask(FROM_HERE, callback);

// Tell the caller of Mount() that the mount completed.
if (!mount_completed_handler.is_null()) {
base::MessageLoopProxy::current()->PostTask(
FROM_HERE,
base::Bind(mount_completed_handler,
MountEntry(mount_error, source_path, type,
mounted_path.AsUTF8Unsafe())));
}
}

} // namespace

FakeCrosDisksClient::FakeCrosDisksClient()
: unmount_call_count_(0),
unmount_success_(true),
Expand All @@ -28,6 +80,23 @@ void FakeCrosDisksClient::Mount(const std::string& source_path,
const std::string& mount_label,
const base::Closure& callback,
const base::Closure& error_callback) {
// This fake implementation only accepts archive mount requests.
const MountType type = MOUNT_TYPE_ARCHIVE;

const base::FilePath mounted_path = GetArchiveMountPoint().Append(
base::FilePath::FromUTF8Unsafe(mount_label));
mounted_paths_.insert(mounted_path);

base::PostTaskAndReplyWithResult(
base::WorkerPool::GetTaskRunner(true /* task_is_slow */).get(),
FROM_HERE,
base::Bind(&PerformFakeMount, source_path, mounted_path),
base::Bind(&DidMount,
mount_completed_handler_,
source_path,
type,
callback,
mounted_path));
}

void FakeCrosDisksClient::Unmount(const std::string& device_path,
Expand All @@ -37,6 +106,18 @@ void FakeCrosDisksClient::Unmount(const std::string& device_path,
DCHECK(!callback.is_null());
DCHECK(!error_callback.is_null());

// Remove the dummy mounted directory if it exists.
if (mounted_paths_.count(base::FilePath::FromUTF8Unsafe(device_path)) > 0) {
mounted_paths_.erase(base::FilePath::FromUTF8Unsafe(device_path));
base::WorkerPool::PostTaskAndReply(
FROM_HERE,
base::Bind(base::IgnoreResult(&base::DeleteFile),
base::FilePath::FromUTF8Unsafe(device_path),
true /* recursive */),
callback,
true /* task_is_slow */);
}

unmount_call_count_++;
last_unmount_device_path_ = device_path;
last_unmount_options_ = options;
Expand Down
10 changes: 9 additions & 1 deletion chromeos/dbus/fake_cros_disks_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,34 @@
#ifndef CHROMEOS_DBUS_FAKE_CROS_DISKS_CLIENT_H_
#define CHROMEOS_DBUS_FAKE_CROS_DISKS_CLIENT_H_

#include <set>
#include <string>

#include "base/callback.h"
#include "base/files/file_path.h"
#include "chromeos/dbus/cros_disks_client.h"

namespace chromeos {

// A fake implementation of CrosDiskeClient. This class provides a fake behavior
// and the user of this class can raise a fake mouse events.
class FakeCrosDisksClient : public CrosDisksClient {
class CHROMEOS_EXPORT FakeCrosDisksClient : public CrosDisksClient {
public:
FakeCrosDisksClient();
virtual ~FakeCrosDisksClient();

// CrosDisksClient overrides
virtual void Init(dbus::Bus* bus) override;

// Performs fake mounting for archive files. Instead of actually extracting
// contents of archive files, this function creates a directory that
// contains a dummy file.
virtual void Mount(const std::string& source_path,
const std::string& source_format,
const std::string& mount_label,
const base::Closure& callback,
const base::Closure& error_callback) override;
// Deletes the directory created in Mount().
virtual void Unmount(const std::string& device_path,
UnmountOptions options,
const base::Closure& callback,
Expand Down Expand Up @@ -122,6 +129,7 @@ class FakeCrosDisksClient : public CrosDisksClient {
std::string last_format_device_path_;
std::string last_format_filesystem_;
bool format_success_;
std::set<base::FilePath> mounted_paths_;
};

} // namespace chromeos
Expand Down

0 comments on commit 8fd2938

Please sign in to comment.