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/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::PermissionType type, | |
| 39 content::WebContents* web_contents, | |
| 40 const GURL& requesting_frame, | |
| 41 bool user_gesture, | |
| 42 Profile* profile, | |
| 43 const PermissionSetCallback& callback) { | |
| 44 DCHECK(web_contents); | |
| 45 | |
| 46 std::unique_ptr<PermissionInfoBarDelegate> infobar_delegate = nullptr; | |
| 47 switch (type) { | |
| 48 case content::PermissionType::GEOLOCATION: | |
| 49 infobar_delegate.reset(new GeolocationInfoBarDelegateAndroid( | |
| 50 requesting_frame, user_gesture, profile, callback)); | |
| 51 break; | |
| 52 #if defined(ENABLE_NOTIFICATIONS) | |
| 53 case content::PermissionType::NOTIFICATIONS: | |
| 54 case content::PermissionType::PUSH_MESSAGING: | |
| 55 infobar_delegate.reset(new NotificationPermissionInfoBarDelegate( | |
| 56 requesting_frame, user_gesture, profile, callback)); | |
| 57 break; | |
| 58 #endif // ENABLE_NOTIFICATIONS | |
| 59 case content::PermissionType::MIDI_SYSEX: | |
| 60 infobar_delegate.reset(new MidiPermissionInfoBarDelegateAndroid( | |
| 61 requesting_frame, user_gesture, profile, callback)); | |
| 62 break; | |
| 63 case content::PermissionType::PROTECTED_MEDIA_IDENTIFIER: | |
| 64 infobar_delegate.reset(new ProtectedMediaIdentifierInfoBarDelegateAndroid( | |
| 65 requesting_frame, user_gesture, profile, callback)); | |
| 66 break; | |
|
raymes
2016/11/01 02:56:46
Could we factor this switch statement out as out a
dominickn
2016/11/01 03:30:58
I thought about it when I was implementing it, and
raymes
2016/11/01 03:50:46
Could we just have this switch statement as the co
dominickn
2016/11/01 04:44:00
Ah, I see. That makes sense, thanks!
| |
| 67 default: | |
| 68 NOTREACHED(); | |
| 69 } | |
| 70 | |
| 71 PermissionDialogDelegate* dialog_delegate = | |
| 72 new PermissionDialogDelegate(web_contents, std::move(infobar_delegate)); | |
| 73 | |
| 74 // Create the dialog delegate's Java counterpart, which manages its lifetime. | |
| 75 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 76 base::android::ScopedJavaLocalRef<jobject> j_delegate = | |
| 77 dialog_delegate->CreateJavaDelegate(env); | |
| 78 | |
| 79 // Send the Java delegate to the Java PermissionDialogController for display. | |
| 80 // The controller takes over lifetime management; when the Java delegate is no | |
| 81 // longer needed it will in turn free the native delegate. | |
| 82 Java_PermissionDialogController_createDialog(env, j_delegate.obj()); | |
| 83 } | |
| 84 | |
| 85 // static | |
| 86 bool PermissionDialogDelegate::PromptUser(bool has_user_gesture) { | |
|
raymes
2016/11/01 02:56:46
As discussed, this might be better as ShouldUseMod
dominickn
2016/11/01 03:30:58
Done.
| |
| 87 if (!base::FeatureList::IsEnabled(features::kModalPermissionPrompts)) | |
| 88 return false; | |
| 89 | |
| 90 // Enforce a user gesture requirement by default. Only relax this if | |
|
raymes
2016/11/01 02:56:46
"Enforce a user gesture requirement by default" is
dominickn
2016/11/01 04:44:00
Done.
| |
| 91 // variations has a require_gesture key, and that key is one of 0 or false. | |
| 92 std::string require_gesture = variations::GetVariationParamValueByFeature( | |
| 93 features::kModalPermissionPrompts, kModalParamsUserGestureKey); | |
| 94 if (require_gesture == "0" || require_gesture == "false") | |
|
raymes
2016/11/01 02:56:46
How come "0" and "false" are both special? Could w
dominickn
2016/11/01 03:30:58
Done.
| |
| 95 return true; | |
| 96 return has_user_gesture; | |
|
raymes
2016/11/01 02:56:46
Hmm, this means that you could get an infobar or a
dominickn
2016/11/01 03:30:58
The UI team actually told us that the original int
raymes
2016/11/01 03:50:46
I see - that still doesn't seem like a great exper
| |
| 97 } | |
| 98 | |
| 99 // static | |
| 100 bool PermissionDialogDelegate::RegisterPermissionDialogDelegate(JNIEnv* env) { | |
| 101 return RegisterNativesImpl(env); | |
| 102 } | |
| 103 | |
| 104 ScopedJavaLocalRef<jobject> PermissionDialogDelegate::CreateJavaDelegate( | |
| 105 JNIEnv* env) { | |
| 106 content::ContentViewCore* cvc = | |
| 107 content::ContentViewCore::FromWebContents(web_contents()); | |
| 108 DCHECK(cvc); | |
| 109 | |
| 110 return Java_PermissionDialogDelegate_create( | |
| 111 env, reinterpret_cast<uintptr_t>(this), | |
| 112 cvc->GetWindowAndroid()->GetJavaObject(), | |
| 113 ResourceMapper::MapFromChromiumId(infobar_delegate_->GetIconId()), | |
| 114 ConvertUTF16ToJavaString(env, infobar_delegate_->GetMessageText()), | |
| 115 ConvertUTF16ToJavaString(env, infobar_delegate_->GetLinkText()), | |
| 116 ConvertUTF16ToJavaString(env, infobar_delegate_->GetButtonLabel( | |
| 117 PermissionInfoBarDelegate::BUTTON_OK)), | |
| 118 ConvertUTF16ToJavaString(env, | |
| 119 infobar_delegate_->GetButtonLabel( | |
| 120 PermissionInfoBarDelegate::BUTTON_CANCEL)), | |
| 121 infobar_delegate_->ShouldShowPersistenceToggle()); | |
| 122 } | |
| 123 | |
| 124 void PermissionDialogDelegate::Accept(JNIEnv* env, | |
| 125 const JavaParamRef<jobject>& obj, | |
| 126 jboolean persist) { | |
| 127 if (infobar_delegate_->ShouldShowPersistenceToggle()) | |
| 128 infobar_delegate_->set_persist(persist); | |
| 129 infobar_delegate_->Accept(); | |
| 130 } | |
| 131 | |
| 132 void PermissionDialogDelegate::Cancel(JNIEnv* env, | |
| 133 const JavaParamRef<jobject>& obj, | |
| 134 jboolean persist) { | |
| 135 if (infobar_delegate_->ShouldShowPersistenceToggle()) | |
| 136 infobar_delegate_->set_persist(persist); | |
| 137 infobar_delegate_->Cancel(); | |
| 138 } | |
| 139 | |
| 140 void PermissionDialogDelegate::Dismissed(JNIEnv* env, | |
| 141 const JavaParamRef<jobject>& obj) { | |
| 142 infobar_delegate_->InfoBarDismissed(); | |
| 143 } | |
| 144 | |
| 145 void PermissionDialogDelegate::LinkClicked(JNIEnv* env, | |
| 146 const JavaParamRef<jobject>& obj) { | |
| 147 // Don't call delegate_->LinkClicked() because that relies on having an | |
| 148 // InfoBarService as an owner() to open the link. That will fail since the | |
| 149 // wrapped delegate has no owner (it hasn't been added as an infobar). | |
| 150 if (web_contents()) { | |
| 151 web_contents()->OpenURL(content::OpenURLParams( | |
| 152 infobar_delegate_->GetLinkURL(), content::Referrer(), | |
| 153 WindowOpenDisposition::NEW_FOREGROUND_TAB, ui::PAGE_TRANSITION_LINK, | |
| 154 false)); | |
| 155 } | |
| 156 } | |
| 157 | |
| 158 void PermissionDialogDelegate::Destroy(JNIEnv* env, | |
| 159 const JavaParamRef<jobject>& obj) { | |
| 160 delete this; | |
| 161 } | |
| 162 | |
| 163 PermissionDialogDelegate::PermissionDialogDelegate( | |
| 164 content::WebContents* web_contents, | |
| 165 std::unique_ptr<PermissionInfoBarDelegate> infobar_delegate) | |
| 166 : content::WebContentsObserver(web_contents), | |
| 167 infobar_delegate_(std::move(infobar_delegate)) { | |
| 168 DCHECK(infobar_delegate_); | |
| 169 } | |
| 170 | |
| 171 PermissionDialogDelegate::~PermissionDialogDelegate() {} | |
| OLD | NEW |