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"
21 PermissionContextBase::PermissionContextBase(
23 const ContentSettingsType permission_type
)
25 permission_type_(permission_type
),
27 permission_queue_controller_
.reset(
28 new PermissionQueueController(profile_
, permission_type_
));
31 PermissionContextBase::~PermissionContextBase() {
32 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
35 void PermissionContextBase::RequestPermission(
36 content::WebContents
* web_contents
,
37 const PermissionRequestID
& id
,
38 const GURL
& requesting_frame
,
40 const BrowserPermissionCallback
& callback
) {
41 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
43 DecidePermission(web_contents
,
45 requesting_frame
.GetOrigin(),
46 web_contents
->GetLastCommittedURL().GetOrigin(),
51 ContentSetting
PermissionContextBase::GetPermissionStatus(
52 const GURL
& requesting_origin
,
53 const GURL
& embedding_origin
) const {
54 return profile_
->GetHostContentSettingsMap()->GetContentSetting(
55 requesting_origin
, embedding_origin
, permission_type_
, std::string());
58 void PermissionContextBase::ResetPermission(
59 const GURL
& requesting_origin
,
60 const GURL
& embedding_origin
) {
61 profile_
->GetHostContentSettingsMap()->SetContentSetting(
62 ContentSettingsPattern::FromURLNoWildcard(requesting_origin
),
63 ContentSettingsPattern::FromURLNoWildcard(embedding_origin
),
64 permission_type_
, std::string(), CONTENT_SETTING_DEFAULT
);
67 void PermissionContextBase::CancelPermissionRequest(
68 content::WebContents
* web_contents
,
69 const PermissionRequestID
& id
) {
70 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
72 if (PermissionBubbleManager::Enabled()) {
73 PermissionBubbleRequest
* cancelling
=
74 pending_bubbles_
.get(id
.ToString());
75 if (cancelling
!= NULL
&& web_contents
!= NULL
&&
76 PermissionBubbleManager::FromWebContents(web_contents
) != NULL
) {
77 PermissionBubbleManager::FromWebContents(web_contents
)->
78 CancelRequest(cancelling
);
83 GetQueueController()->CancelInfoBarRequest(id
);
86 void PermissionContextBase::DecidePermission(
87 content::WebContents
* web_contents
,
88 const PermissionRequestID
& id
,
89 const GURL
& requesting_origin
,
90 const GURL
& embedding_origin
,
92 const BrowserPermissionCallback
& callback
) {
93 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
95 if (!requesting_origin
.is_valid() || !embedding_origin
.is_valid()) {
97 << "Attempt to use " << content_settings::GetTypeName(permission_type_
)
98 << " from an invalid URL: " << requesting_origin
99 << "," << embedding_origin
100 << " (" << content_settings::GetTypeName(permission_type_
)
101 << " is not supported in popups)";
102 NotifyPermissionSet(id
, requesting_origin
, embedding_origin
, callback
,
103 false /* persist */, CONTENT_SETTING_BLOCK
);
107 ContentSetting content_setting
=
108 profile_
->GetHostContentSettingsMap()
109 ->GetContentSettingAndMaybeUpdateLastUsage(
110 requesting_origin
, embedding_origin
, permission_type_
,
113 if (content_setting
== CONTENT_SETTING_ALLOW
||
114 content_setting
== CONTENT_SETTING_BLOCK
) {
115 NotifyPermissionSet(id
, requesting_origin
, embedding_origin
, callback
,
116 false /* persist */, content_setting
);
120 PermissionContextUmaUtil::PermissionRequested(
121 permission_type_
, requesting_origin
);
123 if (PermissionBubbleManager::Enabled()) {
124 if (pending_bubbles_
.get(id
.ToString()) != NULL
)
126 PermissionBubbleManager
* bubble_manager
=
127 PermissionBubbleManager::FromWebContents(web_contents
);
128 DCHECK(bubble_manager
);
129 scoped_ptr
<PermissionBubbleRequest
> request_ptr(
130 new PermissionBubbleRequestImpl(
131 requesting_origin
, user_gesture
, permission_type_
,
132 profile_
->GetPrefs()->GetString(prefs::kAcceptLanguages
),
133 base::Bind(&PermissionContextBase::PermissionDecided
,
134 weak_factory_
.GetWeakPtr(), id
, requesting_origin
,
135 embedding_origin
, callback
),
136 base::Bind(&PermissionContextBase::CleanUpBubble
,
137 weak_factory_
.GetWeakPtr(), id
)));
138 PermissionBubbleRequest
* request
= request_ptr
.get();
140 bool inserted
= pending_bubbles_
.add(
141 id
.ToString(), request_ptr
.Pass()).second
;
142 DCHECK(inserted
) << "Duplicate id " << id
.ToString();
143 bubble_manager
->AddRequest(request
);
147 // TODO(gbillock): Delete this and the infobar delegate when
148 // we're using only bubbles. crbug.com/337458
149 GetQueueController()->CreateInfoBarRequest(
150 id
, requesting_origin
, embedding_origin
,
151 base::Bind(&PermissionContextBase::PermissionDecided
,
152 weak_factory_
.GetWeakPtr(), id
, requesting_origin
,
153 embedding_origin
, callback
,
154 // the queue controller takes care of persisting the
159 void PermissionContextBase::PermissionDecided(
160 const PermissionRequestID
& id
,
161 const GURL
& requesting_origin
,
162 const GURL
& embedding_origin
,
163 const BrowserPermissionCallback
& callback
,
165 ContentSetting content_setting
) {
166 // Infobar persistance and its related UMA is tracked on the infobar
167 // controller directly.
168 if (PermissionBubbleManager::Enabled()) {
170 DCHECK(content_setting
== CONTENT_SETTING_ALLOW
||
171 content_setting
== CONTENT_SETTING_BLOCK
);
172 if (CONTENT_SETTING_ALLOW
)
173 PermissionContextUmaUtil::PermissionGranted(permission_type_
,
176 PermissionContextUmaUtil::PermissionDenied(permission_type_
,
179 DCHECK_EQ(content_setting
, CONTENT_SETTING_DEFAULT
);
180 PermissionContextUmaUtil::PermissionDismissed(permission_type_
,
185 NotifyPermissionSet(id
, requesting_origin
, embedding_origin
, callback
,
186 persist
, content_setting
);
189 PermissionQueueController
* PermissionContextBase::GetQueueController() {
190 return permission_queue_controller_
.get();
193 Profile
* PermissionContextBase::profile() const {
197 void PermissionContextBase::NotifyPermissionSet(
198 const PermissionRequestID
& id
,
199 const GURL
& requesting_origin
,
200 const GURL
& embedding_origin
,
201 const BrowserPermissionCallback
& callback
,
203 ContentSetting content_setting
) {
204 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
207 UpdateContentSetting(requesting_origin
, embedding_origin
, content_setting
);
209 UpdateTabContext(id
, requesting_origin
,
210 content_setting
== CONTENT_SETTING_ALLOW
);
212 if (content_setting
== CONTENT_SETTING_DEFAULT
) {
214 profile_
->GetHostContentSettingsMap()->GetDefaultContentSetting(
215 permission_type_
, nullptr);
218 DCHECK_NE(content_setting
, CONTENT_SETTING_DEFAULT
);
219 callback
.Run(content_setting
);
222 void PermissionContextBase::CleanUpBubble(const PermissionRequestID
& id
) {
223 size_t success
= pending_bubbles_
.erase(id
.ToString());
224 DCHECK(success
== 1) << "Missing request " << id
.ToString();
227 void PermissionContextBase::UpdateContentSetting(
228 const GURL
& requesting_origin
,
229 const GURL
& embedding_origin
,
230 ContentSetting content_setting
) {
231 DCHECK_EQ(requesting_origin
, requesting_origin
.GetOrigin());
232 DCHECK_EQ(embedding_origin
, embedding_origin
.GetOrigin());
233 DCHECK(content_setting
== CONTENT_SETTING_ALLOW
||
234 content_setting
== CONTENT_SETTING_BLOCK
);
236 profile_
->GetHostContentSettingsMap()->SetContentSetting(
237 ContentSettingsPattern::FromURLNoWildcard(requesting_origin
),
238 ContentSettingsPattern::FromURLNoWildcard(embedding_origin
),
239 permission_type_
, std::string(), content_setting
);