| 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/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 #include <fileapi.h> | 9 #include <fileapi.h> |
| 10 | 10 |
| 11 #include "base/file_path.h" | |
| 12 #include "base/win/wrapped_window_proc.h" | 11 #include "base/win/wrapped_window_proc.h" |
| 13 #include "chrome/browser/system_monitor/media_storage_util.h" | 12 #include "chrome/browser/system_monitor/media_storage_util.h" |
| 14 | 13 #include "chrome/browser/system_monitor/portable_device_watcher_win.h" |
| 15 using base::SystemMonitor; | 14 #include "chrome/browser/system_monitor/removable_device_constants.h" |
| 16 using base::win::WrappedWindowProc; | 15 #include "chrome/browser/system_monitor/volume_mount_watcher_win.h" |
| 17 | 16 |
| 18 namespace chrome { | 17 namespace chrome { |
| 19 | 18 |
| 20 namespace { | 19 namespace { |
| 21 | 20 |
| 22 const char16 kWindowClassName[] = L"Chrome_RemovableDeviceNotificationWindow"; | 21 const char16 kWindowClassName[] = L"Chrome_RemovableDeviceNotificationWindow"; |
| 23 | 22 |
| 24 RemovableDeviceNotificationsWindowWin* | 23 RemovableDeviceNotificationsWindowWin* |
| 25 g_removable_device_notifications_window_win = NULL; | 24 g_removable_device_notifications_window_win = NULL; |
| 26 | 25 |
| 27 } // namespace | 26 } // namespace |
| 28 | 27 |
| 28 // Manages portable device notification handle for |
| 29 // RemovableDeviceNotificationsWindowWin. |
| 30 class RemovableDeviceNotificationsWindowWin::PortableDeviceNotifications { |
| 31 public: |
| 32 explicit PortableDeviceNotifications(HWND hwnd); |
| 33 ~PortableDeviceNotifications(); |
| 34 |
| 35 private: |
| 36 // Register window handle to receive portable device notifications details. |
| 37 void Register(HWND hwnd); |
| 38 |
| 39 // Unregisters device notifications. |
| 40 void Unregister(); |
| 41 |
| 42 HDEVNOTIFY notifications_; |
| 43 |
| 44 DISALLOW_IMPLICIT_CONSTRUCTORS(PortableDeviceNotifications); |
| 45 }; |
| 46 |
| 47 RemovableDeviceNotificationsWindowWin::PortableDeviceNotifications:: |
| 48 PortableDeviceNotifications(HWND hwnd) { |
| 49 Register(hwnd); |
| 50 } |
| 51 |
| 52 RemovableDeviceNotificationsWindowWin::PortableDeviceNotifications:: |
| 53 ~PortableDeviceNotifications() { |
| 54 Unregister(); |
| 55 } |
| 56 |
| 57 void RemovableDeviceNotificationsWindowWin::PortableDeviceNotifications:: |
| 58 Register(HWND hwnd) { |
| 59 GUID dev_interface_guid = GUID_NULL; |
| 60 HRESULT hr = CLSIDFromString(kWPDDevInterfaceGUID, &dev_interface_guid); |
| 61 if (FAILED(hr)) |
| 62 return; |
| 63 DEV_BROADCAST_DEVICEINTERFACE db = {sizeof(DEV_BROADCAST_DEVICEINTERFACE), |
| 64 DBT_DEVTYP_DEVICEINTERFACE, 0, |
| 65 dev_interface_guid}; |
| 66 notifications_ = RegisterDeviceNotification(hwnd, &db, |
| 67 DEVICE_NOTIFY_WINDOW_HANDLE); |
| 68 } |
| 69 |
| 70 void RemovableDeviceNotificationsWindowWin::PortableDeviceNotifications:: |
| 71 Unregister() { |
| 72 UnregisterDeviceNotification(notifications_); |
| 73 } |
| 74 |
| 29 // static | 75 // static |
| 30 RemovableDeviceNotificationsWindowWin* | 76 RemovableDeviceNotificationsWindowWin* |
| 31 RemovableDeviceNotificationsWindowWin::Create() { | 77 RemovableDeviceNotificationsWindowWin::Create() { |
| 32 return new RemovableDeviceNotificationsWindowWin(new VolumeMountWatcherWin()); | 78 return new RemovableDeviceNotificationsWindowWin( |
| 79 new VolumeMountWatcherWin(), new PortableDeviceWatcherWin()); |
| 33 } | 80 } |
| 34 | 81 |
| 35 RemovableDeviceNotificationsWindowWin:: | 82 RemovableDeviceNotificationsWindowWin:: |
| 36 RemovableDeviceNotificationsWindowWin( | 83 RemovableDeviceNotificationsWindowWin( |
| 37 VolumeMountWatcherWin* volume_mount_watcher) | 84 VolumeMountWatcherWin* volume_mount_watcher, |
| 85 PortableDeviceWatcherWin* portable_device_watcher) |
| 38 : window_class_(0), | 86 : window_class_(0), |
| 39 instance_(NULL), | 87 instance_(NULL), |
| 40 window_(NULL), | 88 window_(NULL), |
| 41 volume_mount_watcher_(volume_mount_watcher) { | 89 volume_mount_watcher_(volume_mount_watcher), |
| 90 portable_device_watcher_(portable_device_watcher) { |
| 42 DCHECK(volume_mount_watcher_); | 91 DCHECK(volume_mount_watcher_); |
| 92 DCHECK(portable_device_watcher_); |
| 43 DCHECK(!g_removable_device_notifications_window_win); | 93 DCHECK(!g_removable_device_notifications_window_win); |
| 44 g_removable_device_notifications_window_win = this; | 94 g_removable_device_notifications_window_win = this; |
| 45 } | 95 } |
| 46 | 96 |
| 47 RemovableDeviceNotificationsWindowWin:: | 97 RemovableDeviceNotificationsWindowWin:: |
| 48 ~RemovableDeviceNotificationsWindowWin() { | 98 ~RemovableDeviceNotificationsWindowWin() { |
| 49 if (window_) | 99 if (window_) |
| 50 DestroyWindow(window_); | 100 DestroyWindow(window_); |
| 51 | 101 |
| 52 if (window_class_) | 102 if (window_class_) |
| 53 UnregisterClass(MAKEINTATOM(window_class_), instance_); | 103 UnregisterClass(MAKEINTATOM(window_class_), instance_); |
| 54 | 104 |
| 55 DCHECK_EQ(this, g_removable_device_notifications_window_win); | 105 DCHECK_EQ(this, g_removable_device_notifications_window_win); |
| 56 g_removable_device_notifications_window_win = NULL; | 106 g_removable_device_notifications_window_win = NULL; |
| 57 } | 107 } |
| 58 | 108 |
| 59 // static | 109 // static |
| 60 RemovableDeviceNotificationsWindowWin* | 110 RemovableDeviceNotificationsWindowWin* |
| 61 RemovableDeviceNotificationsWindowWin::GetInstance() { | 111 RemovableDeviceNotificationsWindowWin::GetInstance() { |
| 62 DCHECK(g_removable_device_notifications_window_win); | 112 DCHECK(g_removable_device_notifications_window_win); |
| 63 return g_removable_device_notifications_window_win; | 113 return g_removable_device_notifications_window_win; |
| 64 } | 114 } |
| 65 | 115 |
| 66 void RemovableDeviceNotificationsWindowWin::Init() { | 116 void RemovableDeviceNotificationsWindowWin::Init() { |
| 67 volume_mount_watcher_->Init(); | 117 volume_mount_watcher_->Init(); |
| 118 portable_device_watcher_->Init(); |
| 68 | 119 |
| 69 WNDCLASSEX window_class; | 120 WNDCLASSEX window_class; |
| 70 base::win::InitializeWindowClass( | 121 base::win::InitializeWindowClass( |
| 71 kWindowClassName, | 122 kWindowClassName, |
| 72 &WrappedWindowProc<RemovableDeviceNotificationsWindowWin::WndProcThunk>, | 123 &base::win::WrappedWindowProc< |
| 124 RemovableDeviceNotificationsWindowWin::WndProcThunk>, |
| 73 0, 0, 0, NULL, NULL, NULL, NULL, NULL, | 125 0, 0, 0, NULL, NULL, NULL, NULL, NULL, |
| 74 &window_class); | 126 &window_class); |
| 75 instance_ = window_class.hInstance; | 127 instance_ = window_class.hInstance; |
| 76 window_class_ = RegisterClassEx(&window_class); | 128 window_class_ = RegisterClassEx(&window_class); |
| 77 DCHECK(window_class_); | 129 DCHECK(window_class_); |
| 78 | 130 |
| 79 window_ = CreateWindow(MAKEINTATOM(window_class_), 0, 0, 0, 0, 0, 0, 0, 0, | 131 window_ = CreateWindow(MAKEINTATOM(window_class_), 0, 0, 0, 0, 0, 0, 0, 0, |
| 80 instance_, 0); | 132 instance_, 0); |
| 81 SetWindowLongPtr(window_, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this)); | 133 SetWindowLongPtr(window_, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this)); |
| 134 portable_device_notifications_.reset( |
| 135 new PortableDeviceNotifications(window_)); |
| 82 } | 136 } |
| 83 | 137 |
| 84 bool RemovableDeviceNotificationsWindowWin::GetDeviceInfoForPath( | 138 bool RemovableDeviceNotificationsWindowWin::GetDeviceInfoForPath( |
| 85 const FilePath& path, | 139 const FilePath& path, |
| 86 base::SystemMonitor::RemovableStorageInfo* device_info) { | 140 base::SystemMonitor::RemovableStorageInfo* device_info) { |
| 87 string16 location; | 141 string16 location; |
| 88 std::string unique_id; | 142 std::string unique_id; |
| 89 string16 name; | 143 string16 name; |
| 90 bool removable; | 144 bool removable; |
| 91 if (!GetDeviceInfo(path, &location, &unique_id, &name, &removable)) | 145 if (!GetDeviceInfo(path, &location, &unique_id, &name, &removable)) |
| 92 return false; | 146 return false; |
| 93 | 147 |
| 94 // To compute the device id, the device type is needed. For removable | 148 // To compute the device id, the device type is needed. For removable |
| 95 // devices, that requires knowing if there's a DCIM directory, which would | 149 // devices, that requires knowing if there's a DCIM directory, which would |
| 96 // require bouncing over to the file thread. Instead, just iterate the | 150 // require bouncing over to the file thread. Instead, just iterate the |
| 97 // devices in SystemMonitor. | 151 // devices in base::SystemMonitor. |
| 98 std::string device_id; | 152 std::string device_id; |
| 99 if (removable) { | 153 if (removable) { |
| 100 std::vector<SystemMonitor::RemovableStorageInfo> attached_devices = | 154 std::vector<base::SystemMonitor::RemovableStorageInfo> attached_devices = |
| 101 SystemMonitor::Get()->GetAttachedRemovableStorage(); | 155 base::SystemMonitor::Get()->GetAttachedRemovableStorage(); |
| 102 bool found = false; | 156 bool found = false; |
| 103 for (size_t i = 0; i < attached_devices.size(); i++) { | 157 for (size_t i = 0; i < attached_devices.size(); i++) { |
| 104 MediaStorageUtil::Type type; | 158 MediaStorageUtil::Type type; |
| 105 std::string id; | 159 std::string id; |
| 106 MediaStorageUtil::CrackDeviceId(attached_devices[i].device_id, &type, | 160 MediaStorageUtil::CrackDeviceId(attached_devices[i].device_id, &type, |
| 107 &id); | 161 &id); |
| 108 if (id == unique_id) { | 162 if (id == unique_id) { |
| 109 found = true; | 163 found = true; |
| 110 device_id = attached_devices[i].device_id; | 164 device_id = attached_devices[i].device_id; |
| 111 break; | 165 break; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 break; | 201 break; |
| 148 } | 202 } |
| 149 | 203 |
| 150 return ::DefWindowProc(hwnd, message, wparam, lparam); | 204 return ::DefWindowProc(hwnd, message, wparam, lparam); |
| 151 } | 205 } |
| 152 | 206 |
| 153 bool RemovableDeviceNotificationsWindowWin::GetDeviceInfo( | 207 bool RemovableDeviceNotificationsWindowWin::GetDeviceInfo( |
| 154 const FilePath& device_path, string16* device_location, | 208 const FilePath& device_path, string16* device_location, |
| 155 std::string* unique_id, string16* name, bool* removable) { | 209 std::string* unique_id, string16* name, bool* removable) { |
| 156 return volume_mount_watcher_->GetDeviceInfo(device_path, device_location, | 210 return volume_mount_watcher_->GetDeviceInfo(device_path, device_location, |
| 157 unique_id, name, removable); | 211 unique_id, name, removable) || |
| 212 portable_device_watcher_->GetDeviceInfo(device_path, device_location, |
| 213 unique_id, name, removable); |
| 158 } | 214 } |
| 159 | 215 |
| 160 void RemovableDeviceNotificationsWindowWin::OnDeviceChange(UINT event_type, | 216 void RemovableDeviceNotificationsWindowWin::OnDeviceChange(UINT event_type, |
| 161 LPARAM data) { | 217 LPARAM data) { |
| 162 volume_mount_watcher_->OnWindowMessage(event_type, data); | 218 volume_mount_watcher_->OnWindowMessage(event_type, data); |
| 219 portable_device_watcher_->OnWindowMessage(event_type, data); |
| 163 } | 220 } |
| 164 | 221 |
| 165 } // namespace chrome | 222 } // namespace chrome |
| OLD | NEW |