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_BrowserChild_h
8 #define mozilla_dom_BrowserChild_h
9 
10 #include "mozilla/dom/ContentFrameMessageManager.h"
11 #include "mozilla/dom/PBrowserChild.h"
12 #include "nsIWebNavigation.h"
13 #include "nsCOMPtr.h"
14 #include "nsIWebBrowserChrome.h"
15 #include "nsIEmbeddingSiteWindow.h"
16 #include "nsIWebBrowserChromeFocus.h"
17 #include "nsIDOMEventListener.h"
18 #include "nsIInterfaceRequestor.h"
19 #include "nsIWindowProvider.h"
20 #include "nsIDocShell.h"
21 #include "nsIInterfaceRequestorUtils.h"
22 #include "nsWeakReference.h"
23 #include "nsIBrowserChild.h"
24 #include "nsITooltipListener.h"
25 #include "nsIWebProgressListener.h"
26 #include "nsIWebProgressListener2.h"
27 #include "mozilla/Attributes.h"
28 #include "mozilla/dom/TabContext.h"
29 #include "mozilla/dom/CoalescedMouseData.h"
30 #include "mozilla/dom/CoalescedTouchData.h"
31 #include "mozilla/dom/CoalescedWheelData.h"
32 #include "mozilla/dom/MessageManagerCallback.h"
33 #include "mozilla/dom/VsyncChild.h"
34 #include "mozilla/DOMEventTargetHelper.h"
35 #include "mozilla/EventDispatcher.h"
36 #include "mozilla/EventForwards.h"
37 #include "mozilla/layers/CompositorTypes.h"
38 #include "mozilla/layers/APZCCallbackHelper.h"
39 #include "mozilla/layers/CompositorOptions.h"
40 #include "mozilla/layers/GeckoContentControllerTypes.h"
41 #include "nsIWebBrowserChrome.h"
42 #include "mozilla/dom/ipc/IdType.h"
43 #include "AudioChannelService.h"
44 #include "PuppetWidget.h"
45 #include "nsDeque.h"
46 #include "nsIRemoteTab.h"
47 
48 class nsBrowserStatusFilter;
49 class nsIDOMWindow;
50 class nsIHttpChannel;
51 class nsIRequest;
52 class nsISerialEventTarget;
53 class nsIWebProgress;
54 class nsWebBrowser;
55 class nsDocShellLoadState;
56 
57 template <typename T>
58 class nsTHashtable;
59 template <typename T>
60 class nsPtrHashKey;
61 
62 namespace mozilla {
63 class AbstractThread;
64 class PresShell;
65 
66 namespace layers {
67 class APZChild;
68 class APZEventState;
69 class AsyncDragMetrics;
70 class IAPZCTreeManager;
71 class ImageCompositeNotification;
72 class PCompositorBridgeChild;
73 }  // namespace layers
74 
75 namespace widget {
76 struct AutoCacheNativeKeyCommands;
77 }  // namespace widget
78 
79 namespace dom {
80 
81 class BrowserChild;
82 class BrowsingContext;
83 class TabGroup;
84 class ClonedMessageData;
85 class CoalescedMouseData;
86 class CoalescedWheelData;
87 class ContentSessionStore;
88 class SessionStoreChangeListener;
89 class TabListener;
90 class RequestData;
91 class WebProgressData;
92 
93 class BrowserChildMessageManager : public ContentFrameMessageManager,
94                                    public nsIMessageSender,
95                                    public DispatcherTrait,
96                                    public nsSupportsWeakReference {
97  public:
98   explicit BrowserChildMessageManager(BrowserChild* aBrowserChild);
99 
100   NS_DECL_ISUPPORTS_INHERITED
101   NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(BrowserChildMessageManager,
102                                            DOMEventTargetHelper)
103 
104   void MarkForCC();
105 
106   JSObject* WrapObject(JSContext* aCx,
107                        JS::Handle<JSObject*> aGivenProto) override;
108 
109   virtual Nullable<WindowProxyHolder> GetContent(ErrorResult& aError) override;
110   virtual already_AddRefed<nsIDocShell> GetDocShell(
111       ErrorResult& aError) override;
112   virtual already_AddRefed<nsIEventTarget> GetTabEventTarget() override;
113 
NS_FORWARD_SAFE_NSIMESSAGESENDER(mMessageManager)114   NS_FORWARD_SAFE_NSIMESSAGESENDER(mMessageManager)
115 
116   void GetEventTargetParent(EventChainPreVisitor& aVisitor) override {
117     aVisitor.mForceContentDispatch = true;
118   }
119 
120   // Dispatch a runnable related to the global.
121   virtual nsresult Dispatch(mozilla::TaskCategory aCategory,
122                             already_AddRefed<nsIRunnable>&& aRunnable) override;
123 
124   virtual nsISerialEventTarget* EventTargetFor(
125       mozilla::TaskCategory aCategory) const override;
126 
127   virtual AbstractThread* AbstractMainThreadFor(
128       mozilla::TaskCategory aCategory) override;
129 
130   RefPtr<BrowserChild> mBrowserChild;
131 
132  protected:
133   ~BrowserChildMessageManager();
134 };
135 
136 class ContentListener final : public nsIDOMEventListener {
137  public:
ContentListener(BrowserChild * aBrowserChild)138   explicit ContentListener(BrowserChild* aBrowserChild)
139       : mBrowserChild(aBrowserChild) {}
140   NS_DECL_ISUPPORTS
141   NS_DECL_NSIDOMEVENTLISTENER
142  protected:
143   ~ContentListener() = default;
144   BrowserChild* mBrowserChild;
145 };
146 
147 /**
148  * BrowserChild implements the child actor part of the PBrowser protocol. See
149  * PBrowser for more information.
150  */
151 class BrowserChild final : public nsMessageManagerScriptExecutor,
152                            public ipc::MessageManagerCallback,
153                            public PBrowserChild,
154                            public nsIWebBrowserChrome,
155                            public nsIEmbeddingSiteWindow,
156                            public nsIWebBrowserChromeFocus,
157                            public nsIInterfaceRequestor,
158                            public nsIWindowProvider,
159                            public nsSupportsWeakReference,
160                            public nsIBrowserChild,
161                            public nsIObserver,
162                            public nsIWebProgressListener2,
163                            public TabContext,
164                            public nsITooltipListener,
165                            public mozilla::ipc::IShmemAllocator {
166   typedef mozilla::widget::PuppetWidget PuppetWidget;
167   typedef mozilla::dom::ClonedMessageData ClonedMessageData;
168   typedef mozilla::dom::CoalescedMouseData CoalescedMouseData;
169   typedef mozilla::dom::CoalescedWheelData CoalescedWheelData;
170   typedef mozilla::layers::APZEventState APZEventState;
171   typedef mozilla::layers::SetAllowedTouchBehaviorCallback
172       SetAllowedTouchBehaviorCallback;
173   typedef mozilla::layers::TouchBehaviorFlags TouchBehaviorFlags;
174 
175   friend class PBrowserChild;
176 
177  public:
178   /**
179    * Find BrowserChild of aTabId in the same content process of the
180    * caller.
181    */
182   static already_AddRefed<BrowserChild> FindBrowserChild(const TabId& aTabId);
183 
184   // Return a list of all active BrowserChildren.
185   static nsTArray<RefPtr<BrowserChild>> GetAll();
186 
187  public:
188   /**
189    * Create a new BrowserChild object.
190    */
191   BrowserChild(ContentChild* aManager, const TabId& aTabId,
192                const TabContext& aContext,
193                dom::BrowsingContext* aBrowsingContext, uint32_t aChromeFlags,
194                bool aIsTopLevel);
195 
196   MOZ_CAN_RUN_SCRIPT nsresult Init(mozIDOMWindowProxy* aParent,
197                                    WindowGlobalChild* aInitialWindowChild);
198 
199   /** Return a BrowserChild with the given attributes. */
200   static already_AddRefed<BrowserChild> Create(
201       ContentChild* aManager, const TabId& aTabId, const TabContext& aContext,
202       BrowsingContext* aBrowsingContext, uint32_t aChromeFlags,
203       bool aIsTopLevel);
204 
205   // Let managees query if it is safe to send messages.
IsDestroyed()206   bool IsDestroyed() const { return mDestroyed; }
207 
GetTabId()208   const TabId GetTabId() const {
209     MOZ_ASSERT(mUniqueId != 0);
210     return mUniqueId;
211   }
212 
213   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
214   NS_DECL_NSIWEBBROWSERCHROME
215   NS_DECL_NSIEMBEDDINGSITEWINDOW
216   NS_DECL_NSIWEBBROWSERCHROMEFOCUS
217   NS_DECL_NSIINTERFACEREQUESTOR
218   NS_DECL_NSIWINDOWPROVIDER
219   NS_DECL_NSIBROWSERCHILD
220   NS_DECL_NSIOBSERVER
221   NS_DECL_NSIWEBPROGRESSLISTENER
222   NS_DECL_NSIWEBPROGRESSLISTENER2
223   NS_DECL_NSITOOLTIPLISTENER
224 
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(BrowserChild,nsIBrowserChild)225   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(BrowserChild,
226                                                          nsIBrowserChild)
227 
228   FORWARD_SHMEM_ALLOCATOR_TO(PBrowserChild)
229 
230   JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
231     return mBrowserChildMessageManager->WrapObject(aCx, aGivenProto);
232   }
233 
234   // Get the Document for the top-level window in this tab.
235   already_AddRefed<Document> GetTopLevelDocument() const;
236 
237   // Get the pres-shell of the document for the top-level window in this tab.
238   PresShell* GetTopLevelPresShell() const;
239 
GetMessageManager()240   BrowserChildMessageManager* GetMessageManager() {
241     return mBrowserChildMessageManager;
242   }
243 
IsTopLevel()244   bool IsTopLevel() const { return mIsTopLevel; }
245 
ShouldSendWebProgressEventsToParent()246   bool ShouldSendWebProgressEventsToParent() const {
247     return mShouldSendWebProgressEventsToParent;
248   }
249 
250   /**
251    * MessageManagerCallback methods that we override.
252    */
253   virtual bool DoSendBlockingMessage(
254       const nsAString& aMessage, StructuredCloneData& aData,
255       nsTArray<StructuredCloneData>* aRetVal) override;
256 
257   virtual nsresult DoSendAsyncMessage(const nsAString& aMessage,
258                                       StructuredCloneData& aData) override;
259 
260   bool DoUpdateZoomConstraints(const uint32_t& aPresShellId,
261                                const ViewID& aViewId,
262                                const Maybe<ZoomConstraints>& aConstraints);
263 
264   mozilla::ipc::IPCResult RecvLoadURL(nsDocShellLoadState* aLoadState,
265                                       const ParentShowInfo& aInfo);
266 
267   mozilla::ipc::IPCResult RecvCreateAboutBlankContentViewer(
268       nsIPrincipal* aPrincipal, nsIPrincipal* aPartitionedPrincipal);
269 
270   mozilla::ipc::IPCResult RecvResumeLoad(const uint64_t& aPendingSwitchID,
271                                          const ParentShowInfo&);
272 
273   MOZ_CAN_RUN_SCRIPT_BOUNDARY mozilla::ipc::IPCResult
274   RecvCloneDocumentTreeIntoSelf(
275       const MaybeDiscarded<BrowsingContext>& aSourceBC,
276       const embedding::PrintData& aPrintData);
277 
278   MOZ_CAN_RUN_SCRIPT_BOUNDARY
279   mozilla::ipc::IPCResult RecvUpdateRemotePrintSettings(
280       const embedding::PrintData& aPrintData);
281 
282   MOZ_CAN_RUN_SCRIPT_BOUNDARY
283   mozilla::ipc::IPCResult RecvShow(const ParentShowInfo&, const OwnerShowInfo&);
284 
285   mozilla::ipc::IPCResult RecvInitRendering(
286       const TextureFactoryIdentifier& aTextureFactoryIdentifier,
287       const layers::LayersId& aLayersId,
288       const mozilla::layers::CompositorOptions& aCompositorOptions,
289       const bool& aLayersConnected);
290 
291   mozilla::ipc::IPCResult RecvCompositorOptionsChanged(
292       const mozilla::layers::CompositorOptions& aNewOptions);
293 
294   mozilla::ipc::IPCResult RecvUpdateDimensions(
295       const mozilla::dom::DimensionInfo& aDimensionInfo);
296   mozilla::ipc::IPCResult RecvSizeModeChanged(const nsSizeMode& aSizeMode);
297 
298   mozilla::ipc::IPCResult RecvChildToParentMatrix(
299       const mozilla::Maybe<mozilla::gfx::Matrix4x4>& aMatrix,
300       const mozilla::ScreenRect& aTopLevelViewportVisibleRectInBrowserCoords);
301 
302   MOZ_CAN_RUN_SCRIPT_BOUNDARY
303   mozilla::ipc::IPCResult RecvDynamicToolbarMaxHeightChanged(
304       const mozilla::ScreenIntCoord& aHeight);
305 
306   mozilla::ipc::IPCResult RecvDynamicToolbarOffsetChanged(
307       const mozilla::ScreenIntCoord& aOffset);
308 
309   mozilla::ipc::IPCResult RecvActivate(uint64_t aActionId);
310 
311   mozilla::ipc::IPCResult RecvDeactivate(uint64_t aActionId);
312 
313   mozilla::ipc::IPCResult RecvRealMouseMoveEvent(
314       const mozilla::WidgetMouseEvent& aEvent, const ScrollableLayerGuid& aGuid,
315       const uint64_t& aInputBlockId);
316   mozilla::ipc::IPCResult RecvNormalPriorityRealMouseMoveEvent(
317       const mozilla::WidgetMouseEvent& aEvent, const ScrollableLayerGuid& aGuid,
318       const uint64_t& aInputBlockId);
319   mozilla::ipc::IPCResult RecvRealMouseMoveEventForTests(
320       const mozilla::WidgetMouseEvent& aEvent, const ScrollableLayerGuid& aGuid,
321       const uint64_t& aInputBlockId);
322   mozilla::ipc::IPCResult RecvNormalPriorityRealMouseMoveEventForTests(
323       const mozilla::WidgetMouseEvent& aEvent, const ScrollableLayerGuid& aGuid,
324       const uint64_t& aInputBlockId);
325 
326   mozilla::ipc::IPCResult RecvSynthMouseMoveEvent(
327       const mozilla::WidgetMouseEvent& aEvent, const ScrollableLayerGuid& aGuid,
328       const uint64_t& aInputBlockId);
329   mozilla::ipc::IPCResult RecvNormalPrioritySynthMouseMoveEvent(
330       const mozilla::WidgetMouseEvent& aEvent, const ScrollableLayerGuid& aGuid,
331       const uint64_t& aInputBlockId);
332 
333   mozilla::ipc::IPCResult RecvRealMouseButtonEvent(
334       const mozilla::WidgetMouseEvent& aEvent, const ScrollableLayerGuid& aGuid,
335       const uint64_t& aInputBlockId);
336   mozilla::ipc::IPCResult RecvNormalPriorityRealMouseButtonEvent(
337       const mozilla::WidgetMouseEvent& aEvent, const ScrollableLayerGuid& aGuid,
338       const uint64_t& aInputBlockId);
339 
340   mozilla::ipc::IPCResult RecvRealMouseEnterExitWidgetEvent(
341       const mozilla::WidgetMouseEvent& aEvent, const ScrollableLayerGuid& aGuid,
342       const uint64_t& aInputBlockId);
343   mozilla::ipc::IPCResult RecvNormalPriorityRealMouseEnterExitWidgetEvent(
344       const mozilla::WidgetMouseEvent& aEvent, const ScrollableLayerGuid& aGuid,
345       const uint64_t& aInputBlockId);
346 
347   MOZ_CAN_RUN_SCRIPT_BOUNDARY
348   mozilla::ipc::IPCResult RecvRealDragEvent(const WidgetDragEvent& aEvent,
349                                             const uint32_t& aDragAction,
350                                             const uint32_t& aDropEffect,
351                                             nsIPrincipal* aPrincipal,
352                                             nsIContentSecurityPolicy* aCsp);
353 
354   mozilla::ipc::IPCResult RecvRealKeyEvent(
355       const mozilla::WidgetKeyboardEvent& aEvent);
356 
357   mozilla::ipc::IPCResult RecvNormalPriorityRealKeyEvent(
358       const mozilla::WidgetKeyboardEvent& aEvent);
359 
360   mozilla::ipc::IPCResult RecvMouseWheelEvent(
361       const mozilla::WidgetWheelEvent& aEvent, const ScrollableLayerGuid& aGuid,
362       const uint64_t& aInputBlockId);
363 
364   mozilla::ipc::IPCResult RecvNormalPriorityMouseWheelEvent(
365       const mozilla::WidgetWheelEvent& aEvent, const ScrollableLayerGuid& aGuid,
366       const uint64_t& aInputBlockId);
367 
368   mozilla::ipc::IPCResult RecvRealTouchEvent(const WidgetTouchEvent& aEvent,
369                                              const ScrollableLayerGuid& aGuid,
370                                              const uint64_t& aInputBlockId,
371                                              const nsEventStatus& aApzResponse);
372 
373   mozilla::ipc::IPCResult RecvNormalPriorityRealTouchEvent(
374       const WidgetTouchEvent& aEvent, const ScrollableLayerGuid& aGuid,
375       const uint64_t& aInputBlockId, const nsEventStatus& aApzResponse);
376 
377   mozilla::ipc::IPCResult RecvRealTouchMoveEvent(
378       const WidgetTouchEvent& aEvent, const ScrollableLayerGuid& aGuid,
379       const uint64_t& aInputBlockId, const nsEventStatus& aApzResponse);
380 
381   mozilla::ipc::IPCResult RecvNormalPriorityRealTouchMoveEvent(
382       const WidgetTouchEvent& aEvent, const ScrollableLayerGuid& aGuid,
383       const uint64_t& aInputBlockId, const nsEventStatus& aApzResponse);
384 
RecvRealTouchMoveEvent2(const WidgetTouchEvent & aEvent,const ScrollableLayerGuid & aGuid,const uint64_t & aInputBlockId,const nsEventStatus & aApzResponse)385   mozilla::ipc::IPCResult RecvRealTouchMoveEvent2(
386       const WidgetTouchEvent& aEvent, const ScrollableLayerGuid& aGuid,
387       const uint64_t& aInputBlockId, const nsEventStatus& aApzResponse) {
388     return RecvRealTouchMoveEvent(aEvent, aGuid, aInputBlockId, aApzResponse);
389   }
390 
RecvNormalPriorityRealTouchMoveEvent2(const WidgetTouchEvent & aEvent,const ScrollableLayerGuid & aGuid,const uint64_t & aInputBlockId,const nsEventStatus & aApzResponse)391   mozilla::ipc::IPCResult RecvNormalPriorityRealTouchMoveEvent2(
392       const WidgetTouchEvent& aEvent, const ScrollableLayerGuid& aGuid,
393       const uint64_t& aInputBlockId, const nsEventStatus& aApzResponse) {
394     return RecvNormalPriorityRealTouchMoveEvent(aEvent, aGuid, aInputBlockId,
395                                                 aApzResponse);
396   }
397 
398   mozilla::ipc::IPCResult RecvUpdateEpoch(const uint32_t& aEpoch);
399 
400   mozilla::ipc::IPCResult RecvUpdateSHistory();
401 
402   mozilla::ipc::IPCResult RecvNativeSynthesisResponse(
403       const uint64_t& aObserverId, const nsCString& aResponse);
404 
405   mozilla::ipc::IPCResult RecvCompositionEvent(
406       const mozilla::WidgetCompositionEvent& aEvent);
407 
408   mozilla::ipc::IPCResult RecvNormalPriorityCompositionEvent(
409       const mozilla::WidgetCompositionEvent& aEvent);
410 
411   mozilla::ipc::IPCResult RecvSelectionEvent(
412       const mozilla::WidgetSelectionEvent& aEvent);
413 
414   mozilla::ipc::IPCResult RecvNormalPrioritySelectionEvent(
415       const mozilla::WidgetSelectionEvent& aEvent);
416 
417   mozilla::ipc::IPCResult RecvSetIsUnderHiddenEmbedderElement(
418       const bool& aIsUnderHiddenEmbedderElement);
419 
420   mozilla::ipc::IPCResult RecvInsertText(const nsString& aStringToInsert);
421 
422   mozilla::ipc::IPCResult RecvNormalPriorityInsertText(
423       const nsString& aStringToInsert);
424 
425   MOZ_CAN_RUN_SCRIPT_BOUNDARY
426   mozilla::ipc::IPCResult RecvPasteTransferable(
427       const IPCDataTransfer& aDataTransfer, const bool& aIsPrivateData,
428       nsIPrincipal* aRequestingPrincipal,
429       const nsContentPolicyType& aContentPolicyType);
430 
431   mozilla::ipc::IPCResult RecvActivateFrameEvent(const nsString& aType,
432                                                  const bool& aCapture);
433 
434   mozilla::ipc::IPCResult RecvLoadRemoteScript(const nsString& aURL,
435                                                const bool& aRunInGlobalScope);
436 
437   mozilla::ipc::IPCResult RecvAsyncMessage(const nsString& aMessage,
438                                            const ClonedMessageData& aData);
439   mozilla::ipc::IPCResult RecvSwappedWithOtherRemoteLoader(
440       const IPCTabContext& aContext);
441 
442   mozilla::ipc::IPCResult RecvSafeAreaInsetsChanged(
443       const mozilla::ScreenIntMargin& aSafeAreaInsets);
444 
445 #ifdef ACCESSIBILITY
446   PDocAccessibleChild* AllocPDocAccessibleChild(PDocAccessibleChild*,
447                                                 const uint64_t&,
448                                                 const uint32_t&,
449                                                 const IAccessibleHolder&);
450   bool DeallocPDocAccessibleChild(PDocAccessibleChild*);
451 #endif
452 
453   PColorPickerChild* AllocPColorPickerChild(const nsString& aTitle,
454                                             const nsString& aInitialColor);
455 
456   bool DeallocPColorPickerChild(PColorPickerChild* aActor);
457 
458   PFilePickerChild* AllocPFilePickerChild(const nsString& aTitle,
459                                           const int16_t& aMode);
460 
461   virtual PVsyncChild* AllocPVsyncChild();
462 
463   virtual bool DeallocPVsyncChild(PVsyncChild* aActor);
464 
465   RefPtr<VsyncChild> GetVsyncChild();
466 
467   bool DeallocPFilePickerChild(PFilePickerChild* aActor);
468 
WebNavigation()469   nsIWebNavigation* WebNavigation() const { return mWebNav; }
470 
WebWidget()471   PuppetWidget* WebWidget() { return mPuppetWidget; }
472 
IsTransparent()473   bool IsTransparent() const { return mIsTransparent; }
474 
GetEffectsInfo()475   const EffectsInfo& GetEffectsInfo() const { return mEffectsInfo; }
476 
GetOrientation()477   hal::ScreenOrientation GetOrientation() const { return mOrientation; }
478 
479   void SetBackgroundColor(const nscolor& aColor);
480 
481   MOZ_CAN_RUN_SCRIPT_BOUNDARY virtual mozilla::ipc::IPCResult RecvUpdateEffects(
482       const EffectsInfo& aEffects);
483 
484   void RequestEditCommands(nsIWidget::NativeKeyBindingsType aType,
485                            const WidgetKeyboardEvent& aEvent,
486                            nsTArray<CommandInt>& aCommands);
487 
488   bool IsVisible();
IsPreservingLayers()489   bool IsPreservingLayers() const { return mIsPreservingLayers; }
490 
491   /**
492    * Signal to this BrowserChild that it should be made visible:
493    * activated widget, retained layer tree, etc.  (Respectively,
494    * made not visible.)
495    */
496   MOZ_CAN_RUN_SCRIPT void UpdateVisibility();
497   MOZ_CAN_RUN_SCRIPT void MakeVisible();
498   void MakeHidden();
499   void PresShellActivenessMaybeChanged();
500 
Manager()501   ContentChild* Manager() const { return mManager; }
502 
GetFrom(nsIDocShell * aDocShell)503   static inline BrowserChild* GetFrom(nsIDocShell* aDocShell) {
504     if (!aDocShell) {
505       return nullptr;
506     }
507 
508     nsCOMPtr<nsIBrowserChild> tc = aDocShell->GetBrowserChild();
509     return static_cast<BrowserChild*>(tc.get());
510   }
511 
GetFrom(mozIDOMWindow * aWindow)512   static inline BrowserChild* GetFrom(mozIDOMWindow* aWindow) {
513     nsCOMPtr<nsIWebNavigation> webNav = do_GetInterface(aWindow);
514     nsCOMPtr<nsIDocShell> docShell = do_QueryInterface(webNav);
515     return GetFrom(docShell);
516   }
517 
GetFrom(mozIDOMWindowProxy * aWindow)518   static inline BrowserChild* GetFrom(mozIDOMWindowProxy* aWindow) {
519     nsCOMPtr<nsIWebNavigation> webNav = do_GetInterface(aWindow);
520     nsCOMPtr<nsIDocShell> docShell = do_QueryInterface(webNav);
521     return GetFrom(docShell);
522   }
523 
524   static BrowserChild* GetFrom(PresShell* aPresShell);
525   static BrowserChild* GetFrom(layers::LayersId aLayersId);
526 
GetLayersId()527   layers::LayersId GetLayersId() { return mLayersId; }
IsLayersConnected()528   Maybe<bool> IsLayersConnected() { return mLayersConnected; }
529 
530   void DidComposite(mozilla::layers::TransactionId aTransactionId,
531                     const TimeStamp& aCompositeStart,
532                     const TimeStamp& aCompositeEnd);
533 
534   void DidRequestComposite(const TimeStamp& aCompositeReqStart,
535                            const TimeStamp& aCompositeReqEnd);
536 
537   void ClearCachedResources();
538   void InvalidateLayers();
539   void SchedulePaint();
540   void ReinitRendering();
541   void ReinitRenderingForDeviceReset();
542 
543   void NotifyJankedAnimations(const nsTArray<uint64_t>& aJankedAnimations);
544 
GetFrom(nsIDOMWindow * aWindow)545   static inline BrowserChild* GetFrom(nsIDOMWindow* aWindow) {
546     nsCOMPtr<nsIWebNavigation> webNav = do_GetInterface(aWindow);
547     nsCOMPtr<nsIDocShell> docShell = do_QueryInterface(webNav);
548     return GetFrom(docShell);
549   }
550 
551   mozilla::ipc::IPCResult RecvUIResolutionChanged(const float& aDpi,
552                                                   const int32_t& aRounding,
553                                                   const double& aScale);
554 
555   mozilla::ipc::IPCResult RecvHandleAccessKey(const WidgetKeyboardEvent& aEvent,
556                                               nsTArray<uint32_t>&& aCharCodes);
557 
558   mozilla::ipc::IPCResult RecvPrintPreview(const PrintData& aPrintData,
559                                            const MaybeDiscardedBrowsingContext&,
560                                            PrintPreviewResolver&& aCallback);
561 
562   mozilla::ipc::IPCResult RecvExitPrintPreview();
563 
564   mozilla::ipc::IPCResult RecvPrint(const MaybeDiscardedBrowsingContext&,
565                                     const PrintData&);
566 
567   mozilla::ipc::IPCResult RecvUpdateNativeWindowHandle(
568       const uintptr_t& aNewHandle);
569 
570   mozilla::ipc::IPCResult RecvWillChangeProcess();
571 
572   PPaymentRequestChild* AllocPPaymentRequestChild();
573 
574   bool DeallocPPaymentRequestChild(PPaymentRequestChild* aActor);
575 
GetClientOffset()576   LayoutDeviceIntPoint GetClientOffset() const { return mClientOffset; }
GetChromeOffset()577   LayoutDeviceIntPoint GetChromeOffset() const { return mChromeOffset; };
GetDynamicToolbarMaxHeight()578   ScreenIntCoord GetDynamicToolbarMaxHeight() const {
579     return mDynamicToolbarMaxHeight;
580   };
581 
IPCOpen()582   bool IPCOpen() const { return mIPCOpen; }
583 
584   const mozilla::layers::CompositorOptions& GetCompositorOptions() const;
585   bool AsyncPanZoomEnabled() const;
586 
587   ScreenIntSize GetInnerSize();
GetUnscaledInnerSize()588   CSSSize GetUnscaledInnerSize() { return mUnscaledInnerSize; }
589 
590   Maybe<nsRect> GetVisibleRect() const;
591 
592   // Call RecvShow(nsIntSize(0, 0)) and block future calls to RecvShow().
593   void DoFakeShow(const ParentShowInfo&);
594 
595   void ContentReceivedInputBlock(uint64_t aInputBlockId,
596                                  bool aPreventDefault) const;
597   void SetTargetAPZC(
598       uint64_t aInputBlockId,
599       const nsTArray<layers::ScrollableLayerGuid>& aTargets) const;
600   MOZ_CAN_RUN_SCRIPT_BOUNDARY
601   mozilla::ipc::IPCResult RecvHandleTap(
602       const layers::GeckoContentController_TapType& aType,
603       const LayoutDevicePoint& aPoint, const Modifiers& aModifiers,
604       const ScrollableLayerGuid& aGuid, const uint64_t& aInputBlockId);
605 
606   MOZ_CAN_RUN_SCRIPT_BOUNDARY
607   mozilla::ipc::IPCResult RecvNormalPriorityHandleTap(
608       const layers::GeckoContentController_TapType& aType,
609       const LayoutDevicePoint& aPoint, const Modifiers& aModifiers,
610       const ScrollableLayerGuid& aGuid, const uint64_t& aInputBlockId);
611 
612   void SetAllowedTouchBehavior(
613       uint64_t aInputBlockId, const nsTArray<TouchBehaviorFlags>& aFlags) const;
614 
615   bool UpdateFrame(const layers::RepaintRequest& aRequest);
616   bool NotifyAPZStateChange(
617       const ViewID& aViewId,
618       const layers::GeckoContentController_APZStateChange& aChange,
619       const int& aArg);
620   void StartScrollbarDrag(const layers::AsyncDragMetrics& aDragMetrics);
621   void ZoomToRect(const uint32_t& aPresShellId,
622                   const ScrollableLayerGuid::ViewID& aViewId,
623                   const CSSRect& aRect, const uint32_t& aFlags);
624 
625   // Request that the docshell be marked as active.
626   void PaintWhileInterruptingJS(const layers::LayersObserverEpoch& aEpoch);
627 
628   nsresult CanCancelContentJS(nsIRemoteTab::NavigationType aNavigationType,
629                               int32_t aNavigationIndex, nsIURI* aNavigationURI,
630                               int32_t aEpoch, bool* aCanCancel);
631 
LayersObserverEpoch()632   layers::LayersObserverEpoch LayersObserverEpoch() const {
633     return mLayersObserverEpoch;
634   }
635 
636 #if defined(XP_WIN) && defined(ACCESSIBILITY)
GetNativeWindowHandle()637   uintptr_t GetNativeWindowHandle() const { return mNativeWindowHandle; }
638 #endif
639 
GetBrowsingContext()640   BrowsingContext* GetBrowsingContext() const { return mBrowsingContext; }
641 
642 #if defined(ACCESSIBILITY)
SetTopLevelDocAccessibleChild(PDocAccessibleChild * aTopLevelChild)643   void SetTopLevelDocAccessibleChild(PDocAccessibleChild* aTopLevelChild) {
644     mTopLevelDocAccessibleChild = aTopLevelChild;
645   }
646 
GetTopLevelDocAccessibleChild()647   PDocAccessibleChild* GetTopLevelDocAccessibleChild() {
648     return mTopLevelDocAccessibleChild;
649   }
650 #endif
651 
652   // The transform from the coordinate space of this BrowserChild to the
653   // coordinate space of the native window its BrowserParent is in.
654   mozilla::LayoutDeviceToLayoutDeviceMatrix4x4
655   GetChildToParentConversionMatrix() const;
656 
657   // Returns the portion of the visible rect of this remote document in the
658   // top browser window coordinate system.  This is the result of being clipped
659   // by all ancestor viewports.
660   Maybe<ScreenRect> GetTopLevelViewportVisibleRectInBrowserCoords() const;
661 
662   // Similar to above GetTopLevelViewportVisibleRectInBrowserCoords(), but in
663   // this out-of-process document's coordinate system.
664   Maybe<LayoutDeviceRect> GetTopLevelViewportVisibleRectInSelfCoords() const;
665 
666   // Prepare to dispatch all coalesced mousemove events. We'll move all data
667   // in mCoalescedMouseData to a nsDeque; then we start processing them. We
668   // can't fetch the coalesced event one by one and dispatch it because we may
669   // reentry the event loop and access to the same hashtable. It's called when
670   // dispatching some mouse events other than mousemove.
671   void FlushAllCoalescedMouseData();
672   void ProcessPendingCoalescedMouseDataAndDispatchEvents();
673 
674   void ProcessPendingColaescedTouchData();
675 
676   void HandleRealMouseButtonEvent(const WidgetMouseEvent& aEvent,
677                                   const ScrollableLayerGuid& aGuid,
678                                   const uint64_t& aInputBlockId);
679 
SetCancelContentJSEpoch(int32_t aEpoch)680   void SetCancelContentJSEpoch(int32_t aEpoch) {
681     mCancelContentJSEpoch = aEpoch;
682   }
683 
684   bool UpdateSessionStore();
685 
686 #ifdef XP_WIN
687   // Check if the window this BrowserChild is associated with supports
688   // protected media (EME) or not.
689   // Returns a promise the will resolve true if the window supports protected
690   // media or false if it does not. The promise will be rejected with an
691   // ResponseRejectReason if the IPC needed to do the check fails. Callers
692   // should treat the reject case as if the window does not support protected
693   // media to ensure robust handling.
694   RefPtr<IsWindowSupportingProtectedMediaPromise>
695   DoesWindowSupportProtectedMedia();
696 #endif
697 
698   // Notify the content blocking event in the parent process. This sends an IPC
699   // message to the BrowserParent in the parent. The BrowserParent will find the
700   // top-level WindowGlobalParent and notify the event from it.
701   void NotifyContentBlockingEvent(
702       uint32_t aEvent, nsIChannel* aChannel, bool aBlocked,
703       const nsACString& aTrackingOrigin,
704       const nsTArray<nsCString>& aTrackingFullHashes,
705       const Maybe<
706           ContentBlockingNotifier::StorageAccessPermissionGrantedReason>&
707           aReason);
708 
709  protected:
710   virtual ~BrowserChild();
711 
712   mozilla::ipc::IPCResult RecvDestroy();
713 
714   MOZ_CAN_RUN_SCRIPT_BOUNDARY
715   mozilla::ipc::IPCResult RecvRenderLayers(
716       const bool& aEnabled, const layers::LayersObserverEpoch& aEpoch);
717 
718   mozilla::ipc::IPCResult RecvPreserveLayers(bool);
719 
720   mozilla::ipc::IPCResult RecvNavigateByKey(const bool& aForward,
721                                             const bool& aForDocumentNavigation);
722 
723   mozilla::ipc::IPCResult RecvRequestNotifyAfterRemotePaint();
724 
725   mozilla::ipc::IPCResult RecvSuppressDisplayport(const bool& aEnabled);
726 
727   mozilla::ipc::IPCResult RecvScrollbarPreferenceChanged(ScrollbarPreference);
728 
729   mozilla::ipc::IPCResult RecvSetKeyboardIndicators(
730       const UIStateChangeType& aShowFocusRings);
731 
732   mozilla::ipc::IPCResult RecvStopIMEStateManagement();
733 
734   mozilla::ipc::IPCResult RecvAllowScriptsToClose();
735 
736   mozilla::ipc::IPCResult RecvReleaseAllPointerCapture();
737 
738  private:
739   void HandleDoubleTap(const CSSPoint& aPoint, const Modifiers& aModifiers,
740                        const ScrollableLayerGuid& aGuid);
741 
742   void ActorDestroy(ActorDestroyReason why) override;
743 
744   bool InitBrowserChildMessageManager();
745 
746   void InitRenderingState(
747       const TextureFactoryIdentifier& aTextureFactoryIdentifier,
748       const layers::LayersId& aLayersId,
749       const mozilla::layers::CompositorOptions& aCompositorOptions);
750   void InitAPZState();
751 
752   void DestroyWindow();
753 
754   void ApplyParentShowInfo(const ParentShowInfo&);
755 
756   bool HasValidInnerSize();
757 
758   ScreenIntRect GetOuterRect();
759 
SetUnscaledInnerSize(const CSSSize & aSize)760   void SetUnscaledInnerSize(const CSSSize& aSize) {
761     mUnscaledInnerSize = aSize;
762   }
763 
764   bool SkipRepeatedKeyEvent(const WidgetKeyboardEvent& aEvent);
765 
766   void UpdateRepeatedKeyEventEndTime(const WidgetKeyboardEvent& aEvent);
767 
768   void DispatchCoalescedWheelEvent();
769 
770   /**
771    * Dispatch aEvent on aEvent.mWidget.
772    */
773   nsEventStatus DispatchWidgetEventViaAPZ(WidgetGUIEvent& aEvent);
774 
775   void DispatchWheelEvent(const WidgetWheelEvent& aEvent,
776                           const ScrollableLayerGuid& aGuid,
777                           const uint64_t& aInputBlockId);
778 
779   void InternalSetDocShellIsActive(bool aIsActive);
780 
781   bool CreateRemoteLayerManager(
782       mozilla::layers::PCompositorBridgeChild* aCompositorChild);
783 
784   nsresult PrepareRequestData(nsIRequest* aRequest, RequestData& aRequestData);
785   nsresult PrepareProgressListenerData(nsIWebProgress* aWebProgress,
786                                        nsIRequest* aRequest,
787                                        WebProgressData& aWebProgressData,
788                                        RequestData& aRequestData);
789 
790   class DelayedDeleteRunnable;
791 
792   RefPtr<BrowserChildMessageManager> mBrowserChildMessageManager;
793   TextureFactoryIdentifier mTextureFactoryIdentifier;
794   RefPtr<nsWebBrowser> mWebBrowser;
795   nsCOMPtr<nsIWebNavigation> mWebNav;
796   RefPtr<PuppetWidget> mPuppetWidget;
797   nsCOMPtr<nsIURI> mLastURI;
798   RefPtr<ContentChild> mManager;
799   RefPtr<BrowsingContext> mBrowsingContext;
800   RefPtr<nsBrowserStatusFilter> mStatusFilter;
801   uint32_t mChromeFlags;
802   uint32_t mMaxTouchPoints;
803   layers::LayersId mLayersId;
804   CSSRect mUnscaledOuterRect;
805   Maybe<bool> mLayersConnected;
806   EffectsInfo mEffectsInfo;
807   hal::ScreenOrientation mOrientation;
808 
809   RefPtr<VsyncChild> mVsyncChild;
810 
811   RefPtr<APZEventState> mAPZEventState;
812   SetAllowedTouchBehaviorCallback mSetAllowedTouchBehaviorCallback;
813 
814   // Position of client area relative to the outer window
815   LayoutDeviceIntPoint mClientOffset;
816   // Position of tab, relative to parent widget (typically the window)
817   // NOTE: This value is valuable only for the top level browser.
818   LayoutDeviceIntPoint mChromeOffset;
819   ScreenIntCoord mDynamicToolbarMaxHeight;
820   TabId mUniqueId;
821 
822   bool mDidFakeShow : 1;
823   bool mTriedBrowserInit : 1;
824   bool mIgnoreKeyPressEvent : 1;
825   bool mHasValidInnerSize : 1;
826   bool mDestroyed : 1;
827 
828   // Whether or not this browser is the child part of the top level PBrowser
829   // actor in a remote browser.
830   bool mIsTopLevel : 1;
831 
832   // Whether or not this tab has siblings (other tabs in the same window).
833   // This is one factor used when choosing to allow or deny a non-system
834   // script's attempt to resize the window.
835   bool mHasSiblings : 1;
836 
837   bool mIsTransparent : 1;
838   bool mIPCOpen : 1;
839 
840   bool mDidSetRealShowInfo : 1;
841   bool mDidLoadURLInit : 1;
842 
843   bool mSkipKeyPress : 1;
844   bool mDidSetEffectsInfo : 1;
845 
846   bool mCoalesceMouseMoveEvents : 1;
847 
848   bool mShouldSendWebProgressEventsToParent : 1;
849 
850   // Whether we are rendering to the compositor or not.
851   bool mRenderLayers : 1;
852 
853   // Whether we're artificially preserving layers.
854   bool mIsPreservingLayers : 1;
855 
856   // In some circumstances, a DocShell might be in a state where it is
857   // "blocked", and we should not attempt to change its active state or
858   // the underlying PresShell state until the DocShell becomes unblocked.
859   // It is possible, however, for the parent process to send commands to
860   // change those states while the DocShell is blocked. We store those
861   // states temporarily as "pending", and only apply them once the DocShell
862   // is no longer blocked.
863   bool mPendingDocShellIsActive : 1;
864   bool mPendingDocShellReceivedMessage : 1;
865   bool mPendingRenderLayers : 1;
866   bool mPendingRenderLayersReceivedMessage : 1;
867 
868   // Holds the compositor options for the compositor rendering this tab,
869   // once we find out which compositor that is.
870   Maybe<mozilla::layers::CompositorOptions> mCompositorOptions;
871 
872   friend class ContentChild;
873 
874 
875   CSSSize mUnscaledInnerSize;
876 
877   // Store the end time of the handling of the last repeated keydown/keypress
878   // event so that in case event handling takes time, some repeated events can
879   // be skipped to not flood child process.
880   mozilla::TimeStamp mRepeatedKeyEventTime;
881 
882   // Similar to mRepeatedKeyEventTime, store the end time (from parent process)
883   // of handling the last repeated wheel event so that in case event handling
884   // takes time, some repeated events can be skipped to not flood child process.
885   mozilla::TimeStamp mLastWheelProcessedTimeFromParent;
886   mozilla::TimeDuration mLastWheelProcessingDuration;
887 
888   // Hash table to track coalesced mousemove events for different pointers.
889   nsClassHashtable<nsUint32HashKey, CoalescedMouseData> mCoalescedMouseData;
890 
891   nsDeque<CoalescedMouseData> mToBeDispatchedMouseData;
892 
893   CoalescedWheelData mCoalescedWheelData;
894   CoalescedTouchData mCoalescedTouchData;
895 
896   RefPtr<CoalescedMouseMoveFlusher> mCoalescedMouseEventFlusher;
897   RefPtr<CoalescedTouchMoveFlusher> mCoalescedTouchMoveEventFlusher;
898 
899   RefPtr<layers::IAPZCTreeManager> mApzcTreeManager;
900   RefPtr<TabListener> mSessionStoreListener;
901   RefPtr<SessionStoreChangeListener> mSessionStoreChangeListener;
902 
903   // The most recently seen layer observer epoch in RecvSetDocShellIsActive.
904   layers::LayersObserverEpoch mLayersObserverEpoch;
905 
906 #if defined(XP_WIN) && defined(ACCESSIBILITY)
907   // The handle associated with the native window that contains this tab
908   uintptr_t mNativeWindowHandle;
909 #endif  // defined(XP_WIN)
910 
911 #if defined(ACCESSIBILITY)
912   PDocAccessibleChild* mTopLevelDocAccessibleChild;
913 #endif
914   layers::LayersObserverEpoch mPendingLayersObserverEpoch;
915   // When mPendingDocShellBlockers is greater than 0, the DocShell is blocked,
916   // and once it reaches 0, it is no longer blocked.
917   uint32_t mPendingDocShellBlockers;
918   int32_t mCancelContentJSEpoch;
919 
920   Maybe<LayoutDeviceToLayoutDeviceMatrix4x4> mChildToParentConversionMatrix;
921   // When mChildToParentConversionMatrix is Nothing() this value is invalid.
922   ScreenRect mTopLevelViewportVisibleRectInBrowserCoords;
923 
924 #ifdef XP_WIN
925   // Should only be accessed on main thread.
926   Maybe<bool> mWindowSupportsProtectedMedia;
927 #endif
928 
929   DISALLOW_EVIL_CONSTRUCTORS(BrowserChild);
930 };
931 
932 }  // namespace dom
933 }  // namespace mozilla
934 
935 #endif  // mozilla_dom_BrowserChild_h
936