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