| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/usb/usb_chooser_controller.h" | 5 #include "chrome/browser/usb/usb_chooser_controller.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 NEW_FOREGROUND_TAB, ui::PAGE_TRANSITION_AUTO_TOPLEVEL, | 142 NEW_FOREGROUND_TAB, ui::PAGE_TRANSITION_AUTO_TOPLEVEL, |
| 143 false /* is_renderer_initialized */)); | 143 false /* is_renderer_initialized */)); |
| 144 } | 144 } |
| 145 | 145 |
| 146 void UsbChooserController::OnDeviceAdded( | 146 void UsbChooserController::OnDeviceAdded( |
| 147 scoped_refptr<device::UsbDevice> device) { | 147 scoped_refptr<device::UsbDevice> device) { |
| 148 if (DisplayDevice(device)) { | 148 if (DisplayDevice(device)) { |
| 149 const base::string16& device_name = device->product_string(); | 149 const base::string16& device_name = device->product_string(); |
| 150 devices_.push_back(std::make_pair(device, device_name)); | 150 devices_.push_back(std::make_pair(device, device_name)); |
| 151 ++device_name_map_[device_name]; | 151 ++device_name_map_[device_name]; |
| 152 if (observer()) | 152 if (view()) |
| 153 observer()->OnOptionAdded(devices_.size() - 1); | 153 view()->OnOptionAdded(devices_.size() - 1); |
| 154 } | 154 } |
| 155 } | 155 } |
| 156 | 156 |
| 157 void UsbChooserController::OnDeviceRemoved( | 157 void UsbChooserController::OnDeviceRemoved( |
| 158 scoped_refptr<device::UsbDevice> device) { | 158 scoped_refptr<device::UsbDevice> device) { |
| 159 for (auto it = devices_.begin(); it != devices_.end(); ++it) { | 159 for (auto it = devices_.begin(); it != devices_.end(); ++it) { |
| 160 if (it->first == device) { | 160 if (it->first == device) { |
| 161 size_t index = it - devices_.begin(); | 161 size_t index = it - devices_.begin(); |
| 162 DCHECK_GT(device_name_map_[it->second], 0); | 162 DCHECK_GT(device_name_map_[it->second], 0); |
| 163 if (--device_name_map_[it->second] == 0) | 163 if (--device_name_map_[it->second] == 0) |
| 164 device_name_map_.erase(it->second); | 164 device_name_map_.erase(it->second); |
| 165 devices_.erase(it); | 165 devices_.erase(it); |
| 166 if (observer()) | 166 if (view()) |
| 167 observer()->OnOptionRemoved(index); | 167 view()->OnOptionRemoved(index); |
| 168 return; | 168 return; |
| 169 } | 169 } |
| 170 } | 170 } |
| 171 } | 171 } |
| 172 | 172 |
| 173 // Get a list of devices that can be shown in the chooser bubble UI for | 173 // Get a list of devices that can be shown in the chooser bubble UI for |
| 174 // user to grant permsssion. | 174 // user to grant permsssion. |
| 175 void UsbChooserController::GotUsbDeviceList( | 175 void UsbChooserController::GotUsbDeviceList( |
| 176 const std::vector<scoped_refptr<device::UsbDevice>>& devices) { | 176 const std::vector<scoped_refptr<device::UsbDevice>>& devices) { |
| 177 for (const auto& device : devices) { | 177 for (const auto& device : devices) { |
| 178 if (DisplayDevice(device)) { | 178 if (DisplayDevice(device)) { |
| 179 const base::string16& device_name = device->product_string(); | 179 const base::string16& device_name = device->product_string(); |
| 180 devices_.push_back(std::make_pair(device, device_name)); | 180 devices_.push_back(std::make_pair(device, device_name)); |
| 181 ++device_name_map_[device_name]; | 181 ++device_name_map_[device_name]; |
| 182 } | 182 } |
| 183 } | 183 } |
| 184 if (observer()) | 184 if (view()) |
| 185 observer()->OnOptionsInitialized(); | 185 view()->OnOptionsInitialized(); |
| 186 } | 186 } |
| 187 | 187 |
| 188 bool UsbChooserController::DisplayDevice( | 188 bool UsbChooserController::DisplayDevice( |
| 189 scoped_refptr<device::UsbDevice> device) const { | 189 scoped_refptr<device::UsbDevice> device) const { |
| 190 return device::UsbDeviceFilter::MatchesAny(device, filters_) && | 190 return device::UsbDeviceFilter::MatchesAny(device, filters_) && |
| 191 (base::CommandLine::ForCurrentProcess()->HasSwitch( | 191 (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 192 switches::kDisableWebUsbSecurity) || | 192 switches::kDisableWebUsbSecurity) || |
| 193 device::FindInWebUsbAllowedOrigins( | 193 device::FindInWebUsbAllowedOrigins( |
| 194 device->webusb_allowed_origins(), | 194 device->webusb_allowed_origins(), |
| 195 render_frame_host_->GetLastCommittedURL().GetOrigin())); | 195 render_frame_host_->GetLastCommittedURL().GetOrigin())); |
| 196 } | 196 } |
| OLD | NEW |