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 "base/feature_list.h" | |
12 #include "build/build_config.h" | |
13 #include "chrome/browser/android/resource_mapper.h" | |
14 #include "chrome/browser/geolocation/geolocation_infobar_delegate_android.h" | |
15 #include "chrome/browser/media/midi_permission_infobar_delegate_android.h" | |
16 #include "chrome/browser/media/protected_media_identifier_infobar_delegate_andro id.h" | |
17 #include "chrome/browser/notifications/notification_permission_infobar_delegate. h" | |
18 #include "chrome/common/chrome_features.h" | |
19 #include "components/variations/variations_associated_data.h" | |
20 #include "content/public/browser/android/content_view_core.h" | |
21 #include "content/public/browser/web_contents.h" | |
22 #include "jni/PermissionDialogController_jni.h" | |
23 #include "jni/PermissionDialogDelegate_jni.h" | |
24 #include "ui/android/window_android.h" | |
25 #include "ui/base/window_open_disposition.h" | |
26 | |
27 using base::android::ConvertUTF16ToJavaString; | |
28 | |
29 namespace { | |
30 | |
31 // Key for querying variations for whether a modal should require a gesture. | |
32 const char kModalParamsUserGestureKey[] = "require_gesture"; | |
33 | |
34 } | |
35 | |
36 // static | |
37 void PermissionDialogDelegate::Create( | |
38 content::PermissionType type, | |
39 content::WebContents* web_contents, | |
40 const GURL& requesting_frame, | |
41 bool user_gesture, | |
42 Profile* profile, | |
43 const PermissionSetCallback& callback) { | |
44 DCHECK(web_contents); | |
45 | |
46 std::unique_ptr<PermissionInfoBarDelegate> infobar_delegate = nullptr; | |
47 switch (type) { | |
48 case content::PermissionType::GEOLOCATION: | |
49 infobar_delegate.reset(new GeolocationInfoBarDelegateAndroid( | |
50 requesting_frame, user_gesture, profile, callback)); | |
51 break; | |
52 #if defined(ENABLE_NOTIFICATIONS) | |
53 case content::PermissionType::NOTIFICATIONS: | |
54 case content::PermissionType::PUSH_MESSAGING: | |
55 infobar_delegate.reset(new NotificationPermissionInfoBarDelegate( | |
56 requesting_frame, user_gesture, profile, callback)); | |
gone
2016/10/27 20:33:35
indentation is off here and below
dominickn
2016/10/27 23:37:13
Done.
| |
57 break; | |
58 #endif // ENABLE_NOTIFICATIONS | |
59 case content::PermissionType::MIDI_SYSEX: | |
60 infobar_delegate.reset(new MidiPermissionInfoBarDelegateAndroid( | |
61 requesting_frame, user_gesture, profile, callback)); | |
62 break; | |
63 case content::PermissionType::PROTECTED_MEDIA_IDENTIFIER: | |
64 infobar_delegate.reset(new ProtectedMediaIdentifierInfoBarDelegateAndroid( | |
65 requesting_frame, user_gesture, profile, callback)); | |
66 break; | |
67 default: | |
68 NOTREACHED(); | |
69 } | |
70 | |
71 PermissionDialogDelegate* dialog_delegate = | |
72 new PermissionDialogDelegate(web_contents, std::move(infobar_delegate)); | |
73 | |
74 JNIEnv* env = base::android::AttachCurrentThread(); | |
75 base::android::ScopedJavaLocalRef<jobject> j_delegate = | |
76 dialog_delegate->CreateJavaDelegate(env); | |
77 | |
78 // Java queues the dialog request if neccesary, and owns the the lifetime of | |
79 // the delegate. | |
gone
2016/10/27 20:33:35
Comment needs reworking. Not sure you can own the
dominickn
2016/10/27 23:37:13
Done.
| |
80 Java_PermissionDialogController_createDialog(env, j_delegate.obj()); | |
81 } | |
82 | |
83 // static | |
84 bool PermissionDialogDelegate::ShouldShowModalPrompt(bool has_user_gesture) { | |
85 if (!base::FeatureList::IsEnabled(features::kModalPermissionPrompts)) | |
86 return false; | |
87 | |
88 // Enforce a user gesture requirement by default. Only relax this if | |
89 // variations has a require_gesture key, and that key is one of 0 or false. | |
90 std::string require_gesture = variations::GetVariationParamValueByFeature( | |
91 features::kModalPermissionPrompts, kModalParamsUserGestureKey); | |
92 if (require_gesture == "0" || require_gesture == "false") | |
93 return true; | |
94 return has_user_gesture; | |
95 } | |
96 | |
97 // static | |
98 bool PermissionDialogDelegate::RegisterPermissionDialogDelegate(JNIEnv* env) { | |
99 return RegisterNativesImpl(env); | |
100 } | |
101 | |
102 ScopedJavaLocalRef<jobject> PermissionDialogDelegate::CreateJavaDelegate( | |
103 JNIEnv* env) { | |
104 content::ContentViewCore* cvc = | |
105 content::ContentViewCore::FromWebContents(web_contents()); | |
106 DCHECK(cvc); | |
107 | |
108 return Java_PermissionDialogDelegate_create( | |
109 env, reinterpret_cast<uintptr_t>(this), | |
110 cvc->GetWindowAndroid()->GetJavaObject(), | |
111 ResourceMapper::MapFromChromiumId(infobar_delegate_->GetIconId()), | |
112 ConvertUTF16ToJavaString(env, infobar_delegate_->GetMessageText()), | |
113 ConvertUTF16ToJavaString(env, infobar_delegate_->GetLinkText()), | |
114 ConvertUTF16ToJavaString(env, infobar_delegate_->GetButtonLabel( | |
115 PermissionInfoBarDelegate::BUTTON_OK)), | |
116 ConvertUTF16ToJavaString(env, | |
117 infobar_delegate_->GetButtonLabel( | |
118 PermissionInfoBarDelegate::BUTTON_CANCEL)), | |
119 infobar_delegate_->ShouldShowPersistenceToggle()); | |
120 } | |
121 | |
122 void PermissionDialogDelegate::Accept(JNIEnv* env, | |
123 const JavaParamRef<jobject>& obj, | |
124 jboolean persist) { | |
125 if (infobar_delegate_->ShouldShowPersistenceToggle()) | |
126 infobar_delegate_->set_persist(persist); | |
127 infobar_delegate_->Accept(); | |
128 } | |
129 | |
130 void PermissionDialogDelegate::Cancel(JNIEnv* env, | |
131 const JavaParamRef<jobject>& obj, | |
132 jboolean persist) { | |
133 if (infobar_delegate_->ShouldShowPersistenceToggle()) | |
134 infobar_delegate_->set_persist(persist); | |
135 infobar_delegate_->Cancel(); | |
136 } | |
137 | |
138 void PermissionDialogDelegate::Dismissed(JNIEnv* env, | |
139 const JavaParamRef<jobject>& obj) { | |
140 infobar_delegate_->InfoBarDismissed(); | |
141 } | |
142 | |
143 void PermissionDialogDelegate::LinkClicked(JNIEnv* env, | |
144 const JavaParamRef<jobject>& obj) { | |
145 // Don't call delegate_->LinkClicked() because that relies on having an | |
146 // InfoBarService as an owner() to open the link. That will fail since the | |
147 // wrapped delegate has no owner (is hasn't been added as an infobar). | |
gone
2016/10/27 20:33:35
is hasn't
dominickn
2016/10/27 23:37:13
Done.
| |
148 if (web_contents()) { | |
149 web_contents()->OpenURL(content::OpenURLParams( | |
150 infobar_delegate_->GetLinkURL(), content::Referrer(), | |
151 WindowOpenDisposition::NEW_FOREGROUND_TAB, ui::PAGE_TRANSITION_LINK, | |
152 false)); | |
153 } | |
154 } | |
155 | |
156 void PermissionDialogDelegate::Destroy(JNIEnv* env, | |
157 const JavaParamRef<jobject>& obj) { | |
158 delete this; | |
159 } | |
160 | |
161 PermissionDialogDelegate::PermissionDialogDelegate( | |
162 content::WebContents* web_contents, | |
163 std::unique_ptr<PermissionInfoBarDelegate> infobar_delegate) | |
164 : content::WebContentsObserver(web_contents), | |
165 infobar_delegate_(std::move(infobar_delegate)) { | |
166 DCHECK(infobar_delegate_); | |
167 } | |
168 | |
169 PermissionDialogDelegate::~PermissionDialogDelegate() {} | |
OLD | NEW |