summaryrefslogtreecommitdiffstats
path: root/embedding/components/windowwatcher/test/browser_new_content_window_chromeflags.js
blob: 887f86152b7a60fda56df487804d612c7d60f99c (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
/**
 * Tests that chromeFlags are set properly on windows that are
 * being opened from content.
 */

// The following features set chrome flags on new windows and are
// supported by web content. The schema for each property on this
// object is as follows:
//
// <feature string>: {
//   flag: <associated nsIWebBrowserChrome flag>,
//   defaults_to: <what this feature defaults to normally>
// }
const ALLOWED = {
  "toolbar": {
    flag: Ci.nsIWebBrowserChrome.CHROME_TOOLBAR,
    defaults_to: true,
  },
  "personalbar": {
    flag: Ci.nsIWebBrowserChrome.CHROME_PERSONAL_TOOLBAR,
    defaults_to: true,
  },
  "menubar": {
    flag: Ci.nsIWebBrowserChrome.CHROME_MENUBAR,
    defaults_to: true,
  },
  "scrollbars": {
    flag: Ci.nsIWebBrowserChrome.CHROME_SCROLLBARS,
    defaults_to: false,
  },
  "minimizable": {
    flag: Ci.nsIWebBrowserChrome.CHROME_WINDOW_MIN,
    defaults_to: true,
  },
};

// Construct a features string that flips all ALLOWED features
// to not be their defaults.
const ALLOWED_STRING = Object.keys(ALLOWED).map(feature => {
  let toValue = ALLOWED[feature].defaults_to ? "no" : "yes";
  return `${feature}=${toValue}`;
}).join(",");

// The following are not allowed from web content, at least
// in the default case (since some are disabled by default
// via the dom.disable_window_open_feature pref branch).
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,
  },
  "macsuppressanimation": {
    flag: Ci.nsIWebBrowserChrome.CHROME_MAC_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,
  },
};

// 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 = [ALLOWED_STRING, 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>`;

// 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, return the nsIWebBrowserChrome chrome flags for
 * the associated XUL window.
 *
 * @param win (nsIDOMWindow)
 *        Some window in the parent process.
 * @returns int
 */
function getParentChromeFlags(win) {
  return win.QueryInterface(Ci.nsIInterfaceRequestor)
            .getInterface(Ci.nsIWebNavigation)
            .QueryInterface(Ci.nsIDocShellTreeItem)
            .treeOwner
            .QueryInterface(Ci.nsIInterfaceRequestor)
            .getInterface(Ci.nsIXULWindow)
            .chromeFlags;
}

/**
 * For some chromeFlags, ensures that flags that are in the
 * ALLOWED group were modified, and that flags in the DISALLOWED
 * group were not modified.
 *
 * @param chromeFlags (int)
 *        Some chromeFlags to check.
 */
function assertContentFlags(chromeFlags) {
  for (let feature in ALLOWED) {
    let flag = ALLOWED[feature].flag;

    if (ALLOWED[feature].defaults_to) {
      // The feature is supposed to default to true, so we should
      // have been able to flip it off.
      Assert.ok(!(chromeFlags & flag),
                `Expected feature ${feature} to be disabled`);
    } else {
      // The feature is supposed to default to false, so we should
      // have been able to flip it on.
      Assert.ok((chromeFlags & flag),
                `Expected feature ${feature} to be enabled`);
    }
  }

  for (let feature in DISALLOWED) {
    let flag = DISALLOWED[feature].flag;
    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 ALLOWED and DISALLOWED. The computed
 * feature string attempts to flip every feature away from their
 * default.
 */
add_task(function* test_new_remote_window_flags() {
  let newWinPromise = BrowserTestUtils.waitForNewWindow();

  yield BrowserTestUtils.withNewTab({
    gBrowser,
    url: SCRIPT_PAGE,
  }, function*(browser) {
    let win = yield 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 TabChild, so we have to check those too.
    let b = win.gBrowser.selectedBrowser;
    let contentChromeFlags = yield ContentTask.spawn(b, null, function*() {
      docShell.QueryInterface(Ci.nsIInterfaceRequestor);
      try {
        // This will throw if we're not a remote browser.
        return docShell.getInterface(Ci.nsITabChild)
                       .QueryInterface(Ci.nsIWebBrowserChrome)
                       .chromeFlags;
      } catch(e) {
        // This must be a non-remote browser...
        return docShell.QueryInterface(Ci.nsIDocShellTreeItem)
                       .treeOwner
                       .QueryInterface(Ci.nsIWebBrowserChrome)
                       .chromeFlags;
      }
    });

    assertContentFlags(contentChromeFlags);
    Assert.ok(!(contentChromeFlags &
                Ci.nsIWebBrowserChrome.CHROME_REMOTE_WINDOW),
              "Should not be remote in the content process.");

    yield 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();

  yield BrowserTestUtils.withNewTab({
    gBrowser,
    url: SCRIPT_PAGE_FOR_CHROME_ALL,
  }, function*(browser) {
    let win = yield 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");
    yield BrowserTestUtils.closeWindow(win);
  });
});