| 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 #include "chrome/browser/system_monitor/volume_mount_watcher_win.h" | 5 #include "chrome/browser/system_monitor/volume_mount_watcher_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 | 10 |
| 11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 12 #include "base/string_number_conversions.h" | 12 #include "base/string_number_conversions.h" |
| 13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 14 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
| 15 #include "chrome/browser/system_monitor/media_device_notifications_utils.h" | 15 #include "chrome/browser/system_monitor/media_device_notifications_utils.h" |
| 16 #include "chrome/browser/system_monitor/media_storage_util.h" | 16 #include "chrome/browser/system_monitor/media_storage_util.h" |
| 17 #include "chrome/browser/system_monitor/removable_storage_notifications.h" | |
| 18 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 19 | 18 |
| 20 using content::BrowserThread; | 19 using content::BrowserThread; |
| 21 | 20 |
| 22 namespace { | 21 namespace { |
| 23 | 22 |
| 24 const DWORD kMaxPathBufLen = MAX_PATH + 1; | 23 const DWORD kMaxPathBufLen = MAX_PATH + 1; |
| 25 | 24 |
| 26 bool IsRemovable(const string16& mount_point) { | 25 bool IsRemovable(const string16& mount_point) { |
| 27 if (GetDriveType(mount_point.c_str()) != DRIVE_REMOVABLE) | 26 if (GetDriveType(mount_point.c_str()) != DRIVE_REMOVABLE) |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 } // namespace | 136 } // namespace |
| 138 | 137 |
| 139 namespace chrome { | 138 namespace chrome { |
| 140 | 139 |
| 141 const int kWorkerPoolNumThreads = 3; | 140 const int kWorkerPoolNumThreads = 3; |
| 142 const char* kWorkerPoolNamePrefix = "DeviceInfoPool"; | 141 const char* kWorkerPoolNamePrefix = "DeviceInfoPool"; |
| 143 | 142 |
| 144 VolumeMountWatcherWin::VolumeMountWatcherWin() | 143 VolumeMountWatcherWin::VolumeMountWatcherWin() |
| 145 : device_info_worker_pool_(new base::SequencedWorkerPool( | 144 : device_info_worker_pool_(new base::SequencedWorkerPool( |
| 146 kWorkerPoolNumThreads, kWorkerPoolNamePrefix)), | 145 kWorkerPoolNumThreads, kWorkerPoolNamePrefix)), |
| 147 weak_factory_(this) { | 146 weak_factory_(this), |
| 147 notifications_(NULL) { |
| 148 get_attached_devices_callback_ = base::Bind(&GetAttachedDevices); | 148 get_attached_devices_callback_ = base::Bind(&GetAttachedDevices); |
| 149 get_device_details_callback_ = base::Bind(&GetDeviceDetails); | 149 get_device_details_callback_ = base::Bind(&GetDeviceDetails); |
| 150 } | 150 } |
| 151 | 151 |
| 152 // static | 152 // static |
| 153 base::FilePath VolumeMountWatcherWin::DriveNumberToFilePath(int drive_number) { | 153 base::FilePath VolumeMountWatcherWin::DriveNumberToFilePath(int drive_number) { |
| 154 if (drive_number < 0 || drive_number > 25) | 154 if (drive_number < 0 || drive_number > 25) |
| 155 return base::FilePath(); | 155 return base::FilePath(); |
| 156 string16 path(L"_:\\"); | 156 string16 path(L"_:\\"); |
| 157 path[0] = L'A' + drive_number; | 157 path[0] = L'A' + drive_number; |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 if (!(unitmask & 0x01)) | 300 if (!(unitmask & 0x01)) |
| 301 continue; | 301 continue; |
| 302 HandleDeviceDetachEventOnUIThread(DriveNumberToFilePath(i).value()); | 302 HandleDeviceDetachEventOnUIThread(DriveNumberToFilePath(i).value()); |
| 303 } | 303 } |
| 304 } | 304 } |
| 305 break; | 305 break; |
| 306 } | 306 } |
| 307 } | 307 } |
| 308 } | 308 } |
| 309 | 309 |
| 310 void VolumeMountWatcherWin::SetNotifications( |
| 311 RemovableStorageNotifications::Receiver* notifications) { |
| 312 notifications_ = notifications; |
| 313 } |
| 314 |
| 310 VolumeMountWatcherWin::~VolumeMountWatcherWin() { | 315 VolumeMountWatcherWin::~VolumeMountWatcherWin() { |
| 311 weak_factory_.InvalidateWeakPtrs(); | 316 weak_factory_.InvalidateWeakPtrs(); |
| 312 } | 317 } |
| 313 | 318 |
| 314 | 319 |
| 315 void VolumeMountWatcherWin::HandleDeviceAttachEventOnUIThread( | 320 void VolumeMountWatcherWin::HandleDeviceAttachEventOnUIThread( |
| 316 const base::FilePath& device_path, | 321 const base::FilePath& device_path, |
| 317 const MountPointInfo& info) { | 322 const MountPointInfo& info) { |
| 318 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 323 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 319 | 324 |
| 320 device_metadata_[device_path.value()] = info; | 325 device_metadata_[device_path.value()] = info; |
| 321 | 326 |
| 322 DeviceCheckComplete(device_path); | 327 DeviceCheckComplete(device_path); |
| 323 | 328 |
| 324 // Don't call removable storage observers for fixed volumes. | 329 // Don't call removable storage observers for fixed volumes. |
| 325 if (!info.removable) | 330 if (!info.removable) |
| 326 return; | 331 return; |
| 327 | 332 |
| 328 RemovableStorageNotifications* notifications = | 333 if (notifications_) { |
| 329 RemovableStorageNotifications::GetInstance(); | |
| 330 if (notifications) { | |
| 331 string16 display_name = GetDisplayNameForDevice(0, info.name); | 334 string16 display_name = GetDisplayNameForDevice(0, info.name); |
| 332 notifications->ProcessAttach(info.device_id, display_name, | 335 notifications_->ProcessAttach(info.device_id, display_name, |
| 333 device_path.value()); | 336 device_path.value()); |
| 334 } | 337 } |
| 335 } | 338 } |
| 336 | 339 |
| 337 void VolumeMountWatcherWin::HandleDeviceDetachEventOnUIThread( | 340 void VolumeMountWatcherWin::HandleDeviceDetachEventOnUIThread( |
| 338 const string16& device_location) { | 341 const string16& device_location) { |
| 339 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 342 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 340 | 343 |
| 341 MountPointDeviceMetadataMap::const_iterator device_info = | 344 MountPointDeviceMetadataMap::const_iterator device_info = |
| 342 device_metadata_.find(device_location); | 345 device_metadata_.find(device_location); |
| 343 // If the device isn't type removable (like a CD), it won't be there. | 346 // If the device isn't type removable (like a CD), it won't be there. |
| 344 if (device_info == device_metadata_.end()) | 347 if (device_info == device_metadata_.end()) |
| 345 return; | 348 return; |
| 346 | 349 |
| 347 RemovableStorageNotifications* notifications = | 350 if (notifications_) |
| 348 RemovableStorageNotifications::GetInstance(); | 351 notifications_->ProcessDetach(device_info->second.device_id); |
| 349 if (notifications) | |
| 350 notifications->ProcessDetach(device_info->second.device_id); | |
| 351 device_metadata_.erase(device_info); | 352 device_metadata_.erase(device_info); |
| 352 } | 353 } |
| 353 | 354 |
| 354 } // namespace chrome | 355 } // namespace chrome |
| OLD | NEW |