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_delegate.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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
70 | 70 |
71 usb_service->GetDevices(base::Bind( | 71 usb_service->GetDevices(base::Bind( |
72 &UsbChooserBubbleDelegate::GotUsbDeviceList, weak_factory_.GetWeakPtr())); | 72 &UsbChooserBubbleDelegate::GotUsbDeviceList, weak_factory_.GetWeakPtr())); |
73 } | 73 } |
74 | 74 |
75 UsbChooserBubbleDelegate::~UsbChooserBubbleDelegate() { | 75 UsbChooserBubbleDelegate::~UsbChooserBubbleDelegate() { |
76 if (!callback_.is_null()) | 76 if (!callback_.is_null()) |
77 callback_.Run(nullptr); | 77 callback_.Run(nullptr); |
78 } | 78 } |
79 | 79 |
80 const std::vector<base::string16>& UsbChooserBubbleDelegate::GetOptions() | 80 size_t UsbChooserBubbleDelegate::NumOptions() const { |
81 const { | 81 return devices_.size(); |
82 return devices_names_; | |
83 } | 82 } |
84 | 83 |
85 void UsbChooserBubbleDelegate::Select(int index) { | 84 const base::string16& UsbChooserBubbleDelegate::GetOption(size_t index) const { |
86 size_t idx = static_cast<size_t>(index); | 85 DCHECK_LT(index, devices_.size()); |
87 size_t num_options = devices_.size(); | 86 return devices_[index].second; |
88 DCHECK_LT(idx, num_options); | 87 } |
89 if (idx < num_options) { | |
90 content::WebContents* web_contents = | |
91 content::WebContents::FromRenderFrameHost(render_frame_host_); | |
92 GURL embedding_origin = | |
93 web_contents->GetMainFrame()->GetLastCommittedURL().GetOrigin(); | |
94 Profile* profile = | |
95 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | |
96 UsbChooserContext* chooser_context = | |
97 UsbChooserContextFactory::GetForProfile(profile); | |
98 chooser_context->GrantDevicePermission( | |
99 render_frame_host_->GetLastCommittedURL().GetOrigin(), embedding_origin, | |
100 devices_[idx]->guid()); | |
101 | 88 |
102 device::usb::DeviceInfoPtr device_info_ptr = | 89 void UsbChooserBubbleDelegate::Select(size_t index) { |
103 device::usb::DeviceInfo::From(*devices_[idx]); | 90 DCHECK_LT(index, devices_.size()); |
104 callback_.Run(std::move(device_info_ptr)); | 91 content::WebContents* web_contents = |
105 } else { | 92 content::WebContents::FromRenderFrameHost(render_frame_host_); |
106 callback_.Run(nullptr); | 93 GURL embedding_origin = |
107 } | 94 web_contents->GetMainFrame()->GetLastCommittedURL().GetOrigin(); |
95 Profile* profile = | |
96 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | |
97 UsbChooserContext* chooser_context = | |
98 UsbChooserContextFactory::GetForProfile(profile); | |
99 chooser_context->GrantDevicePermission( | |
100 render_frame_host_->GetLastCommittedURL().GetOrigin(), embedding_origin, | |
101 devices_[index].first->guid()); | |
102 | |
103 device::usb::DeviceInfoPtr device_info_ptr = | |
104 device::usb::DeviceInfo::From(*devices_[index].first); | |
105 callback_.Run(std::move(device_info_ptr)); | |
108 callback_.reset(); // Reset |callback_| so that it is only run once. | 106 callback_.reset(); // Reset |callback_| so that it is only run once. |
109 | 107 |
110 if (bubble_controller_) | 108 if (bubble_controller_) |
111 bubble_controller_->CloseBubble(BUBBLE_CLOSE_ACCEPTED); | 109 bubble_controller_->CloseBubble(BUBBLE_CLOSE_ACCEPTED); |
112 } | 110 } |
113 | 111 |
114 void UsbChooserBubbleDelegate::Cancel() { | 112 void UsbChooserBubbleDelegate::Cancel() { |
115 if (bubble_controller_) | 113 if (bubble_controller_) |
116 bubble_controller_->CloseBubble(BUBBLE_CLOSE_CANCELED); | 114 bubble_controller_->CloseBubble(BUBBLE_CLOSE_CANCELED); |
117 } | 115 } |
118 | 116 |
119 void UsbChooserBubbleDelegate::Close() {} | 117 void UsbChooserBubbleDelegate::Close() {} |
120 | 118 |
121 void UsbChooserBubbleDelegate::OnDeviceAdded( | 119 void UsbChooserBubbleDelegate::OnDeviceAdded( |
122 scoped_refptr<device::UsbDevice> device) { | 120 scoped_refptr<device::UsbDevice> device) { |
123 DCHECK(!ContainsValue(devices_, device)); | |
124 if (device::UsbDeviceFilter::MatchesAny(device, filters_) && | 121 if (device::UsbDeviceFilter::MatchesAny(device, filters_) && |
125 FindOriginInDescriptorSet( | 122 FindOriginInDescriptorSet( |
126 device->webusb_allowed_origins(), | 123 device->webusb_allowed_origins(), |
127 render_frame_host_->GetLastCommittedURL().GetOrigin())) { | 124 render_frame_host_->GetLastCommittedURL().GetOrigin())) { |
128 devices_.push_back(device); | 125 devices_.push_back(std::make_pair(device, device->product_string())); |
129 devices_names_.push_back(device->product_string()); | |
130 if (observer()) | 126 if (observer()) |
131 observer()->OnOptionAdded(static_cast<int>(devices_names_.size()) - 1); | 127 observer()->OnOptionAdded(devices_.size() - 1); |
132 } | 128 } |
133 } | 129 } |
134 | 130 |
135 void UsbChooserBubbleDelegate::OnDeviceRemoved( | 131 void UsbChooserBubbleDelegate::OnDeviceRemoved( |
136 scoped_refptr<device::UsbDevice> device) { | 132 scoped_refptr<device::UsbDevice> device) { |
137 auto iter = std::find(devices_.begin(), devices_.end(), device); | 133 size_t index = 0; |
138 if (iter != devices_.end()) { | 134 for (const auto& item : devices_) { |
139 int index = iter - devices_.begin(); | 135 if (item.first == device) { |
140 devices_.erase(iter); | 136 devices_.erase(devices_.begin() + index); |
Reilly Grant (use Gerrit)
2016/01/04 19:00:25
Same comment here about just using an iterator for
juncai
2016/01/04 19:35:55
Done.
| |
141 devices_names_.erase(devices_names_.begin() + index); | 137 if (observer()) |
142 if (observer()) | 138 observer()->OnOptionRemoved(index); |
143 observer()->OnOptionRemoved(index); | 139 break; |
140 } | |
141 ++index; | |
144 } | 142 } |
145 } | 143 } |
146 | 144 |
147 // Get a list of devices that can be shown in the chooser bubble UI for | 145 // Get a list of devices that can be shown in the chooser bubble UI for |
148 // user to grant permsssion. | 146 // user to grant permsssion. |
149 void UsbChooserBubbleDelegate::GotUsbDeviceList( | 147 void UsbChooserBubbleDelegate::GotUsbDeviceList( |
150 const std::vector<scoped_refptr<device::UsbDevice>>& devices) { | 148 const std::vector<scoped_refptr<device::UsbDevice>>& devices) { |
151 for (const auto& device : devices) { | 149 for (const auto& device : devices) { |
152 if (device::UsbDeviceFilter::MatchesAny(device, filters_) && | 150 if (device::UsbDeviceFilter::MatchesAny(device, filters_) && |
153 FindOriginInDescriptorSet( | 151 FindOriginInDescriptorSet( |
154 device->webusb_allowed_origins(), | 152 device->webusb_allowed_origins(), |
155 render_frame_host_->GetLastCommittedURL().GetOrigin())) { | 153 render_frame_host_->GetLastCommittedURL().GetOrigin())) { |
156 devices_.push_back(device); | 154 devices_.push_back(std::make_pair(device, device->product_string())); |
157 devices_names_.push_back(device->product_string()); | |
158 } | 155 } |
159 } | 156 } |
160 if (observer()) | 157 if (observer()) |
161 observer()->OnOptionsInitialized(); | 158 observer()->OnOptionsInitialized(); |
162 } | 159 } |
163 | 160 |
164 void UsbChooserBubbleDelegate::set_bubble_controller( | 161 void UsbChooserBubbleDelegate::set_bubble_controller( |
165 BubbleReference bubble_controller) { | 162 BubbleReference bubble_controller) { |
166 bubble_controller_ = bubble_controller; | 163 bubble_controller_ = bubble_controller; |
167 } | 164 } |
OLD | NEW |