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 "content/browser/permissions/permission_service_impl.h"
7 #include "content/public/browser/content_browser_client.h"
13 PermissionType
PermissionNameToPermissionType(PermissionName name
) {
15 case PERMISSION_NAME_GEOLOCATION
:
16 return PERMISSION_GEOLOCATION
;
17 case PERMISSION_NAME_MIDI_SYSEX
:
18 return PERMISSION_MIDI_SYSEX
;
19 case PERMISSION_NAME_NOTIFICATIONS
:
20 return PERMISSION_NOTIFICATIONS
;
24 return PERMISSION_NUM
;
27 } // anonymous namespace
29 PermissionServiceImpl::PendingRequest::PendingRequest(PermissionType permission
,
31 : permission(permission
),
35 PermissionServiceImpl::PermissionServiceImpl(PermissionServiceContext
* context
)
40 PermissionServiceImpl::~PermissionServiceImpl() {
43 void PermissionServiceImpl::OnConnectionError() {
44 context_
->ServiceHadConnectionError(this);
45 // After that call, |this| will be deleted.
48 void PermissionServiceImpl::RequestPermission(
49 PermissionName permission
,
50 const mojo::String
& origin
,
51 const mojo::Callback
<void(PermissionStatus
)>& callback
) {
52 // This condition is valid if the call is coming from a ChildThread instead of
53 // a RenderFrame. Some consumers of the service run in Workers and some in
54 // Frames. In the context of a Worker, it is not possible to show a
55 // permission prompt because there is no tab. In the context of a Frame, we
56 // can. Even if the call comes from a context where it is not possible to show
57 // any UI, we want to still return something relevant so the current
58 // permission status is returned.
59 if (!context_
->web_contents()) {
60 // There is no way to show a UI so the call will simply return the current
62 HasPermission(permission
, origin
, callback
);
66 PermissionType permission_type
= PermissionNameToPermissionType(permission
);
67 int request_id
= pending_requests_
.Add(
68 new PendingRequest(permission_type
, GURL(origin
)));
70 GetContentClient()->browser()->RequestPermission(
72 context_
->web_contents(),
75 true, // TODO(mlamouri): should be removed, see http://crbug.com/423770
76 base::Bind(&PermissionServiceImpl::OnRequestPermissionResponse
,
77 weak_factory_
.GetWeakPtr(),
82 void PermissionServiceImpl::OnRequestPermissionResponse(
83 const mojo::Callback
<void(PermissionStatus
)>& callback
,
86 pending_requests_
.Remove(request_id
);
88 // TODO(mlamouri): for now, we only get a boolean back, but we would ideally
89 // need a ContentSetting, see http://crbug.com/432978
90 callback
.Run(allowed
? PERMISSION_STATUS_GRANTED
: PERMISSION_STATUS_ASK
);
93 void PermissionServiceImpl::CancelPendingRequests() {
94 DCHECK(context_
->web_contents());
96 for (RequestsMap::Iterator
<PendingRequest
> it(&pending_requests_
);
97 !it
.IsAtEnd(); it
.Advance()) {
98 GetContentClient()->browser()->CancelPermissionRequest(
99 it
.GetCurrentValue()->permission
,
100 context_
->web_contents(),
102 it
.GetCurrentValue()->origin
);
105 pending_requests_
.Clear();
108 void PermissionServiceImpl::HasPermission(
109 PermissionName permission
,
110 const mojo::String
& origin
,
111 const mojo::Callback
<void(PermissionStatus
)>& callback
) {
115 } // namespace content