1 // Copyright 2019 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 "extensions/renderer/worker_thread_util.h"
6 
7 #include "base/lazy_instance.h"
8 #include "base/threading/thread_local.h"
9 #include "content/public/renderer/worker_thread.h"
10 #include "extensions/common/constants.h"
11 #include "third_party/blink/public/web/modules/service_worker/web_service_worker_context_proxy.h"
12 
13 namespace extensions {
14 namespace worker_thread_util {
15 
16 namespace {
17 base::LazyInstance<
18     base::ThreadLocalPointer<blink::WebServiceWorkerContextProxy>>::Leaky
19     g_worker_context_proxy_tls = LAZY_INSTANCE_INITIALIZER;
20 }
21 
IsWorkerThread()22 bool IsWorkerThread() {
23   return content::WorkerThread::GetCurrentId() != kMainThreadId;
24 }
25 
SetWorkerContextProxy(blink::WebServiceWorkerContextProxy * context_proxy)26 void SetWorkerContextProxy(blink::WebServiceWorkerContextProxy* context_proxy) {
27   g_worker_context_proxy_tls.Pointer()->Set(context_proxy);
28 }
29 
HasWorkerContextProxyInteraction()30 bool HasWorkerContextProxyInteraction() {
31   DCHECK(IsWorkerThread());
32   blink::WebServiceWorkerContextProxy* proxy =
33       g_worker_context_proxy_tls.Pointer()->Get();
34   return proxy && proxy->IsWindowInteractionAllowed();
35 }
36 
37 }  // namespace worker_thread_util
38 }  // namespace extensions
39