1 // Copyright 2014 The Chromium Authors
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/files/file_path.h"
14 #include "base/functional/callback.h"
15 #include "base/memory/raw_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/task/sequenced_task_runner.h"
18 #include "components/storage_monitor/storage_info.h"
19 #include "components/storage_monitor/storage_monitor.h"
23 namespace storage_monitor {
25 class TestVolumeMountWatcherWin;
27 // This class watches the volume mount points and sends notifications to
28 // StorageMonitor about the device attach/detach events.
29 // This is a singleton class instantiated by StorageMonitorWin.
30 class VolumeMountWatcherWin {
32 VolumeMountWatcherWin();
34 VolumeMountWatcherWin(const VolumeMountWatcherWin&) = delete;
35 VolumeMountWatcherWin& operator=(const VolumeMountWatcherWin&) = delete;
37 virtual ~VolumeMountWatcherWin();
39 // Returns the volume file path of the drive specified by the |drive_number|.
40 // |drive_number| inputs of 0 - 25 are valid. Returns an empty file path if
41 // the |drive_number| is invalid.
42 static base::FilePath DriveNumberToFilePath(int drive_number);
46 // Gets the information about the device mounted at |device_path|. On success,
47 // returns true and fills in |info|.
48 // Can block during startup while device info is still loading.
49 bool GetDeviceInfo(const base::FilePath& device_path,
50 StorageInfo* info) const;
52 // Processes DEV_BROADCAST_VOLUME messages and triggers a
53 // notification if appropriate.
54 void OnWindowMessage(UINT event_type, LPARAM data);
56 // Processes SHCNE_MEDIAINSERTED (and REMOVED).
57 void OnMediaChange(WPARAM wparam, LPARAM lparam);
59 // Set the volume notifications object to be used when new
60 // removable volumes are found.
61 void SetNotifications(StorageMonitor::Receiver* notifications);
64 const std::string& device_id,
65 base::OnceCallback<void(StorageMonitor::EjectStatus)> callback);
68 using GetDeviceDetailsCallbackType =
69 base::OnceCallback<bool(const base::FilePath&, StorageInfo*)>;
71 using GetAttachedDevicesCallbackType =
72 base::OnceCallback<std::vector<base::FilePath>()>;
74 // Handles mass storage device attach event on UI thread.
75 void HandleDeviceAttachEventOnUIThread(
76 const base::FilePath& device_path,
77 const StorageInfo& info);
79 // Handles mass storage device detach event on UI thread.
80 void HandleDeviceDetachEventOnUIThread(const std::wstring& device_location);
82 // UI thread delegate to set up adding storage devices.
83 void AddDevicesOnUIThread(std::vector<base::FilePath> removable_devices);
85 // Runs |get_device_details_callback| for |device_path| on a worker thread.
86 // |volume_watcher| points back to the VolumeMountWatcherWin that called it.
87 static void RetrieveInfoForDeviceAndAdd(
88 const base::FilePath& device_path,
89 GetDeviceDetailsCallbackType get_device_details_callback,
90 base::WeakPtr<VolumeMountWatcherWin> volume_watcher);
92 // Mark that a device we started a metadata check for has completed.
93 virtual void DeviceCheckComplete(const base::FilePath& device_path);
95 virtual GetAttachedDevicesCallbackType GetAttachedDevicesCallback() const;
96 virtual GetDeviceDetailsCallbackType GetDeviceDetailsCallback() const;
98 // Used for device info calls that may take a long time.
99 const scoped_refptr<base::SequencedTaskRunner> device_info_task_runner_;
102 friend class TestVolumeMountWatcherWin;
104 // Key: Mass storage device mount point.
105 // Value: Mass storage device metadata.
106 typedef std::map<base::FilePath, StorageInfo> MountPointDeviceMetadataMap;
108 // Maintain a set of device attribute check calls in-flight. Only accessed
109 // on the UI thread. This is to try and prevent the same device from
110 // occupying our worker pool in case of windows API call hangs.
111 std::set<base::FilePath> pending_device_checks_;
113 // A map from device mount point to device metadata. Only accessed on the UI
115 MountPointDeviceMetadataMap device_metadata_;
117 // The notifications object to use to signal newly attached volumes. Only
118 // removable devices will be notified.
119 raw_ptr<StorageMonitor::Receiver> notifications_;
121 base::WeakPtrFactory<VolumeMountWatcherWin> weak_factory_{this};
124 } // namespace storage_monitor
126 #endif // COMPONENTS_STORAGE_MONITOR_VOLUME_MOUNT_WATCHER_WIN_H_