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" |
(...skipping 29 matching lines...) Expand all Loading... | |
40 // static | 40 // static |
41 void PermissionDialogDelegate::Create( | 41 void PermissionDialogDelegate::Create( |
42 content::WebContents* web_contents, | 42 content::WebContents* web_contents, |
43 content::PermissionType type, | 43 content::PermissionType type, |
44 const GURL& requesting_frame, | 44 const GURL& requesting_frame, |
45 bool user_gesture, | 45 bool user_gesture, |
46 Profile* profile, | 46 Profile* profile, |
47 const PermissionSetCallback& callback) { | 47 const PermissionSetCallback& callback) { |
48 DCHECK(web_contents); | 48 DCHECK(web_contents); |
49 | 49 |
50 // If we don't have a tab, just act as though the prompt was dismissed. | |
51 TabAndroid* tab = TabAndroid::FromWebContents(web_contents); | |
52 if (!tab) { | |
53 callback.Run(false, PermissionAction::DISMISSED); | |
54 return; | |
55 } | |
56 | |
50 // Dispatch the dialog to Java, which manages the lifetime of this object. | 57 // Dispatch the dialog to Java, which manages the lifetime of this object. |
51 new PermissionDialogDelegate( | 58 new PermissionDialogDelegate( |
52 web_contents, | 59 tab, PermissionInfoBarDelegate::CreateDelegate( |
53 PermissionInfoBarDelegate::CreateDelegate( | 60 type, requesting_frame, user_gesture, profile, callback)); |
54 type, requesting_frame, user_gesture, profile, callback)); | |
55 } | 61 } |
56 | 62 |
57 // static | 63 // static |
58 void PermissionDialogDelegate::CreateMediaStreamDialog( | 64 void PermissionDialogDelegate::CreateMediaStreamDialog( |
59 content::WebContents* web_contents, | 65 content::WebContents* web_contents, |
60 bool user_gesture, | 66 bool user_gesture, |
61 std::unique_ptr<MediaStreamDevicesController> controller) { | 67 std::unique_ptr<MediaStreamDevicesController> controller) { |
68 DCHECK(web_contents); | |
69 | |
70 // If we don't have a tab, just act as though the prompt was dismissed. | |
71 TabAndroid* tab = TabAndroid::FromWebContents(web_contents); | |
72 if (!tab) { | |
73 controller->Cancelled(); | |
74 return; | |
75 } | |
76 | |
62 // Called this way because the infobar delegate has a private destructor. | 77 // Called this way because the infobar delegate has a private destructor. |
63 std::unique_ptr<PermissionInfoBarDelegate> infobar_delegate; | 78 std::unique_ptr<PermissionInfoBarDelegate> infobar_delegate; |
64 infobar_delegate.reset(new MediaStreamInfoBarDelegateAndroid( | 79 infobar_delegate.reset(new MediaStreamInfoBarDelegateAndroid( |
65 Profile::FromBrowserContext(web_contents->GetBrowserContext()), | 80 Profile::FromBrowserContext(web_contents->GetBrowserContext()), |
66 user_gesture, | 81 user_gesture, |
67 std::move(controller))); | 82 std::move(controller))); |
68 | 83 |
69 // Dispatch the dialog to Java, which manages the lifetime of this object. | 84 // Dispatch the dialog to Java, which manages the lifetime of this object. |
70 new PermissionDialogDelegate(web_contents, std::move(infobar_delegate)); | 85 new PermissionDialogDelegate(tab, std::move(infobar_delegate)); |
71 } | 86 } |
72 | 87 |
73 // static | 88 // static |
74 bool PermissionDialogDelegate::ShouldShowDialog(bool has_user_gesture) { | 89 bool PermissionDialogDelegate::ShouldShowDialog(bool has_user_gesture) { |
75 if (!base::FeatureList::IsEnabled(features::kModalPermissionPrompts)) | 90 if (!base::FeatureList::IsEnabled(features::kModalPermissionPrompts)) |
76 return false; | 91 return false; |
77 | 92 |
78 // Only use modals when the prompt is triggered by a user gesture, unless the | 93 // Only use modals when the prompt is triggered by a user gesture, unless the |
79 // kModalParamsUserGestureKey is set to false. | 94 // kModalParamsUserGestureKey is set to false. |
80 std::string require_gesture = variations::GetVariationParamValueByFeature( | 95 std::string require_gesture = variations::GetVariationParamValueByFeature( |
81 features::kModalPermissionPrompts, kModalParamsUserGestureKey); | 96 features::kModalPermissionPrompts, kModalParamsUserGestureKey); |
82 if (require_gesture == "false") | 97 if (require_gesture == "false") |
83 return true; | 98 return true; |
84 return has_user_gesture; | 99 return has_user_gesture; |
85 } | 100 } |
86 | 101 |
87 // static | 102 // static |
88 bool PermissionDialogDelegate::RegisterPermissionDialogDelegate(JNIEnv* env) { | 103 bool PermissionDialogDelegate::RegisterPermissionDialogDelegate(JNIEnv* env) { |
89 return RegisterNativesImpl(env); | 104 return RegisterNativesImpl(env); |
90 } | 105 } |
91 | 106 |
92 ScopedJavaLocalRef<jobject> PermissionDialogDelegate::CreateJavaDelegate( | 107 ScopedJavaLocalRef<jobject> PermissionDialogDelegate::CreateJavaDelegate( |
93 JNIEnv* env) { | 108 JNIEnv* env) { |
94 TabAndroid* tab = TabAndroid::FromWebContents(web_contents()); | |
95 DCHECK(tab); | |
96 | |
97 std::vector<int> content_settings_types{ | 109 std::vector<int> content_settings_types{ |
98 infobar_delegate_->content_settings_types()}; | 110 infobar_delegate_->content_settings_types()}; |
99 | 111 |
100 return Java_PermissionDialogDelegate_create( | 112 return Java_PermissionDialogDelegate_create( |
101 env, reinterpret_cast<uintptr_t>(this), | 113 env, reinterpret_cast<uintptr_t>(this), |
102 tab->GetJavaObject(), | 114 tab_->GetJavaObject(), |
103 base::android::ToJavaIntArray(env, content_settings_types).obj(), | 115 base::android::ToJavaIntArray(env, content_settings_types).obj(), |
104 ResourceMapper::MapFromChromiumId(infobar_delegate_->GetIconId()), | 116 ResourceMapper::MapFromChromiumId(infobar_delegate_->GetIconId()), |
105 ConvertUTF16ToJavaString(env, infobar_delegate_->GetMessageText()), | 117 ConvertUTF16ToJavaString(env, infobar_delegate_->GetMessageText()), |
106 ConvertUTF16ToJavaString(env, infobar_delegate_->GetLinkText()), | 118 ConvertUTF16ToJavaString(env, infobar_delegate_->GetLinkText()), |
107 ConvertUTF16ToJavaString(env, infobar_delegate_->GetButtonLabel( | 119 ConvertUTF16ToJavaString(env, infobar_delegate_->GetButtonLabel( |
108 PermissionInfoBarDelegate::BUTTON_OK)), | 120 PermissionInfoBarDelegate::BUTTON_OK)), |
109 ConvertUTF16ToJavaString(env, | 121 ConvertUTF16ToJavaString(env, |
110 infobar_delegate_->GetButtonLabel( | 122 infobar_delegate_->GetButtonLabel( |
111 PermissionInfoBarDelegate::BUTTON_CANCEL)), | 123 PermissionInfoBarDelegate::BUTTON_CANCEL)), |
112 infobar_delegate_->ShouldShowPersistenceToggle()); | 124 infobar_delegate_->ShouldShowPersistenceToggle()); |
(...skipping 18 matching lines...) Expand all Loading... | |
131 void PermissionDialogDelegate::Dismissed(JNIEnv* env, | 143 void PermissionDialogDelegate::Dismissed(JNIEnv* env, |
132 const JavaParamRef<jobject>& obj) { | 144 const JavaParamRef<jobject>& obj) { |
133 infobar_delegate_->InfoBarDismissed(); | 145 infobar_delegate_->InfoBarDismissed(); |
134 } | 146 } |
135 | 147 |
136 void PermissionDialogDelegate::LinkClicked(JNIEnv* env, | 148 void PermissionDialogDelegate::LinkClicked(JNIEnv* env, |
137 const JavaParamRef<jobject>& obj) { | 149 const JavaParamRef<jobject>& obj) { |
138 // Don't call delegate_->LinkClicked() because that relies on having an | 150 // Don't call delegate_->LinkClicked() because that relies on having an |
139 // InfoBarService as an owner() to open the link. That will fail since the | 151 // InfoBarService as an owner() to open the link. That will fail since the |
140 // wrapped delegate has no owner (it hasn't been added as an infobar). | 152 // wrapped delegate has no owner (it hasn't been added as an infobar). |
141 if (web_contents()) { | 153 if (tab_->web_contents()) { |
142 web_contents()->OpenURL(content::OpenURLParams( | 154 tab_->web_contents()->OpenURL(content::OpenURLParams( |
143 infobar_delegate_->GetLinkURL(), content::Referrer(), | 155 infobar_delegate_->GetLinkURL(), content::Referrer(), |
144 WindowOpenDisposition::NEW_FOREGROUND_TAB, ui::PAGE_TRANSITION_LINK, | 156 WindowOpenDisposition::NEW_FOREGROUND_TAB, ui::PAGE_TRANSITION_LINK, |
145 false)); | 157 false)); |
146 } | 158 } |
147 } | 159 } |
148 | 160 |
149 void PermissionDialogDelegate::Destroy(JNIEnv* env, | 161 void PermissionDialogDelegate::Destroy(JNIEnv* env, |
150 const JavaParamRef<jobject>& obj) { | 162 const JavaParamRef<jobject>& obj) { |
151 delete this; | 163 delete this; |
152 } | 164 } |
153 | 165 |
154 PermissionDialogDelegate::PermissionDialogDelegate( | 166 PermissionDialogDelegate::PermissionDialogDelegate( |
155 content::WebContents* web_contents, | 167 TabAndroid* tab, |
raymes
2016/12/01 00:31:08
Do you know how the TabAndroid lifetime works? It
dominickn
2016/12/01 01:11:54
We already require the tab to live as long as Link
raymes
2016/12/01 03:22:25
I see - a reference to the TabAndroid will get pas
| |
156 std::unique_ptr<PermissionInfoBarDelegate> infobar_delegate) | 168 std::unique_ptr<PermissionInfoBarDelegate> infobar_delegate) |
157 : content::WebContentsObserver(web_contents), | 169 : tab_(tab), infobar_delegate_(std::move(infobar_delegate)) { |
158 infobar_delegate_(std::move(infobar_delegate)) { | 170 DCHECK(tab_); |
159 DCHECK(infobar_delegate_); | 171 DCHECK(infobar_delegate_); |
160 | 172 |
161 // Create our Java counterpart, which manages our lifetime. | 173 // Create our Java counterpart, which manages our lifetime. |
162 JNIEnv* env = base::android::AttachCurrentThread(); | 174 JNIEnv* env = base::android::AttachCurrentThread(); |
163 base::android::ScopedJavaLocalRef<jobject> j_delegate = | 175 base::android::ScopedJavaLocalRef<jobject> j_delegate = |
164 CreateJavaDelegate(env); | 176 CreateJavaDelegate(env); |
165 | 177 |
166 // Send the Java delegate to the Java PermissionDialogController for display. | 178 // Send the Java delegate to the Java PermissionDialogController for display. |
167 // The controller takes over lifetime management; when the Java delegate is no | 179 // The controller takes over lifetime management; when the Java delegate is no |
168 // longer needed it will in turn free the native delegate. | 180 // longer needed it will in turn free the native delegate. |
169 Java_PermissionDialogController_createDialog(env, j_delegate.obj()); | 181 Java_PermissionDialogController_createDialog(env, j_delegate.obj()); |
170 } | 182 } |
171 | 183 |
172 PermissionDialogDelegate::~PermissionDialogDelegate() {} | 184 PermissionDialogDelegate::~PermissionDialogDelegate() {} |
OLD | NEW |