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 "content/renderer/media/media_permission_dispatcher.h"
8 #include "base/logging.h"
9 #include "content/public/common/service_registry.h"
10 #include "content/public/renderer/render_frame.h"
11 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h"
16 using Type
= media::MediaPermission::Type
;
18 content::PermissionName
MediaPermissionTypeToPermissionName(Type type
) {
20 case Type::PROTECTED_MEDIA_IDENTIFIER
:
21 return content::PERMISSION_NAME_PROTECTED_MEDIA_IDENTIFIER
;
22 case Type::AUDIO_CAPTURE
:
23 return content::PERMISSION_NAME_AUDIO_CAPTURE
;
24 case Type::VIDEO_CAPTURE
:
25 return content::PERMISSION_NAME_VIDEO_CAPTURE
;
28 return content::PERMISSION_NAME_PROTECTED_MEDIA_IDENTIFIER
;
35 MediaPermissionDispatcher::MediaPermissionDispatcher(RenderFrame
* render_frame
)
36 : RenderFrameObserver(render_frame
), next_request_id_(0) {
39 MediaPermissionDispatcher::~MediaPermissionDispatcher() {
40 DCHECK(thread_checker_
.CalledOnValidThread());
42 // Fire all pending callbacks with |false|.
43 for (auto& request
: requests_
)
44 request
.second
.Run(false);
49 void MediaPermissionDispatcher::HasPermission(
51 const GURL
& security_origin
,
52 const PermissionStatusCB
& permission_status_cb
) {
53 DCHECK(thread_checker_
.CalledOnValidThread());
55 if (!permission_service_
.get()) {
56 render_frame()->GetServiceRegistry()->ConnectToRemoteService(
57 mojo::GetProxy(&permission_service_
));
60 uint32_t request_id
= next_request_id_
++;
61 DCHECK(!requests_
.count(request_id
));
62 requests_
[request_id
] = permission_status_cb
;
64 DVLOG(2) << __FUNCTION__
<< ": request ID " << request_id
;
66 permission_service_
->HasPermission(
67 MediaPermissionTypeToPermissionName(type
), security_origin
.spec(),
68 base::Bind(&MediaPermissionDispatcher::OnPermissionStatus
,
69 base::Unretained(this), request_id
));
72 void MediaPermissionDispatcher::RequestPermission(
74 const GURL
& security_origin
,
75 const PermissionStatusCB
& permission_status_cb
) {
76 DCHECK(thread_checker_
.CalledOnValidThread());
78 if (!permission_service_
.get()) {
79 render_frame()->GetServiceRegistry()->ConnectToRemoteService(
80 mojo::GetProxy(&permission_service_
));
83 uint32_t request_id
= next_request_id_
++;
84 DCHECK(!requests_
.count(request_id
));
85 requests_
[request_id
] = permission_status_cb
;
87 DVLOG(2) << __FUNCTION__
<< ": request ID " << request_id
;
89 permission_service_
->RequestPermission(
90 MediaPermissionTypeToPermissionName(type
), security_origin
.spec(),
91 blink::WebUserGestureIndicator::isProcessingUserGesture(),
92 base::Bind(&MediaPermissionDispatcher::OnPermissionStatus
,
93 base::Unretained(this), request_id
));
96 void MediaPermissionDispatcher::OnPermissionStatus(uint32_t request_id
,
97 PermissionStatus status
) {
98 DVLOG(2) << __FUNCTION__
<< ": (" << request_id
<< ", " << status
<< ")";
99 DCHECK(thread_checker_
.CalledOnValidThread());
101 RequestMap::iterator iter
= requests_
.find(request_id
);
102 if (iter
== requests_
.end()) {
103 DVLOG(2) << "Request not found.";
107 PermissionStatusCB permission_status_cb
= iter
->second
;
108 requests_
.erase(iter
);
110 permission_status_cb
.Run(status
== PERMISSION_STATUS_GRANTED
);
113 } // namespace content