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.
5 #include "chrome/browser/permissions/permission_manager.h"
7 #include "base/callback.h"
8 #include "chrome/browser/permissions/permission_context.h"
9 #include "chrome/browser/permissions/permission_context_base.h"
10 #include "chrome/browser/permissions/permission_request_id.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h"
13 #include "components/content_settings/core/browser/host_content_settings_map.h"
14 #include "content/public/browser/permission_type.h"
15 #include "content/public/browser/render_frame_host.h"
16 #include "content/public/browser/render_process_host.h"
17 #include "content/public/browser/web_contents.h"
19 using content::PermissionStatus
;
20 using content::PermissionType
;
24 // Helper method to convert ContentSetting to PermissionStatus.
25 PermissionStatus
ContentSettingToPermissionStatus(ContentSetting setting
) {
27 case CONTENT_SETTING_ALLOW
:
28 case CONTENT_SETTING_SESSION_ONLY
:
29 return content::PERMISSION_STATUS_GRANTED
;
30 case CONTENT_SETTING_BLOCK
:
31 return content::PERMISSION_STATUS_DENIED
;
32 case CONTENT_SETTING_ASK
:
33 return content::PERMISSION_STATUS_ASK
;
34 case CONTENT_SETTING_DETECT_IMPORTANT_CONTENT
:
35 case CONTENT_SETTING_DEFAULT
:
36 case CONTENT_SETTING_NUM_SETTINGS
:
41 return content::PERMISSION_STATUS_DENIED
;
44 // Helper method to convert PermissionType to ContentSettingType.
45 ContentSettingsType
PermissionTypeToContentSetting(PermissionType permission
) {
47 case PermissionType::MIDI_SYSEX
:
48 return CONTENT_SETTINGS_TYPE_MIDI_SYSEX
;
49 case PermissionType::PUSH_MESSAGING
:
50 return CONTENT_SETTINGS_TYPE_PUSH_MESSAGING
;
51 case PermissionType::NOTIFICATIONS
:
52 return CONTENT_SETTINGS_TYPE_NOTIFICATIONS
;
53 case PermissionType::GEOLOCATION
:
54 return CONTENT_SETTINGS_TYPE_GEOLOCATION
;
55 case PermissionType::PROTECTED_MEDIA_IDENTIFIER
:
56 #if defined(OS_ANDROID) || defined(OS_CHROMEOS)
57 return CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER
;
62 case PermissionType::DURABLE_STORAGE
:
63 return CONTENT_SETTINGS_TYPE_DURABLE_STORAGE
;
64 case PermissionType::MIDI
:
65 // This will hit the NOTREACHED below.
67 case PermissionType::AUDIO_CAPTURE
:
68 return CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
;
69 case PermissionType::VIDEO_CAPTURE
:
70 return CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
;
71 case PermissionType::NUM
:
72 // This will hit the NOTREACHED below.
76 NOTREACHED() << "Unknown content setting for permission "
77 << static_cast<int>(permission
);
78 return CONTENT_SETTINGS_TYPE_DEFAULT
;
81 // Helper method that wraps a callback a void(PermissionStatus)
82 // callback into a void(ContentSetting) callback.
83 void PermissionStatusCallbackWrapper(
84 const base::Callback
<void(PermissionStatus
)>& callback
,
85 ContentSetting content_setting
) {
86 callback
.Run(ContentSettingToPermissionStatus(content_setting
));
89 // Returns whether the permission has a constant PermissionStatus value (i.e.
90 // always approved or always denied)
91 // The PermissionTypes for which true is returned should be exactly those which
92 // return nullptr in PermissionContext::Get since they don't have a context.
93 bool IsConstantPermission(PermissionType type
) {
95 case PermissionType::MIDI
:
102 // Function used for handling permission types which do not change their
103 // value i.e. they are always approved or always denied etc.
104 // CONTENT_SETTING_DEFAULT is returned if the permission needs further handling.
105 // This function should only be called when IsConstantPermission has returned
106 // true for the PermissionType.
107 ContentSetting
GetContentSettingForConstantPermission(PermissionType type
) {
108 DCHECK(IsConstantPermission(type
));
110 case PermissionType::MIDI
:
111 return CONTENT_SETTING_ALLOW
;
113 return CONTENT_SETTING_DEFAULT
;
117 PermissionStatus
GetPermissionStatusForConstantPermission(PermissionType type
) {
118 return ContentSettingToPermissionStatus(
119 GetContentSettingForConstantPermission(type
));
122 } // anonymous namespace
124 struct PermissionManager::Subscription
{
125 PermissionType permission
;
126 GURL requesting_origin
;
127 GURL embedding_origin
;
128 base::Callback
<void(PermissionStatus
)> callback
;
129 ContentSetting current_value
;
132 PermissionManager::PermissionManager(Profile
* profile
)
133 : profile_(profile
) {
136 PermissionManager::~PermissionManager() {
137 if (!subscriptions_
.IsEmpty())
138 profile_
->GetHostContentSettingsMap()->RemoveObserver(this);
141 void PermissionManager::RequestPermission(
142 PermissionType permission
,
143 content::RenderFrameHost
* render_frame_host
,
145 const GURL
& requesting_origin
,
147 const base::Callback
<void(PermissionStatus
)>& callback
) {
148 if (IsConstantPermission(permission
)) {
149 callback
.Run(GetPermissionStatusForConstantPermission(permission
));
153 PermissionContextBase
* context
= PermissionContext::Get(profile_
, permission
);
155 callback
.Run(content::PERMISSION_STATUS_DENIED
);
159 content::WebContents
* web_contents
=
160 content::WebContents::FromRenderFrameHost(render_frame_host
);
161 if (IsPermissionBubbleManagerMissing(web_contents
)) {
163 GetPermissionStatus(permission
, requesting_origin
,
164 web_contents
->GetLastCommittedURL().GetOrigin()));
168 int render_process_id
= render_frame_host
->GetProcess()->GetID();
169 int render_frame_id
= render_frame_host
->GetRoutingID();
170 const PermissionRequestID
request(render_process_id
,
175 context
->RequestPermission(
176 web_contents
, request
, requesting_origin
, user_gesture
,
177 base::Bind(&PermissionStatusCallbackWrapper
, callback
));
180 void PermissionManager::CancelPermissionRequest(
181 PermissionType permission
,
182 content::RenderFrameHost
* render_frame_host
,
184 const GURL
& requesting_origin
) {
185 PermissionContextBase
* context
= PermissionContext::Get(profile_
, permission
);
189 content::WebContents
* web_contents
=
190 content::WebContents::FromRenderFrameHost(render_frame_host
);
191 if (IsPermissionBubbleManagerMissing(web_contents
))
194 int render_process_id
= render_frame_host
->GetProcess()->GetID();
195 int render_frame_id
= render_frame_host
->GetRoutingID();
196 const PermissionRequestID
request(render_process_id
,
201 context
->CancelPermissionRequest(web_contents
, request
);
204 void PermissionManager::ResetPermission(PermissionType permission
,
205 const GURL
& requesting_origin
,
206 const GURL
& embedding_origin
) {
207 PermissionContextBase
* context
= PermissionContext::Get(profile_
, permission
);
211 context
->ResetPermission(requesting_origin
.GetOrigin(),
212 embedding_origin
.GetOrigin());
215 PermissionStatus
PermissionManager::GetPermissionStatus(
216 PermissionType permission
,
217 const GURL
& requesting_origin
,
218 const GURL
& embedding_origin
) {
219 if (IsConstantPermission(permission
))
220 return GetPermissionStatusForConstantPermission(permission
);
222 PermissionContextBase
* context
= PermissionContext::Get(profile_
, permission
);
224 return content::PERMISSION_STATUS_DENIED
;
226 return ContentSettingToPermissionStatus(context
->GetPermissionStatus(
227 requesting_origin
.GetOrigin(), embedding_origin
.GetOrigin()));
230 void PermissionManager::RegisterPermissionUsage(PermissionType permission
,
231 const GURL
& requesting_origin
,
232 const GURL
& embedding_origin
) {
233 // This is required because constant permissions don't have a
234 // ContentSettingsType.
235 if (IsConstantPermission(permission
))
238 profile_
->GetHostContentSettingsMap()->UpdateLastUsage(
241 PermissionTypeToContentSetting(permission
));
244 int PermissionManager::SubscribePermissionStatusChange(
245 PermissionType permission
,
246 const GURL
& requesting_origin
,
247 const GURL
& embedding_origin
,
248 const base::Callback
<void(PermissionStatus
)>& callback
) {
249 if (subscriptions_
.IsEmpty())
250 profile_
->GetHostContentSettingsMap()->AddObserver(this);
252 Subscription
* subscription
= new Subscription();
253 subscription
->permission
= permission
;
254 subscription
->requesting_origin
= requesting_origin
;
255 subscription
->embedding_origin
= embedding_origin
;
256 subscription
->callback
= callback
;
258 if (IsConstantPermission(permission
)) {
259 subscription
->current_value
= GetContentSettingForConstantPermission(
262 subscription
->current_value
= PermissionContext::Get(profile_
, permission
)
263 ->GetPermissionStatus(subscription
->requesting_origin
,
264 subscription
->embedding_origin
);
267 return subscriptions_
.Add(subscription
);
270 void PermissionManager::UnsubscribePermissionStatusChange(int subscription_id
) {
271 // Whether |subscription_id| is known will be checked by the Remove() call.
272 subscriptions_
.Remove(subscription_id
);
274 if (subscriptions_
.IsEmpty())
275 profile_
->GetHostContentSettingsMap()->RemoveObserver(this);
278 bool PermissionManager::IsPermissionBubbleManagerMissing(
279 content::WebContents
* web_contents
) {
280 // Can't be missing if it isn't needed to begin with.
281 if (!PermissionBubbleManager::Enabled())
284 return PermissionBubbleManager::FromWebContents(web_contents
) == nullptr;
287 void PermissionManager::OnContentSettingChanged(
288 const ContentSettingsPattern
& primary_pattern
,
289 const ContentSettingsPattern
& secondary_pattern
,
290 ContentSettingsType content_type
,
291 std::string resource_identifier
) {
292 std::list
<base::Closure
> callbacks
;
294 for (SubscriptionsMap::iterator
iter(&subscriptions_
);
295 !iter
.IsAtEnd(); iter
.Advance()) {
296 Subscription
* subscription
= iter
.GetCurrentValue();
297 if (PermissionTypeToContentSetting(subscription
->permission
) !=
302 if (primary_pattern
.IsValid() &&
303 !primary_pattern
.Matches(subscription
->requesting_origin
))
305 if (secondary_pattern
.IsValid() &&
306 !secondary_pattern
.Matches(subscription
->embedding_origin
))
309 ContentSetting new_value
=
310 PermissionContext::Get(profile_
, subscription
->permission
)
311 ->GetPermissionStatus(subscription
->requesting_origin
,
312 subscription
->embedding_origin
);
313 if (subscription
->current_value
== new_value
)
316 subscription
->current_value
= new_value
;
318 // Add the callback to |callbacks| which will be run after the loop to
319 // prevent re-entrance issues.
321 base::Bind(subscription
->callback
,
322 ContentSettingToPermissionStatus(new_value
)));
325 for (const auto& callback
: callbacks
)