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_devices_controller.h" | 19 #include "chrome/browser/media/webrtc/media_stream_devices_controller.h" |
20 #include "chrome/browser/media/webrtc/media_stream_infobar_delegate_android.h" | 20 #include "chrome/browser/media/webrtc/media_stream_infobar_delegate_android.h" |
21 #include "chrome/browser/notifications/notification_permission_infobar_delegate. h" | 21 #include "chrome/browser/notifications/notification_permission_infobar_delegate. h" |
22 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
23 #include "chrome/common/chrome_features.h" | 23 #include "chrome/common/chrome_features.h" |
24 #include "chrome/grit/generated_resources.h" | |
24 #include "components/variations/variations_associated_data.h" | 25 #include "components/variations/variations_associated_data.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) { |
raymes
2017/04/11 02:50:35
Is it worth DCHECKing that the PermissionRequestMa
Timothy Loh
2017/05/24 03:30:22
I'd prefer to not add the dependency.
| |
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, permission_prompt); | |
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, PermissionInfoBarDelegate::CreateDelegate( |
60 type, requesting_frame, user_gesture, profile, callback)); | 78 type, requesting_frame, user_gesture, profile, callback)); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
99 return has_user_gesture; | 117 return has_user_gesture; |
100 } | 118 } |
101 | 119 |
102 // static | 120 // static |
103 bool PermissionDialogDelegate::RegisterPermissionDialogDelegate(JNIEnv* env) { | 121 bool PermissionDialogDelegate::RegisterPermissionDialogDelegate(JNIEnv* env) { |
104 return RegisterNativesImpl(env); | 122 return RegisterNativesImpl(env); |
105 } | 123 } |
106 | 124 |
107 ScopedJavaLocalRef<jobject> PermissionDialogDelegate::CreateJavaDelegate( | 125 ScopedJavaLocalRef<jobject> PermissionDialogDelegate::CreateJavaDelegate( |
108 JNIEnv* env) { | 126 JNIEnv* env) { |
127 ScopedJavaLocalRef<jstring> primaryButtonText = ConvertUTF16ToJavaString( | |
128 env, l10n_util::GetStringUTF16(IDS_PERMISSION_ALLOW)); | |
129 ScopedJavaLocalRef<jstring> secondaryButtonText = ConvertUTF16ToJavaString( | |
130 env, l10n_util::GetStringUTF16(IDS_PERMISSION_DENY)); | |
131 | |
132 if (infobar_delegate_) { | |
133 std::vector<int> content_settings_types{ | |
134 infobar_delegate_->content_settings_types()}; | |
135 | |
136 return Java_PermissionDialogDelegate_create( | |
137 env, reinterpret_cast<uintptr_t>(this), tab_->GetJavaObject(), | |
138 base::android::ToJavaIntArray(env, content_settings_types).obj(), | |
139 ResourceMapper::MapFromChromiumId(infobar_delegate_->GetIconId()), | |
140 ConvertUTF16ToJavaString(env, infobar_delegate_->GetMessageText()), | |
141 ConvertUTF16ToJavaString(env, infobar_delegate_->GetLinkText()), | |
142 primaryButtonText, secondaryButtonText, | |
raymes
2017/04/11 02:50:35
Does this change the button labels for the old cod
Timothy Loh
2017/05/24 03:30:22
I just inlined the logic of GetButtonLabel so the
| |
143 infobar_delegate_->ShouldShowPersistenceToggle()); | |
144 } | |
145 | |
146 // TODO(timloh): Handle grouped media permissions (camera + microphone). | |
147 DCHECK_EQ(1u, permission_prompt_->permission_count()); | |
148 | |
109 std::vector<int> content_settings_types{ | 149 std::vector<int> content_settings_types{ |
110 infobar_delegate_->content_settings_types()}; | 150 permission_prompt_->GetContentSettingType(0)}; |
111 | 151 |
112 return Java_PermissionDialogDelegate_create( | 152 return Java_PermissionDialogDelegate_create( |
113 env, reinterpret_cast<uintptr_t>(this), | 153 env, reinterpret_cast<uintptr_t>(this), tab_->GetJavaObject(), |
114 tab_->GetJavaObject(), | |
115 base::android::ToJavaIntArray(env, content_settings_types).obj(), | 154 base::android::ToJavaIntArray(env, content_settings_types).obj(), |
116 ResourceMapper::MapFromChromiumId(infobar_delegate_->GetIconId()), | 155 ResourceMapper::MapFromChromiumId( |
117 ConvertUTF16ToJavaString(env, infobar_delegate_->GetMessageText()), | 156 permission_prompt_->GetIconIdForPermission(0)), |
118 ConvertUTF16ToJavaString(env, infobar_delegate_->GetLinkText()), | 157 // TODO(timloh): This is the wrong string. |
119 ConvertUTF16ToJavaString(env, infobar_delegate_->GetButtonLabel( | |
120 PermissionInfoBarDelegate::BUTTON_OK)), | |
121 ConvertUTF16ToJavaString(env, | 158 ConvertUTF16ToJavaString(env, |
122 infobar_delegate_->GetButtonLabel( | 159 permission_prompt_->GetMessageTextFragment(0)), |
123 PermissionInfoBarDelegate::BUTTON_CANCEL)), | 160 // TODO(timloh): Pass the actual link text for EME. |
124 infobar_delegate_->ShouldShowPersistenceToggle()); | 161 ConvertUTF16ToJavaString(env, base::string16()), primaryButtonText, |
162 secondaryButtonText, | |
163 // TODO(timloh): Hook up the persistence toggle. | |
164 false); | |
125 } | 165 } |
126 | 166 |
127 void PermissionDialogDelegate::Accept(JNIEnv* env, | 167 void PermissionDialogDelegate::Accept(JNIEnv* env, |
128 const JavaParamRef<jobject>& obj, | 168 const JavaParamRef<jobject>& obj, |
129 jboolean persist) { | 169 jboolean persist) { |
130 if (infobar_delegate_->ShouldShowPersistenceToggle()) | 170 if (infobar_delegate_) { |
131 infobar_delegate_->set_persist(persist); | 171 if (infobar_delegate_->ShouldShowPersistenceToggle()) |
132 infobar_delegate_->Accept(); | 172 infobar_delegate_->set_persist(persist); |
173 infobar_delegate_->Accept(); | |
174 return; | |
175 } | |
176 | |
177 permission_prompt_->Accept(); | |
raymes
2017/04/11 02:50:35
nit: Should we add TODOs here and below to hook up
Timothy Loh
2017/05/24 03:30:22
Don't think it's needed.
| |
133 } | 178 } |
134 | 179 |
135 void PermissionDialogDelegate::Cancel(JNIEnv* env, | 180 void PermissionDialogDelegate::Cancel(JNIEnv* env, |
136 const JavaParamRef<jobject>& obj, | 181 const JavaParamRef<jobject>& obj, |
137 jboolean persist) { | 182 jboolean persist) { |
138 if (infobar_delegate_->ShouldShowPersistenceToggle()) | 183 if (infobar_delegate_) { |
139 infobar_delegate_->set_persist(persist); | 184 if (infobar_delegate_->ShouldShowPersistenceToggle()) |
140 infobar_delegate_->Cancel(); | 185 infobar_delegate_->set_persist(persist); |
186 infobar_delegate_->Cancel(); | |
187 return; | |
188 } | |
189 | |
190 permission_prompt_->Deny(); | |
141 } | 191 } |
142 | 192 |
143 void PermissionDialogDelegate::Dismissed(JNIEnv* env, | 193 void PermissionDialogDelegate::Dismissed(JNIEnv* env, |
144 const JavaParamRef<jobject>& obj) { | 194 const JavaParamRef<jobject>& obj) { |
145 infobar_delegate_->InfoBarDismissed(); | 195 if (infobar_delegate_) { |
196 infobar_delegate_->InfoBarDismissed(); | |
197 return; | |
198 } | |
199 | |
200 permission_prompt_->Closing(); | |
146 } | 201 } |
147 | 202 |
148 void PermissionDialogDelegate::LinkClicked(JNIEnv* env, | 203 void PermissionDialogDelegate::LinkClicked(JNIEnv* env, |
149 const JavaParamRef<jobject>& obj) { | 204 const JavaParamRef<jobject>& obj) { |
150 // Don't call delegate_->LinkClicked() because that relies on having an | 205 // Don't call delegate_->LinkClicked() because that relies on having an |
151 // InfoBarService as an owner() to open the link. That will fail since the | 206 // InfoBarService as an owner() to open the link. That will fail since the |
152 // wrapped delegate has no owner (it hasn't been added as an infobar). | 207 // wrapped delegate has no owner (it hasn't been added as an infobar). |
153 if (tab_->web_contents()) { | 208 if (tab_->web_contents()) { |
154 tab_->web_contents()->OpenURL(content::OpenURLParams( | 209 if (infobar_delegate_) { |
155 infobar_delegate_->GetLinkURL(), content::Referrer(), | 210 tab_->web_contents()->OpenURL(content::OpenURLParams( |
156 WindowOpenDisposition::NEW_FOREGROUND_TAB, ui::PAGE_TRANSITION_LINK, | 211 infobar_delegate_->GetLinkURL(), content::Referrer(), |
157 false)); | 212 WindowOpenDisposition::NEW_FOREGROUND_TAB, ui::PAGE_TRANSITION_LINK, |
213 false)); | |
214 } | |
215 // TODO(timloh): Show a 'learn more' link in the PermissionRequestManager | |
216 // codepath for EME. | |
158 } | 217 } |
159 } | 218 } |
160 | 219 |
161 void PermissionDialogDelegate::Destroy(JNIEnv* env, | 220 void PermissionDialogDelegate::Destroy(JNIEnv* env, |
162 const JavaParamRef<jobject>& obj) { | 221 const JavaParamRef<jobject>& obj) { |
163 delete this; | 222 delete this; |
164 } | 223 } |
165 | 224 |
166 PermissionDialogDelegate::PermissionDialogDelegate( | 225 PermissionDialogDelegate::PermissionDialogDelegate( |
167 TabAndroid* tab, | 226 TabAndroid* tab, |
227 PermissionPromptAndroid* permission_prompt) | |
228 : tab_(tab), | |
229 infobar_delegate_(nullptr), | |
230 permission_prompt_(permission_prompt) { | |
231 DCHECK(tab_); | |
232 DCHECK(permission_prompt_); | |
233 Init(); | |
234 } | |
235 | |
236 PermissionDialogDelegate::PermissionDialogDelegate( | |
237 TabAndroid* tab, | |
168 std::unique_ptr<PermissionInfoBarDelegate> infobar_delegate) | 238 std::unique_ptr<PermissionInfoBarDelegate> infobar_delegate) |
169 : tab_(tab), infobar_delegate_(std::move(infobar_delegate)) { | 239 : tab_(tab), |
240 infobar_delegate_(std::move(infobar_delegate)), | |
241 permission_prompt_(nullptr) { | |
170 DCHECK(tab_); | 242 DCHECK(tab_); |
171 DCHECK(infobar_delegate_); | 243 DCHECK(infobar_delegate_); |
244 Init(); | |
245 } | |
raymes
2017/04/11 02:50:35
nit: should we just merge these 2 constructors tog
Timothy Loh
2017/05/24 03:30:22
Sure, done.
| |
172 | 246 |
247 PermissionDialogDelegate::~PermissionDialogDelegate() {} | |
248 | |
249 void PermissionDialogDelegate::Init() { | |
173 // Create our Java counterpart, which manages our lifetime. | 250 // Create our Java counterpart, which manages our lifetime. |
174 JNIEnv* env = base::android::AttachCurrentThread(); | 251 JNIEnv* env = base::android::AttachCurrentThread(); |
175 base::android::ScopedJavaLocalRef<jobject> j_delegate = | 252 base::android::ScopedJavaLocalRef<jobject> j_delegate = |
176 CreateJavaDelegate(env); | 253 CreateJavaDelegate(env); |
177 | 254 |
178 // Send the Java delegate to the Java PermissionDialogController for display. | 255 // Send the Java delegate to the Java PermissionDialogController for display. |
179 // The controller takes over lifetime management; when the Java delegate is no | 256 // The controller takes over lifetime management; when the Java delegate is no |
180 // longer needed it will in turn free the native delegate. | 257 // longer needed it will in turn free the native delegate. |
181 Java_PermissionDialogController_createDialog(env, j_delegate.obj()); | 258 Java_PermissionDialogController_createDialog(env, j_delegate.obj()); |
182 } | 259 } |
183 | |
184 PermissionDialogDelegate::~PermissionDialogDelegate() {} | |
OLD | NEW |