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/child/worker_task_runner.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(WorkerTaskRunner::Instance()->CurrentWorkerId());
41 WorkerTaskRunner::Instance()->AddStopObserver(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(
63 base::Bind(&PermissionDispatcher::QueryPermissionForWorker
,
64 base::Unretained(permission_dispatcher_
),
66 origin
.string().utf8(),
67 base::Unretained(callback
),
68 WorkerTaskRunner::Instance()->CurrentWorkerId()));
71 void PermissionDispatcherThreadProxy::requestPermission(
72 blink::WebPermissionType type
,
73 const blink::WebURL
& origin
,
74 blink::WebPermissionCallback
* callback
) {
75 main_thread_task_runner_
->PostTask(
77 base::Bind(&PermissionDispatcher::RequestPermissionForWorker
,
78 base::Unretained(permission_dispatcher_
),
80 origin
.string().utf8(),
81 base::Unretained(callback
),
82 WorkerTaskRunner::Instance()->CurrentWorkerId()));
85 void PermissionDispatcherThreadProxy::revokePermission(
86 blink::WebPermissionType type
,
87 const blink::WebURL
& origin
,
88 blink::WebPermissionCallback
* callback
) {
89 main_thread_task_runner_
->PostTask(
91 base::Bind(&PermissionDispatcher::RevokePermissionForWorker
,
92 base::Unretained(permission_dispatcher_
),
94 origin
.string().utf8(),
95 base::Unretained(callback
),
96 WorkerTaskRunner::Instance()->CurrentWorkerId()));
99 void PermissionDispatcherThreadProxy::startListening(
100 blink::WebPermissionType type
,
101 const blink::WebURL
& origin
,
102 blink::WebPermissionObserver
* observer
) {
103 if (!PermissionDispatcher::IsObservable(type
))
106 RegisterObserver(observer
);
108 main_thread_task_runner_
->PostTask(FROM_HERE
,
109 base::Bind(&PermissionDispatcher::StartListeningForWorker
,
110 base::Unretained(permission_dispatcher_
),
112 origin
.string().utf8(),
113 WorkerTaskRunner::Instance()->CurrentWorkerId(),
114 base::Bind(&PermissionDispatcherThreadProxy::OnPermissionChanged
,
115 base::Unretained(this),
117 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(FROM_HERE
,
137 base::Bind(&PermissionDispatcher::GetNextPermissionChangeForWorker
,
138 base::Unretained(permission_dispatcher_
),
142 WorkerTaskRunner::Instance()->CurrentWorkerId(),
143 base::Bind(&PermissionDispatcherThreadProxy::OnPermissionChanged
,
144 base::Unretained(this),
147 base::Unretained(observer
))));
150 void PermissionDispatcherThreadProxy::OnWorkerRunLoopStopped() {
154 } // namespace content