ManagerId.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 "mozilla/dom/cache/ManagerId.h"
  6. #include "mozilla/dom/quota/QuotaManager.h"
  7. #include "nsIPrincipal.h"
  8. #include "nsProxyRelease.h"
  9. #include "mozilla/RefPtr.h"
  10. #include "nsThreadUtils.h"
  11. namespace mozilla {
  12. namespace dom {
  13. namespace cache {
  14. using mozilla::dom::quota::QuotaManager;
  15. // static
  16. nsresult
  17. ManagerId::Create(nsIPrincipal* aPrincipal, ManagerId** aManagerIdOut)
  18. {
  19. MOZ_ASSERT(NS_IsMainThread());
  20. // The QuotaManager::GetInfoFromPrincipal() has special logic for system
  21. // and about: principals. We need to use the same modified origin in
  22. // order to interpret calls from QM correctly.
  23. nsCString quotaOrigin;
  24. nsresult rv = QuotaManager::GetInfoFromPrincipal(aPrincipal,
  25. nullptr, // suffix
  26. nullptr, // group
  27. &quotaOrigin,
  28. nullptr); // is app
  29. if (NS_WARN_IF(NS_FAILED(rv))) { return rv; }
  30. RefPtr<ManagerId> ref = new ManagerId(aPrincipal, quotaOrigin);
  31. ref.forget(aManagerIdOut);
  32. return NS_OK;
  33. }
  34. already_AddRefed<nsIPrincipal>
  35. ManagerId::Principal() const
  36. {
  37. MOZ_ASSERT(NS_IsMainThread());
  38. nsCOMPtr<nsIPrincipal> ref = mPrincipal;
  39. return ref.forget();
  40. }
  41. ManagerId::ManagerId(nsIPrincipal* aPrincipal, const nsACString& aQuotaOrigin)
  42. : mPrincipal(aPrincipal)
  43. , mQuotaOrigin(aQuotaOrigin)
  44. {
  45. MOZ_DIAGNOSTIC_ASSERT(mPrincipal);
  46. }
  47. ManagerId::~ManagerId()
  48. {
  49. // If we're already on the main thread, then default destruction is fine
  50. if (NS_IsMainThread()) {
  51. return;
  52. }
  53. // Otherwise we need to proxy to main thread to do the release
  54. // The PBackground worker thread shouldn't be running after the main thread
  55. // is stopped. So main thread is guaranteed to exist here.
  56. NS_ReleaseOnMainThread(mPrincipal.forget());
  57. }
  58. } // namespace cache
  59. } // namespace dom
  60. } // namespace mozilla