Class: Bidi2pdf::Bidi::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/bidi2pdf/bidi/session.rb

Constant Summary collapse

SUBSCRIBE_EVENTS =

Events to subscribe to during the session.

%w[script].freeze
DEFAULT_CHROME_ARGS =

Default Chrome arguments for the session.

[
  "--allow-pre-commit-input", # Allow pre-commit input for form fields
  "--disable-dev-shm-usage", # Disable /dev/shm usage; use /tmp instead
  "--disable-gpu", # Disable GPU hardware acceleration; force software rendering
  "--disable-popup-blocking", # Allow all pop-ups; bypass built-in popup blocker
  "--disable-hang-monitor", # Disable “Page Unresponsive” / “Aw, Snap!” dialogs on hangs
  "--disable-background-networking", # Turn off speculative/periodic network requests (DNS prefetch, Safe Browsing updates, etc.)
  "--disable-background-timer-throttling", # Prevent JS timers from being throttled in background tabs
  "--disable-client-side-phishing-detection", # Disable built-in phishing checks; rely only on server-side detection
  "--disable-component-extensions-with-background-pages", # Block component extensions that run persistent background pages (PDF viewer, Translate, etc.)
  "--disable-crash-reporter", # Disable crash-report uploads and UI
  "--disable-default-apps", # Stop installation of Chrome’s default apps on a fresh profile
  "--disable-infobars", # Suppress “Chrome is being controlled by automated test software” infobar (and similar)
  "--disable-ipc-flooding-protection", # Turn off defenses against too-many IPC messages from renderers
  "--disable-prompt-on-repost", # Skip “Confirm Form Resubmission” dialogs on page reloads after POST
  "--disable-renderer-backgrounding", # Keep background tab renderers at full priority
  "--disable-search-engine-choice-screen", # Skip first-run search engine selection UI
  "--disable-sync", # Turn off all Google account sync (bookmarks, passwords, etc.)
  "--enable-automation", # Expose WebDriver hooks (navigator.webdriver) for automation frameworks
  "--export-tagged-pdf", # When printing to PDF, include tagged structure for accessibility
  "--force-color-profile=srgb", # Force rendering to use the sRGB color profile
  "--generate-pdf-document-outline", # Auto-generate PDF bookmarks/outlines from HTML headings, not supported by chrome/chromium https://issues.chromium.org/issues/41387522#comment48
  "--metrics-recording-only", # Collect UMA metrics locally but never upload them
  "--no-first-run", # Skip the “Welcome” or “What’s New” screens on fresh profiles
  "--password-store=basic", # Use Chrome’s basic (in-profile) password storage vs. OS vault
  "--use-mock-keychain", # On macOS, use a fake keychain for testing (don’t touch the real one)
  "--disable-backgrounding-occluded-windows", # Prevent fully-occluded windows from being treated as background
  "--disable-breakpad", # Disable the Breakpad crash-reporting library entirely
  "--enable-features=PdfOopif", # Enable out-of-process iframe (OOPIF) architecture for PDF rendering
  "--disable-features=Translate,AcceptCHFrame,MediaRouter,OptimizationHints,ProcessPerSiteUpToMainFrameThreshold,IsolateSandboxedIframes",
  "--disable-extensions about:blank"
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session_url:, headless: true, chrome_args: DEFAULT_CHROME_ARGS) ⇒ Session

Initializes a new session.

Parameters:

  • session_url (String)

    The URL for the session.

  • headless (Boolean) (defaults to: true)

    Whether to run the browser in headless mode. Defaults to true.

  • chrome_args (Array<String>) (defaults to: DEFAULT_CHROME_ARGS)

    Additional Chrome arguments. Defaults to predefined arguments.



82
83
84
85
86
87
# File 'lib/bidi2pdf/bidi/session.rb', line 82

def initialize(session_url:, headless: true, chrome_args: DEFAULT_CHROME_ARGS)
  @session_uri = URI(session_url)
  @headless = headless
  @started = false
  @chrome_args = chrome_args.dup
end

Instance Attribute Details

#chrome_argsArray<String> (readonly)

Returns The Chrome arguments for the session.

Returns:

  • (Array<String>)

    The Chrome arguments for the session.



75
76
77
# File 'lib/bidi2pdf/bidi/session.rb', line 75

def chrome_args
  @chrome_args
end

#session_uriURI (readonly)

Returns The URI of the session.

Returns:

  • (URI)

    The URI of the session.



69
70
71
# File 'lib/bidi2pdf/bidi/session.rb', line 69

def session_uri
  @session_uri
end

#startedBoolean (readonly)

Returns Whether the session has started.

Returns:

  • (Boolean)

    Whether the session has started.



72
73
74
# File 'lib/bidi2pdf/bidi/session.rb', line 72

def started
  @started
end

Instance Method Details

#browserBidi2pdf::Bidi::Browser

Returns the browser instance for the session.

Returns:



112
113
114
# File 'lib/bidi2pdf/bidi/session.rb', line 112

def browser
  @browser ||= create_browser
end

#clientBidi2pdf::Bidi::Client?

Returns the WebSocket client for the session.

Returns:



105
106
107
# File 'lib/bidi2pdf/bidi/session.rb', line 105

def client
  @client ||= started? ? create_client : nil
end

#closeObject

Closes the session and cleans up resources. rubocop:disable Metrics/AbcSize



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/bidi2pdf/bidi/session.rb', line 118

def close
  return unless started?

  2.times do |attempt|
    success = Bidi2pdf.notification_service.instrument("session_close.bidi2pdf", { session_uri: session_uri.to_s, attempt: attempt + 1 }) do |payload|
      client&.send_cmd_and_wait(Bidi2pdf::Bidi::Commands::SessionEnd.new, timeout: 1) do |response|
        payload[:response] = response
        cleanup
      end

      true
    rescue CmdTimeoutError => e
      payload[:error] = e
      payload[:retry] = attempt < 1 # whether we'll retry again

      false
    end

    break if success
  end
ensure
  @started = false
end

#startObject

Starts the session and initializes the client.

Raises:

  • (StandardError)

    If an error occurs during session start.



92
93
94
95
96
97
98
99
100
# File 'lib/bidi2pdf/bidi/session.rb', line 92

def start
  return if started?

  @started = true
  client
rescue StandardError => e
  @started = false
  raise e
end

#started?Boolean

Checks if the session has started.

Returns:

  • (Boolean)

    True if the session has started, false otherwise.



160
161
162
# File 'lib/bidi2pdf/bidi/session.rb', line 160

def started?
  @started
end

#statusObject

Retrieves the status of the session.



150
151
152
153
154
155
# File 'lib/bidi2pdf/bidi/session.rb', line 150

def status
  send_cmd(Bidi2pdf::Bidi::Commands::SessionStatus.new) do |resp|
    Bidi2pdf.logger.info "Session status: #{resp["result"].inspect}"
    resp["result"]
  end
end

#user_contextsObject

Retrieves user contexts for the session.



145
146
147
# File 'lib/bidi2pdf/bidi/session.rb', line 145

def user_contexts
  send_cmd(Bidi2pdf::Bidi::Commands::GetUserContexts.new) { |resp| Bidi2pdf.logger.debug "User contexts: #{resp}" }
end

#websocket_urlString

Retrieves the WebSocket URL for the session.

Returns:

  • (String)

    The WebSocket URL.



167
168
169
170
171
172
173
174
175
# File 'lib/bidi2pdf/bidi/session.rb', line 167

def websocket_url
  return @websocket_url if @websocket_url

  @websocket_url = if %w[ws wss].include?(session_uri.scheme)
                     session_uri.to_s
                   else
                     create_new_session
                   end
end