Home | History | Annotate | Download | only in storage_monitor
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef COMPONENTS_STORAGE_MONITOR_PORTABLE_DEVICE_WATCHER_WIN_H_
      6 #define COMPONENTS_STORAGE_MONITOR_PORTABLE_DEVICE_WATCHER_WIN_H_
      7 
      8 #include <portabledeviceapi.h>
      9 
     10 #include <map>
     11 #include <string>
     12 #include <vector>
     13 
     14 #include "base/memory/ref_counted.h"
     15 #include "base/memory/weak_ptr.h"
     16 #include "base/strings/string16.h"
     17 #include "components/storage_monitor/storage_monitor.h"
     18 
     19 namespace base {
     20 class SequencedTaskRunner;
     21 }
     22 
     23 namespace storage_monitor {
     24 
     25 class TestPortableDeviceWatcherWin;
     26 
     27 // This class watches the portable device mount points and sends notifications
     28 // about the attached/detached media transfer protocol (MTP) devices.
     29 // This is a singleton class instantiated by StorageMonitorWin. This class is
     30 // created, destroyed and operates on the UI thread, except for long running
     31 // tasks it spins off to a SequencedTaskRunner.
     32 class PortableDeviceWatcherWin {
     33  public:
     34   typedef std::vector<base::string16> StorageObjectIDs;
     35 
     36   struct DeviceStorageObject {
     37     DeviceStorageObject(const base::string16& temporary_id,
     38                         const std::string& persistent_id);
     39 
     40     // Storage object temporary identifier, e.g. "s10001". This string ID
     41     // uniquely identifies the object on the device. This ID need not be
     42     // persistent across sessions. This ID is obtained from WPD_OBJECT_ID
     43     // property.
     44     base::string16 object_temporary_id;
     45 
     46     // Storage object persistent identifier,
     47     // e.g. "StorageSerial:<SID-{10001,D,31080448}>:<123456789>".
     48     std::string object_persistent_id;
     49   };
     50   typedef std::vector<DeviceStorageObject> StorageObjects;
     51 
     52   // Struct to store attached MTP device details.
     53   struct DeviceDetails {
     54     // Device name.
     55     base::string16 name;
     56 
     57     // Device interface path.
     58     base::string16 location;
     59 
     60     // Device storage details. A device can have multiple data partitions.
     61     StorageObjects storage_objects;
     62   };
     63   typedef std::vector<DeviceDetails> Devices;
     64 
     65   // TODO(gbillock): Change to take the device notifications object as
     66   // an argument.
     67   PortableDeviceWatcherWin();
     68   virtual ~PortableDeviceWatcherWin();
     69 
     70   // Must be called after the browser blocking pool is ready for use.
     71   // StorageMonitorWin::Init() will call this function.
     72   void Init(HWND hwnd);
     73 
     74   // Processes DEV_BROADCAST_DEVICEINTERFACE messages and triggers a
     75   // notification if appropriate.
     76   void OnWindowMessage(UINT event_type, LPARAM data);
     77 
     78   // Gets the information of the MTP storage specified by |storage_device_id|.
     79   // On success, returns true and fills in |device_location| with device
     80   // interface details and |storage_object_id| with storage object temporary
     81   // identifier.
     82   virtual bool GetMTPStorageInfoFromDeviceId(
     83       const std::string& storage_device_id,
     84       base::string16* device_location,
     85       base::string16* storage_object_id) const;
     86 
     87   // Constructs and returns a storage path from storage unique identifier.
     88   static base::string16 GetStoragePathFromStorageId(
     89       const std::string& storage_unique_id);
     90 
     91   // Set the volume notifications object to be used when new
     92   // devices are found.
     93   void SetNotifications(StorageMonitor::Receiver* notifications);
     94 
     95   void EjectDevice(const std::string& device_id,
     96                    base::Callback<void(StorageMonitor::EjectStatus)> callback);
     97 
     98  private:
     99   friend class TestPortableDeviceWatcherWin;
    100 
    101   // Key: MTP device storage unique id.
    102   // Value: Metadata for the given storage.
    103   typedef std::map<std::string, StorageInfo> MTPStorageMap;
    104 
    105   // Key: MTP device plug and play ID string.
    106   // Value: Vector of device storage objects.
    107   typedef std::map<base::string16, StorageObjects> MTPDeviceMap;
    108 
    109   // Helpers to enumerate existing MTP storage devices.
    110   virtual void EnumerateAttachedDevices();
    111   void OnDidEnumerateAttachedDevices(const Devices* devices,
    112                                      const bool result);
    113 
    114   // Helpers to handle device attach event.
    115   virtual void HandleDeviceAttachEvent(const base::string16& pnp_device_id);
    116   void OnDidHandleDeviceAttachEvent(const DeviceDetails* device_details,
    117                                     const bool result);
    118 
    119   // Handles the detach event of the device specified by |pnp_device_id|.
    120   void HandleDeviceDetachEvent(const base::string16& pnp_device_id);
    121 
    122   // The portable device notifications handle.
    123   HDEVNOTIFY notifications_;
    124 
    125   // Attached media transfer protocol device map.
    126   MTPDeviceMap device_map_;
    127 
    128   // Attached media transfer protocol device storage objects map.
    129   MTPStorageMap storage_map_;
    130 
    131   // The task runner used to execute tasks that may take a long time and thus
    132   // should not be performed on the UI thread.
    133   scoped_refptr<base::SequencedTaskRunner> media_task_runner_;
    134 
    135   // The notifications object to use to signal newly attached devices.
    136   StorageMonitor::Receiver* storage_notifications_;
    137 
    138   // Used by |media_task_runner_| to create cancelable callbacks.
    139   base::WeakPtrFactory<PortableDeviceWatcherWin> weak_ptr_factory_;
    140 
    141   DISALLOW_COPY_AND_ASSIGN(PortableDeviceWatcherWin);
    142 };
    143 
    144 }  // namespace storage_monitor
    145 
    146 #endif  // COMPONENTS_STORAGE_MONITOR_PORTABLE_DEVICE_WATCHER_WIN_H_
    147