summaryrefslogtreecommitdiffstats
path: root/toolkit/components/windowwatcher/test/browser_new_content_window_chromeflags.js
blob: d76fa9bd4074833f110dce42fe4f9b74b0df64fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**
 * Tests that chromeFlags are set properly on windows that are
 * being opened from content.
 */

// The following are not allowed from web content.
//
// <feature string>: {
//   flag: <associated nsIWebBrowserChrome flag>,
//   defaults_to: <what this feature defaults to normally>
// }
const DISALLOWED = {
  location: {
    flag: Ci.nsIWebBrowserChrome.CHROME_LOCATIONBAR,
    defaults_to: true,
  },
  chrome: {
    flag: Ci.nsIWebBrowserChrome.CHROME_OPENAS_CHROME,
    defaults_to: false,
  },
  dialog: {
    flag: Ci.nsIWebBrowserChrome.CHROME_OPENAS_DIALOG,
    defaults_to: false,
  },
  private: {
    flag: Ci.nsIWebBrowserChrome.CHROME_PRIVATE_WINDOW,
    defaults_to: false,
  },
  "non-private": {
    flag: Ci.nsIWebBrowserChrome.CHROME_NON_PRIVATE_WINDOW,
    defaults_to: false,
  },
  // "all":
  //   checked manually, since this is an aggregate
  //   flag.
  //
  // "remote":
  //   checked manually, since its default value will
  //   depend on whether or not e10s is enabled by default.
  popup: {
    flag: Ci.nsIWebBrowserChrome.CHROME_WINDOW_POPUP,
    defaults_to: false,
  },
  alwaysLowered: {
    flag: Ci.nsIWebBrowserChrome.CHROME_WINDOW_LOWERED,
    defaults_to: false,
  },
  "z-lock": {
    flag: Ci.nsIWebBrowserChrome.CHROME_WINDOW_LOWERED, // Renamed to alwaysLowered
    defaults_to: false,
  },
  alwaysRaised: {
    flag: Ci.nsIWebBrowserChrome.CHROME_WINDOW_RAISED,
    defaults_to: false,
  },
  alwaysOnTop: {
    flag: Ci.nsIWebBrowserChrome.CHROME_ALWAYS_ON_TOP,
    defaults_to: false,
  },
  suppressanimation: {
    flag: Ci.nsIWebBrowserChrome.CHROME_SUPPRESS_ANIMATION,
    defaults_to: false,
  },
  extrachrome: {
    flag: Ci.nsIWebBrowserChrome.CHROME_EXTRA,
    defaults_to: false,
  },
  centerscreen: {
    flag: Ci.nsIWebBrowserChrome.CHROME_CENTER_SCREEN,
    defaults_to: false,
  },
  dependent: {
    flag: Ci.nsIWebBrowserChrome.CHROME_DEPENDENT,
    defaults_to: false,
  },
  modal: {
    flag: Ci.nsIWebBrowserChrome.CHROME_MODAL,
    defaults_to: false,
  },
  titlebar: {
    flag: Ci.nsIWebBrowserChrome.CHROME_TITLEBAR,
    defaults_to: true,
  },
  close: {
    flag: Ci.nsIWebBrowserChrome.CHROME_WINDOW_CLOSE,
    defaults_to: true,
  },
  resizable: {
    flag: Ci.nsIWebBrowserChrome.CHROME_WINDOW_RESIZE,
    defaults_to: true,
  },
  status: {
    flag: Ci.nsIWebBrowserChrome.CHROME_STATUSBAR,
    defaults_to: true,
  },
};

// This magic value of 2 means that by default, when content tries
// to open a new window, it'll actually open in a new window instead
// of a new tab.
Services.prefs.setIntPref("browser.link.open_newwindow", 2);
registerCleanupFunction(() => {
  Services.prefs.clearUserPref("browser.link.open_newwindow");
});

/**
 * Given some nsIDOMWindow for a window running in the parent process,
 * asynchronously return the nsIWebBrowserChrome chrome flags for the
 * associated content window.
 *
 * @param win (nsIDOMWindow)
 * @returns int
 */
function getContentChromeFlags(win) {
  let b = win.gBrowser.selectedBrowser;
  return SpecialPowers.spawn(b, [], async function () {
    // Content scripts provide docShell as a global.
    /* global docShell */
    docShell.QueryInterface(Ci.nsIInterfaceRequestor);
    try {
      // This will throw if we're not a remote browser.
      return docShell
        .getInterface(Ci.nsIBrowserChild)
        .QueryInterface(Ci.nsIWebBrowserChrome).chromeFlags;
    } catch (e) {
      // This must be a non-remote browser...
      return docShell.treeOwner.QueryInterface(
        Ci.nsIWebBrowserChrome
      ).chromeFlags;
    }
  });
}

/**
 * For some chromeFlags, ensures that flags in the DISALLOWED
 * group were not modified.
 *
 * @param chromeFlags (int)
 *        Some chromeFlags to check.
 */
function assertContentFlags(chromeFlags, isPopup) {
  for (let feature in DISALLOWED) {
    let flag = DISALLOWED[feature].flag;
    Assert.ok(flag, "Expected flag to be a non-zeroish value");
    if (DISALLOWED[feature].defaults_to) {
      // The feature is supposed to default to true, so it should
      // stay true.
      Assert.ok(
        chromeFlags & flag,
        `Expected feature ${feature} to be unchanged`
      );
    } else {
      // The feature is supposed to default to false, so it should
      // stay false.
      Assert.ok(
        !(chromeFlags & flag),
        `Expected feature ${feature} to be unchanged`
      );
    }
  }
}

/**
 * Opens a window from content using window.open with the
 * features computed from DISALLOWED. The computed feature string attempts to
 * flip every feature away from their default.
 */
add_task(async function test_disallowed_flags() {
  // Construct a features string that flips all DISALLOWED features
  // to not be their defaults.
  const DISALLOWED_STRING = Object.keys(DISALLOWED)
    .map(feature => {
      let toValue = DISALLOWED[feature].defaults_to ? "no" : "yes";
      return `${feature}=${toValue}`;
    })
    .join(",");

  const FEATURES = [DISALLOWED_STRING].join(",");

  const SCRIPT_PAGE = `data:text/html,<script>window.open("about:blank", "_blank", "${FEATURES}");</script>`;
  const SCRIPT_PAGE_FOR_CHROME_ALL = `data:text/html,<script>window.open("about:blank", "_blank", "all");</script>`;

  let newWinPromise = BrowserTestUtils.waitForNewWindow();

  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: SCRIPT_PAGE,
    },
    async function (browser) {
      let win = await newWinPromise;
      let parentChromeFlags = getParentChromeFlags(win);
      assertContentFlags(parentChromeFlags);

      if (win.gMultiProcessBrowser) {
        Assert.ok(
          parentChromeFlags & Ci.nsIWebBrowserChrome.CHROME_REMOTE_WINDOW,
          "Should be remote by default"
        );
      } else {
        Assert.ok(
          !(parentChromeFlags & Ci.nsIWebBrowserChrome.CHROME_REMOTE_WINDOW),
          "Should not be remote by default"
        );
      }

      // Confusingly, chromeFlags also exist in the content process
      // as part of the BrowserChild, so we have to check those too.
      let contentChromeFlags = await getContentChromeFlags(win);
      assertContentFlags(contentChromeFlags);

      Assert.equal(
        parentChromeFlags & Ci.nsIWebBrowserChrome.CHROME_REMOTE_WINDOW,
        contentChromeFlags & Ci.nsIWebBrowserChrome.CHROME_REMOTE_WINDOW,
        "Should have matching remote value in parent and content"
      );

      Assert.equal(
        parentChromeFlags & Ci.nsIWebBrowserChrome.CHROME_FISSION_WINDOW,
        contentChromeFlags & Ci.nsIWebBrowserChrome.CHROME_FISSION_WINDOW,
        "Should have matching fission value in parent and content"
      );

      await BrowserTestUtils.closeWindow(win);
    }
  );

  // We check "all" manually, since that's an aggregate flag
  // and doesn't fit nicely into the ALLOWED / DISALLOWED scheme
  newWinPromise = BrowserTestUtils.waitForNewWindow();

  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: SCRIPT_PAGE_FOR_CHROME_ALL,
    },
    async function (browser) {
      let win = await newWinPromise;
      let parentChromeFlags = getParentChromeFlags(win);
      Assert.notEqual(
        parentChromeFlags & Ci.nsIWebBrowserChrome.CHROME_ALL,
        Ci.nsIWebBrowserChrome.CHROME_ALL,
        "Should not have been able to set CHROME_ALL"
      );
      await BrowserTestUtils.closeWindow(win);
    }
  );
});

/**
 * Opens a window with some chrome flags specified, which should not affect
 * scrollbars flag which defaults to true when not disabled explicitly.
 */
add_task(async function test_scrollbars_flag() {
  const SCRIPT = 'window.open("about:blank", "_blank", "toolbar=0");';
  const SCRIPT_PAGE = `data:text/html,<script>${SCRIPT}</script>`;

  let newWinPromise = BrowserTestUtils.waitForNewWindow();
  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: SCRIPT_PAGE,
    },
    async function (browser) {
      let win = await newWinPromise;

      let parentChromeFlags = getParentChromeFlags(win);
      Assert.ok(
        parentChromeFlags & Ci.nsIWebBrowserChrome.CHROME_SCROLLBARS,
        "Should have scrollbars when not disabled explicitly"
      );

      let contentChromeFlags = await getContentChromeFlags(win);
      Assert.ok(
        contentChromeFlags & Ci.nsIWebBrowserChrome.CHROME_SCROLLBARS,
        "Should have scrollbars when not disabled explicitly"
      );

      await BrowserTestUtils.closeWindow(win);
    }
  );
});