| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/system_monitor/removable_device_notifications_window_wi
n.h" |
| 6 |
| 7 #include <windows.h> |
| 8 #include <dbt.h> |
| 9 |
| 10 #include <string> |
| 11 |
| 12 #include "base/file_path.h" |
| 13 #include "base/string_number_conversions.h" |
| 14 #include "base/system_monitor/system_monitor.h" |
| 15 #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_storage_util.h" |
| 18 #include "content/public/browser/browser_thread.h" |
| 19 |
| 20 using base::SystemMonitor; |
| 21 using content::BrowserThread; |
| 22 |
| 23 namespace { |
| 24 |
| 25 const wchar_t WindowClassName[] = L"Chrome_RemovableDeviceNotificationWindow"; |
| 26 |
| 27 LRESULT GetVolumeName(LPCWSTR drive, |
| 28 LPWSTR volume_name, |
| 29 unsigned int volume_name_len) { |
| 30 return GetVolumeInformation(drive, volume_name, volume_name_len, NULL, NULL, |
| 31 NULL, NULL, 0); |
| 32 } |
| 33 |
| 34 // Returns 0 if the devicetype is not volume. |
| 35 DWORD GetVolumeBitMaskFromBroadcastHeader(DWORD data) { |
| 36 PDEV_BROADCAST_HDR dev_broadcast_hdr = |
| 37 reinterpret_cast<PDEV_BROADCAST_HDR>(data); |
| 38 if (dev_broadcast_hdr->dbch_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; |
| 42 } |
| 43 return 0; |
| 44 } |
| 45 |
| 46 } // namespace |
| 47 |
| 48 namespace chrome { |
| 49 |
| 50 RemovableDeviceNotificationsWindowWin::RemovableDeviceNotificationsWindowWin() |
| 51 : atom_(0), |
| 52 instance_(NULL), |
| 53 window_(NULL), |
| 54 volume_name_func_(&GetVolumeName) { |
| 55 Init(); |
| 56 } |
| 57 |
| 58 RemovableDeviceNotificationsWindowWin::RemovableDeviceNotificationsWindowWin( |
| 59 VolumeNameFunc volume_name_func) |
| 60 : atom_(0), |
| 61 instance_(NULL), |
| 62 window_(NULL), |
| 63 volume_name_func_(volume_name_func) { |
| 64 Init(); |
| 65 } |
| 66 |
| 67 void RemovableDeviceNotificationsWindowWin::Init() { |
| 68 WNDCLASSEX window_class; |
| 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 } |
| 83 |
| 84 RemovableDeviceNotificationsWindowWin::~RemovableDeviceNotificationsWindowWin( |
| 85 ) { |
| 86 if (window_) |
| 87 DestroyWindow(window_); |
| 88 |
| 89 if (atom_) |
| 90 UnregisterClass(MAKEINTATOM(atom_), instance_); |
| 91 } |
| 92 |
| 93 LRESULT RemovableDeviceNotificationsWindowWin::OnDeviceChange(UINT event_type, |
| 94 DWORD data) { |
| 95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 96 switch (event_type) { |
| 97 case DBT_DEVICEARRIVAL: { |
| 98 DWORD unitmask = GetVolumeBitMaskFromBroadcastHeader(data); |
| 99 for (int i = 0; unitmask; ++i, unitmask >>= 1) { |
| 100 if (unitmask & 0x01) { |
| 101 FilePath::StringType drive(L"_:\\"); |
| 102 drive[0] = L'A' + 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 } |
| 118 break; |
| 119 } |
| 120 case DBT_DEVICEREMOVECOMPLETE: { |
| 121 DWORD unitmask = GetVolumeBitMaskFromBroadcastHeader(data); |
| 122 for (int i = 0; unitmask; ++i, unitmask >>= 1) { |
| 123 if (unitmask & 0x01) { |
| 124 std::string device_id = MediaStorageUtil::MakeDeviceId( |
| 125 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM, |
| 126 base::IntToString(i)); |
| 127 SystemMonitor::Get()->ProcessRemovableStorageDetached(device_id); |
| 128 } |
| 129 } |
| 130 break; |
| 131 } |
| 132 } |
| 133 return TRUE; |
| 134 } |
| 135 |
| 136 void RemovableDeviceNotificationsWindowWin::CheckDeviceTypeOnFileThread( |
| 137 const std::string& id, |
| 138 const FilePath::StringType& device_name, |
| 139 const FilePath& path) { |
| 140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 141 if (!IsMediaDevice(path.value())) |
| 142 return; |
| 143 |
| 144 BrowserThread::PostTask( |
| 145 BrowserThread::UI, FROM_HERE, |
| 146 base::Bind( |
| 147 &RemovableDeviceNotificationsWindowWin:: |
| 148 ProcessRemovableDeviceAttachedOnUIThread, |
| 149 this, id, device_name, path)); |
| 150 } |
| 151 |
| 152 void |
| 153 RemovableDeviceNotificationsWindowWin::ProcessRemovableDeviceAttachedOnUIThread( |
| 154 const std::string& id, |
| 155 const FilePath::StringType& device_name, |
| 156 const FilePath& path) { |
| 157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 158 |
| 159 SystemMonitor::Get()->ProcessRemovableStorageAttached(id, |
| 160 device_name, |
| 161 path.value()); |
| 162 } |
| 163 |
| 164 LRESULT CALLBACK RemovableDeviceNotificationsWindowWin::WndProc( |
| 165 HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { |
| 166 switch (message) { |
| 167 case WM_DEVICECHANGE: |
| 168 return OnDeviceChange(static_cast<UINT>(wparam), |
| 169 static_cast<DWORD>(lparam)); |
| 170 default: |
| 171 break; |
| 172 } |
| 173 |
| 174 return ::DefWindowProc(hwnd, message, wparam, lparam); |
| 175 } |
| 176 |
| 177 // static |
| 178 LRESULT CALLBACK RemovableDeviceNotificationsWindowWin::WndProcThunk( |
| 179 HWND hwnd, |
| 180 UINT message, |
| 181 WPARAM wparam, |
| 182 LPARAM lparam) { |
| 183 RemovableDeviceNotificationsWindowWin* msg_wnd = |
| 184 reinterpret_cast<RemovableDeviceNotificationsWindowWin*>( |
| 185 GetWindowLongPtr(hwnd, GWLP_USERDATA)); |
| 186 if (msg_wnd) |
| 187 return msg_wnd->WndProc(hwnd, message, wparam, lparam); |
| 188 return ::DefWindowProc(hwnd, message, wparam, lparam); |
| 189 } |
| 190 |
| 191 } // namespace chrome |
| OLD | NEW |