PrincipalVerifier.h (2366B)
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 #ifndef mozilla_dom_cache_PrincipalVerifier_h 8 #define mozilla_dom_cache_PrincipalVerifier_h 9 10 #include "mozilla/dom/SafeRefPtr.h" 11 #include "mozilla/ipc/PBackgroundSharedTypes.h" 12 #include "nsTObserverArray.h" 13 #include "nsThreadUtils.h" 14 15 namespace mozilla { 16 17 namespace ipc { 18 class PBackgroundParent; 19 } // namespace ipc 20 21 namespace dom { 22 23 class ThreadsafeContentParentHandle; 24 25 namespace cache { 26 27 class ManagerId; 28 29 class PrincipalVerifier final : public Runnable { 30 public: 31 // An interface to be implemented by code wishing to use the 32 // PrincipalVerifier. Note, the Listener implementation is responsible 33 // for calling RemoveListener() on the PrincipalVerifier to clear the 34 // weak reference. 35 class Listener { 36 public: 37 virtual void OnPrincipalVerified( 38 nsresult aRv, const SafeRefPtr<ManagerId>& aManagerId) = 0; 39 }; 40 41 static already_AddRefed<PrincipalVerifier> CreateAndDispatch( 42 Listener& aListener, mozilla::ipc::PBackgroundParent* aActor, 43 const mozilla::ipc::PrincipalInfo& aPrincipalInfo); 44 45 void AddListener(Listener& aListener); 46 47 // The Listener must call RemoveListener() when OnPrincipalVerified() is 48 // called or when the Listener is destroyed. 49 void RemoveListener(Listener& aListener); 50 51 private: 52 PrincipalVerifier(Listener& aListener, 53 mozilla::ipc::PBackgroundParent* aActor, 54 const mozilla::ipc::PrincipalInfo& aPrincipalInfo); 55 virtual ~PrincipalVerifier(); 56 57 void VerifyOnMainThread(); 58 void CompleteOnInitiatingThread(); 59 60 void DispatchToInitiatingThread(nsresult aRv); 61 62 // Weak reference cleared by RemoveListener() 63 nsTObserverArray<NotNull<Listener*>> mListenerList; 64 65 RefPtr<ThreadsafeContentParentHandle> mHandle; 66 67 const mozilla::ipc::PrincipalInfo mPrincipalInfo; 68 nsCOMPtr<nsIEventTarget> mInitiatingEventTarget; 69 nsresult mResult; 70 SafeRefPtr<ManagerId> mManagerId; 71 72 public: 73 NS_DECL_NSIRUNNABLE 74 }; 75 76 } // namespace cache 77 } // namespace dom 78 } // namespace mozilla 79 80 #endif // mozilla_dom_cache_PrincipalVerifier_h