1 // Copyright (c) 2012 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 CHROME_BROWSER_STORAGE_MONITOR_PORTABLE_DEVICE_WATCHER_WIN_H_
6 #define CHROME_BROWSER_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 "chrome/browser/storage_monitor/storage_monitor.h"
20 class SequencedTaskRunner
;
23 class TestPortableDeviceWatcherWin
;
25 // This class watches the portable device mount points and sends notifications
26 // about the attached/detached media transfer protocol (MTP) devices.
27 // This is a singleton class instantiated by StorageMonitorWin. This class is
28 // created, destroyed and operates on the UI thread, except for long running
29 // tasks it spins off to a SequencedTaskRunner.
30 class PortableDeviceWatcherWin
{
32 typedef std::vector
<base::string16
> StorageObjectIDs
;
34 struct DeviceStorageObject
{
35 DeviceStorageObject(const base::string16
& temporary_id
,
36 const std::string
& persistent_id
);
38 // Storage object temporary identifier, e.g. "s10001". This string ID
39 // uniquely identifies the object on the device. This ID need not be
40 // persistent across sessions. This ID is obtained from WPD_OBJECT_ID
42 base::string16 object_temporary_id
;
44 // Storage object persistent identifier,
45 // e.g. "StorageSerial:<SID-{10001,D,31080448}>:<123456789>".
46 std::string object_persistent_id
;
48 typedef std::vector
<DeviceStorageObject
> StorageObjects
;
50 // Struct to store attached MTP device details.
51 struct DeviceDetails
{
55 // Device interface path.
56 base::string16 location
;
58 // Device storage details. A device can have multiple data partitions.
59 StorageObjects storage_objects
;
61 typedef std::vector
<DeviceDetails
> Devices
;
63 // TODO(gbillock): Change to take the device notifications object as
65 PortableDeviceWatcherWin();
66 virtual ~PortableDeviceWatcherWin();
68 // Must be called after the browser blocking pool is ready for use.
69 // StorageMonitorWin::Init() will call this function.
72 // Processes DEV_BROADCAST_DEVICEINTERFACE messages and triggers a
73 // notification if appropriate.
74 void OnWindowMessage(UINT event_type
, LPARAM data
);
76 // Gets the information of the MTP storage specified by |storage_device_id|.
77 // On success, returns true and fills in |device_location| with device
78 // interface details and |storage_object_id| with storage object temporary
80 virtual bool GetMTPStorageInfoFromDeviceId(
81 const std::string
& storage_device_id
,
82 base::string16
* device_location
,
83 base::string16
* storage_object_id
) const;
85 // Constructs and returns a storage path from storage unique identifier.
86 static base::string16
GetStoragePathFromStorageId(
87 const std::string
& storage_unique_id
);
89 // Set the volume notifications object to be used when new
91 void SetNotifications(StorageMonitor::Receiver
* notifications
);
93 void EjectDevice(const std::string
& device_id
,
94 base::Callback
<void(StorageMonitor::EjectStatus
)> callback
);
97 friend class TestPortableDeviceWatcherWin
;
99 // Key: MTP device storage unique id.
100 // Value: Metadata for the given storage.
101 typedef std::map
<std::string
, StorageInfo
> MTPStorageMap
;
103 // Key: MTP device plug and play ID string.
104 // Value: Vector of device storage objects.
105 typedef std::map
<base::string16
, StorageObjects
> MTPDeviceMap
;
107 // Helpers to enumerate existing MTP storage devices.
108 virtual void EnumerateAttachedDevices();
109 void OnDidEnumerateAttachedDevices(const Devices
* devices
,
112 // Helpers to handle device attach event.
113 virtual void HandleDeviceAttachEvent(const base::string16
& pnp_device_id
);
114 void OnDidHandleDeviceAttachEvent(const DeviceDetails
* device_details
,
117 // Handles the detach event of the device specified by |pnp_device_id|.
118 void HandleDeviceDetachEvent(const base::string16
& pnp_device_id
);
120 // The portable device notifications handle.
121 HDEVNOTIFY notifications_
;
123 // Attached media transfer protocol device map.
124 MTPDeviceMap device_map_
;
126 // Attached media transfer protocol device storage objects map.
127 MTPStorageMap storage_map_
;
129 // The task runner used to execute tasks that may take a long time and thus
130 // should not be performed on the UI thread.
131 scoped_refptr
<base::SequencedTaskRunner
> media_task_runner_
;
133 // Used by |media_task_runner_| to create cancelable callbacks.
134 base::WeakPtrFactory
<PortableDeviceWatcherWin
> weak_ptr_factory_
;
136 // The notifications object to use to signal newly attached devices.
137 StorageMonitor::Receiver
* storage_notifications_
;
139 DISALLOW_COPY_AND_ASSIGN(PortableDeviceWatcherWin
);
142 #endif // CHROME_BROWSER_STORAGE_MONITOR_PORTABLE_DEVICE_WATCHER_WIN_H_