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 "extensions/browser/api/device_permissions_prompt.h" | 5 #include "extensions/browser/api/device_permissions_prompt.h" |
6 | 6 |
7 #include "base/barrier_closure.h" | 7 #include "base/barrier_closure.h" |
8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 using content::BrowserThread; | 21 using content::BrowserThread; |
22 using device::UsbDevice; | 22 using device::UsbDevice; |
23 using device::UsbDeviceFilter; | 23 using device::UsbDeviceFilter; |
24 using device::UsbService; | 24 using device::UsbService; |
25 | 25 |
26 namespace extensions { | 26 namespace extensions { |
27 | 27 |
28 DevicePermissionsPrompt::Prompt::DeviceInfo::DeviceInfo( | 28 DevicePermissionsPrompt::Prompt::DeviceInfo::DeviceInfo( |
29 scoped_refptr<UsbDevice> device) | 29 scoped_refptr<UsbDevice> device) |
30 : device(device) { | 30 : device(device) { |
31 base::string16 manufacturer_string; | 31 base::string16 manufacturer_string = device->manufacturer_string(); |
32 if (device->GetManufacturer(&manufacturer_string)) { | 32 if (manufacturer_string.empty()) { |
33 original_manufacturer_string = manufacturer_string; | |
34 } else { | |
35 const char* vendor_name = | 33 const char* vendor_name = |
36 device::UsbIds::GetVendorName(device->vendor_id()); | 34 device::UsbIds::GetVendorName(device->vendor_id()); |
37 if (vendor_name) { | 35 if (vendor_name) { |
38 manufacturer_string = base::UTF8ToUTF16(vendor_name); | 36 manufacturer_string = base::UTF8ToUTF16(vendor_name); |
39 } else { | 37 } else { |
40 base::string16 vendor_id = | 38 base::string16 vendor_id = |
41 base::ASCIIToUTF16(base::StringPrintf("0x%04x", device->vendor_id())); | 39 base::ASCIIToUTF16(base::StringPrintf("0x%04x", device->vendor_id())); |
42 manufacturer_string = | 40 manufacturer_string = |
43 l10n_util::GetStringFUTF16(IDS_DEVICE_UNKNOWN_VENDOR, vendor_id); | 41 l10n_util::GetStringFUTF16(IDS_DEVICE_UNKNOWN_VENDOR, vendor_id); |
44 } | 42 } |
45 } | 43 } |
46 | 44 |
47 base::string16 product_string; | 45 base::string16 product_string = device->product_string(); |
48 if (device->GetProduct(&product_string)) { | 46 if (product_string.empty()) { |
49 original_product_string = product_string; | |
50 } else { | |
51 const char* product_name = device::UsbIds::GetProductName( | 47 const char* product_name = device::UsbIds::GetProductName( |
52 device->vendor_id(), device->product_id()); | 48 device->vendor_id(), device->product_id()); |
53 if (product_name) { | 49 if (product_name) { |
54 product_string = base::UTF8ToUTF16(product_name); | 50 product_string = base::UTF8ToUTF16(product_name); |
55 } else { | 51 } else { |
56 base::string16 product_id = base::ASCIIToUTF16( | 52 base::string16 product_id = base::ASCIIToUTF16( |
57 base::StringPrintf("0x%04x", device->product_id())); | 53 base::StringPrintf("0x%04x", device->product_id())); |
58 product_string = | 54 product_string = |
59 l10n_util::GetStringFUTF16(IDS_DEVICE_UNKNOWN_PRODUCT, product_id); | 55 l10n_util::GetStringFUTF16(IDS_DEVICE_UNKNOWN_PRODUCT, product_id); |
60 } | 56 } |
61 } | 57 } |
62 | 58 |
63 if (!device->GetSerialNumber(&serial_number)) { | |
64 serial_number.clear(); | |
65 } | |
66 | |
67 name = l10n_util::GetStringFUTF16(IDS_DEVICE_PERMISSIONS_DEVICE_NAME, | 59 name = l10n_util::GetStringFUTF16(IDS_DEVICE_PERMISSIONS_DEVICE_NAME, |
68 product_string, manufacturer_string); | 60 product_string, manufacturer_string); |
69 } | 61 } |
70 | 62 |
71 DevicePermissionsPrompt::Prompt::DeviceInfo::~DeviceInfo() { | 63 DevicePermissionsPrompt::Prompt::DeviceInfo::~DeviceInfo() { |
72 } | 64 } |
73 | 65 |
74 DevicePermissionsPrompt::Prompt::Prompt() | 66 DevicePermissionsPrompt::Prompt::Prompt() : usb_service_observer_(this) { |
75 : extension_(nullptr), | |
76 browser_context_(nullptr), | |
77 multiple_(false), | |
78 observer_(nullptr), | |
79 usb_service_observer_(this) { | |
80 } | 67 } |
81 | 68 |
82 void DevicePermissionsPrompt::Prompt::SetObserver(Observer* observer) { | 69 void DevicePermissionsPrompt::Prompt::SetObserver(Observer* observer) { |
83 observer_ = observer; | 70 observer_ = observer; |
84 | 71 |
85 if (observer_) { | 72 if (observer_) { |
86 BrowserThread::PostTask( | 73 UsbService* service = device::DeviceClient::Get()->GetUsbService(); |
87 BrowserThread::FILE, FROM_HERE, | 74 if (service && !usb_service_observer_.IsObserving(service)) { |
88 base::Bind(&DevicePermissionsPrompt::Prompt::DoDeviceQuery, this)); | 75 service->GetDevices(base::Bind( |
| 76 &DevicePermissionsPrompt::Prompt::OnDevicesEnumerated, this)); |
| 77 usb_service_observer_.Add(service); |
| 78 } |
89 } | 79 } |
90 } | 80 } |
91 | 81 |
92 base::string16 DevicePermissionsPrompt::Prompt::GetHeading() const { | 82 base::string16 DevicePermissionsPrompt::Prompt::GetHeading() const { |
93 return l10n_util::GetStringUTF16( | 83 return l10n_util::GetStringUTF16( |
94 multiple_ ? IDS_DEVICE_PERMISSIONS_PROMPT_TITLE_MULTIPLE | 84 multiple_ ? IDS_DEVICE_PERMISSIONS_PROMPT_TITLE_MULTIPLE |
95 : IDS_DEVICE_PERMISSIONS_PROMPT_TITLE_SINGLE); | 85 : IDS_DEVICE_PERMISSIONS_PROMPT_TITLE_SINGLE); |
96 } | 86 } |
97 | 87 |
98 base::string16 DevicePermissionsPrompt::Prompt::GetPromptMessage() const { | 88 base::string16 DevicePermissionsPrompt::Prompt::GetPromptMessage() const { |
99 return l10n_util::GetStringFUTF16(multiple_ | 89 return l10n_util::GetStringFUTF16(multiple_ |
100 ? IDS_DEVICE_PERMISSIONS_PROMPT_MULTIPLE | 90 ? IDS_DEVICE_PERMISSIONS_PROMPT_MULTIPLE |
101 : IDS_DEVICE_PERMISSIONS_PROMPT_SINGLE, | 91 : IDS_DEVICE_PERMISSIONS_PROMPT_SINGLE, |
102 base::UTF8ToUTF16(extension_->name())); | 92 base::UTF8ToUTF16(extension_->name())); |
103 } | 93 } |
104 | 94 |
105 scoped_refptr<UsbDevice> DevicePermissionsPrompt::Prompt::GetDevice( | 95 scoped_refptr<UsbDevice> DevicePermissionsPrompt::Prompt::GetDevice( |
106 size_t index) const { | 96 size_t index) const { |
107 DCHECK_LT(index, devices_.size()); | 97 DCHECK_LT(index, devices_.size()); |
108 return devices_[index].device; | 98 return devices_[index].device; |
109 } | 99 } |
110 | 100 |
111 void DevicePermissionsPrompt::Prompt::GrantDevicePermission( | 101 void DevicePermissionsPrompt::Prompt::GrantDevicePermission( |
112 size_t index) const { | 102 size_t index) const { |
113 DCHECK_LT(index, devices_.size()); | 103 DCHECK_LT(index, devices_.size()); |
114 DevicePermissionsManager* permissions_manager = | 104 DevicePermissionsManager* permissions_manager = |
115 DevicePermissionsManager::Get(browser_context_); | 105 DevicePermissionsManager::Get(browser_context_); |
116 if (permissions_manager) { | 106 if (permissions_manager) { |
117 const DeviceInfo& device = devices_[index]; | 107 const DeviceInfo& device = devices_[index]; |
118 permissions_manager->AllowUsbDevice( | 108 permissions_manager->AllowUsbDevice(extension_->id(), device.device); |
119 extension_->id(), device.device, device.original_product_string, | |
120 device.original_manufacturer_string, device.serial_number); | |
121 } | 109 } |
122 } | 110 } |
123 | 111 |
124 void DevicePermissionsPrompt::Prompt::set_filters( | 112 void DevicePermissionsPrompt::Prompt::set_filters( |
125 const std::vector<UsbDeviceFilter>& filters) { | 113 const std::vector<UsbDeviceFilter>& filters) { |
126 filters_ = filters; | 114 filters_ = filters; |
127 } | 115 } |
128 | 116 |
129 DevicePermissionsPrompt::Prompt::~Prompt() { | 117 DevicePermissionsPrompt::Prompt::~Prompt() { |
130 } | 118 } |
131 | 119 |
132 void DevicePermissionsPrompt::Prompt::DoDeviceQuery() { | 120 void DevicePermissionsPrompt::Prompt::OnDeviceAdded( |
133 UsbService* service = device::DeviceClient::Get()->GetUsbService(); | 121 scoped_refptr<UsbDevice> device) { |
134 if (!service) { | 122 if (!(filters_.empty() || UsbDeviceFilter::MatchesAny(device, filters_))) { |
135 return; | 123 return; |
136 } | 124 } |
137 | 125 |
138 std::vector<scoped_refptr<UsbDevice>> devices; | 126 device->CheckUsbAccess(base::Bind( |
139 service->GetDevices(&devices); | 127 &DevicePermissionsPrompt::Prompt::AddCheckedUsbDevice, this, device)); |
140 | |
141 if (!usb_service_observer_.IsObserving(service)) { | |
142 usb_service_observer_.Add(service); | |
143 } | |
144 | |
145 std::vector<DeviceInfo>* device_info = new std::vector<DeviceInfo>(); | |
146 base::Closure barrier = base::BarrierClosure( | |
147 devices.size(), | |
148 base::Bind(&DevicePermissionsPrompt::Prompt::DeviceQueryComplete, this, | |
149 base::Owned(device_info))); | |
150 | |
151 for (const auto& device : devices) { | |
152 if (filters_.empty() || UsbDeviceFilter::MatchesAny(device, filters_)) { | |
153 device->CheckUsbAccess( | |
154 base::Bind(&DevicePermissionsPrompt::Prompt::AppendCheckedUsbDevice, | |
155 this, device_info, device, barrier)); | |
156 } else { | |
157 barrier.Run(); | |
158 } | |
159 } | |
160 } | 128 } |
161 | 129 |
162 void DevicePermissionsPrompt::Prompt::AppendCheckedUsbDevice( | 130 void DevicePermissionsPrompt::Prompt::OnDeviceRemoved( |
163 std::vector<DeviceInfo>* device_info, | |
164 scoped_refptr<UsbDevice> device, | |
165 const base::Closure& callback, | |
166 bool allowed) { | |
167 if (allowed) { | |
168 device_info->push_back(DeviceInfo(device)); | |
169 } | |
170 callback.Run(); | |
171 } | |
172 | |
173 void DevicePermissionsPrompt::Prompt::AddCheckedUsbDevice( | |
174 scoped_refptr<UsbDevice> device, | |
175 bool allowed) { | |
176 if (allowed) { | |
177 BrowserThread::PostTask( | |
178 BrowserThread::UI, FROM_HERE, | |
179 base::Bind(&DevicePermissionsPrompt::Prompt::AddDevice, this, | |
180 DeviceInfo(device))); | |
181 } | |
182 } | |
183 | |
184 void DevicePermissionsPrompt::Prompt::DeviceQueryComplete( | |
185 std::vector<DeviceInfo>* device_info) { | |
186 BrowserThread::PostTask( | |
187 BrowserThread::UI, FROM_HERE, | |
188 base::Bind(&DevicePermissionsPrompt::Prompt::SetDevices, this, | |
189 *device_info)); | |
190 } | |
191 | |
192 void DevicePermissionsPrompt::Prompt::SetDevices( | |
193 const std::vector<DeviceInfo>& devices) { | |
194 devices_ = devices; | |
195 if (observer_) { | |
196 observer_->OnDevicesChanged(); | |
197 } | |
198 } | |
199 | |
200 void DevicePermissionsPrompt::Prompt::AddDevice(const DeviceInfo& device) { | |
201 devices_.push_back(device); | |
202 if (observer_) { | |
203 observer_->OnDevicesChanged(); | |
204 } | |
205 } | |
206 | |
207 void DevicePermissionsPrompt::Prompt::RemoveDevice( | |
208 scoped_refptr<UsbDevice> device) { | 131 scoped_refptr<UsbDevice> device) { |
209 bool removed_entry = false; | 132 bool removed_entry = false; |
210 for (std::vector<DeviceInfo>::iterator it = devices_.begin(); | 133 for (std::vector<DeviceInfo>::iterator it = devices_.begin(); |
211 it != devices_.end(); ++it) { | 134 it != devices_.end(); ++it) { |
212 if (it->device == device) { | 135 if (it->device == device) { |
213 devices_.erase(it); | 136 devices_.erase(it); |
214 removed_entry = true; | 137 removed_entry = true; |
215 break; | 138 break; |
216 } | 139 } |
217 } | 140 } |
218 if (observer_ && removed_entry) { | 141 if (observer_ && removed_entry) { |
219 observer_->OnDevicesChanged(); | 142 observer_->OnDevicesChanged(); |
220 } | 143 } |
221 } | 144 } |
222 | 145 |
223 void DevicePermissionsPrompt::Prompt::OnDeviceAdded( | 146 void DevicePermissionsPrompt::Prompt::OnDevicesEnumerated( |
224 scoped_refptr<UsbDevice> device) { | 147 const std::vector<scoped_refptr<UsbDevice>>& devices) { |
225 if (!(filters_.empty() || UsbDeviceFilter::MatchesAny(device, filters_))) { | 148 for (const auto& device : devices) { |
226 return; | 149 if (filters_.empty() || UsbDeviceFilter::MatchesAny(device, filters_)) { |
| 150 device->CheckUsbAccess(base::Bind( |
| 151 &DevicePermissionsPrompt::Prompt::AddCheckedUsbDevice, this, device)); |
| 152 } |
227 } | 153 } |
228 | |
229 device->CheckUsbAccess(base::Bind( | |
230 &DevicePermissionsPrompt::Prompt::AddCheckedUsbDevice, this, device)); | |
231 } | 154 } |
232 | 155 |
233 void DevicePermissionsPrompt::Prompt::OnDeviceRemoved( | 156 void DevicePermissionsPrompt::Prompt::AddCheckedUsbDevice( |
234 scoped_refptr<UsbDevice> device) { | 157 scoped_refptr<UsbDevice> device, |
235 BrowserThread::PostTask( | 158 bool allowed) { |
236 BrowserThread::UI, FROM_HERE, | 159 if (allowed) { |
237 base::Bind(&DevicePermissionsPrompt::Prompt::RemoveDevice, this, device)); | 160 devices_.push_back(DeviceInfo(device)); |
| 161 if (observer_) { |
| 162 observer_->OnDevicesChanged(); |
| 163 } |
| 164 } |
238 } | 165 } |
239 | 166 |
240 DevicePermissionsPrompt::DevicePermissionsPrompt( | 167 DevicePermissionsPrompt::DevicePermissionsPrompt( |
241 content::WebContents* web_contents) | 168 content::WebContents* web_contents) |
242 : web_contents_(web_contents), delegate_(nullptr) { | 169 : web_contents_(web_contents), delegate_(nullptr) { |
243 } | 170 } |
244 | 171 |
245 DevicePermissionsPrompt::~DevicePermissionsPrompt() { | 172 DevicePermissionsPrompt::~DevicePermissionsPrompt() { |
246 } | 173 } |
247 | 174 |
248 void DevicePermissionsPrompt::AskForUsbDevices( | 175 void DevicePermissionsPrompt::AskForUsbDevices( |
249 Delegate* delegate, | 176 Delegate* delegate, |
250 const Extension* extension, | 177 const Extension* extension, |
251 content::BrowserContext* context, | 178 content::BrowserContext* context, |
252 bool multiple, | 179 bool multiple, |
253 const std::vector<UsbDeviceFilter>& filters) { | 180 const std::vector<UsbDeviceFilter>& filters) { |
254 prompt_ = new Prompt(); | 181 prompt_ = new Prompt(); |
255 prompt_->set_extension(extension); | 182 prompt_->set_extension(extension); |
256 prompt_->set_browser_context(context); | 183 prompt_->set_browser_context(context); |
257 prompt_->set_multiple(multiple); | 184 prompt_->set_multiple(multiple); |
258 prompt_->set_filters(filters); | 185 prompt_->set_filters(filters); |
259 delegate_ = delegate; | 186 delegate_ = delegate; |
260 | 187 |
261 ShowDialog(); | 188 ShowDialog(); |
262 } | 189 } |
263 | 190 |
264 } // namespace extensions | 191 } // namespace extensions |
OLD | NEW |