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

Side by Side Diff: chrome/browser/system_monitor/removable_device_notifications_window_win.cc

Issue 11088012: [Win, MediaGallery] Enumerate and handle mtp device attach/detach events. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed review comments Created 8 years, 1 month 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 | Annotate | Revision Log
OLDNEW
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/removable_device_notifications_window_wi n.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 #include <fileapi.h> 9 #include <fileapi.h>
10 10
11 #include "base/file_path.h"
12 #include "base/win/wrapped_window_proc.h" 11 #include "base/win/wrapped_window_proc.h"
13 #include "chrome/browser/system_monitor/media_storage_util.h" 12 #include "chrome/browser/system_monitor/media_storage_util.h"
14 13 #include "chrome/browser/system_monitor/portable_device_watcher_win.h"
15 using base::SystemMonitor; 14 #include "chrome/browser/system_monitor/removable_device_constants.h"
16 using base::win::WrappedWindowProc; 15 #include "chrome/browser/system_monitor/volume_mount_watcher_win.h"
17 16
18 namespace chrome { 17 namespace chrome {
19 18
20 namespace { 19 namespace {
21 20
22 const char16 kWindowClassName[] = L"Chrome_RemovableDeviceNotificationWindow"; 21 const char16 kWindowClassName[] = L"Chrome_RemovableDeviceNotificationWindow";
23 22
24 RemovableDeviceNotificationsWindowWin* 23 RemovableDeviceNotificationsWindowWin*
25 g_removable_device_notifications_window_win = NULL; 24 g_removable_device_notifications_window_win = NULL;
26 25
27 } // namespace 26 } // namespace
28 27
28 ///////////////////////////////////////////////////////////////////////////
29 // RemovableDeviceNotificationsWindowWin::PortableDeviceNotifications //
30 // //
31 // Manages portable device notification handle for //
32 // RemovableDeviceNotificationsWindowWin. //
33 ///////////////////////////////////////////////////////////////////////////
Peter Kasting 2012/10/26 23:46:23 Nit: As in other file, I suggest: ... } // names
kmadhusu 2012/10/28 22:57:16 Done.
34 class RemovableDeviceNotificationsWindowWin::PortableDeviceNotifications {
35 public:
36 // Register window handle to receive portable device notifications details.
37 explicit PortableDeviceNotifications(HWND hwnd);
38
39 // Unregisters device notifications.
40 ~PortableDeviceNotifications();
41
42 private:
43 HDEVNOTIFY notifications_;
44
45 DISALLOW_IMPLICIT_CONSTRUCTORS(PortableDeviceNotifications);
46 };
47
48 RemovableDeviceNotificationsWindowWin::PortableDeviceNotifications::
49 PortableDeviceNotifications(HWND hwnd) {
Peter Kasting 2012/10/26 23:46:23 Nit: Indent 4
kmadhusu 2012/10/28 22:57:16 Done. Some reviewer said if the function name span
50 GUID dev_interface_guid = GUID_NULL;
51 HRESULT hr = CLSIDFromString(kWPDDevInterfaceGUID, &dev_interface_guid);
52 if (FAILED(hr))
53 return;
54 DEV_BROADCAST_DEVICEINTERFACE db = {sizeof(DEV_BROADCAST_DEVICEINTERFACE),
Peter Kasting 2012/10/26 23:46:23 Nit: I suggest: DEV_BROADCAST_DEVICEINTERFACE d
kmadhusu 2012/10/28 22:57:16 Done.
55 DBT_DEVTYP_DEVICEINTERFACE, 0,
56 dev_interface_guid};
57 notifications_ = RegisterDeviceNotification(hwnd, &db,
58 DEVICE_NOTIFY_WINDOW_HANDLE);
59 }
60
61 RemovableDeviceNotificationsWindowWin::PortableDeviceNotifications::
62 ~PortableDeviceNotifications() {
Peter Kasting 2012/10/26 23:46:23 Nit: Indent 4
kmadhusu 2012/10/28 22:57:16 Done.
63 UnregisterDeviceNotification(notifications_);
64 }
65
66 ///////////////////////////////////////////////////////////////////////////
67 // RemovableDeviceNotificationsWindowWin implementaion //
68 ///////////////////////////////////////////////////////////////////////////
29 // static 69 // static
30 RemovableDeviceNotificationsWindowWin* 70 RemovableDeviceNotificationsWindowWin*
31 RemovableDeviceNotificationsWindowWin::Create() { 71 RemovableDeviceNotificationsWindowWin::Create() {
32 return new RemovableDeviceNotificationsWindowWin(new VolumeMountWatcherWin()); 72 return new RemovableDeviceNotificationsWindowWin(
73 new VolumeMountWatcherWin(), new PortableDeviceWatcherWin());
33 } 74 }
34 75
35 RemovableDeviceNotificationsWindowWin:: 76 RemovableDeviceNotificationsWindowWin::
36 RemovableDeviceNotificationsWindowWin( 77 RemovableDeviceNotificationsWindowWin(
37 VolumeMountWatcherWin* volume_mount_watcher) 78 VolumeMountWatcherWin* volume_mount_watcher,
79 PortableDeviceWatcherWin* portable_device_watcher)
38 : window_class_(0), 80 : window_class_(0),
39 instance_(NULL), 81 instance_(NULL),
40 window_(NULL), 82 window_(NULL),
41 volume_mount_watcher_(volume_mount_watcher) { 83 volume_mount_watcher_(volume_mount_watcher),
84 portable_device_watcher_(portable_device_watcher) {
42 DCHECK(volume_mount_watcher_); 85 DCHECK(volume_mount_watcher_);
86 DCHECK(portable_device_watcher_);
43 DCHECK(!g_removable_device_notifications_window_win); 87 DCHECK(!g_removable_device_notifications_window_win);
44 g_removable_device_notifications_window_win = this; 88 g_removable_device_notifications_window_win = this;
45 } 89 }
46 90
47 RemovableDeviceNotificationsWindowWin:: 91 RemovableDeviceNotificationsWindowWin::
48 ~RemovableDeviceNotificationsWindowWin() { 92 ~RemovableDeviceNotificationsWindowWin() {
49 if (window_) 93 if (window_)
50 DestroyWindow(window_); 94 DestroyWindow(window_);
51 95
52 if (window_class_) 96 if (window_class_)
53 UnregisterClass(MAKEINTATOM(window_class_), instance_); 97 UnregisterClass(MAKEINTATOM(window_class_), instance_);
54 98
55 DCHECK_EQ(this, g_removable_device_notifications_window_win); 99 DCHECK_EQ(this, g_removable_device_notifications_window_win);
56 g_removable_device_notifications_window_win = NULL; 100 g_removable_device_notifications_window_win = NULL;
57 } 101 }
58 102
59 // static 103 // static
60 RemovableDeviceNotificationsWindowWin* 104 RemovableDeviceNotificationsWindowWin*
61 RemovableDeviceNotificationsWindowWin::GetInstance() { 105 RemovableDeviceNotificationsWindowWin::GetInstance() {
62 DCHECK(g_removable_device_notifications_window_win); 106 DCHECK(g_removable_device_notifications_window_win);
63 return g_removable_device_notifications_window_win; 107 return g_removable_device_notifications_window_win;
64 } 108 }
65 109
66 void RemovableDeviceNotificationsWindowWin::Init() { 110 void RemovableDeviceNotificationsWindowWin::Init() {
67 volume_mount_watcher_->Init(); 111 volume_mount_watcher_->Init();
112 portable_device_watcher_->Init();
68 113
69 WNDCLASSEX window_class; 114 WNDCLASSEX window_class;
70 base::win::InitializeWindowClass( 115 base::win::InitializeWindowClass(
71 kWindowClassName, 116 kWindowClassName,
72 &WrappedWindowProc<RemovableDeviceNotificationsWindowWin::WndProcThunk>, 117 &base::win::WrappedWindowProc<
118 RemovableDeviceNotificationsWindowWin::WndProcThunk>,
73 0, 0, 0, NULL, NULL, NULL, NULL, NULL, 119 0, 0, 0, NULL, NULL, NULL, NULL, NULL,
74 &window_class); 120 &window_class);
75 instance_ = window_class.hInstance; 121 instance_ = window_class.hInstance;
76 window_class_ = RegisterClassEx(&window_class); 122 window_class_ = RegisterClassEx(&window_class);
77 DCHECK(window_class_); 123 DCHECK(window_class_);
78 124
79 window_ = CreateWindow(MAKEINTATOM(window_class_), 0, 0, 0, 0, 0, 0, 0, 0, 125 window_ = CreateWindow(MAKEINTATOM(window_class_), 0, 0, 0, 0, 0, 0, 0, 0,
80 instance_, 0); 126 instance_, 0);
81 SetWindowLongPtr(window_, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this)); 127 SetWindowLongPtr(window_, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
128 portable_device_notifications_.reset(
129 new PortableDeviceNotifications(window_));
82 } 130 }
83 131
84 bool RemovableDeviceNotificationsWindowWin::GetDeviceInfoForPath( 132 bool RemovableDeviceNotificationsWindowWin::GetDeviceInfoForPath(
85 const FilePath& path, 133 const FilePath& path,
86 base::SystemMonitor::RemovableStorageInfo* device_info) { 134 base::SystemMonitor::RemovableStorageInfo* device_info) {
87 string16 location; 135 string16 location;
88 std::string unique_id; 136 std::string unique_id;
89 string16 name; 137 string16 name;
90 bool removable; 138 bool removable;
91 if (!GetDeviceInfo(path, &location, &unique_id, &name, &removable)) 139 if (!GetDeviceInfo(path, &location, &unique_id, &name, &removable))
92 return false; 140 return false;
93 141
94 // To compute the device id, the device type is needed. For removable 142 // 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 143 // devices, that requires knowing if there's a DCIM directory, which would
96 // require bouncing over to the file thread. Instead, just iterate the 144 // require bouncing over to the file thread. Instead, just iterate the
97 // devices in SystemMonitor. 145 // devices in base::SystemMonitor.
98 std::string device_id; 146 std::string device_id;
99 if (removable) { 147 if (removable) {
100 std::vector<SystemMonitor::RemovableStorageInfo> attached_devices = 148 std::vector<base::SystemMonitor::RemovableStorageInfo> attached_devices =
101 SystemMonitor::Get()->GetAttachedRemovableStorage(); 149 base::SystemMonitor::Get()->GetAttachedRemovableStorage();
102 bool found = false; 150 bool found = false;
103 for (size_t i = 0; i < attached_devices.size(); i++) { 151 for (size_t i = 0; i < attached_devices.size(); i++) {
104 MediaStorageUtil::Type type; 152 MediaStorageUtil::Type type;
105 std::string id; 153 std::string id;
106 MediaStorageUtil::CrackDeviceId(attached_devices[i].device_id, &type, 154 MediaStorageUtil::CrackDeviceId(attached_devices[i].device_id, &type,
107 &id); 155 &id);
108 if (id == unique_id) { 156 if (id == unique_id) {
109 found = true; 157 found = true;
110 device_id = attached_devices[i].device_id; 158 device_id = attached_devices[i].device_id;
111 break; 159 break;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 default: 194 default:
147 break; 195 break;
148 } 196 }
149 197
150 return ::DefWindowProc(hwnd, message, wparam, lparam); 198 return ::DefWindowProc(hwnd, message, wparam, lparam);
151 } 199 }
152 200
153 bool RemovableDeviceNotificationsWindowWin::GetDeviceInfo( 201 bool RemovableDeviceNotificationsWindowWin::GetDeviceInfo(
154 const FilePath& device_path, string16* device_location, 202 const FilePath& device_path, string16* device_location,
155 std::string* unique_id, string16* name, bool* removable) { 203 std::string* unique_id, string16* name, bool* removable) {
204 // TODO(kmadhusu) Implement PortableDeviceWatcherWin::GetDeviceInfo()
205 // function when we have the functionality to add a sub directory of
206 // portable device as a media gallery.
156 return volume_mount_watcher_->GetDeviceInfo(device_path, device_location, 207 return volume_mount_watcher_->GetDeviceInfo(device_path, device_location,
157 unique_id, name, removable); 208 unique_id, name, removable);
158 } 209 }
159 210
160 void RemovableDeviceNotificationsWindowWin::OnDeviceChange(UINT event_type, 211 void RemovableDeviceNotificationsWindowWin::OnDeviceChange(UINT event_type,
161 LPARAM data) { 212 LPARAM data) {
162 volume_mount_watcher_->OnWindowMessage(event_type, data); 213 volume_mount_watcher_->OnWindowMessage(event_type, data);
214 portable_device_watcher_->OnWindowMessage(event_type, data);
163 } 215 }
164 216
165 } // namespace chrome 217 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698