Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / content_settings / permission_context_base.cc
bloba011d568d476c42cd084cbf0ba2959f4194a5132
1 // Copyright 2014 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.
5 #include "chrome/browser/content_settings/permission_context_base.h"
7 #include "base/logging.h"
8 #include "base/prefs/pref_service.h"
9 #include "chrome/browser/content_settings/permission_bubble_request_impl.h"
10 #include "chrome/browser/content_settings/permission_context_uma_util.h"
11 #include "chrome/browser/content_settings/permission_queue_controller.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
14 #include "chrome/common/pref_names.h"
15 #include "components/content_settings/core/browser/content_settings_utils.h"
16 #include "components/content_settings/core/browser/host_content_settings_map.h"
17 #include "components/content_settings/core/common/permission_request_id.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/web_contents.h"
20 #include "net/base/net_util.h"
22 PermissionContextBase::PermissionContextBase(
23 Profile* profile,
24 const ContentSettingsType permission_type)
25 : profile_(profile),
26 permission_type_(permission_type),
27 weak_factory_(this) {
28 permission_queue_controller_.reset(
29 new PermissionQueueController(profile_, permission_type_));
32 PermissionContextBase::~PermissionContextBase() {
33 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
36 void PermissionContextBase::RequestPermission(
37 content::WebContents* web_contents,
38 const PermissionRequestID& id,
39 const GURL& requesting_frame,
40 bool user_gesture,
41 const BrowserPermissionCallback& callback) {
42 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
44 DecidePermission(web_contents,
45 id,
46 requesting_frame.GetOrigin(),
47 web_contents->GetLastCommittedURL().GetOrigin(),
48 user_gesture,
49 callback);
52 ContentSetting PermissionContextBase::GetPermissionStatus(
53 const GURL& requesting_origin,
54 const GURL& embedding_origin) const {
55 return profile_->GetHostContentSettingsMap()->GetContentSetting(
56 requesting_origin, embedding_origin, permission_type_, std::string());
59 void PermissionContextBase::ResetPermission(
60 const GURL& requesting_origin,
61 const GURL& embedding_origin) {
62 profile_->GetHostContentSettingsMap()->SetContentSetting(
63 ContentSettingsPattern::FromURLNoWildcard(requesting_origin),
64 ContentSettingsPattern::FromURLNoWildcard(embedding_origin),
65 permission_type_, std::string(), CONTENT_SETTING_DEFAULT);
68 void PermissionContextBase::CancelPermissionRequest(
69 content::WebContents* web_contents,
70 const PermissionRequestID& id) {
71 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
73 if (PermissionBubbleManager::Enabled()) {
74 PermissionBubbleRequest* cancelling =
75 pending_bubbles_.get(id.ToString());
76 if (cancelling != NULL && web_contents != NULL &&
77 PermissionBubbleManager::FromWebContents(web_contents) != NULL) {
78 PermissionBubbleManager::FromWebContents(web_contents)->
79 CancelRequest(cancelling);
81 return;
84 GetQueueController()->CancelInfoBarRequest(id);
87 void PermissionContextBase::DecidePermission(
88 content::WebContents* web_contents,
89 const PermissionRequestID& id,
90 const GURL& requesting_origin,
91 const GURL& embedding_origin,
92 bool user_gesture,
93 const BrowserPermissionCallback& callback) {
94 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
96 if (!requesting_origin.is_valid() || !embedding_origin.is_valid()) {
97 DVLOG(1)
98 << "Attempt to use " << content_settings::GetTypeName(permission_type_)
99 << " from an invalid URL: " << requesting_origin
100 << "," << embedding_origin
101 << " (" << content_settings::GetTypeName(permission_type_)
102 << " is not supported in popups)";
103 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback,
104 false /* persist */, CONTENT_SETTING_BLOCK);
105 return;
108 // The Web MIDI API is not available for origin with non secure schemes.
109 // Access to the MIDI API is blocked.
110 // TODO(crbug.com/362214): Use a standard way to check the secure origin.
111 if (permission_type_ == CONTENT_SETTINGS_TYPE_MIDI_SYSEX &&
112 !requesting_origin.SchemeIsSecure() &&
113 !net::IsLocalhost(requesting_origin.host())) {
114 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback,
115 false /* persist */, CONTENT_SETTING_BLOCK);
116 return;
119 ContentSetting content_setting =
120 profile_->GetHostContentSettingsMap()
121 ->GetContentSettingAndMaybeUpdateLastUsage(
122 requesting_origin, embedding_origin, permission_type_,
123 std::string());
125 if (content_setting == CONTENT_SETTING_ALLOW ||
126 content_setting == CONTENT_SETTING_BLOCK) {
127 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback,
128 false /* persist */, content_setting);
129 return;
132 PermissionContextUmaUtil::PermissionRequested(
133 permission_type_, requesting_origin);
135 if (PermissionBubbleManager::Enabled()) {
136 if (pending_bubbles_.get(id.ToString()) != NULL)
137 return;
138 PermissionBubbleManager* bubble_manager =
139 PermissionBubbleManager::FromWebContents(web_contents);
140 // TODO(mlamouri): sometimes |bubble_manager| is null. This check is meant
141 // to prevent crashes. See bug 457091.
142 if (!bubble_manager)
143 return;
144 scoped_ptr<PermissionBubbleRequest> request_ptr(
145 new PermissionBubbleRequestImpl(
146 requesting_origin, user_gesture, permission_type_,
147 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages),
148 base::Bind(&PermissionContextBase::PermissionDecided,
149 weak_factory_.GetWeakPtr(), id, requesting_origin,
150 embedding_origin, callback),
151 base::Bind(&PermissionContextBase::CleanUpBubble,
152 weak_factory_.GetWeakPtr(), id)));
153 PermissionBubbleRequest* request = request_ptr.get();
155 bool inserted = pending_bubbles_.add(
156 id.ToString(), request_ptr.Pass()).second;
157 DCHECK(inserted) << "Duplicate id " << id.ToString();
158 bubble_manager->AddRequest(request);
159 return;
162 // TODO(gbillock): Delete this and the infobar delegate when
163 // we're using only bubbles. crbug.com/337458
164 GetQueueController()->CreateInfoBarRequest(
165 id, requesting_origin, embedding_origin,
166 base::Bind(&PermissionContextBase::PermissionDecided,
167 weak_factory_.GetWeakPtr(), id, requesting_origin,
168 embedding_origin, callback,
169 // the queue controller takes care of persisting the
170 // permission
171 false));
174 void PermissionContextBase::PermissionDecided(
175 const PermissionRequestID& id,
176 const GURL& requesting_origin,
177 const GURL& embedding_origin,
178 const BrowserPermissionCallback& callback,
179 bool persist,
180 ContentSetting content_setting) {
181 // Infobar persistance and its related UMA is tracked on the infobar
182 // controller directly.
183 if (PermissionBubbleManager::Enabled()) {
184 if (persist) {
185 DCHECK(content_setting == CONTENT_SETTING_ALLOW ||
186 content_setting == CONTENT_SETTING_BLOCK);
187 if (CONTENT_SETTING_ALLOW)
188 PermissionContextUmaUtil::PermissionGranted(permission_type_,
189 requesting_origin);
190 else
191 PermissionContextUmaUtil::PermissionDenied(permission_type_,
192 requesting_origin);
193 } else {
194 DCHECK_EQ(content_setting, CONTENT_SETTING_DEFAULT);
195 PermissionContextUmaUtil::PermissionDismissed(permission_type_,
196 requesting_origin);
200 NotifyPermissionSet(id, requesting_origin, embedding_origin, callback,
201 persist, content_setting);
204 PermissionQueueController* PermissionContextBase::GetQueueController() {
205 return permission_queue_controller_.get();
208 Profile* PermissionContextBase::profile() const {
209 return profile_;
212 void PermissionContextBase::NotifyPermissionSet(
213 const PermissionRequestID& id,
214 const GURL& requesting_origin,
215 const GURL& embedding_origin,
216 const BrowserPermissionCallback& callback,
217 bool persist,
218 ContentSetting content_setting) {
219 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
221 if (persist)
222 UpdateContentSetting(requesting_origin, embedding_origin, content_setting);
224 UpdateTabContext(id, requesting_origin,
225 content_setting == CONTENT_SETTING_ALLOW);
227 if (content_setting == CONTENT_SETTING_DEFAULT) {
228 content_setting =
229 profile_->GetHostContentSettingsMap()->GetDefaultContentSetting(
230 permission_type_, nullptr);
233 DCHECK_NE(content_setting, CONTENT_SETTING_DEFAULT);
234 callback.Run(content_setting);
237 void PermissionContextBase::CleanUpBubble(const PermissionRequestID& id) {
238 size_t success = pending_bubbles_.erase(id.ToString());
239 DCHECK(success == 1) << "Missing request " << id.ToString();
242 void PermissionContextBase::UpdateContentSetting(
243 const GURL& requesting_origin,
244 const GURL& embedding_origin,
245 ContentSetting content_setting) {
246 DCHECK_EQ(requesting_origin, requesting_origin.GetOrigin());
247 DCHECK_EQ(embedding_origin, embedding_origin.GetOrigin());
248 DCHECK(content_setting == CONTENT_SETTING_ALLOW ||
249 content_setting == CONTENT_SETTING_BLOCK);
251 profile_->GetHostContentSettingsMap()->SetContentSetting(
252 ContentSettingsPattern::FromURLNoWildcard(requesting_origin),
253 ContentSettingsPattern::FromURLNoWildcard(embedding_origin),
254 permission_type_, std::string(), content_setting);