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: 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:
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 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 chromiumDir = path.join(__dirname, "chromium-snapshots",
97 `chromium-${platform}-${CHROMIUM_REVISION}`);
98 if (fs.existsSync(chromiumDir))
99 return Promise.resolve(getChromiumExecutable(chromiumDir));
100 96
101 if (!fs.existsSync(path.dirname(chromiumDir))) 97 return Promise.resolve().then(() =>
102 fs.mkdirSync(path.dirname(chromiumDir)); 98 {
99 let snapshotsDir = path.join(__dirname, "chromium-snapshots");
100 let chromiumDir = path.join(snapshotsDir,
101 `chromium-${platform}-${CHROMIUM_REVISION}`);
102 if (fs.existsSync(chromiumDir))
103 return chromiumDir;
104
105 if (!fs.existsSync(path.dirname(chromiumDir)))
106 fs.mkdirSync(path.dirname(chromiumDir));
107
108 let [dir, fileName] = buildTypes[platform];
109 let archive = path.join(snapshotsDir, "download-cache",
110 `${CHROMIUM_REVISION}-${fileName}`);
111
112 return Promise.resolve()
113 .then(() =>
114 {
115 if (!fs.existsSync(archive))
116 {
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 console.info("Downloading Chromium...");
119 return download(url, archive);
120 }
121 console.info(`Reusing cached archive ${archive}`);
122 })
123 .then(() => unzipArchive(archive, chromiumDir))
124 .then(() => chromiumDir);
125 }).then(dir => getChromiumExecutable(dir));
126 }
127
128 function download(url, destFile)
129 {
103 return new Promise((resolve, reject) => 130 return new Promise((resolve, reject) =>
104 { 131 {
105 console.info("Downloading Chromium..."); 132 let cacheDir = path.dirname(destFile);
106 let [dir, fileName] = buildTypes[platform]; 133 if (!fs.existsSync(cacheDir))
107 let url = `https://www.googleapis.com/download/storage/v1/b/chromium-browser -snapshots/o/${dir}%2F${CHROMIUM_REVISION}%2F${fileName}?alt=media`; 134 fs.mkdirSync(cacheDir);
135 let tempDest = destFile + "-" + process.pid;
136 let writable = fs.createWriteStream(tempDest);
137
108 https.get(url, response => 138 https.get(url, response =>
109 { 139 {
110 if (response.statusCode != 200) 140 if (response.statusCode != 200)
111 { 141 {
112 reject(new Error(`Unexpected server response: ${response.statusCode}`)); 142 reject(
143 new Error(`Unexpected server response: ${response.statusCode}`));
113 response.resume(); 144 response.resume();
114 return; 145 return;
115 } 146 }
116 147
117 response.pipe(unzip.Extract({path: chromiumDir})) 148 response.pipe(writable)
118 .on("error", reject) 149 .on("error", error =>
119 .on("close", () => resolve(getChromiumExecutable(chromiumDir))); 150 {
151 writable.close();
152 fs.unlinkSync(tempDest);
153 reject(error);
154 })
155 .on("close", () =>
156 {
157 writable.close();
158 fs.renameSync(tempDest, destFile);
159 resolve();
160 });
120 }).on("error", reject); 161 }).on("error", reject);
121 }); 162 });
122 } 163 }
123 164
165 function unzipArchive(archive, destDir)
166 {
167 return new Promise((resolve, reject) =>
168 {
169 extractZip(archive, {dir: destDir}, err =>
170 {
171 if (err)
172 reject(err);
173 else
174 resolve();
175 });
176 });
177 }
178
124 function startChromium(chromiumPath) 179 function startChromium(chromiumPath)
125 { 180 {
126 fs.chmodSync(chromiumPath, fs.constants.S_IRWXU); 181 fs.chmodSync(chromiumPath, fs.constants.S_IRWXU);
127 182
128 let dataDir = fs.mkdtempSync(path.join(os.tmpdir(), "chromium-data")); 183 let dataDir = fs.mkdtempSync(path.join(os.tmpdir(), "chromium-data"));
129 let child = null; 184 let child = null;
130 return { 185 return {
131 kill: () => child && child.kill(), 186 kill: () => child && child.kill(),
132 done: new Promise((resolve, reject) => 187 done: new Promise((resolve, reject) =>
133 { 188 {
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 { 318 {
264 child.kill(); 319 child.kill();
265 return result; 320 return result;
266 }).catch(error => 321 }).catch(error =>
267 { 322 {
268 child.kill(); 323 child.kill();
269 throw error; 324 throw error;
270 }); 325 });
271 }); 326 });
272 }; 327 };
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