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 "build/build_config.h" |
| 12 #include "chrome/browser/android/resource_mapper.h" |
| 13 #include "chrome/browser/geolocation/geolocation_infobar_delegate_android.h" |
| 14 #include "chrome/browser/media/midi_permission_infobar_delegate_android.h" |
| 15 #include "chrome/browser/media/protected_media_identifier_infobar_delegate_andro
id.h" |
| 16 #include "chrome/browser/notifications/notification_permission_infobar_delegate.
h" |
| 17 #include "content/public/browser/android/content_view_core.h" |
| 18 #include "content/public/browser/web_contents.h" |
| 19 #include "jni/PermissionDialogController_jni.h" |
| 20 #include "jni/PermissionDialogDelegate_jni.h" |
| 21 #include "ui/android/window_android.h" |
| 22 #include "ui/base/window_open_disposition.h" |
| 23 |
| 24 using base::android::ConvertUTF16ToJavaString; |
| 25 |
| 26 // static |
| 27 void PermissionDialogDelegate::Create( |
| 28 content::PermissionType type, |
| 29 content::WebContents* web_contents, |
| 30 const GURL& requesting_frame, |
| 31 bool user_gesture, |
| 32 Profile* profile, |
| 33 const PermissionSetCallback& callback) { |
| 34 DCHECK(web_contents); |
| 35 |
| 36 std::unique_ptr<PermissionInfoBarDelegate> infobar_delegate = nullptr; |
| 37 switch (type) { |
| 38 case content::PermissionType::GEOLOCATION: |
| 39 infobar_delegate.reset(new GeolocationInfoBarDelegateAndroid( |
| 40 requesting_frame, user_gesture, profile, callback)); |
| 41 break; |
| 42 #if defined(ENABLE_NOTIFICATIONS) |
| 43 case content::PermissionType::NOTIFICATIONS: |
| 44 case content::PermissionType::PUSH_MESSAGING: |
| 45 infobar_delegate.reset(new NotificationPermissionInfoBarDelegate( |
| 46 requesting_frame, user_gesture, profile, callback)); |
| 47 break; |
| 48 #endif // ENABLE_NOTIFICATIONS |
| 49 case content::PermissionType::MIDI_SYSEX: |
| 50 infobar_delegate.reset(new MidiPermissionInfoBarDelegateAndroid( |
| 51 requesting_frame, user_gesture, profile, callback)); |
| 52 break; |
| 53 case content::PermissionType::PROTECTED_MEDIA_IDENTIFIER: |
| 54 infobar_delegate.reset(new ProtectedMediaIdentifierInfoBarDelegateAndroid( |
| 55 requesting_frame, user_gesture, profile, callback)); |
| 56 break; |
| 57 default: |
| 58 NOTREACHED(); |
| 59 } |
| 60 |
| 61 PermissionDialogDelegate* dialog_delegate = |
| 62 new PermissionDialogDelegate(web_contents, std::move(infobar_delegate)); |
| 63 |
| 64 JNIEnv* env = base::android::AttachCurrentThread(); |
| 65 base::android::ScopedJavaLocalRef<jobject> j_delegate = |
| 66 dialog_delegate->CreateJavaDelegate(env); |
| 67 |
| 68 // Java queues the dialog request if neccesary, and owns the the lifetime of |
| 69 // the delegate. |
| 70 Java_PermissionDialogController_createDialog(env, j_delegate.obj()); |
| 71 } |
| 72 |
| 73 // static |
| 74 bool PermissionDialogDelegate::RegisterPermissionDialogDelegate(JNIEnv* env) { |
| 75 return RegisterNativesImpl(env); |
| 76 } |
| 77 |
| 78 ScopedJavaLocalRef<jobject> PermissionDialogDelegate::CreateJavaDelegate( |
| 79 JNIEnv* env) { |
| 80 content::ContentViewCore* cvc = |
| 81 content::ContentViewCore::FromWebContents(web_contents()); |
| 82 DCHECK(cvc); |
| 83 |
| 84 return Java_PermissionDialogDelegate_create( |
| 85 env, reinterpret_cast<uintptr_t>(this), |
| 86 cvc->GetWindowAndroid()->GetJavaObject(), |
| 87 ResourceMapper::MapFromChromiumId(infobar_delegate_->GetIconId()), |
| 88 ConvertUTF16ToJavaString(env, infobar_delegate_->GetMessageText()), |
| 89 ConvertUTF16ToJavaString(env, infobar_delegate_->GetLinkText()), |
| 90 ConvertUTF16ToJavaString(env, infobar_delegate_->GetButtonLabel( |
| 91 PermissionInfoBarDelegate::BUTTON_OK)), |
| 92 ConvertUTF16ToJavaString(env, |
| 93 infobar_delegate_->GetButtonLabel( |
| 94 PermissionInfoBarDelegate::BUTTON_CANCEL)), |
| 95 infobar_delegate_->ShouldShowPersistenceToggle()); |
| 96 } |
| 97 |
| 98 void PermissionDialogDelegate::Accept(JNIEnv* env, |
| 99 const JavaParamRef<jobject>& obj, |
| 100 jboolean persist) { |
| 101 if (infobar_delegate_->ShouldShowPersistenceToggle()) |
| 102 infobar_delegate_->set_persist(persist); |
| 103 infobar_delegate_->Accept(); |
| 104 } |
| 105 |
| 106 void PermissionDialogDelegate::Cancel(JNIEnv* env, |
| 107 const JavaParamRef<jobject>& obj, |
| 108 jboolean persist) { |
| 109 if (infobar_delegate_->ShouldShowPersistenceToggle()) |
| 110 infobar_delegate_->set_persist(persist); |
| 111 infobar_delegate_->Cancel(); |
| 112 } |
| 113 |
| 114 void PermissionDialogDelegate::Dismissed(JNIEnv* env, |
| 115 const JavaParamRef<jobject>& obj) { |
| 116 infobar_delegate_->InfoBarDismissed(); |
| 117 } |
| 118 |
| 119 void PermissionDialogDelegate::LinkClicked(JNIEnv* env, |
| 120 const JavaParamRef<jobject>& obj) { |
| 121 // Don't call delegate_->LinkClicked() because that relies on having an |
| 122 // InfoBarService as an owner() to open the link. That will fail since the |
| 123 // wrapped delegate has no owner (is hasn't been added as an infobar). |
| 124 if (web_contents()) { |
| 125 web_contents()->OpenURL(content::OpenURLParams( |
| 126 infobar_delegate_->GetLinkURL(), content::Referrer(), |
| 127 WindowOpenDisposition::NEW_FOREGROUND_TAB, ui::PAGE_TRANSITION_LINK, |
| 128 false)); |
| 129 } |
| 130 } |
| 131 |
| 132 void PermissionDialogDelegate::Destroy(JNIEnv* env, |
| 133 const JavaParamRef<jobject>& obj) { |
| 134 delete this; |
| 135 } |
| 136 |
| 137 PermissionDialogDelegate::PermissionDialogDelegate( |
| 138 content::WebContents* web_contents, |
| 139 std::unique_ptr<PermissionInfoBarDelegate> infobar_delegate) |
| 140 : content::WebContentsObserver(web_contents), |
| 141 infobar_delegate_(std::move(infobar_delegate)) { |
| 142 DCHECK(infobar_delegate_); |
| 143 } |
| 144 |
| 145 PermissionDialogDelegate::~PermissionDialogDelegate() {} |
OLD | NEW |