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_bubble_delegate.h" | 5 #include "chrome/browser/usb/usb_chooser_bubble_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" |
11 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/usb/usb_chooser_context.h" | 14 #include "chrome/browser/usb/usb_chooser_context.h" |
15 #include "chrome/browser/usb/usb_chooser_context_factory.h" | 15 #include "chrome/browser/usb/usb_chooser_context_factory.h" |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 if (ContainsValue(function.origins, origin)) | 66 if (ContainsValue(function.origins, origin)) |
67 return true; | 67 return true; |
68 } | 68 } |
69 } | 69 } |
70 | 70 |
71 return false; | 71 return false; |
72 } | 72 } |
73 | 73 |
74 } // namespace | 74 } // namespace |
75 | 75 |
76 UsbChooserBubbleDelegate::UsbChooserBubbleDelegate( | 76 UsbChooserBubbleController::UsbChooserBubbleController( |
77 content::RenderFrameHost* owner, | 77 content::RenderFrameHost* owner, |
78 mojo::Array<device::usb::DeviceFilterPtr> device_filters, | 78 mojo::Array<device::usb::DeviceFilterPtr> device_filters, |
79 content::RenderFrameHost* render_frame_host, | 79 content::RenderFrameHost* render_frame_host, |
80 const webusb::WebUsbPermissionBubble::GetPermissionCallback& callback) | 80 const webusb::WebUsbPermissionBubble::GetPermissionCallback& callback) |
81 : ChooserBubbleDelegate(owner), | 81 : ChooserBubbleController(owner), |
82 render_frame_host_(render_frame_host), | 82 render_frame_host_(render_frame_host), |
83 callback_(callback), | 83 callback_(callback), |
84 usb_service_observer_(this), | 84 usb_service_observer_(this), |
85 weak_factory_(this) { | 85 weak_factory_(this) { |
86 device::UsbService* usb_service = | 86 device::UsbService* usb_service = |
87 device::DeviceClient::Get()->GetUsbService(); | 87 device::DeviceClient::Get()->GetUsbService(); |
88 if (!usb_service) | 88 if (!usb_service) |
89 return; | 89 return; |
90 | 90 |
91 if (!usb_service_observer_.IsObserving(usb_service)) | 91 if (!usb_service_observer_.IsObserving(usb_service)) |
92 usb_service_observer_.Add(usb_service); | 92 usb_service_observer_.Add(usb_service); |
93 | 93 |
94 if (!device_filters.is_null()) | 94 if (!device_filters.is_null()) |
95 filters_ = device_filters.To<std::vector<device::UsbDeviceFilter>>(); | 95 filters_ = device_filters.To<std::vector<device::UsbDeviceFilter>>(); |
96 | 96 |
97 usb_service->GetDevices(base::Bind( | 97 usb_service->GetDevices( |
98 &UsbChooserBubbleDelegate::GotUsbDeviceList, weak_factory_.GetWeakPtr())); | 98 base::Bind(&UsbChooserBubbleController::GotUsbDeviceList, |
| 99 weak_factory_.GetWeakPtr())); |
99 } | 100 } |
100 | 101 |
101 UsbChooserBubbleDelegate::~UsbChooserBubbleDelegate() { | 102 UsbChooserBubbleController::~UsbChooserBubbleController() { |
102 if (!callback_.is_null()) | 103 if (!callback_.is_null()) |
103 callback_.Run(nullptr); | 104 callback_.Run(nullptr); |
104 } | 105 } |
105 | 106 |
106 size_t UsbChooserBubbleDelegate::NumOptions() const { | 107 size_t UsbChooserBubbleController::NumOptions() const { |
107 return devices_.size(); | 108 return devices_.size(); |
108 } | 109 } |
109 | 110 |
110 const base::string16& UsbChooserBubbleDelegate::GetOption(size_t index) const { | 111 const base::string16& UsbChooserBubbleController::GetOption( |
| 112 size_t index) const { |
111 DCHECK_LT(index, devices_.size()); | 113 DCHECK_LT(index, devices_.size()); |
112 return devices_[index].second; | 114 return devices_[index].second; |
113 } | 115 } |
114 | 116 |
115 void UsbChooserBubbleDelegate::Select(size_t index) { | 117 void UsbChooserBubbleController::Select(size_t index) { |
116 DCHECK_LT(index, devices_.size()); | 118 DCHECK_LT(index, devices_.size()); |
117 content::WebContents* web_contents = | 119 content::WebContents* web_contents = |
118 content::WebContents::FromRenderFrameHost(render_frame_host_); | 120 content::WebContents::FromRenderFrameHost(render_frame_host_); |
119 GURL embedding_origin = | 121 GURL embedding_origin = |
120 web_contents->GetMainFrame()->GetLastCommittedURL().GetOrigin(); | 122 web_contents->GetMainFrame()->GetLastCommittedURL().GetOrigin(); |
121 Profile* profile = | 123 Profile* profile = |
122 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 124 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
123 UsbChooserContext* chooser_context = | 125 UsbChooserContext* chooser_context = |
124 UsbChooserContextFactory::GetForProfile(profile); | 126 UsbChooserContextFactory::GetForProfile(profile); |
125 chooser_context->GrantDevicePermission( | 127 chooser_context->GrantDevicePermission( |
126 render_frame_host_->GetLastCommittedURL().GetOrigin(), embedding_origin, | 128 render_frame_host_->GetLastCommittedURL().GetOrigin(), embedding_origin, |
127 devices_[index].first->guid()); | 129 devices_[index].first->guid()); |
128 | 130 |
129 device::usb::DeviceInfoPtr device_info_ptr = | 131 device::usb::DeviceInfoPtr device_info_ptr = |
130 device::usb::DeviceInfo::From(*devices_[index].first); | 132 device::usb::DeviceInfo::From(*devices_[index].first); |
131 callback_.Run(std::move(device_info_ptr)); | 133 callback_.Run(std::move(device_info_ptr)); |
132 callback_.reset(); // Reset |callback_| so that it is only run once. | 134 callback_.reset(); // Reset |callback_| so that it is only run once. |
133 | 135 |
134 RecordChooserClosure(devices_[index].first->serial_number().empty() | 136 RecordChooserClosure(devices_[index].first->serial_number().empty() |
135 ? WEBUSB_CHOOSER_CLOSED_EPHEMERAL_PERMISSION_GRANTED | 137 ? WEBUSB_CHOOSER_CLOSED_EPHEMERAL_PERMISSION_GRANTED |
136 : WEBUSB_CHOOSER_CLOSED_PERMISSION_GRANTED); | 138 : WEBUSB_CHOOSER_CLOSED_PERMISSION_GRANTED); |
137 | 139 |
138 if (bubble_controller_) | 140 if (bubble_reference_) |
139 bubble_controller_->CloseBubble(BUBBLE_CLOSE_ACCEPTED); | 141 bubble_reference_->CloseBubble(BUBBLE_CLOSE_ACCEPTED); |
140 } | 142 } |
141 | 143 |
142 void UsbChooserBubbleDelegate::Cancel() { | 144 void UsbChooserBubbleController::Cancel() { |
143 RecordChooserClosure(devices_.size() == 0 | 145 RecordChooserClosure(devices_.size() == 0 |
144 ? WEBUSB_CHOOSER_CLOSED_CANCELLED_NO_DEVICES | 146 ? WEBUSB_CHOOSER_CLOSED_CANCELLED_NO_DEVICES |
145 : WEBUSB_CHOOSER_CLOSED_CANCELLED); | 147 : WEBUSB_CHOOSER_CLOSED_CANCELLED); |
146 | 148 |
147 if (bubble_controller_) | 149 if (bubble_reference_) |
148 bubble_controller_->CloseBubble(BUBBLE_CLOSE_CANCELED); | 150 bubble_reference_->CloseBubble(BUBBLE_CLOSE_CANCELED); |
149 } | 151 } |
150 | 152 |
151 void UsbChooserBubbleDelegate::Close() {} | 153 void UsbChooserBubbleController::Close() {} |
152 | 154 |
153 void UsbChooserBubbleDelegate::OnDeviceAdded( | 155 void UsbChooserBubbleController::OnDeviceAdded( |
154 scoped_refptr<device::UsbDevice> device) { | 156 scoped_refptr<device::UsbDevice> device) { |
155 if (device::UsbDeviceFilter::MatchesAny(device, filters_) && | 157 if (device::UsbDeviceFilter::MatchesAny(device, filters_) && |
156 FindInAllowedOrigins( | 158 FindInAllowedOrigins( |
157 device->webusb_allowed_origins(), | 159 device->webusb_allowed_origins(), |
158 render_frame_host_->GetLastCommittedURL().GetOrigin())) { | 160 render_frame_host_->GetLastCommittedURL().GetOrigin())) { |
159 devices_.push_back(std::make_pair(device, device->product_string())); | 161 devices_.push_back(std::make_pair(device, device->product_string())); |
160 if (observer()) | 162 if (observer()) |
161 observer()->OnOptionAdded(devices_.size() - 1); | 163 observer()->OnOptionAdded(devices_.size() - 1); |
162 } | 164 } |
163 } | 165 } |
164 | 166 |
165 GURL UsbChooserBubbleDelegate::GetHelpCenterUrl() const { | 167 GURL UsbChooserBubbleController::GetHelpCenterUrl() const { |
166 return GURL(chrome::kChooserUsbOverviewURL); | 168 return GURL(chrome::kChooserUsbOverviewURL); |
167 } | 169 } |
168 | 170 |
169 void UsbChooserBubbleDelegate::OnDeviceRemoved( | 171 void UsbChooserBubbleController::OnDeviceRemoved( |
170 scoped_refptr<device::UsbDevice> device) { | 172 scoped_refptr<device::UsbDevice> device) { |
171 for (auto it = devices_.begin(); it != devices_.end(); ++it) { | 173 for (auto it = devices_.begin(); it != devices_.end(); ++it) { |
172 if (it->first == device) { | 174 if (it->first == device) { |
173 size_t index = it - devices_.begin(); | 175 size_t index = it - devices_.begin(); |
174 devices_.erase(it); | 176 devices_.erase(it); |
175 if (observer()) | 177 if (observer()) |
176 observer()->OnOptionRemoved(index); | 178 observer()->OnOptionRemoved(index); |
177 return; | 179 return; |
178 } | 180 } |
179 } | 181 } |
180 } | 182 } |
181 | 183 |
182 // Get a list of devices that can be shown in the chooser bubble UI for | 184 // Get a list of devices that can be shown in the chooser bubble UI for |
183 // user to grant permsssion. | 185 // user to grant permsssion. |
184 void UsbChooserBubbleDelegate::GotUsbDeviceList( | 186 void UsbChooserBubbleController::GotUsbDeviceList( |
185 const std::vector<scoped_refptr<device::UsbDevice>>& devices) { | 187 const std::vector<scoped_refptr<device::UsbDevice>>& devices) { |
186 for (const auto& device : devices) { | 188 for (const auto& device : devices) { |
187 if (device::UsbDeviceFilter::MatchesAny(device, filters_) && | 189 if (device::UsbDeviceFilter::MatchesAny(device, filters_) && |
188 FindInAllowedOrigins( | 190 FindInAllowedOrigins( |
189 device->webusb_allowed_origins(), | 191 device->webusb_allowed_origins(), |
190 render_frame_host_->GetLastCommittedURL().GetOrigin())) { | 192 render_frame_host_->GetLastCommittedURL().GetOrigin())) { |
191 devices_.push_back(std::make_pair(device, device->product_string())); | 193 devices_.push_back(std::make_pair(device, device->product_string())); |
192 } | 194 } |
193 } | 195 } |
194 if (observer()) | 196 if (observer()) |
195 observer()->OnOptionsInitialized(); | 197 observer()->OnOptionsInitialized(); |
196 } | 198 } |
197 | 199 |
198 void UsbChooserBubbleDelegate::set_bubble_controller( | 200 void UsbChooserBubbleController::set_bubble_reference( |
199 BubbleReference bubble_controller) { | 201 BubbleReference bubble_reference) { |
200 bubble_controller_ = bubble_controller; | 202 bubble_reference_ = bubble_reference; |
201 } | 203 } |
OLD | NEW |