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
7var EXPORTED_SYMBOLS = ["ThumbnailsChild"];
8const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
9
10ChromeUtils.defineModuleGetter(
11  this,
12  "PageThumbUtils",
13  "resource://gre/modules/PageThumbUtils.jsm"
14);
15
16class ThumbnailsChild extends JSWindowActorChild {
17  receiveMessage(message) {
18    switch (message.name) {
19      case "Browser:Thumbnail:ContentInfo": {
20        let [width, height] = PageThumbUtils.getContentSize(this.contentWindow);
21        return { width, height };
22      }
23      case "Browser:Thumbnail:CheckState": {
24        /**
25         * Remote isSafeForCapture request handler for PageThumbs.
26         */
27        return new Promise(resolve =>
28          Services.tm.idleDispatchToMainThread(() => {
29            if (!this.manager) {
30              // If we have no manager, our actor has been destroyed, which
31              // means we can't respond, and trying to touch
32              // `this.contentWindow` or `this.browsingContext` will throw.
33              // The `sendQuery` call in the parent will already have been
34              // rejected when the actor was destroyed, so there's no need to
35              // reject our promise or log an additional error.
36              return;
37            }
38
39            let result = PageThumbUtils.shouldStoreContentThumbnail(
40              this.contentWindow,
41              this.browsingContext.docShell
42            );
43            resolve(result);
44          })
45        );
46      }
47      case "Browser:Thumbnail:GetOriginalURL": {
48        /**
49         * Remote GetOriginalURL request handler for PageThumbs.
50         */
51        let channel = this.browsingContext.docShell.currentDocumentChannel;
52        let channelError = PageThumbUtils.isChannelErrorResponse(channel);
53        let originalURL;
54        try {
55          originalURL = channel.originalURI.spec;
56        } catch (ex) {}
57        return { channelError, originalURL };
58      }
59    }
60    return undefined;
61  }
62}
63