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

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 (!GetVolumeNameForVolumeMountPoint(mount_point, guid, kMaxPathBufLen))
50 return false;
51 // In case it has two GUID's (see above mentioned blog), do it again.
52 if (!GetVolumeNameForVolumeMountPoint(guid, guid, kMaxPathBufLen))
53 return false;
54 WideToUTF8(guid, wcslen(guid), unique_id);
55 }
56
57 if (name) {
58 char16 volume_name[kMaxPathBufLen];
59 if (!GetVolumeInformation(mount_point, volume_name, kMaxPathBufLen, NULL,
60 NULL, NULL, NULL, 0)) {
61 return false;
62 }
63 if (wcslen(volume_name) > 0) {
64 *name = string16(volume_name);
65 } else {
66 *name = device_path.LossyDisplayName();
67 }
68 }
69
70 if (removable) {
71 UINT type = GetDriveType(mount_point);
Lei Zhang 2012/09/18 00:06:26 nit: merge with next line?
vandebo (ex-Chrome) 2012/09/18 00:16:21 Done.
72 *removable = (type == DRIVE_REMOVABLE);
73 }
74
75 return true;
76 }
77
78 std::vector<FilePath> GetAttachedDevices() {
79 std::vector<FilePath> result;
80 char16 volume_name[kMaxPathBufLen];
81 HANDLE find_handle = FindFirstVolume(volume_name, kMaxPathBufLen);
82 if (find_handle == INVALID_HANDLE_VALUE)
83 return result;
84
85 while (true) {
86 char16 volume_path[kMaxPathBufLen];
87 DWORD return_count;
88 if (GetVolumePathNamesForVolumeName(volume_name, volume_path,
89 kMaxPathBufLen, &return_count)) {
90 if (GetDriveType(volume_path) == DRIVE_REMOVABLE)
91 result.push_back(FilePath(volume_path));
92 } else {
93 DPLOG(ERROR);
Lei Zhang 2012/09/18 00:06:26 I haven't looked at this closely, but it stands ou
vandebo (ex-Chrome) 2012/09/18 00:16:21 The iterator increment is right after the if, so t
94 }
95 if (!FindNextVolume(find_handle, volume_name, kMaxPathBufLen)) {
96 if (GetLastError() != ERROR_NO_MORE_FILES)
97 DPLOG(ERROR);
98 break;
99 }
100 }
101
102 FindVolumeClose(find_handle);
103 return result;
32 } 104 }
33 105
34 // Returns 0 if the devicetype is not volume. 106 // Returns 0 if the devicetype is not volume.
35 DWORD GetVolumeBitMaskFromBroadcastHeader(DWORD data) { 107 uint32 GetVolumeBitMaskFromBroadcastHeader(LPARAM data) {
36 PDEV_BROADCAST_HDR dev_broadcast_hdr = 108 DEV_BROADCAST_VOLUME* dev_broadcast_volume =
37 reinterpret_cast<PDEV_BROADCAST_HDR>(data); 109 reinterpret_cast<DEV_BROADCAST_VOLUME*>(data);
38 if (dev_broadcast_hdr->dbch_devicetype == DBT_DEVTYP_VOLUME) { 110 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; 111 return dev_broadcast_volume->dbcv_unitmask;
42 }
43 return 0; 112 return 0;
44 } 113 }
45 114
115 FilePath DriveNumberToFilePath(int drive_number) {
116 string16 path(L"_:\\");
117 path[0] = L'A' + drive_number;
118 return FilePath(path);
119 }
120
46 } // namespace 121 } // namespace
47 122
48 namespace chrome { 123 namespace chrome {
49 124
50 RemovableDeviceNotificationsWindowWin::RemovableDeviceNotificationsWindowWin() 125 RemovableDeviceNotificationsWindowWin::RemovableDeviceNotificationsWindowWin()
51 : atom_(0), 126 : window_class_(0),
52 instance_(NULL), 127 instance_(NULL),
53 window_(NULL), 128 window_(NULL),
54 volume_name_func_(&GetVolumeName) { 129 get_device_info_func_(&GetDeviceInfo) {
55 Init(); 130 DCHECK(!g_removable_device_notifications_window_win);
56 } 131 g_removable_device_notifications_window_win = this;
57 132 }
58 RemovableDeviceNotificationsWindowWin::RemovableDeviceNotificationsWindowWin( 133
59 VolumeNameFunc volume_name_func) 134 // static
60 : atom_(0), 135 RemovableDeviceNotificationsWindowWin*
61 instance_(NULL), 136 RemovableDeviceNotificationsWindowWin::GetInstance() {
62 window_(NULL), 137 DCHECK(g_removable_device_notifications_window_win);
63 volume_name_func_(volume_name_func) { 138 return g_removable_device_notifications_window_win;
64 Init();
65 } 139 }
66 140
67 void RemovableDeviceNotificationsWindowWin::Init() { 141 void RemovableDeviceNotificationsWindowWin::Init() {
68 WNDCLASSEX window_class; 142 DoInit(&GetAttachedDevices);
69 base::win::InitializeWindowClass( 143 }
70 WindowClassName, 144
71 &base::win::WrappedWindowProc< 145 bool RemovableDeviceNotificationsWindowWin::GetDeviceInfoForPath(
72 RemovableDeviceNotificationsWindowWin::WndProcThunk>, 146 const FilePath& path,
73 0, 0, 0, NULL, NULL, NULL, NULL, NULL, 147 base::SystemMonitor::RemovableStorageInfo* device_info) {
74 &window_class); 148 string16 location;
75 instance_ = window_class.hInstance; 149 std::string unique_id;
76 atom_ = RegisterClassEx(&window_class); 150 string16 name;
77 DCHECK(atom_); 151 bool removable;
78 152 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_, 153 return false;
80 0); 154
81 SetWindowLongPtr(window_, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this)); 155 // To compute the device id, the device type is needed. For removable
82 } 156 // devices, that requires knowing if there's a DCIM directory, which would
83 157 // require bouncing over to the file thread. Instead, just iterate the
84 RemovableDeviceNotificationsWindowWin::~RemovableDeviceNotificationsWindowWin( 158 // devices in SystemMonitor.
85 ) { 159 std::string device_id;
86 if (window_) 160 if (removable) {
87 DestroyWindow(window_); 161 std::vector<SystemMonitor::RemovableStorageInfo> attached_devices =
88 162 SystemMonitor::Get()->GetAttachedRemovableStorage();
89 if (atom_) 163 bool found = false;
90 UnregisterClass(MAKEINTATOM(atom_), instance_); 164 for (size_t i = 0; i < attached_devices.size(); i++) {
91 } 165 MediaStorageUtil::Type type;
92 166 std::string id;
93 LRESULT RemovableDeviceNotificationsWindowWin::OnDeviceChange(UINT event_type, 167 MediaStorageUtil::CrackDeviceId(attached_devices[i].device_id, &type,
94 DWORD data) { 168 &id);
169 if (id == unique_id) {
170 found = true;
171 device_id = attached_devices[i].device_id;
172 break;
173 }
174 }
175 if (!found)
176 return false;
177 } else {
178 device_id = MediaStorageUtil::MakeDeviceId(
179 MediaStorageUtil::FIXED_MASS_STORAGE, unique_id);
180 }
181
182 if (device_info) {
183 device_info->device_id = device_id;
184 device_info->name = name;
185 device_info->location = location;
186 }
187 return true;
188 }
189
190 void RemovableDeviceNotificationsWindowWin::InitForTest(
191 GetDeviceInfoFunc get_device_info_func,
192 GetAttachedDevicesFunc get_attached_devices_func) {
193 get_device_info_func_ = get_device_info_func;
194 DoInit(get_attached_devices_func);
195 }
196
197 void RemovableDeviceNotificationsWindowWin::OnDeviceChange(UINT event_type,
198 LPARAM data) {
95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 199 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
96 switch (event_type) { 200 switch (event_type) {
97 case DBT_DEVICEARRIVAL: { 201 case DBT_DEVICEARRIVAL: {
98 DWORD unitmask = GetVolumeBitMaskFromBroadcastHeader(data); 202 DWORD unitmask = GetVolumeBitMaskFromBroadcastHeader(data);
99 for (int i = 0; unitmask; ++i, unitmask >>= 1) { 203 for (int i = 0; unitmask; ++i, unitmask >>= 1) {
100 if (unitmask & 0x01) { 204 if (!(unitmask & 0x01))
101 FilePath::StringType drive(L"_:\\"); 205 continue;
102 drive[0] = L'A' + i; 206 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 } 207 }
118 break; 208 break;
119 } 209 }
120 case DBT_DEVICEREMOVECOMPLETE: { 210 case DBT_DEVICEREMOVECOMPLETE: {
121 DWORD unitmask = GetVolumeBitMaskFromBroadcastHeader(data); 211 DWORD unitmask = GetVolumeBitMaskFromBroadcastHeader(data);
122 for (int i = 0; unitmask; ++i, unitmask >>= 1) { 212 for (int i = 0; unitmask; ++i, unitmask >>= 1) {
123 if (unitmask & 0x01) { 213 if (!(unitmask & 0x01))
124 std::string device_id = MediaStorageUtil::MakeDeviceId( 214 continue;
125 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM, 215
126 base::IntToString(i)); 216 FilePath device = DriveNumberToFilePath(i);
127 SystemMonitor::Get()->ProcessRemovableStorageDetached(device_id); 217 MountPointDeviceIdMap::const_iterator device_info =
128 } 218 device_ids_.find(device);
219 // If the devices isn't type removable (like a CD), it won't be there.
220 if (device_info == device_ids_.end())
221 continue;
222
223 SystemMonitor::Get()->ProcessRemovableStorageDetached(
224 device_info->second);
225 device_ids_.erase(device_info);
129 } 226 }
130 break; 227 break;
131 } 228 }
132 } 229 }
133 return TRUE; 230 }
134 } 231
135 232 RemovableDeviceNotificationsWindowWin::
136 void RemovableDeviceNotificationsWindowWin::CheckDeviceTypeOnFileThread( 233 ~RemovableDeviceNotificationsWindowWin() {
137 const std::string& id, 234 if (window_)
138 const FilePath::StringType& device_name, 235 DestroyWindow(window_);
139 const FilePath& path) { 236
140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 237 if (window_class_)
141 if (!IsMediaDevice(path.value())) 238 UnregisterClass(MAKEINTATOM(window_class_), instance_);
142 return; 239
143 240 DCHECK_EQ(this, g_removable_device_notifications_window_win);
144 BrowserThread::PostTask( 241 g_removable_device_notifications_window_win = NULL;
145 BrowserThread::UI, FROM_HERE, 242 }
146 base::Bind( 243
147 &RemovableDeviceNotificationsWindowWin:: 244 // static
148 ProcessRemovableDeviceAttachedOnUIThread, 245 LRESULT CALLBACK RemovableDeviceNotificationsWindowWin::WndProcThunk(
149 this, id, device_name, path)); 246 HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) {
150 } 247 RemovableDeviceNotificationsWindowWin* msg_wnd =
151 248 reinterpret_cast<RemovableDeviceNotificationsWindowWin*>(
152 void 249 GetWindowLongPtr(hwnd, GWLP_USERDATA));
153 RemovableDeviceNotificationsWindowWin::ProcessRemovableDeviceAttachedOnUIThread( 250 if (msg_wnd)
154 const std::string& id, 251 return msg_wnd->WndProc(hwnd, message, wparam, lparam);
155 const FilePath::StringType& device_name, 252 return ::DefWindowProc(hwnd, message, wparam, lparam);
156 const FilePath& path) {
157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
158
159 SystemMonitor::Get()->ProcessRemovableStorageAttached(id,
160 device_name,
161 path.value());
162 } 253 }
163 254
164 LRESULT CALLBACK RemovableDeviceNotificationsWindowWin::WndProc( 255 LRESULT CALLBACK RemovableDeviceNotificationsWindowWin::WndProc(
165 HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { 256 HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) {
166 switch (message) { 257 switch (message) {
167 case WM_DEVICECHANGE: 258 case WM_DEVICECHANGE:
168 return OnDeviceChange(static_cast<UINT>(wparam), 259 OnDeviceChange(static_cast<UINT>(wparam), lparam);
169 static_cast<DWORD>(lparam)); 260 return TRUE;
170 default: 261 default:
171 break; 262 break;
172 } 263 }
173 264
174 return ::DefWindowProc(hwnd, message, wparam, lparam); 265 return ::DefWindowProc(hwnd, message, wparam, lparam);
175 } 266 }
176 267
177 // static 268 void RemovableDeviceNotificationsWindowWin::DoInit(
178 LRESULT CALLBACK RemovableDeviceNotificationsWindowWin::WndProcThunk( 269 GetAttachedDevicesFunc get_attached_devices_func) {
179 HWND hwnd, 270 WNDCLASSEX window_class;
180 UINT message, 271 base::win::InitializeWindowClass(
181 WPARAM wparam, 272 kWindowClassName,
182 LPARAM lparam) { 273 &WrappedWindowProc<RemovableDeviceNotificationsWindowWin::WndProcThunk>,
183 RemovableDeviceNotificationsWindowWin* msg_wnd = 274 0, 0, 0, NULL, NULL, NULL, NULL, NULL,
184 reinterpret_cast<RemovableDeviceNotificationsWindowWin*>( 275 &window_class);
185 GetWindowLongPtr(hwnd, GWLP_USERDATA)); 276 instance_ = window_class.hInstance;
186 if (msg_wnd) 277 window_class_ = RegisterClassEx(&window_class);
187 return msg_wnd->WndProc(hwnd, message, wparam, lparam); 278 DCHECK(window_class_);
188 return ::DefWindowProc(hwnd, message, wparam, lparam); 279
280 window_ = CreateWindow(MAKEINTATOM(window_class_), 0, 0, 0, 0, 0, 0, 0, 0,
281 instance_, 0);
282 SetWindowLongPtr(window_, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
283
284 std::vector<FilePath> removable_devices = get_attached_devices_func();
285 for (size_t i = 0; i < removable_devices.size(); i++)
286 AddNewDevice(removable_devices[i]);
287 }
288
289 void RemovableDeviceNotificationsWindowWin::AddNewDevice(
290 const FilePath& device_path) {
291 std::string unique_id;
292 string16 device_name;
293 bool removable;
294 if (!get_device_info_func_(device_path, NULL, &unique_id, &device_name,
295 &removable)) {
296 return;
297 }
298
299 if (!removable)
300 return;
301
302 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, base::Bind(
Lei Zhang 2012/09/18 00:06:26 nit: This an be better formatted to show the argum
vandebo (ex-Chrome) 2012/09/18 00:16:21 Done.
303 &RemovableDeviceNotificationsWindowWin::CheckDeviceTypeOnFileThread,
304 this, unique_id, device_name, device_path));
305 }
306
307 void RemovableDeviceNotificationsWindowWin::CheckDeviceTypeOnFileThread(
308 const std::string& unique_id,
309 const FilePath::StringType& device_name,
310 const FilePath& device) {
311 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
312
313 MediaStorageUtil::Type type =
314 MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM;
315 if (IsMediaDevice(device.value()))
316 type = MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM;
317 std::string device_id = MediaStorageUtil::MakeDeviceId(type, unique_id);
318
319 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
320 &RemovableDeviceNotificationsWindowWin::ProcessDeviceAttachedOnUIThread,
321 this, device_id, device_name, device));
322 }
323
324 void RemovableDeviceNotificationsWindowWin::ProcessDeviceAttachedOnUIThread(
325 const std::string& device_id,
326 const FilePath::StringType& device_name,
327 const FilePath& device) {
328 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
329
330 device_ids_[device] = device_id;
331 SystemMonitor::Get()->ProcessRemovableStorageAttached(device_id,
332 device_name,
333 device.value());
189 } 334 }
190 335
191 } // namespace chrome 336 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698