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_VOLUME_MOUNT_WATCHER_WIN_H_
6 #define COMPONENTS_STORAGE_MONITOR_VOLUME_MOUNT_WATCHER_WIN_H_
13 #include "base/basictypes.h"
14 #include "base/callback.h"
15 #include "base/files/file_path.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/sequenced_task_runner.h"
18 #include "base/strings/string16.h"
19 #include "components/storage_monitor/storage_info.h"
20 #include "components/storage_monitor/storage_monitor.h"
22 namespace storage_monitor
{
24 class TestVolumeMountWatcherWin
;
26 // This class watches the volume mount points and sends notifications to
27 // StorageMonitor about the device attach/detach events.
28 // This is a singleton class instantiated by StorageMonitorWin.
29 class VolumeMountWatcherWin
{
31 VolumeMountWatcherWin();
32 virtual ~VolumeMountWatcherWin();
34 // Returns the volume file path of the drive specified by the |drive_number|.
35 // |drive_number| inputs of 0 - 25 are valid. Returns an empty file path if
36 // the |drive_number| is invalid.
37 static base::FilePath
DriveNumberToFilePath(int drive_number
);
41 // Gets the information about the device mounted at |device_path|. On success,
42 // returns true and fills in |info|.
43 // Can block during startup while device info is still loading.
44 bool GetDeviceInfo(const base::FilePath
& device_path
,
45 StorageInfo
* info
) const;
47 // Processes DEV_BROADCAST_VOLUME messages and triggers a
48 // notification if appropriate.
49 void OnWindowMessage(UINT event_type
, LPARAM data
);
51 // Processes SHCNE_MEDIAINSERTED (and REMOVED).
52 void OnMediaChange(WPARAM wparam
, LPARAM lparam
);
54 // Set the volume notifications object to be used when new
55 // removable volumes are found.
56 void SetNotifications(StorageMonitor::Receiver
* notifications
);
58 void EjectDevice(const std::string
& device_id
,
59 base::Callback
<void(StorageMonitor::EjectStatus
)> callback
);
62 typedef base::Callback
<bool(const base::FilePath
&,
63 StorageInfo
*)> GetDeviceDetailsCallbackType
;
65 typedef base::Callback
<std::vector
<base::FilePath
>(void)>
66 GetAttachedDevicesCallbackType
;
68 // Handles mass storage device attach event on UI thread.
69 void HandleDeviceAttachEventOnUIThread(
70 const base::FilePath
& device_path
,
71 const StorageInfo
& info
);
73 // Handles mass storage device detach event on UI thread.
74 void HandleDeviceDetachEventOnUIThread(const base::string16
& device_location
);
76 // UI thread delegate to set up adding storage devices.
77 void AddDevicesOnUIThread(std::vector
<base::FilePath
> removable_devices
);
79 // Runs |get_device_details_callback| for |device_path| on a worker thread.
80 // |volume_watcher| points back to the VolumeMountWatcherWin that called it.
81 static void RetrieveInfoForDeviceAndAdd(
82 const base::FilePath
& device_path
,
83 const GetDeviceDetailsCallbackType
& get_device_details_callback
,
84 base::WeakPtr
<VolumeMountWatcherWin
> volume_watcher
);
86 // Mark that a device we started a metadata check for has completed.
87 virtual void DeviceCheckComplete(const base::FilePath
& device_path
);
89 virtual GetAttachedDevicesCallbackType
GetAttachedDevicesCallback() const;
90 virtual GetDeviceDetailsCallbackType
GetDeviceDetailsCallback() const;
92 // Used for device info calls that may take a long time.
93 scoped_refptr
<base::SequencedTaskRunner
> device_info_task_runner_
;
96 friend class TestVolumeMountWatcherWin
;
98 // Key: Mass storage device mount point.
99 // Value: Mass storage device metadata.
100 typedef std::map
<base::FilePath
, StorageInfo
> MountPointDeviceMetadataMap
;
102 // Maintain a set of device attribute check calls in-flight. Only accessed
103 // on the UI thread. This is to try and prevent the same device from
104 // occupying our worker pool in case of windows API call hangs.
105 std::set
<base::FilePath
> pending_device_checks_
;
107 // A map from device mount point to device metadata. Only accessed on the UI
109 MountPointDeviceMetadataMap device_metadata_
;
111 // The notifications object to use to signal newly attached volumes. Only
112 // removable devices will be notified.
113 StorageMonitor::Receiver
* notifications_
;
115 base::WeakPtrFactory
<VolumeMountWatcherWin
> weak_factory_
;
117 DISALLOW_COPY_AND_ASSIGN(VolumeMountWatcherWin
);
120 } // namespace storage_monitor
122 #endif // COMPONENTS_STORAGE_MONITOR_VOLUME_MOUNT_WATCHER_WIN_H_