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