OLD | NEW |
(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_infobar_request.h" |
| 6 |
| 7 #include "base/prefs/pref_service.h" |
| 8 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 9 #include "chrome/browser/geolocation/geolocation_infobar_delegate_android.h" |
| 10 #include "chrome/browser/infobars/infobar_service.h" |
| 11 #include "chrome/browser/media/midi_permission_infobar_delegate_android.h" |
| 12 #include "chrome/browser/media/protected_media_identifier_infobar_delegate_andro
id.h" |
| 13 #include "chrome/browser/notifications/notification_permission_infobar_delegate.
h" |
| 14 #include "chrome/browser/permissions/permission_manager.h" |
| 15 #include "chrome/browser/permissions/permission_manager_factory.h" |
| 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/storage/durable_storage_permission_infobar_delegate_and
roid.h" |
| 18 #include "chrome/common/pref_names.h" |
| 19 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 20 #include "components/infobars/core/infobar.h" |
| 21 #include "content/public/browser/browser_thread.h" |
| 22 #include "content/public/browser/permission_type.h" |
| 23 #include "content/public/browser/web_contents.h" |
| 24 #include "content/public/common/permission_status.mojom.h" |
| 25 |
| 26 using content::PermissionStatus; |
| 27 using content::PermissionType; |
| 28 |
| 29 namespace { |
| 30 |
| 31 PermissionType ContentSettingsTypeToPermissionType(ContentSettingsType type) { |
| 32 switch (type) { |
| 33 case CONTENT_SETTINGS_TYPE_MIDI_SYSEX: |
| 34 return PermissionType::MIDI_SYSEX; |
| 35 case CONTENT_SETTINGS_TYPE_NOTIFICATIONS: |
| 36 return PermissionType::NOTIFICATIONS; |
| 37 case CONTENT_SETTINGS_TYPE_GEOLOCATION: |
| 38 return PermissionType::GEOLOCATION; |
| 39 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) |
| 40 case CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER: |
| 41 return PermissionType::PROTECTED_MEDIA_IDENTIFIER; |
| 42 #endif |
| 43 case CONTENT_SETTINGS_TYPE_DURABLE_STORAGE: |
| 44 return PermissionType::DURABLE_STORAGE; |
| 45 default: |
| 46 NOTREACHED(); |
| 47 break; |
| 48 } |
| 49 return PermissionType::GEOLOCATION; |
| 50 } |
| 51 |
| 52 ContentSetting PermissionStatusToContentSetting(PermissionStatus status) { |
| 53 switch (status) { |
| 54 case content::PERMISSION_STATUS_GRANTED: |
| 55 return CONTENT_SETTING_ALLOW; |
| 56 case content::PERMISSION_STATUS_DENIED: |
| 57 return CONTENT_SETTING_BLOCK; |
| 58 case content::PERMISSION_STATUS_ASK: |
| 59 return CONTENT_SETTING_ASK; |
| 60 default: |
| 61 NOTREACHED(); |
| 62 break; |
| 63 } |
| 64 return CONTENT_SETTING_BLOCK; |
| 65 } |
| 66 |
| 67 } // anonymous namespace |
| 68 |
| 69 PermissionInfoBarRequest::PermissionRequest::PermissionRequest( |
| 70 ContentSettingsType type, |
| 71 const PermissionDecidedCallback& callback) |
| 72 : type_(type), |
| 73 callback_(callback) { |
| 74 } |
| 75 |
| 76 PermissionInfoBarRequest::PermissionRequest::~PermissionRequest() { |
| 77 } |
| 78 |
| 79 PermissionInfoBarRequest::PermissionInfoBarRequest( |
| 80 int request_id, |
| 81 const GURL& requesting_origin, |
| 82 const GURL& embedding_origin, |
| 83 const base::Closure& callback) |
| 84 : infobar_(nullptr), |
| 85 request_id_(request_id), |
| 86 requesting_origin_(requesting_origin), |
| 87 embedding_origin_(embedding_origin), |
| 88 callback_(callback), |
| 89 weak_factory_(this) { |
| 90 } |
| 91 |
| 92 PermissionInfoBarRequest::~PermissionInfoBarRequest() { |
| 93 } |
| 94 |
| 95 void PermissionInfoBarRequest::AddPermission( |
| 96 const ContentSettingsType type, |
| 97 const PermissionDecidedCallback& callback) { |
| 98 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 99 DCHECK(!infobar_); |
| 100 |
| 101 requests_.push_back(PermissionRequest(type, callback)); |
| 102 } |
| 103 |
| 104 bool PermissionInfoBarRequest::ShowInfobar( |
| 105 content::WebContents* web_contents) { |
| 106 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 107 DCHECK(!infobar_); |
| 108 |
| 109 Profile* profile = |
| 110 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 111 DCHECK(profile); |
| 112 |
| 113 // After pruning if there is nothing left then just ignore this request. |
| 114 PruneAnsweredPermissions(profile); |
| 115 if (requests_.size() == 0) |
| 116 return false; |
| 117 |
| 118 InfoBarService* infobar_service = |
| 119 InfoBarService::FromWebContents(web_contents); |
| 120 DCHECK(infobar_service); |
| 121 |
| 122 std::string display_languages = |
| 123 profile->GetPrefs()->GetString(prefs::kAcceptLanguages); |
| 124 if (requests_.size() == 1) { |
| 125 CreateInfoBar( |
| 126 infobar_service, |
| 127 requests_[0], |
| 128 display_languages, |
| 129 base::Bind(&PermissionInfoBarRequest::OnPermissionSet, |
| 130 weak_factory_.GetWeakPtr())); |
| 131 return true; |
| 132 } |
| 133 |
| 134 // TODO(lalitm) once multiple permissions is ready to land, this |
| 135 // should be implemented properly. |
| 136 NOTIMPLEMENTED(); |
| 137 for (size_t i = 0; i < requests_.size(); ++i) |
| 138 requests_[i].callback().Run(false, CONTENT_SETTING_DEFAULT); |
| 139 return false; |
| 140 } |
| 141 |
| 142 void PermissionInfoBarRequest::AcceptForTests() { |
| 143 OnCancel(); |
| 144 OnPermissionsSet(true, |
| 145 std::vector<ContentSetting>(requests_.size(), CONTENT_SETTING_ALLOW)); |
| 146 } |
| 147 |
| 148 void PermissionInfoBarRequest::ClosingForTests() { |
| 149 OnCancel(); |
| 150 OnPermissionsSet(false, |
| 151 std::vector<ContentSetting>(requests_.size(), CONTENT_SETTING_DEFAULT)); |
| 152 } |
| 153 |
| 154 void PermissionInfoBarRequest::Cancel() { |
| 155 OnCancel(); |
| 156 callback_.Run(); |
| 157 } |
| 158 |
| 159 void PermissionInfoBarRequest::OnCancel() { |
| 160 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 161 DCHECK(infobar_); |
| 162 |
| 163 infobar_->RemoveSelf(); |
| 164 infobar_ = nullptr; |
| 165 } |
| 166 |
| 167 void PermissionInfoBarRequest::OnPermissionSet( |
| 168 bool update_content_setting, |
| 169 bool allowed) { |
| 170 OnPermissionsSet(update_content_setting, |
| 171 std::vector<ContentSetting>(1, |
| 172 allowed ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK)); |
| 173 } |
| 174 |
| 175 void PermissionInfoBarRequest::OnPermissionsSet( |
| 176 bool update_content_setting, |
| 177 const std::vector<ContentSetting>& result) { |
| 178 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 179 |
| 180 // Set the infobar to nullptr so there is no attempt to remove it later. |
| 181 infobar_ = nullptr; |
| 182 |
| 183 for (size_t i = 0; i < requests_.size(); ++i) { |
| 184 if (!update_content_setting) |
| 185 DCHECK_EQ(CONTENT_SETTING_DEFAULT, result[i]); |
| 186 requests_[i].callback().Run(update_content_setting, result[i]); |
| 187 } |
| 188 callback_.Run(); |
| 189 } |
| 190 |
| 191 void PermissionInfoBarRequest::PruneAnsweredPermissions( |
| 192 Profile* profile) { |
| 193 for (auto it = requests_.begin(); it != requests_.end();) { |
| 194 PermissionStatus status = PermissionManagerFactory::GetForProfile(profile) |
| 195 ->GetPermissionStatus( |
| 196 ContentSettingsTypeToPermissionType(it->type()), |
| 197 requesting_origin_, |
| 198 embedding_origin_); |
| 199 if (status == content::PERMISSION_STATUS_GRANTED || |
| 200 status == content::PERMISSION_STATUS_DENIED) { |
| 201 it->callback().Run(true, |
| 202 PermissionStatusToContentSetting(status)); |
| 203 it = requests_.erase(it); |
| 204 } else { |
| 205 ++it; |
| 206 } |
| 207 } |
| 208 } |
| 209 |
| 210 void PermissionInfoBarRequest::CreateInfoBar( |
| 211 InfoBarService* infobar_service, |
| 212 const PermissionRequest& request, |
| 213 const std::string& display_languages, |
| 214 const PermissionInfobarDelegate::PermissionSetCallback& callback) { |
| 215 switch (request.type()) { |
| 216 case CONTENT_SETTINGS_TYPE_GEOLOCATION: |
| 217 infobar_ = GeolocationInfoBarDelegateAndroid::Create( |
| 218 infobar_service, requesting_origin_, |
| 219 display_languages, callback); |
| 220 break; |
| 221 #if defined(ENABLE_NOTIFICATIONS) |
| 222 case CONTENT_SETTINGS_TYPE_NOTIFICATIONS: |
| 223 infobar_ = NotificationPermissionInfobarDelegate::Create( |
| 224 infobar_service, requesting_origin_, |
| 225 display_languages, callback); |
| 226 break; |
| 227 #endif // ENABLE_NOTIFICATIONS |
| 228 case CONTENT_SETTINGS_TYPE_MIDI_SYSEX: |
| 229 infobar_ = MidiPermissionInfoBarDelegateAndroid::Create( |
| 230 infobar_service, requesting_origin_, |
| 231 display_languages, request.type(), callback); |
| 232 break; |
| 233 case CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER: |
| 234 infobar_ = ProtectedMediaIdentifierInfoBarDelegateAndroid::Create( |
| 235 infobar_service, requesting_origin_, |
| 236 display_languages, callback); |
| 237 break; |
| 238 case CONTENT_SETTINGS_TYPE_DURABLE_STORAGE: |
| 239 infobar_ = DurableStoragePermissionInfoBarDelegateAndroid::Create( |
| 240 infobar_service, requesting_origin_, |
| 241 display_languages, request.type(), callback); |
| 242 break; |
| 243 default: |
| 244 NOTREACHED(); |
| 245 break; |
| 246 } |
| 247 } |
OLD | NEW |