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& user_callback, | |
| 27 const PermissionDecidedCallback& non_user_callback) | |
| 28 : type_(type), | |
| 29 user_callback_(user_callback), | |
| 30 non_user_callback_(non_user_callback) { | |
| 31 } | |
| 32 | |
| 33 PermissionInfoBarRequest::PermissionRequest::~PermissionRequest() { | |
| 34 } | |
| 35 | |
| 36 PermissionInfoBarRequest::PermissionInfoBarRequest( | |
| 37 const GURL& requesting_frame, | |
| 38 const GURL& embedding_frame, | |
| 39 const base::Callback<void(bool)>& finished_callback) | |
| 40 : infobar_(nullptr), | |
| 41 requesting_frame_(requesting_frame), | |
| 42 embedding_frame_(embedding_frame), | |
| 43 finished_callback_(finished_callback), | |
| 44 weak_factory_(this) { | |
| 45 } | |
| 46 | |
| 47 PermissionInfoBarRequest::~PermissionInfoBarRequest() { | |
| 48 } | |
| 49 | |
| 50 void PermissionInfoBarRequest::AddPermission( | |
| 51 const ContentSettingsType type, | |
| 52 const PermissionDecidedCallback& user_callback, | |
| 53 const PermissionDecidedCallback& non_user_callback) { | |
| 54 requests_.push_back( | |
| 55 PermissionRequest(type, user_callback, non_user_callback)); | |
| 56 } | |
| 57 | |
| 58 bool PermissionInfoBarRequest::ShowInfobar( | |
| 59 InfoBarService* infobar_service, | |
| 60 Profile* profile) { | |
| 61 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 62 | |
| 63 // After pruning if there is nothing left then just ignore this request. | |
| 64 PruneAnsweredPermissions(profile); | |
| 65 if (requests_.size() == 0) | |
| 66 return false; | |
| 67 | |
| 68 std::string display_languages = | |
| 69 profile->GetPrefs()->GetString(prefs::kAcceptLanguages); | |
| 70 if (requests_.size() == 1) { | |
| 71 CreateInfoBar( | |
| 72 infobar_service, | |
| 73 requests_[0], | |
| 74 display_languages, | |
| 75 base::Bind(&PermissionInfoBarRequest::OnPermissionSet, | |
| 76 weak_factory_.GetWeakPtr())); | |
| 77 return true; | |
| 78 } | |
| 79 | |
| 80 // TODO(lalitm) once multiple permissions is ready to land, this | |
| 81 // should be implemented properly. | |
| 82 NOTIMPLEMENTED(); | |
| 83 for (size_t i = 0; i < requests_.size(); ++i) | |
| 84 requests_[i].non_user_callback().Run(false, CONTENT_SETTING_DEFAULT); | |
|
mlamouri (slow - plz ping)
2015/09/28 13:30:15
Maybe you could create N infobars?
Lalit Maganti
2015/09/28 15:32:38
Would require big changes in the way this class is
| |
| 85 return false; | |
| 86 } | |
| 87 | |
| 88 void PermissionInfoBarRequest::Cancel(bool allowed) { | |
|
mlamouri (slow - plz ping)
2015/09/28 13:30:15
You do not seem to be using |allowed|.
Lalit Maganti
2015/09/28 15:32:38
Fixed.
| |
| 89 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 90 DCHECK(infobar_); | |
| 91 | |
| 92 infobar_->RemoveSelf(); | |
| 93 infobar_ = nullptr; | |
| 94 finished_callback_.Run(true); | |
| 95 } | |
| 96 | |
| 97 void PermissionInfoBarRequest::OnPermissionSet( | |
| 98 bool update_content_setting, | |
| 99 bool allowed) { | |
| 100 OnPermissionsSet(update_content_setting, | |
| 101 std::vector<ContentSetting>(1, | |
| 102 allowed ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK)); | |
| 103 } | |
| 104 | |
| 105 void PermissionInfoBarRequest::OnPermissionsSet( | |
| 106 bool update_content_setting, | |
| 107 const std::vector<ContentSetting>& allowed) { | |
| 108 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 109 | |
| 110 // Set the infobar to nullptr so there is no attempt to remove it later. | |
| 111 infobar_ = nullptr; | |
| 112 | |
| 113 for (size_t i = 0; i < requests_.size(); ++i) { | |
| 114 requests_[i].user_callback() | |
| 115 .Run(update_content_setting, | |
| 116 update_content_setting ? allowed[i] : CONTENT_SETTING_DEFAULT); | |
| 117 } | |
| 118 finished_callback_.Run(false); | |
| 119 } | |
| 120 | |
| 121 void PermissionInfoBarRequest::PruneAnsweredPermissions( | |
| 122 Profile* profile) { | |
| 123 for (auto it = requests_.begin(); it != requests_.end();) { | |
| 124 ContentSetting content_setting = | |
| 125 HostContentSettingsMapFactory::GetForProfile(profile) | |
| 126 ->GetContentSettingAndMaybeUpdateLastUsage(requesting_frame_, | |
| 127 embedding_frame_, it->type(), std::string()); | |
| 128 if (content_setting == CONTENT_SETTING_ALLOW || | |
| 129 content_setting == CONTENT_SETTING_BLOCK) { | |
| 130 it->non_user_callback().Run(true, content_setting); | |
| 131 it = requests_.erase(it); | |
| 132 } else { | |
| 133 ++it; | |
| 134 } | |
| 135 } | |
| 136 } | |
| 137 | |
| 138 void PermissionInfoBarRequest::CreateInfoBar( | |
| 139 InfoBarService* infobar_service, | |
| 140 const PermissionRequest& request, | |
| 141 const std::string& display_languages, | |
| 142 const PermissionInfobarDelegate::PermissionSetCallback& callback) { | |
| 143 switch (request.type()) { | |
| 144 case CONTENT_SETTINGS_TYPE_GEOLOCATION: | |
| 145 infobar_ = GeolocationInfoBarDelegate::Create( | |
| 146 infobar_service, requesting_frame_, | |
| 147 display_languages, callback); | |
| 148 break; | |
| 149 #if defined(ENABLE_NOTIFICATIONS) | |
| 150 case CONTENT_SETTINGS_TYPE_NOTIFICATIONS: | |
| 151 infobar_ = NotificationPermissionInfobarDelegate::Create( | |
| 152 infobar_service, requesting_frame_, | |
| 153 display_languages, callback); | |
| 154 break; | |
| 155 #endif // ENABLE_NOTIFICATIONS | |
| 156 case CONTENT_SETTINGS_TYPE_MIDI_SYSEX: | |
| 157 infobar_ = MidiPermissionInfoBarDelegate::Create( | |
| 158 infobar_service, requesting_frame_, | |
| 159 display_languages, request.type(), callback); | |
| 160 break; | |
| 161 #if defined(OS_ANDROID) || defined(OS_CHROMEOS) | |
| 162 case CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER: | |
| 163 infobar_ = ProtectedMediaIdentifierInfoBarDelegate::Create( | |
| 164 infobar_service, requesting_frame_, | |
| 165 display_languages, callback); | |
| 166 break; | |
| 167 #endif | |
| 168 case CONTENT_SETTINGS_TYPE_DURABLE_STORAGE: | |
| 169 infobar_ = DurableStoragePermissionInfoBarDelegate::Create( | |
| 170 infobar_service, requesting_frame_, | |
| 171 display_languages, request.type(), callback); | |
| 172 break; | |
| 173 default: | |
| 174 NOTREACHED(); | |
| 175 break; | |
| 176 } | |
| 177 } | |
| OLD | NEW |