OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/ui/android/usb_chooser_android.h" | |
6 | |
7 #include <stddef.h> | |
8 | |
9 #include <algorithm> | |
10 | |
11 #include "base/android/jni_android.h" | |
12 #include "base/android/jni_array.h" | |
13 #include "base/android/jni_string.h" | |
14 #include "base/bind.h" | |
15 #include "base/strings/utf_string_conversions.h" | |
16 #include "chrome/browser/profiles/profile.h" | |
17 #include "chrome/browser/ssl/chrome_security_state_model_client.h" | |
18 #include "chrome/browser/ui/android/view_android_helper.h" | |
19 #include "chrome/browser/usb/usb_chooser_context.h" | |
20 #include "chrome/browser/usb/usb_chooser_context_factory.h" | |
21 #include "chrome/browser/usb/web_usb_histograms.h" | |
22 #include "chrome/common/url_constants.h" | |
23 #include "content/public/browser/android/content_view_core.h" | |
24 #include "content/public/browser/render_frame_host.h" | |
25 #include "content/public/browser/web_contents.h" | |
26 #include "device/core/device_client.h" | |
27 #include "device/usb/mojo/type_converters.h" | |
28 #include "device/usb/usb_device.h" | |
29 #include "device/usb/usb_device_filter.h" | |
30 #include "device/usb/webusb_descriptors.h" | |
31 #include "jni/UsbChooserDialog_jni.h" | |
32 #include "ui/android/window_android.h" | |
33 #include "url/gurl.h" | |
34 #include "url/origin.h" | |
35 | |
36 UsbChooserAndroid::UsbChooserAndroid( | |
37 mojo::Array<device::usb::DeviceFilterPtr> device_filters, | |
38 content::RenderFrameHost* render_frame_host, | |
39 const device::usb::ChooserService::GetPermissionCallback& callback) | |
40 : render_frame_host_(render_frame_host), | |
41 web_contents_( | |
Yaron
2016/03/07 21:51:49
Don't think it's worth holding on to this given yo
juncai
2016/03/09 01:26:51
Done.
| |
42 content::WebContents::FromRenderFrameHost(render_frame_host)), | |
43 callback_(callback), | |
44 usb_service_observer_(this), | |
45 weak_factory_(this) { | |
46 device::UsbService* usb_service = | |
47 device::DeviceClient::Get()->GetUsbService(); | |
48 if (!usb_service) | |
49 return; | |
50 | |
51 if (!usb_service_observer_.IsObserving(usb_service)) | |
52 usb_service_observer_.Add(usb_service); | |
53 | |
54 if (!device_filters.is_null()) | |
55 filters_ = device_filters.To<std::vector<device::UsbDeviceFilter>>(); | |
56 | |
57 // Create (and show) the UsbChooser dialog. | |
58 base::android::ScopedJavaLocalRef<jobject> window_android = | |
59 content::ContentViewCore::FromWebContents(web_contents_) | |
60 ->GetWindowAndroid() | |
61 ->GetJavaObject(); | |
62 JNIEnv* env = base::android::AttachCurrentThread(); | |
63 const url::Origin origin = render_frame_host->GetLastCommittedOrigin(); | |
64 DCHECK(!origin.unique()); | |
65 base::android::ScopedJavaLocalRef<jstring> origin_string = | |
Yaron
2016/03/07 21:51:49
I believe this should use url_formatter::FormatUrl
Finnur
2016/03/08 10:26:20
Then BluetoothChooserAndroid ctor needs a correspo
juncai
2016/03/09 01:26:51
Done.
juncai
2016/03/09 01:26:51
A patch was created to fix it at BluetoothChooserA
| |
66 base::android::ConvertUTF8ToJavaString(env, origin.Serialize()); | |
67 ChromeSecurityStateModelClient* security_model_client = | |
68 ChromeSecurityStateModelClient::FromWebContents(web_contents_); | |
69 DCHECK(security_model_client); | |
70 java_dialog_.Reset(Java_UsbChooserDialog_create( | |
71 env, window_android.obj(), origin_string.obj(), | |
72 security_model_client->GetSecurityInfo().security_level, | |
73 reinterpret_cast<intptr_t>(this))); | |
74 | |
75 usb_service->GetDevices(base::Bind(&UsbChooserAndroid::GotUsbDeviceList, | |
76 weak_factory_.GetWeakPtr())); | |
77 } | |
78 | |
79 UsbChooserAndroid::~UsbChooserAndroid() { | |
80 if (!callback_.is_null()) | |
81 callback_.Run(nullptr); | |
82 | |
83 if (!java_dialog_.is_null()) { | |
84 Java_UsbChooserDialog_closeDialog(base::android::AttachCurrentThread(), | |
85 java_dialog_.obj()); | |
86 } | |
87 } | |
88 | |
89 void UsbChooserAndroid::OnDeviceAdded(scoped_refptr<device::UsbDevice> device) { | |
90 if (device::UsbDeviceFilter::MatchesAny(device, filters_) && | |
91 FindInWebUsbAllowedOrigins( | |
92 device->webusb_allowed_origins(), | |
93 render_frame_host_->GetLastCommittedURL().GetOrigin())) { | |
94 devices_.push_back(device); | |
95 device_guids_.push_back(device->guid()); | |
96 device_names_.push_back(device->product_string()); | |
97 UpdateChooserDialog(); | |
98 } | |
99 } | |
100 | |
101 void UsbChooserAndroid::OnDeviceRemoved( | |
102 scoped_refptr<device::UsbDevice> device) { | |
103 auto it = std::find(devices_.begin(), devices_.end(), device); | |
104 if (it != devices_.end()) { | |
105 size_t index = it - devices_.begin(); | |
106 devices_.erase(it); | |
107 device_guids_.erase(device_guids_.begin() + index); | |
108 device_names_.erase(device_names_.begin() + index); | |
109 UpdateChooserDialog(); | |
110 } | |
111 } | |
112 | |
113 void UsbChooserAndroid::Select(const std::string& guid) { | |
114 auto it = std::find(device_guids_.begin(), device_guids_.end(), guid); | |
115 if (it != device_guids_.end()) { | |
116 size_t index = it - device_guids_.begin(); | |
117 content::WebContents* web_contents = | |
118 content::WebContents::FromRenderFrameHost(render_frame_host_); | |
119 GURL embedding_origin = | |
120 web_contents->GetMainFrame()->GetLastCommittedURL().GetOrigin(); | |
121 Profile* profile = | |
122 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | |
123 UsbChooserContext* chooser_context = | |
124 UsbChooserContextFactory::GetForProfile(profile); | |
125 chooser_context->GrantDevicePermission( | |
126 render_frame_host_->GetLastCommittedURL().GetOrigin(), embedding_origin, | |
127 *it); | |
128 device::usb::DeviceInfoPtr device_info_ptr = | |
129 device::usb::DeviceInfo::From(*devices_[index]); | |
130 callback_.Run(std::move(device_info_ptr)); | |
131 callback_.reset(); // Reset |callback_| so that it is only run once. | |
132 Java_UsbChooserDialog_closeDialog(base::android::AttachCurrentThread(), | |
133 java_dialog_.obj()); | |
134 | |
135 RecordWebUsbChooserClosure( | |
136 devices_[index]->serial_number().empty() | |
137 ? WEBUSB_CHOOSER_CLOSED_EPHEMERAL_PERMISSION_GRANTED | |
138 : WEBUSB_CHOOSER_CLOSED_PERMISSION_GRANTED); | |
139 } | |
140 } | |
141 | |
142 void UsbChooserAndroid::Cancel() { | |
143 callback_.Run(nullptr); | |
144 callback_.reset(); // Reset |callback_| so that it is only run once. | |
145 Java_UsbChooserDialog_closeDialog(base::android::AttachCurrentThread(), | |
146 java_dialog_.obj()); | |
147 | |
148 RecordWebUsbChooserClosure(devices_.size() == 0 | |
149 ? WEBUSB_CHOOSER_CLOSED_CANCELLED_NO_DEVICES | |
150 : WEBUSB_CHOOSER_CLOSED_CANCELLED); | |
151 } | |
152 | |
153 // Get a list of devices that can be shown in the chooser bubble UI for | |
154 // user to grant permsssion. | |
155 void UsbChooserAndroid::GotUsbDeviceList( | |
156 const std::vector<scoped_refptr<device::UsbDevice>>& devices) { | |
157 for (const auto& device : devices) { | |
158 if (device::UsbDeviceFilter::MatchesAny(device, filters_) && | |
159 FindInWebUsbAllowedOrigins( | |
160 device->webusb_allowed_origins(), | |
161 render_frame_host_->GetLastCommittedURL().GetOrigin())) { | |
162 devices_.push_back(device); | |
163 device_guids_.push_back(device->guid()); | |
164 device_names_.push_back(device->product_string()); | |
165 } | |
166 } | |
167 UpdateChooserDialog(); | |
168 } | |
169 | |
170 void UsbChooserAndroid::OnDialogFinished( | |
171 JNIEnv* env, | |
172 const JavaParamRef<jobject>& obj, | |
173 jint event_type, | |
174 const JavaParamRef<jstring>& device_id) { | |
175 // Values are defined in UsbChooserDialog as DIALOG_FINISHED constants. | |
176 switch (event_type) { | |
177 case 0: | |
178 Cancel(); | |
179 return; | |
180 case 1: | |
181 Select(base::android::ConvertJavaStringToUTF8(env, device_id)); | |
182 return; | |
183 } | |
184 NOTREACHED(); | |
185 } | |
186 | |
187 void UsbChooserAndroid::ShowUsbOverviewLink( | |
188 JNIEnv* env, | |
189 const base::android::JavaParamRef<jobject>& obj) { | |
190 OpenUrl(chrome::kChooserUsbOverviewURL); | |
191 } | |
192 | |
193 void UsbChooserAndroid::UpdateChooserDialog() const { | |
194 JNIEnv* env = base::android::AttachCurrentThread(); | |
195 base::android::ScopedJavaLocalRef<jobjectArray> guids = | |
196 base::android::ToJavaArrayOfStrings(env, device_guids_); | |
197 base::android::ScopedJavaLocalRef<jobjectArray> names = | |
198 base::android::ToJavaArrayOfStrings(env, device_names_); | |
199 Java_UsbChooserDialog_updateOptions(env, java_dialog_.obj(), guids.obj(), | |
200 names.obj()); | |
201 } | |
202 | |
203 void UsbChooserAndroid::OpenUrl(const std::string& url) { | |
204 web_contents_->OpenURL(content::OpenURLParams( | |
205 GURL(url), content::Referrer(), NEW_FOREGROUND_TAB, | |
206 ui::PAGE_TRANSITION_AUTO_TOPLEVEL, false /* is_renderer_initiated */)); | |
207 } | |
208 | |
209 // static | |
210 bool UsbChooserAndroid::Register(JNIEnv* env) { | |
211 return RegisterNativesImpl(env); | |
212 } | |
OLD | NEW |