InProcessBrowserChildMessageManager.h (4171B)
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 nsInProcessBrowserChildGlobal_h 8 #define nsInProcessBrowserChildGlobal_h 9 10 #include "mozilla/DOMEventTargetHelper.h" 11 #include "mozilla/RefPtr.h" 12 #include "mozilla/dom/ContentFrameMessageManager.h" 13 #include "mozilla/dom/MessageManagerCallback.h" 14 #include "nsCOMArray.h" 15 #include "nsCOMPtr.h" 16 #include "nsDocShell.h" 17 #include "nsIScriptContext.h" 18 #include "nsIScriptObjectPrincipal.h" 19 #include "nsWeakReference.h" 20 21 class nsFrameMessageManager; 22 23 namespace mozilla { 24 class EventChainPreVisitor; 25 26 namespace dom { 27 28 /** 29 * This class implements a ContentFrameMessageManager for use by frame loaders 30 * in the parent process. It is bound to a DocShell rather than a BrowserChild, 31 * and does not use any IPC infrastructure for its message passing. 32 */ 33 34 class InProcessBrowserChildMessageManager final 35 : public ContentFrameMessageManager, 36 public nsMessageManagerScriptExecutor, 37 public nsIInProcessContentFrameMessageManager, 38 public nsSupportsWeakReference, 39 public mozilla::dom::ipc::MessageManagerCallback { 40 using StructuredCloneData = mozilla::dom::ipc::StructuredCloneData; 41 42 private: 43 InProcessBrowserChildMessageManager(nsDocShell* aShell, nsIContent* aOwner, 44 nsFrameMessageManager* aChrome); 45 46 public: 47 static already_AddRefed<InProcessBrowserChildMessageManager> Create( 48 nsDocShell* aShell, nsIContent* aOwner, nsFrameMessageManager* aChrome); 49 50 NS_DECL_ISUPPORTS_INHERITED 51 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED( 52 InProcessBrowserChildMessageManager, DOMEventTargetHelper) 53 54 void MarkForCC(); 55 56 virtual JSObject* WrapObject(JSContext* aCx, 57 JS::Handle<JSObject*> aGivenProto) override; 58 59 Nullable<WindowProxyHolder> GetContent(ErrorResult& aError) override; 60 virtual already_AddRefed<nsIDocShell> GetDocShell( 61 ErrorResult& aError) override { 62 return do_AddRef(mDocShell); 63 } 64 virtual already_AddRefed<nsIEventTarget> GetTabEventTarget() override; 65 66 NS_FORWARD_SAFE_NSIMESSAGESENDER(mMessageManager) 67 68 NS_DECL_NSIINPROCESSCONTENTFRAMEMESSAGEMANAGER 69 70 void CacheFrameLoader(nsFrameLoader* aFrameLoader); 71 72 /** 73 * MessageManagerCallback methods that we override. 74 */ 75 virtual bool DoSendBlockingMessage( 76 const nsAString& aMessage, StructuredCloneData& aData, 77 nsTArray<UniquePtr<StructuredCloneData>>* aRetVal) override; 78 virtual nsresult DoSendAsyncMessage(const nsAString& aMessage, 79 StructuredCloneData& aData) override; 80 81 void GetEventTargetParent(EventChainPreVisitor& aVisitor) override; 82 83 void LoadFrameScript(const nsAString& aURL, bool aRunInGlobalScope); 84 void FireUnloadEvent(); 85 void DisconnectEventListeners(); 86 void Disconnect(); 87 void SendMessageToParent(const nsString& aMessage, bool aSync, 88 const nsString& aJSON, 89 nsTArray<nsString>* aJSONRetVal); 90 nsFrameMessageManager* GetInnerManager() { 91 return static_cast<nsFrameMessageManager*>(mMessageManager.get()); 92 } 93 94 void SetOwner(nsIContent* aOwner) { mOwner = aOwner; } 95 nsFrameMessageManager* GetChromeMessageManager() { 96 return mChromeMessageManager; 97 } 98 void SetChromeMessageManager(nsFrameMessageManager* aParent) { 99 mChromeMessageManager = aParent; 100 } 101 102 already_AddRefed<nsFrameLoader> GetFrameLoader(); 103 104 protected: 105 virtual ~InProcessBrowserChildMessageManager(); 106 107 RefPtr<nsDocShell> mDocShell; 108 bool mLoadingScript; 109 110 bool mPreventEventsEscaping; 111 112 // We keep a strong reference to the frameloader after we've started 113 // teardown. This allows us to dispatch message manager messages during this 114 // time. 115 RefPtr<nsFrameLoader> mFrameLoader; 116 117 public: 118 nsIContent* mOwner; 119 nsFrameMessageManager* mChromeMessageManager; 120 }; 121 122 } // namespace dom 123 } // namespace mozilla 124 125 #endif