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

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

Issue 1240923002: Add an infobar to resolve android runtime permission mismatches. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 5 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
(Empty)
1 // Copyright 2015 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_update_infobar_delegate_android. h"
6
7 #include <string>
8
9 #include "base/android/jni_array.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "chrome/browser/android/preferences/pref_service_bridge.h"
12 #include "chrome/browser/infobars/infobar_service.h"
13 #include "chrome/grit/chromium_strings.h"
14 #include "chrome/grit/generated_resources.h"
15 #include "chrome/grit/theme_resources.h"
16 #include "components/infobars/core/infobar.h"
17 #include "content/public/browser/android/content_view_core.h"
18 #include "content/public/browser/web_contents.h"
19 #include "jni/PermissionUpdateInfoBarDelegate_jni.h"
20 #include "ui/android/window_android.h"
21 #include "ui/base/l10n/l10n_util.h"
22
23 // static
24 infobars::InfoBar* PermissionUpdateInfoBarDelegate::Create(
25 content::WebContents* web_contents,
26 const std::vector<ContentSettingsType>& content_settings_types,
27 const PermissionUpdatedCallback& callback) {
28 DCHECK(ShouldShowPermissionInfobar(web_contents, content_settings_types))
29 << "Caller shoudl check ShouldShowPermissionInfobar before creating the "
gone 2015/07/17 17:41:40 you shoudl fix this typo
Ted C 2015/07/17 20:37:18 :-) Dnoe
30 << "infobar.";
31
32 InfoBarService* infobar_service =
33 InfoBarService::FromWebContents(web_contents);
34 if (!infobar_service) {
35 callback.Run(false);
36 return nullptr;
37 }
38
39 // Trim the list of content settings types to just those that are missing the
40 // corresponding android permission.
gone 2015/07/17 17:41:39 what happens if the infobar is already on screen w
Ted C 2015/07/17 20:37:18 Fair point. I removed the trimming here and propa
41 content::ContentViewCore* cvc =
42 content::ContentViewCore::FromWebContents(web_contents);
43 ui::WindowAndroid* window_android = cvc->GetWindowAndroid();
44 std::vector<ContentSettingsType> invalid_content_settings_types;
45 for (ContentSettingsType content_settings_type : content_settings_types) {
46 std::string android_permission =
47 PrefServiceBridge::GetAndroidPermissionForContentSetting(
48 content_settings_type);
49
50 if (!android_permission.empty() &&
51 !window_android->HasPermission(android_permission)) {
52 invalid_content_settings_types.push_back(content_settings_type);
53 }
54 }
55
56 if (invalid_content_settings_types.empty()) {
57 NOTREACHED();
gone 2015/07/17 17:41:40 does a NOTREACHED() crash, or is that only on debu
Ted C 2015/07/17 20:37:18 It is a DCHECK(false), so only debug builds.
58 callback.Run(false);
59 return nullptr;
60 }
61
62 return infobar_service->AddInfoBar(infobar_service->CreateConfirmInfoBar(
63 scoped_ptr<ConfirmInfoBarDelegate>(new PermissionUpdateInfoBarDelegate(
64 web_contents, invalid_content_settings_types, callback))));
65 }
66
67 // static
68 bool PermissionUpdateInfoBarDelegate::ShouldShowPermissionInfobar(
69 content::WebContents* web_contents,
70 const std::vector<ContentSettingsType>& content_settings_types) {
71 if (!web_contents)
72 return false;
73
74 content::ContentViewCore* cvc =
75 content::ContentViewCore::FromWebContents(web_contents);
76 if (!cvc || !cvc->GetWindowAndroid())
77 return false;
78 ui::WindowAndroid* window_android = cvc->GetWindowAndroid();
79
80 for (ContentSettingsType content_settings_type : content_settings_types) {
81 std::string android_permission =
82 PrefServiceBridge::GetAndroidPermissionForContentSetting(
83 content_settings_type);
84
85 if (!android_permission.empty() &&
86 !window_android->HasPermission(android_permission)) {
87 return true;
88 }
89 }
90
91 return false;
92 }
93
94 // static
95 bool PermissionUpdateInfoBarDelegate::RegisterPermissionUpdateInfoBarDelegate(
96 JNIEnv* env) {
97 return RegisterNativesImpl(env);
98 }
99
100 void PermissionUpdateInfoBarDelegate::OnPermissionResult(
101 JNIEnv* env, jobject obj, jboolean all_permissions_granted) {
102 callback_.Run(all_permissions_granted);
103 infobar()->RemoveSelf();
104 }
105
106 PermissionUpdateInfoBarDelegate::PermissionUpdateInfoBarDelegate(
107 content::WebContents* web_contents,
108 const std::vector<ContentSettingsType>& content_settings_types,
109 const PermissionUpdatedCallback& callback)
110 : ConfirmInfoBarDelegate(),
111 content_settings_types_(content_settings_types),
112 callback_(callback) {
113
gone 2015/07/17 17:41:40 nit: intentional newline?
Ted C 2015/07/17 20:37:18 Nope...removed
114 std::vector<int> content_settings_type_values;
115 for (ContentSettingsType type : content_settings_types)
116 content_settings_type_values.push_back(type);
117
118 JNIEnv* env = base::android::AttachCurrentThread();
119 java_delegate_.Reset(Java_PermissionUpdateInfoBarDelegate_create(
120 env,
121 reinterpret_cast<intptr_t>(this),
122 web_contents->GetJavaWebContents().obj(),
123 base::android::ToJavaIntArray(env, content_settings_type_values).obj()));
124 }
125
126 PermissionUpdateInfoBarDelegate::~PermissionUpdateInfoBarDelegate() {
127 Java_PermissionUpdateInfoBarDelegate_onNativeDestroyed(
128 base::android::AttachCurrentThread(), java_delegate_.obj());
129 }
130
131 int PermissionUpdateInfoBarDelegate::GetIconID() const {
132 return IDR_INFOBAR_WARNING;
133 }
134
135 base::string16 PermissionUpdateInfoBarDelegate::GetMessageText() const {
136 if (content_settings_types_.size() > 1) {
137 return l10n_util::GetStringUTF16(
138 IDS_INFOBAR_MISSING_MULTIPLE_PERMISSIONS_TEXT);
139 }
140
141 int message_id;
142 switch (content_settings_types_[0]) {
143 case CONTENT_SETTINGS_TYPE_GEOLOCATION:
144 message_id = IDS_INFOBAR_MISSING_LOCATION_PERMISSION_TEXT;
145 break;
146 case CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC:
147 message_id = IDS_INFOBAR_MISSING_MICROPHONE_PERMISSION_TEXT;
148 break;
149 case CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA:
150 message_id = IDS_INFOBAR_MISSING_CAMERA_PERMISSION_TEXT;
151 break;
152 default:
153 NOTREACHED();
154 message_id = IDS_INFOBAR_MISSING_MULTIPLE_PERMISSIONS_TEXT;
155 }
156
157 return l10n_util::GetStringUTF16(message_id);
158 }
159
160 int PermissionUpdateInfoBarDelegate::GetButtons() const {
161 return BUTTON_OK;
162 }
163
164 base::string16 PermissionUpdateInfoBarDelegate::GetButtonLabel(
165 InfoBarButton button) const {
166 DCHECK_EQ(button, BUTTON_OK);
167 return l10n_util::GetStringUTF16(IDS_INFOBAR_UPDATE_PERMISSIONS_BUTTON_TEXT);
168 }
169
170 bool PermissionUpdateInfoBarDelegate::Accept() {
171 Java_PermissionUpdateInfoBarDelegate_requestPermissions(
172 base::android::AttachCurrentThread(), java_delegate_.obj());
173 return false;
174 }
175
176 bool PermissionUpdateInfoBarDelegate::Cancel() {
177 callback_.Run(false);
178 return true;
179 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698