Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(664)

Side by Side Diff: chrome/browser/usb/usb_chooser_controller.cc

Issue 2746313002: Remove RenderFrameHost pointer from ChooserController. (Closed)
Patch Set: Fix Android build and juncai@ comment. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 17 matching lines...) Expand all
28 #include "device/usb/mojo/type_converters.h" 28 #include "device/usb/mojo/type_converters.h"
29 #include "device/usb/usb_device.h" 29 #include "device/usb/usb_device.h"
30 #include "device/usb/usb_device_filter.h" 30 #include "device/usb/usb_device_filter.h"
31 #include "device/usb/usb_ids.h" 31 #include "device/usb/usb_ids.h"
32 #include "device/usb/webusb_descriptors.h" 32 #include "device/usb/webusb_descriptors.h"
33 #include "ui/base/l10n/l10n_util.h" 33 #include "ui/base/l10n/l10n_util.h"
34 #include "url/gurl.h" 34 #include "url/gurl.h"
35 35
36 using content::RenderFrameHost; 36 using content::RenderFrameHost;
37 using content::WebContents; 37 using content::WebContents;
38 using device::UsbDevice;
39 using device::UsbDeviceFilter;
38 40
39 namespace { 41 namespace {
40 42
41 Browser* GetBrowser() { 43 Browser* GetBrowser() {
42 chrome::ScopedTabbedBrowserDisplayer browser_displayer( 44 chrome::ScopedTabbedBrowserDisplayer browser_displayer(
43 ProfileManager::GetActiveUserProfile()); 45 ProfileManager::GetActiveUserProfile());
44 DCHECK(browser_displayer.browser()); 46 DCHECK(browser_displayer.browser());
45 return browser_displayer.browser(); 47 return browser_displayer.browser();
46 } 48 }
47 49
48 base::string16 GetDeviceName(scoped_refptr<device::UsbDevice> device) { 50 base::string16 GetDeviceName(scoped_refptr<UsbDevice> device) {
49 base::string16 device_name = device->product_string(); 51 base::string16 device_name = device->product_string();
50 if (device_name.empty()) { 52 if (device_name.empty()) {
51 uint16_t vendor_id = device->vendor_id(); 53 uint16_t vendor_id = device->vendor_id();
52 uint16_t product_id = device->product_id(); 54 uint16_t product_id = device->product_id();
53 if (const char* product_name = 55 if (const char* product_name =
54 device::UsbIds::GetProductName(vendor_id, product_id)) { 56 device::UsbIds::GetProductName(vendor_id, product_id)) {
55 device_name = base::UTF8ToUTF16(product_name); 57 device_name = base::UTF8ToUTF16(product_name);
56 } else if (const char* vendor_name = 58 } else if (const char* vendor_name =
57 device::UsbIds::GetVendorName(vendor_id)) { 59 device::UsbIds::GetVendorName(vendor_id)) {
58 device_name = l10n_util::GetStringFUTF16( 60 device_name = l10n_util::GetStringFUTF16(
59 IDS_DEVICE_CHOOSER_DEVICE_NAME_UNKNOWN_DEVICE_WITH_VENDOR_NAME, 61 IDS_DEVICE_CHOOSER_DEVICE_NAME_UNKNOWN_DEVICE_WITH_VENDOR_NAME,
60 base::UTF8ToUTF16(vendor_name)); 62 base::UTF8ToUTF16(vendor_name));
61 } else { 63 } else {
62 device_name = l10n_util::GetStringFUTF16( 64 device_name = l10n_util::GetStringFUTF16(
63 IDS_DEVICE_CHOOSER_DEVICE_NAME_UNKNOWN_DEVICE_WITH_VENDOR_ID_AND_PRODU CT_ID, 65 IDS_DEVICE_CHOOSER_DEVICE_NAME_UNKNOWN_DEVICE_WITH_VENDOR_ID_AND_PRODU CT_ID,
64 base::ASCIIToUTF16(base::StringPrintf("%04x", vendor_id)), 66 base::ASCIIToUTF16(base::StringPrintf("%04x", vendor_id)),
65 base::ASCIIToUTF16(base::StringPrintf("%04x", product_id))); 67 base::ASCIIToUTF16(base::StringPrintf("%04x", product_id)));
66 } 68 }
67 } 69 }
68 70
69 return device_name; 71 return device_name;
70 } 72 }
71 73
72 } // namespace 74 } // namespace
73 75
74 UsbChooserController::UsbChooserController( 76 UsbChooserController::UsbChooserController(
75 RenderFrameHost* render_frame_host, 77 RenderFrameHost* render_frame_host,
76 const std::vector<device::UsbDeviceFilter>& device_filters, 78 const std::vector<UsbDeviceFilter>& device_filters,
77 const device::usb::ChooserService::GetPermissionCallback& callback) 79 const device::usb::ChooserService::GetPermissionCallback& callback)
78 : ChooserController(render_frame_host, 80 : ChooserController(render_frame_host,
79 IDS_USB_DEVICE_CHOOSER_PROMPT_ORIGIN, 81 IDS_USB_DEVICE_CHOOSER_PROMPT_ORIGIN,
80 IDS_USB_DEVICE_CHOOSER_PROMPT_EXTENSION_NAME), 82 IDS_USB_DEVICE_CHOOSER_PROMPT_EXTENSION_NAME),
81 render_frame_host_(render_frame_host), 83 filters_(device_filters),
82 callback_(callback), 84 callback_(callback),
83 usb_service_observer_(this), 85 usb_service_observer_(this),
84 filters_(device_filters),
85 weak_factory_(this) { 86 weak_factory_(this) {
86 device::UsbService* usb_service = 87 device::UsbService* usb_service =
87 device::DeviceClient::Get()->GetUsbService(); 88 device::DeviceClient::Get()->GetUsbService();
88 if (!usb_service) 89 if (usb_service) {
89 return; 90 usb_service_observer_.Add(usb_service);
91 usb_service->GetDevices(base::Bind(&UsbChooserController::GotUsbDeviceList,
92 weak_factory_.GetWeakPtr()));
93 }
90 94
91 if (!usb_service_observer_.IsObserving(usb_service)) 95 WebContents* web_contents =
92 usb_service_observer_.Add(usb_service); 96 WebContents::FromRenderFrameHost(render_frame_host);
93 97 RenderFrameHost* main_frame = web_contents->GetMainFrame();
94 usb_service->GetDevices(base::Bind(&UsbChooserController::GotUsbDeviceList, 98 requesting_origin_ = render_frame_host->GetLastCommittedURL().GetOrigin();
95 weak_factory_.GetWeakPtr())); 99 embedding_origin_ = main_frame->GetLastCommittedURL().GetOrigin();
100 is_embedded_frame_ = render_frame_host != main_frame;
101 Profile* profile =
102 Profile::FromBrowserContext(web_contents->GetBrowserContext());
103 chooser_context_ =
104 UsbChooserContextFactory::GetForProfile(profile)->AsWeakPtr();
96 } 105 }
97 106
98 UsbChooserController::~UsbChooserController() { 107 UsbChooserController::~UsbChooserController() {
99 if (!callback_.is_null()) 108 if (!callback_.is_null())
100 callback_.Run(nullptr); 109 callback_.Run(nullptr);
101 } 110 }
102 111
103 base::string16 UsbChooserController::GetNoOptionsText() const { 112 base::string16 UsbChooserController::GetNoOptionsText() const {
104 return l10n_util::GetStringUTF16(IDS_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT); 113 return l10n_util::GetStringUTF16(IDS_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT);
105 } 114 }
(...skipping 12 matching lines...) Expand all
118 const auto& it = device_name_map_.find(device_name); 127 const auto& it = device_name_map_.find(device_name);
119 DCHECK(it != device_name_map_.end()); 128 DCHECK(it != device_name_map_.end());
120 return it->second == 1 129 return it->second == 1
121 ? device_name 130 ? device_name
122 : l10n_util::GetStringFUTF16( 131 : l10n_util::GetStringFUTF16(
123 IDS_DEVICE_CHOOSER_DEVICE_NAME_WITH_ID, device_name, 132 IDS_DEVICE_CHOOSER_DEVICE_NAME_WITH_ID, device_name,
124 devices_[index].first->serial_number()); 133 devices_[index].first->serial_number());
125 } 134 }
126 135
127 bool UsbChooserController::IsPaired(size_t index) const { 136 bool UsbChooserController::IsPaired(size_t index) const {
128 return WebUSBPermissionProvider::HasDevicePermission(render_frame_host_, 137 scoped_refptr<UsbDevice> device = devices_[index].first;
129 devices_[index].first); 138
139 if (!chooser_context_)
140 return false;
141
142 return WebUSBPermissionProvider::HasDevicePermission(
143 chooser_context_.get(), requesting_origin_, embedding_origin_,
144 is_embedded_frame_, device);
130 } 145 }
131 146
132 void UsbChooserController::RefreshOptions() {} 147 void UsbChooserController::RefreshOptions() {}
133 148
134 base::string16 UsbChooserController::GetStatus() const { 149 base::string16 UsbChooserController::GetStatus() const {
135 return base::string16(); 150 return base::string16();
136 } 151 }
137 152
138 void UsbChooserController::Select(const std::vector<size_t>& indices) { 153 void UsbChooserController::Select(const std::vector<size_t>& indices) {
139 DCHECK_EQ(1u, indices.size()); 154 DCHECK_EQ(1u, indices.size());
140 size_t index = indices[0]; 155 size_t index = indices[0];
141 DCHECK_LT(index, devices_.size()); 156 DCHECK_LT(index, devices_.size());
142 WebContents* web_contents = 157
143 WebContents::FromRenderFrameHost(render_frame_host_); 158 if (chooser_context_) {
144 GURL embedding_origin = 159 chooser_context_->GrantDevicePermission(
145 web_contents->GetMainFrame()->GetLastCommittedURL().GetOrigin(); 160 requesting_origin_, embedding_origin_, devices_[index].first->guid());
146 Profile* profile = 161 }
147 Profile::FromBrowserContext(web_contents->GetBrowserContext());
148 UsbChooserContext* chooser_context =
149 UsbChooserContextFactory::GetForProfile(profile);
150 chooser_context->GrantDevicePermission(
151 render_frame_host_->GetLastCommittedURL().GetOrigin(), embedding_origin,
152 devices_[index].first->guid());
153 162
154 device::usb::DeviceInfoPtr device_info_ptr = 163 device::usb::DeviceInfoPtr device_info_ptr =
155 device::usb::DeviceInfo::From(*devices_[index].first); 164 device::usb::DeviceInfo::From(*devices_[index].first);
156 callback_.Run(std::move(device_info_ptr)); 165 callback_.Run(std::move(device_info_ptr));
157 callback_.Reset(); // Reset |callback_| so that it is only run once. 166 callback_.Reset(); // Reset |callback_| so that it is only run once.
158 167
159 RecordWebUsbChooserClosure( 168 RecordWebUsbChooserClosure(
160 devices_[index].first->serial_number().empty() 169 devices_[index].first->serial_number().empty()
161 ? WEBUSB_CHOOSER_CLOSED_EPHEMERAL_PERMISSION_GRANTED 170 ? WEBUSB_CHOOSER_CLOSED_EPHEMERAL_PERMISSION_GRANTED
162 : WEBUSB_CHOOSER_CLOSED_PERMISSION_GRANTED); 171 : WEBUSB_CHOOSER_CLOSED_PERMISSION_GRANTED);
163 } 172 }
164 173
165 void UsbChooserController::Cancel() { 174 void UsbChooserController::Cancel() {
166 RecordWebUsbChooserClosure(devices_.size() == 0 175 RecordWebUsbChooserClosure(devices_.size() == 0
167 ? WEBUSB_CHOOSER_CLOSED_CANCELLED_NO_DEVICES 176 ? WEBUSB_CHOOSER_CLOSED_CANCELLED_NO_DEVICES
168 : WEBUSB_CHOOSER_CLOSED_CANCELLED); 177 : WEBUSB_CHOOSER_CLOSED_CANCELLED);
169 } 178 }
170 179
171 void UsbChooserController::Close() {} 180 void UsbChooserController::Close() {}
172 181
173 void UsbChooserController::OpenHelpCenterUrl() const { 182 void UsbChooserController::OpenHelpCenterUrl() const {
174 GetBrowser()->OpenURL(content::OpenURLParams( 183 GetBrowser()->OpenURL(content::OpenURLParams(
175 GURL(chrome::kChooserUsbOverviewURL), content::Referrer(), 184 GURL(chrome::kChooserUsbOverviewURL), content::Referrer(),
176 WindowOpenDisposition::NEW_FOREGROUND_TAB, 185 WindowOpenDisposition::NEW_FOREGROUND_TAB,
177 ui::PAGE_TRANSITION_AUTO_TOPLEVEL, false /* is_renderer_initialized */)); 186 ui::PAGE_TRANSITION_AUTO_TOPLEVEL, false /* is_renderer_initialized */));
178 } 187 }
179 188
180 void UsbChooserController::OnDeviceAdded( 189 void UsbChooserController::OnDeviceAdded(scoped_refptr<UsbDevice> device) {
181 scoped_refptr<device::UsbDevice> device) {
182 if (DisplayDevice(device)) { 190 if (DisplayDevice(device)) {
183 base::string16 device_name = GetDeviceName(device); 191 base::string16 device_name = GetDeviceName(device);
184 devices_.push_back(std::make_pair(device, device_name)); 192 devices_.push_back(std::make_pair(device, device_name));
185 ++device_name_map_[device_name]; 193 ++device_name_map_[device_name];
186 if (view()) 194 if (view())
187 view()->OnOptionAdded(devices_.size() - 1); 195 view()->OnOptionAdded(devices_.size() - 1);
188 } 196 }
189 } 197 }
190 198
191 void UsbChooserController::OnDeviceRemoved( 199 void UsbChooserController::OnDeviceRemoved(scoped_refptr<UsbDevice> device) {
192 scoped_refptr<device::UsbDevice> device) {
193 for (auto it = devices_.begin(); it != devices_.end(); ++it) { 200 for (auto it = devices_.begin(); it != devices_.end(); ++it) {
194 if (it->first == device) { 201 if (it->first == device) {
195 size_t index = it - devices_.begin(); 202 size_t index = it - devices_.begin();
196 DCHECK_GT(device_name_map_[it->second], 0); 203 DCHECK_GT(device_name_map_[it->second], 0);
197 if (--device_name_map_[it->second] == 0) 204 if (--device_name_map_[it->second] == 0)
198 device_name_map_.erase(it->second); 205 device_name_map_.erase(it->second);
199 devices_.erase(it); 206 devices_.erase(it);
200 if (view()) 207 if (view())
201 view()->OnOptionRemoved(index); 208 view()->OnOptionRemoved(index);
202 return; 209 return;
203 } 210 }
204 } 211 }
205 } 212 }
206 213
207 // Get a list of devices that can be shown in the chooser bubble UI for 214 // Get a list of devices that can be shown in the chooser bubble UI for
208 // user to grant permsssion. 215 // user to grant permsssion.
209 void UsbChooserController::GotUsbDeviceList( 216 void UsbChooserController::GotUsbDeviceList(
210 const std::vector<scoped_refptr<device::UsbDevice>>& devices) { 217 const std::vector<scoped_refptr<UsbDevice>>& devices) {
211 for (const auto& device : devices) { 218 for (const auto& device : devices) {
212 if (DisplayDevice(device)) { 219 if (DisplayDevice(device)) {
213 base::string16 device_name = GetDeviceName(device); 220 base::string16 device_name = GetDeviceName(device);
214 devices_.push_back(std::make_pair(device, device_name)); 221 devices_.push_back(std::make_pair(device, device_name));
215 ++device_name_map_[device_name]; 222 ++device_name_map_[device_name];
216 } 223 }
217 } 224 }
218 if (view()) 225 if (view())
219 view()->OnOptionsInitialized(); 226 view()->OnOptionsInitialized();
220 } 227 }
221 228
222 bool UsbChooserController::DisplayDevice( 229 bool UsbChooserController::DisplayDevice(
223 scoped_refptr<device::UsbDevice> device) const { 230 scoped_refptr<UsbDevice> device) const {
224 if (!device::UsbDeviceFilter::MatchesAny(device, filters_)) 231 if (!UsbDeviceFilter::MatchesAny(device, filters_))
225 return false; 232 return false;
226 233
227 if (UsbBlocklist::Get().IsExcluded(device)) 234 if (UsbBlocklist::Get().IsExcluded(device))
228 return false; 235 return false;
229 236
230 // Embedded frames must have their origin in the list provided by the device. 237 // Embedded frames must have their origin in the list provided by the device.
231 RenderFrameHost* main_frame = 238 if (is_embedded_frame_) {
232 WebContents::FromRenderFrameHost(render_frame_host_)->GetMainFrame(); 239 return device::FindInWebUsbAllowedOrigins(device->webusb_allowed_origins(),
233 if (render_frame_host_ != main_frame) { 240 requesting_origin_, base::nullopt,
234 return device::FindInWebUsbAllowedOrigins( 241 base::nullopt);
235 device->webusb_allowed_origins(),
236 render_frame_host_->GetLastCommittedURL().GetOrigin());
237 } 242 }
238 243
239 return true; 244 return true;
240 } 245 }
OLDNEW
« no previous file with comments | « chrome/browser/usb/usb_chooser_controller.h ('k') | chrome/browser/usb/web_usb_permission_provider.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698