Roll src/third_party/WebKit f298044:aa8346d (svn 202628:202629)
[chromium-blink-merge.git] / components / storage_monitor / portable_device_watcher_win.h
blob4708c811d4a530b634f045f34e82a30cb2b5b713
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>
10 #include <map>
11 #include <string>
12 #include <vector>
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"
19 namespace base {
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 {
33 public:
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
43 // property.
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 {
54 DeviceDetails();
55 ~DeviceDetails();
57 // Device name.
58 base::string16 name;
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
69 // an argument.
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.
75 void Init(HWND hwnd);
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
84 // identifier.
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
95 // devices are found.
96 void SetNotifications(StorageMonitor::Receiver* notifications);
98 void EjectDevice(const std::string& device_id,
99 base::Callback<void(StorageMonitor::EjectStatus)> callback);
101 private:
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,
115 const bool result);
117 // Helpers to handle device attach event.
118 virtual void HandleDeviceAttachEvent(const base::string16& pnp_device_id);
119 void OnDidHandleDeviceAttachEvent(const DeviceDetails* device_details,
120 const bool result);
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_