Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / content / renderer / media / media_permission_dispatcher.cc
blob7c08edd4ab883cb8867b7e17696d6502ef4ac2de
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"
7 #include "base/bind.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"
12 #include "url/gurl.h"
14 namespace content {
16 MediaPermissionDispatcher::MediaPermissionDispatcher(RenderFrame* render_frame)
17 : RenderFrameObserver(render_frame), next_request_id_(0) {
20 MediaPermissionDispatcher::~MediaPermissionDispatcher() {
21 DCHECK(thread_checker_.CalledOnValidThread());
23 // Fire all pending callbacks with |false|.
24 for (auto& request : requests_)
25 request.second.Run(false);
27 requests_.clear();
30 void MediaPermissionDispatcher::HasPermission(
31 Type type,
32 const GURL& security_origin,
33 const PermissionStatusCB& permission_status_cb) {
34 DCHECK(thread_checker_.CalledOnValidThread());
35 CHECK_EQ(PROTECTED_MEDIA_IDENTIFIER, type);
37 if (!permission_service_.get()) {
38 render_frame()->GetServiceRegistry()->ConnectToRemoteService(
39 mojo::GetProxy(&permission_service_));
42 uint32_t request_id = next_request_id_++;
43 DCHECK(!requests_.count(request_id));
44 requests_[request_id] = permission_status_cb;
46 DVLOG(2) << __FUNCTION__ << ": request ID " << request_id;
48 permission_service_->HasPermission(
49 PERMISSION_NAME_PROTECTED_MEDIA_IDENTIFIER, security_origin.spec(),
50 base::Bind(&MediaPermissionDispatcher::OnPermissionStatus,
51 base::Unretained(this), request_id));
54 void MediaPermissionDispatcher::RequestPermission(
55 Type type,
56 const GURL& security_origin,
57 const PermissionStatusCB& permission_status_cb) {
58 DCHECK(thread_checker_.CalledOnValidThread());
59 CHECK_EQ(PROTECTED_MEDIA_IDENTIFIER, type);
61 if (!permission_service_.get()) {
62 render_frame()->GetServiceRegistry()->ConnectToRemoteService(
63 mojo::GetProxy(&permission_service_));
66 uint32_t request_id = next_request_id_++;
67 DCHECK(!requests_.count(request_id));
68 requests_[request_id] = permission_status_cb;
70 DVLOG(2) << __FUNCTION__ << ": request ID " << request_id;
72 permission_service_->RequestPermission(
73 PERMISSION_NAME_PROTECTED_MEDIA_IDENTIFIER, security_origin.spec(),
74 blink::WebUserGestureIndicator::isProcessingUserGesture(),
75 base::Bind(&MediaPermissionDispatcher::OnPermissionStatus,
76 base::Unretained(this), request_id));
79 void MediaPermissionDispatcher::OnPermissionStatus(uint32_t request_id,
80 PermissionStatus status) {
81 DVLOG(2) << __FUNCTION__ << ": (" << request_id << ", " << status << ")";
82 DCHECK(thread_checker_.CalledOnValidThread());
84 RequestMap::iterator iter = requests_.find(request_id);
85 if (iter == requests_.end()) {
86 DVLOG(2) << "Request not found.";
87 return;
90 PermissionStatusCB permission_status_cb = iter->second;
91 requests_.erase(iter);
93 permission_status_cb.Run(status == PERMISSION_STATUS_GRANTED);
96 } // namespace content