storage_monitor_win.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.
  4. #ifndef COMPONENTS_STORAGE_MONITOR_STORAGE_MONITOR_WIN_H_
  5. #define COMPONENTS_STORAGE_MONITOR_STORAGE_MONITOR_WIN_H_
  6. #include <windows.h>
  7. #include <memory>
  8. #include "base/memory/ref_counted.h"
  9. #include "components/storage_monitor/storage_monitor.h"
  10. namespace base {
  11. class FilePath;
  12. }
  13. namespace storage_monitor {
  14. class PortableDeviceWatcherWin;
  15. class TestStorageMonitorWin;
  16. class VolumeMountWatcherWin;
  17. class StorageMonitorWin : public StorageMonitor {
  18. public:
  19. // Should only be called by browser start up code.
  20. // Use StorageMonitor::GetInstance() instead.
  21. // To support unit tests, this constructor takes |volume_mount_watcher| and
  22. // |portable_device_watcher| objects. These params are either constructed in
  23. // unit tests or in StorageMonitorWin CreateInternal() function.
  24. StorageMonitorWin(
  25. std::unique_ptr<VolumeMountWatcherWin> volume_mount_watcher,
  26. std::unique_ptr<PortableDeviceWatcherWin> portable_device_watcher);
  27. StorageMonitorWin(const StorageMonitorWin&) = delete;
  28. StorageMonitorWin& operator=(const StorageMonitorWin&) = delete;
  29. ~StorageMonitorWin() override;
  30. // Must be called after the file thread is created.
  31. void Init() override;
  32. // StorageMonitor:
  33. bool GetStorageInfoForPath(const base::FilePath& path,
  34. StorageInfo* device_info) const override;
  35. bool GetMTPStorageInfoFromDeviceId(
  36. const std::string& storage_device_id,
  37. std::wstring* device_location,
  38. std::wstring* storage_object_id) const override;
  39. void EjectDevice(const std::string& device_id,
  40. base::OnceCallback<void(EjectStatus)> callback) override;
  41. private:
  42. class PortableDeviceNotifications;
  43. friend class TestStorageMonitorWin;
  44. void MediaChangeNotificationRegister();
  45. // Gets the removable storage information given a |device_path|. On success,
  46. // returns true and fills in |info|.
  47. bool GetDeviceInfo(const base::FilePath& device_path,
  48. StorageInfo* info) const;
  49. static LRESULT CALLBACK WndProcThunk(HWND hwnd, UINT message, WPARAM wparam,
  50. LPARAM lparam);
  51. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam,
  52. LPARAM lparam);
  53. void OnDeviceChange(UINT event_type, LPARAM data);
  54. void OnMediaChange(WPARAM wparam, LPARAM lparam);
  55. // The window class of |window_|.
  56. ATOM window_class_ = 0;
  57. // The handle of the module that contains the window procedure of |window_|.
  58. HMODULE instance_ = nullptr;
  59. HWND window_ = nullptr;
  60. // The handle of a registration for shell notifications.
  61. ULONG shell_change_notify_id_ = 0;
  62. // The volume mount point watcher, used to manage the mounted devices.
  63. const std::unique_ptr<VolumeMountWatcherWin> volume_mount_watcher_;
  64. // The portable device watcher, used to manage media transfer protocol
  65. // devices.
  66. const std::unique_ptr<PortableDeviceWatcherWin> portable_device_watcher_;
  67. };
  68. } // namespace storage_monitor
  69. #endif // COMPONENTS_STORAGE_MONITOR_STORAGE_MONITOR_WIN_H_