Index: chrome/browser/ui/android/usb_chooser_android.cc |
diff --git a/chrome/browser/ui/android/usb_chooser_android.cc b/chrome/browser/ui/android/usb_chooser_android.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f14954255ebf13825a8575f88074d563118ffbcb |
--- /dev/null |
+++ b/chrome/browser/ui/android/usb_chooser_android.cc |
@@ -0,0 +1,255 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/ui/android/usb_chooser_android.h" |
+ |
+#include <stddef.h> |
+ |
+#include "base/android/jni_android.h" |
+#include "base/android/jni_array.h" |
+#include "base/android/jni_string.h" |
+#include "base/bind.h" |
+#include "base/metrics/histogram_macros.h" |
+#include "base/stl_util.h" |
+#include "base/strings/utf_string_conversions.h" |
+#include "chrome/browser/profiles/profile.h" |
+#include "chrome/browser/ssl/chrome_security_state_model_client.h" |
+#include "chrome/browser/ui/android/view_android_helper.h" |
+#include "chrome/browser/usb/usb_chooser_context.h" |
+#include "chrome/browser/usb/usb_chooser_context_factory.h" |
+#include "chrome/common/url_constants.h" |
+#include "content/public/browser/android/content_view_core.h" |
+#include "content/public/browser/render_frame_host.h" |
+#include "content/public/browser/web_contents.h" |
+#include "device/core/device_client.h" |
+#include "device/usb/mojo/type_converters.h" |
+#include "device/usb/usb_device.h" |
+#include "device/usb/usb_device_filter.h" |
+#include "jni/UsbChooserDialog_jni.h" |
+#include "ui/android/window_android.h" |
+#include "url/gurl.h" |
+#include "url/origin.h" |
+ |
+namespace { |
+ |
+// Reasons the chooser may be closed. These are used in histograms so do not |
+// remove/reorder entries. Only add at the end just before |
+// WEBUSB_CHOOSER_CLOSED_MAX. Also remember to update the enum listing in |
+// tools/metrics/histograms/histograms.xml. |
+enum WebUsbChooserClosed { |
+ // The user cancelled the permission prompt without selecting a device. |
+ WEBUSB_CHOOSER_CLOSED_CANCELLED = 0, |
+ // The user probably cancelled the permission prompt without selecting a |
+ // device because there were no devices to select. |
+ WEBUSB_CHOOSER_CLOSED_CANCELLED_NO_DEVICES, |
+ // The user granted permission to access a device. |
+ WEBUSB_CHOOSER_CLOSED_PERMISSION_GRANTED, |
+ // The user granted permission to access a device but that permission will be |
+ // revoked when the device is disconnected. |
+ WEBUSB_CHOOSER_CLOSED_EPHEMERAL_PERMISSION_GRANTED, |
+ // Maximum value for the enum. |
+ WEBUSB_CHOOSER_CLOSED_MAX |
+}; |
+ |
+void RecordChooserClosure(WebUsbChooserClosed disposition) { |
+ UMA_HISTOGRAM_ENUMERATION("WebUsb.ChooserClosed", disposition, |
+ WEBUSB_CHOOSER_CLOSED_MAX); |
+} |
+ |
+// Check if the origin is allowed. |
+bool FindInAllowedOrigins(const device::WebUsbAllowedOrigins* allowed_origins, |
+ const GURL& origin) { |
+ if (!allowed_origins) |
+ return false; |
+ |
+ if (ContainsValue(allowed_origins->origins, origin)) |
+ return true; |
+ |
+ for (const auto& config : allowed_origins->configurations) { |
+ if (ContainsValue(config.origins, origin)) |
+ return true; |
+ |
+ for (const auto& function : config.functions) { |
+ if (ContainsValue(function.origins, origin)) |
+ return true; |
+ } |
+ } |
+ |
+ return false; |
+} |
+ |
+} // namespace |
Reilly Grant (use Gerrit)
2016/03/03 01:57:56
This is copy-pasted from usb_chooser_bubble_contro
juncai
2016/03/04 01:36:39
Done.
|
+ |
+UsbChooserAndroid::UsbChooserAndroid( |
+ mojo::Array<device::usb::DeviceFilterPtr> device_filters, |
+ content::RenderFrameHost* render_frame_host, |
+ const webusb::WebUsbPermissionBubble::GetPermissionCallback& callback) |
+ : render_frame_host_(render_frame_host), |
Reilly Grant (use Gerrit)
2016/03/03 01:57:55
Like the desktop version we should be careful to t
juncai
2016/03/04 01:36:39
Done.
|
+ web_contents_( |
+ content::WebContents::FromRenderFrameHost(render_frame_host)), |
+ callback_(callback), |
+ usb_service_observer_(this), |
+ weak_factory_(this) { |
+ device::UsbService* usb_service = |
+ device::DeviceClient::Get()->GetUsbService(); |
+ if (!usb_service) |
+ return; |
+ |
+ if (!usb_service_observer_.IsObserving(usb_service)) |
+ usb_service_observer_.Add(usb_service); |
+ |
+ if (!device_filters.is_null()) |
+ filters_ = device_filters.To<std::vector<device::UsbDeviceFilter>>(); |
+ |
+ // Create (and show) the UsbChooser dialog. |
+ base::android::ScopedJavaLocalRef<jobject> window_android = |
+ content::ContentViewCore::FromWebContents(web_contents_) |
+ ->GetWindowAndroid() |
+ ->GetJavaObject(); |
+ JNIEnv* env = base::android::AttachCurrentThread(); |
+ const url::Origin origin = render_frame_host->GetLastCommittedOrigin(); |
+ DCHECK(!origin.unique()); |
+ base::android::ScopedJavaLocalRef<jstring> origin_string = |
+ base::android::ConvertUTF8ToJavaString(env, origin.Serialize()); |
+ ChromeSecurityStateModelClient* security_model_client = |
+ ChromeSecurityStateModelClient::FromWebContents(web_contents_); |
+ DCHECK(security_model_client); |
+ java_dialog_.Reset(Java_UsbChooserDialog_create( |
+ env, window_android.obj(), origin_string.obj(), |
+ security_model_client->GetSecurityInfo().security_level, |
+ reinterpret_cast<intptr_t>(this))); |
+ |
+ usb_service->GetDevices(base::Bind(&UsbChooserAndroid::GotUsbDeviceList, |
+ weak_factory_.GetWeakPtr())); |
+} |
+ |
+UsbChooserAndroid::~UsbChooserAndroid() { |
+ if (!callback_.is_null()) |
+ callback_.Run(nullptr); |
+ |
+ if (!java_dialog_.is_null()) { |
+ Java_UsbChooserDialog_closeDialog(base::android::AttachCurrentThread(), |
+ java_dialog_.obj()); |
+ } |
+} |
+ |
+void UsbChooserAndroid::OnDeviceAdded(scoped_refptr<device::UsbDevice> device) { |
+ if (device::UsbDeviceFilter::MatchesAny(device, filters_) && |
+ FindInAllowedOrigins( |
+ device->webusb_allowed_origins(), |
+ render_frame_host_->GetLastCommittedURL().GetOrigin())) { |
+ devices_.push_back(device); |
+ device_guids_.push_back(device->guid()); |
+ device_names_.push_back(device->product_string()); |
+ UpdateChooserDialog(); |
+ } |
+} |
+ |
+void UsbChooserAndroid::OnDeviceRemoved( |
+ scoped_refptr<device::UsbDevice> device) { |
+ for (auto it = devices_.begin(); it != devices_.end(); ++it) { |
+ if (*it == device) { |
+ devices_.erase(it); |
+ size_t index = it - devices_.begin(); |
+ device_guids_.erase(device_guids_.begin() + index); |
+ device_names_.erase(device_names_.begin() + index); |
+ UpdateChooserDialog(); |
+ return; |
+ } |
+ } |
+} |
+ |
+void UsbChooserAndroid::Select(const std::string& guid) { |
+ for (size_t index = 0; index < device_guids_.size(); ++index) { |
+ if (device_guids_[index] == guid) { |
+ content::WebContents* web_contents = |
+ content::WebContents::FromRenderFrameHost(render_frame_host_); |
+ GURL embedding_origin = |
+ web_contents->GetMainFrame()->GetLastCommittedURL().GetOrigin(); |
+ Profile* profile = |
+ Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
+ UsbChooserContext* chooser_context = |
+ UsbChooserContextFactory::GetForProfile(profile); |
+ chooser_context->GrantDevicePermission( |
+ render_frame_host_->GetLastCommittedURL().GetOrigin(), |
+ embedding_origin, device_guids_[index]); |
+ device::usb::DeviceInfoPtr device_info_ptr = |
+ device::usb::DeviceInfo::From(*devices_[index]); |
+ callback_.Run(std::move(device_info_ptr)); |
+ callback_.reset(); // Reset |callback_| so that it is only run once. |
+ |
+ RecordChooserClosure( |
+ devices_[index]->serial_number().empty() |
+ ? WEBUSB_CHOOSER_CLOSED_EPHEMERAL_PERMISSION_GRANTED |
+ : WEBUSB_CHOOSER_CLOSED_PERMISSION_GRANTED); |
+ } |
+ } |
+} |
+ |
+void UsbChooserAndroid::Cancel() { |
+ RecordChooserClosure(devices_.size() == 0 |
+ ? WEBUSB_CHOOSER_CLOSED_CANCELLED_NO_DEVICES |
+ : WEBUSB_CHOOSER_CLOSED_CANCELLED); |
+} |
+ |
+// Get a list of devices that can be shown in the chooser bubble UI for |
+// user to grant permsssion. |
+void UsbChooserAndroid::GotUsbDeviceList( |
+ const std::vector<scoped_refptr<device::UsbDevice>>& devices) { |
+ for (const auto& device : devices) { |
+ if (device::UsbDeviceFilter::MatchesAny(device, filters_) && |
+ FindInAllowedOrigins( |
+ device->webusb_allowed_origins(), |
+ render_frame_host_->GetLastCommittedURL().GetOrigin())) { |
+ devices_.push_back(device); |
+ device_guids_.push_back(device->guid()); |
+ device_names_.push_back(device->product_string()); |
+ } |
+ } |
+ UpdateChooserDialog(); |
+} |
+ |
+void UsbChooserAndroid::OnDialogFinished( |
+ JNIEnv* env, |
+ const JavaParamRef<jobject>& obj, |
+ jint event_type, |
+ const JavaParamRef<jstring>& device_id) { |
+ // Values are defined in UsbChooserDialog as DIALOG_FINISHED constants. |
+ switch (event_type) { |
+ case 0: |
+ Cancel(); |
+ return; |
+ case 1: |
+ Select(base::android::ConvertJavaStringToUTF8(env, device_id)); |
+ return; |
+ } |
+ NOTREACHED(); |
+} |
+ |
+void UsbChooserAndroid::ShowUsbOverviewLink( |
+ JNIEnv* env, |
+ const base::android::JavaParamRef<jobject>& obj) { |
+ OpenUrl(chrome::kChooserUsbOverviewURL); |
+} |
+ |
+void UsbChooserAndroid::UpdateChooserDialog() const { |
+ JNIEnv* env = base::android::AttachCurrentThread(); |
+ base::android::ScopedJavaLocalRef<jobjectArray> guids = |
+ base::android::ToJavaArrayOfStrings(env, device_guids_); |
+ base::android::ScopedJavaLocalRef<jobjectArray> names = |
+ base::android::ToJavaArrayOfStrings(env, device_names_); |
+ Java_UsbChooserDialog_updateOptions(env, java_dialog_.obj(), guids.obj(), |
+ names.obj()); |
+} |
+ |
+void UsbChooserAndroid::OpenUrl(const std::string& url) { |
+ web_contents_->OpenURL(content::OpenURLParams( |
+ GURL(url), content::Referrer(), NEW_FOREGROUND_TAB, |
+ ui::PAGE_TRANSITION_AUTO_TOPLEVEL, false /* is_renderer_initiated */)); |
+} |
+ |
+// static |
+bool UsbChooserAndroid::Register(JNIEnv* env) { |
+ return RegisterNativesImpl(env); |
+} |