| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "device/core/device_monitor_win.h" | 5 #include "device/base/device_monitor_win.h" |
| 6 | 6 |
| 7 #include <dbt.h> | 7 #include <dbt.h> |
| 8 #include <windows.h> | 8 #include <windows.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| 11 #include <memory> | 11 #include <memory> |
| 12 | 12 |
| 13 #include "base/at_exit.h" | 13 #include "base/at_exit.h" |
| 14 #include "base/bind.h" | 14 #include "base/bind.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 38 // This singleton class manages a shared message window for all registered | 38 // This singleton class manages a shared message window for all registered |
| 39 // device notification observers. It vends one instance of DeviceManagerWin for | 39 // device notification observers. It vends one instance of DeviceManagerWin for |
| 40 // each unique GUID it sees. | 40 // each unique GUID it sees. |
| 41 class DeviceMonitorMessageWindow { | 41 class DeviceMonitorMessageWindow { |
| 42 public: | 42 public: |
| 43 static DeviceMonitorMessageWindow* GetInstance() { | 43 static DeviceMonitorMessageWindow* GetInstance() { |
| 44 if (!g_message_window) { | 44 if (!g_message_window) { |
| 45 g_message_window = new DeviceMonitorMessageWindow(); | 45 g_message_window = new DeviceMonitorMessageWindow(); |
| 46 if (g_message_window->Init()) { | 46 if (g_message_window->Init()) { |
| 47 base::AtExitManager::RegisterTask( | 47 base::AtExitManager::RegisterTask( |
| 48 base::Bind(&base::DeletePointer<DeviceMonitorMessageWindow>, | 48 base::Bind(&base::DeletePointer<DeviceMonitorMessageWindow>, |
| 49 base::Unretained(g_message_window))); | 49 base::Unretained(g_message_window))); |
| 50 } else { | 50 } else { |
| 51 delete g_message_window; | 51 delete g_message_window; |
| 52 g_message_window = nullptr; | 52 g_message_window = nullptr; |
| 53 } | 53 } |
| 54 } | 54 } |
| 55 return g_message_window; | 55 return g_message_window; |
| 56 } | 56 } |
| 57 | 57 |
| 58 DeviceMonitorWin* GetForDeviceInterface(const GUID& device_interface) { | 58 DeviceMonitorWin* GetForDeviceInterface(const GUID& device_interface) { |
| 59 std::unique_ptr<DeviceMonitorWin>& device_monitor = | 59 std::unique_ptr<DeviceMonitorWin>& device_monitor = |
| 60 device_monitors_[device_interface]; | 60 device_monitors_[device_interface]; |
| 61 if (!device_monitor) { | 61 if (!device_monitor) { |
| 62 device_monitor.reset(new DeviceMonitorWin()); | 62 device_monitor.reset(new DeviceMonitorWin()); |
| 63 } | 63 } |
| 64 return device_monitor.get(); | 64 return device_monitor.get(); |
| 65 } | 65 } |
| 66 | 66 |
| 67 DeviceMonitorWin* GetForAllInterfaces() { return &all_device_monitor_; } | 67 DeviceMonitorWin* GetForAllInterfaces() { return &all_device_monitor_; } |
| 68 | 68 |
| 69 private: | 69 private: |
| 70 friend void base::DeletePointer<DeviceMonitorMessageWindow>( | 70 friend void base::DeletePointer<DeviceMonitorMessageWindow>( |
| 71 DeviceMonitorMessageWindow* message_window); | 71 DeviceMonitorMessageWindow* message_window); |
| 72 | 72 |
| 73 DeviceMonitorMessageWindow() { | 73 DeviceMonitorMessageWindow() {} |
| 74 } | |
| 75 | 74 |
| 76 ~DeviceMonitorMessageWindow() { | 75 ~DeviceMonitorMessageWindow() { |
| 77 if (notify_handle_) { | 76 if (notify_handle_) { |
| 78 UnregisterDeviceNotification(notify_handle_); | 77 UnregisterDeviceNotification(notify_handle_); |
| 79 } | 78 } |
| 80 } | 79 } |
| 81 | 80 |
| 82 bool Init() { | 81 bool Init() { |
| 83 window_.reset(new base::win::MessageWindow()); | 82 window_.reset(new base::win::MessageWindow()); |
| 84 if (!window_->CreateNamed( | 83 if (!window_->CreateNamed( |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 | 149 |
| 151 DISALLOW_COPY_AND_ASSIGN(DeviceMonitorMessageWindow); | 150 DISALLOW_COPY_AND_ASSIGN(DeviceMonitorMessageWindow); |
| 152 }; | 151 }; |
| 153 | 152 |
| 154 void DeviceMonitorWin::Observer::OnDeviceAdded(const GUID& class_guid, | 153 void DeviceMonitorWin::Observer::OnDeviceAdded(const GUID& class_guid, |
| 155 const std::string& device_path) { | 154 const std::string& device_path) { |
| 156 } | 155 } |
| 157 | 156 |
| 158 void DeviceMonitorWin::Observer::OnDeviceRemoved( | 157 void DeviceMonitorWin::Observer::OnDeviceRemoved( |
| 159 const GUID& class_guid, | 158 const GUID& class_guid, |
| 160 const std::string& device_path) { | 159 const std::string& device_path) {} |
| 161 } | |
| 162 | 160 |
| 163 // static | 161 // static |
| 164 DeviceMonitorWin* DeviceMonitorWin::GetForDeviceInterface( | 162 DeviceMonitorWin* DeviceMonitorWin::GetForDeviceInterface( |
| 165 const GUID& device_interface) { | 163 const GUID& device_interface) { |
| 166 DeviceMonitorMessageWindow* message_window = | 164 DeviceMonitorMessageWindow* message_window = |
| 167 DeviceMonitorMessageWindow::GetInstance(); | 165 DeviceMonitorMessageWindow::GetInstance(); |
| 168 if (message_window) { | 166 if (message_window) { |
| 169 return message_window->GetForDeviceInterface(device_interface); | 167 return message_window->GetForDeviceInterface(device_interface); |
| 170 } | 168 } |
| 171 return nullptr; | 169 return nullptr; |
| 172 } | 170 } |
| 173 | 171 |
| 174 // static | 172 // static |
| 175 DeviceMonitorWin* DeviceMonitorWin::GetForAllInterfaces() { | 173 DeviceMonitorWin* DeviceMonitorWin::GetForAllInterfaces() { |
| 176 DeviceMonitorMessageWindow* message_window = | 174 DeviceMonitorMessageWindow* message_window = |
| 177 DeviceMonitorMessageWindow::GetInstance(); | 175 DeviceMonitorMessageWindow::GetInstance(); |
| 178 if (message_window) { | 176 if (message_window) { |
| 179 return message_window->GetForAllInterfaces(); | 177 return message_window->GetForAllInterfaces(); |
| 180 } | 178 } |
| 181 return nullptr; | 179 return nullptr; |
| 182 } | 180 } |
| 183 | 181 |
| 184 DeviceMonitorWin::~DeviceMonitorWin() { | 182 DeviceMonitorWin::~DeviceMonitorWin() {} |
| 185 } | |
| 186 | 183 |
| 187 void DeviceMonitorWin::AddObserver(Observer* observer) { | 184 void DeviceMonitorWin::AddObserver(Observer* observer) { |
| 188 observer_list_.AddObserver(observer); | 185 observer_list_.AddObserver(observer); |
| 189 } | 186 } |
| 190 | 187 |
| 191 void DeviceMonitorWin::RemoveObserver(Observer* observer) { | 188 void DeviceMonitorWin::RemoveObserver(Observer* observer) { |
| 192 observer_list_.RemoveObserver(observer); | 189 observer_list_.RemoveObserver(observer); |
| 193 } | 190 } |
| 194 | 191 |
| 195 DeviceMonitorWin::DeviceMonitorWin() { | 192 DeviceMonitorWin::DeviceMonitorWin() {} |
| 196 } | |
| 197 | 193 |
| 198 void DeviceMonitorWin::NotifyDeviceAdded(const GUID& class_guid, | 194 void DeviceMonitorWin::NotifyDeviceAdded(const GUID& class_guid, |
| 199 const std::string& device_path) { | 195 const std::string& device_path) { |
| 200 FOR_EACH_OBSERVER(Observer, observer_list_, | 196 for (auto& observer : observer_list_) |
| 201 OnDeviceAdded(class_guid, device_path)); | 197 observer.OnDeviceAdded(class_guid, device_path); |
| 202 } | 198 } |
| 203 | 199 |
| 204 void DeviceMonitorWin::NotifyDeviceRemoved(const GUID& class_guid, | 200 void DeviceMonitorWin::NotifyDeviceRemoved(const GUID& class_guid, |
| 205 const std::string& device_path) { | 201 const std::string& device_path) { |
| 206 FOR_EACH_OBSERVER(Observer, observer_list_, | 202 for (auto& observer : observer_list_) |
| 207 OnDeviceRemoved(class_guid, device_path)); | 203 observer.OnDeviceRemoved(class_guid, device_path); |
| 208 } | 204 } |
| 209 | 205 |
| 210 } // namespace device | 206 } // namespace device |
| OLD | NEW |