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, nullptr, permission_prompt); | |
raymes
2017/05/29 04:31:39
nit: /*infobar_delegate=*/nullptr
Timothy Loh
2017/05/29 06:58:20
Done.
| |
57 } | |
58 | |
59 // static | |
60 void PermissionDialogDelegate::Create(content::WebContents* web_contents, | |
61 ContentSettingsType type, | |
62 const GURL& requesting_frame, | |
63 bool user_gesture, | |
64 Profile* profile, | |
65 const PermissionSetCallback& callback) { | |
66 DCHECK(web_contents); | |
67 | |
68 // If we don't have a tab, just act as though the prompt was dismissed. | |
51 TabAndroid* tab = TabAndroid::FromWebContents(web_contents); | 69 TabAndroid* tab = TabAndroid::FromWebContents(web_contents); |
52 if (!tab) { | 70 if (!tab) { |
53 callback.Run(false, PermissionAction::DISMISSED); | 71 callback.Run(false, PermissionAction::DISMISSED); |
54 return; | 72 return; |
55 } | 73 } |
56 | 74 |
57 // Dispatch the dialog to Java, which manages the lifetime of this object. | 75 // Dispatch the dialog to Java, which manages the lifetime of this object. |
58 new PermissionDialogDelegate( | 76 new PermissionDialogDelegate( |
59 tab, PermissionInfoBarDelegate::CreateDelegate( | 77 tab, |
60 type, requesting_frame, user_gesture, profile, callback)); | 78 PermissionInfoBarDelegate::CreateDelegate( |
79 type, requesting_frame, user_gesture, profile, callback), | |
80 nullptr); | |
61 } | 81 } |
62 | 82 |
63 // static | 83 // static |
64 void PermissionDialogDelegate::CreateMediaStreamDialog( | 84 void PermissionDialogDelegate::CreateMediaStreamDialog( |
65 content::WebContents* web_contents, | 85 content::WebContents* web_contents, |
66 bool user_gesture, | 86 bool user_gesture, |
67 std::unique_ptr<MediaStreamDevicesController::Request> request) { | 87 std::unique_ptr<MediaStreamDevicesController::Request> request) { |
68 DCHECK(web_contents); | 88 DCHECK(web_contents); |
69 | 89 |
70 // If we don't have a tab, just act as though the prompt was dismissed. | 90 // If we don't have a tab, just act as though the prompt was dismissed. |
71 TabAndroid* tab = TabAndroid::FromWebContents(web_contents); | 91 TabAndroid* tab = TabAndroid::FromWebContents(web_contents); |
72 if (!tab) { | 92 if (!tab) { |
73 request->Cancelled(); | 93 request->Cancelled(); |
74 return; | 94 return; |
75 } | 95 } |
76 | 96 |
77 // Called this way because the infobar delegate has a private destructor. | 97 // Called this way because the infobar delegate has a private destructor. |
78 std::unique_ptr<PermissionInfoBarDelegate> infobar_delegate; | 98 std::unique_ptr<PermissionInfoBarDelegate> infobar_delegate; |
79 infobar_delegate.reset(new MediaStreamInfoBarDelegateAndroid( | 99 infobar_delegate.reset(new MediaStreamInfoBarDelegateAndroid( |
80 Profile::FromBrowserContext(web_contents->GetBrowserContext()), | 100 Profile::FromBrowserContext(web_contents->GetBrowserContext()), |
81 user_gesture, std::move(request))); | 101 user_gesture, std::move(request))); |
82 | 102 |
83 // Dispatch the dialog to Java, which manages the lifetime of this object. | 103 // Dispatch the dialog to Java, which manages the lifetime of this object. |
84 new PermissionDialogDelegate(tab, std::move(infobar_delegate)); | 104 new PermissionDialogDelegate(tab, std::move(infobar_delegate), nullptr); |
raymes
2017/05/29 04:31:39
nit: /*permission_prompt=*/nullptr
Timothy Loh
2017/05/29 06:58:20
Done.
| |
85 } | 105 } |
86 | 106 |
87 // static | 107 // static |
88 bool PermissionDialogDelegate::ShouldShowDialog(bool has_user_gesture) { | 108 bool PermissionDialogDelegate::ShouldShowDialog(bool has_user_gesture) { |
89 if (!base::FeatureList::IsEnabled(features::kModalPermissionPrompts)) | 109 if (!base::FeatureList::IsEnabled(features::kModalPermissionPrompts)) |
90 return false; | 110 return false; |
91 | 111 |
92 // Only use modals when the prompt is triggered by a user gesture, unless the | 112 // Only use modals when the prompt is triggered by a user gesture, unless the |
93 // kModalParamsUserGestureKey is set to false. | 113 // kModalParamsUserGestureKey is set to false. |
94 std::string require_gesture = variations::GetVariationParamValueByFeature( | 114 std::string require_gesture = variations::GetVariationParamValueByFeature( |
95 features::kModalPermissionPrompts, kModalParamsUserGestureKey); | 115 features::kModalPermissionPrompts, kModalParamsUserGestureKey); |
96 if (require_gesture == "false") | 116 if (require_gesture == "false") |
97 return true; | 117 return true; |
98 return has_user_gesture; | 118 return has_user_gesture; |
99 } | 119 } |
100 | 120 |
101 // static | 121 // static |
102 bool PermissionDialogDelegate::RegisterPermissionDialogDelegate(JNIEnv* env) { | 122 bool PermissionDialogDelegate::RegisterPermissionDialogDelegate(JNIEnv* env) { |
103 return RegisterNativesImpl(env); | 123 return RegisterNativesImpl(env); |
104 } | 124 } |
105 | 125 |
106 void PermissionDialogDelegate::CreateJavaDelegate(JNIEnv* env) { | 126 void PermissionDialogDelegate::CreateJavaDelegate(JNIEnv* env) { |
127 base::android::ScopedJavaLocalRef<jstring> primaryButtonText = | |
128 ConvertUTF16ToJavaString(env, | |
129 l10n_util::GetStringUTF16(IDS_PERMISSION_ALLOW)); | |
130 base::android::ScopedJavaLocalRef<jstring> secondaryButtonText = | |
131 ConvertUTF16ToJavaString(env, | |
132 l10n_util::GetStringUTF16(IDS_PERMISSION_DENY)); | |
133 | |
134 if (infobar_delegate_) { | |
135 std::vector<int> content_settings_types{ | |
136 infobar_delegate_->content_settings_types()}; | |
137 | |
138 j_delegate_.Reset(Java_PermissionDialogDelegate_create( | |
139 env, reinterpret_cast<uintptr_t>(this), tab_->GetJavaObject(), | |
140 base::android::ToJavaIntArray(env, content_settings_types).obj(), | |
141 ResourceMapper::MapFromChromiumId(infobar_delegate_->GetIconId()), | |
142 ConvertUTF16ToJavaString(env, infobar_delegate_->GetMessageText()), | |
143 ConvertUTF16ToJavaString(env, infobar_delegate_->GetLinkText()), | |
144 primaryButtonText, secondaryButtonText, | |
145 infobar_delegate_->ShouldShowPersistenceToggle())); | |
146 return; | |
147 } | |
148 | |
149 // TODO(timloh): Handle grouped media permissions (camera + microphone). | |
150 DCHECK_EQ(1u, permission_prompt_->PermissionCount()); | |
151 | |
107 std::vector<int> content_settings_types{ | 152 std::vector<int> content_settings_types{ |
108 infobar_delegate_->content_settings_types()}; | 153 permission_prompt_->GetContentSettingType(0)}; |
109 | 154 |
110 j_delegate_.Reset(Java_PermissionDialogDelegate_create( | 155 j_delegate_.Reset(Java_PermissionDialogDelegate_create( |
111 env, reinterpret_cast<uintptr_t>(this), tab_->GetJavaObject(), | 156 env, reinterpret_cast<uintptr_t>(this), tab_->GetJavaObject(), |
112 base::android::ToJavaIntArray(env, content_settings_types).obj(), | 157 base::android::ToJavaIntArray(env, content_settings_types).obj(), |
113 ResourceMapper::MapFromChromiumId(infobar_delegate_->GetIconId()), | 158 ResourceMapper::MapFromChromiumId( |
114 ConvertUTF16ToJavaString(env, infobar_delegate_->GetMessageText()), | 159 permission_prompt_->GetIconIdForPermission(0)), |
115 ConvertUTF16ToJavaString(env, infobar_delegate_->GetLinkText()), | 160 // TODO(timloh): This is the wrong string. |
116 ConvertUTF16ToJavaString(env, infobar_delegate_->GetButtonLabel( | |
117 PermissionInfoBarDelegate::BUTTON_OK)), | |
118 ConvertUTF16ToJavaString(env, | 161 ConvertUTF16ToJavaString(env, |
119 infobar_delegate_->GetButtonLabel( | 162 permission_prompt_->GetMessageTextFragment(0)), |
120 PermissionInfoBarDelegate::BUTTON_CANCEL)), | 163 // TODO(timloh): Pass the actual link text for EME. |
121 infobar_delegate_->ShouldShowPersistenceToggle())); | 164 ConvertUTF16ToJavaString(env, base::string16()), primaryButtonText, |
165 secondaryButtonText, | |
166 // TODO(timloh): Hook up the persistence toggle. | |
167 false)); | |
122 } | 168 } |
123 | 169 |
124 void PermissionDialogDelegate::Accept(JNIEnv* env, | 170 void PermissionDialogDelegate::Accept(JNIEnv* env, |
125 const JavaParamRef<jobject>& obj, | 171 const JavaParamRef<jobject>& obj, |
126 jboolean persist) { | 172 jboolean persist) { |
127 if (infobar_delegate_->ShouldShowPersistenceToggle()) | 173 if (infobar_delegate_) { |
128 infobar_delegate_->set_persist(persist); | 174 if (infobar_delegate_->ShouldShowPersistenceToggle()) |
129 infobar_delegate_->Accept(); | 175 infobar_delegate_->set_persist(persist); |
176 infobar_delegate_->Accept(); | |
177 return; | |
178 } | |
179 | |
180 permission_prompt_->Accept(); | |
130 } | 181 } |
131 | 182 |
132 void PermissionDialogDelegate::Cancel(JNIEnv* env, | 183 void PermissionDialogDelegate::Cancel(JNIEnv* env, |
133 const JavaParamRef<jobject>& obj, | 184 const JavaParamRef<jobject>& obj, |
134 jboolean persist) { | 185 jboolean persist) { |
135 if (infobar_delegate_->ShouldShowPersistenceToggle()) | 186 if (infobar_delegate_) { |
136 infobar_delegate_->set_persist(persist); | 187 if (infobar_delegate_->ShouldShowPersistenceToggle()) |
137 infobar_delegate_->Cancel(); | 188 infobar_delegate_->set_persist(persist); |
189 infobar_delegate_->Cancel(); | |
190 return; | |
191 } | |
192 | |
193 permission_prompt_->Deny(); | |
138 } | 194 } |
139 | 195 |
140 void PermissionDialogDelegate::Dismissed(JNIEnv* env, | 196 void PermissionDialogDelegate::Dismissed(JNIEnv* env, |
141 const JavaParamRef<jobject>& obj) { | 197 const JavaParamRef<jobject>& obj) { |
142 infobar_delegate_->InfoBarDismissed(); | 198 if (infobar_delegate_) { |
199 infobar_delegate_->InfoBarDismissed(); | |
200 return; | |
201 } | |
202 | |
203 permission_prompt_->Closing(); | |
143 } | 204 } |
144 | 205 |
145 void PermissionDialogDelegate::LinkClicked(JNIEnv* env, | 206 void PermissionDialogDelegate::LinkClicked(JNIEnv* env, |
146 const JavaParamRef<jobject>& obj) { | 207 const JavaParamRef<jobject>& obj) { |
147 // Don't call delegate_->LinkClicked() because that relies on having an | 208 // 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 | 209 // 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). | 210 // wrapped delegate has no owner (it hasn't been added as an infobar). |
150 if (tab_->web_contents()) { | 211 if (tab_->web_contents()) { |
151 tab_->web_contents()->OpenURL(content::OpenURLParams( | 212 if (infobar_delegate_) { |
152 infobar_delegate_->GetLinkURL(), content::Referrer(), | 213 tab_->web_contents()->OpenURL(content::OpenURLParams( |
153 WindowOpenDisposition::NEW_FOREGROUND_TAB, ui::PAGE_TRANSITION_LINK, | 214 infobar_delegate_->GetLinkURL(), content::Referrer(), |
154 false)); | 215 WindowOpenDisposition::NEW_FOREGROUND_TAB, ui::PAGE_TRANSITION_LINK, |
216 false)); | |
217 } | |
218 // TODO(timloh): Show a 'learn more' link in the PermissionRequestManager | |
219 // codepath for EME. | |
155 } | 220 } |
156 } | 221 } |
157 | 222 |
158 void PermissionDialogDelegate::Destroy(JNIEnv* env, | 223 void PermissionDialogDelegate::Destroy(JNIEnv* env, |
159 const JavaParamRef<jobject>& obj) { | 224 const JavaParamRef<jobject>& obj) { |
160 j_delegate_ = nullptr; | 225 j_delegate_ = nullptr; |
161 delete this; | 226 delete this; |
162 } | 227 } |
163 | 228 |
164 PermissionDialogDelegate::PermissionDialogDelegate( | 229 PermissionDialogDelegate::PermissionDialogDelegate( |
165 TabAndroid* tab, | 230 TabAndroid* tab, |
166 std::unique_ptr<PermissionInfoBarDelegate> infobar_delegate) | 231 std::unique_ptr<PermissionInfoBarDelegate> infobar_delegate, |
167 : content::WebContentsObserver(tab->web_contents()), | 232 PermissionPromptAndroid* permission_prompt) |
168 tab_(tab), | 233 : tab_(tab), |
169 infobar_delegate_(std::move(infobar_delegate)) { | 234 infobar_delegate_(std::move(infobar_delegate)), |
235 permission_prompt_(permission_prompt) { | |
170 DCHECK(tab_); | 236 DCHECK(tab_); |
171 DCHECK(infobar_delegate_); | 237 // Only one of the PermissionPromptAndroid and PermissionInfoBarDelegate is |
238 // used, depending on whether the PermissionRequestManager is enabled or not. | |
239 DCHECK(!!permission_prompt_ ^ !!infobar_delegate_); | |
172 | 240 |
173 // Create our Java counterpart, which manages our lifetime. | 241 // Create our Java counterpart, which manages our lifetime. |
174 JNIEnv* env = base::android::AttachCurrentThread(); | 242 JNIEnv* env = base::android::AttachCurrentThread(); |
175 CreateJavaDelegate(env); | 243 CreateJavaDelegate(env); |
176 | 244 |
177 // Send the Java delegate to the Java PermissionDialogController for display. | 245 // Send the Java delegate to the Java PermissionDialogController for display. |
178 // The controller takes over lifetime management; when the Java delegate is no | 246 // The controller takes over lifetime management; when the Java delegate is no |
179 // longer needed it will in turn free the native delegate. | 247 // longer needed it will in turn free the native delegate. |
180 Java_PermissionDialogController_createDialog(env, j_delegate_.obj()); | 248 Java_PermissionDialogController_createDialog(env, j_delegate_.obj()); |
181 } | 249 } |
(...skipping 14 matching lines...) Expand all Loading... | |
196 } | 264 } |
197 | 265 |
198 DismissDialog(); | 266 DismissDialog(); |
199 delete this; | 267 delete this; |
200 } | 268 } |
201 | 269 |
202 void PermissionDialogDelegate::WebContentsDestroyed() { | 270 void PermissionDialogDelegate::WebContentsDestroyed() { |
203 DismissDialog(); | 271 DismissDialog(); |
204 delete this; | 272 delete this; |
205 } | 273 } |
OLD | NEW |