Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Side by Side Diff: chrome/browser/media_gallery/media_device_notifications_window_win.cc

Issue 10919051: Move device notification impl out of chrome/browser/media_gallery (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: modify for chromeos and win Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/media_gallery/media_device_notifications_window_win.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/media_gallery/media_device_notifications_utils.h"
17 #include "chrome/browser/media_gallery/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_MediaDeviceNotificationWindow";
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 MediaDeviceNotificationsWindowWin::MediaDeviceNotificationsWindowWin()
51 : atom_(0),
52 instance_(NULL),
53 window_(NULL),
54 volume_name_func_(&GetVolumeName) {
55 Init();
56 }
57
58 MediaDeviceNotificationsWindowWin::MediaDeviceNotificationsWindowWin(
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 MediaDeviceNotificationsWindowWin::Init() {
68 WNDCLASSEX window_class;
69 base::win::InitializeWindowClass(
70 WindowClassName,
71 &base::win::WrappedWindowProc<
72 MediaDeviceNotificationsWindowWin::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 MediaDeviceNotificationsWindowWin::~MediaDeviceNotificationsWindowWin() {
85 if (window_)
86 DestroyWindow(window_);
87
88 if (atom_)
89 UnregisterClass(MAKEINTATOM(atom_), instance_);
90 }
91
92 LRESULT MediaDeviceNotificationsWindowWin::OnDeviceChange(UINT event_type,
93 DWORD data) {
94 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
95 switch (event_type) {
96 case DBT_DEVICEARRIVAL: {
97 DWORD unitmask = GetVolumeBitMaskFromBroadcastHeader(data);
98 for (int i = 0; unitmask; ++i, unitmask >>= 1) {
99 if (unitmask & 0x01) {
100 FilePath::StringType drive(L"_:\\");
101 drive[0] = L'A' + i;
102 WCHAR volume_name[MAX_PATH + 1];
103 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 // having a fall back for volume name.
106 std::string device_id = MediaStorageUtil::MakeDeviceId(
107 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM,
108 base::IntToString(i));
109 BrowserThread::PostTask(
110 BrowserThread::FILE, FROM_HERE,
111 base::Bind(&MediaDeviceNotificationsWindowWin::
112 CheckDeviceTypeOnFileThread, this, device_id,
113 FilePath::StringType(volume_name), FilePath(drive)));
114 }
115 }
116 }
117 break;
118 }
119 case DBT_DEVICEREMOVECOMPLETE: {
120 DWORD unitmask = GetVolumeBitMaskFromBroadcastHeader(data);
121 for (int i = 0; unitmask; ++i, unitmask >>= 1) {
122 if (unitmask & 0x01) {
123 std::string device_id = MediaStorageUtil::MakeDeviceId(
124 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM,
125 base::IntToString(i));
126 SystemMonitor::Get()->ProcessRemovableStorageDetached(device_id);
127 }
128 }
129 break;
130 }
131 }
132 return TRUE;
133 }
134
135 void MediaDeviceNotificationsWindowWin::CheckDeviceTypeOnFileThread(
136 const std::string& id,
137 const FilePath::StringType& device_name,
138 const FilePath& path) {
139 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
140 if (!IsMediaDevice(path.value()))
141 return;
142
143 BrowserThread::PostTask(
144 BrowserThread::UI, FROM_HERE,
145 base::Bind(
146 &MediaDeviceNotificationsWindowWin::
147 ProcessMediaDeviceAttachedOnUIThread,
148 this, id, device_name, path));
149 }
150
151 void MediaDeviceNotificationsWindowWin::ProcessMediaDeviceAttachedOnUIThread(
152 const std::string& id,
153 const FilePath::StringType& device_name,
154 const FilePath& path) {
155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
156
157 SystemMonitor::Get()->ProcessRemovableStorageAttached(id,
158 device_name,
159 path.value());
160 }
161
162 LRESULT CALLBACK MediaDeviceNotificationsWindowWin::WndProc(
163 HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) {
164 switch (message) {
165 case WM_DEVICECHANGE:
166 return OnDeviceChange(static_cast<UINT>(wparam),
167 static_cast<DWORD>(lparam));
168 default:
169 break;
170 }
171
172 return ::DefWindowProc(hwnd, message, wparam, lparam);
173 }
174
175 // static
176 LRESULT CALLBACK MediaDeviceNotificationsWindowWin::WndProcThunk(
177 HWND hwnd,
178 UINT message,
179 WPARAM wparam,
180 LPARAM lparam) {
181 MediaDeviceNotificationsWindowWin* msg_wnd =
182 reinterpret_cast<MediaDeviceNotificationsWindowWin*>(
183 GetWindowLongPtr(hwnd, GWLP_USERDATA));
184 if (msg_wnd)
185 return msg_wnd->WndProc(hwnd, message, wparam, lparam);
186 return ::DefWindowProc(hwnd, message, wparam, lparam);
187 }
188
189 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698