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

Delta Between Two Patch Sets: chromium_process.js

Issue 29435631: Issue 5230 - Properly download Chromium on macOS (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Fix the issues from the review. Created May 16, 2017, 3:47 p.m.
Right Patch Set: Addressed review comments. Created May 18, 2017, 1:22 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | package.json » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 DecompressZip = require('decompress-zip'); 30 const extractZip = require("extract-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", 96
97 "download-cache"); 97 return Promise.resolve().then(() =>
98 let chromiumDir = path.join(__dirname, "chromium-snapshots", 98 {
99 `chromium-${platform}-${CHROMIUM_REVISION}`); 99 let snapshotsDir = path.join(__dirname, "chromium-snapshots");
100 if (fs.existsSync(chromiumDir)) 100 let chromiumDir = path.join(snapshotsDir,
101 return Promise.resolve(getChromiumExecutable(chromiumDir)); 101 `chromium-${platform}-${CHROMIUM_REVISION}`);
102 102 if (fs.existsSync(chromiumDir))
103 if (!fs.existsSync(path.dirname(chromiumDir))) 103 return chromiumDir;
104 fs.mkdirSync(path.dirname(chromiumDir)); 104
105 105 if (!fs.existsSync(path.dirname(chromiumDir)))
106 let [dir, fileName] = buildTypes[platform]; 106 fs.mkdirSync(path.dirname(chromiumDir));
107 let archive = path.join(chromiumDownloadCache, 107
108 `${CHROMIUM_REVISION}-${fileName}`); 108 let [dir, fileName] = buildTypes[platform];
109 if (fs.existsSync(archive)) 109 let archive = path.join(snapshotsDir, "download-cache",
110 { 110 `${CHROMIUM_REVISION}-${fileName}`);
111 console.info(`Reusing cached archive ${archive}`); 111
112 return unzipArchive(archive, chromiumDir) 112 return Promise.resolve()
113 .then(dir => { 113 .then(() =>
114 return Promise.resolve(getChromiumExecutable(dir)); 114 {
115 }); 115 if (!fs.existsSync(archive))
Wladimir Palant 2017/05/17 10:37:02 This is equivalent to: .then(dir => getChromium
hub 2017/05/17 17:25:02 Done.
116 } 116 {
117 117 let url = `https://www.googleapis.com/download/storage/v1/b/chromium-b rowser-snapshots/o/${dir}%2F${CHROMIUM_REVISION}%2F${fileName}?alt=media`;
118 let url = `https://www.googleapis.com/download/storage/v1/b/chromium-browser-s napshots/o/${dir}%2F${CHROMIUM_REVISION}%2F${fileName}?alt=media`; 118 console.info("Downloading Chromium...");
119 119 return download(url, archive);
120 return downloadCacheAndExtract(url, chromiumDownloadCache, archive, chromiumDi r); 120 }
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 } 121 console.info(`Reusing cached archive ${archive}`);
122 122 })
123 function downloadCacheAndExtract(url, downloadCache, destArchive, destDir) 123 .then(() => unzipArchive(archive, chromiumDir))
124 .then(() => chromiumDir);
125 }).then(dir => getChromiumExecutable(dir));
126 }
127
128 function download(url, destFile)
124 { 129 {
125 return new Promise((resolve, reject) => 130 return new Promise((resolve, reject) =>
126 { 131 {
127 console.info("Downloading Chromium..."); 132 let cacheDir = path.dirname(destFile);
128 133 if (!fs.existsSync(cacheDir))
129 if (!fs.existsSync(downloadCache)) 134 fs.mkdirSync(cacheDir);
130 fs.mkdirSync(downloadCache); 135 let tempDest = destFile + "-" + process.pid;
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); 136 let writable = fs.createWriteStream(tempDest);
132 137
133 https.get(url, response => 138 https.get(url, response =>
134 { 139 {
135 if (response.statusCode != 200) 140 if (response.statusCode != 200)
136 { 141 {
137 reject(new Error(`Unexpected server response: ${response.statusCode}`)); 142 reject(
143 new Error(`Unexpected server response: ${response.statusCode}`));
138 response.resume(); 144 response.resume();
139 return; 145 return;
140 } 146 }
141 147
142 response.pipe(writable) 148 response.pipe(writable)
143 .on("error", error => { 149 .on("error", error =>
144 fs.unlinkSync(destArchive); 150 {
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
151 writable.close();
152 fs.unlinkSync(tempDest);
145 reject(error); 153 reject(error);
146 }) 154 })
147 .on("close", () => { 155 .on("close", () =>
156 {
148 writable.close(); 157 writable.close();
149 unzipArchive(destArchive, destDir) 158 fs.renameSync(tempDest, destFile);
150 .then(dir => { 159 resolve();
151 resolve(getChromiumExecutable(dir));
152 })
153 .catch(reject);
154 }); 160 });
155 }).on("error", reject); 161 }).on("error", reject);
156 }); 162 });
157 } 163 }
158 164
159 function unzipArchive(archive, destDir) 165 function unzipArchive(archive, destDir)
160 { 166 {
161 return new Promise((resolve, reject) => 167 return new Promise((resolve, reject) =>
162 { 168 {
163 let unzipper = new DecompressZip(archive); 169 extractZip(archive, {dir: destDir}, err =>
164 unzipper.on('extract', () => { 170 {
165 resolve(destDir); 171 if (err)
172 reject(err);
173 else
174 resolve();
166 }); 175 });
167 unzipper.on('error', reject);
168 unzipper.extract({path: destDir});
169 }); 176 });
170 } 177 }
171 178
172 function startChromium(chromiumPath) 179 function startChromium(chromiumPath)
173 { 180 {
174 fs.chmodSync(chromiumPath, fs.constants.S_IRWXU); 181 fs.chmodSync(chromiumPath, fs.constants.S_IRWXU);
175 182
176 let dataDir = fs.mkdtempSync(path.join(os.tmpdir(), "chromium-data")); 183 let dataDir = fs.mkdtempSync(path.join(os.tmpdir(), "chromium-data"));
177 let child = null; 184 let child = null;
178 return { 185 return {
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
311 { 318 {
312 child.kill(); 319 child.kill();
313 return result; 320 return result;
314 }).catch(error => 321 }).catch(error =>
315 { 322 {
316 child.kill(); 323 child.kill();
317 throw error; 324 throw error;
318 }); 325 });
319 }); 326 });
320 }; 327 };
LEFTRIGHT
« no previous file | package.json » ('j') | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld