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

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 // RemovableDeviceNotificationsWindowWin::PortableDeviceNotifications ---------
Peter Kasting 2012/10/29 21:57:19 Why make this a separate class instead of simply p
kmadhusu 2012/10/30 01:29:24 Done. Removed this helper class. Made code changes
29 class RemovableDeviceNotificationsWindowWin::PortableDeviceNotifications {
Peter Kasting 2012/10/29 21:57:19 Nit: Add blank line. Consider also adding a blank
kmadhusu 2012/10/30 01:29:24 -NA-
30 public:
31 // Register window handle to receive portable device notifications details.
Peter Kasting 2012/10/29 21:57:19 Nit: Register window handle -> Registers |hwnd|
kmadhusu 2012/10/30 01:29:24 Done.
32 explicit PortableDeviceNotifications(HWND hwnd);
33
34 // Unregisters device notifications.
35 ~PortableDeviceNotifications();
36
37 private:
38 HDEVNOTIFY notifications_;
39
40 DISALLOW_IMPLICIT_CONSTRUCTORS(PortableDeviceNotifications);
41 };
42
43 RemovableDeviceNotificationsWindowWin::PortableDeviceNotifications::
44 PortableDeviceNotifications(HWND hwnd) {
45 GUID dev_interface_guid = GUID_NULL;
46 HRESULT hr = CLSIDFromString(kWPDDevInterfaceGUID, &dev_interface_guid);
47 if (FAILED(hr))
48 return;
49 DEV_BROADCAST_DEVICEINTERFACE db = {
50 sizeof(DEV_BROADCAST_DEVICEINTERFACE),
51 DBT_DEVTYP_DEVICEINTERFACE,
52 0,
53 dev_interface_guid
54 };
55 notifications_ = RegisterDeviceNotification(hwnd, &db,
56 DEVICE_NOTIFY_WINDOW_HANDLE);
57 }
58
59 RemovableDeviceNotificationsWindowWin::PortableDeviceNotifications::
60 ~PortableDeviceNotifications() {
61 UnregisterDeviceNotification(notifications_);
62 }
63
64 // RemovableDeviceNotificationsWindowWin implementaion -----------------------
Peter Kasting 2012/10/29 21:57:19 Nit: remove "implementation"
kmadhusu 2012/10/30 01:29:24 Done.
65
29 // static 66 // static
30 RemovableDeviceNotificationsWindowWin* 67 RemovableDeviceNotificationsWindowWin*
31 RemovableDeviceNotificationsWindowWin::Create() { 68 RemovableDeviceNotificationsWindowWin::Create() {
32 return new RemovableDeviceNotificationsWindowWin(new VolumeMountWatcherWin()); 69 return new RemovableDeviceNotificationsWindowWin(
70 new VolumeMountWatcherWin(), new PortableDeviceWatcherWin());
33 } 71 }
34 72
35 RemovableDeviceNotificationsWindowWin:: 73 RemovableDeviceNotificationsWindowWin::
36 RemovableDeviceNotificationsWindowWin( 74 RemovableDeviceNotificationsWindowWin(
37 VolumeMountWatcherWin* volume_mount_watcher) 75 VolumeMountWatcherWin* volume_mount_watcher,
76 PortableDeviceWatcherWin* portable_device_watcher)
38 : window_class_(0), 77 : window_class_(0),
39 instance_(NULL), 78 instance_(NULL),
40 window_(NULL), 79 window_(NULL),
41 volume_mount_watcher_(volume_mount_watcher) { 80 volume_mount_watcher_(volume_mount_watcher),
81 portable_device_watcher_(portable_device_watcher) {
42 DCHECK(volume_mount_watcher_); 82 DCHECK(volume_mount_watcher_);
83 DCHECK(portable_device_watcher_);
43 DCHECK(!g_removable_device_notifications_window_win); 84 DCHECK(!g_removable_device_notifications_window_win);
44 g_removable_device_notifications_window_win = this; 85 g_removable_device_notifications_window_win = this;
45 } 86 }
46 87
47 RemovableDeviceNotificationsWindowWin:: 88 RemovableDeviceNotificationsWindowWin::
48 ~RemovableDeviceNotificationsWindowWin() { 89 ~RemovableDeviceNotificationsWindowWin() {
49 if (window_) 90 if (window_)
50 DestroyWindow(window_); 91 DestroyWindow(window_);
51 92
52 if (window_class_) 93 if (window_class_)
53 UnregisterClass(MAKEINTATOM(window_class_), instance_); 94 UnregisterClass(MAKEINTATOM(window_class_), instance_);
54 95
55 DCHECK_EQ(this, g_removable_device_notifications_window_win); 96 DCHECK_EQ(this, g_removable_device_notifications_window_win);
56 g_removable_device_notifications_window_win = NULL; 97 g_removable_device_notifications_window_win = NULL;
57 } 98 }
58 99
59 // static 100 // static
60 RemovableDeviceNotificationsWindowWin* 101 RemovableDeviceNotificationsWindowWin*
61 RemovableDeviceNotificationsWindowWin::GetInstance() { 102 RemovableDeviceNotificationsWindowWin::GetInstance() {
62 DCHECK(g_removable_device_notifications_window_win); 103 DCHECK(g_removable_device_notifications_window_win);
63 return g_removable_device_notifications_window_win; 104 return g_removable_device_notifications_window_win;
64 } 105 }
65 106
66 void RemovableDeviceNotificationsWindowWin::Init() { 107 void RemovableDeviceNotificationsWindowWin::Init() {
67 volume_mount_watcher_->Init(); 108 volume_mount_watcher_->Init();
109 portable_device_watcher_->Init();
68 110
69 WNDCLASSEX window_class; 111 WNDCLASSEX window_class;
70 base::win::InitializeWindowClass( 112 base::win::InitializeWindowClass(
71 kWindowClassName, 113 kWindowClassName,
72 &WrappedWindowProc<RemovableDeviceNotificationsWindowWin::WndProcThunk>, 114 &base::win::WrappedWindowProc<
115 RemovableDeviceNotificationsWindowWin::WndProcThunk>,
73 0, 0, 0, NULL, NULL, NULL, NULL, NULL, 116 0, 0, 0, NULL, NULL, NULL, NULL, NULL,
74 &window_class); 117 &window_class);
75 instance_ = window_class.hInstance; 118 instance_ = window_class.hInstance;
76 window_class_ = RegisterClassEx(&window_class); 119 window_class_ = RegisterClassEx(&window_class);
77 DCHECK(window_class_); 120 DCHECK(window_class_);
78 121
79 window_ = CreateWindow(MAKEINTATOM(window_class_), 0, 0, 0, 0, 0, 0, 0, 0, 122 window_ = CreateWindow(MAKEINTATOM(window_class_), 0, 0, 0, 0, 0, 0, 0, 0,
80 instance_, 0); 123 instance_, 0);
81 SetWindowLongPtr(window_, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this)); 124 SetWindowLongPtr(window_, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
125 portable_device_notifications_.reset(
126 new PortableDeviceNotifications(window_));
82 } 127 }
83 128
84 bool RemovableDeviceNotificationsWindowWin::GetDeviceInfoForPath( 129 bool RemovableDeviceNotificationsWindowWin::GetDeviceInfoForPath(
85 const FilePath& path, 130 const FilePath& path,
86 base::SystemMonitor::RemovableStorageInfo* device_info) { 131 base::SystemMonitor::RemovableStorageInfo* device_info) {
87 string16 location; 132 string16 location;
88 std::string unique_id; 133 std::string unique_id;
89 string16 name; 134 string16 name;
90 bool removable; 135 bool removable;
91 if (!GetDeviceInfo(path, &location, &unique_id, &name, &removable)) 136 if (!GetDeviceInfo(path, &location, &unique_id, &name, &removable))
92 return false; 137 return false;
93 138
94 // To compute the device id, the device type is needed. For removable 139 // 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 140 // devices, that requires knowing if there's a DCIM directory, which would
96 // require bouncing over to the file thread. Instead, just iterate the 141 // require bouncing over to the file thread. Instead, just iterate the
97 // devices in SystemMonitor. 142 // devices in base::SystemMonitor.
98 std::string device_id; 143 std::string device_id;
99 if (removable) { 144 if (removable) {
100 std::vector<SystemMonitor::RemovableStorageInfo> attached_devices = 145 std::vector<base::SystemMonitor::RemovableStorageInfo> attached_devices =
101 SystemMonitor::Get()->GetAttachedRemovableStorage(); 146 base::SystemMonitor::Get()->GetAttachedRemovableStorage();
102 bool found = false; 147 bool found = false;
103 for (size_t i = 0; i < attached_devices.size(); i++) { 148 for (size_t i = 0; i < attached_devices.size(); i++) {
104 MediaStorageUtil::Type type; 149 MediaStorageUtil::Type type;
105 std::string id; 150 std::string id;
106 MediaStorageUtil::CrackDeviceId(attached_devices[i].device_id, &type, 151 MediaStorageUtil::CrackDeviceId(attached_devices[i].device_id, &type,
107 &id); 152 &id);
108 if (id == unique_id) { 153 if (id == unique_id) {
109 found = true; 154 found = true;
110 device_id = attached_devices[i].device_id; 155 device_id = attached_devices[i].device_id;
111 break; 156 break;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 default: 191 default:
147 break; 192 break;
148 } 193 }
149 194
150 return ::DefWindowProc(hwnd, message, wparam, lparam); 195 return ::DefWindowProc(hwnd, message, wparam, lparam);
151 } 196 }
152 197
153 bool RemovableDeviceNotificationsWindowWin::GetDeviceInfo( 198 bool RemovableDeviceNotificationsWindowWin::GetDeviceInfo(
154 const FilePath& device_path, string16* device_location, 199 const FilePath& device_path, string16* device_location,
155 std::string* unique_id, string16* name, bool* removable) { 200 std::string* unique_id, string16* name, bool* removable) {
201 // TODO(kmadhusu) Implement PortableDeviceWatcherWin::GetDeviceInfo()
202 // function when we have the functionality to add a sub directory of
203 // portable device as a media gallery.
156 return volume_mount_watcher_->GetDeviceInfo(device_path, device_location, 204 return volume_mount_watcher_->GetDeviceInfo(device_path, device_location,
157 unique_id, name, removable); 205 unique_id, name, removable);
158 } 206 }
159 207
160 void RemovableDeviceNotificationsWindowWin::OnDeviceChange(UINT event_type, 208 void RemovableDeviceNotificationsWindowWin::OnDeviceChange(UINT event_type,
161 LPARAM data) { 209 LPARAM data) {
162 volume_mount_watcher_->OnWindowMessage(event_type, data); 210 volume_mount_watcher_->OnWindowMessage(event_type, data);
211 portable_device_watcher_->OnWindowMessage(event_type, data);
163 } 212 }
164 213
165 } // namespace chrome 214 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698