Skip to content

Commit

Permalink
Merge pull request #380 from initprism/latest_chrome_revision
Browse files Browse the repository at this point in the history
Added installation latest version of chrome and refactored fetcher
  • Loading branch information
mdrokz committed Feb 22, 2023
2 parents 3aa701f + bbbc98a commit 284754b
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 16 deletions.
85 changes: 69 additions & 16 deletions src/browser/fetcher.rs
Expand Up @@ -32,12 +32,18 @@ const PLATFORM: &str = "mac";
#[cfg(windows)]
const PLATFORM: &str = "win";

#[derive(Clone, Debug)]
pub enum Revision {
Specific(String),
Latest,
}

#[derive(Clone, Debug)]
pub struct FetcherOptions {
/// The desired chrome revision.
///
/// defaults to CUR_REV
revision: String,
revision: Revision,

/// The prefered installation directory. If not None we will look here first
/// for existing installs.
Expand All @@ -59,7 +65,7 @@ pub struct FetcherOptions {
impl Default for FetcherOptions {
fn default() -> Self {
Self {
revision: CUR_REV.into(),
revision: Revision::Specific(CUR_REV.into()),
install_dir: None,
allow_download: true,
allow_standard_dirs: true,
Expand All @@ -68,8 +74,8 @@ impl Default for FetcherOptions {
}

impl FetcherOptions {
pub fn with_revision<S: Into<String>>(mut self, revision: S) -> Self {
self.revision = revision.into();
pub fn with_revision(mut self, revision: Revision) -> Self {
self.revision = revision;
self
}

Expand Down Expand Up @@ -104,26 +110,31 @@ impl Fetcher {

// look for good existing installation, if none exists then download and install
pub fn fetch(&self) -> Result<PathBuf> {
if let Ok(chrome_path) = self.chrome_path() {
let rev = match self.options.revision {
Revision::Specific(ref v) => v.to_string(),
Revision::Latest => latest_revision()?,
};

if let Ok(chrome_path) = self.chrome_path(&rev) {
// we found it!
return Ok(chrome_path);
}

if self.options.allow_download {
let zip_path = self.download()?;
let zip_path = self.download(&rev)?;

self.unzip(zip_path)?;

// look again
return self.chrome_path();
return self.chrome_path(&rev);
}

// couldn't find and not allowed to download
Err(anyhow!("Could not fetch"))
}

// Look for an installation directory matching self.options.revision
fn base_path(&self) -> Result<PathBuf> {
fn base_path(&self, revision: &str) -> Result<PathBuf> {
// we want to look in install_dir first, then data dir
let mut search_dirs: Vec<&Path> = Vec::new();
let project_dirs = get_project_dirs()?;
Expand All @@ -146,7 +157,7 @@ impl Fetcher {

if filename_parts.len() == 2
&& filename_parts[0] == PLATFORM
&& filename_parts[1] == self.options.revision
&& filename_parts[1] == revision
{
return Ok(entry.path().into());
}
Expand All @@ -157,9 +168,9 @@ impl Fetcher {
}

// find full path to chrome executable from base_path
fn chrome_path(&self) -> Result<PathBuf> {
let mut path = self.base_path()?;
path.push(archive_name(&self.options.revision)?);
fn chrome_path(&self, revision: &str) -> Result<PathBuf> {
let mut path = self.base_path(revision)?;
path.push(archive_name(revision)?);

#[cfg(target_os = "linux")]
{
Expand All @@ -181,19 +192,19 @@ impl Fetcher {
}

// download a .zip of the revision we want
fn download(&self) -> Result<PathBuf> {
let url = dl_url(&self.options.revision)?;
fn download(&self, revision: &str) -> Result<PathBuf> {
let url = dl_url(revision)?;
info!("Chrome download url: {}", url);
let total = get_size(&url)?;
info!("Total size of download: {} MiB", total);

let mut path: PathBuf = if let Some(mut dir) = self.options.install_dir.clone() {
// we have a preferred install location
dir.push(format!("{}-{}", PLATFORM, self.options.revision));
dir.push(format!("{}-{}", PLATFORM, revision));
dir
} else if self.options.allow_standard_dirs {
let mut dir = get_project_dirs()?.data_dir().to_path_buf();
dir.push(format!("{}-{}", PLATFORM, self.options.revision));
dir.push(format!("{}-{}", PLATFORM, revision));
dir
} else {
// No preferred install dir and not allowed to use standard dirs.
Expand Down Expand Up @@ -404,3 +415,45 @@ fn archive_name<R: AsRef<str>>(revision: R) -> Result<&'static str> {
}
}
}

// Returns the latest chrome revision for the current platform.
// This function will panic on unsupported platforms.
fn latest_revision() -> Result<String> {
let mut url = format!("{}/chromium-browser-snapshots", DEFAULT_HOST);

#[cfg(target_os = "linux")]
{
url = format!("{}/Linux_x64/LAST_CHANGE", url);
ureq::get(&url)
.call()?
.into_string()
.map_err(anyhow::Error::from)
}

#[cfg(all(target_os = "macos", target_arch = "aarch64"))]
{
url = format!("{}/Mac_Arm/LAST_CHANGE", url);
ureq::get(&url)
.call()?
.into_string()
.map_err(anyhow::Error::from)
}

#[cfg(all(target_os = "macos", not(target_arch = "aarch64")))]
{
url = format!("{}/Mac/LAST_CHANGE", url);
ureq::get(&url)
.call()?
.into_string()
.map_err(anyhow::Error::from)
}

#[cfg(windows)]
{
url = format!("{}/Win_x64/LAST_CHANGE", url);
ureq::get(&url)
.call()?
.into_string()
.map_err(anyhow::Error::from)
}
}
3 changes: 3 additions & 0 deletions src/browser/mod.rs
Expand Up @@ -26,6 +26,9 @@ pub use B::GetVersionReturnObject;
#[cfg(feature = "fetch")]
pub use fetcher::FetcherOptions;

#[cfg(feature = "fetch")]
pub use fetcher::Revision;

pub mod context;
#[cfg(feature = "fetch")]
mod fetcher;
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Expand Up @@ -94,6 +94,9 @@ pub use browser::{
#[cfg(feature = "fetch")]
pub use browser::FetcherOptions;

#[cfg(feature = "fetch")]
pub use browser::Revision;

pub mod browser;
pub mod protocol;
pub mod types;
Expand Down

0 comments on commit 284754b

Please sign in to comment.