OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 #include "components/storage_monitor/storage_monitor_win.h" | 5 #include "components/storage_monitor/storage_monitor_win.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 #include <dbt.h> | 8 #include <dbt.h> |
9 #include <fileapi.h> | 9 #include <fileapi.h> |
| 10 #include <shlobj.h> |
10 | 11 |
11 #include "base/win/wrapped_window_proc.h" | 12 #include "base/win/wrapped_window_proc.h" |
12 #include "components/storage_monitor/portable_device_watcher_win.h" | 13 #include "components/storage_monitor/portable_device_watcher_win.h" |
13 #include "components/storage_monitor/removable_device_constants.h" | 14 #include "components/storage_monitor/removable_device_constants.h" |
14 #include "components/storage_monitor/storage_info.h" | 15 #include "components/storage_monitor/storage_info.h" |
15 #include "components/storage_monitor/volume_mount_watcher_win.h" | 16 #include "components/storage_monitor/volume_mount_watcher_win.h" |
16 | 17 |
| 18 #define WM_USER_MEDIACHANGED (WM_USER + 5) |
| 19 |
17 // StorageMonitorWin ------------------------------------------------------- | 20 // StorageMonitorWin ------------------------------------------------------- |
18 | 21 |
19 namespace storage_monitor { | 22 namespace storage_monitor { |
20 | 23 |
21 StorageMonitorWin::StorageMonitorWin( | 24 StorageMonitorWin::StorageMonitorWin( |
22 VolumeMountWatcherWin* volume_mount_watcher, | 25 VolumeMountWatcherWin* volume_mount_watcher, |
23 PortableDeviceWatcherWin* portable_device_watcher) | 26 PortableDeviceWatcherWin* portable_device_watcher) |
24 : window_class_(0), | 27 : window_class_(0), |
25 instance_(NULL), | 28 instance_(NULL), |
26 window_(NULL), | 29 window_(NULL), |
| 30 shell_change_notify_id_(0), |
27 volume_mount_watcher_(volume_mount_watcher), | 31 volume_mount_watcher_(volume_mount_watcher), |
28 portable_device_watcher_(portable_device_watcher) { | 32 portable_device_watcher_(portable_device_watcher) { |
29 DCHECK(volume_mount_watcher_); | 33 DCHECK(volume_mount_watcher_); |
30 DCHECK(portable_device_watcher_); | 34 DCHECK(portable_device_watcher_); |
31 volume_mount_watcher_->SetNotifications(receiver()); | 35 volume_mount_watcher_->SetNotifications(receiver()); |
32 portable_device_watcher_->SetNotifications(receiver()); | 36 portable_device_watcher_->SetNotifications(receiver()); |
33 } | 37 } |
34 | 38 |
35 StorageMonitorWin::~StorageMonitorWin() { | 39 StorageMonitorWin::~StorageMonitorWin() { |
| 40 if (shell_change_notify_id_) |
| 41 SHChangeNotifyDeregister(shell_change_notify_id_); |
36 volume_mount_watcher_->SetNotifications(NULL); | 42 volume_mount_watcher_->SetNotifications(NULL); |
37 portable_device_watcher_->SetNotifications(NULL); | 43 portable_device_watcher_->SetNotifications(NULL); |
38 | 44 |
39 if (window_) | 45 if (window_) |
40 DestroyWindow(window_); | 46 DestroyWindow(window_); |
41 | 47 |
42 if (window_class_) | 48 if (window_class_) |
43 UnregisterClass(MAKEINTATOM(window_class_), instance_); | 49 UnregisterClass(MAKEINTATOM(window_class_), instance_); |
44 } | 50 } |
45 | 51 |
46 void StorageMonitorWin::Init() { | 52 void StorageMonitorWin::Init() { |
47 WNDCLASSEX window_class; | 53 WNDCLASSEX window_class; |
48 base::win::InitializeWindowClass( | 54 base::win::InitializeWindowClass( |
49 L"Chrome_StorageMonitorWindow", | 55 L"Chrome_StorageMonitorWindow", |
50 &base::win::WrappedWindowProc<StorageMonitorWin::WndProcThunk>, | 56 &base::win::WrappedWindowProc<StorageMonitorWin::WndProcThunk>, |
51 0, 0, 0, NULL, NULL, NULL, NULL, NULL, | 57 0, 0, 0, NULL, NULL, NULL, NULL, NULL, |
52 &window_class); | 58 &window_class); |
53 instance_ = window_class.hInstance; | 59 instance_ = window_class.hInstance; |
54 window_class_ = RegisterClassEx(&window_class); | 60 window_class_ = RegisterClassEx(&window_class); |
55 DCHECK(window_class_); | 61 DCHECK(window_class_); |
56 | 62 |
57 window_ = CreateWindow(MAKEINTATOM(window_class_), 0, 0, 0, 0, 0, 0, 0, 0, | 63 window_ = CreateWindow(MAKEINTATOM(window_class_), 0, 0, 0, 0, 0, 0, 0, 0, |
58 instance_, 0); | 64 instance_, 0); |
59 SetWindowLongPtr(window_, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this)); | 65 SetWindowLongPtr(window_, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this)); |
60 volume_mount_watcher_->Init(); | 66 volume_mount_watcher_->Init(); |
61 portable_device_watcher_->Init(window_); | 67 portable_device_watcher_->Init(window_); |
| 68 MediaChangeNotificationRegister(); |
62 } | 69 } |
63 | 70 |
64 bool StorageMonitorWin::GetStorageInfoForPath(const base::FilePath& path, | 71 bool StorageMonitorWin::GetStorageInfoForPath(const base::FilePath& path, |
65 StorageInfo* device_info) const { | 72 StorageInfo* device_info) const { |
66 DCHECK(device_info); | 73 DCHECK(device_info); |
67 | 74 |
68 // TODO(gbillock): Move this logic up to StorageMonitor. | 75 // TODO(gbillock): Move this logic up to StorageMonitor. |
69 // If we already know the StorageInfo for the path, just return it. | 76 // If we already know the StorageInfo for the path, just return it. |
70 // This will account for portable devices as well. | 77 // This will account for portable devices as well. |
71 std::vector<StorageInfo> attached_devices = GetAllAvailableStorages(); | 78 std::vector<StorageInfo> attached_devices = GetAllAvailableStorages(); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 return msg_wnd->WndProc(hwnd, message, wparam, lparam); | 138 return msg_wnd->WndProc(hwnd, message, wparam, lparam); |
132 return ::DefWindowProc(hwnd, message, wparam, lparam); | 139 return ::DefWindowProc(hwnd, message, wparam, lparam); |
133 } | 140 } |
134 | 141 |
135 LRESULT CALLBACK StorageMonitorWin::WndProc(HWND hwnd, UINT message, | 142 LRESULT CALLBACK StorageMonitorWin::WndProc(HWND hwnd, UINT message, |
136 WPARAM wparam, LPARAM lparam) { | 143 WPARAM wparam, LPARAM lparam) { |
137 switch (message) { | 144 switch (message) { |
138 case WM_DEVICECHANGE: | 145 case WM_DEVICECHANGE: |
139 OnDeviceChange(static_cast<UINT>(wparam), lparam); | 146 OnDeviceChange(static_cast<UINT>(wparam), lparam); |
140 return TRUE; | 147 return TRUE; |
| 148 case WM_USER_MEDIACHANGED: |
| 149 OnMediaChange(wparam, lparam); |
| 150 return TRUE; |
141 default: | 151 default: |
142 break; | 152 break; |
143 } | 153 } |
144 | 154 |
145 return ::DefWindowProc(hwnd, message, wparam, lparam); | 155 return ::DefWindowProc(hwnd, message, wparam, lparam); |
146 } | 156 } |
147 | 157 |
| 158 void StorageMonitorWin::MediaChangeNotificationRegister() { |
| 159 LPITEMIDLIST id_list; |
| 160 if (SHGetSpecialFolderLocation(NULL, CSIDL_DRIVES, &id_list) == NOERROR) { |
| 161 SHChangeNotifyEntry notify_entry; |
| 162 notify_entry.pidl = id_list; |
| 163 notify_entry.fRecursive = TRUE; |
| 164 shell_change_notify_id_ = SHChangeNotifyRegister( |
| 165 window_, SHCNRF_ShellLevel, SHCNE_MEDIAINSERTED | SHCNE_MEDIAREMOVED, |
| 166 WM_USER_MEDIACHANGED, 1, ¬ify_entry); |
| 167 if (!shell_change_notify_id_) |
| 168 DVLOG(1) << "SHChangeNotifyRegister FAILED"; |
| 169 } else { |
| 170 DVLOG(1) << "SHGetSpecialFolderLocation FAILED"; |
| 171 } |
| 172 } |
| 173 |
148 bool StorageMonitorWin::GetDeviceInfo(const base::FilePath& device_path, | 174 bool StorageMonitorWin::GetDeviceInfo(const base::FilePath& device_path, |
149 StorageInfo* info) const { | 175 StorageInfo* info) const { |
150 DCHECK(info); | 176 DCHECK(info); |
151 | 177 |
152 // TODO(kmadhusu) Implement PortableDeviceWatcherWin::GetDeviceInfo() | 178 // TODO(kmadhusu) Implement PortableDeviceWatcherWin::GetDeviceInfo() |
153 // function when we have the functionality to add a sub directory of | 179 // function when we have the functionality to add a sub directory of |
154 // portable device as a media gallery. | 180 // portable device as a media gallery. |
155 return volume_mount_watcher_->GetDeviceInfo(device_path, info); | 181 return volume_mount_watcher_->GetDeviceInfo(device_path, info); |
156 } | 182 } |
157 | 183 |
158 void StorageMonitorWin::OnDeviceChange(UINT event_type, LPARAM data) { | 184 void StorageMonitorWin::OnDeviceChange(UINT event_type, LPARAM data) { |
| 185 DVLOG(1) << "OnDeviceChange " << event_type << " " << data; |
159 volume_mount_watcher_->OnWindowMessage(event_type, data); | 186 volume_mount_watcher_->OnWindowMessage(event_type, data); |
160 portable_device_watcher_->OnWindowMessage(event_type, data); | 187 portable_device_watcher_->OnWindowMessage(event_type, data); |
161 } | 188 } |
162 | 189 |
| 190 void StorageMonitorWin::OnMediaChange(WPARAM wparam, LPARAM lparam) { |
| 191 volume_mount_watcher_->OnMediaChange(wparam, lparam); |
| 192 } |
| 193 |
163 StorageMonitor* StorageMonitor::CreateInternal() { | 194 StorageMonitor* StorageMonitor::CreateInternal() { |
164 return new StorageMonitorWin(new VolumeMountWatcherWin(), | 195 return new StorageMonitorWin(new VolumeMountWatcherWin(), |
165 new PortableDeviceWatcherWin()); | 196 new PortableDeviceWatcherWin()); |
166 } | 197 } |
167 | 198 |
168 } // namespace storage_monitor | 199 } // namespace storage_monitor |
OLD | NEW |