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<MediaPermissionDispatcherProxy> 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 private: | |
| 36 // Reports the result on the caller's thread. | |
| 37 void ReportResult(uint32_t request_id, bool granted); | |
| 38 | |
| 39 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; | |
| 40 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_; | |
| 41 base::WeakPtr<MediaPermissionDispatcherProxy> parent_; | |
| 42 base::WeakPtr<MediaPermissionDispatcher> media_permission_; | |
| 43 | |
| 44 DISALLOW_COPY_AND_ASSIGN(Core); | |
| 45 }; | |
| 46 | |
| 47 MediaPermissionDispatcherProxy::Core::Core( | |
| 48 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, | |
| 49 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, | |
| 50 base::WeakPtr<MediaPermissionDispatcherProxy> parent, | |
| 51 base::WeakPtr<MediaPermissionDispatcher> media_permission) | |
| 52 : ui_task_runner_(ui_task_runner), | |
| 53 caller_task_runner_(caller_task_runner), | |
| 54 parent_(parent), | |
| 55 media_permission_(media_permission) {} | |
| 56 | |
| 57 MediaPermissionDispatcherProxy::Core::~Core() { | |
| 58 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | |
| 59 } | |
| 60 | |
| 61 void MediaPermissionDispatcherProxy::Core::HasPermission( | |
| 62 Type type, | |
| 63 const GURL& security_origin, | |
| 64 uint32_t request_id) { | |
| 65 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | |
| 66 | |
| 67 if (media_permission_.get()) { | |
| 68 media_permission_->HasPermission( | |
| 69 type, security_origin, | |
| 70 base::Bind(&MediaPermissionDispatcherProxy::Core::ReportResult, | |
| 71 base::Unretained(this), request_id)); | |
|
perkj_chrome
2015/09/22 08:24:16
is this safe?
guoweis_left_chromium
2015/09/22 18:48:49
Done.
| |
| 72 } else { | |
| 73 ReportResult(request_id, false); | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 void MediaPermissionDispatcherProxy::Core::ReportResult(uint32_t request_id, | |
| 78 bool result) { | |
| 79 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | |
| 80 caller_task_runner_->PostTask( | |
| 81 FROM_HERE, base::Bind(&MediaPermissionDispatcherProxy::DeliverResult, | |
| 82 parent_, request_id, result)); | |
| 83 } | |
| 84 | |
| 85 MediaPermissionDispatcherProxy::MediaPermissionDispatcherProxy( | |
| 86 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, | |
| 87 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, | |
| 88 base::WeakPtr<MediaPermissionDispatcher> media_permission) { | |
| 89 core_.reset(new Core(ui_task_runner, caller_task_runner, | |
| 90 base::AsWeakPtr(this), media_permission)); | |
| 91 } | |
| 92 | |
| 93 MediaPermissionDispatcherProxy::~MediaPermissionDispatcherProxy() { | |
| 94 DCHECK(core_->caller_task_runner()->BelongsToCurrentThread()); | |
| 95 | |
| 96 auto ui_task_runner = core_->ui_task_runner(); | |
| 97 ui_task_runner->DeleteSoon(FROM_HERE, core_.release()); | |
| 98 } | |
| 99 | |
| 100 void MediaPermissionDispatcherProxy::HasPermission( | |
| 101 Type type, | |
| 102 const GURL& security_origin, | |
| 103 const PermissionStatusCB& permission_status_cb) { | |
| 104 DCHECK(core_->caller_task_runner()->BelongsToCurrentThread()); | |
| 105 | |
| 106 core_->ui_task_runner()->PostTask( | |
| 107 FROM_HERE, | |
| 108 base::Bind(&Core::HasPermission, base::Unretained(core_.get()), type, | |
|
perkj_chrome
2015/09/22 08:24:16
Why is Unretained safe here? Document.
guoweis_left_chromium
2015/09/22 18:48:49
Done.
| |
| 109 security_origin, RegisterCallback(permission_status_cb))); | |
| 110 } | |
| 111 | |
| 112 void MediaPermissionDispatcherProxy::RequestPermission( | |
| 113 Type type, | |
| 114 const GURL& security_origin, | |
| 115 const PermissionStatusCB& permission_status_cb) { | |
| 116 DCHECK(core_->caller_task_runner()->BelongsToCurrentThread()); | |
| 117 NOTREACHED() | |
| 118 << "RequestPermission is not supported in MediaPermissionDispatcherProxy"; | |
|
perkj_chrome
2015/09/22 08:24:16
why not implement this too at the same time?
guoweis_left_chromium
2015/09/22 18:48:49
I believe further down, this could trigger a promp
| |
| 119 if (!permission_status_cb.is_null()) | |
| 120 permission_status_cb.Run(false); | |
| 121 } | |
| 122 | |
| 123 } // namespace content | |
| OLD | NEW |