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 nsWebBrowser_h__
8 #define nsWebBrowser_h__
9 
10 // Local Includes
11 #include "nsDocShellTreeOwner.h"
12 
13 // Core Includes
14 #include "nsCOMPtr.h"
15 #include "nsCycleCollectionParticipant.h"
16 
17 // Interfaces needed
18 #include "nsIBaseWindow.h"
19 #include "nsIDocShell.h"
20 #include "nsIDocShellTreeItem.h"
21 #include "nsIInterfaceRequestor.h"
22 #include "nsIInterfaceRequestorUtils.h"
23 #include "nsIWidget.h"
24 #include "nsIWebProgress.h"
25 #include "nsIWebBrowser.h"
26 #include "nsIWebNavigation.h"
27 #include "nsIWebBrowserPersist.h"
28 #include "nsIWindowWatcher.h"
29 #include "nsIPrintSettings.h"
30 #include "nsIWidgetListener.h"
31 
32 #include "mozilla/BasePrincipal.h"
33 #include "nsTArray.h"
34 #include "nsIWeakReferenceUtils.h"
35 
36 class nsWebBrowserInitInfo {
37  public:
38   // nsIBaseWindow Stuff
39   int32_t x;
40   int32_t y;
41   int32_t cx;
42   int32_t cy;
43   bool visible;
44   nsString name;
45 };
46 
47 //  {cda5863a-aa9c-411e-be49-ea0d525ab4b5} -
48 #define NS_WEBBROWSER_CID                            \
49   {                                                  \
50     0xcda5863a, 0xaa9c, 0x411e, {                    \
51       0xbe, 0x49, 0xea, 0x0d, 0x52, 0x5a, 0xb4, 0xb5 \
52     }                                                \
53   }
54 
55 class mozIDOMWindowProxy;
56 
57 namespace mozilla {
58 namespace dom {
59 class WindowGlobalChild;
60 }  // namespace dom
61 }  // namespace mozilla
62 
63 class nsWebBrowser final : public nsIWebBrowser,
64                            public nsIWebNavigation,
65                            public nsIDocShellTreeItem,
66                            public nsIBaseWindow,
67                            public nsIInterfaceRequestor,
68                            public nsIWebBrowserPersist,
69                            public nsIWebProgressListener,
70                            public nsSupportsWeakReference {
71   friend class nsDocShellTreeOwner;
72 
73  public:
74   // The implementation of non-refcounted nsIWidgetListener, which would hold a
75   // strong reference on stack before calling nsWebBrowser's
76   // MOZ_CAN_RUN_SCRIPT methods.
77   class WidgetListenerDelegate : public nsIWidgetListener {
78    public:
WidgetListenerDelegate(nsWebBrowser * aWebBrowser)79     explicit WidgetListenerDelegate(nsWebBrowser* aWebBrowser)
80         : mWebBrowser(aWebBrowser) {}
81     MOZ_CAN_RUN_SCRIPT_BOUNDARY virtual void WindowActivated() override;
82     MOZ_CAN_RUN_SCRIPT_BOUNDARY virtual void WindowDeactivated() override;
83     MOZ_CAN_RUN_SCRIPT_BOUNDARY virtual bool PaintWindow(
84         nsIWidget* aWidget, mozilla::LayoutDeviceIntRegion aRegion) override;
85 
86    private:
87     // The lifetime of WidgetListenerDelegate is bound to nsWebBrowser so we
88     // just use raw pointer here.
89     nsWebBrowser* mWebBrowser;
90   };
91 
92   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
93   NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(nsWebBrowser, nsIWebBrowser)
94 
95   NS_DECL_NSIBASEWINDOW
96   NS_DECL_NSIDOCSHELLTREEITEM
97   NS_DECL_NSIINTERFACEREQUESTOR
98   NS_DECL_NSIWEBBROWSER
99   NS_DECL_NSIWEBNAVIGATION
100   NS_DECL_NSIWEBBROWSERPERSIST
101   NS_DECL_NSICANCELABLE
102   NS_DECL_NSIWEBPROGRESSLISTENER
103 
104   void SetAllowDNSPrefetch(bool aAllowPrefetch);
105   void FocusActivate();
106   void FocusDeactivate();
107   void SetWillChangeProcess();
108 
109   static already_AddRefed<nsWebBrowser> Create(
110       nsIWebBrowserChrome* aContainerWindow, nsIWidget* aParentWidget,
111       mozilla::dom::BrowsingContext* aBrowsingContext,
112       mozilla::dom::WindowGlobalChild* aInitialWindowChild);
113 
114  protected:
115   virtual ~nsWebBrowser();
116   NS_IMETHOD InternalDestroy();
117 
118   // XXXbz why are these NS_IMETHOD?  They're not interface methods!
119   NS_IMETHOD SetDocShell(nsIDocShell* aDocShell);
120   NS_IMETHOD EnsureDocShellTreeOwner();
121 
122   nsIWidget* EnsureWidget();
123 
124   // nsIWidgetListener methods for WidgetListenerDelegate.
125   MOZ_CAN_RUN_SCRIPT void WindowActivated();
126   MOZ_CAN_RUN_SCRIPT void WindowDeactivated();
127   MOZ_CAN_RUN_SCRIPT bool PaintWindow(nsIWidget* aWidget,
128                                       mozilla::LayoutDeviceIntRegion aRegion);
129 
130   explicit nsWebBrowser(int aItemType);
131 
132  protected:
133   RefPtr<nsDocShellTreeOwner> mDocShellTreeOwner;
134   nsCOMPtr<nsIDocShell> mDocShell;
135   nsCOMPtr<nsIInterfaceRequestor> mDocShellAsReq;
136   nsCOMPtr<nsIBaseWindow> mDocShellAsWin;
137   nsCOMPtr<nsIWebNavigation> mDocShellAsNav;
138   mozilla::OriginAttributes mOriginAttributes;
139 
140   nsCOMPtr<nsIWidget> mInternalWidget;
141   nsCOMPtr<nsIWindowWatcher> mWWatch;
142   const uint32_t mContentType;
143   bool mShouldEnableHistory;
144   bool mWillChangeProcess;
145   nativeWindow mParentNativeWindow;
146   nsIWebProgressListener* mProgressListener;
147   nsCOMPtr<nsIWebProgress> mWebProgress;
148 
149   nsCOMPtr<nsIPrintSettings> mPrintSettings;
150 
151   WidgetListenerDelegate mWidgetListenerDelegate;
152 
153   // cached background color
154   nscolor mBackgroundColor;
155 
156   // persistence object
157   nsCOMPtr<nsIWebBrowserPersist> mPersist;
158   uint32_t mPersistCurrentState;
159   nsresult mPersistResult;
160   uint32_t mPersistFlags;
161 
162   // Weak Reference interfaces...
163   nsIWidget* mParentWidget;
164 };
165 
166 #endif /* nsWebBrowser_h__ */
167