1/* vim: set ts=2 sw=2 sts=2 et tw=80: */
2/* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5"use strict";
6
7const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
8
9var EXPORTED_SYMBOLS = ["AutoScrollParent"];
10
11class AutoScrollParent extends JSWindowActorParent {
12  receiveMessage(msg) {
13    let browser = this.manager.browsingContext.top.embedderElement;
14    if (!browser) {
15      return null;
16    }
17
18    // If another tab is activated, we shouldn't start autoscroll requested
19    // for the previous active window if and only if the browser is a remote
20    // browser.  This is required for web apps which don't prevent default of
21    // middle click after opening a new window.  If the active tab is our
22    // documents like about:*, we don't need this check since our documents
23    // should do it correctly.
24    const requestedInForegroundTab = browser.isRemoteBrowser
25      ? Services.focus.focusedElement == browser
26      : true;
27
28    let data = msg.data;
29    switch (msg.name) {
30      case "Autoscroll:Start":
31        // Don't start autoscroll if the tab has already been a background tab.
32        if (!requestedInForegroundTab) {
33          return Promise.resolve({ autoscrollEnabled: false, usingAPZ: false });
34        }
35        return Promise.resolve(browser.startScroll(data));
36      case "Autoscroll:MaybeStartInParent":
37        // Don't start autoscroll if the tab has already been a background tab.
38        if (!requestedInForegroundTab) {
39          return Promise.resolve({ autoscrollEnabled: false, usingAPZ: false });
40        }
41        let parent = this.browsingContext.parent;
42        if (parent) {
43          let actor = parent.currentWindowGlobal.getActor("AutoScroll");
44          actor.sendAsyncMessage("Autoscroll:MaybeStart", data);
45        }
46        break;
47      case "Autoscroll:Cancel":
48        browser.cancelScroll();
49        break;
50    }
51    return null;
52  }
53}
54