Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / storage_monitor / portable_device_watcher_win.h
blobe893d1dcd5fc6009cbf53ee9d41f6157057ad383
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>
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 "chrome/browser/storage_monitor/storage_monitor.h"
19 namespace base {
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 {
31 public:
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
41 // property.
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 {
52 // Device name.
53 base::string16 name;
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
64 // an argument.
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.
70 void Init(HWND hwnd);
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
79 // identifier.
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
90 // devices are found.
91 void SetNotifications(StorageMonitor::Receiver* notifications);
93 void EjectDevice(const std::string& device_id,
94 base::Callback<void(StorageMonitor::EjectStatus)> callback);
96 private:
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,
110 const bool result);
112 // Helpers to handle device attach event.
113 virtual void HandleDeviceAttachEvent(const base::string16& pnp_device_id);
114 void OnDidHandleDeviceAttachEvent(const DeviceDetails* device_details,
115 const bool result);
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_