| LEFT | RIGHT | 
|    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-present eyeo GmbH |    3  * Copyright (C) 2006-present 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 */ |  | 
|   19 /* eslint no-console: "off" */ |  | 
|   20  |  | 
|   21 "use strict"; |   18 "use strict"; | 
|   22  |   19  | 
|   23 const fs = require("fs"); |   20 const fs = require("fs"); | 
|   24 const https = require("https"); |  | 
|   25 const path = require("path"); |   21 const path = require("path"); | 
|   26  |   22  | 
|   27 const extractZip = require("extract-zip"); |   23 const {download, unzipArchive} = require("./download"); | 
|   28  |   24  | 
|   29 function getChromiumExecutable(chromiumDir) |   25 function getChromiumExecutable(chromiumDir) | 
|   30 { |   26 { | 
|   31   switch (process.platform) |   27   switch (process.platform) | 
|   32   { |   28   { | 
|   33     case "win32": |   29     case "win32": | 
|   34       return path.join(chromiumDir, "chrome-win32", "chrome.exe"); |   30       return path.join(chromiumDir, "chrome-win32", "chrome.exe"); | 
|   35     case "linux": |   31     case "linux": | 
|   36       return path.join(chromiumDir, "chrome-linux", "chrome"); |   32       return path.join(chromiumDir, "chrome-linux", "chrome"); | 
|   37     case "darwin": |   33     case "darwin": | 
| (...skipping 15 matching lines...) Expand all  Loading... | 
|   53     "linux": ["Linux_x64", "chrome-linux.zip"], |   49     "linux": ["Linux_x64", "chrome-linux.zip"], | 
|   54     "darwin": ["Mac", "chrome-mac.zip"] |   50     "darwin": ["Mac", "chrome-mac.zip"] | 
|   55   }; |   51   }; | 
|   56  |   52  | 
|   57   if (!buildTypes.hasOwnProperty(platform)) |   53   if (!buildTypes.hasOwnProperty(platform)) | 
|   58   { |   54   { | 
|   59     let err = new Error(`Cannot run browser tests, ${platform} is unsupported`); |   55     let err = new Error(`Cannot run browser tests, ${platform} is unsupported`); | 
|   60     return Promise.reject(err); |   56     return Promise.reject(err); | 
|   61   } |   57   } | 
|   62  |   58  | 
|   63  |  | 
|   64   return Promise.resolve().then(() => |   59   return Promise.resolve().then(() => | 
|   65   { |   60   { | 
|   66     let snapshotsDir = path.join(__dirname, "..", "..", "chromium-snapshots"); |   61     let snapshotsDir = path.join(__dirname, "..", "..", "chromium-snapshots"); | 
|   67     let chromiumDir = path.join(snapshotsDir, |   62     let chromiumDir = path.join(snapshotsDir, | 
|   68                                 `chromium-${platform}-${chromiumRevision}`); |   63                                 `chromium-${platform}-${chromiumRevision}`); | 
|   69     if (fs.existsSync(chromiumDir)) |   64     if (fs.existsSync(chromiumDir)) | 
|   70       return chromiumDir; |   65       return chromiumDir; | 
|   71  |   66  | 
|   72     if (!fs.existsSync(path.dirname(chromiumDir))) |   67     if (!fs.existsSync(path.dirname(chromiumDir))) | 
|   73       fs.mkdirSync(path.dirname(chromiumDir)); |   68       fs.mkdirSync(path.dirname(chromiumDir)); | 
|   74  |   69  | 
|   75     let [dir, fileName] = buildTypes[platform]; |   70     let [dir, fileName] = buildTypes[platform]; | 
|   76     let archive = path.join(snapshotsDir, "download-cache", |   71     let archive = path.join(snapshotsDir, "download-cache", | 
|   77                             `${chromiumRevision}-${fileName}`); |   72                             `${chromiumRevision}-${fileName}`); | 
|   78  |   73  | 
|   79     return Promise.resolve() |   74     return Promise.resolve() | 
|   80       .then(() => |   75       .then(() => | 
|   81       { |   76       { | 
|   82         if (!fs.existsSync(archive)) |   77         if (!fs.existsSync(archive)) | 
|   83         { |   78         { | 
|   84           let url = `https://www.googleapis.com/download/storage/v1/b/chromium-b
     rowser-snapshots/o/${dir}%2F${chromiumRevision}%2F${fileName}?alt=media`; |  | 
|   85           console.info("Downloading Chromium..."); |   79           console.info("Downloading Chromium..."); | 
|   86           return download(url, archive); |   80           return download( | 
 |   81             `https://www.googleapis.com/download/storage/v1/b/chromium-browser-s
     napshots/o/${dir}%2F${chromiumRevision}%2F${fileName}?alt=media`, | 
 |   82             archive); | 
|   87         } |   83         } | 
|   88         console.info(`Reusing cached archive ${archive}`); |   84         console.info(`Reusing cached archive ${archive}`); | 
|   89       }) |   85       }) | 
|   90       .then(() => unzipArchive(archive, chromiumDir)) |   86       .then(() => unzipArchive(archive, chromiumDir)) | 
|   91       .then(() => chromiumDir); |   87       .then(() => chromiumDir); | 
|   92   }).then(dir => getChromiumExecutable(dir)); |   88   }).then(dir => getChromiumExecutable(dir)); | 
|   93 } |   89 } | 
|   94  |   90  | 
|   95 function download(url, destFile) |  | 
|   96 { |  | 
|   97   return new Promise((resolve, reject) => |  | 
|   98   { |  | 
|   99     let cacheDir = path.dirname(destFile); |  | 
|  100     if (!fs.existsSync(cacheDir)) |  | 
|  101       fs.mkdirSync(cacheDir); |  | 
|  102     let tempDest = destFile + "-" + process.pid; |  | 
|  103     let writable = fs.createWriteStream(tempDest); |  | 
|  104  |  | 
|  105     https.get(url, response => |  | 
|  106     { |  | 
|  107       if (response.statusCode != 200) |  | 
|  108       { |  | 
|  109         reject( |  | 
|  110           new Error(`Unexpected server response: ${response.statusCode}`)); |  | 
|  111         response.resume(); |  | 
|  112         return; |  | 
|  113       } |  | 
|  114  |  | 
|  115       response.pipe(writable) |  | 
|  116               .on("error", error => |  | 
|  117               { |  | 
|  118                 writable.close(); |  | 
|  119                 fs.unlinkSync(tempDest); |  | 
|  120                 reject(error); |  | 
|  121               }) |  | 
|  122               .on("close", () => |  | 
|  123               { |  | 
|  124                 writable.close(); |  | 
|  125                 fs.renameSync(tempDest, destFile); |  | 
|  126                 resolve(); |  | 
|  127               }); |  | 
|  128     }).on("error", reject); |  | 
|  129   }); |  | 
|  130 } |  | 
|  131  |  | 
|  132 function unzipArchive(archive, destDir) |  | 
|  133 { |  | 
|  134   return new Promise((resolve, reject) => |  | 
|  135   { |  | 
|  136     extractZip(archive, {dir: destDir}, err => |  | 
|  137     { |  | 
|  138       if (err) |  | 
|  139         reject(err); |  | 
|  140       else |  | 
|  141         resolve(); |  | 
|  142     }); |  | 
|  143   }); |  | 
|  144 } |  | 
|  145  |  | 
|  146 module.exports.ensureChromium = ensureChromium; |   91 module.exports.ensureChromium = ensureChromium; | 
| LEFT | RIGHT |