worker_thread_util.cc 1.2 KB

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