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.
5 #include "content/child/permissions/permission_dispatcher_thread_proxy.h"
8 #include "base/bind_helpers.h"
9 #include "base/lazy_instance.h"
10 #include "base/location.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/threading/thread_local.h"
13 #include "content/child/permissions/permission_dispatcher.h"
14 #include "content/public/child/worker_thread.h"
15 #include "third_party/WebKit/public/platform/WebURL.h"
16 #include "third_party/WebKit/public/platform/modules/permissions/WebPermissionObserver.h"
18 using base::LazyInstance
;
19 using base::ThreadLocalPointer
;
25 LazyInstance
<ThreadLocalPointer
<PermissionDispatcherThreadProxy
>>::Leaky
26 g_permission_dispatcher_tls
= LAZY_INSTANCE_INITIALIZER
;
28 } // anonymous namespace
30 PermissionDispatcherThreadProxy
*
31 PermissionDispatcherThreadProxy::GetThreadInstance(
32 base::SingleThreadTaskRunner
* main_thread_task_runner
,
33 PermissionDispatcher
* permission_dispatcher
) {
34 if (g_permission_dispatcher_tls
.Pointer()->Get())
35 return g_permission_dispatcher_tls
.Pointer()->Get();
37 PermissionDispatcherThreadProxy
* instance
=
38 new PermissionDispatcherThreadProxy(main_thread_task_runner
,
39 permission_dispatcher
);
40 DCHECK(WorkerThread::GetCurrentId());
41 WorkerThread::AddObserver(instance
);
45 PermissionDispatcherThreadProxy::PermissionDispatcherThreadProxy(
46 base::SingleThreadTaskRunner
* main_thread_task_runner
,
47 PermissionDispatcher
* permission_dispatcher
)
48 : main_thread_task_runner_(main_thread_task_runner
),
49 permission_dispatcher_(permission_dispatcher
) {
50 g_permission_dispatcher_tls
.Pointer()->Set(this);
53 PermissionDispatcherThreadProxy::~PermissionDispatcherThreadProxy() {
54 g_permission_dispatcher_tls
.Pointer()->Set(nullptr);
57 void PermissionDispatcherThreadProxy::queryPermission(
58 blink::WebPermissionType type
,
59 const blink::WebURL
& origin
,
60 blink::WebPermissionCallback
* callback
) {
61 main_thread_task_runner_
->PostTask(
62 FROM_HERE
, base::Bind(&PermissionDispatcher::QueryPermissionForWorker
,
63 base::Unretained(permission_dispatcher_
), type
,
64 origin
.string().utf8(), base::Unretained(callback
),
65 WorkerThread::GetCurrentId()));
68 void PermissionDispatcherThreadProxy::requestPermission(
69 blink::WebPermissionType type
,
70 const blink::WebURL
& origin
,
71 blink::WebPermissionCallback
* callback
) {
72 main_thread_task_runner_
->PostTask(
73 FROM_HERE
, base::Bind(&PermissionDispatcher::RequestPermissionForWorker
,
74 base::Unretained(permission_dispatcher_
), type
,
75 origin
.string().utf8(), base::Unretained(callback
),
76 WorkerThread::GetCurrentId()));
79 void PermissionDispatcherThreadProxy::requestPermissions(
80 const blink::WebVector
<blink::WebPermissionType
>& types
,
81 const blink::WebURL
& origin
,
82 blink::WebPermissionsCallback
* callback
) {
83 main_thread_task_runner_
->PostTask(
84 FROM_HERE
, base::Bind(&PermissionDispatcher::RequestPermissionsForWorker
,
85 base::Unretained(permission_dispatcher_
), types
,
86 origin
.string().utf8(), base::Unretained(callback
),
87 WorkerThread::GetCurrentId()));
90 void PermissionDispatcherThreadProxy::revokePermission(
91 blink::WebPermissionType type
,
92 const blink::WebURL
& origin
,
93 blink::WebPermissionCallback
* callback
) {
94 main_thread_task_runner_
->PostTask(
95 FROM_HERE
, base::Bind(&PermissionDispatcher::RevokePermissionForWorker
,
96 base::Unretained(permission_dispatcher_
), type
,
97 origin
.string().utf8(), base::Unretained(callback
),
98 WorkerThread::GetCurrentId()));
101 void PermissionDispatcherThreadProxy::startListening(
102 blink::WebPermissionType type
,
103 const blink::WebURL
& origin
,
104 blink::WebPermissionObserver
* observer
) {
105 if (!PermissionDispatcher::IsObservable(type
))
108 RegisterObserver(observer
);
110 main_thread_task_runner_
->PostTask(
113 &PermissionDispatcher::StartListeningForWorker
,
114 base::Unretained(permission_dispatcher_
), type
,
115 origin
.string().utf8(), WorkerThread::GetCurrentId(),
116 base::Bind(&PermissionDispatcherThreadProxy::OnPermissionChanged
,
117 base::Unretained(this), type
, origin
.string().utf8(),
118 base::Unretained(observer
))));
121 void PermissionDispatcherThreadProxy::stopListening(
122 blink::WebPermissionObserver
* observer
) {
123 UnregisterObserver(observer
);
126 void PermissionDispatcherThreadProxy::OnPermissionChanged(
127 blink::WebPermissionType type
,
128 const std::string
& origin
,
129 blink::WebPermissionObserver
* observer
,
130 blink::WebPermissionStatus status
) {
131 if (!IsObserverRegistered(observer
))
134 observer
->permissionChanged(type
, status
);
136 main_thread_task_runner_
->PostTask(
139 &PermissionDispatcher::GetNextPermissionChangeForWorker
,
140 base::Unretained(permission_dispatcher_
), type
, origin
, status
,
141 WorkerThread::GetCurrentId(),
142 base::Bind(&PermissionDispatcherThreadProxy::OnPermissionChanged
,
143 base::Unretained(this), type
, origin
,
144 base::Unretained(observer
))));
147 void PermissionDispatcherThreadProxy::WillStopCurrentWorkerThread() {
151 } // namespace content