Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(241)

Side by Side Diff: chrome/browser/system_monitor/removable_device_notifications_window_win.cc

Issue 10911234: Update Windows System Monitor Removable Device Impl. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 #include "chrome/browser/system_monitor/removable_device_notifications_window_wi n.h" 5 #include "chrome/browser/system_monitor/removable_device_notifications_window_wi n.h"
6 6
7 #include <windows.h> 7 #include <windows.h>
8 #include <dbt.h> 8 #include <dbt.h>
9 9 #include <fileapi.h>
10 #include <string>
11 10
12 #include "base/file_path.h" 11 #include "base/file_path.h"
12 #include "base/metrics/histogram.h"
13 #include "base/string_number_conversions.h" 13 #include "base/string_number_conversions.h"
14 #include "base/system_monitor/system_monitor.h" 14 #include "base/system_monitor/system_monitor.h"
15 #include "base/utf_string_conversions.h"
15 #include "base/win/wrapped_window_proc.h" 16 #include "base/win/wrapped_window_proc.h"
16 #include "chrome/browser/system_monitor/media_device_notifications_utils.h" 17 #include "chrome/browser/system_monitor/media_device_notifications_utils.h"
17 #include "chrome/browser/system_monitor/media_storage_util.h" 18 #include "chrome/browser/system_monitor/media_storage_util.h"
18 #include "content/public/browser/browser_thread.h" 19 #include "content/public/browser/browser_thread.h"
19 20
20 using base::SystemMonitor; 21 using base::SystemMonitor;
22 using base::win::WrappedWindowProc;
21 using content::BrowserThread; 23 using content::BrowserThread;
22 24
23 namespace { 25 namespace {
24 26
25 const wchar_t WindowClassName[] = L"Chrome_RemovableDeviceNotificationWindow"; 27 const DWORD kMaxPathBufLen = MAX_PATH + 1;
26 28
27 LRESULT GetVolumeName(LPCWSTR drive, 29 const char16 kWindowClassName[] = L"Chrome_RemovableDeviceNotificationWindow";
28 LPWSTR volume_name, 30
29 unsigned int volume_name_len) { 31 static chrome::RemovableDeviceNotificationsWindowWin*
30 return GetVolumeInformation(drive, volume_name, volume_name_len, NULL, NULL, 32 g_removable_device_notifications_window_win = NULL;
31 NULL, NULL, 0); 33
34 // The following msdn blog entry is helpful for understanding disk volumes
35 // and how they are treated in Windows:
36 // http://blogs.msdn.com/b/adioltean/archive/2005/04/16/408947.aspx
37 bool GetDeviceInfo(const FilePath& device_path, string16* device_location,
38 std::string* unique_id, string16* name, bool* removable) {
39 char16 mount_point[kMaxPathBufLen];
40 if (!GetVolumePathName(device_path.value().c_str(), mount_point,
41 kMaxPathBufLen)) {
42 return false;
43 }
44 if (device_location)
45 *device_location = string16(mount_point);
46
47 if (unique_id) {
48 char16 guid[kMaxPathBufLen];
49 // If there's a drive letter and a mount point, we have to resolve the
50 // drive letter to a GUID first (see above mentioned blog).
51 FilePath mount(mount_point);
52 if (mount != mount.DirName() && mount.value().length() > 2 &&
53 mount_point[1] == L':' &&
54 ((mount_point[0] >= L'A' && mount_point[0] <= L'Z') ||
55 (mount_point[0] >= L'a' && mount_point[0] <= L'z'))) {
56 string16 drive = mount.value().substr(0, 3);
57 if (!GetVolumeNameForVolumeMountPoint(drive.c_str(), guid,
58 kMaxPathBufLen)) {
59 return false;
60 }
61 FilePath resolved(guid);
62 resolved = resolved.Append(mount_point + 2);
63 if (!GetVolumeNameForVolumeMountPoint(resolved.value().c_str(), guid,
64 kMaxPathBufLen)) {
65 return false;
66 }
67 } else {
68 if (!GetVolumeNameForVolumeMountPoint(mount_point, guid, kMaxPathBufLen))
69 return false;
70 }
71 WideToUTF8(guid, wcslen(guid), unique_id);
72 }
73
74 if (name) {
75 char16 volume_name[kMaxPathBufLen];
76 if (!GetVolumeInformation(mount_point, volume_name, kMaxPathBufLen, NULL,
77 NULL, NULL, NULL, 0)) {
78 return false;
79 }
80 if (wcslen(volume_name) > 0) {
81 *name = string16(volume_name);
82 } else {
83 *name = device_path.LossyDisplayName();
84 }
85 }
86
87 if (removable) {
88 UINT type = GetDriveType(mount_point);
89 *removable = (type == DRIVE_REMOVABLE);
90 }
91
92 return true;
93 }
94
95 std::vector<FilePath> GetAttachedDevices() {
96 std::vector<FilePath> result;
97 char16 volume_name[kMaxPathBufLen];
98 HANDLE find_handle = FindFirstVolume(volume_name, kMaxPathBufLen);
99 if (find_handle == INVALID_HANDLE_VALUE)
100 return result;
101
102 while (true) {
103 char16 volume_path[kMaxPathBufLen];
104 DWORD return_count;
105 if (GetVolumePathNamesForVolumeName(volume_name, volume_path,
106 kMaxPathBufLen, &return_count)) {
107 if (GetDriveType(volume_path) == DRIVE_REMOVABLE)
108 result.push_back(FilePath(volume_path));
109 } else {
110 DLOG(ERROR) << "GetVolmePathNamesForVolueName() failed. LastError: "
rvargas (doing something else) 2012/09/17 22:26:06 DPLOG
vandebo (ex-Chrome) 2012/09/17 23:09:16 Done.
111 << GetLastError();
112 }
113 if (!FindNextVolume(find_handle, volume_name, kMaxPathBufLen)) {
114 if (GetLastError() != ERROR_NO_MORE_FILES)
115 DLOG(ERROR) << "FindNextVolume() failed. LastError: " << GetLastError();
116 break;
117 }
118 }
119
120 FindVolumeClose(find_handle);
121 return result;
32 } 122 }
33 123
34 // Returns 0 if the devicetype is not volume. 124 // Returns 0 if the devicetype is not volume.
35 DWORD GetVolumeBitMaskFromBroadcastHeader(DWORD data) { 125 uint32 GetVolumeBitMaskFromBroadcastHeader(LPARAM data) {
36 PDEV_BROADCAST_HDR dev_broadcast_hdr = 126 DEV_BROADCAST_VOLUME* dev_broadcast_volume =
37 reinterpret_cast<PDEV_BROADCAST_HDR>(data); 127 reinterpret_cast<DEV_BROADCAST_VOLUME*>(data);
38 if (dev_broadcast_hdr->dbch_devicetype == DBT_DEVTYP_VOLUME) { 128 if (dev_broadcast_volume->dbcv_devicetype == DBT_DEVTYP_VOLUME)
39 PDEV_BROADCAST_VOLUME dev_broadcast_volume =
40 reinterpret_cast<PDEV_BROADCAST_VOLUME>(dev_broadcast_hdr);
41 return dev_broadcast_volume->dbcv_unitmask; 129 return dev_broadcast_volume->dbcv_unitmask;
42 }
43 return 0; 130 return 0;
44 } 131 }
45 132
133 FilePath DriveNumberToFilePath(int drive_number) {
134 string16 path(L"_:\\");
135 path[0] = L'A' + drive_number;
136 return FilePath(path);
137 }
138
46 } // namespace 139 } // namespace
47 140
48 namespace chrome { 141 namespace chrome {
49 142
50 RemovableDeviceNotificationsWindowWin::RemovableDeviceNotificationsWindowWin() 143 RemovableDeviceNotificationsWindowWin::RemovableDeviceNotificationsWindowWin()
51 : atom_(0), 144 : window_class_(0),
52 instance_(NULL), 145 instance_(NULL),
53 window_(NULL), 146 window_(NULL),
54 volume_name_func_(&GetVolumeName) { 147 get_device_info_func_(&GetDeviceInfo) {
55 Init(); 148 DCHECK(!g_removable_device_notifications_window_win);
56 } 149 g_removable_device_notifications_window_win = this;
57 150 }
58 RemovableDeviceNotificationsWindowWin::RemovableDeviceNotificationsWindowWin( 151
59 VolumeNameFunc volume_name_func) 152 // static
60 : atom_(0), 153 RemovableDeviceNotificationsWindowWin*
61 instance_(NULL), 154 RemovableDeviceNotificationsWindowWin::GetInstance() {
62 window_(NULL), 155 DCHECK(g_removable_device_notifications_window_win);
63 volume_name_func_(volume_name_func) { 156 return g_removable_device_notifications_window_win;
64 Init();
65 } 157 }
66 158
67 void RemovableDeviceNotificationsWindowWin::Init() { 159 void RemovableDeviceNotificationsWindowWin::Init() {
68 WNDCLASSEX window_class; 160 DoInit(&GetAttachedDevices);
69 base::win::InitializeWindowClass( 161 }
70 WindowClassName, 162
71 &base::win::WrappedWindowProc< 163 bool RemovableDeviceNotificationsWindowWin::GetDeviceInfoForPath(
72 RemovableDeviceNotificationsWindowWin::WndProcThunk>, 164 const FilePath& path,
73 0, 0, 0, NULL, NULL, NULL, NULL, NULL, 165 base::SystemMonitor::RemovableStorageInfo* device_info) {
74 &window_class); 166 string16 location;
75 instance_ = window_class.hInstance; 167 std::string unique_id;
76 atom_ = RegisterClassEx(&window_class); 168 string16 name;
77 DCHECK(atom_); 169 bool removable;
78 170 if (!get_device_info_func_(path, &location, &unique_id, &name, &removable))
79 window_ = CreateWindow(MAKEINTATOM(atom_), 0, 0, 0, 0, 0, 0, 0, 0, instance_, 171 return false;
80 0); 172
81 SetWindowLongPtr(window_, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this)); 173 // To compute the device id, the device type is needed. For removable
82 } 174 // devices, that requires knowing if there's a DCIM directory, which would
83 175 // require bouncing over to the file thread. Instead, just iterate the
84 RemovableDeviceNotificationsWindowWin::~RemovableDeviceNotificationsWindowWin( 176 // devices in SystemMonitor.
85 ) { 177 std::string device_id;
86 if (window_) 178 if (removable) {
87 DestroyWindow(window_); 179 std::vector<SystemMonitor::RemovableStorageInfo> attached_devices =
88 180 SystemMonitor::Get()->GetAttachedRemovableStorage();
89 if (atom_) 181 bool found = false;
90 UnregisterClass(MAKEINTATOM(atom_), instance_); 182 for (size_t i = 0; i < attached_devices.size(); i++) {
91 } 183 MediaStorageUtil::Type type;
92 184 std::string id;
93 LRESULT RemovableDeviceNotificationsWindowWin::OnDeviceChange(UINT event_type, 185 MediaStorageUtil::CrackDeviceId(attached_devices[i].device_id, &type,
94 DWORD data) { 186 &id);
187 if (id == unique_id) {
188 found = true;
189 device_id = attached_devices[i].device_id;
190 break;
191 }
192 }
193 if (!found)
194 return false;
195 } else {
196 device_id = MediaStorageUtil::MakeDeviceId(
197 MediaStorageUtil::FIXED_MASS_STORAGE, unique_id);
198 }
199
200 if (device_info) {
201 device_info->device_id = device_id;
202 device_info->name = name;
203 device_info->location = location;
204 }
205 return true;
206 }
207
208 void RemovableDeviceNotificationsWindowWin::InitForTest(
209 GetDeviceInfoFunc get_device_info_func,
210 GetAttachedDevicesFunc get_attached_devices_func) {
211 get_device_info_func_ = get_device_info_func;
212 DoInit(get_attached_devices_func);
213 }
214
215 void RemovableDeviceNotificationsWindowWin::OnDeviceChange(UINT event_type,
216 LPARAM data) {
95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 217 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
96 switch (event_type) { 218 switch (event_type) {
97 case DBT_DEVICEARRIVAL: { 219 case DBT_DEVICEARRIVAL: {
98 DWORD unitmask = GetVolumeBitMaskFromBroadcastHeader(data); 220 DWORD unitmask = GetVolumeBitMaskFromBroadcastHeader(data);
99 for (int i = 0; unitmask; ++i, unitmask >>= 1) { 221 for (int i = 0; unitmask; ++i, unitmask >>= 1) {
100 if (unitmask & 0x01) { 222 if (!(unitmask & 0x01))
101 FilePath::StringType drive(L"_:\\"); 223 continue;
102 drive[0] = L'A' + i; 224 AddNewDevice(DriveNumberToFilePath(i));
103 WCHAR volume_name[MAX_PATH + 1];
104 if ((*volume_name_func_)(drive.c_str(), volume_name, MAX_PATH + 1)) {
105 // TODO(kmadhusu) We need to look up a real device id as well as
106 // having a fall back for volume name.
107 std::string device_id = MediaStorageUtil::MakeDeviceId(
108 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM,
109 base::IntToString(i));
110 BrowserThread::PostTask(
111 BrowserThread::FILE, FROM_HERE,
112 base::Bind(&RemovableDeviceNotificationsWindowWin::
113 CheckDeviceTypeOnFileThread, this, device_id,
114 FilePath::StringType(volume_name), FilePath(drive)));
115 }
116 }
117 } 225 }
118 break; 226 break;
119 } 227 }
120 case DBT_DEVICEREMOVECOMPLETE: { 228 case DBT_DEVICEREMOVECOMPLETE: {
121 DWORD unitmask = GetVolumeBitMaskFromBroadcastHeader(data); 229 DWORD unitmask = GetVolumeBitMaskFromBroadcastHeader(data);
122 for (int i = 0; unitmask; ++i, unitmask >>= 1) { 230 for (int i = 0; unitmask; ++i, unitmask >>= 1) {
123 if (unitmask & 0x01) { 231 if (!(unitmask & 0x01))
124 std::string device_id = MediaStorageUtil::MakeDeviceId( 232 continue;
125 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM, 233
126 base::IntToString(i)); 234 FilePath device = DriveNumberToFilePath(i);
127 SystemMonitor::Get()->ProcessRemovableStorageDetached(device_id); 235 MountPointDeviceIdMap::const_iterator device_info =
128 } 236 device_ids_.find(device);
129 } 237 // If the devices isn't type removable (like a CD), it won't be there.
130 break; 238 if (device_info == device_ids_.end())
131 } 239 continue;
132 } 240
133 return TRUE; 241 SystemMonitor::Get()->ProcessRemovableStorageDetached(
134 } 242 device_info->second);
135 243 device_ids_.erase(device_info);
136 void RemovableDeviceNotificationsWindowWin::CheckDeviceTypeOnFileThread( 244 }
137 const std::string& id, 245 break;
138 const FilePath::StringType& device_name, 246 }
139 const FilePath& path) { 247 }
140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 248 }
141 if (!IsMediaDevice(path.value())) 249
142 return; 250 RemovableDeviceNotificationsWindowWin::
143 251 ~RemovableDeviceNotificationsWindowWin() {
144 BrowserThread::PostTask( 252 if (window_)
145 BrowserThread::UI, FROM_HERE, 253 DestroyWindow(window_);
146 base::Bind( 254
147 &RemovableDeviceNotificationsWindowWin:: 255 if (window_class_)
148 ProcessRemovableDeviceAttachedOnUIThread, 256 UnregisterClass(MAKEINTATOM(window_class_), instance_);
149 this, id, device_name, path)); 257
150 } 258 DCHECK_EQ(this, g_removable_device_notifications_window_win);
151 259 g_removable_device_notifications_window_win = NULL;
152 void 260 }
153 RemovableDeviceNotificationsWindowWin::ProcessRemovableDeviceAttachedOnUIThread( 261
154 const std::string& id, 262 // static
155 const FilePath::StringType& device_name, 263 LRESULT CALLBACK RemovableDeviceNotificationsWindowWin::WndProcThunk(
156 const FilePath& path) { 264 HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) {
157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 265 RemovableDeviceNotificationsWindowWin* msg_wnd =
158 266 reinterpret_cast<RemovableDeviceNotificationsWindowWin*>(
159 SystemMonitor::Get()->ProcessRemovableStorageAttached(id, 267 GetWindowLongPtr(hwnd, GWLP_USERDATA));
160 device_name, 268 if (msg_wnd)
161 path.value()); 269 return msg_wnd->WndProc(hwnd, message, wparam, lparam);
270 return ::DefWindowProc(hwnd, message, wparam, lparam);
162 } 271 }
163 272
164 LRESULT CALLBACK RemovableDeviceNotificationsWindowWin::WndProc( 273 LRESULT CALLBACK RemovableDeviceNotificationsWindowWin::WndProc(
165 HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { 274 HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) {
166 switch (message) { 275 switch (message) {
167 case WM_DEVICECHANGE: 276 case WM_DEVICECHANGE:
168 return OnDeviceChange(static_cast<UINT>(wparam), 277 OnDeviceChange(static_cast<UINT>(wparam), lparam);
169 static_cast<DWORD>(lparam)); 278 return TRUE;
170 default: 279 default:
171 break; 280 break;
172 } 281 }
173 282
174 return ::DefWindowProc(hwnd, message, wparam, lparam); 283 return ::DefWindowProc(hwnd, message, wparam, lparam);
175 } 284 }
176 285
177 // static 286 void RemovableDeviceNotificationsWindowWin::DoInit(
178 LRESULT CALLBACK RemovableDeviceNotificationsWindowWin::WndProcThunk( 287 GetAttachedDevicesFunc get_attached_devices_func) {
179 HWND hwnd, 288 WNDCLASSEX window_class;
180 UINT message, 289 base::win::InitializeWindowClass(
181 WPARAM wparam, 290 kWindowClassName,
182 LPARAM lparam) { 291 &WrappedWindowProc<RemovableDeviceNotificationsWindowWin::WndProcThunk>,
183 RemovableDeviceNotificationsWindowWin* msg_wnd = 292 0, 0, 0, NULL, NULL, NULL, NULL, NULL,
184 reinterpret_cast<RemovableDeviceNotificationsWindowWin*>( 293 &window_class);
185 GetWindowLongPtr(hwnd, GWLP_USERDATA)); 294 instance_ = window_class.hInstance;
186 if (msg_wnd) 295 window_class_ = RegisterClassEx(&window_class);
187 return msg_wnd->WndProc(hwnd, message, wparam, lparam); 296 DCHECK(window_class_);
188 return ::DefWindowProc(hwnd, message, wparam, lparam); 297
298 window_ = CreateWindow(MAKEINTATOM(window_class_), 0, 0, 0, 0, 0, 0, 0, 0,
299 instance_, 0);
300 SetWindowLongPtr(window_, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
301
302 std::vector<FilePath> removable_devices = get_attached_devices_func();
303 for (size_t i = 0; i < removable_devices.size(); i++)
304 AddNewDevice(removable_devices[i]);
305 }
306
307 void RemovableDeviceNotificationsWindowWin::AddNewDevice(
308 const FilePath& device_path) {
309 std::string unique_id;
310 string16 device_name;
311 bool removable;
312 if (!get_device_info_func_(device_path, NULL, &unique_id, &device_name,
313 &removable)) {
314 return;
315 }
316
317 if (!removable)
318 return;
319
320 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, base::Bind(
321 &RemovableDeviceNotificationsWindowWin::CheckDeviceTypeOnFileThread,
322 this, unique_id, device_name, device_path));
323 }
324
325 void RemovableDeviceNotificationsWindowWin::CheckDeviceTypeOnFileThread(
326 const std::string& unique_id,
327 const FilePath::StringType& device_name,
328 const FilePath& device) {
329 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
330
331 MediaStorageUtil::Type type =
332 MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM;
333 if (IsMediaDevice(device.value()))
334 type = MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM;
335 std::string device_id = MediaStorageUtil::MakeDeviceId(type, unique_id);
336
337 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
338 &RemovableDeviceNotificationsWindowWin::ProcessDeviceAttachedOnUIThread,
339 this, device_id, device_name, device));
340 }
341
342 void RemovableDeviceNotificationsWindowWin::ProcessDeviceAttachedOnUIThread(
343 const std::string& device_id,
344 const FilePath::StringType& device_name,
345 const FilePath& device) {
346 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
347
348 device_ids_[device] = device_id;
349 SystemMonitor::Get()->ProcessRemovableStorageAttached(device_id,
350 device_name,
351 device.value());
189 } 352 }
190 353
191 } // namespace chrome 354 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698