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/storage_monitor/removable_device_notifications_window_w
in.h" | |
6 | |
7 #include <windows.h> | |
8 #include <dbt.h> | |
9 #include <fileapi.h> | |
10 | |
11 #include "base/win/wrapped_window_proc.h" | |
12 #include "chrome/browser/storage_monitor/media_storage_util.h" | |
13 #include "chrome/browser/storage_monitor/portable_device_watcher_win.h" | |
14 #include "chrome/browser/storage_monitor/removable_device_constants.h" | |
15 #include "chrome/browser/storage_monitor/volume_mount_watcher_win.h" | |
16 | |
17 namespace chrome { | |
18 | |
19 namespace { | |
20 | |
21 const char16 kWindowClassName[] = L"Chrome_RemovableDeviceNotificationWindow"; | |
22 | |
23 } // namespace | |
24 | |
25 | |
26 // RemovableDeviceNotificationsWindowWin -------------------------------------- | |
27 | |
28 // static | |
29 RemovableDeviceNotificationsWindowWin* | |
30 RemovableDeviceNotificationsWindowWin::Create() { | |
31 return new RemovableDeviceNotificationsWindowWin( | |
32 new VolumeMountWatcherWin(), new PortableDeviceWatcherWin()); | |
33 } | |
34 | |
35 RemovableDeviceNotificationsWindowWin:: | |
36 RemovableDeviceNotificationsWindowWin( | |
37 VolumeMountWatcherWin* volume_mount_watcher, | |
38 PortableDeviceWatcherWin* portable_device_watcher) | |
39 : window_class_(0), | |
40 instance_(NULL), | |
41 window_(NULL), | |
42 volume_mount_watcher_(volume_mount_watcher), | |
43 portable_device_watcher_(portable_device_watcher) { | |
44 DCHECK(volume_mount_watcher_); | |
45 DCHECK(portable_device_watcher_); | |
46 volume_mount_watcher_->SetNotifications(receiver()); | |
47 portable_device_watcher_->SetNotifications(receiver()); | |
48 } | |
49 | |
50 RemovableDeviceNotificationsWindowWin:: | |
51 ~RemovableDeviceNotificationsWindowWin() { | |
52 volume_mount_watcher_->SetNotifications(NULL); | |
53 portable_device_watcher_->SetNotifications(NULL); | |
54 | |
55 if (window_) | |
56 DestroyWindow(window_); | |
57 | |
58 if (window_class_) | |
59 UnregisterClass(MAKEINTATOM(window_class_), instance_); | |
60 } | |
61 | |
62 void RemovableDeviceNotificationsWindowWin::Init() { | |
63 WNDCLASSEX window_class; | |
64 base::win::InitializeWindowClass( | |
65 kWindowClassName, | |
66 &base::win::WrappedWindowProc< | |
67 RemovableDeviceNotificationsWindowWin::WndProcThunk>, | |
68 0, 0, 0, NULL, NULL, NULL, NULL, NULL, | |
69 &window_class); | |
70 instance_ = window_class.hInstance; | |
71 window_class_ = RegisterClassEx(&window_class); | |
72 DCHECK(window_class_); | |
73 | |
74 window_ = CreateWindow(MAKEINTATOM(window_class_), 0, 0, 0, 0, 0, 0, 0, 0, | |
75 instance_, 0); | |
76 SetWindowLongPtr(window_, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this)); | |
77 volume_mount_watcher_->Init(); | |
78 portable_device_watcher_->Init(window_); | |
79 } | |
80 | |
81 bool RemovableDeviceNotificationsWindowWin::GetStorageInfoForPath( | |
82 const base::FilePath& path, | |
83 StorageInfo* device_info) const { | |
84 string16 location; | |
85 std::string unique_id; | |
86 string16 name; | |
87 bool removable; | |
88 uint64 total_size_in_bytes; | |
89 if (!GetDeviceInfo(path, &location, &unique_id, &name, &removable, | |
90 &total_size_in_bytes)) { | |
91 return false; | |
92 } | |
93 | |
94 // 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 | |
96 // require bouncing over to the file thread. Instead, just iterate the | |
97 // devices. | |
98 std::string device_id; | |
99 if (removable) { | |
100 std::vector<StorageInfo> attached_devices = GetAttachedStorage(); | |
101 bool found = false; | |
102 for (size_t i = 0; i < attached_devices.size(); i++) { | |
103 MediaStorageUtil::Type type; | |
104 std::string id; | |
105 MediaStorageUtil::CrackDeviceId(attached_devices[i].device_id, &type, | |
106 &id); | |
107 if (id == unique_id) { | |
108 found = true; | |
109 device_id = attached_devices[i].device_id; | |
110 break; | |
111 } | |
112 } | |
113 if (!found) | |
114 return false; | |
115 } else { | |
116 device_id = MediaStorageUtil::MakeDeviceId( | |
117 MediaStorageUtil::FIXED_MASS_STORAGE, unique_id); | |
118 } | |
119 | |
120 if (device_info) { | |
121 device_info->device_id = device_id; | |
122 device_info->name = name; | |
123 device_info->location = location; | |
124 } | |
125 return true; | |
126 } | |
127 | |
128 uint64 RemovableDeviceNotificationsWindowWin::GetStorageSize( | |
129 const base::FilePath::StringType& location) const { | |
130 return volume_mount_watcher_->GetStorageSize(location); | |
131 } | |
132 | |
133 bool RemovableDeviceNotificationsWindowWin::GetMTPStorageInfoFromDeviceId( | |
134 const std::string& storage_device_id, | |
135 string16* device_location, | |
136 string16* storage_object_id) const { | |
137 MediaStorageUtil::Type type; | |
138 MediaStorageUtil::CrackDeviceId(storage_device_id, &type, NULL); | |
139 return ((type == MediaStorageUtil::MTP_OR_PTP) && | |
140 portable_device_watcher_->GetMTPStorageInfoFromDeviceId( | |
141 storage_device_id, device_location, storage_object_id)); | |
142 } | |
143 | |
144 // static | |
145 LRESULT CALLBACK RemovableDeviceNotificationsWindowWin::WndProcThunk( | |
146 HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { | |
147 RemovableDeviceNotificationsWindowWin* msg_wnd = | |
148 reinterpret_cast<RemovableDeviceNotificationsWindowWin*>( | |
149 GetWindowLongPtr(hwnd, GWLP_USERDATA)); | |
150 if (msg_wnd) | |
151 return msg_wnd->WndProc(hwnd, message, wparam, lparam); | |
152 return ::DefWindowProc(hwnd, message, wparam, lparam); | |
153 } | |
154 | |
155 LRESULT CALLBACK RemovableDeviceNotificationsWindowWin::WndProc( | |
156 HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { | |
157 switch (message) { | |
158 case WM_DEVICECHANGE: | |
159 OnDeviceChange(static_cast<UINT>(wparam), lparam); | |
160 return TRUE; | |
161 default: | |
162 break; | |
163 } | |
164 | |
165 return ::DefWindowProc(hwnd, message, wparam, lparam); | |
166 } | |
167 | |
168 bool RemovableDeviceNotificationsWindowWin::GetDeviceInfo( | |
169 const base::FilePath& device_path, string16* device_location, | |
170 std::string* unique_id, string16* name, bool* removable, | |
171 uint64* total_size_in_bytes) const { | |
172 // TODO(kmadhusu) Implement PortableDeviceWatcherWin::GetDeviceInfo() | |
173 // function when we have the functionality to add a sub directory of | |
174 // portable device as a media gallery. | |
175 return volume_mount_watcher_->GetDeviceInfo(device_path, device_location, | |
176 unique_id, name, removable, | |
177 total_size_in_bytes); | |
178 } | |
179 | |
180 void RemovableDeviceNotificationsWindowWin::OnDeviceChange(UINT event_type, | |
181 LPARAM data) { | |
182 volume_mount_watcher_->OnWindowMessage(event_type, data); | |
183 portable_device_watcher_->OnWindowMessage(event_type, data); | |
184 } | |
185 | |
186 } // namespace chrome | |
OLD | NEW |