Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: chromium_process.js

Issue 29435631: Issue 5230 - Properly download Chromium on macOS (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Fix the issues from the review. Created May 16, 2017, 3:47 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | package.json » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2017 eyeo GmbH 3 * Copyright (C) 2006-2017 eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 /* eslint-env node */ 18 /* eslint-env node */
19 /* eslint no-console: "off" */ 19 /* eslint no-console: "off" */
20 20
21 "use strict"; 21 "use strict";
22 22
23 const childProcess = require("child_process"); 23 const childProcess = require("child_process");
24 const fs = require("fs"); 24 const fs = require("fs");
25 const https = require("https"); 25 const https = require("https");
26 const os = require("os"); 26 const os = require("os");
27 const path = require("path"); 27 const path = require("path");
28 28
29 const remoteInterface = require("chrome-remote-interface"); 29 const remoteInterface = require("chrome-remote-interface");
30 const unzip = require("unzip"); 30 const DecompressZip = require('decompress-zip');
31 31
32 const CHROMIUM_REVISION = 467222; 32 const CHROMIUM_REVISION = 467222;
33 33
34 function rmdir(dirPath) 34 function rmdir(dirPath)
35 { 35 {
36 for (let file of fs.readdirSync(dirPath)) 36 for (let file of fs.readdirSync(dirPath))
37 { 37 {
38 let filePath = path.join(dirPath, file); 38 let filePath = path.join(dirPath, file);
39 try 39 try
40 { 40 {
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 "linux": ["Linux_x64", "chrome-linux.zip"], 86 "linux": ["Linux_x64", "chrome-linux.zip"],
87 "darwin": ["Mac", "chrome-mac.zip"] 87 "darwin": ["Mac", "chrome-mac.zip"]
88 }; 88 };
89 89
90 if (!buildTypes.hasOwnProperty(platform)) 90 if (!buildTypes.hasOwnProperty(platform))
91 { 91 {
92 let err = new Error(`Cannot run browser tests, ${platform} is unsupported`); 92 let err = new Error(`Cannot run browser tests, ${platform} is unsupported`);
93 return Promise.reject(err); 93 return Promise.reject(err);
94 } 94 }
95 95
96 let chromiumDownloadCache = path.join(__dirname, "chromium-snapshots",
97 "download-cache");
96 let chromiumDir = path.join(__dirname, "chromium-snapshots", 98 let chromiumDir = path.join(__dirname, "chromium-snapshots",
97 `chromium-${platform}-${CHROMIUM_REVISION}`); 99 `chromium-${platform}-${CHROMIUM_REVISION}`);
98 if (fs.existsSync(chromiumDir)) 100 if (fs.existsSync(chromiumDir))
99 return Promise.resolve(getChromiumExecutable(chromiumDir)); 101 return Promise.resolve(getChromiumExecutable(chromiumDir));
100 102
101 if (!fs.existsSync(path.dirname(chromiumDir))) 103 if (!fs.existsSync(path.dirname(chromiumDir)))
102 fs.mkdirSync(path.dirname(chromiumDir)); 104 fs.mkdirSync(path.dirname(chromiumDir));
105
106 let [dir, fileName] = buildTypes[platform];
107 let archive = path.join(chromiumDownloadCache,
108 `${CHROMIUM_REVISION}-${fileName}`);
109 if (fs.existsSync(archive))
110 {
111 console.info(`Reusing cached archive ${archive}`);
112 return unzipArchive(archive, chromiumDir)
113 .then(dir => {
114 return Promise.resolve(getChromiumExecutable(dir));
115 });
Wladimir Palant 2017/05/17 10:37:02 This is equivalent to: .then(dir => getChromium
hub 2017/05/17 17:25:02 Done.
116 }
117
118 let url = `https://www.googleapis.com/download/storage/v1/b/chromium-browser-s napshots/o/${dir}%2F${CHROMIUM_REVISION}%2F${fileName}?alt=media`;
119
120 return downloadCacheAndExtract(url, chromiumDownloadCache, archive, chromiumDi r);
Wladimir Palant 2017/05/17 10:37:02 The point of using promises is making the logic ob
hub 2017/05/17 17:25:02 Acknowledged.
121 }
122
123 function downloadCacheAndExtract(url, downloadCache, destArchive, destDir)
124 {
103 return new Promise((resolve, reject) => 125 return new Promise((resolve, reject) =>
104 { 126 {
105 console.info("Downloading Chromium..."); 127 console.info("Downloading Chromium...");
106 let [dir, fileName] = buildTypes[platform]; 128
107 let url = `https://www.googleapis.com/download/storage/v1/b/chromium-browser -snapshots/o/${dir}%2F${CHROMIUM_REVISION}%2F${fileName}?alt=media`; 129 if (!fs.existsSync(downloadCache))
130 fs.mkdirSync(downloadCache);
Wladimir Palant 2017/05/17 10:37:02 You don't need the downloadCache parameter, just u
hub 2017/05/17 17:25:02 Acknowledged.
131 let writable = fs.createWriteStream(destArchive);
132
108 https.get(url, response => 133 https.get(url, response =>
109 { 134 {
110 if (response.statusCode != 200) 135 if (response.statusCode != 200)
111 { 136 {
112 reject(new Error(`Unexpected server response: ${response.statusCode}`)); 137 reject(new Error(`Unexpected server response: ${response.statusCode}`));
113 response.resume(); 138 response.resume();
114 return; 139 return;
115 } 140 }
116 141
117 response.pipe(unzip.Extract({path: chromiumDir})) 142 response.pipe(writable)
118 .on("error", reject) 143 .on("error", error => {
119 .on("close", () => resolve(getChromiumExecutable(chromiumDir))); 144 fs.unlinkSync(destArchive);
Wladimir Palant 2017/05/17 10:37:02 Will unlink work on Windows if the stream is still
hub 2017/05/17 17:25:02 From what I can read it will work and the file wil
Wladimir Palant 2017/05/18 11:18:27 Depends on the lock type I think. With an exclusiv
hub 2017/05/18 13:22:37 I'll add a "writeable.close()" first, just to make
145 reject(error);
146 })
147 .on("close", () => {
148 writable.close();
149 unzipArchive(destArchive, destDir)
150 .then(dir => {
151 resolve(getChromiumExecutable(dir));
152 })
153 .catch(reject);
154 });
120 }).on("error", reject); 155 }).on("error", reject);
121 }); 156 });
122 } 157 }
123 158
159 function unzipArchive(archive, destDir)
160 {
161 return new Promise((resolve, reject) =>
162 {
163 let unzipper = new DecompressZip(archive);
164 unzipper.on('extract', () => {
165 resolve(destDir);
166 });
167 unzipper.on('error', reject);
168 unzipper.extract({path: destDir});
169 });
170 }
171
124 function startChromium(chromiumPath) 172 function startChromium(chromiumPath)
125 { 173 {
126 fs.chmodSync(chromiumPath, fs.constants.S_IRWXU); 174 fs.chmodSync(chromiumPath, fs.constants.S_IRWXU);
127 175
128 let dataDir = fs.mkdtempSync(path.join(os.tmpdir(), "chromium-data")); 176 let dataDir = fs.mkdtempSync(path.join(os.tmpdir(), "chromium-data"));
129 let child = null; 177 let child = null;
130 return { 178 return {
131 kill: () => child && child.kill(), 179 kill: () => child && child.kill(),
132 done: new Promise((resolve, reject) => 180 done: new Promise((resolve, reject) =>
133 { 181 {
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 { 311 {
264 child.kill(); 312 child.kill();
265 return result; 313 return result;
266 }).catch(error => 314 }).catch(error =>
267 { 315 {
268 child.kill(); 316 child.kill();
269 throw error; 317 throw error;
270 }); 318 });
271 }); 319 });
272 }; 320 };
OLDNEW
« no previous file with comments | « no previous file | package.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld