tor-browser

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

SandboxTestingThread.h (1586B)


      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 https://mozilla.org/MPL/2.0/. */
      6 
      7 #ifndef mozilla_SandboxTestingThread_h
      8 #define mozilla_SandboxTestingThread_h
      9 
     10 #include "nsThreadManager.h"
     11 
     12 #if !defined(MOZ_SANDBOX) || !defined(MOZ_DEBUG) || !defined(ENABLE_TESTS)
     13 #  error "This file should not be used outside of debug with tests"
     14 #endif
     15 
     16 namespace mozilla {
     17 
     18 class SandboxTestingThread {
     19 public:
     20  void Dispatch(already_AddRefed<nsIRunnable>&& aRunnable) {
     21    mThread->Dispatch(std::move(aRunnable), nsIEventTarget::NS_DISPATCH_NORMAL);
     22  }
     23 
     24  bool IsOnThread() {
     25    bool on;
     26    return NS_SUCCEEDED(mThread->IsOnCurrentThread(&on)) && on;
     27  }
     28 
     29  static SandboxTestingThread* Create() {
     30    MOZ_RELEASE_ASSERT(NS_IsMainThread());
     31    nsCOMPtr<nsIThread> thread;
     32    if (NS_FAILED(
     33            NS_NewNamedThread("Sandbox Testing", getter_AddRefs(thread)))) {
     34      return nullptr;
     35    }
     36    return new SandboxTestingThread(thread);
     37  }
     38 
     39  ~SandboxTestingThread() {
     40    NS_DispatchToMainThread(NewRunnableMethod("~SandboxTestingThread", mThread,
     41                                              &nsIThread::Shutdown));
     42  }
     43 
     44 private:
     45  explicit SandboxTestingThread(nsIThread* aThread) : mThread(aThread) {
     46    MOZ_ASSERT(mThread);
     47  }
     48 
     49  nsCOMPtr<nsIThread> mThread;
     50 };
     51 }  // namespace mozilla
     52 
     53 #endif  // mozilla_SandboxTestingThread_h