Bug 1586801 - Use the contextual WalkerFront in _duplicateNode(). r=pbro
[gecko.git] / toolkit / modules / GMPExtractorWorker.js
blobd4835da90d228c474cf2e11c3acbe29da6c3c540
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 /* eslint-env mozilla/chrome-worker */
7 "use strict";
9 importScripts("resource://gre/modules/osfile.jsm");
11 const FILE_ENTRY = "201: ";
13 onmessage = async function(msg) {
14   try {
15     let extractedPaths = [];
16     let jarPath = "jar:file://" + msg.data.zipPath + "!/";
17     let jarResponse = await fetch(jarPath);
18     let dirListing = await jarResponse.text();
19     let lines = dirListing.split("\n");
20     let reader = new FileReader();
21     for (let line of lines) {
22       if (!line.startsWith(FILE_ENTRY)) {
23         // Not a file entry, skip.
24         continue;
25       }
26       let lineSplits = line.split(" ");
27       let fileName = lineSplits[1];
28       // We don't need these types of files.
29       if (
30         fileName == "verified_contents.json" ||
31         fileName == "icon-128x128.png"
32       ) {
33         continue;
34       }
35       let filePath = jarPath + fileName;
36       let filePathResponse = await fetch(filePath);
37       let fileContents = await filePathResponse.blob();
38       let fileData = await new Promise(resolve => {
39         reader.onloadend = function() {
40           resolve(reader.result);
41         };
42         reader.readAsArrayBuffer(fileContents);
43       });
44       let profileDirPath = OS.Constants.Path.profileDir;
45       let installToDirPath = OS.Path.join(
46         profileDirPath,
47         msg.data.relativeInstallPath
48       );
49       await OS.File.makeDir(installToDirPath, {
50         ignoreExisting: true,
51         unixMode: 0o755,
52         from: profileDirPath,
53       });
54       // Do not extract into directories. Extract all files to the same
55       // directory.
56       let destPath = OS.Path.join(installToDirPath, fileName);
57       await OS.File.writeAtomic(destPath, new Uint8Array(fileData), {
58         tmpPath: destPath + ".tmp",
59       });
60       // Ensure files are writable and executable. Otherwise, we may be
61       // unable to execute or uninstall them.
62       await OS.File.setPermissions(destPath, { unixMode: 0o700 });
63       if (OS.Constants.Sys.Name == "Darwin") {
64         // If we're on MacOS Firefox will add the quarantine xattr to files it
65         // downloads. In this case we want to clear that xattr so we can load
66         // the CDM.
67         try {
68           await OS.File.macRemoveXAttr(destPath, "com.apple.quarantine");
69         } catch (e) {
70           // Failed to remove the attribute. This could be because the profile
71           // exists on a file system without xattr support.
72           //
73           // Don't fail the extraction here, as in this case it's likely we
74           // didn't set quarantine on these files in the first place.
75         }
76       }
77       extractedPaths.push(destPath);
78     }
79     postMessage({
80       result: "success",
81       extractedPaths,
82     });
83   } catch (e) {
84     postMessage({
85       result: "fail",
86       exception: e.message,
87     });
88   }