tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

WorkerLoadContext.cpp (3118B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "WorkerLoadContext.h"
      8 
      9 #include "CacheLoadHandler.h"  // CacheCreator
     10 #include "js/loader/ScriptLoadRequest.h"
     11 #include "mozilla/dom/workerinternals/ScriptLoader.h"
     12 
     13 namespace mozilla {
     14 namespace dom {
     15 
     16 WorkerLoadContext::WorkerLoadContext(
     17    Kind aKind, const Maybe<ClientInfo>& aClientInfo,
     18    workerinternals::loader::WorkerScriptLoader* aScriptLoader,
     19    bool aOnlyExistingCachedResourcesAllowed)
     20    : JS::loader::LoadContextBase(JS::loader::ContextKind::Worker),
     21      mKind(aKind),
     22      mClientInfo(aClientInfo),
     23      mScriptLoader(aScriptLoader),
     24      mOnlyExistingCachedResourcesAllowed(aOnlyExistingCachedResourcesAllowed) {
     25      };
     26 
     27 bool WorkerLoadContext::IsTopLevel() {
     28  return mRequest->IsTopLevel() && (mKind == Kind::MainScript);
     29 };
     30 
     31 ThreadSafeRequestHandle::ThreadSafeRequestHandle(
     32    JS::loader::ScriptLoadRequest* aRequest, nsISerialEventTarget* aSyncTarget)
     33    : mRequest(aRequest), mOwningEventTarget(aSyncTarget) {}
     34 
     35 WorkerLoadContext* ThreadSafeRequestHandle::GetContext() {
     36  return mRequest->GetWorkerLoadContext();
     37 }
     38 
     39 already_AddRefed<JS::loader::ScriptLoadRequest>
     40 ThreadSafeRequestHandle::ReleaseRequest() {
     41  RefPtr<JS::loader::ScriptLoadRequest> request;
     42  mRequest.swap(request);
     43  mRunnable = nullptr;
     44  return request.forget();
     45 }
     46 
     47 nsresult ThreadSafeRequestHandle::OnStreamComplete(nsresult aStatus) {
     48  return mRunnable->OnStreamComplete(this, aStatus);
     49 }
     50 
     51 void ThreadSafeRequestHandle::LoadingFinished(nsresult aRv) {
     52  mRunnable->LoadingFinished(this, aRv);
     53 }
     54 
     55 void ThreadSafeRequestHandle::MaybeExecuteFinishedScripts() {
     56  mRunnable->MaybeExecuteFinishedScripts(this);
     57 }
     58 
     59 bool ThreadSafeRequestHandle::IsCancelled() { return mRunnable->IsCancelled(); }
     60 
     61 nsresult ThreadSafeRequestHandle::GetCancelResult() {
     62  return mRunnable->GetCancelResult();
     63 }
     64 
     65 workerinternals::loader::CacheCreator*
     66 ThreadSafeRequestHandle::GetCacheCreator() {
     67  AssertIsOnMainThread();
     68  return mRunnable->GetCacheCreator();
     69 }
     70 
     71 ThreadSafeRequestHandle::~ThreadSafeRequestHandle() {
     72  // Normally we only touch mStrongRef on the owning thread.  This is safe,
     73  // however, because when we do use mStrongRef on the owning thread we are
     74  // always holding a strong ref to the ThreadsafeHandle via the owning
     75  // runnable.  So we cannot run the ThreadsafeHandle destructor simultaneously.
     76  if (!mRequest || mOwningEventTarget->IsOnCurrentThread()) {
     77    return;
     78  }
     79 
     80  // Dispatch in NS_ProxyRelease is guaranteed to succeed here because we block
     81  // shutdown until all Contexts have been destroyed. Therefore it is ok to have
     82  // MOZ_ALWAYS_SUCCEED here.
     83  MOZ_ALWAYS_SUCCEEDS(NS_ProxyRelease("ThreadSafeRequestHandle::mRequest",
     84                                      mOwningEventTarget, mRequest.forget()));
     85 }
     86 
     87 }  // namespace dom
     88 }  // namespace mozilla