Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/renderer/media/media_permission_dispatcher.h" | 5 #include "content/renderer/media/media_permission_dispatcher.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | |
| 7 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/single_thread_task_runner.h" | |
| 10 #include "base/thread_task_runner_handle.h" | |
| 11 #include "content/public/common/service_registry.h" | |
| 12 #include "content/public/renderer/render_frame.h" | |
| 13 #include "media/base/bind_to_current_loop.h" | |
| 14 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h" | |
| 15 #include "url/gurl.h" | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 using Type = media::MediaPermission::Type; | |
| 20 | |
| 21 content::PermissionName MediaPermissionTypeToPermissionName(Type type) { | |
| 22 switch (type) { | |
| 23 case Type::PROTECTED_MEDIA_IDENTIFIER: | |
| 24 return content::PERMISSION_NAME_PROTECTED_MEDIA_IDENTIFIER; | |
| 25 case Type::AUDIO_CAPTURE: | |
| 26 return content::PERMISSION_NAME_AUDIO_CAPTURE; | |
| 27 case Type::VIDEO_CAPTURE: | |
| 28 return content::PERMISSION_NAME_VIDEO_CAPTURE; | |
| 29 } | |
| 30 NOTREACHED(); | |
| 31 return content::PERMISSION_NAME_PROTECTED_MEDIA_IDENTIFIER; | |
| 32 } | |
| 33 | |
| 34 } // namespace | |
| 8 | 35 |
| 9 namespace content { | 36 namespace content { |
| 10 | 37 |
| 11 MediaPermissionDispatcher::MediaPermissionDispatcher() : next_request_id_(0) {} | 38 MediaPermissionDispatcher::MediaPermissionDispatcher(RenderFrame* render_frame) |
| 39 : RenderFrameObserver(render_frame), | |
| 40 task_runner_(base::ThreadTaskRunnerHandle::Get()), | |
| 41 next_request_id_(0), | |
| 42 weak_factory_(this) {} | |
| 12 | 43 |
| 13 MediaPermissionDispatcher::~MediaPermissionDispatcher() { | 44 MediaPermissionDispatcher::~MediaPermissionDispatcher() { |
| 14 DCHECK(thread_checker_.CalledOnValidThread()); | 45 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 46 | |
| 15 // Fire all pending callbacks with |false|. | 47 // Fire all pending callbacks with |false|. |
| 16 for (auto& request : requests_) | 48 for (auto& request : requests_) |
| 17 request.second.Run(false); | 49 request.second.Run(false); |
| 18 | 50 |
| 19 requests_.clear(); | 51 requests_.clear(); |
| 20 } | 52 } |
| 21 | 53 |
| 54 void MediaPermissionDispatcher::HasPermission( | |
|
Sergey Ulanov
2015/11/24 21:08:42
So this method can be called from any thread and y
xhwang
2015/11/24 22:54:59
Good point! The MediaPermission has the same lifet
guoweis_left_chromium
2015/11/24 23:15:07
PeerConnectionDependencyFactory is owned by Render
| |
| 55 Type type, | |
| 56 const GURL& security_origin, | |
| 57 const PermissionStatusCB& permission_status_cb) { | |
| 58 if (!task_runner_->RunsTasksOnCurrentThread()) { | |
| 59 // Use BindToCurrentLoop to bind the callback to the current thread. | |
| 60 task_runner_->PostTask( | |
| 61 FROM_HERE, base::Bind(&MediaPermissionDispatcher::HasPermission, | |
| 62 weak_factory_.GetWeakPtr(), type, security_origin, | |
|
Sergey Ulanov
2015/11/24 21:08:42
You are posting a task to a different thread using
xhwang
2015/11/24 22:54:59
From weak_ptr.h:
// To ensure correct use, the fi
Wez
2015/11/25 00:44:48
That's not true; it's only required that the WeakP
Sergey Ulanov
2015/11/25 01:01:36
What happens here is that the object is created an
Wez
2015/11/25 01:09:00
OK, gotcha! It _is_ safe to pass such a WeakPtr o
xhwang
2015/11/25 01:18:02
Will do.
Actually I saw cases where we have two W
Wez
2015/11/25 02:09:54
Do you mean that you saw a WeakPtrFactory, and a W
xhwang
2015/11/25 07:06:03
(omitting old comments)
Wez
2015/11/25 20:01:40
tl;dr: That code is overcomplicated and should be
| |
| 63 media::BindToCurrentLoop(permission_status_cb))); | |
| 64 return; | |
| 65 } | |
| 66 | |
| 67 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | |
| 68 | |
| 69 if (!permission_service_.get()) { | |
| 70 render_frame()->GetServiceRegistry()->ConnectToRemoteService( | |
| 71 mojo::GetProxy(&permission_service_)); | |
| 72 } | |
| 73 | |
| 74 int request_id = RegisterCallback(permission_status_cb); | |
| 75 DVLOG(2) << __FUNCTION__ << ": request ID " << request_id; | |
| 76 | |
| 77 permission_service_->HasPermission( | |
| 78 MediaPermissionTypeToPermissionName(type), security_origin.spec(), | |
| 79 base::Bind(&MediaPermissionDispatcher::OnPermissionStatus, | |
| 80 base::Unretained(this), request_id)); | |
| 81 } | |
| 82 | |
| 83 void MediaPermissionDispatcher::RequestPermission( | |
| 84 Type type, | |
| 85 const GURL& security_origin, | |
| 86 const PermissionStatusCB& permission_status_cb) { | |
| 87 if (!task_runner_->RunsTasksOnCurrentThread()) { | |
| 88 // Use BindToCurrentLoop to bind the callback to the current thread. | |
| 89 task_runner_->PostTask( | |
| 90 FROM_HERE, base::Bind(&MediaPermissionDispatcher::RequestPermission, | |
| 91 weak_factory_.GetWeakPtr(), type, security_origin, | |
| 92 media::BindToCurrentLoop(permission_status_cb))); | |
| 93 return; | |
| 94 } | |
| 95 | |
| 96 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | |
| 97 | |
| 98 if (!permission_service_.get()) { | |
| 99 render_frame()->GetServiceRegistry()->ConnectToRemoteService( | |
| 100 mojo::GetProxy(&permission_service_)); | |
| 101 } | |
| 102 | |
| 103 int request_id = RegisterCallback(permission_status_cb); | |
| 104 DVLOG(2) << __FUNCTION__ << ": request ID " << request_id; | |
| 105 | |
| 106 permission_service_->RequestPermission( | |
| 107 MediaPermissionTypeToPermissionName(type), security_origin.spec(), | |
| 108 blink::WebUserGestureIndicator::isProcessingUserGesture(), | |
| 109 base::Bind(&MediaPermissionDispatcher::OnPermissionStatus, | |
| 110 base::Unretained(this), request_id)); | |
| 111 } | |
| 112 | |
| 22 uint32_t MediaPermissionDispatcher::RegisterCallback( | 113 uint32_t MediaPermissionDispatcher::RegisterCallback( |
| 23 const PermissionStatusCB& permission_status_cb) { | 114 const PermissionStatusCB& permission_status_cb) { |
| 24 DCHECK(thread_checker_.CalledOnValidThread()); | 115 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 116 | |
| 25 uint32_t request_id = next_request_id_++; | 117 uint32_t request_id = next_request_id_++; |
| 26 DCHECK(!requests_.count(request_id)); | 118 DCHECK(!requests_.count(request_id)); |
| 27 requests_[request_id] = permission_status_cb; | 119 requests_[request_id] = permission_status_cb; |
| 120 | |
| 28 return request_id; | 121 return request_id; |
| 29 } | 122 } |
| 30 | 123 |
| 31 void MediaPermissionDispatcher::DeliverResult(uint32_t request_id, | 124 void MediaPermissionDispatcher::OnPermissionStatus(uint32_t request_id, |
| 32 bool granted) { | 125 PermissionStatus status) { |
| 33 DCHECK(thread_checker_.CalledOnValidThread()); | 126 DVLOG(2) << __FUNCTION__ << ": (" << request_id << ", " << status << ")"; |
| 34 DVLOG(2) << __FUNCTION__ << ": (" << request_id << ", " << granted << ")"; | 127 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 35 | 128 |
| 36 RequestMap::iterator iter = requests_.find(request_id); | 129 RequestMap::iterator iter = requests_.find(request_id); |
| 37 if (iter == requests_.end()) { | 130 if (iter == requests_.end()) { |
| 38 DVLOG(2) << "Request not found."; | 131 DVLOG(2) << "Request not found."; |
| 39 return; | 132 return; |
| 40 } | 133 } |
| 41 | 134 |
| 42 PermissionStatusCB permission_status_cb = iter->second; | 135 PermissionStatusCB permission_status_cb = iter->second; |
| 43 requests_.erase(iter); | 136 requests_.erase(iter); |
| 44 | 137 |
| 45 permission_status_cb.Run(granted); | 138 permission_status_cb.Run(status == PERMISSION_STATUS_GRANTED); |
| 46 } | 139 } |
| 47 | 140 |
| 48 } // namespace content | 141 } // namespace content |
| OLD | NEW |