Principal.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "Principal.h"
  6. #include "jsapi.h"
  7. #include "mozilla/Assertions.h"
  8. BEGIN_WORKERS_NAMESPACE
  9. struct WorkerPrincipal final : public JSPrincipals
  10. {
  11. bool write(JSContext* aCx, JSStructuredCloneWriter* aWriter) override {
  12. MOZ_CRASH("WorkerPrincipal::write not implemented");
  13. return false;
  14. }
  15. };
  16. JSPrincipals*
  17. GetWorkerPrincipal()
  18. {
  19. static WorkerPrincipal sPrincipal;
  20. /*
  21. * To make sure the the principals refcount is initialized to one, atomically
  22. * increment it on every pass though this function. If we discover this wasn't
  23. * the first time, decrement it again. This avoids the need for
  24. * synchronization.
  25. */
  26. int32_t prevRefcount = sPrincipal.refcount++;
  27. if (prevRefcount > 0) {
  28. --sPrincipal.refcount;
  29. } else {
  30. #ifdef DEBUG
  31. sPrincipal.debugToken = kJSPrincipalsDebugToken;
  32. #endif
  33. }
  34. return &sPrincipal;
  35. }
  36. void
  37. DestroyWorkerPrincipals(JSPrincipals* aPrincipals)
  38. {
  39. MOZ_ASSERT_UNREACHABLE("Worker principals refcount should never fall below one");
  40. }
  41. END_WORKERS_NAMESPACE