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
{
57 // Device interface path.
58 base::string16 location
;
60 // Device storage details. A device can have multiple data partitions.
61 StorageObjects storage_objects
;
63 typedef std::vector
<DeviceDetails
> Devices
;
65 // TODO(gbillock): Change to take the device notifications object as
67 PortableDeviceWatcherWin();
68 virtual ~PortableDeviceWatcherWin();
70 // Must be called after the browser blocking pool is ready for use.
71 // StorageMonitorWin::Init() will call this function.
74 // Processes DEV_BROADCAST_DEVICEINTERFACE messages and triggers a
75 // notification if appropriate.
76 void OnWindowMessage(UINT event_type
, LPARAM data
);
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
82 virtual bool GetMTPStorageInfoFromDeviceId(
83 const std::string
& storage_device_id
,
84 base::string16
* device_location
,
85 base::string16
* storage_object_id
) const;
87 // Constructs and returns a storage path from storage unique identifier.
88 static base::string16
GetStoragePathFromStorageId(
89 const std::string
& storage_unique_id
);
91 // Set the volume notifications object to be used when new
93 void SetNotifications(StorageMonitor::Receiver
* notifications
);
95 void EjectDevice(const std::string
& device_id
,
96 base::Callback
<void(StorageMonitor::EjectStatus
)> callback
);
99 friend class TestPortableDeviceWatcherWin
;
101 // Key: MTP device storage unique id.
102 // Value: Metadata for the given storage.
103 typedef std::map
<std::string
, StorageInfo
> MTPStorageMap
;
105 // Key: MTP device plug and play ID string.
106 // Value: Vector of device storage objects.
107 typedef std::map
<base::string16
, StorageObjects
> MTPDeviceMap
;
109 // Helpers to enumerate existing MTP storage devices.
110 virtual void EnumerateAttachedDevices();
111 void OnDidEnumerateAttachedDevices(const Devices
* devices
,
114 // Helpers to handle device attach event.
115 virtual void HandleDeviceAttachEvent(const base::string16
& pnp_device_id
);
116 void OnDidHandleDeviceAttachEvent(const DeviceDetails
* device_details
,
119 // Handles the detach event of the device specified by |pnp_device_id|.
120 void HandleDeviceDetachEvent(const base::string16
& pnp_device_id
);
122 // The portable device notifications handle.
123 HDEVNOTIFY notifications_
;
125 // Attached media transfer protocol device map.
126 MTPDeviceMap device_map_
;
128 // Attached media transfer protocol device storage objects map.
129 MTPStorageMap storage_map_
;
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_
;
135 // The notifications object to use to signal newly attached devices.
136 StorageMonitor::Receiver
* storage_notifications_
;
138 // Used by |media_task_runner_| to create cancelable callbacks.
139 base::WeakPtrFactory
<PortableDeviceWatcherWin
> weak_ptr_factory_
;
141 DISALLOW_COPY_AND_ASSIGN(PortableDeviceWatcherWin
);
144 } // namespace storage_monitor
146 #endif // COMPONENTS_STORAGE_MONITOR_PORTABLE_DEVICE_WATCHER_WIN_H_