Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for a custom user data dir #91

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions R/Chrome.R
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,17 @@ perform_with_chrome <- function(
#' connect to headless Chromium/Chrome.
#' * `max_attempts`: Integer scalar, number of tries to connect to headless
#' Chromium/Chrome.
#' * `user_data_dir`: Character, for advanced user, it allows to set a path to a
#' custom user data dir. If provided, this folder will be used instead of a
#' new and empty one created by crrri. If it does not exist, this folder will
#' be created but not removed afterwards. It must be done manually.
#' [About user data directory in Chrome](https://chromium.googlesource.com/chromium/src/+/master/docs/user_data_dir.md)
#' * `callback`: Function with one argument.
#' * `async`: Does the function return a promise?
#'
#' @section Details:
#' `$new()` opens a new headless Chromium/Chrome. You can deactivate verbose
#' from chrome process launching byt setting option `crrri.verbose` to FALSE.
#' from chrome process launching by setting option `crrri.verbose` to FALSE.
#'
#' `$connect(callback = NULL)` connects the R session to the remote instance of
#' headless Chromium/Chrome. The returned value depends on the value of the
Expand Down Expand Up @@ -272,7 +277,8 @@ Chrome <- R6::R6Class(
public = list(
initialize = function(
bin = Sys.getenv("HEADLESS_CHROME"), debug_port = 9222L, local = FALSE,
extra_args = NULL, headless = TRUE, retry_delay = 0.2, max_attempts = 15L
extra_args = NULL, headless = TRUE, retry_delay = 0.2, max_attempts = 15L,
user_data_dir = NULL
) {
assert_that(is_scalar_character(bin))
assert_that(
Expand All @@ -286,7 +292,14 @@ Chrome <- R6::R6Class(
assert_that(is_scalar_integerish(max_attempts))

private$.bin <- bin
work_dir <- chr_new_data_dir()
work_dir <- if (is.null(user_data_dir)) {
private$.remove_work_dir <- TRUE
chr_new_data_dir()
} else {
assert_that(is_scalar_character(user_data_dir))
private$.remove_work_dir <- FALSE
normalizePath(user_data_dir)
}
chr_process <- chr_launch(bin, debug_port, extra_args, headless, work_dir)
private$.work_dir <- work_dir
private$.process <- chr_process
Expand Down Expand Up @@ -331,6 +344,7 @@ Chrome <- R6::R6Class(
private = list(
.bin = NULL,
.work_dir = NULL,
.remove_work_dir = NULL,
.process = NULL,
.async_finalizer = function() {
clients_disconnected <- timeout(
Expand Down Expand Up @@ -362,7 +376,17 @@ Chrome <- R6::R6Class(
}
private$.process$wait()
}
chr_clean_work_dir(private$.work_dir)
if (private$.remove_work_dir) {
chr_clean_work_dir(private$.work_dir)
} else {
if (getOption("crrri.verbose", TRUE)) {
message(paste0(
"\nCustom user_data_dir has been provided and not deleted by crrri.",
" Delete manually if needed:\n",
private$.work_dir
))
}
}
}
)
killed_and_cleaned
Expand Down
7 changes: 6 additions & 1 deletion man/Chrome.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions tests/testthat/helper-chrome.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,10 @@ setup_chrome_test <- function(env = rlang::caller_env()) {
# we need this because these function are normally called in the test file directly
env = env)
}

without_verbose <- function(code) {
old <- getOption("crrri.verbose", TRUE)
options(crrri.verbose = FALSE)
on.exit(options(crrri.verbose = old))
force(code)
}