Chromium Code Reviews| Index: content/renderer/media/media_permission_dispatcher.cc |
| diff --git a/content/renderer/media/media_permission_dispatcher.cc b/content/renderer/media/media_permission_dispatcher.cc |
| index 666d194e76b901963bddba78ea951754384d2a6f..14d29285495d71abea57f2f443fad4a9015ffa4f 100644 |
| --- a/content/renderer/media/media_permission_dispatcher.cc |
| +++ b/content/renderer/media/media_permission_dispatcher.cc |
| @@ -4,14 +4,46 @@ |
| #include "content/renderer/media/media_permission_dispatcher.h" |
| +#include "base/bind.h" |
| #include "base/logging.h" |
| +#include "base/single_thread_task_runner.h" |
| +#include "base/thread_task_runner_handle.h" |
| +#include "content/public/common/service_registry.h" |
| +#include "content/public/renderer/render_frame.h" |
| +#include "media/base/bind_to_current_loop.h" |
| +#include "third_party/WebKit/public/web/WebUserGestureIndicator.h" |
| +#include "url/gurl.h" |
| + |
| +namespace { |
| + |
| +using Type = media::MediaPermission::Type; |
| + |
| +content::PermissionName MediaPermissionTypeToPermissionName(Type type) { |
| + switch (type) { |
| + case Type::PROTECTED_MEDIA_IDENTIFIER: |
| + return content::PERMISSION_NAME_PROTECTED_MEDIA_IDENTIFIER; |
| + case Type::AUDIO_CAPTURE: |
| + return content::PERMISSION_NAME_AUDIO_CAPTURE; |
| + case Type::VIDEO_CAPTURE: |
| + return content::PERMISSION_NAME_VIDEO_CAPTURE; |
| + } |
| + NOTREACHED(); |
| + return content::PERMISSION_NAME_PROTECTED_MEDIA_IDENTIFIER; |
| +} |
| + |
| +} // namespace |
| namespace content { |
| -MediaPermissionDispatcher::MediaPermissionDispatcher() : next_request_id_(0) {} |
| +MediaPermissionDispatcher::MediaPermissionDispatcher(RenderFrame* render_frame) |
| + : RenderFrameObserver(render_frame), |
| + task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| + next_request_id_(0), |
| + weak_factory_(this) {} |
| MediaPermissionDispatcher::~MediaPermissionDispatcher() { |
| - DCHECK(thread_checker_.CalledOnValidThread()); |
| + DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| + |
| // Fire all pending callbacks with |false|. |
| for (auto& request : requests_) |
| request.second.Run(false); |
| @@ -19,19 +51,80 @@ MediaPermissionDispatcher::~MediaPermissionDispatcher() { |
| requests_.clear(); |
| } |
| +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
|
| + Type type, |
| + const GURL& security_origin, |
| + const PermissionStatusCB& permission_status_cb) { |
| + if (!task_runner_->RunsTasksOnCurrentThread()) { |
| + // Use BindToCurrentLoop to bind the callback to the current thread. |
| + task_runner_->PostTask( |
| + FROM_HERE, base::Bind(&MediaPermissionDispatcher::HasPermission, |
| + 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
|
| + media::BindToCurrentLoop(permission_status_cb))); |
| + return; |
| + } |
| + |
| + DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| + |
| + if (!permission_service_.get()) { |
| + render_frame()->GetServiceRegistry()->ConnectToRemoteService( |
| + mojo::GetProxy(&permission_service_)); |
| + } |
| + |
| + int request_id = RegisterCallback(permission_status_cb); |
| + DVLOG(2) << __FUNCTION__ << ": request ID " << request_id; |
| + |
| + permission_service_->HasPermission( |
| + MediaPermissionTypeToPermissionName(type), security_origin.spec(), |
| + base::Bind(&MediaPermissionDispatcher::OnPermissionStatus, |
| + base::Unretained(this), request_id)); |
| +} |
| + |
| +void MediaPermissionDispatcher::RequestPermission( |
| + Type type, |
| + const GURL& security_origin, |
| + const PermissionStatusCB& permission_status_cb) { |
| + if (!task_runner_->RunsTasksOnCurrentThread()) { |
| + // Use BindToCurrentLoop to bind the callback to the current thread. |
| + task_runner_->PostTask( |
| + FROM_HERE, base::Bind(&MediaPermissionDispatcher::RequestPermission, |
| + weak_factory_.GetWeakPtr(), type, security_origin, |
| + media::BindToCurrentLoop(permission_status_cb))); |
| + return; |
| + } |
| + |
| + DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| + |
| + if (!permission_service_.get()) { |
| + render_frame()->GetServiceRegistry()->ConnectToRemoteService( |
| + mojo::GetProxy(&permission_service_)); |
| + } |
| + |
| + int request_id = RegisterCallback(permission_status_cb); |
| + DVLOG(2) << __FUNCTION__ << ": request ID " << request_id; |
| + |
| + permission_service_->RequestPermission( |
| + MediaPermissionTypeToPermissionName(type), security_origin.spec(), |
| + blink::WebUserGestureIndicator::isProcessingUserGesture(), |
| + base::Bind(&MediaPermissionDispatcher::OnPermissionStatus, |
| + base::Unretained(this), request_id)); |
| +} |
| + |
| uint32_t MediaPermissionDispatcher::RegisterCallback( |
| const PermissionStatusCB& permission_status_cb) { |
| - DCHECK(thread_checker_.CalledOnValidThread()); |
| + DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| + |
| uint32_t request_id = next_request_id_++; |
| DCHECK(!requests_.count(request_id)); |
| requests_[request_id] = permission_status_cb; |
| + |
| return request_id; |
| } |
| -void MediaPermissionDispatcher::DeliverResult(uint32_t request_id, |
| - bool granted) { |
| - DCHECK(thread_checker_.CalledOnValidThread()); |
| - DVLOG(2) << __FUNCTION__ << ": (" << request_id << ", " << granted << ")"; |
| +void MediaPermissionDispatcher::OnPermissionStatus(uint32_t request_id, |
| + PermissionStatus status) { |
| + DVLOG(2) << __FUNCTION__ << ": (" << request_id << ", " << status << ")"; |
| + DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| RequestMap::iterator iter = requests_.find(request_id); |
| if (iter == requests_.end()) { |
| @@ -42,7 +135,7 @@ void MediaPermissionDispatcher::DeliverResult(uint32_t request_id, |
| PermissionStatusCB permission_status_cb = iter->second; |
| requests_.erase(iter); |
| - permission_status_cb.Run(granted); |
| + permission_status_cb.Run(status == PERMISSION_STATUS_GRANTED); |
| } |
| } // namespace content |