[M120 Migration][VD] Enable direct rendering for TVPlus
[platform/framework/web/chromium-efl.git] / components / storage_monitor / volume_mount_watcher_win.h
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.
4
5 #ifndef COMPONENTS_STORAGE_MONITOR_VOLUME_MOUNT_WATCHER_WIN_H_
6 #define COMPONENTS_STORAGE_MONITOR_VOLUME_MOUNT_WATCHER_WIN_H_
7
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <vector>
12
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"
20
21 #include <windows.h>
22
23 namespace storage_monitor {
24
25 class TestVolumeMountWatcherWin;
26
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 {
31  public:
32   VolumeMountWatcherWin();
33
34   VolumeMountWatcherWin(const VolumeMountWatcherWin&) = delete;
35   VolumeMountWatcherWin& operator=(const VolumeMountWatcherWin&) = delete;
36
37   virtual ~VolumeMountWatcherWin();
38
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);
43
44   void Init();
45
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;
51
52   // Processes DEV_BROADCAST_VOLUME messages and triggers a
53   // notification if appropriate.
54   void OnWindowMessage(UINT event_type, LPARAM data);
55
56   // Processes SHCNE_MEDIAINSERTED (and REMOVED).
57   void OnMediaChange(WPARAM wparam, LPARAM lparam);
58
59   // Set the volume notifications object to be used when new
60   // removable volumes are found.
61   void SetNotifications(StorageMonitor::Receiver* notifications);
62
63   void EjectDevice(
64       const std::string& device_id,
65       base::OnceCallback<void(StorageMonitor::EjectStatus)> callback);
66
67  protected:
68   using GetDeviceDetailsCallbackType =
69       base::OnceCallback<bool(const base::FilePath&, StorageInfo*)>;
70
71   using GetAttachedDevicesCallbackType =
72       base::OnceCallback<std::vector<base::FilePath>()>;
73
74   // Handles mass storage device attach event on UI thread.
75   void HandleDeviceAttachEventOnUIThread(
76       const base::FilePath& device_path,
77       const StorageInfo& info);
78
79   // Handles mass storage device detach event on UI thread.
80   void HandleDeviceDetachEventOnUIThread(const std::wstring& device_location);
81
82   // UI thread delegate to set up adding storage devices.
83   void AddDevicesOnUIThread(std::vector<base::FilePath> removable_devices);
84
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);
91
92   // Mark that a device we started a metadata check for has completed.
93   virtual void DeviceCheckComplete(const base::FilePath& device_path);
94
95   virtual GetAttachedDevicesCallbackType GetAttachedDevicesCallback() const;
96   virtual GetDeviceDetailsCallbackType GetDeviceDetailsCallback() const;
97
98   // Used for device info calls that may take a long time.
99   const scoped_refptr<base::SequencedTaskRunner> device_info_task_runner_;
100
101  private:
102   friend class TestVolumeMountWatcherWin;
103
104   // Key: Mass storage device mount point.
105   // Value: Mass storage device metadata.
106   typedef std::map<base::FilePath, StorageInfo> MountPointDeviceMetadataMap;
107
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_;
112
113   // A map from device mount point to device metadata. Only accessed on the UI
114   // thread.
115   MountPointDeviceMetadataMap device_metadata_;
116
117   // The notifications object to use to signal newly attached volumes. Only
118   // removable devices will be notified.
119   raw_ptr<StorageMonitor::Receiver> notifications_;
120
121   base::WeakPtrFactory<VolumeMountWatcherWin> weak_factory_{this};
122 };
123
124 }  // namespace storage_monitor
125
126 #endif  // COMPONENTS_STORAGE_MONITOR_VOLUME_MOUNT_WATCHER_WIN_H_