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 <iterator> | |
8 #include <utility> | 9 #include <utility> |
9 | 10 |
10 #include "base/bind.h" | 11 #include "base/bind.h" |
11 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
12 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
13 #include "chrome/browser/usb/usb_chooser_context.h" | 14 #include "chrome/browser/usb/usb_chooser_context.h" |
14 #include "chrome/browser/usb/usb_chooser_context_factory.h" | 15 #include "chrome/browser/usb/usb_chooser_context_factory.h" |
15 #include "components/bubble/bubble_controller.h" | 16 #include "components/bubble/bubble_controller.h" |
16 #include "content/public/browser/render_frame_host.h" | 17 #include "content/public/browser/render_frame_host.h" |
17 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
70 | 71 |
71 usb_service->GetDevices(base::Bind( | 72 usb_service->GetDevices(base::Bind( |
72 &UsbChooserBubbleDelegate::GotUsbDeviceList, weak_factory_.GetWeakPtr())); | 73 &UsbChooserBubbleDelegate::GotUsbDeviceList, weak_factory_.GetWeakPtr())); |
73 } | 74 } |
74 | 75 |
75 UsbChooserBubbleDelegate::~UsbChooserBubbleDelegate() { | 76 UsbChooserBubbleDelegate::~UsbChooserBubbleDelegate() { |
76 if (!callback_.is_null()) | 77 if (!callback_.is_null()) |
77 callback_.Run(nullptr); | 78 callback_.Run(nullptr); |
78 } | 79 } |
79 | 80 |
80 const std::vector<base::string16>& UsbChooserBubbleDelegate::GetOptions() | 81 size_t UsbChooserBubbleDelegate::NumOptions() const { |
81 const { | 82 return devices_.size(); |
82 return devices_names_; | |
83 } | 83 } |
84 | 84 |
85 void UsbChooserBubbleDelegate::Select(int index) { | 85 const base::string16& UsbChooserBubbleDelegate::GetOption(size_t index) const { |
86 size_t idx = static_cast<size_t>(index); | 86 DCHECK_LT(index, devices_.size()); |
87 size_t num_options = devices_.size(); | 87 return devices_[index].second; |
88 DCHECK_LT(idx, num_options); | 88 } |
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 | 89 |
102 device::usb::DeviceInfoPtr device_info_ptr = | 90 void UsbChooserBubbleDelegate::Select(size_t index) { |
103 device::usb::DeviceInfo::From(*devices_[idx]); | 91 DCHECK_LT(index, devices_.size()); |
104 callback_.Run(std::move(device_info_ptr)); | 92 content::WebContents* web_contents = |
105 } else { | 93 content::WebContents::FromRenderFrameHost(render_frame_host_); |
106 callback_.Run(nullptr); | 94 GURL embedding_origin = |
107 } | 95 web_contents->GetMainFrame()->GetLastCommittedURL().GetOrigin(); |
96 Profile* profile = | |
97 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | |
98 UsbChooserContext* chooser_context = | |
99 UsbChooserContextFactory::GetForProfile(profile); | |
100 chooser_context->GrantDevicePermission( | |
101 render_frame_host_->GetLastCommittedURL().GetOrigin(), embedding_origin, | |
102 devices_[index].first->guid()); | |
103 | |
104 device::usb::DeviceInfoPtr device_info_ptr = | |
105 device::usb::DeviceInfo::From(*devices_[index].first); | |
106 callback_.Run(std::move(device_info_ptr)); | |
108 callback_.reset(); // Reset |callback_| so that it is only run once. | 107 callback_.reset(); // Reset |callback_| so that it is only run once. |
109 | 108 |
110 if (bubble_controller_) | 109 if (bubble_controller_) |
111 bubble_controller_->CloseBubble(BUBBLE_CLOSE_ACCEPTED); | 110 bubble_controller_->CloseBubble(BUBBLE_CLOSE_ACCEPTED); |
112 } | 111 } |
113 | 112 |
114 void UsbChooserBubbleDelegate::Cancel() { | 113 void UsbChooserBubbleDelegate::Cancel() { |
115 if (bubble_controller_) | 114 if (bubble_controller_) |
116 bubble_controller_->CloseBubble(BUBBLE_CLOSE_CANCELED); | 115 bubble_controller_->CloseBubble(BUBBLE_CLOSE_CANCELED); |
117 } | 116 } |
118 | 117 |
119 void UsbChooserBubbleDelegate::Close() {} | 118 void UsbChooserBubbleDelegate::Close() {} |
120 | 119 |
121 void UsbChooserBubbleDelegate::OnDeviceAdded( | 120 void UsbChooserBubbleDelegate::OnDeviceAdded( |
122 scoped_refptr<device::UsbDevice> device) { | 121 scoped_refptr<device::UsbDevice> device) { |
123 DCHECK(!ContainsValue(devices_, device)); | |
124 if (device::UsbDeviceFilter::MatchesAny(device, filters_) && | 122 if (device::UsbDeviceFilter::MatchesAny(device, filters_) && |
125 FindOriginInDescriptorSet( | 123 FindOriginInDescriptorSet( |
126 device->webusb_allowed_origins(), | 124 device->webusb_allowed_origins(), |
127 render_frame_host_->GetLastCommittedURL().GetOrigin())) { | 125 render_frame_host_->GetLastCommittedURL().GetOrigin())) { |
128 devices_.push_back(device); | 126 devices_.push_back(std::make_pair(device, device->product_string())); |
129 devices_names_.push_back(device->product_string()); | |
130 if (observer()) | 127 if (observer()) |
131 observer()->OnOptionAdded(static_cast<int>(devices_names_.size()) - 1); | 128 observer()->OnOptionAdded(devices_.size() - 1); |
132 } | 129 } |
133 } | 130 } |
134 | 131 |
135 void UsbChooserBubbleDelegate::OnDeviceRemoved( | 132 void UsbChooserBubbleDelegate::OnDeviceRemoved( |
136 scoped_refptr<device::UsbDevice> device) { | 133 scoped_refptr<device::UsbDevice> device) { |
Peter Kasting
2016/01/04 21:02:52
Same comments in this function as in the other fil
juncai
2016/01/04 22:44:47
Done.
| |
137 auto iter = std::find(devices_.begin(), devices_.end(), device); | 134 for (auto it = devices_.begin(), end = devices_.end(); it != end; ++it) { |
138 if (iter != devices_.end()) { | 135 if (it->first == device) { |
139 int index = iter - devices_.begin(); | 136 size_t index = std::distance(devices_.begin(), it); |
140 devices_.erase(iter); | 137 devices_.erase(it); |
141 devices_names_.erase(devices_names_.begin() + index); | 138 if (observer()) |
142 if (observer()) | 139 observer()->OnOptionRemoved(index); |
143 observer()->OnOptionRemoved(index); | 140 break; |
141 } | |
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 |