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::revokePermission(
72 blink::WebPermissionType type
,
73 const blink::WebURL
& origin
,
74 blink::WebPermissionCallback
* callback
) {
75 main_thread_task_runner_
->PostTask(
77 base::Bind(&PermissionDispatcher::RevokePermissionForWorker
,
78 base::Unretained(permission_dispatcher_
),
80 origin
.string().utf8(),
81 base::Unretained(callback
),
82 WorkerTaskRunner::Instance()->CurrentWorkerId()));
85 void PermissionDispatcherThreadProxy::startListening(
86 blink::WebPermissionType type
,
87 const blink::WebURL
& origin
,
88 blink::WebPermissionObserver
* observer
) {
89 if (!PermissionDispatcher::IsObservable(type
))
92 RegisterObserver(observer
);
94 main_thread_task_runner_
->PostTask(FROM_HERE
,
95 base::Bind(&PermissionDispatcher::StartListeningForWorker
,
96 base::Unretained(permission_dispatcher_
),
98 origin
.string().utf8(),
99 WorkerTaskRunner::Instance()->CurrentWorkerId(),
100 base::Bind(&PermissionDispatcherThreadProxy::OnPermissionChanged
,
101 base::Unretained(this),
103 origin
.string().utf8(),
104 base::Unretained(observer
))));
107 void PermissionDispatcherThreadProxy::stopListening(
108 blink::WebPermissionObserver
* observer
) {
109 UnregisterObserver(observer
);
112 void PermissionDispatcherThreadProxy::OnPermissionChanged(
113 blink::WebPermissionType type
,
114 const std::string
& origin
,
115 blink::WebPermissionObserver
* observer
,
116 blink::WebPermissionStatus status
) {
117 if (!IsObserverRegistered(observer
))
120 observer
->permissionChanged(type
, status
);
122 main_thread_task_runner_
->PostTask(FROM_HERE
,
123 base::Bind(&PermissionDispatcher::GetNextPermissionChangeForWorker
,
124 base::Unretained(permission_dispatcher_
),
128 WorkerTaskRunner::Instance()->CurrentWorkerId(),
129 base::Bind(&PermissionDispatcherThreadProxy::OnPermissionChanged
,
130 base::Unretained(this),
133 base::Unretained(observer
))));
136 void PermissionDispatcherThreadProxy::OnWorkerRunLoopStopped() {
140 } // namespace content