Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chromeos / dbus / cros_disks_client.cc
index cca2b17..9819a31 100644 (file)
@@ -7,8 +7,8 @@
 #include <map>
 
 #include "base/bind.h"
-#include "base/file_util.h"
 #include "base/files/file_path.h"
+#include "base/files/file_util.h"
 #include "base/location.h"
 #include "base/message_loop/message_loop_proxy.h"
 #include "base/stl_util.h"
@@ -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"
@@ -76,6 +77,22 @@ DeviceType DeviceMediaTypeToDeviceType(uint32 media_type_uint32) {
   }
 }
 
+bool ReadMountEntryFromDbus(dbus::MessageReader* reader, MountEntry* entry) {
+  uint32 error_code = 0;
+  std::string source_path;
+  uint32 mount_type = 0;
+  std::string mount_path;
+  if (!reader->PopUint32(&error_code) ||
+      !reader->PopString(&source_path) ||
+      !reader->PopUint32(&mount_type) ||
+      !reader->PopString(&mount_path)) {
+    return false;
+  }
+  *entry = MountEntry(static_cast<MountError>(error_code), source_path,
+                      static_cast<MountType>(mount_type), mount_path);
+  return true;
+}
+
 // The CrosDisksClient implementation.
 class CrosDisksClientImpl : public CrosDisksClient {
  public:
@@ -86,7 +103,7 @@ class CrosDisksClientImpl : public CrosDisksClient {
                      const std::string& source_format,
                      const std::string& mount_label,
                      const base::Closure& callback,
-                     const base::Closure& error_callback) OVERRIDE {
+                     const base::Closure& error_callback) override {
     dbus::MethodCall method_call(cros_disks::kCrosDisksInterface,
                                  cros_disks::kMount);
     dbus::MessageWriter writer(&method_call);
@@ -113,7 +130,7 @@ class CrosDisksClientImpl : public CrosDisksClient {
   virtual void Unmount(const std::string& device_path,
                        UnmountOptions options,
                        const base::Closure& callback,
-                       const base::Closure& error_callback) OVERRIDE {
+                       const base::Closure& error_callback) override {
     dbus::MethodCall method_call(cros_disks::kCrosDisksInterface,
                                  cros_disks::kUnmount);
     dbus::MessageWriter writer(&method_call);
@@ -136,7 +153,7 @@ class CrosDisksClientImpl : public CrosDisksClient {
   // CrosDisksClient override.
   virtual void EnumerateAutoMountableDevices(
       const EnumerateAutoMountableDevicesCallback& callback,
-      const base::Closure& error_callback) OVERRIDE {
+      const base::Closure& error_callback) override {
     dbus::MethodCall method_call(cros_disks::kCrosDisksInterface,
                                  cros_disks::kEnumerateAutoMountableDevices);
     proxy_->CallMethod(
@@ -148,10 +165,24 @@ class CrosDisksClientImpl : public CrosDisksClient {
   }
 
   // CrosDisksClient override.
+  virtual void EnumerateMountEntries(
+      const EnumerateMountEntriesCallback& callback,
+      const base::Closure& error_callback) override {
+    dbus::MethodCall method_call(cros_disks::kCrosDisksInterface,
+                                 cros_disks::kEnumerateMountEntries);
+    proxy_->CallMethod(
+        &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
+        base::Bind(&CrosDisksClientImpl::OnEnumerateMountEntries,
+                   weak_ptr_factory_.GetWeakPtr(),
+                   callback,
+                   error_callback));
+  }
+
+  // CrosDisksClient override.
   virtual void Format(const std::string& device_path,
                       const std::string& filesystem,
                       const base::Closure& callback,
-                      const base::Closure& error_callback) OVERRIDE {
+                      const base::Closure& error_callback) override {
     dbus::MethodCall method_call(cros_disks::kCrosDisksInterface,
                                  cros_disks::kFormat);
     dbus::MessageWriter writer(&method_call);
@@ -172,7 +203,7 @@ class CrosDisksClientImpl : public CrosDisksClient {
   virtual void GetDeviceProperties(
       const std::string& device_path,
       const GetDevicePropertiesCallback& callback,
-      const base::Closure& error_callback) OVERRIDE {
+      const base::Closure& error_callback) override {
     dbus::MethodCall method_call(cros_disks::kCrosDisksInterface,
                                  cros_disks::kGetDeviceProperties);
     dbus::MessageWriter writer(&method_call);
@@ -188,7 +219,7 @@ class CrosDisksClientImpl : public CrosDisksClient {
 
   // CrosDisksClient override.
   virtual void SetMountEventHandler(
-      const MountEventHandler& mount_event_handler) OVERRIDE {
+      const MountEventHandler& mount_event_handler) override {
     static const SignalEventTuple kSignalEventTuples[] = {
       { cros_disks::kDeviceAdded, CROS_DISKS_DEVICE_ADDED },
       { cros_disks::kDeviceScanned, CROS_DISKS_DEVICE_SCANNED },
@@ -214,7 +245,7 @@ class CrosDisksClientImpl : public CrosDisksClient {
 
   // CrosDisksClient override.
   virtual void SetMountCompletedHandler(
-      const MountCompletedHandler& mount_completed_handler) OVERRIDE {
+      const MountCompletedHandler& mount_completed_handler) override {
     proxy_->ConnectToSignal(
         cros_disks::kCrosDisksInterface,
         cros_disks::kMountCompleted,
@@ -227,7 +258,7 @@ class CrosDisksClientImpl : public CrosDisksClient {
 
   // CrosDisksClient override.
   virtual void SetFormatCompletedHandler(
-      const FormatCompletedHandler& format_completed_handler) OVERRIDE {
+      const FormatCompletedHandler& format_completed_handler) override {
     proxy_->ConnectToSignal(
         cros_disks::kCrosDisksInterface,
         cros_disks::kFormatCompleted,
@@ -239,7 +270,7 @@ class CrosDisksClientImpl : public CrosDisksClient {
   }
 
  protected:
-  virtual void Init(dbus::Bus* bus) OVERRIDE {
+  virtual void Init(dbus::Bus* bus) override {
     proxy_ = bus->GetObjectProxy(
         cros_disks::kCrosDisksServiceName,
         dbus::ObjectPath(cros_disks::kCrosDisksServicePath));
@@ -264,7 +295,7 @@ class CrosDisksClientImpl : public CrosDisksClient {
     callback.Run();
   }
 
-  // Handles the result of Unount and calls |callback| or |error_callback|.
+  // Handles the result of Unmount and calls |callback| or |error_callback|.
   void OnUnmount(const base::Closure& callback,
                  const base::Closure& error_callback,
                  dbus::Response* response) {
@@ -312,6 +343,40 @@ class CrosDisksClientImpl : public CrosDisksClient {
     callback.Run(device_paths);
   }
 
+  // Handles the result of EnumerateMountEntries and calls |callback| or
+  // |error_callback|.
+  void OnEnumerateMountEntries(
+      const EnumerateMountEntriesCallback& callback,
+      const base::Closure& error_callback,
+      dbus::Response* response) {
+    if (!response) {
+      error_callback.Run();
+      return;
+    }
+
+    dbus::MessageReader reader(response);
+    dbus::MessageReader array_reader(NULL);
+    if (!reader.PopArray(&array_reader)) {
+      LOG(ERROR) << "Invalid response: " << response->ToString();
+      error_callback.Run();
+      return;
+    }
+
+    std::vector<MountEntry> entries;
+    while (array_reader.HasMoreData()) {
+      MountEntry entry;
+      dbus::MessageReader sub_reader(NULL);
+      if (!array_reader.PopStruct(&sub_reader) ||
+          !ReadMountEntryFromDbus(&sub_reader, &entry)) {
+        LOG(ERROR) << "Invalid response: " << response->ToString();
+        error_callback.Run();
+        return;
+      }
+      entries.push_back(entry);
+    }
+    callback.Run(entries);
+  }
+
   // Handles the result of Format and calls |callback| or |error_callback|.
   void OnFormat(const base::Closure& callback,
                 const base::Closure& error_callback,
@@ -353,19 +418,12 @@ class CrosDisksClientImpl : public CrosDisksClient {
   // Handles MountCompleted signal and calls |handler|.
   void OnMountCompleted(MountCompletedHandler handler, dbus::Signal* signal) {
     dbus::MessageReader reader(signal);
-    uint32 error_code = 0;
-    std::string source_path;
-    uint32 mount_type = 0;
-    std::string mount_path;
-    if (!reader.PopUint32(&error_code) ||
-        !reader.PopString(&source_path) ||
-        !reader.PopUint32(&mount_type) ||
-        !reader.PopString(&mount_path)) {
+    MountEntry entry;
+    if (!ReadMountEntryFromDbus(&reader, &entry)) {
       LOG(ERROR) << "Invalid signal: " << signal->ToString();
       return;
     }
-    handler.Run(static_cast<MountError>(error_code), source_path,
-                static_cast<MountType>(mount_type), mount_path);
+    handler.Run(entry);
   }
 
   // Handles FormatCompleted signal and calls |handler|.
@@ -397,180 +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 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_,
-                     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
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -581,6 +465,7 @@ DiskInfo::DiskInfo(const std::string& device_path, dbus::Response* response)
       is_drive_(false),
       has_media_(false),
       on_boot_device_(false),
+      on_removable_device_(false),
       device_type_(DEVICE_TYPE_UNKNOWN),
       total_size_in_bytes_(0),
       is_read_only_(false),
@@ -624,6 +509,10 @@ DiskInfo::~DiskInfo() {
 //     variant       bool false
 //   }
 //   dict entry {
+//     string "DeviceIsOnRemovableDevice"
+//     variant       bool true
+//   }
+//   dict entry {
 //     string "DeviceIsReadOnly"
 //     variant       bool false
 //   }
@@ -702,6 +591,8 @@ void DiskInfo::InitializeFromResponse(dbus::Response* response) {
       cros_disks::kDeviceIsMediaAvailable, &has_media_);
   properties->GetBooleanWithoutPathExpansion(
       cros_disks::kDeviceIsOnBootDevice, &on_boot_device_);
+  properties->GetBooleanWithoutPathExpansion(
+      cros_disks::kDeviceIsOnRemovableDevice, &on_removable_device_);
   properties->GetStringWithoutPathExpansion(
       cros_disks::kNativePath, &system_path_);
   properties->GetStringWithoutPathExpansion(
@@ -750,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