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 <stdint.h> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/callback_helpers.h" | |
11 #include "base/location.h" | |
12 #include "base/macros.h" | |
13 #include "base/thread_task_runner_handle.h" | |
14 #include "url/gurl.h" | |
15 | |
16 namespace content { | |
17 | |
18 class MediaPermissionDispatcherProxy::Core { | |
19 public: | |
20 Core(scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, | |
21 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, | |
22 base::WeakPtr<MediaPermissionDispatcher> parent, | |
23 base::WeakPtr<MediaPermissionDispatcher> media_permission_dispatcher); | |
24 ~Core(); | |
25 | |
26 void HasPermission(Type type, | |
27 const GURL& security_origin, | |
28 uint32_t request_id); | |
29 | |
30 void RequestPermission(Type type, | |
31 const GURL& security_origin, | |
32 uint32_t request_id); | |
33 | |
34 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner() { | |
35 return ui_task_runner_; | |
36 } | |
37 | |
38 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner() { | |
39 return caller_task_runner_; | |
40 } | |
41 | |
42 base::WeakPtr<MediaPermissionDispatcherProxy::Core> GetWeakPtr() { | |
43 return weak_ptr_factory_.GetWeakPtr(); | |
44 } | |
45 | |
46 private: | |
47 // Reports the result on the caller's thread. | |
48 void ReportResult(uint32_t request_id, bool granted); | |
49 | |
50 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; | |
51 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_; | |
52 | |
53 // |parent_| is the proxy which holds this Core. | |
54 base::WeakPtr<MediaPermissionDispatcher> parent_; | |
55 | |
56 // |media_permission_| is the real implementation which calls | |
57 // PermissionServices on the UI thread. | |
58 base::WeakPtr<MediaPermissionDispatcher> media_permission_; | |
59 | |
60 base::WeakPtrFactory<MediaPermissionDispatcherProxy::Core> weak_ptr_factory_; | |
61 | |
62 DISALLOW_COPY_AND_ASSIGN(Core); | |
63 }; | |
64 | |
65 MediaPermissionDispatcherProxy::Core::Core( | |
66 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, | |
67 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, | |
68 base::WeakPtr<MediaPermissionDispatcher> parent, | |
69 base::WeakPtr<MediaPermissionDispatcher> media_permission) | |
70 : ui_task_runner_(ui_task_runner), | |
71 caller_task_runner_(caller_task_runner), | |
72 parent_(parent), | |
73 media_permission_(media_permission), | |
74 weak_ptr_factory_(this) {} | |
75 | |
76 MediaPermissionDispatcherProxy::Core::~Core() { | |
77 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | |
78 } | |
79 | |
80 void MediaPermissionDispatcherProxy::Core::HasPermission( | |
81 Type type, | |
82 const GURL& security_origin, | |
83 uint32_t request_id) { | |
84 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | |
85 | |
86 if (media_permission_.get()) { | |
87 media_permission_->HasPermission( | |
88 type, security_origin, | |
89 base::Bind(&MediaPermissionDispatcherProxy::Core::ReportResult, | |
90 GetWeakPtr(), request_id)); | |
91 } else { | |
92 ReportResult(request_id, false); | |
93 } | |
94 } | |
95 | |
96 void MediaPermissionDispatcherProxy::Core::RequestPermission( | |
97 Type type, | |
98 const GURL& security_origin, | |
99 uint32_t request_id) { | |
100 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | |
101 | |
102 if (media_permission_.get()) { | |
103 media_permission_->RequestPermission( | |
104 type, security_origin, | |
105 base::Bind(&MediaPermissionDispatcherProxy::Core::ReportResult, | |
106 GetWeakPtr(), request_id)); | |
107 } else { | |
108 ReportResult(request_id, false); | |
109 } | |
110 } | |
111 | |
112 void MediaPermissionDispatcherProxy::Core::ReportResult(uint32_t request_id, | |
113 bool result) { | |
114 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | |
115 caller_task_runner_->PostTask( | |
116 FROM_HERE, base::Bind(&MediaPermissionDispatcherProxy::DeliverResult, | |
117 parent_, request_id, result)); | |
118 } | |
119 | |
120 MediaPermissionDispatcherProxy::MediaPermissionDispatcherProxy( | |
121 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, | |
122 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, | |
123 base::WeakPtr<MediaPermissionDispatcher> media_permission) | |
124 : weak_ptr_factory_(this) { | |
125 thread_checker().DetachFromThread(); | |
126 core_.reset(new Core(ui_task_runner, caller_task_runner, | |
127 weak_ptr_factory_.GetWeakPtr(), media_permission)); | |
128 } | |
129 | |
130 MediaPermissionDispatcherProxy::~MediaPermissionDispatcherProxy() { | |
131 DCHECK(core_->caller_task_runner()->BelongsToCurrentThread()); | |
132 | |
133 auto ui_task_runner = core_->ui_task_runner(); | |
134 ui_task_runner->DeleteSoon(FROM_HERE, core_.release()); | |
135 } | |
136 | |
137 void MediaPermissionDispatcherProxy::HasPermission( | |
138 Type type, | |
139 const GURL& security_origin, | |
140 const PermissionStatusCB& permission_status_cb) { | |
141 DCHECK(core_->caller_task_runner()->BelongsToCurrentThread()); | |
142 | |
143 core_->ui_task_runner()->PostTask( | |
144 FROM_HERE, | |
145 base::Bind(&Core::HasPermission, core_->GetWeakPtr(), type, | |
146 security_origin, RegisterCallback(permission_status_cb))); | |
147 } | |
148 | |
149 void MediaPermissionDispatcherProxy::RequestPermission( | |
150 Type type, | |
151 const GURL& security_origin, | |
152 const PermissionStatusCB& permission_status_cb) { | |
153 DCHECK(core_->caller_task_runner()->BelongsToCurrentThread()); | |
154 | |
155 core_->ui_task_runner()->PostTask( | |
156 FROM_HERE, | |
157 base::Bind(&Core::RequestPermission, core_->GetWeakPtr(), type, | |
158 security_origin, RegisterCallback(permission_status_cb))); | |
159 } | |
160 | |
161 } // namespace content | |
OLD | NEW |