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 "content/browser/system_message_window_win.h" | 5 #include "chrome/browser/media_gallery/media_device_notification_window_win.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 #include <dbt.h> | 8 #include <dbt.h> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/file_path.h" | 11 #include "base/file_path.h" |
12 #include "base/sys_string_conversions.h" | 12 #include "base/sys_string_conversions.h" |
13 #include "base/system_monitor/system_monitor.h" | 13 #include "base/system_monitor/system_monitor.h" |
14 #include "base/win/wrapped_window_proc.h" | 14 #include "base/win/wrapped_window_proc.h" |
15 | 15 |
16 static const wchar_t* const WindowClassName = L"Chrome_SystemMessageWindow"; | 16 static const wchar_t* const WindowClassName = |
17 L"Chrome_MediaDeviceNotificationWindow"; | |
17 | 18 |
18 namespace { | 19 namespace { |
19 | 20 |
20 LRESULT GetVolumeName(LPCWSTR drive, | 21 LRESULT GetVolumeName(LPCWSTR drive, |
21 LPWSTR volume_name, | 22 LPWSTR volume_name, |
22 unsigned int volume_name_len) { | 23 unsigned int volume_name_len) { |
23 return GetVolumeInformation(drive, volume_name, volume_name_len, NULL, NULL, | 24 return GetVolumeInformation(drive, volume_name, volume_name_len, NULL, NULL, |
24 NULL, NULL, 0); | 25 NULL, NULL, 0); |
25 } | 26 } |
26 | 27 |
27 // Returns 0 if the devicetype is not volume. | 28 // Returns 0 if the devicetype is not volume. |
28 DWORD GetVolumeBitMaskFromBroadcastHeader(DWORD data) { | 29 DWORD GetVolumeBitMaskFromBroadcastHeader(DWORD data) { |
29 PDEV_BROADCAST_HDR dev_broadcast_hdr = | 30 PDEV_BROADCAST_HDR dev_broadcast_hdr = |
30 reinterpret_cast<PDEV_BROADCAST_HDR>(data); | 31 reinterpret_cast<PDEV_BROADCAST_HDR>(data); |
31 if (dev_broadcast_hdr->dbch_devicetype == DBT_DEVTYP_VOLUME) { | 32 if (dev_broadcast_hdr->dbch_devicetype == DBT_DEVTYP_VOLUME) { |
32 PDEV_BROADCAST_VOLUME dev_broadcast_volume = | 33 PDEV_BROADCAST_VOLUME dev_broadcast_volume = |
33 reinterpret_cast<PDEV_BROADCAST_VOLUME>(dev_broadcast_hdr); | 34 reinterpret_cast<PDEV_BROADCAST_VOLUME>(dev_broadcast_hdr); |
34 return dev_broadcast_volume->dbcv_unitmask; | 35 return dev_broadcast_volume->dbcv_unitmask; |
35 } | 36 } |
36 return 0; | 37 return 0; |
37 } | 38 } |
38 | 39 |
39 } // namespace | 40 } // namespace |
40 | 41 |
42 namespace chrome { | |
41 | 43 |
42 SystemMessageWindowWin::SystemMessageWindowWin() | 44 MediaDeviceNotificationWindowWin::MediaDeviceNotificationWindowWin() |
43 : volume_name_func_(&GetVolumeName) { | 45 : volume_name_func_(&GetVolumeName) { |
44 Init(); | 46 Init(); |
45 } | 47 } |
46 | 48 |
47 SystemMessageWindowWin::SystemMessageWindowWin(VolumeNameFunc volume_name_func) | 49 MediaDeviceNotificationWindowWin::MediaDeviceNotificationWindowWin( |
48 : volume_name_func_(volume_name_func) { | 50 VolumeNameFunc volume_name_func) : volume_name_func_(volume_name_func) { |
49 Init(); | 51 Init(); |
50 } | 52 } |
51 | 53 |
52 void SystemMessageWindowWin::Init() { | 54 void MediaDeviceNotificationWindowWin::Init() { |
53 HINSTANCE hinst = GetModuleHandle(NULL); | 55 HINSTANCE hinst = GetModuleHandle(NULL); |
54 | 56 |
55 WNDCLASSEX wc = {0}; | 57 WNDCLASSEX wc = {0}; |
56 wc.cbSize = sizeof(wc); | 58 wc.cbSize = sizeof(wc); |
57 wc.lpfnWndProc = | 59 wc.lpfnWndProc = base::win::WrappedWindowProc< |
58 base::win::WrappedWindowProc<&SystemMessageWindowWin::WndProcThunk>; | 60 &MediaDeviceNotificationWindowWin::WndProcThunk>; |
vandebo (ex-Chrome)
2012/04/24 21:58:17
nit: four spaces
tpayne
2012/04/24 22:59:17
Done.
| |
59 wc.hInstance = hinst; | 61 wc.hInstance = hinst; |
60 wc.lpszClassName = WindowClassName; | 62 wc.lpszClassName = WindowClassName; |
61 ATOM clazz = RegisterClassEx(&wc); | 63 ATOM clazz = RegisterClassEx(&wc); |
62 DCHECK(clazz); | 64 DCHECK(clazz); |
63 | 65 |
64 window_ = CreateWindow(WindowClassName, | 66 window_ = CreateWindow(WindowClassName, |
65 0, 0, 0, 0, 0, 0, 0, 0, hinst, 0); | 67 0, 0, 0, 0, 0, 0, 0, 0, hinst, 0); |
66 SetWindowLongPtr(window_, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this)); | 68 SetWindowLongPtr(window_, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this)); |
67 } | 69 } |
68 | 70 |
69 SystemMessageWindowWin::~SystemMessageWindowWin() { | 71 MediaDeviceNotificationWindowWin::~MediaDeviceNotificationWindowWin() { |
70 if (window_) { | 72 if (window_) { |
71 DestroyWindow(window_); | 73 DestroyWindow(window_); |
72 UnregisterClass(WindowClassName, GetModuleHandle(NULL)); | 74 UnregisterClass(WindowClassName, GetModuleHandle(NULL)); |
73 } | 75 } |
74 } | 76 } |
75 | 77 |
76 LRESULT SystemMessageWindowWin::OnDeviceChange(UINT event_type, DWORD data) { | 78 LRESULT MediaDeviceNotificationWindowWin::OnDeviceChange( |
79 UINT event_type, DWORD data) { | |
vandebo (ex-Chrome)
2012/04/24 21:58:17
nit: looks like the first arg will fit on the prev
tpayne
2012/04/24 22:59:17
Done.
| |
77 base::SystemMonitor* monitor = base::SystemMonitor::Get(); | 80 base::SystemMonitor* monitor = base::SystemMonitor::Get(); |
78 switch (event_type) { | 81 switch (event_type) { |
79 case DBT_DEVNODES_CHANGED: | |
80 monitor->ProcessDevicesChanged(); | |
81 break; | |
82 case DBT_DEVICEARRIVAL: { | 82 case DBT_DEVICEARRIVAL: { |
83 DWORD unitmask = GetVolumeBitMaskFromBroadcastHeader(data); | 83 DWORD unitmask = GetVolumeBitMaskFromBroadcastHeader(data); |
84 for (int i = 0; unitmask; ++i, unitmask >>= 1) { | 84 for (int i = 0; unitmask; ++i, unitmask >>= 1) { |
85 if (unitmask & 0x01) { | 85 if (unitmask & 0x01) { |
86 FilePath::StringType drive(L"_:\\"); | 86 FilePath::StringType drive(L"_:\\"); |
87 drive[0] = L'A' + i; | 87 drive[0] = L'A' + i; |
88 WCHAR volume_name[MAX_PATH + 1]; | 88 WCHAR volume_name[MAX_PATH + 1]; |
89 if ((*volume_name_func_)(drive.c_str(), volume_name, MAX_PATH + 1)) { | 89 if ((*volume_name_func_)(drive.c_str(), volume_name, MAX_PATH + 1)) { |
90 monitor->ProcessMediaDeviceAttached( | 90 monitor->ProcessMediaDeviceAttached( |
91 i, base::SysWideToUTF8(volume_name), FilePath(drive)); | 91 i, base::SysWideToUTF8(volume_name), FilePath(drive)); |
92 } | 92 } |
93 } | 93 } |
94 } | 94 } |
95 break; | 95 break; |
96 } | 96 } |
97 case DBT_DEVICEREMOVECOMPLETE: { | 97 case DBT_DEVICEREMOVECOMPLETE: { |
98 DWORD unitmask = GetVolumeBitMaskFromBroadcastHeader(data); | 98 DWORD unitmask = GetVolumeBitMaskFromBroadcastHeader(data); |
99 for (int i = 0; unitmask; ++i, unitmask >>= 1) { | 99 for (int i = 0; unitmask; ++i, unitmask >>= 1) { |
100 if (unitmask & 0x01) { | 100 if (unitmask & 0x01) { |
101 monitor->ProcessMediaDeviceDetached(i); | 101 monitor->ProcessMediaDeviceDetached(i); |
102 } | 102 } |
103 } | 103 } |
104 break; | 104 break; |
105 } | 105 } |
106 } | 106 } |
107 return TRUE; | 107 return TRUE; |
108 } | 108 } |
109 | 109 |
110 LRESULT CALLBACK SystemMessageWindowWin::WndProc(HWND hwnd, UINT message, | 110 LRESULT CALLBACK MediaDeviceNotificationWindowWin::WndProc( |
111 WPARAM wparam, LPARAM lparam) { | 111 HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { |
112 switch (message) { | 112 switch (message) { |
113 case WM_DEVICECHANGE: | 113 case WM_DEVICECHANGE: |
114 return OnDeviceChange(static_cast<UINT>(wparam), | 114 return OnDeviceChange(static_cast<UINT>(wparam), |
115 static_cast<DWORD>(lparam)); | 115 static_cast<DWORD>(lparam)); |
116 default: | 116 default: |
117 break; | 117 break; |
118 } | 118 } |
119 | 119 |
120 return ::DefWindowProc(hwnd, message, wparam, lparam); | 120 return ::DefWindowProc(hwnd, message, wparam, lparam); |
121 } | 121 } |
122 | |
123 } // namespace chrome | |
OLD | NEW |