Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(46)

Side by Side Diff: chrome/browser/permissions/permission_dialog_delegate.cc

Issue 2899973002: Hide modal permission prompts on Android upon tab navigation/destruction (Closed)
Patch Set: address comments Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "components/variations/variations_associated_data.h" 23 #include "components/variations/variations_associated_data.h"
24 #include "content/public/browser/navigation_handle.h"
24 #include "content/public/browser/web_contents.h" 25 #include "content/public/browser/web_contents.h"
25 #include "jni/PermissionDialogController_jni.h" 26 #include "jni/PermissionDialogController_jni.h"
26 #include "jni/PermissionDialogDelegate_jni.h" 27 #include "jni/PermissionDialogDelegate_jni.h"
27 #include "ui/android/window_android.h" 28 #include "ui/android/window_android.h"
28 #include "ui/base/window_open_disposition.h" 29 #include "ui/base/window_open_disposition.h"
29 30
30 using base::android::ConvertUTF16ToJavaString; 31 using base::android::ConvertUTF16ToJavaString;
31 32
32 namespace { 33 namespace {
33 34
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 if (require_gesture == "false") 96 if (require_gesture == "false")
96 return true; 97 return true;
97 return has_user_gesture; 98 return has_user_gesture;
98 } 99 }
99 100
100 // static 101 // static
101 bool PermissionDialogDelegate::RegisterPermissionDialogDelegate(JNIEnv* env) { 102 bool PermissionDialogDelegate::RegisterPermissionDialogDelegate(JNIEnv* env) {
102 return RegisterNativesImpl(env); 103 return RegisterNativesImpl(env);
103 } 104 }
104 105
105 ScopedJavaLocalRef<jobject> PermissionDialogDelegate::CreateJavaDelegate( 106 void PermissionDialogDelegate::CreateJavaDelegate(JNIEnv* env) {
106 JNIEnv* env) {
107 std::vector<int> content_settings_types{ 107 std::vector<int> content_settings_types{
108 infobar_delegate_->content_settings_types()}; 108 infobar_delegate_->content_settings_types()};
109 109
110 return Java_PermissionDialogDelegate_create( 110 j_delegate_.Reset(Java_PermissionDialogDelegate_create(
111 env, reinterpret_cast<uintptr_t>(this), 111 env, reinterpret_cast<uintptr_t>(this), tab_->GetJavaObject(),
112 tab_->GetJavaObject(),
113 base::android::ToJavaIntArray(env, content_settings_types).obj(), 112 base::android::ToJavaIntArray(env, content_settings_types).obj(),
114 ResourceMapper::MapFromChromiumId(infobar_delegate_->GetIconId()), 113 ResourceMapper::MapFromChromiumId(infobar_delegate_->GetIconId()),
115 ConvertUTF16ToJavaString(env, infobar_delegate_->GetMessageText()), 114 ConvertUTF16ToJavaString(env, infobar_delegate_->GetMessageText()),
116 ConvertUTF16ToJavaString(env, infobar_delegate_->GetLinkText()), 115 ConvertUTF16ToJavaString(env, infobar_delegate_->GetLinkText()),
117 ConvertUTF16ToJavaString(env, infobar_delegate_->GetButtonLabel( 116 ConvertUTF16ToJavaString(env, infobar_delegate_->GetButtonLabel(
118 PermissionInfoBarDelegate::BUTTON_OK)), 117 PermissionInfoBarDelegate::BUTTON_OK)),
119 ConvertUTF16ToJavaString(env, 118 ConvertUTF16ToJavaString(env,
120 infobar_delegate_->GetButtonLabel( 119 infobar_delegate_->GetButtonLabel(
121 PermissionInfoBarDelegate::BUTTON_CANCEL)), 120 PermissionInfoBarDelegate::BUTTON_CANCEL)),
122 infobar_delegate_->ShouldShowPersistenceToggle()); 121 infobar_delegate_->ShouldShowPersistenceToggle()));
123 } 122 }
124 123
125 void PermissionDialogDelegate::Accept(JNIEnv* env, 124 void PermissionDialogDelegate::Accept(JNIEnv* env,
126 const JavaParamRef<jobject>& obj, 125 const JavaParamRef<jobject>& obj,
127 jboolean persist) { 126 jboolean persist) {
128 if (infobar_delegate_->ShouldShowPersistenceToggle()) 127 if (infobar_delegate_->ShouldShowPersistenceToggle())
129 infobar_delegate_->set_persist(persist); 128 infobar_delegate_->set_persist(persist);
130 infobar_delegate_->Accept(); 129 infobar_delegate_->Accept();
131 } 130 }
132 131
(...skipping 18 matching lines...) Expand all
151 if (tab_->web_contents()) { 150 if (tab_->web_contents()) {
152 tab_->web_contents()->OpenURL(content::OpenURLParams( 151 tab_->web_contents()->OpenURL(content::OpenURLParams(
153 infobar_delegate_->GetLinkURL(), content::Referrer(), 152 infobar_delegate_->GetLinkURL(), content::Referrer(),
154 WindowOpenDisposition::NEW_FOREGROUND_TAB, ui::PAGE_TRANSITION_LINK, 153 WindowOpenDisposition::NEW_FOREGROUND_TAB, ui::PAGE_TRANSITION_LINK,
155 false)); 154 false));
156 } 155 }
157 } 156 }
158 157
159 void PermissionDialogDelegate::Destroy(JNIEnv* env, 158 void PermissionDialogDelegate::Destroy(JNIEnv* env,
160 const JavaParamRef<jobject>& obj) { 159 const JavaParamRef<jobject>& obj) {
160 j_delegate_ = nullptr;
Bernhard Bauer 2017/05/25 09:57:17 This isn't really necessary if the object is going
Timothy Loh 2017/05/26 05:08:03 Err yeah, removed.
161 delete this; 161 delete this;
162 } 162 }
163 163
164 PermissionDialogDelegate::PermissionDialogDelegate( 164 PermissionDialogDelegate::PermissionDialogDelegate(
165 TabAndroid* tab, 165 TabAndroid* tab,
166 std::unique_ptr<PermissionInfoBarDelegate> infobar_delegate) 166 std::unique_ptr<PermissionInfoBarDelegate> infobar_delegate)
167 : tab_(tab), infobar_delegate_(std::move(infobar_delegate)) { 167 : content::WebContentsObserver(tab->web_contents()),
168 tab_(tab),
169 infobar_delegate_(std::move(infobar_delegate)) {
168 DCHECK(tab_); 170 DCHECK(tab_);
169 DCHECK(infobar_delegate_); 171 DCHECK(infobar_delegate_);
170 172
171 // Create our Java counterpart, which manages our lifetime. 173 // Create our Java counterpart, which manages our lifetime.
172 JNIEnv* env = base::android::AttachCurrentThread(); 174 JNIEnv* env = base::android::AttachCurrentThread();
173 base::android::ScopedJavaLocalRef<jobject> j_delegate = 175 CreateJavaDelegate(env);
174 CreateJavaDelegate(env);
175 176
176 // Send the Java delegate to the Java PermissionDialogController for display. 177 // Send the Java delegate to the Java PermissionDialogController for display.
177 // The controller takes over lifetime management; when the Java delegate is no 178 // The controller takes over lifetime management; when the Java delegate is no
178 // longer needed it will in turn free the native delegate. 179 // longer needed it will in turn free the native delegate.
179 Java_PermissionDialogController_createDialog(env, j_delegate.obj()); 180 Java_PermissionDialogController_createDialog(env, j_delegate_.obj());
180 } 181 }
181 182
182 PermissionDialogDelegate::~PermissionDialogDelegate() {} 183 PermissionDialogDelegate::~PermissionDialogDelegate() {}
184
185 void PermissionDialogDelegate::DismissDialog() {
186 JNIEnv* env = base::android::AttachCurrentThread();
187 Java_PermissionDialogDelegate_dismissFromNative(env, j_delegate_.obj());
188 }
189
190 void PermissionDialogDelegate::DidFinishNavigation(
191 content::NavigationHandle* navigation_handle) {
192 if (!navigation_handle->IsInMainFrame() ||
193 !navigation_handle->HasCommitted() ||
194 navigation_handle->IsSameDocument()) {
195 return;
196 }
197
198 DismissDialog();
199 delete this;
Bernhard Bauer 2017/05/25 09:57:17 I have to say, I don't like these deletions very m
Timothy Loh 2017/05/26 05:08:03 I considered it but figured it would be nicer this
Bernhard Bauer 2017/05/26 09:01:16 Thanks! I do think this is nicer. If you want to,
200 }
201
202 void PermissionDialogDelegate::WebContentsDestroyed() {
203 DismissDialog();
204 delete this;
205 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698