| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/permissions/permission_dialog_delegate.h" | 5 #include "chrome/browser/permissions/permission_dialog_delegate.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 10 #include "base/android/jni_array.h" | 10 #include "base/android/jni_array.h" |
| 11 #include "base/android/jni_string.h" | 11 #include "base/android/jni_string.h" |
| 12 #include "base/feature_list.h" | 12 #include "base/feature_list.h" |
| 13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 14 #include "chrome/browser/android/resource_mapper.h" | 14 #include "chrome/browser/android/resource_mapper.h" |
| 15 #include "chrome/browser/android/tab_android.h" | 15 #include "chrome/browser/android/tab_android.h" |
| 16 #include "chrome/browser/geolocation/geolocation_infobar_delegate_android.h" | 16 #include "chrome/browser/geolocation/geolocation_infobar_delegate_android.h" |
| 17 #include "chrome/browser/media/midi_permission_infobar_delegate_android.h" | 17 #include "chrome/browser/media/midi_permission_infobar_delegate_android.h" |
| 18 #include "chrome/browser/media/protected_media_identifier_infobar_delegate_andro
id.h" | 18 #include "chrome/browser/media/protected_media_identifier_infobar_delegate_andro
id.h" |
| 19 #include "chrome/browser/media/webrtc/media_stream_infobar_delegate_android.h" | 19 #include "chrome/browser/media/webrtc/media_stream_infobar_delegate_android.h" |
| 20 #include "chrome/browser/notifications/notification_permission_infobar_delegate.
h" | 20 #include "chrome/browser/notifications/notification_permission_infobar_delegate.
h" |
| 21 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
| 22 #include "chrome/common/chrome_features.h" | 22 #include "chrome/common/chrome_features.h" |
| 23 #include "chrome/grit/generated_resources.h" |
| 23 #include "components/variations/variations_associated_data.h" | 24 #include "components/variations/variations_associated_data.h" |
| 24 #include "content/public/browser/navigation_handle.h" | 25 #include "content/public/browser/navigation_handle.h" |
| 25 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
| 26 #include "jni/PermissionDialogController_jni.h" | 27 #include "jni/PermissionDialogController_jni.h" |
| 27 #include "jni/PermissionDialogDelegate_jni.h" | 28 #include "jni/PermissionDialogDelegate_jni.h" |
| 28 #include "ui/android/window_android.h" | 29 #include "ui/android/window_android.h" |
| 30 #include "ui/base/l10n/l10n_util.h" |
| 29 #include "ui/base/window_open_disposition.h" | 31 #include "ui/base/window_open_disposition.h" |
| 30 | 32 |
| 31 using base::android::ConvertUTF16ToJavaString; | 33 using base::android::ConvertUTF16ToJavaString; |
| 32 | 34 |
| 33 namespace { | 35 namespace { |
| 34 | 36 |
| 35 // Key for querying variations for whether a modal should require a gesture. | 37 // Key for querying variations for whether a modal should require a gesture. |
| 36 const char kModalParamsUserGestureKey[] = "require_gesture"; | 38 const char kModalParamsUserGestureKey[] = "require_gesture"; |
| 37 | 39 |
| 38 } | 40 } |
| 39 | 41 |
| 40 // static | 42 // static |
| 41 void PermissionDialogDelegate::Create( | 43 void PermissionDialogDelegate::Create( |
| 42 content::WebContents* web_contents, | 44 content::WebContents* web_contents, |
| 43 ContentSettingsType type, | 45 PermissionPromptAndroid* permission_prompt) { |
| 44 const GURL& requesting_frame, | |
| 45 bool user_gesture, | |
| 46 Profile* profile, | |
| 47 const PermissionSetCallback& callback) { | |
| 48 DCHECK(web_contents); | 46 DCHECK(web_contents); |
| 49 | 47 |
| 50 // If we don't have a tab, just act as though the prompt was dismissed. | 48 // If we don't have a tab, just act as though the prompt was dismissed. |
| 49 TabAndroid* tab = TabAndroid::FromWebContents(web_contents); |
| 50 if (!tab) { |
| 51 permission_prompt->Closing(); |
| 52 return; |
| 53 } |
| 54 |
| 55 // Dispatch the dialog to Java, which manages the lifetime of this object. |
| 56 new PermissionDialogDelegate(tab, /*infobar_delegate=*/nullptr, |
| 57 permission_prompt); |
| 58 } |
| 59 |
| 60 // static |
| 61 void PermissionDialogDelegate::Create(content::WebContents* web_contents, |
| 62 ContentSettingsType type, |
| 63 const GURL& requesting_frame, |
| 64 bool user_gesture, |
| 65 Profile* profile, |
| 66 const PermissionSetCallback& callback) { |
| 67 DCHECK(web_contents); |
| 68 |
| 69 // If we don't have a tab, just act as though the prompt was dismissed. |
| 51 TabAndroid* tab = TabAndroid::FromWebContents(web_contents); | 70 TabAndroid* tab = TabAndroid::FromWebContents(web_contents); |
| 52 if (!tab) { | 71 if (!tab) { |
| 53 callback.Run(false, PermissionAction::DISMISSED); | 72 callback.Run(false, PermissionAction::DISMISSED); |
| 54 return; | 73 return; |
| 55 } | 74 } |
| 56 | 75 |
| 57 // Dispatch the dialog to Java, which manages the lifetime of this object. | 76 // Dispatch the dialog to Java, which manages the lifetime of this object. |
| 58 new PermissionDialogDelegate( | 77 new PermissionDialogDelegate( |
| 59 tab, PermissionInfoBarDelegate::CreateDelegate( | 78 tab, |
| 60 type, requesting_frame, user_gesture, profile, callback)); | 79 PermissionInfoBarDelegate::CreateDelegate( |
| 80 type, requesting_frame, user_gesture, profile, callback), |
| 81 /*permission_prompt=*/nullptr); |
| 61 } | 82 } |
| 62 | 83 |
| 63 // static | 84 // static |
| 64 void PermissionDialogDelegate::CreateMediaStreamDialog( | 85 void PermissionDialogDelegate::CreateMediaStreamDialog( |
| 65 content::WebContents* web_contents, | 86 content::WebContents* web_contents, |
| 66 bool user_gesture, | 87 bool user_gesture, |
| 67 std::unique_ptr<MediaStreamDevicesController::Request> request) { | 88 std::unique_ptr<MediaStreamDevicesController::Request> request) { |
| 68 DCHECK(web_contents); | 89 DCHECK(web_contents); |
| 69 | 90 |
| 70 // If we don't have a tab, just act as though the prompt was dismissed. | 91 // If we don't have a tab, just act as though the prompt was dismissed. |
| 71 TabAndroid* tab = TabAndroid::FromWebContents(web_contents); | 92 TabAndroid* tab = TabAndroid::FromWebContents(web_contents); |
| 72 if (!tab) { | 93 if (!tab) { |
| 73 request->Cancelled(); | 94 request->Cancelled(); |
| 74 return; | 95 return; |
| 75 } | 96 } |
| 76 | 97 |
| 77 // Called this way because the infobar delegate has a private destructor. | 98 // Called this way because the infobar delegate has a private destructor. |
| 78 std::unique_ptr<PermissionInfoBarDelegate> infobar_delegate; | 99 std::unique_ptr<PermissionInfoBarDelegate> infobar_delegate; |
| 79 infobar_delegate.reset(new MediaStreamInfoBarDelegateAndroid( | 100 infobar_delegate.reset(new MediaStreamInfoBarDelegateAndroid( |
| 80 Profile::FromBrowserContext(web_contents->GetBrowserContext()), | 101 Profile::FromBrowserContext(web_contents->GetBrowserContext()), |
| 81 user_gesture, std::move(request))); | 102 user_gesture, std::move(request))); |
| 82 | 103 |
| 83 // Dispatch the dialog to Java, which manages the lifetime of this object. | 104 // Dispatch the dialog to Java, which manages the lifetime of this object. |
| 84 new PermissionDialogDelegate(tab, std::move(infobar_delegate)); | 105 new PermissionDialogDelegate(tab, std::move(infobar_delegate), |
| 106 /*permission_prompt=*/nullptr); |
| 85 } | 107 } |
| 86 | 108 |
| 87 // static | 109 // static |
| 88 bool PermissionDialogDelegate::ShouldShowDialog(bool has_user_gesture) { | 110 bool PermissionDialogDelegate::ShouldShowDialog(bool has_user_gesture) { |
| 89 if (!base::FeatureList::IsEnabled(features::kModalPermissionPrompts)) | 111 if (!base::FeatureList::IsEnabled(features::kModalPermissionPrompts)) |
| 90 return false; | 112 return false; |
| 91 | 113 |
| 92 // Only use modals when the prompt is triggered by a user gesture, unless the | 114 // Only use modals when the prompt is triggered by a user gesture, unless the |
| 93 // kModalParamsUserGestureKey is set to false. | 115 // kModalParamsUserGestureKey is set to false. |
| 94 std::string require_gesture = variations::GetVariationParamValueByFeature( | 116 std::string require_gesture = variations::GetVariationParamValueByFeature( |
| 95 features::kModalPermissionPrompts, kModalParamsUserGestureKey); | 117 features::kModalPermissionPrompts, kModalParamsUserGestureKey); |
| 96 if (require_gesture == "false") | 118 if (require_gesture == "false") |
| 97 return true; | 119 return true; |
| 98 return has_user_gesture; | 120 return has_user_gesture; |
| 99 } | 121 } |
| 100 | 122 |
| 101 // static | 123 // static |
| 102 bool PermissionDialogDelegate::RegisterPermissionDialogDelegate(JNIEnv* env) { | 124 bool PermissionDialogDelegate::RegisterPermissionDialogDelegate(JNIEnv* env) { |
| 103 return RegisterNativesImpl(env); | 125 return RegisterNativesImpl(env); |
| 104 } | 126 } |
| 105 | 127 |
| 106 void PermissionDialogDelegate::CreateJavaDelegate(JNIEnv* env) { | 128 void PermissionDialogDelegate::CreateJavaDelegate(JNIEnv* env) { |
| 129 base::android::ScopedJavaLocalRef<jstring> primaryButtonText = |
| 130 ConvertUTF16ToJavaString(env, |
| 131 l10n_util::GetStringUTF16(IDS_PERMISSION_ALLOW)); |
| 132 base::android::ScopedJavaLocalRef<jstring> secondaryButtonText = |
| 133 ConvertUTF16ToJavaString(env, |
| 134 l10n_util::GetStringUTF16(IDS_PERMISSION_DENY)); |
| 135 |
| 136 if (infobar_delegate_) { |
| 137 std::vector<int> content_settings_types{ |
| 138 infobar_delegate_->content_settings_types()}; |
| 139 |
| 140 j_delegate_.Reset(Java_PermissionDialogDelegate_create( |
| 141 env, reinterpret_cast<uintptr_t>(this), tab_->GetJavaObject(), |
| 142 base::android::ToJavaIntArray(env, content_settings_types).obj(), |
| 143 ResourceMapper::MapFromChromiumId(infobar_delegate_->GetIconId()), |
| 144 ConvertUTF16ToJavaString(env, infobar_delegate_->GetMessageText()), |
| 145 ConvertUTF16ToJavaString(env, infobar_delegate_->GetLinkText()), |
| 146 primaryButtonText, secondaryButtonText, |
| 147 infobar_delegate_->ShouldShowPersistenceToggle())); |
| 148 return; |
| 149 } |
| 150 |
| 151 // TODO(timloh): Handle grouped media permissions (camera + microphone). |
| 152 DCHECK_EQ(1u, permission_prompt_->PermissionCount()); |
| 153 |
| 107 std::vector<int> content_settings_types{ | 154 std::vector<int> content_settings_types{ |
| 108 infobar_delegate_->content_settings_types()}; | 155 permission_prompt_->GetContentSettingType(0)}; |
| 109 | 156 |
| 110 j_delegate_.Reset(Java_PermissionDialogDelegate_create( | 157 j_delegate_.Reset(Java_PermissionDialogDelegate_create( |
| 111 env, reinterpret_cast<uintptr_t>(this), tab_->GetJavaObject(), | 158 env, reinterpret_cast<uintptr_t>(this), tab_->GetJavaObject(), |
| 112 base::android::ToJavaIntArray(env, content_settings_types).obj(), | 159 base::android::ToJavaIntArray(env, content_settings_types).obj(), |
| 113 ResourceMapper::MapFromChromiumId(infobar_delegate_->GetIconId()), | 160 ResourceMapper::MapFromChromiumId( |
| 114 ConvertUTF16ToJavaString(env, infobar_delegate_->GetMessageText()), | 161 permission_prompt_->GetIconIdForPermission(0)), |
| 115 ConvertUTF16ToJavaString(env, infobar_delegate_->GetLinkText()), | 162 // TODO(timloh): This is the wrong string. |
| 116 ConvertUTF16ToJavaString(env, infobar_delegate_->GetButtonLabel( | |
| 117 PermissionInfoBarDelegate::BUTTON_OK)), | |
| 118 ConvertUTF16ToJavaString(env, | 163 ConvertUTF16ToJavaString(env, |
| 119 infobar_delegate_->GetButtonLabel( | 164 permission_prompt_->GetMessageTextFragment(0)), |
| 120 PermissionInfoBarDelegate::BUTTON_CANCEL)), | 165 // TODO(timloh): Pass the actual link text for EME. |
| 121 infobar_delegate_->ShouldShowPersistenceToggle())); | 166 ConvertUTF16ToJavaString(env, base::string16()), primaryButtonText, |
| 167 secondaryButtonText, |
| 168 // TODO(timloh): Hook up the persistence toggle. |
| 169 false)); |
| 122 } | 170 } |
| 123 | 171 |
| 124 void PermissionDialogDelegate::Accept(JNIEnv* env, | 172 void PermissionDialogDelegate::Accept(JNIEnv* env, |
| 125 const JavaParamRef<jobject>& obj, | 173 const JavaParamRef<jobject>& obj, |
| 126 jboolean persist) { | 174 jboolean persist) { |
| 127 if (infobar_delegate_->ShouldShowPersistenceToggle()) | 175 if (infobar_delegate_) { |
| 128 infobar_delegate_->set_persist(persist); | 176 if (infobar_delegate_->ShouldShowPersistenceToggle()) |
| 129 infobar_delegate_->Accept(); | 177 infobar_delegate_->set_persist(persist); |
| 178 infobar_delegate_->Accept(); |
| 179 return; |
| 180 } |
| 181 |
| 182 permission_prompt_->Accept(); |
| 130 } | 183 } |
| 131 | 184 |
| 132 void PermissionDialogDelegate::Cancel(JNIEnv* env, | 185 void PermissionDialogDelegate::Cancel(JNIEnv* env, |
| 133 const JavaParamRef<jobject>& obj, | 186 const JavaParamRef<jobject>& obj, |
| 134 jboolean persist) { | 187 jboolean persist) { |
| 135 if (infobar_delegate_->ShouldShowPersistenceToggle()) | 188 if (infobar_delegate_) { |
| 136 infobar_delegate_->set_persist(persist); | 189 if (infobar_delegate_->ShouldShowPersistenceToggle()) |
| 137 infobar_delegate_->Cancel(); | 190 infobar_delegate_->set_persist(persist); |
| 191 infobar_delegate_->Cancel(); |
| 192 return; |
| 193 } |
| 194 |
| 195 permission_prompt_->Deny(); |
| 138 } | 196 } |
| 139 | 197 |
| 140 void PermissionDialogDelegate::Dismissed(JNIEnv* env, | 198 void PermissionDialogDelegate::Dismissed(JNIEnv* env, |
| 141 const JavaParamRef<jobject>& obj) { | 199 const JavaParamRef<jobject>& obj) { |
| 142 infobar_delegate_->InfoBarDismissed(); | 200 if (infobar_delegate_) { |
| 201 infobar_delegate_->InfoBarDismissed(); |
| 202 return; |
| 203 } |
| 204 |
| 205 permission_prompt_->Closing(); |
| 143 } | 206 } |
| 144 | 207 |
| 145 void PermissionDialogDelegate::LinkClicked(JNIEnv* env, | 208 void PermissionDialogDelegate::LinkClicked(JNIEnv* env, |
| 146 const JavaParamRef<jobject>& obj) { | 209 const JavaParamRef<jobject>& obj) { |
| 147 // Don't call delegate_->LinkClicked() because that relies on having an | 210 // 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 | 211 // 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). | 212 // wrapped delegate has no owner (it hasn't been added as an infobar). |
| 150 if (tab_->web_contents()) { | 213 if (tab_->web_contents()) { |
| 151 tab_->web_contents()->OpenURL(content::OpenURLParams( | 214 if (infobar_delegate_) { |
| 152 infobar_delegate_->GetLinkURL(), content::Referrer(), | 215 tab_->web_contents()->OpenURL(content::OpenURLParams( |
| 153 WindowOpenDisposition::NEW_FOREGROUND_TAB, ui::PAGE_TRANSITION_LINK, | 216 infobar_delegate_->GetLinkURL(), content::Referrer(), |
| 154 false)); | 217 WindowOpenDisposition::NEW_FOREGROUND_TAB, ui::PAGE_TRANSITION_LINK, |
| 218 false)); |
| 219 } |
| 220 // TODO(timloh): Show a 'learn more' link in the PermissionRequestManager |
| 221 // codepath for EME. |
| 155 } | 222 } |
| 156 } | 223 } |
| 157 | 224 |
| 158 void PermissionDialogDelegate::Destroy(JNIEnv* env, | 225 void PermissionDialogDelegate::Destroy(JNIEnv* env, |
| 159 const JavaParamRef<jobject>& obj) { | 226 const JavaParamRef<jobject>& obj) { |
| 160 delete this; | 227 delete this; |
| 161 } | 228 } |
| 162 | 229 |
| 163 PermissionDialogDelegate::PermissionDialogDelegate( | 230 PermissionDialogDelegate::PermissionDialogDelegate( |
| 164 TabAndroid* tab, | 231 TabAndroid* tab, |
| 165 std::unique_ptr<PermissionInfoBarDelegate> infobar_delegate) | 232 std::unique_ptr<PermissionInfoBarDelegate> infobar_delegate, |
| 233 PermissionPromptAndroid* permission_prompt) |
| 166 : content::WebContentsObserver(tab->web_contents()), | 234 : content::WebContentsObserver(tab->web_contents()), |
| 167 tab_(tab), | 235 tab_(tab), |
| 168 infobar_delegate_(std::move(infobar_delegate)) { | 236 infobar_delegate_(std::move(infobar_delegate)), |
| 237 permission_prompt_(permission_prompt) { |
| 169 DCHECK(tab_); | 238 DCHECK(tab_); |
| 170 DCHECK(infobar_delegate_); | 239 // Only one of the PermissionPromptAndroid and PermissionInfoBarDelegate is |
| 240 // used, depending on whether the PermissionRequestManager is enabled or not. |
| 241 DCHECK(!!permission_prompt_ ^ !!infobar_delegate_); |
| 171 | 242 |
| 172 // Create our Java counterpart, which manages our lifetime. | 243 // Create our Java counterpart, which manages our lifetime. |
| 173 JNIEnv* env = base::android::AttachCurrentThread(); | 244 JNIEnv* env = base::android::AttachCurrentThread(); |
| 174 CreateJavaDelegate(env); | 245 CreateJavaDelegate(env); |
| 175 | 246 |
| 176 // Send the Java delegate to the Java PermissionDialogController for display. | 247 // Send the Java delegate to the Java PermissionDialogController for display. |
| 177 // The controller takes over lifetime management; when the Java delegate is no | 248 // The controller takes over lifetime management; when the Java delegate is no |
| 178 // longer needed it will in turn free the native delegate. | 249 // longer needed it will in turn free the native delegate. |
| 179 Java_PermissionDialogController_createDialog(env, j_delegate_.obj()); | 250 Java_PermissionDialogController_createDialog(env, j_delegate_.obj()); |
| 180 } | 251 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 193 navigation_handle->IsSameDocument()) { | 264 navigation_handle->IsSameDocument()) { |
| 194 return; | 265 return; |
| 195 } | 266 } |
| 196 | 267 |
| 197 DismissDialog(); | 268 DismissDialog(); |
| 198 } | 269 } |
| 199 | 270 |
| 200 void PermissionDialogDelegate::WebContentsDestroyed() { | 271 void PermissionDialogDelegate::WebContentsDestroyed() { |
| 201 DismissDialog(); | 272 DismissDialog(); |
| 202 } | 273 } |
| OLD | NEW |