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() |
75 : extension_(nullptr), | 67 : extension_(nullptr), |
76 browser_context_(nullptr), | 68 browser_context_(nullptr), |
77 multiple_(false), | 69 multiple_(false), |
78 observer_(nullptr), | 70 observer_(nullptr), |
79 usb_service_observer_(this) { | 71 usb_service_observer_(this) { |
80 } | 72 } |
81 | 73 |
82 void DevicePermissionsPrompt::Prompt::SetObserver(Observer* observer) { | 74 void DevicePermissionsPrompt::Prompt::SetObserver(Observer* observer) { |
83 observer_ = observer; | 75 observer_ = observer; |
84 | 76 |
85 if (observer_) { | 77 if (observer_) { |
86 BrowserThread::PostTask( | 78 UsbService* service = device::DeviceClient::Get()->GetUsbService(); |
87 BrowserThread::FILE, FROM_HERE, | 79 if (!service) { |
88 base::Bind(&DevicePermissionsPrompt::Prompt::DoDeviceQuery, this)); | 80 return; |
81 } | |
82 | |
83 service->GetDevices(base::Bind( | |
84 &DevicePermissionsPrompt::Prompt::OnDevicesEnumerated, this)); | |
85 | |
86 if (!usb_service_observer_.IsObserving(service)) { | |
87 usb_service_observer_.Add(service); | |
Ken Rockot(use gerrit already)
2015/04/08 07:45:57
Why is DevicePermissionsPrompt's observation of Us
Reilly Grant (use Gerrit)
2015/04/08 21:39:04
I want to start observing and call GetDevices at t
| |
88 } | |
89 } | 89 } |
90 } | 90 } |
91 | 91 |
92 base::string16 DevicePermissionsPrompt::Prompt::GetHeading() const { | 92 base::string16 DevicePermissionsPrompt::Prompt::GetHeading() const { |
93 return l10n_util::GetStringUTF16( | 93 return l10n_util::GetStringUTF16( |
94 multiple_ ? IDS_DEVICE_PERMISSIONS_PROMPT_TITLE_MULTIPLE | 94 multiple_ ? IDS_DEVICE_PERMISSIONS_PROMPT_TITLE_MULTIPLE |
95 : IDS_DEVICE_PERMISSIONS_PROMPT_TITLE_SINGLE); | 95 : IDS_DEVICE_PERMISSIONS_PROMPT_TITLE_SINGLE); |
96 } | 96 } |
97 | 97 |
98 base::string16 DevicePermissionsPrompt::Prompt::GetPromptMessage() const { | 98 base::string16 DevicePermissionsPrompt::Prompt::GetPromptMessage() const { |
99 return l10n_util::GetStringFUTF16(multiple_ | 99 return l10n_util::GetStringFUTF16(multiple_ |
100 ? IDS_DEVICE_PERMISSIONS_PROMPT_MULTIPLE | 100 ? IDS_DEVICE_PERMISSIONS_PROMPT_MULTIPLE |
101 : IDS_DEVICE_PERMISSIONS_PROMPT_SINGLE, | 101 : IDS_DEVICE_PERMISSIONS_PROMPT_SINGLE, |
102 base::UTF8ToUTF16(extension_->name())); | 102 base::UTF8ToUTF16(extension_->name())); |
103 } | 103 } |
104 | 104 |
105 scoped_refptr<UsbDevice> DevicePermissionsPrompt::Prompt::GetDevice( | 105 scoped_refptr<UsbDevice> DevicePermissionsPrompt::Prompt::GetDevice( |
106 size_t index) const { | 106 size_t index) const { |
107 DCHECK_LT(index, devices_.size()); | 107 DCHECK_LT(index, devices_.size()); |
108 return devices_[index].device; | 108 return devices_[index].device; |
109 } | 109 } |
110 | 110 |
111 void DevicePermissionsPrompt::Prompt::GrantDevicePermission( | 111 void DevicePermissionsPrompt::Prompt::GrantDevicePermission( |
112 size_t index) const { | 112 size_t index) const { |
113 DCHECK_LT(index, devices_.size()); | 113 DCHECK_LT(index, devices_.size()); |
114 DevicePermissionsManager* permissions_manager = | 114 DevicePermissionsManager* permissions_manager = |
115 DevicePermissionsManager::Get(browser_context_); | 115 DevicePermissionsManager::Get(browser_context_); |
116 if (permissions_manager) { | 116 if (permissions_manager) { |
117 const DeviceInfo& device = devices_[index]; | 117 const DeviceInfo& device = devices_[index]; |
118 permissions_manager->AllowUsbDevice( | 118 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 } | 119 } |
122 } | 120 } |
123 | 121 |
124 void DevicePermissionsPrompt::Prompt::set_filters( | 122 void DevicePermissionsPrompt::Prompt::set_filters( |
125 const std::vector<UsbDeviceFilter>& filters) { | 123 const std::vector<UsbDeviceFilter>& filters) { |
126 filters_ = filters; | 124 filters_ = filters; |
127 } | 125 } |
128 | 126 |
129 DevicePermissionsPrompt::Prompt::~Prompt() { | 127 DevicePermissionsPrompt::Prompt::~Prompt() { |
130 } | 128 } |
131 | 129 |
132 void DevicePermissionsPrompt::Prompt::DoDeviceQuery() { | 130 void DevicePermissionsPrompt::Prompt::OnDeviceAdded( |
133 UsbService* service = device::DeviceClient::Get()->GetUsbService(); | 131 scoped_refptr<UsbDevice> device) { |
134 if (!service) { | 132 if (!(filters_.empty() || UsbDeviceFilter::MatchesAny(device, filters_))) { |
135 return; | 133 return; |
136 } | 134 } |
137 | 135 |
138 std::vector<scoped_refptr<UsbDevice>> devices; | 136 device->CheckUsbAccess(base::Bind( |
139 service->GetDevices(&devices); | 137 &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 } | 138 } |
161 | 139 |
162 void DevicePermissionsPrompt::Prompt::AppendCheckedUsbDevice( | 140 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) { | 141 scoped_refptr<UsbDevice> device) { |
209 bool removed_entry = false; | 142 bool removed_entry = false; |
210 for (std::vector<DeviceInfo>::iterator it = devices_.begin(); | 143 for (std::vector<DeviceInfo>::iterator it = devices_.begin(); |
211 it != devices_.end(); ++it) { | 144 it != devices_.end(); ++it) { |
212 if (it->device == device) { | 145 if (it->device == device) { |
213 devices_.erase(it); | 146 devices_.erase(it); |
214 removed_entry = true; | 147 removed_entry = true; |
215 break; | 148 break; |
216 } | 149 } |
217 } | 150 } |
218 if (observer_ && removed_entry) { | 151 if (observer_ && removed_entry) { |
219 observer_->OnDevicesChanged(); | 152 observer_->OnDevicesChanged(); |
220 } | 153 } |
221 } | 154 } |
222 | 155 |
223 void DevicePermissionsPrompt::Prompt::OnDeviceAdded( | 156 void DevicePermissionsPrompt::Prompt::OnDevicesEnumerated( |
224 scoped_refptr<UsbDevice> device) { | 157 const std::vector<scoped_refptr<UsbDevice>>& devices) { |
225 if (!(filters_.empty() || UsbDeviceFilter::MatchesAny(device, filters_))) { | 158 for (const auto& device : devices) { |
226 return; | 159 if (filters_.empty() || UsbDeviceFilter::MatchesAny(device, filters_)) { |
160 device->CheckUsbAccess(base::Bind( | |
161 &DevicePermissionsPrompt::Prompt::AddCheckedUsbDevice, this, device)); | |
162 } | |
227 } | 163 } |
228 | |
229 device->CheckUsbAccess(base::Bind( | |
230 &DevicePermissionsPrompt::Prompt::AddCheckedUsbDevice, this, device)); | |
231 } | 164 } |
232 | 165 |
233 void DevicePermissionsPrompt::Prompt::OnDeviceRemoved( | 166 void DevicePermissionsPrompt::Prompt::AddCheckedUsbDevice( |
234 scoped_refptr<UsbDevice> device) { | 167 scoped_refptr<UsbDevice> device, |
235 BrowserThread::PostTask( | 168 bool allowed) { |
236 BrowserThread::UI, FROM_HERE, | 169 if (allowed) { |
237 base::Bind(&DevicePermissionsPrompt::Prompt::RemoveDevice, this, device)); | 170 devices_.push_back(DeviceInfo(device)); |
171 if (observer_) { | |
172 observer_->OnDevicesChanged(); | |
173 } | |
174 } | |
238 } | 175 } |
239 | 176 |
240 DevicePermissionsPrompt::DevicePermissionsPrompt( | 177 DevicePermissionsPrompt::DevicePermissionsPrompt( |
241 content::WebContents* web_contents) | 178 content::WebContents* web_contents) |
242 : web_contents_(web_contents), delegate_(nullptr) { | 179 : web_contents_(web_contents), delegate_(nullptr) { |
243 } | 180 } |
244 | 181 |
245 DevicePermissionsPrompt::~DevicePermissionsPrompt() { | 182 DevicePermissionsPrompt::~DevicePermissionsPrompt() { |
246 } | 183 } |
247 | 184 |
248 void DevicePermissionsPrompt::AskForUsbDevices( | 185 void DevicePermissionsPrompt::AskForUsbDevices( |
249 Delegate* delegate, | 186 Delegate* delegate, |
250 const Extension* extension, | 187 const Extension* extension, |
251 content::BrowserContext* context, | 188 content::BrowserContext* context, |
252 bool multiple, | 189 bool multiple, |
253 const std::vector<UsbDeviceFilter>& filters) { | 190 const std::vector<UsbDeviceFilter>& filters) { |
254 prompt_ = new Prompt(); | 191 prompt_ = new Prompt(); |
255 prompt_->set_extension(extension); | 192 prompt_->set_extension(extension); |
256 prompt_->set_browser_context(context); | 193 prompt_->set_browser_context(context); |
257 prompt_->set_multiple(multiple); | 194 prompt_->set_multiple(multiple); |
258 prompt_->set_filters(filters); | 195 prompt_->set_filters(filters); |
259 delegate_ = delegate; | 196 delegate_ = delegate; |
260 | 197 |
261 ShowDialog(); | 198 ShowDialog(); |
262 } | 199 } |
263 | 200 |
264 } // namespace extensions | 201 } // namespace extensions |
OLD | NEW |