Chromium Code Reviews| 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.h" | |
| 10 #include "chrome/browser/infobars/infobar_service.h" | |
| 11 #include "chrome/browser/media/midi_permission_infobar_delegate.h" | |
| 12 #include "chrome/browser/notifications/notification_permission_infobar_delegate. h" | |
| 13 #include "chrome/browser/profiles/profile.h" | |
| 14 #include "chrome/browser/storage/durable_storage_permission_infobar_delegate.h" | |
| 15 #include "chrome/common/pref_names.h" | |
| 16 #include "components/content_settings/core/browser/host_content_settings_map.h" | |
| 17 #include "components/infobars/core/infobar.h" | |
| 18 #include "content/public/browser/browser_thread.h" | |
| 19 | |
| 20 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) | |
| 21 #include "chrome/browser/media/protected_media_identifier_infobar_delegate.h" | |
| 22 #endif | |
| 23 | |
| 24 PermissionInfoBarRequest::PermissionRequest::PermissionRequest( | |
| 25 ContentSettingsType type, | |
| 26 const PermissionDecidedCallback& response_callback) | |
| 27 : type_(type), | |
| 28 response_callback_(response_callback) { | |
| 29 } | |
| 30 | |
| 31 PermissionInfoBarRequest::PermissionRequest::~PermissionRequest() { | |
| 32 } | |
| 33 | |
| 34 PermissionInfoBarRequest::PermissionInfoBarRequest( | |
| 35 const GURL& requesting_origin, | |
| 36 const GURL& embedding_origin, | |
| 37 const base::Closure& callback) | |
| 38 : infobar_(nullptr), | |
| 39 requesting_origin_(requesting_origin), | |
| 40 embedding_origin_(embedding_origin), | |
| 41 callback_(callback), | |
| 42 weak_factory_(this) { | |
| 43 } | |
| 44 | |
| 45 PermissionInfoBarRequest::~PermissionInfoBarRequest() { | |
| 46 } | |
| 47 | |
| 48 void PermissionInfoBarRequest::AddPermission( | |
| 49 const ContentSettingsType type, | |
| 50 const PermissionDecidedCallback& response_callback) { | |
| 51 requests_.push_back(PermissionRequest(type, response_callback)); | |
| 52 } | |
| 53 | |
| 54 bool PermissionInfoBarRequest::ShowInfobar( | |
| 55 InfoBarService* infobar_service, | |
| 56 Profile* profile) { | |
| 57 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 58 | |
| 59 // After pruning if there is nothing left then just ignore this request. | |
| 60 PruneAnsweredPermissions(profile); | |
| 61 if (requests_.size() == 0) | |
| 62 return false; | |
| 63 | |
| 64 std::string display_languages = | |
| 65 profile->GetPrefs()->GetString(prefs::kAcceptLanguages); | |
| 66 if (requests_.size() == 1) { | |
| 67 CreateInfoBar( | |
| 68 infobar_service, | |
| 69 requests_[0], | |
| 70 display_languages, | |
| 71 base::Bind(&PermissionInfoBarRequest::OnPermissionSet, | |
| 72 weak_factory_.GetWeakPtr())); | |
| 73 return true; | |
| 74 } | |
| 75 | |
| 76 // TODO(lalitm) once multiple permissions is ready to land, this | |
| 77 // should be implemented properly. | |
| 78 NOTIMPLEMENTED(); | |
| 79 for (size_t i = 0; i < requests_.size(); ++i) | |
| 80 requests_[i].response_callback().Run(false, CONTENT_SETTING_DEFAULT); | |
| 81 return false; | |
| 82 } | |
| 83 | |
| 84 void PermissionInfoBarRequest::Accept() { | |
| 85 OnExternalRemoval(); | |
| 86 OnPermissionsSet(true, | |
| 87 std::vector<ContentSetting>(requests_.size(), CONTENT_SETTING_ALLOW)); | |
| 88 } | |
| 89 | |
| 90 void PermissionInfoBarRequest::Closing() { | |
| 91 OnExternalRemoval(); | |
| 92 OnPermissionsSet(false, | |
| 93 std::vector<ContentSetting>(requests_.size(), CONTENT_SETTING_BLOCK)); | |
| 94 } | |
| 95 | |
| 96 void PermissionInfoBarRequest::Cancel() { | |
| 97 OnExternalRemoval(); | |
| 98 callback_.Run(); | |
| 99 } | |
| 100 | |
| 101 void PermissionInfoBarRequest::OnExternalRemoval() { | |
| 102 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 103 DCHECK(infobar_); | |
| 104 | |
| 105 infobar_->RemoveSelf(); | |
| 106 infobar_ = nullptr; | |
| 107 } | |
| 108 | |
| 109 void PermissionInfoBarRequest::OnPermissionSet( | |
| 110 bool update_content_setting, | |
| 111 bool allowed) { | |
| 112 OnPermissionsSet(update_content_setting, | |
| 113 std::vector<ContentSetting>(1, | |
| 114 allowed ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK)); | |
| 115 } | |
| 116 | |
| 117 void PermissionInfoBarRequest::OnPermissionsSet( | |
| 118 bool update_content_setting, | |
| 119 const std::vector<ContentSetting>& allowed) { | |
| 120 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 121 | |
| 122 // Set the infobar to nullptr so there is no attempt to remove it later. | |
| 123 infobar_ = nullptr; | |
| 124 | |
| 125 for (size_t i = 0; i < requests_.size(); ++i) { | |
| 126 requests_[i].response_callback() | |
| 127 .Run(update_content_setting, | |
| 128 update_content_setting ? allowed[i] : CONTENT_SETTING_DEFAULT); | |
| 129 } | |
| 130 callback_.Run(); | |
| 131 } | |
| 132 | |
| 133 void PermissionInfoBarRequest::PruneAnsweredPermissions( | |
| 134 Profile* profile) { | |
| 135 for (auto it = requests_.begin(); it != requests_.end();) { | |
| 136 ContentSetting content_setting = | |
| 137 HostContentSettingsMapFactory::GetForProfile(profile) | |
|
mlamouri (slow - plz ping)
2015/09/29 14:02:18
Do not use HostContentSettingsMapFactory but Permi
Lalit Maganti
2015/10/01 17:05:14
Hmmmm I would do this except PermissionManager tak
| |
| 138 ->GetContentSettingAndMaybeUpdateLastUsage(requesting_origin_, | |
| 139 embedding_origin_, it->type(), std::string()); | |
| 140 if (content_setting == CONTENT_SETTING_ALLOW || | |
| 141 content_setting == CONTENT_SETTING_BLOCK) { | |
| 142 it->response_callback().Run(true, content_setting); | |
| 143 it = requests_.erase(it); | |
| 144 } else { | |
| 145 ++it; | |
| 146 } | |
| 147 } | |
| 148 } | |
| 149 | |
| 150 void PermissionInfoBarRequest::CreateInfoBar( | |
| 151 InfoBarService* infobar_service, | |
| 152 const PermissionRequest& request, | |
| 153 const std::string& display_languages, | |
| 154 const PermissionInfobarDelegate::PermissionSetCallback& callback) { | |
| 155 switch (request.type()) { | |
| 156 case CONTENT_SETTINGS_TYPE_GEOLOCATION: | |
| 157 infobar_ = GeolocationInfoBarDelegate::Create( | |
| 158 infobar_service, requesting_origin_, | |
| 159 display_languages, callback); | |
| 160 break; | |
| 161 #if defined(ENABLE_NOTIFICATIONS) | |
| 162 case CONTENT_SETTINGS_TYPE_NOTIFICATIONS: | |
| 163 infobar_ = NotificationPermissionInfobarDelegate::Create( | |
| 164 infobar_service, requesting_origin_, | |
| 165 display_languages, callback); | |
| 166 break; | |
| 167 #endif // ENABLE_NOTIFICATIONS | |
| 168 case CONTENT_SETTINGS_TYPE_MIDI_SYSEX: | |
| 169 infobar_ = MidiPermissionInfoBarDelegate::Create( | |
| 170 infobar_service, requesting_origin_, | |
| 171 display_languages, request.type(), callback); | |
| 172 break; | |
| 173 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) | |
| 174 case CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER: | |
| 175 infobar_ = ProtectedMediaIdentifierInfoBarDelegate::Create( | |
| 176 infobar_service, requesting_origin_, | |
| 177 display_languages, callback); | |
| 178 break; | |
| 179 #endif | |
| 180 case CONTENT_SETTINGS_TYPE_DURABLE_STORAGE: | |
| 181 infobar_ = DurableStoragePermissionInfoBarDelegate::Create( | |
| 182 infobar_service, requesting_origin_, | |
| 183 display_languages, request.type(), callback); | |
| 184 break; | |
| 185 default: | |
| 186 NOTREACHED(); | |
| 187 break; | |
| 188 } | |
| 189 } | |
| OLD | NEW |