Class: Ferrum::Contexts

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ferrum/contexts.rb

Overview

Owns the browser's whole collection of Contexts, keyed by their browserContextId. Subscribes to the CDP Target.* events, creates Contexts and Targets as they're discovered/attached, and routes target lifecycle updates (info changed, destroyed, crashed) to the Context they belong to.

Constant Summary collapse

ALLOWED_TARGET_TYPES =
%w[page iframe worker shared_worker service_worker].freeze
RECURSIVE_AUTO_ATTACH_TYPES =
%w[page iframe worker shared_worker].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Contexts

Returns a new instance of Contexts.



21
22
23
24
25
26
27
28
# File 'lib/ferrum/contexts.rb', line 21

def initialize(client)
  @client = client
  @contexts = Concurrent::Map.new
  @manually_attached = Concurrent::Map.new
  subscribe
  auto_attach
  discover
end

Instance Attribute Details

#contextsObject (readonly)

Returns the value of attribute contexts.



19
20
21
# File 'lib/ferrum/contexts.rb', line 19

def contexts
  @contexts
end

Instance Method Details

#[](id) ⇒ Context?

The context with the given id, if any.

Parameters:

  • id (String)

Returns:



58
59
60
# File 'lib/ferrum/contexts.rb', line 58

def [](id)
  @contexts[id]
end

#close_connectionsvoid

This method returns an undefined value.

Closes the WebSocket connection of every target in every context, without disposing the contexts themselves.



106
107
108
# File 'lib/ferrum/contexts.rb', line 106

def close_connections
  @contexts.each_value(&:close_targets_connection)
end

#create(**options) ⇒ Context

Creates a new browser context (like an incognito profile).

Parameters:

  • options (Hash)

    Keyword arguments forwarded to Target.createBrowserContext.

Returns:



79
80
81
82
83
84
85
# File 'lib/ferrum/contexts.rb', line 79

def create(**options)
  response = @client.command("Target.createBrowserContext", **options)
  context_id = response["browserContextId"]
  context = Context.new(@client, self, context_id)
  @contexts[context_id] = context
  context
end

#default_contextContext

The browser's first context, created lazily.

Returns:



39
40
41
# File 'lib/ferrum/contexts.rb', line 39

def default_context
  @default_context ||= create
end

#dispose(context_id) ⇒ Boolean

Disposes a browser context and all of its targets.

Parameters:

  • context_id (String)

Returns:

  • (Boolean)


92
93
94
95
96
97
98
99
100
# File 'lib/ferrum/contexts.rb', line 92

def dispose(context_id)
  context = @contexts[context_id]
  return unless context

  context.close_targets_connection
  @client.command("Target.disposeBrowserContext", browserContextId: context.id)
  @contexts.delete(context_id)
  true
end

#eachvoid, Enumerator

Iterates over [id, context] pairs; returns an Enumerator if no block is given.

Returns:

  • (void, Enumerator)


47
48
49
50
51
# File 'lib/ferrum/contexts.rb', line 47

def each(&)
  return enum_for(__method__) unless block_given?

  @contexts.each(&)
end

#find_by(target_id:) ⇒ Context?

The context that owns the given target, if any.

Parameters:

  • target_id (String)

Returns:



67
68
69
70
71
# File 'lib/ferrum/contexts.rb', line 67

def find_by(target_id:)
  context = nil
  @contexts.each_value { |c| context = c if c.target?(target_id) }
  context
end

#manually_attached(target_id) ⇒ Object

Marks a target, so the next time we see it attached, we leave its session alone instead of #detaching it. Used by Ferrum::Context#attach_target right before it manually attaches to a service worker on the caller's behalf.



32
33
34
# File 'lib/ferrum/contexts.rb', line 32

def manually_attached(target_id)
  @manually_attached[target_id] = true
end

#resetvoid

This method returns an undefined value.

Disposes every context still known to the browser.



113
114
115
116
117
# File 'lib/ferrum/contexts.rb', line 113

def reset
  context_ids = @client.command("Target.getBrowserContexts")["browserContextIds"]
  @default_context = nil if context_ids.include?(@default_context&.id)
  @contexts.each_key { |id| dispose(id) if context_ids.include?(id) }
end

#sizeInteger

Number of known contexts.

Returns:

  • (Integer)


122
123
124
# File 'lib/ferrum/contexts.rb', line 122

def size
  @contexts.size
end