summaryrefslogtreecommitdiffstats
path: root/chromeos
diff options
context:
space:
mode:
authorhashimoto@chromium.org <hashimoto@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-03-05 04:16:52 +0000
committerhashimoto@chromium.org <hashimoto@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-03-05 04:16:52 +0000
commit593cf3b0db4bc27865ab51b6aeb38594b9e290d2 (patch)
treedbced3c6e10abe71b31b40edb6ef80e2d570514e /chromeos
parent7e31df8fe2753863416731f29b37c01441afaee4 (diff)
downloadchromium_src-593cf3b0db4bc27865ab51b6aeb38594b9e290d2.zip
chromium_src-593cf3b0db4bc27865ab51b6aeb38594b9e290d2.tar.gz
chromium_src-593cf3b0db4bc27865ab51b6aeb38594b9e290d2.tar.bz2
chromeos: Implement fake archive mount functionality to CrosDisksClient
Mimic archive mount/unmount by creating empty directory under /media/archive. Implement stub for other methods at the same time. BUG=178987 Review URL: https://codereview.chromium.org/12380013 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@186093 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chromeos')
-rw-r--r--chromeos/dbus/cros_disks_client.cc159
1 files changed, 152 insertions, 7 deletions
diff --git a/chromeos/dbus/cros_disks_client.cc b/chromeos/dbus/cros_disks_client.cc
index 0116a63..c8ac903 100644
--- a/chromeos/dbus/cros_disks_client.cc
+++ b/chromeos/dbus/cros_disks_client.cc
@@ -8,9 +8,14 @@
#include "base/bind.h"
#include "base/chromeos/chromeos_version.h"
+#include "base/file_util.h"
#include "base/files/file_path.h"
+#include "base/location.h"
+#include "base/message_loop_proxy.h"
#include "base/stl_util.h"
#include "base/stringprintf.h"
+#include "base/task_runner_util.h"
+#include "base/threading/worker_pool.h"
#include "dbus/bus.h"
#include "dbus/message.h"
#include "dbus/object_path.h"
@@ -398,35 +403,175 @@ class CrosDisksClientImpl : public CrosDisksClient {
// A stub implementaion of CrosDisksClient.
class CrosDisksClientStubImpl : public CrosDisksClient {
public:
- CrosDisksClientStubImpl() {}
+ CrosDisksClientStubImpl()
+ : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {}
+
virtual ~CrosDisksClientStubImpl() {}
+ // CrosDisksClient overrides:
virtual void Mount(const std::string& source_path,
const std::string& source_format,
const std::string& mount_label,
MountType type,
const MountCallback& callback,
- const ErrorCallback& error_callback) OVERRIDE {}
+ const ErrorCallback& error_callback) OVERRIDE {
+ // Already mounted path.
+ if (mounted_paths_.count(source_path) != 0) {
+ FinishMount(MOUNT_ERROR_PATH_ALREADY_MOUNTED, source_path, type,
+ std::string(), callback);
+ return;
+ }
+
+ // This stub implementation only accepts archive mount requests.
+ if (type != MOUNT_TYPE_ARCHIVE) {
+ FinishMount(MOUNT_ERROR_INTERNAL, source_path, type, std::string(),
+ callback);
+ return;
+ }
+
+ const base::FilePath mounted_path = GetArchiveMountPoint().Append(
+ base::FilePath::FromUTF8Unsafe(mount_label));
+
+ // Perform fake mount.
+ base::PostTaskAndReplyWithResult(
+ base::WorkerPool::GetTaskRunner(true /* task_is_slow */),
+ 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 UnmountCallback& callback,
- const UnmountCallback& error_callback) OVERRIDE {}
+ const UnmountCallback& error_callback) OVERRIDE {
+ // Not mounted.
+ if (mounted_paths_.count(device_path) == 0) {
+ base::MessageLoopProxy::current()->PostTask(
+ FROM_HERE, base::Bind(error_callback, device_path));
+ return;
+ }
+
+ const base::FilePath mounted_path = mounted_paths_[device_path];
+ mounted_paths_.erase(device_path);
+
+ // Remove the directory created in Mount().
+ base::WorkerPool::PostTask(
+ FROM_HERE,
+ base::Bind(base::IgnoreResult(&file_util::Delete),
+ mounted_path,
+ true /* recursive */),
+ true /* task_is_slow */);
+
+ base::MessageLoopProxy::current()->PostTask(
+ FROM_HERE, base::Bind(callback, device_path));
+ }
+
virtual void EnumerateAutoMountableDevices(
const EnumerateAutoMountableDevicesCallback& callback,
- const ErrorCallback& error_callback) OVERRIDE {}
+ const ErrorCallback& error_callback) OVERRIDE {
+ std::vector<std::string> device_paths;
+ base::MessageLoopProxy::current()->PostTask(
+ FROM_HERE, base::Bind(callback, device_paths));
+ }
+
virtual void FormatDevice(const std::string& device_path,
const std::string& filesystem,
const FormatDeviceCallback& callback,
- const ErrorCallback& error_callback) OVERRIDE {}
+ const ErrorCallback& error_callback) OVERRIDE {
+ base::MessageLoopProxy::current()->PostTask(FROM_HERE, error_callback);
+ }
+
virtual void GetDeviceProperties(
const std::string& device_path,
const GetDevicePropertiesCallback& callback,
- const ErrorCallback& error_callback) OVERRIDE {}
+ const ErrorCallback& error_callback) OVERRIDE {
+ base::MessageLoopProxy::current()->PostTask(FROM_HERE, error_callback);
+ }
+
virtual void SetUpConnections(
const MountEventHandler& mount_event_handler,
- const MountCompletedHandler& mount_completed_handler) OVERRIDE {}
+ const MountCompletedHandler& mount_completed_handler) OVERRIDE {
+ mount_event_handler_ = mount_event_handler;
+ mount_completed_handler_ = mount_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 (!file_util::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 (!file_util::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 = file_util::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 MountCallback& 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_paths_[source_path] = mounted_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 MountCallback& 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_,
+ error, source_path, type, mounted_path));
+ }
+ }
+
+ // Source path to mounted path map.
+ std::map<std::string, base::FilePath> mounted_paths_;
+
+ MountEventHandler mount_event_handler_;
+ MountCompletedHandler mount_completed_handler_;
+
+ base::WeakPtrFactory<CrosDisksClientStubImpl> weak_ptr_factory_;
+
DISALLOW_COPY_AND_ASSIGN(CrosDisksClientStubImpl);
};