summaryrefslogtreecommitdiffstats
path: root/toolkit/components/aboutthirdparty/tests/browser/head.js
blob: eba39480461242d035f53e62a3a26df6bebe6dd7 (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
/* 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";

const kClsidTestShellEx = "{10a9521e-0205-4cc7-93a1-62f30a9a54b3}";
const kFriendlyName = "Minimum Shell Extension for Firefox testing";
const kExtensionSubkeys = [".zzz\\shellex\\IconHandler"];
const kExtensionModuleName = "TestShellEx.dll";
const kUserBlockedModuleName = "TestDllBlocklist_UserBlocked.dll";
const kFileFilterInDialog = "*.zzz";
const kATP = Cc["@mozilla.org/about-thirdparty;1"].getService(
  Ci.nsIAboutThirdParty
);

function loadShellExtension() {
  // This method call opens the file dialog and shows the support file
  // "hello.zzz" in it, which loads TestShellEx.dll to show an icon
  // for files with the .zzz extension.
  kATP.openAndCloseFileDialogForTesting(
    kExtensionModuleName,
    getTestFilePath(""),
    kFileFilterInDialog
  );
}

async function registerObject() {
  const reg = Cc["@mozilla.org/windows-registry-key;1"].createInstance(
    Ci.nsIWindowsRegKey
  );

  reg.create(
    Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER,
    "Software\\Classes\\CLSID\\" + kClsidTestShellEx,
    Ci.nsIWindowsRegKey.ACCESS_ALL
  );

  reg.writeStringValue("", kFriendlyName);

  const inprocServer = reg.createChild(
    "InprocServer32",
    Ci.nsIWindowsRegKey.ACCESS_ALL
  );

  const moduleFullPath = getTestFilePath(kExtensionModuleName);
  Assert.ok(await IOUtils.exists(moduleFullPath), "The module file exists.");

  inprocServer.writeStringValue("", moduleFullPath);
  inprocServer.writeStringValue("ThreadingModel", "Apartment");
  reg.close();

  info("registerObject() done - " + moduleFullPath);
}

function registerExtensions() {
  for (const subkey of kExtensionSubkeys) {
    const reg = Cc["@mozilla.org/windows-registry-key;1"].createInstance(
      Ci.nsIWindowsRegKey
    );
    reg.create(
      Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER,
      "Software\\Classes\\" + subkey,
      Ci.nsIWindowsRegKey.ACCESS_ALL
    );

    let currentExtension = "";
    try {
      // If the key was just created above, the default value does not exist,
      // so readStringValue will throw NS_ERROR_FAILURE.
      currentExtension = reg.readStringValue("");
    } catch (e) {}

    try {
      if (!currentExtension) {
        reg.writeStringValue("", kClsidTestShellEx);
      } else if (currentExtension != kClsidTestShellEx) {
        throw new Error(
          `Another extension \`${currentExtension}\` has been registered.`
        );
      }
    } catch (e) {
      throw new Error("Failed to register TestShellEx.dll: " + e);
    } finally {
      reg.close();
    }
  }
}

function unregisterAll() {
  for (const subkey of kExtensionSubkeys) {
    const reg = Cc["@mozilla.org/windows-registry-key;1"].createInstance(
      Ci.nsIWindowsRegKey
    );

    try {
      reg.open(
        Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER,
        "Software\\Classes\\" + subkey,
        Ci.nsIWindowsRegKey.ACCESS_ALL
      );

      if (reg.readStringValue("") != kClsidTestShellEx) {
        // If another extension is registered, don't overwrite it.
        continue;
      }

      // Set an empty string instead of deleting the key
      // not to touch non-default values.
      reg.writeStringValue("", "");
    } catch (e) {
      info(`Failed to unregister \`${subkey}\`: ` + e);
    } finally {
      reg.close();
    }
  }

  const reg = Cc["@mozilla.org/windows-registry-key;1"].createInstance(
    Ci.nsIWindowsRegKey
  );
  reg.open(
    Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER,
    "Software\\Classes\\CLSID",
    Ci.nsIWindowsRegKey.ACCESS_ALL
  );

  try {
    const child = reg.openChild(
      kClsidTestShellEx,
      Ci.nsIWindowsRegKey.ACCESS_ALL
    );
    try {
      child.removeChild("InprocServer32");
    } catch (e) {
    } finally {
      child.close();
    }

    reg.removeChild(kClsidTestShellEx);
  } catch (e) {
  } finally {
    reg.close();
  }
}