Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "content/renderer/media/media_permission_dispatcher_proxy.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/callback_helpers.h" | |
| 9 #include "base/location.h" | |
| 10 #include "base/thread_task_runner_handle.h" | |
| 11 #include "url/gurl.h" | |
| 12 | |
| 13 namespace content { | |
| 14 | |
| 15 class MediaPermissionDispatcherProxy::Core { | |
| 16 public: | |
| 17 Core(scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, | |
| 18 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, | |
| 19 base::WeakPtr<MediaPermissionDispatcher> parent, | |
| 20 base::WeakPtr<MediaPermissionDispatcher> media_permission_dispatcher); | |
| 21 ~Core(); | |
| 22 | |
| 23 void HasPermission(Type type, | |
| 24 const GURL& security_origin, | |
| 25 uint32_t request_id); | |
| 26 | |
| 27 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner() { | |
| 28 return ui_task_runner_; | |
| 29 } | |
| 30 | |
| 31 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner() { | |
| 32 return caller_task_runner_; | |
| 33 } | |
| 34 | |
| 35 base::WeakPtr<MediaPermissionDispatcherProxy::Core> GetWeakPtr() { | |
| 36 return weak_ptr_factory_.GetWeakPtr(); | |
| 37 } | |
| 38 | |
| 39 private: | |
| 40 // Reports the result on the caller's thread. | |
| 41 void ReportResult(uint32_t request_id, bool granted); | |
| 42 | |
| 43 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; | |
| 44 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_; | |
| 45 | |
| 46 // |parent_| is the proxy which holds this Core. | |
| 47 base::WeakPtr<MediaPermissionDispatcher> parent_; | |
| 48 | |
| 49 // |media_permission_| is the real implementation which calls | |
| 50 // |PermissionServices on the UI thread. | |
|
perkj_chrome
2015/09/23 13:25:23
nit - remove extra | around PermissionServices
| |
| 51 base::WeakPtr<MediaPermissionDispatcher> media_permission_; | |
| 52 | |
| 53 base::WeakPtrFactory<MediaPermissionDispatcherProxy::Core> weak_ptr_factory_; | |
| 54 | |
| 55 DISALLOW_COPY_AND_ASSIGN(Core); | |
| 56 }; | |
| 57 | |
| 58 MediaPermissionDispatcherProxy::Core::Core( | |
| 59 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, | |
| 60 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, | |
| 61 base::WeakPtr<MediaPermissionDispatcher> parent, | |
| 62 base::WeakPtr<MediaPermissionDispatcher> media_permission) | |
| 63 : ui_task_runner_(ui_task_runner), | |
| 64 caller_task_runner_(caller_task_runner), | |
| 65 parent_(parent), | |
| 66 media_permission_(media_permission), | |
| 67 weak_ptr_factory_(this) {} | |
| 68 | |
| 69 MediaPermissionDispatcherProxy::Core::~Core() { | |
| 70 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | |
| 71 } | |
| 72 | |
| 73 void MediaPermissionDispatcherProxy::Core::HasPermission( | |
| 74 Type type, | |
| 75 const GURL& security_origin, | |
| 76 uint32_t request_id) { | |
| 77 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | |
| 78 | |
| 79 if (media_permission_.get()) { | |
| 80 media_permission_->HasPermission( | |
| 81 type, security_origin, | |
| 82 base::Bind(&MediaPermissionDispatcherProxy::Core::ReportResult, | |
| 83 GetWeakPtr(), request_id)); | |
| 84 } else { | |
| 85 ReportResult(request_id, false); | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 void MediaPermissionDispatcherProxy::Core::ReportResult(uint32_t request_id, | |
| 90 bool result) { | |
| 91 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | |
| 92 caller_task_runner_->PostTask( | |
| 93 FROM_HERE, base::Bind(&MediaPermissionDispatcherProxy::DeliverResult, | |
| 94 parent_, request_id, result)); | |
| 95 } | |
| 96 | |
| 97 MediaPermissionDispatcherProxy::MediaPermissionDispatcherProxy( | |
| 98 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, | |
| 99 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, | |
| 100 base::WeakPtr<MediaPermissionDispatcher> media_permission) | |
| 101 : weak_ptr_factory_(this) { | |
| 102 thread_checker().DetachFromThread(); | |
| 103 core_.reset(new Core(ui_task_runner, caller_task_runner, | |
| 104 weak_ptr_factory_.GetWeakPtr(), media_permission)); | |
| 105 } | |
| 106 | |
| 107 MediaPermissionDispatcherProxy::~MediaPermissionDispatcherProxy() { | |
| 108 DCHECK(core_->caller_task_runner()->BelongsToCurrentThread()); | |
| 109 | |
| 110 auto ui_task_runner = core_->ui_task_runner(); | |
| 111 ui_task_runner->DeleteSoon(FROM_HERE, core_.release()); | |
| 112 } | |
| 113 | |
| 114 void MediaPermissionDispatcherProxy::HasPermission( | |
| 115 Type type, | |
| 116 const GURL& security_origin, | |
| 117 const PermissionStatusCB& permission_status_cb) { | |
| 118 DCHECK(core_->caller_task_runner()->BelongsToCurrentThread()); | |
| 119 | |
| 120 core_->ui_task_runner()->PostTask( | |
| 121 FROM_HERE, | |
| 122 base::Bind(&Core::HasPermission, core_->GetWeakPtr(), type, | |
| 123 security_origin, RegisterCallback(permission_status_cb))); | |
| 124 } | |
| 125 | |
| 126 // This is left as unimplemented at this point. The reason is that | |
| 127 // RequestPermission could trigger UX prompt once MediaStreamDevicesController | |
| 128 // is merged into MediaStreamDevicePermissionContext. | |
| 129 void MediaPermissionDispatcherProxy::RequestPermission( | |
| 130 Type type, | |
| 131 const GURL& security_origin, | |
| 132 const PermissionStatusCB& permission_status_cb) { | |
| 133 DCHECK(core_->caller_task_runner()->BelongsToCurrentThread()); | |
| 134 NOTREACHED() | |
| 135 << "RequestPermission is not supported in MediaPermissionDispatcherProxy"; | |
| 136 if (!permission_status_cb.is_null()) | |
| 137 permission_status_cb.Run(false); | |
| 138 } | |
| 139 | |
| 140 } // namespace content | |
| OLD | NEW |