| 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/media_device_notifications_window_win.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 |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/file_path.h" | 12 #include "base/file_path.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/win/wrapped_window_proc.h" | 15 #include "base/win/wrapped_window_proc.h" |
| 16 #include "chrome/browser/system_monitor/media_device_notifications_utils.h" | 16 #include "chrome/browser/system_monitor/media_device_notifications_utils.h" |
| 17 #include "chrome/browser/system_monitor/media_storage_util.h" | 17 #include "chrome/browser/system_monitor/media_storage_util.h" |
| 18 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 19 | 19 |
| 20 using base::SystemMonitor; | 20 using base::SystemMonitor; |
| 21 using content::BrowserThread; | 21 using content::BrowserThread; |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 const wchar_t WindowClassName[] = L"Chrome_MediaDeviceNotificationWindow"; | 25 const wchar_t WindowClassName[] = L"Chrome_RemovableDeviceNotificationWindow"; |
| 26 | 26 |
| 27 LRESULT GetVolumeName(LPCWSTR drive, | 27 LRESULT GetVolumeName(LPCWSTR drive, |
| 28 LPWSTR volume_name, | 28 LPWSTR volume_name, |
| 29 unsigned int volume_name_len) { | 29 unsigned int volume_name_len) { |
| 30 return GetVolumeInformation(drive, volume_name, volume_name_len, NULL, NULL, | 30 return GetVolumeInformation(drive, volume_name, volume_name_len, NULL, NULL, |
| 31 NULL, NULL, 0); | 31 NULL, NULL, 0); |
| 32 } | 32 } |
| 33 | 33 |
| 34 // Returns 0 if the devicetype is not volume. | 34 // Returns 0 if the devicetype is not volume. |
| 35 DWORD GetVolumeBitMaskFromBroadcastHeader(DWORD data) { | 35 DWORD GetVolumeBitMaskFromBroadcastHeader(DWORD data) { |
| 36 PDEV_BROADCAST_HDR dev_broadcast_hdr = | 36 PDEV_BROADCAST_HDR dev_broadcast_hdr = |
| 37 reinterpret_cast<PDEV_BROADCAST_HDR>(data); | 37 reinterpret_cast<PDEV_BROADCAST_HDR>(data); |
| 38 if (dev_broadcast_hdr->dbch_devicetype == DBT_DEVTYP_VOLUME) { | 38 if (dev_broadcast_hdr->dbch_devicetype == DBT_DEVTYP_VOLUME) { |
| 39 PDEV_BROADCAST_VOLUME dev_broadcast_volume = | 39 PDEV_BROADCAST_VOLUME dev_broadcast_volume = |
| 40 reinterpret_cast<PDEV_BROADCAST_VOLUME>(dev_broadcast_hdr); | 40 reinterpret_cast<PDEV_BROADCAST_VOLUME>(dev_broadcast_hdr); |
| 41 return dev_broadcast_volume->dbcv_unitmask; | 41 return dev_broadcast_volume->dbcv_unitmask; |
| 42 } | 42 } |
| 43 return 0; | 43 return 0; |
| 44 } | 44 } |
| 45 | 45 |
| 46 } // namespace | 46 } // namespace |
| 47 | 47 |
| 48 namespace chrome { | 48 namespace chrome { |
| 49 | 49 |
| 50 MediaDeviceNotificationsWindowWin::MediaDeviceNotificationsWindowWin() | 50 RemovableDeviceNotificationsWindowWin::RemovableDeviceNotificationsWindowWin() |
| 51 : atom_(0), | 51 : atom_(0), |
| 52 instance_(NULL), | 52 instance_(NULL), |
| 53 window_(NULL), | 53 window_(NULL), |
| 54 volume_name_func_(&GetVolumeName) { | 54 volume_name_func_(&GetVolumeName) { |
| 55 Init(); | 55 Init(); |
| 56 } | 56 } |
| 57 | 57 |
| 58 MediaDeviceNotificationsWindowWin::MediaDeviceNotificationsWindowWin( | 58 RemovableDeviceNotificationsWindowWin::RemovableDeviceNotificationsWindowWin( |
| 59 VolumeNameFunc volume_name_func) | 59 VolumeNameFunc volume_name_func) |
| 60 : atom_(0), | 60 : atom_(0), |
| 61 instance_(NULL), | 61 instance_(NULL), |
| 62 window_(NULL), | 62 window_(NULL), |
| 63 volume_name_func_(volume_name_func) { | 63 volume_name_func_(volume_name_func) { |
| 64 Init(); | 64 Init(); |
| 65 } | 65 } |
| 66 | 66 |
| 67 void MediaDeviceNotificationsWindowWin::Init() { | 67 void RemovableDeviceNotificationsWindowWin::Init() { |
| 68 WNDCLASSEX window_class; | 68 WNDCLASSEX window_class; |
| 69 base::win::InitializeWindowClass( | 69 base::win::InitializeWindowClass( |
| 70 WindowClassName, | 70 WindowClassName, |
| 71 &base::win::WrappedWindowProc< | 71 &base::win::WrappedWindowProc< |
| 72 MediaDeviceNotificationsWindowWin::WndProcThunk>, | 72 RemovableDeviceNotificationsWindowWin::WndProcThunk>, |
| 73 0, 0, 0, NULL, NULL, NULL, NULL, NULL, | 73 0, 0, 0, NULL, NULL, NULL, NULL, NULL, |
| 74 &window_class); | 74 &window_class); |
| 75 instance_ = window_class.hInstance; | 75 instance_ = window_class.hInstance; |
| 76 atom_ = RegisterClassEx(&window_class); | 76 atom_ = RegisterClassEx(&window_class); |
| 77 DCHECK(atom_); | 77 DCHECK(atom_); |
| 78 | 78 |
| 79 window_ = CreateWindow(MAKEINTATOM(atom_), 0, 0, 0, 0, 0, 0, 0, 0, instance_, | 79 window_ = CreateWindow(MAKEINTATOM(atom_), 0, 0, 0, 0, 0, 0, 0, 0, instance_, |
| 80 0); | 80 0); |
| 81 SetWindowLongPtr(window_, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this)); | 81 SetWindowLongPtr(window_, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this)); |
| 82 } | 82 } |
| 83 | 83 |
| 84 MediaDeviceNotificationsWindowWin::~MediaDeviceNotificationsWindowWin() { | 84 RemovableDeviceNotificationsWindowWin::~RemovableDeviceNotificationsWindowWin( |
| 85 ) { |
| 85 if (window_) | 86 if (window_) |
| 86 DestroyWindow(window_); | 87 DestroyWindow(window_); |
| 87 | 88 |
| 88 if (atom_) | 89 if (atom_) |
| 89 UnregisterClass(MAKEINTATOM(atom_), instance_); | 90 UnregisterClass(MAKEINTATOM(atom_), instance_); |
| 90 } | 91 } |
| 91 | 92 |
| 92 LRESULT MediaDeviceNotificationsWindowWin::OnDeviceChange(UINT event_type, | 93 LRESULT RemovableDeviceNotificationsWindowWin::OnDeviceChange(UINT event_type, |
| 93 DWORD data) { | 94 DWORD data) { |
| 94 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 95 switch (event_type) { | 96 switch (event_type) { |
| 96 case DBT_DEVICEARRIVAL: { | 97 case DBT_DEVICEARRIVAL: { |
| 97 DWORD unitmask = GetVolumeBitMaskFromBroadcastHeader(data); | 98 DWORD unitmask = GetVolumeBitMaskFromBroadcastHeader(data); |
| 98 for (int i = 0; unitmask; ++i, unitmask >>= 1) { | 99 for (int i = 0; unitmask; ++i, unitmask >>= 1) { |
| 99 if (unitmask & 0x01) { | 100 if (unitmask & 0x01) { |
| 100 FilePath::StringType drive(L"_:\\"); | 101 FilePath::StringType drive(L"_:\\"); |
| 101 drive[0] = L'A' + i; | 102 drive[0] = L'A' + i; |
| 102 WCHAR volume_name[MAX_PATH + 1]; | 103 WCHAR volume_name[MAX_PATH + 1]; |
| 103 if ((*volume_name_func_)(drive.c_str(), volume_name, MAX_PATH + 1)) { | 104 if ((*volume_name_func_)(drive.c_str(), volume_name, MAX_PATH + 1)) { |
| 104 // TODO(kmadhusu) We need to look up a real device id as well as | 105 // TODO(kmadhusu) We need to look up a real device id as well as |
| 105 // having a fall back for volume name. | 106 // having a fall back for volume name. |
| 106 std::string device_id = MediaStorageUtil::MakeDeviceId( | 107 std::string device_id = MediaStorageUtil::MakeDeviceId( |
| 107 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM, | 108 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM, |
| 108 base::IntToString(i)); | 109 base::IntToString(i)); |
| 109 BrowserThread::PostTask( | 110 BrowserThread::PostTask( |
| 110 BrowserThread::FILE, FROM_HERE, | 111 BrowserThread::FILE, FROM_HERE, |
| 111 base::Bind(&MediaDeviceNotificationsWindowWin:: | 112 base::Bind(&RemovableDeviceNotificationsWindowWin:: |
| 112 CheckDeviceTypeOnFileThread, this, device_id, | 113 CheckDeviceTypeOnFileThread, this, device_id, |
| 113 FilePath::StringType(volume_name), FilePath(drive))); | 114 FilePath::StringType(volume_name), FilePath(drive))); |
| 114 } | 115 } |
| 115 } | 116 } |
| 116 } | 117 } |
| 117 break; | 118 break; |
| 118 } | 119 } |
| 119 case DBT_DEVICEREMOVECOMPLETE: { | 120 case DBT_DEVICEREMOVECOMPLETE: { |
| 120 DWORD unitmask = GetVolumeBitMaskFromBroadcastHeader(data); | 121 DWORD unitmask = GetVolumeBitMaskFromBroadcastHeader(data); |
| 121 for (int i = 0; unitmask; ++i, unitmask >>= 1) { | 122 for (int i = 0; unitmask; ++i, unitmask >>= 1) { |
| 122 if (unitmask & 0x01) { | 123 if (unitmask & 0x01) { |
| 123 std::string device_id = MediaStorageUtil::MakeDeviceId( | 124 std::string device_id = MediaStorageUtil::MakeDeviceId( |
| 124 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM, | 125 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM, |
| 125 base::IntToString(i)); | 126 base::IntToString(i)); |
| 126 SystemMonitor::Get()->ProcessRemovableStorageDetached(device_id); | 127 SystemMonitor::Get()->ProcessRemovableStorageDetached(device_id); |
| 127 } | 128 } |
| 128 } | 129 } |
| 129 break; | 130 break; |
| 130 } | 131 } |
| 131 } | 132 } |
| 132 return TRUE; | 133 return TRUE; |
| 133 } | 134 } |
| 134 | 135 |
| 135 void MediaDeviceNotificationsWindowWin::CheckDeviceTypeOnFileThread( | 136 void RemovableDeviceNotificationsWindowWin::CheckDeviceTypeOnFileThread( |
| 136 const std::string& id, | 137 const std::string& id, |
| 137 const FilePath::StringType& device_name, | 138 const FilePath::StringType& device_name, |
| 138 const FilePath& path) { | 139 const FilePath& path) { |
| 139 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 140 if (!IsMediaDevice(path.value())) | 141 if (!IsMediaDevice(path.value())) |
| 141 return; | 142 return; |
| 142 | 143 |
| 143 BrowserThread::PostTask( | 144 BrowserThread::PostTask( |
| 144 BrowserThread::UI, FROM_HERE, | 145 BrowserThread::UI, FROM_HERE, |
| 145 base::Bind( | 146 base::Bind( |
| 146 &MediaDeviceNotificationsWindowWin:: | 147 &RemovableDeviceNotificationsWindowWin:: |
| 147 ProcessMediaDeviceAttachedOnUIThread, | 148 ProcessRemovableDeviceAttachedOnUIThread, |
| 148 this, id, device_name, path)); | 149 this, id, device_name, path)); |
| 149 } | 150 } |
| 150 | 151 |
| 151 void MediaDeviceNotificationsWindowWin::ProcessMediaDeviceAttachedOnUIThread( | 152 void |
| 153 RemovableDeviceNotificationsWindowWin::ProcessRemovableDeviceAttachedOnUIThread( |
| 152 const std::string& id, | 154 const std::string& id, |
| 153 const FilePath::StringType& device_name, | 155 const FilePath::StringType& device_name, |
| 154 const FilePath& path) { | 156 const FilePath& path) { |
| 155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 156 | 158 |
| 157 SystemMonitor::Get()->ProcessRemovableStorageAttached(id, | 159 SystemMonitor::Get()->ProcessRemovableStorageAttached(id, |
| 158 device_name, | 160 device_name, |
| 159 path.value()); | 161 path.value()); |
| 160 } | 162 } |
| 161 | 163 |
| 162 LRESULT CALLBACK MediaDeviceNotificationsWindowWin::WndProc( | 164 LRESULT CALLBACK RemovableDeviceNotificationsWindowWin::WndProc( |
| 163 HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { | 165 HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { |
| 164 switch (message) { | 166 switch (message) { |
| 165 case WM_DEVICECHANGE: | 167 case WM_DEVICECHANGE: |
| 166 return OnDeviceChange(static_cast<UINT>(wparam), | 168 return OnDeviceChange(static_cast<UINT>(wparam), |
| 167 static_cast<DWORD>(lparam)); | 169 static_cast<DWORD>(lparam)); |
| 168 default: | 170 default: |
| 169 break; | 171 break; |
| 170 } | 172 } |
| 171 | 173 |
| 172 return ::DefWindowProc(hwnd, message, wparam, lparam); | 174 return ::DefWindowProc(hwnd, message, wparam, lparam); |
| 173 } | 175 } |
| 174 | 176 |
| 175 // static | 177 // static |
| 176 LRESULT CALLBACK MediaDeviceNotificationsWindowWin::WndProcThunk( | 178 LRESULT CALLBACK RemovableDeviceNotificationsWindowWin::WndProcThunk( |
| 177 HWND hwnd, | 179 HWND hwnd, |
| 178 UINT message, | 180 UINT message, |
| 179 WPARAM wparam, | 181 WPARAM wparam, |
| 180 LPARAM lparam) { | 182 LPARAM lparam) { |
| 181 MediaDeviceNotificationsWindowWin* msg_wnd = | 183 RemovableDeviceNotificationsWindowWin* msg_wnd = |
| 182 reinterpret_cast<MediaDeviceNotificationsWindowWin*>( | 184 reinterpret_cast<RemovableDeviceNotificationsWindowWin*>( |
| 183 GetWindowLongPtr(hwnd, GWLP_USERDATA)); | 185 GetWindowLongPtr(hwnd, GWLP_USERDATA)); |
| 184 if (msg_wnd) | 186 if (msg_wnd) |
| 185 return msg_wnd->WndProc(hwnd, message, wparam, lparam); | 187 return msg_wnd->WndProc(hwnd, message, wparam, lparam); |
| 186 return ::DefWindowProc(hwnd, message, wparam, lparam); | 188 return ::DefWindowProc(hwnd, message, wparam, lparam); |
| 187 } | 189 } |
| 188 | 190 |
| 189 } // namespace chrome | 191 } // namespace chrome |
| OLD | NEW |