summaryrefslogtreecommitdiff
path: root/chromium/components/storage_monitor/volume_mount_watcher_win.h
blob: c8eafb2bf7c4aeb5ad293dbba8a1c3d28fbf5dd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_STORAGE_MONITOR_VOLUME_MOUNT_WATCHER_WIN_H_
#define COMPONENTS_STORAGE_MONITOR_VOLUME_MOUNT_WATCHER_WIN_H_

#include <map>
#include <set>
#include <string>
#include <vector>

#include "base/callback.h"
#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/sequenced_task_runner.h"
#include "base/strings/string16.h"
#include "components/storage_monitor/storage_info.h"
#include "components/storage_monitor/storage_monitor.h"

#include <windows.h>

namespace storage_monitor {

class TestVolumeMountWatcherWin;

// This class watches the volume mount points and sends notifications to
// StorageMonitor about the device attach/detach events.
// This is a singleton class instantiated by StorageMonitorWin.
class VolumeMountWatcherWin {
 public:
  VolumeMountWatcherWin();
  virtual ~VolumeMountWatcherWin();

  // Returns the volume file path of the drive specified by the |drive_number|.
  // |drive_number| inputs of 0 - 25 are valid. Returns an empty file path if
  // the |drive_number| is invalid.
  static base::FilePath DriveNumberToFilePath(int drive_number);

  void Init();

  // Gets the information about the device mounted at |device_path|. On success,
  // returns true and fills in |info|.
  // Can block during startup while device info is still loading.
  bool GetDeviceInfo(const base::FilePath& device_path,
                     StorageInfo* info) const;

  // Processes DEV_BROADCAST_VOLUME messages and triggers a
  // notification if appropriate.
  void OnWindowMessage(UINT event_type, LPARAM data);

  // Processes SHCNE_MEDIAINSERTED (and REMOVED).
  void OnMediaChange(WPARAM wparam, LPARAM lparam);

  // Set the volume notifications object to be used when new
  // removable volumes are found.
  void SetNotifications(StorageMonitor::Receiver* notifications);

  void EjectDevice(const std::string& device_id,
                   base::Callback<void(StorageMonitor::EjectStatus)> callback);

 protected:
  typedef base::Callback<bool(const base::FilePath&,
                              StorageInfo*)> GetDeviceDetailsCallbackType;

  typedef base::Callback<std::vector<base::FilePath>(void)>
      GetAttachedDevicesCallbackType;

  // Handles mass storage device attach event on UI thread.
  void HandleDeviceAttachEventOnUIThread(
      const base::FilePath& device_path,
      const StorageInfo& info);

  // Handles mass storage device detach event on UI thread.
  void HandleDeviceDetachEventOnUIThread(const base::string16& device_location);

  // UI thread delegate to set up adding storage devices.
  void AddDevicesOnUIThread(std::vector<base::FilePath> removable_devices);

  // Runs |get_device_details_callback| for |device_path| on a worker thread.
  // |volume_watcher| points back to the VolumeMountWatcherWin that called it.
  static void RetrieveInfoForDeviceAndAdd(
      const base::FilePath& device_path,
      const GetDeviceDetailsCallbackType& get_device_details_callback,
      base::WeakPtr<VolumeMountWatcherWin> volume_watcher);

  // Mark that a device we started a metadata check for has completed.
  virtual void DeviceCheckComplete(const base::FilePath& device_path);

  virtual GetAttachedDevicesCallbackType GetAttachedDevicesCallback() const;
  virtual GetDeviceDetailsCallbackType GetDeviceDetailsCallback() const;

  // Used for device info calls that may take a long time.
  const scoped_refptr<base::SequencedTaskRunner> device_info_task_runner_;

 private:
  friend class TestVolumeMountWatcherWin;

  // Key: Mass storage device mount point.
  // Value: Mass storage device metadata.
  typedef std::map<base::FilePath, StorageInfo> MountPointDeviceMetadataMap;

  // Maintain a set of device attribute check calls in-flight. Only accessed
  // on the UI thread. This is to try and prevent the same device from
  // occupying our worker pool in case of windows API call hangs.
  std::set<base::FilePath> pending_device_checks_;

  // A map from device mount point to device metadata. Only accessed on the UI
  // thread.
  MountPointDeviceMetadataMap device_metadata_;

  // The notifications object to use to signal newly attached volumes. Only
  // removable devices will be notified.
  StorageMonitor::Receiver* notifications_;

  base::WeakPtrFactory<VolumeMountWatcherWin> weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(VolumeMountWatcherWin);
};

}  // namespace storage_monitor

#endif  // COMPONENTS_STORAGE_MONITOR_VOLUME_MOUNT_WATCHER_WIN_H_