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.
5 #ifndef COMPONENTS_STORAGE_MONITOR_PORTABLE_DEVICE_WATCHER_WIN_H_
6 #define COMPONENTS_STORAGE_MONITOR_PORTABLE_DEVICE_WATCHER_WIN_H_
8 #include <portabledeviceapi.h>
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"
20 class SequencedTaskRunner
;
23 namespace storage_monitor
{
25 class TestPortableDeviceWatcherWin
;
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
{
34 typedef std::vector
<base::string16
> StorageObjectIDs
;
36 struct DeviceStorageObject
{
37 DeviceStorageObject(const base::string16
& temporary_id
,
38 const std::string
& persistent_id
);
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
44 base::string16 object_temporary_id
;
46 // Storage object persistent identifier,
47 // e.g. "StorageSerial:<SID-{10001,D,31080448}>:<123456789>".
48 std::string object_persistent_id
;
50 typedef std::vector
<DeviceStorageObject
> StorageObjects
;
52 // Struct to store attached MTP device details.
53 struct DeviceDetails
{
60 // Device interface path.
61 base::string16 location
;
63 // Device storage details. A device can have multiple data partitions.
64 StorageObjects storage_objects
;
66 typedef std::vector
<DeviceDetails
> Devices
;
68 // TODO(gbillock): Change to take the device notifications object as
70 PortableDeviceWatcherWin();
71 virtual ~PortableDeviceWatcherWin();
73 // Must be called after the browser blocking pool is ready for use.
74 // StorageMonitorWin::Init() will call this function.
77 // Processes DEV_BROADCAST_DEVICEINTERFACE messages and triggers a
78 // notification if appropriate.
79 void OnWindowMessage(UINT event_type
, LPARAM data
);
81 // Gets the information of the MTP storage specified by |storage_device_id|.
82 // On success, returns true and fills in |device_location| with device
83 // interface details and |storage_object_id| with storage object temporary
85 virtual bool GetMTPStorageInfoFromDeviceId(
86 const std::string
& storage_device_id
,
87 base::string16
* device_location
,
88 base::string16
* storage_object_id
) const;
90 // Constructs and returns a storage path from storage unique identifier.
91 static base::string16
GetStoragePathFromStorageId(
92 const std::string
& storage_unique_id
);
94 // Set the volume notifications object to be used when new
96 void SetNotifications(StorageMonitor::Receiver
* notifications
);
98 void EjectDevice(const std::string
& device_id
,
99 base::Callback
<void(StorageMonitor::EjectStatus
)> callback
);
102 friend class TestPortableDeviceWatcherWin
;
104 // Key: MTP device storage unique id.
105 // Value: Metadata for the given storage.
106 typedef std::map
<std::string
, StorageInfo
> MTPStorageMap
;
108 // Key: MTP device plug and play ID string.
109 // Value: Vector of device storage objects.
110 typedef std::map
<base::string16
, StorageObjects
> MTPDeviceMap
;
112 // Helpers to enumerate existing MTP storage devices.
113 virtual void EnumerateAttachedDevices();
114 void OnDidEnumerateAttachedDevices(const Devices
* devices
,
117 // Helpers to handle device attach event.
118 virtual void HandleDeviceAttachEvent(const base::string16
& pnp_device_id
);
119 void OnDidHandleDeviceAttachEvent(const DeviceDetails
* device_details
,
122 // Handles the detach event of the device specified by |pnp_device_id|.
123 void HandleDeviceDetachEvent(const base::string16
& pnp_device_id
);
125 // The portable device notifications handle.
126 HDEVNOTIFY notifications_
;
128 // Attached media transfer protocol device map.
129 MTPDeviceMap device_map_
;
131 // Attached media transfer protocol device storage objects map.
132 MTPStorageMap storage_map_
;
134 // The task runner used to execute tasks that may take a long time and thus
135 // should not be performed on the UI thread.
136 scoped_refptr
<base::SequencedTaskRunner
> media_task_runner_
;
138 // The notifications object to use to signal newly attached devices.
139 StorageMonitor::Receiver
* storage_notifications_
;
141 // Used by |media_task_runner_| to create cancelable callbacks.
142 base::WeakPtrFactory
<PortableDeviceWatcherWin
> weak_ptr_factory_
;
144 DISALLOW_COPY_AND_ASSIGN(PortableDeviceWatcherWin
);
147 } // namespace storage_monitor
149 #endif // COMPONENTS_STORAGE_MONITOR_PORTABLE_DEVICE_WATCHER_WIN_H_