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_VOLUME_MOUNT_WATCHER_WIN_H_
6 #define CHROME_BROWSER_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 "base/threading/sequenced_worker_pool.h"
20 #include "chrome/browser/storage_monitor/storage_info.h"
21 #include "chrome/browser/storage_monitor/storage_monitor.h"
23 class TestVolumeMountWatcherWin
;
25 // This class watches the volume mount points and sends notifications to
26 // StorageMonitor about the device attach/detach events.
27 // This is a singleton class instantiated by StorageMonitorWin.
28 class VolumeMountWatcherWin
{
30 VolumeMountWatcherWin();
31 virtual ~VolumeMountWatcherWin();
33 // Returns the volume file path of the drive specified by the |drive_number|.
34 // |drive_number| inputs of 0 - 25 are valid. Returns an empty file path if
35 // the |drive_number| is invalid.
36 static base::FilePath
DriveNumberToFilePath(int drive_number
);
40 // Gets the information about the device mounted at |device_path|. On success,
41 // returns true and fills in |info|.
42 // Can block during startup while device info is still loading.
43 bool GetDeviceInfo(const base::FilePath
& device_path
,
44 StorageInfo
* info
) const;
46 // Processes DEV_BROADCAST_VOLUME messages and triggers a
47 // notification if appropriate.
48 void OnWindowMessage(UINT event_type
, LPARAM data
);
50 // Set the volume notifications object to be used when new
51 // removable volumes are found.
52 void SetNotifications(StorageMonitor::Receiver
* notifications
);
54 void EjectDevice(const std::string
& device_id
,
55 base::Callback
<void(StorageMonitor::EjectStatus
)> callback
);
58 typedef base::Callback
<bool(const base::FilePath
&,
59 StorageInfo
*)> GetDeviceDetailsCallbackType
;
61 typedef base::Callback
<std::vector
<base::FilePath
>(void)>
62 GetAttachedDevicesCallbackType
;
64 // Handles mass storage device attach event on UI thread.
65 void HandleDeviceAttachEventOnUIThread(
66 const base::FilePath
& device_path
,
67 const StorageInfo
& info
);
69 // Handles mass storage device detach event on UI thread.
70 void HandleDeviceDetachEventOnUIThread(const base::string16
& device_location
);
72 // UI thread delegate to set up adding storage devices.
73 void AddDevicesOnUIThread(std::vector
<base::FilePath
> removable_devices
);
75 // Runs |get_device_details_callback| for |device_path| on a worker thread.
76 // |volume_watcher| points back to the VolumeMountWatcherWin that called it.
77 static void RetrieveInfoForDeviceAndAdd(
78 const base::FilePath
& device_path
,
79 const GetDeviceDetailsCallbackType
& get_device_details_callback
,
80 base::WeakPtr
<VolumeMountWatcherWin
> volume_watcher
);
82 // Mark that a device we started a metadata check for has completed.
83 virtual void DeviceCheckComplete(const base::FilePath
& device_path
);
85 virtual GetAttachedDevicesCallbackType
GetAttachedDevicesCallback() const;
86 virtual GetDeviceDetailsCallbackType
GetDeviceDetailsCallback() const;
88 // Worker pool used to collect device information. Used because some
89 // devices freeze workers trying to get device info, resulting in
91 scoped_refptr
<base::SequencedWorkerPool
> device_info_worker_pool_
;
92 scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
95 friend class TestVolumeMountWatcherWin
;
97 // Key: Mass storage device mount point.
98 // Value: Mass storage device metadata.
99 typedef std::map
<base::FilePath
, StorageInfo
> MountPointDeviceMetadataMap
;
101 // Maintain a set of device attribute check calls in-flight. Only accessed
102 // on the UI thread. This is to try and prevent the same device from
103 // occupying our worker pool in case of windows API call hangs.
104 std::set
<base::FilePath
> pending_device_checks_
;
106 // A map from device mount point to device metadata. Only accessed on the UI
108 MountPointDeviceMetadataMap device_metadata_
;
110 base::WeakPtrFactory
<VolumeMountWatcherWin
> weak_factory_
;
112 // The notifications object to use to signal newly attached volumes. Only
113 // removable devices will be notified.
114 StorageMonitor::Receiver
* notifications_
;
116 DISALLOW_COPY_AND_ASSIGN(VolumeMountWatcherWin
);
119 #endif // CHROME_BROWSER_STORAGE_MONITOR_VOLUME_MOUNT_WATCHER_WIN_H_