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