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/permissions/permission_dialog_delegate.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/android/jni_android.h" |
| 10 #include "base/android/jni_string.h" |
| 11 #include "base/feature_list.h" |
| 12 #include "build/build_config.h" |
| 13 #include "chrome/browser/android/resource_mapper.h" |
| 14 #include "chrome/browser/geolocation/geolocation_infobar_delegate_android.h" |
| 15 #include "chrome/browser/media/midi_permission_infobar_delegate_android.h" |
| 16 #include "chrome/browser/media/protected_media_identifier_infobar_delegate_andro
id.h" |
| 17 #include "chrome/browser/notifications/notification_permission_infobar_delegate.
h" |
| 18 #include "chrome/common/chrome_features.h" |
| 19 #include "components/variations/variations_associated_data.h" |
| 20 #include "content/public/browser/android/content_view_core.h" |
| 21 #include "content/public/browser/web_contents.h" |
| 22 #include "jni/PermissionDialogController_jni.h" |
| 23 #include "jni/PermissionDialogDelegate_jni.h" |
| 24 #include "ui/android/window_android.h" |
| 25 #include "ui/base/window_open_disposition.h" |
| 26 |
| 27 using base::android::ConvertUTF16ToJavaString; |
| 28 |
| 29 namespace { |
| 30 |
| 31 // Key for querying variations for whether a modal should require a gesture. |
| 32 const char kModalParamsUserGestureKey[] = "require_gesture"; |
| 33 |
| 34 } |
| 35 |
| 36 // static |
| 37 void PermissionDialogDelegate::Create( |
| 38 content::WebContents* web_contents, |
| 39 content::PermissionType type, |
| 40 const GURL& requesting_frame, |
| 41 bool user_gesture, |
| 42 Profile* profile, |
| 43 const PermissionSetCallback& callback) { |
| 44 DCHECK(web_contents); |
| 45 |
| 46 // Dispatch the dialog to Java, which manages the lifetime of this object. |
| 47 new PermissionDialogDelegate( |
| 48 web_contents, |
| 49 PermissionInfoBarDelegate::CreateDelegate( |
| 50 type, requesting_frame, user_gesture, profile, callback)); |
| 51 } |
| 52 |
| 53 // static |
| 54 bool PermissionDialogDelegate::ShouldShowDialog(bool has_user_gesture) { |
| 55 if (!base::FeatureList::IsEnabled(features::kModalPermissionPrompts)) |
| 56 return false; |
| 57 |
| 58 // Only use modals when the prompt is triggered by a user gesture, unless the |
| 59 // kModalParamsUserGestureKey is set to false. |
| 60 std::string require_gesture = variations::GetVariationParamValueByFeature( |
| 61 features::kModalPermissionPrompts, kModalParamsUserGestureKey); |
| 62 if (require_gesture == "false") |
| 63 return true; |
| 64 return has_user_gesture; |
| 65 } |
| 66 |
| 67 // static |
| 68 bool PermissionDialogDelegate::RegisterPermissionDialogDelegate(JNIEnv* env) { |
| 69 return RegisterNativesImpl(env); |
| 70 } |
| 71 |
| 72 ScopedJavaLocalRef<jobject> PermissionDialogDelegate::CreateJavaDelegate( |
| 73 JNIEnv* env) { |
| 74 content::ContentViewCore* cvc = |
| 75 content::ContentViewCore::FromWebContents(web_contents()); |
| 76 DCHECK(cvc); |
| 77 |
| 78 return Java_PermissionDialogDelegate_create( |
| 79 env, reinterpret_cast<uintptr_t>(this), |
| 80 cvc->GetWindowAndroid()->GetJavaObject(), |
| 81 ResourceMapper::MapFromChromiumId(infobar_delegate_->GetIconId()), |
| 82 ConvertUTF16ToJavaString(env, infobar_delegate_->GetMessageText()), |
| 83 ConvertUTF16ToJavaString(env, infobar_delegate_->GetLinkText()), |
| 84 ConvertUTF16ToJavaString(env, infobar_delegate_->GetButtonLabel( |
| 85 PermissionInfoBarDelegate::BUTTON_OK)), |
| 86 ConvertUTF16ToJavaString(env, |
| 87 infobar_delegate_->GetButtonLabel( |
| 88 PermissionInfoBarDelegate::BUTTON_CANCEL)), |
| 89 infobar_delegate_->ShouldShowPersistenceToggle()); |
| 90 } |
| 91 |
| 92 void PermissionDialogDelegate::Accept(JNIEnv* env, |
| 93 const JavaParamRef<jobject>& obj, |
| 94 jboolean persist) { |
| 95 if (infobar_delegate_->ShouldShowPersistenceToggle()) |
| 96 infobar_delegate_->set_persist(persist); |
| 97 infobar_delegate_->Accept(); |
| 98 } |
| 99 |
| 100 void PermissionDialogDelegate::Cancel(JNIEnv* env, |
| 101 const JavaParamRef<jobject>& obj, |
| 102 jboolean persist) { |
| 103 if (infobar_delegate_->ShouldShowPersistenceToggle()) |
| 104 infobar_delegate_->set_persist(persist); |
| 105 infobar_delegate_->Cancel(); |
| 106 } |
| 107 |
| 108 void PermissionDialogDelegate::Dismissed(JNIEnv* env, |
| 109 const JavaParamRef<jobject>& obj) { |
| 110 infobar_delegate_->InfoBarDismissed(); |
| 111 } |
| 112 |
| 113 void PermissionDialogDelegate::LinkClicked(JNIEnv* env, |
| 114 const JavaParamRef<jobject>& obj) { |
| 115 // Don't call delegate_->LinkClicked() because that relies on having an |
| 116 // InfoBarService as an owner() to open the link. That will fail since the |
| 117 // wrapped delegate has no owner (it hasn't been added as an infobar). |
| 118 if (web_contents()) { |
| 119 web_contents()->OpenURL(content::OpenURLParams( |
| 120 infobar_delegate_->GetLinkURL(), content::Referrer(), |
| 121 WindowOpenDisposition::NEW_FOREGROUND_TAB, ui::PAGE_TRANSITION_LINK, |
| 122 false)); |
| 123 } |
| 124 } |
| 125 |
| 126 void PermissionDialogDelegate::Destroy(JNIEnv* env, |
| 127 const JavaParamRef<jobject>& obj) { |
| 128 delete this; |
| 129 } |
| 130 |
| 131 PermissionDialogDelegate::PermissionDialogDelegate( |
| 132 content::WebContents* web_contents, |
| 133 std::unique_ptr<PermissionInfoBarDelegate> infobar_delegate) |
| 134 : content::WebContentsObserver(web_contents), |
| 135 infobar_delegate_(std::move(infobar_delegate)) { |
| 136 DCHECK(infobar_delegate_); |
| 137 |
| 138 // Create our Java counterpart, which manages our lifetime. |
| 139 JNIEnv* env = base::android::AttachCurrentThread(); |
| 140 base::android::ScopedJavaLocalRef<jobject> j_delegate = |
| 141 CreateJavaDelegate(env); |
| 142 |
| 143 // Send the Java delegate to the Java PermissionDialogController for display. |
| 144 // The controller takes over lifetime management; when the Java delegate is no |
| 145 // longer needed it will in turn free the native delegate. |
| 146 Java_PermissionDialogController_createDialog(env, j_delegate.obj()); |
| 147 } |
| 148 |
| 149 PermissionDialogDelegate::~PermissionDialogDelegate() {} |
OLD | NEW |