OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_SYSTEM_MONITOR_REMOVABLE_DEVICE_NOTIFICATIONS_WINDOW_WIN_
H_ | 5 #ifndef CHROME_BROWSER_SYSTEM_MONITOR_REMOVABLE_DEVICE_NOTIFICATIONS_WINDOW_WIN_
H_ |
6 #define CHROME_BROWSER_SYSTEM_MONITOR_REMOVABLE_DEVICE_NOTIFICATIONS_WINDOW_WIN_
H_ | 6 #define CHROME_BROWSER_SYSTEM_MONITOR_REMOVABLE_DEVICE_NOTIFICATIONS_WINDOW_WIN_
H_ |
7 | 7 |
8 #include <windows.h> | 8 #include <windows.h> |
9 | 9 |
| 10 #include <map> |
10 #include <string> | 11 #include <string> |
| 12 #include <vector> |
11 | 13 |
12 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
13 #include "base/file_path.h" | 15 #include "base/file_path.h" |
14 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
| 17 #include "base/string16.h" |
| 18 #include "base/system_monitor/system_monitor.h" |
15 | 19 |
16 typedef LRESULT (*VolumeNameFunc)(LPCWSTR drive, | |
17 LPWSTR volume_name, | |
18 unsigned int volume_name_len); | |
19 namespace chrome { | 20 namespace chrome { |
20 | 21 |
| 22 // Gets device information given a |device_path|. On success, returns true and |
| 23 // fills in |unique_id|, |name|, and |removable|. |
| 24 typedef bool (*GetDeviceInfoFunc)(const FilePath& device, |
| 25 string16* location, |
| 26 std::string* unique_id, |
| 27 string16* name, |
| 28 bool* removable); |
| 29 |
| 30 // Returns a vector of all the removable devices that are connected. |
| 31 typedef std::vector<FilePath> (*GetAttachedDevicesFunc)(); |
| 32 |
21 class RemovableDeviceNotificationsWindowWin; | 33 class RemovableDeviceNotificationsWindowWin; |
22 typedef RemovableDeviceNotificationsWindowWin RemovableDeviceNotifications; | 34 typedef RemovableDeviceNotificationsWindowWin RemovableDeviceNotifications; |
23 | 35 |
24 class RemovableDeviceNotificationsWindowWin | 36 class RemovableDeviceNotificationsWindowWin |
25 : public base::RefCountedThreadSafe<RemovableDeviceNotificationsWindowWin> { | 37 : public base::RefCountedThreadSafe<RemovableDeviceNotificationsWindowWin> { |
26 public: | 38 public: |
| 39 // Should only be called by browser start up code. Use GetInstance() instead. |
27 RemovableDeviceNotificationsWindowWin(); | 40 RemovableDeviceNotificationsWindowWin(); |
| 41 |
| 42 // base::SystemMonitor has a lifetime somewhat shorter than a Singleton and |
| 43 // |this| is constructed/destroyed just after/before SystemMonitor. |
| 44 static RemovableDeviceNotificationsWindowWin* GetInstance(); |
| 45 |
| 46 // Must be called after the file thread is created. |
| 47 void Init(); |
| 48 |
| 49 // Finds the device that contains |path| and populates |device_info|. |
| 50 // Returns false if unable to find the device. |
| 51 bool GetDeviceInfoForPath( |
| 52 const FilePath& path, |
| 53 base::SystemMonitor::RemovableStorageInfo* device_info); |
| 54 |
| 55 protected: |
28 // Only for use in unit tests. | 56 // Only for use in unit tests. |
29 explicit RemovableDeviceNotificationsWindowWin(VolumeNameFunc volumeNameFunc); | 57 void InitForTest(GetDeviceInfoFunc getDeviceInfo, |
| 58 GetAttachedDevicesFunc getAttachedDevices); |
30 | 59 |
31 LRESULT OnDeviceChange(UINT event_type, DWORD data); | 60 void OnDeviceChange(UINT event_type, LPARAM data); |
32 | 61 |
33 private: | 62 private: |
34 friend class | 63 friend class |
35 base::RefCountedThreadSafe<RemovableDeviceNotificationsWindowWin>; | 64 base::RefCountedThreadSafe<RemovableDeviceNotificationsWindowWin>; |
| 65 friend class TestRemovableDeviceNotificationsWindowWin; |
| 66 |
| 67 typedef std::map<FilePath, std::string> MountPointDeviceIdMap; |
36 | 68 |
37 virtual ~RemovableDeviceNotificationsWindowWin(); | 69 virtual ~RemovableDeviceNotificationsWindowWin(); |
38 | 70 |
39 void Init(); | 71 static LRESULT CALLBACK WndProcThunk(HWND hwnd, UINT message, WPARAM wparam, |
| 72 LPARAM lparam); |
40 | 73 |
41 LRESULT CALLBACK WndProc(HWND hwnd, | 74 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, |
42 UINT message, | |
43 WPARAM wparam, | |
44 LPARAM lparam); | 75 LPARAM lparam); |
45 | 76 |
46 static LRESULT CALLBACK WndProcThunk(HWND hwnd, | 77 void DoInit(GetAttachedDevicesFunc get_attached_devices_func); |
47 UINT message, | |
48 WPARAM wparam, | |
49 LPARAM lparam); | |
50 | 78 |
51 void CheckDeviceTypeOnFileThread(const std::string& id, | 79 void AddNewDevice(const FilePath& device_path); |
| 80 |
| 81 void CheckDeviceTypeOnFileThread(const std::string& unique_id, |
52 const FilePath::StringType& device_name, | 82 const FilePath::StringType& device_name, |
53 const FilePath& path); | 83 const FilePath& device); |
54 | 84 |
55 void ProcessRemovableDeviceAttachedOnUIThread( | 85 void ProcessDeviceAttachedOnUIThread( |
56 const std::string& id, | 86 const std::string& device_id, |
57 const FilePath::StringType& device_name, | 87 const FilePath::StringType& device_name, |
58 const FilePath& path); | 88 const FilePath& device); |
59 | 89 |
60 // The window class of |window_|. | 90 // The window class of |window_|. |
61 ATOM atom_; | 91 ATOM window_class_; |
62 | |
63 // The handle of the module that contains the window procedure of |window_|. | 92 // The handle of the module that contains the window procedure of |window_|. |
64 HMODULE instance_; | 93 HMODULE instance_; |
| 94 HWND window_; |
65 | 95 |
66 HWND window_; | 96 GetDeviceInfoFunc get_device_info_func_; |
67 VolumeNameFunc volume_name_func_; | 97 |
| 98 // A map from device mount point to device id. Only accessed on the UI Thread. |
| 99 MountPointDeviceIdMap device_ids_; |
68 | 100 |
69 DISALLOW_COPY_AND_ASSIGN(RemovableDeviceNotificationsWindowWin); | 101 DISALLOW_COPY_AND_ASSIGN(RemovableDeviceNotificationsWindowWin); |
70 }; | 102 }; |
71 | 103 |
72 } // namespace chrome | 104 } // namespace chrome |
73 | 105 |
74 #endif // CHROME_BROWSER_SYSTEM_MONITOR_REMOVABLE_DEVICE_NOTIFICATIONS_WINDOW_W
IN_H_ | 106 #endif // CHROME_BROWSER_SYSTEM_MONITOR_REMOVABLE_DEVICE_NOTIFICATIONS_WINDOW_W
IN_H_ |
OLD | NEW |