Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / storage_monitor / portable_device_watcher_win.h
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.
4
5 #ifndef CHROME_BROWSER_STORAGE_MONITOR_PORTABLE_DEVICE_WATCHER_WIN_H_
6 #define CHROME_BROWSER_STORAGE_MONITOR_PORTABLE_DEVICE_WATCHER_WIN_H_
7
8 #include <portabledeviceapi.h>
9
10 #include <map>
11 #include <string>
12 #include <vector>
13
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"
18
19 namespace base {
20 class SequencedTaskRunner;
21 }
22
23 class TestPortableDeviceWatcherWin;
24
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;
33
34   struct DeviceStorageObject {
35     DeviceStorageObject(const base::string16& temporary_id,
36                         const std::string& persistent_id);
37
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;
43
44     // Storage object persistent identifier,
45     // e.g. "StorageSerial:<SID-{10001,D,31080448}>:<123456789>".
46     std::string object_persistent_id;
47   };
48   typedef std::vector<DeviceStorageObject> StorageObjects;
49
50   // Struct to store attached MTP device details.
51   struct DeviceDetails {
52     // Device name.
53     base::string16 name;
54
55     // Device interface path.
56     base::string16 location;
57
58     // Device storage details. A device can have multiple data partitions.
59     StorageObjects storage_objects;
60   };
61   typedef std::vector<DeviceDetails> Devices;
62
63   // TODO(gbillock): Change to take the device notifications object as
64   // an argument.
65   PortableDeviceWatcherWin();
66   virtual ~PortableDeviceWatcherWin();
67
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);
71
72   // Processes DEV_BROADCAST_DEVICEINTERFACE messages and triggers a
73   // notification if appropriate.
74   void OnWindowMessage(UINT event_type, LPARAM data);
75
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;
84
85   // Constructs and returns a storage path from storage unique identifier.
86   static base::string16 GetStoragePathFromStorageId(
87       const std::string& storage_unique_id);
88
89   // Set the volume notifications object to be used when new
90   // devices are found.
91   void SetNotifications(StorageMonitor::Receiver* notifications);
92
93   void EjectDevice(const std::string& device_id,
94                    base::Callback<void(StorageMonitor::EjectStatus)> callback);
95
96  private:
97   friend class TestPortableDeviceWatcherWin;
98
99   // Key: MTP device storage unique id.
100   // Value: Metadata for the given storage.
101   typedef std::map<std::string, StorageInfo> MTPStorageMap;
102
103   // Key: MTP device plug and play ID string.
104   // Value: Vector of device storage objects.
105   typedef std::map<base::string16, StorageObjects> MTPDeviceMap;
106
107   // Helpers to enumerate existing MTP storage devices.
108   virtual void EnumerateAttachedDevices();
109   void OnDidEnumerateAttachedDevices(const Devices* devices,
110                                      const bool result);
111
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);
116
117   // Handles the detach event of the device specified by |pnp_device_id|.
118   void HandleDeviceDetachEvent(const base::string16& pnp_device_id);
119
120   // The portable device notifications handle.
121   HDEVNOTIFY notifications_;
122
123   // Attached media transfer protocol device map.
124   MTPDeviceMap device_map_;
125
126   // Attached media transfer protocol device storage objects map.
127   MTPStorageMap storage_map_;
128
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_;
132
133   // Used by |media_task_runner_| to create cancelable callbacks.
134   base::WeakPtrFactory<PortableDeviceWatcherWin> weak_ptr_factory_;
135
136   // The notifications object to use to signal newly attached devices.
137   StorageMonitor::Receiver* storage_notifications_;
138
139   DISALLOW_COPY_AND_ASSIGN(PortableDeviceWatcherWin);
140 };
141
142 #endif  // CHROME_BROWSER_STORAGE_MONITOR_PORTABLE_DEVICE_WATCHER_WIN_H_