File: implementation.js

package info (click to toggle)
allow-html-temp 10.0.8-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 368 kB
  • sloc: javascript: 1,454; makefile: 25; sh: 7
file content (82 lines) | stat: -rw-r--r-- 2,722 bytes parent folder | download | duplicates (3)
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
/* 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/.
 *
 * This API is based on Jonathan Kamens Remote Content API in his
 * "Remote Content by Folder" Thunderbird extension provided at 
 * https://github.com/Extended-Thunder/remote-content-by-folder
*/

var { ExtensionParent } = ChromeUtils.importESModule(
  "resource://gre/modules/ExtensionParent.sys.mjs"
);
var { ExtensionSupport } = ChromeUtils.importESModule(
  "resource:///modules/ExtensionSupport.sys.mjs"
);

// From nsMsgContentPolicy.cpp
const kNoRemoteContentPolicy = 0;
const kBlockRemoteContent = 1;
const kAllowRemoteContent = 2;

const contentPolicyProperty = "remoteContentPolicy";
const policyMap = [
  {
    id: kNoRemoteContentPolicy,
    name: "None",
  },
  {
    id: kBlockRemoteContent,
    name: "Block",
  },
  {
    id: kAllowRemoteContent,
    name: "Allow",
  },
];

function getMessageWindow(nativeTab) {
  if (nativeTab instanceof Ci.nsIDOMWindow) {
    return nativeTab.messageBrowser.contentWindow;
  } else if (nativeTab.mode && nativeTab.mode.name == "mail3PaneTab") {
    return nativeTab.chromeBrowser.contentWindow.messageBrowser.contentWindow;
  } else if (nativeTab.mode && nativeTab.mode.name == "mailMessageTab") {
    return nativeTab.chromeBrowser.contentWindow;
  }
  return null;
}

var remoteContent = class extends ExtensionCommon.ExtensionAPI {
  getAPI(context) {
    return {
      remoteContent: {
        getContentPolicy: function (messageId) {
          let realMessage = context.extension.messageManager.get(messageId);
          let policyId = realMessage.getUint32Property(contentPolicyProperty);
          let policy = policyMap.find((e) => e.id == policyId);
          if (!policy) {
            throw new Error(`Unknown policy id ${policyId}`);
          }
          return policy.name;
        },

        setContentPolicy: async function (messageId, policyName) {
          let realMessage = context.extension.messageManager.get(messageId);
          let newPolicy = policyMap.find((e) => e.name == policyName);
          if (!newPolicy) {
            throw new Error(`Unknown policy name ${policyName}`);
          }

          let oldPolicyId = realMessage.getUint32Property(
            contentPolicyProperty,
          );
          if (newPolicy.id == oldPolicyId) {
            return;
          }
          realMessage.setUint32Property(contentPolicyProperty, newPolicy.id);
          // console.debug("AHT: setContentPolicy finished");
        },
      },
    };
  }
};