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_SYSTEM_MONITOR_PORTABLE_DEVICE_WATCHER_WIN_H_
6 #define CHROME_BROWSER_SYSTEM_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/string16.h"
17 #include "base/system_monitor/system_monitor.h"
20 class SequencedTaskRunner
;
27 // This class watches the portable device mount points and sends notifications
28 // to base::SystemMonitor about the attached/detached media transfer protocol
29 // (MTP) devices. This is a singleton class instantiated by
30 // RemovableDeviceNotificationsWindowWin. This class is created, destroyed and
31 // operates on the UI thread, except for long running tasks it spins off to a
32 // SequencedTaskRunner.
33 class PortableDeviceWatcherWin
{
35 typedef std::vector
<string16
> StorageObjectIDs
;
37 struct DeviceStorageObject
{
38 DeviceStorageObject(const string16
& temporary_id
,
39 const std::string
& persistent_id
);
41 // Storage object temporary identifier, e.g. "s10001".
42 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.
58 // Device storage details. A device can have multiple data partitions.
59 StorageObjects storage_objects
;
61 typedef std::vector
<DeviceDetails
> Devices
;
63 PortableDeviceWatcherWin();
64 virtual ~PortableDeviceWatcherWin();
66 // Must be called after the browser blocking pool is ready for use.
67 // RemovableDeviceNotificationsWindowsWin::Init() will call this function.
70 // Processes DEV_BROADCAST_DEVICEINTERFACE messages and triggers a
71 // SystemMonitor notification if appropriate.
72 void OnWindowMessage(UINT event_type
, LPARAM data
);
75 friend class TestPortableDeviceWatcherWin
;
77 // Key: MTP device storage unique id.
78 // Value: Metadata for the given storage.
79 typedef std::map
<std::string
, base::SystemMonitor::RemovableStorageInfo
>
82 // Key: MTP device plug and play ID string.
83 // Value: Vector of device storage objects.
84 typedef std::map
<string16
, StorageObjects
> MTPDeviceMap
;
86 // Helpers to enumerate existing MTP storage devices.
87 virtual void EnumerateAttachedDevices();
88 virtual void OnDidEnumerateAttachedDevices(const Devices
* devices
,
91 // Helpers to handle device attach event.
92 virtual void HandleDeviceAttachEvent(const string16
& pnp_device_id
);
93 virtual void OnDidHandleDeviceAttachEvent(
94 const DeviceDetails
* device_details
, const bool result
);
96 // Handles the detach event of the device specified by |pnp_device_id|.
97 void HandleDeviceDetachEvent(const string16
& pnp_device_id
);
99 // The portable device notifications handle.
100 HDEVNOTIFY notifications_
;
102 // Attached media transfer protocol device map.
103 MTPDeviceMap device_map_
;
105 // Attached media transfer protocol device storage objects map.
106 MTPStorageMap storage_map_
;
108 // The task runner used to execute tasks that may take a long time and thus
109 // should not be performed on the UI thread.
110 scoped_refptr
<base::SequencedTaskRunner
> media_task_runner_
;
112 // Used by |media_task_runner_| to create cancelable callbacks.
113 base::WeakPtrFactory
<PortableDeviceWatcherWin
> weak_ptr_factory_
;
115 DISALLOW_COPY_AND_ASSIGN(PortableDeviceWatcherWin
);
118 } // namespace chrome
120 #endif // CHROME_BROWSER_SYSTEM_MONITOR_PORTABLE_DEVICE_WATCHER_WIN_H_