summaryrefslogtreecommitdiffstats
path: root/toolkit/actors/PrintingParent.jsm
blob: 00baca696c447680c88096387bd3a94924e3d04a (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
/* vim: set ts=2 sw=2 sts=2 et tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

var EXPORTED_SYMBOLS = ["PrintingParent"];

let gTestListener = null;

class PrintingParent extends JSWindowActorParent {
  static setTestListener(listener) {
    gTestListener = listener;
  }

  getPrintPreviewToolbar(browser) {
    return browser.ownerDocument.getElementById("print-preview-toolbar");
  }

  receiveMessage(message) {
    let browser = this.browsingContext.top.embedderElement;
    let PrintUtils = browser.ownerGlobal.PrintUtils;

    if (message.name == "Printing:Error") {
      PrintUtils._displayPrintingError(
        message.data.nsresult,
        message.data.isPrinting
      );
      return undefined;
    }

    if (this.ignoreListeners) {
      return undefined;
    }

    let listener = PrintUtils._webProgressPP?.value;
    let data = message.data;

    switch (message.name) {
      case "Printing:Preview:CurrentPage": {
        browser.setAttribute("current-page", message.data.currentPage);
        break;
      }

      case "Printing:Preview:Entered": {
        // This message is sent by the content process once it has completed
        // putting the content into print preview mode. We must wait for that to
        // to complete before switching the chrome UI to print preview mode,
        // otherwise we have layout issues.

        if (gTestListener) {
          gTestListener(browser);
        }

        PrintUtils.printPreviewEntered(browser, message.data);
        break;
      }

      case "Printing:Preview:ReaderModeReady": {
        PrintUtils.readerModeReady(browser);
        break;
      }

      case "Printing:Preview:UpdatePageCount": {
        let toolbar = this.getPrintPreviewToolbar(browser);
        toolbar.updatePageCount(message.data.totalPages);
        break;
      }

      case "Printing:Preview:ProgressChange": {
        if (!PrintUtils._webProgressPP.value) {
          // We somehow didn't get a nsIWebProgressListener to be updated...
          // I guess there's nothing to do.
          return undefined;
        }

        return listener.onProgressChange(
          null,
          null,
          data.curSelfProgress,
          data.maxSelfProgress,
          data.curTotalProgress,
          data.maxTotalProgress
        );
      }

      case "Printing:Preview:StateChange": {
        if (!PrintUtils._webProgressPP.value) {
          // We somehow didn't get a nsIWebProgressListener to be updated...
          // I guess there's nothing to do.
          return undefined;
        }

        if (data.stateFlags & Ci.nsIWebProgressListener.STATE_STOP) {
          // Strangely, the printing engine sends 2 STATE_STOP messages when
          // print preview is finishing. One has the STATE_IS_DOCUMENT flag,
          // the other has the STATE_IS_NETWORK flag. However, the webProgressPP
          // listener stops listening once the first STATE_STOP is sent.
          // Any subsequent messages result in NS_ERROR_FAILURE errors getting
          // thrown. This should all get torn out once bug 1088061 is fixed.

          // Enable toobar elements that we disabled during update.
          let printPreviewTB = this.getPrintPreviewToolbar(browser);
          printPreviewTB.disableUpdateTriggers(false);
        }

        return listener.onStateChange(null, null, data.stateFlags, data.status);
      }
    }

    return undefined;
  }
}