PrincipalVerifier.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #ifndef mozilla_dom_cache_PrincipalVerifier_h
  6. #define mozilla_dom_cache_PrincipalVerifier_h
  7. #include "mozilla/ipc/PBackgroundSharedTypes.h"
  8. #include "nsThreadUtils.h"
  9. #include "nsTObserverArray.h"
  10. namespace mozilla {
  11. namespace ipc {
  12. class PBackgroundParent;
  13. } // namespace ipc
  14. namespace dom {
  15. namespace cache {
  16. class ManagerId;
  17. class PrincipalVerifier final : public Runnable
  18. {
  19. public:
  20. // An interface to be implemented by code wishing to use the
  21. // PrincipalVerifier. Note, the Listener implementation is responsible
  22. // for calling RemoveListener() on the PrincipalVerifier to clear the
  23. // weak reference.
  24. class Listener
  25. {
  26. public:
  27. virtual void OnPrincipalVerified(nsresult aRv, ManagerId* aManagerId) = 0;
  28. };
  29. static already_AddRefed<PrincipalVerifier>
  30. CreateAndDispatch(Listener* aListener, mozilla::ipc::PBackgroundParent* aActor,
  31. const mozilla::ipc::PrincipalInfo& aPrincipalInfo);
  32. void AddListener(Listener* aListener);
  33. // The Listener must call RemoveListener() when OnPrincipalVerified() is
  34. // called or when the Listener is destroyed.
  35. void RemoveListener(Listener* aListener);
  36. private:
  37. PrincipalVerifier(Listener* aListener, mozilla::ipc::PBackgroundParent* aActor,
  38. const mozilla::ipc::PrincipalInfo& aPrincipalInfo);
  39. virtual ~PrincipalVerifier();
  40. void VerifyOnMainThread();
  41. void CompleteOnInitiatingThread();
  42. void DispatchToInitiatingThread(nsresult aRv);
  43. // Weak reference cleared by RemoveListener()
  44. typedef nsTObserverArray<Listener*> ListenerList;
  45. ListenerList mListenerList;
  46. // set in originating thread at construction, but must be accessed and
  47. // released on main thread
  48. RefPtr<ContentParent> mActor;
  49. const mozilla::ipc::PrincipalInfo mPrincipalInfo;
  50. nsCOMPtr<nsIThread> mInitiatingThread;
  51. nsresult mResult;
  52. RefPtr<ManagerId> mManagerId;
  53. public:
  54. NS_DECL_NSIRUNNABLE
  55. };
  56. } // namespace cache
  57. } // namespace dom
  58. } // namespace mozilla
  59. #endif // mozilla_dom_cache_PrincipalVerifier_h