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

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

Powered by Google App Engine
This is Rietveld 408576698