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