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