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 __nsWindowWatcher_h__
8 #define __nsWindowWatcher_h__
9 
10 // {a21bfa01-f349-4394-a84c-8de5cf0737d0}
11 #define NS_WINDOWWATCHER_CID                        \
12   {                                                 \
13     0xa21bfa01, 0xf349, 0x4394, {                   \
14       0xa8, 0x4c, 0x8d, 0xe5, 0xcf, 0x7, 0x37, 0xd0 \
15     }                                               \
16   }
17 
18 #include "nsCOMPtr.h"
19 #include "mozilla/Mutex.h"
20 #include "mozilla/Maybe.h"
21 #include "nsIWindowCreator.h"  // for stupid compilers
22 #include "nsIWindowWatcher.h"
23 #include "nsIPromptFactory.h"
24 #include "nsITabParent.h"
25 #include "nsPIWindowWatcher.h"
26 #include "nsTArray.h"
27 
28 class nsIURI;
29 class nsIDocShellTreeItem;
30 class nsIDocShellTreeOwner;
31 class nsPIDOMWindowOuter;
32 class nsWatcherWindowEnumerator;
33 class nsPromptService;
34 struct nsWatcherWindowEntry;
35 struct SizeSpec;
36 
37 class nsWindowWatcher : public nsIWindowWatcher,
38                         public nsPIWindowWatcher,
39                         public nsIPromptFactory {
40   friend class nsWatcherWindowEnumerator;
41 
42  public:
43   nsWindowWatcher();
44 
45   nsresult Init();
46 
47   NS_DECL_ISUPPORTS
48 
49   NS_DECL_NSIWINDOWWATCHER
50   NS_DECL_NSPIWINDOWWATCHER
51   NS_DECL_NSIPROMPTFACTORY
52 
53   static int32_t GetWindowOpenLocation(nsPIDOMWindowOuter* aParent,
54                                        uint32_t aChromeFlags,
55                                        bool aCalledFromJS,
56                                        bool aPositionSpecified,
57                                        bool aSizeSpecified);
58 
59  protected:
60   virtual ~nsWindowWatcher();
61 
62   friend class nsPromptService;
63   bool AddEnumerator(nsWatcherWindowEnumerator* aEnumerator);
64   bool RemoveEnumerator(nsWatcherWindowEnumerator* aEnumerator);
65 
66   nsWatcherWindowEntry* FindWindowEntry(mozIDOMWindowProxy* aWindow);
67   nsresult RemoveWindow(nsWatcherWindowEntry* aInfo);
68 
69   // Get the caller tree item.  Look on the JS stack, then fall back
70   // to the parent if there's nothing there.
71   already_AddRefed<nsIDocShellTreeItem> GetCallerTreeItem(
72       nsIDocShellTreeItem* aParentItem);
73 
74   // Unlike GetWindowByName this will look for a caller on the JS
75   // stack, and then fall back on aCurrentWindow if it can't find one.
76   // It also knows to not look for things if aForceNoOpener is set.
77   nsPIDOMWindowOuter* SafeGetWindowByName(const nsAString& aName,
78                                           bool aForceNoOpener,
79                                           mozIDOMWindowProxy* aCurrentWindow);
80 
81   // Just like OpenWindowJS, but knows whether it got called via OpenWindowJS
82   // (which means called from script) or called via OpenWindow.
83   nsresult OpenWindowInternal(mozIDOMWindowProxy* aParent, const char* aUrl,
84                               const char* aName, const char* aFeatures,
85                               bool aCalledFromJS, bool aDialog, bool aNavigate,
86                               nsIArray* aArgv, bool aIsPopupSpam,
87                               bool aForceNoOpener,
88                               nsIDocShellLoadInfo* aLoadInfo,
89                               mozIDOMWindowProxy** aResult);
90 
91   static nsresult URIfromURL(const char* aURL, mozIDOMWindowProxy* aParent,
92                              nsIURI** aURI);
93 
94   static uint32_t CalculateChromeFlagsForChild(const nsACString& aFeaturesStr);
95 
96   static uint32_t CalculateChromeFlagsForParent(mozIDOMWindowProxy* aParent,
97                                                 const nsACString& aFeaturesStr,
98                                                 bool aDialog, bool aChromeURL,
99                                                 bool aHasChromeParent,
100                                                 bool aCalledFromJS);
101 
102   static int32_t WinHasOption(const nsACString& aOptions, const char* aName,
103                               int32_t aDefault, bool* aPresenceFlag);
104   /* Compute the right SizeSpec based on aFeatures */
105   static void CalcSizeSpec(const nsACString& aFeatures, SizeSpec& aResult);
106   static nsresult ReadyOpenedDocShellItem(nsIDocShellTreeItem* aOpenedItem,
107                                           nsPIDOMWindowOuter* aParent,
108                                           bool aWindowIsNew,
109                                           bool aForceNoOpener,
110                                           mozIDOMWindowProxy** aOpenedWindow);
111   static void SizeOpenedWindow(
112       nsIDocShellTreeOwner* aTreeOwner, mozIDOMWindowProxy* aParent,
113       bool aIsCallerChrome, const SizeSpec& aSizeSpec,
114       const mozilla::Maybe<float>& aOpenerFullZoom = mozilla::Nothing());
115   static void GetWindowTreeItem(mozIDOMWindowProxy* aWindow,
116                                 nsIDocShellTreeItem** aResult);
117   static void GetWindowTreeOwner(nsPIDOMWindowOuter* aWindow,
118                                  nsIDocShellTreeOwner** aResult);
119 
120  private:
121   nsresult CreateChromeWindow(const nsACString& aFeatures,
122                               nsIWebBrowserChrome* aParentChrome,
123                               uint32_t aChromeFlags,
124                               nsITabParent* aOpeningTabParent,
125                               mozIDOMWindowProxy* aOpener,
126                               uint64_t aNextTabParentId,
127                               nsIWebBrowserChrome** aResult);
128 
129   void MaybeDisablePersistence(const nsACString& aFeatures,
130                                nsIDocShellTreeOwner* aTreeOwner);
131 
132   static uint32_t CalculateChromeFlagsHelper(uint32_t aInitialFlags,
133                                              const nsACString& aFeatures,
134                                              bool& presenceFlag,
135                                              bool aDialog = false,
136                                              bool aHasChromeParent = false,
137                                              bool aChromeURL = false);
138   static uint32_t EnsureFlagsSafeForContent(uint32_t aChromeFlags,
139                                             bool aChromeURL = false);
140 
141  protected:
142   nsTArray<nsWatcherWindowEnumerator*> mEnumeratorList;
143   nsWatcherWindowEntry* mOldestWindow;
144   mozilla::Mutex mListLock;
145 
146   nsCOMPtr<nsIWindowCreator> mWindowCreator;
147 };
148 
149 #endif
150