Class: Ferrum::Contexts
- Inherits:
-
Object
- Object
- Ferrum::Contexts
- Includes:
- Enumerable
- Defined in:
- lib/ferrum/contexts.rb
Overview
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
-
#contexts ⇒ Object
readonly
Returns the value of attribute contexts.
Instance Method Summary collapse
-
#[](id) ⇒ Context?
The context with the given id, if any.
-
#close_connections ⇒ void
Closes the WebSocket connection of every target in every context, without disposing the contexts themselves.
-
#create(**options) ⇒ Context
Creates a new browser context (like an incognito profile).
-
#default_context ⇒ Context
The browser's first context, created lazily.
-
#dispose(context_id) ⇒ Boolean
Disposes a browser context and all of its targets.
-
#each ⇒ void, Enumerator
Iterates over
[id, context]pairs; returns anEnumeratorif no block is given. -
#find_by(target_id:) ⇒ Context?
The context that owns the given target, if any.
-
#initialize(client) ⇒ Contexts
constructor
A new instance of Contexts.
-
#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.
-
#reset ⇒ void
Disposes every context still known to the browser.
-
#size ⇒ Integer
Number of known contexts.
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
#contexts ⇒ Object (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.
58 59 60 |
# File 'lib/ferrum/contexts.rb', line 58 def [](id) @contexts[id] end |
#close_connections ⇒ void
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).
79 80 81 82 83 84 85 |
# File 'lib/ferrum/contexts.rb', line 79 def create(**) response = @client.command("Target.createBrowserContext", **) context_id = response["browserContextId"] context = Context.new(@client, self, context_id) @contexts[context_id] = context context end |
#default_context ⇒ Context
The browser's first context, created lazily.
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.
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 |
#each ⇒ void, Enumerator
Iterates over [id, context] pairs; returns an Enumerator if no
block is given.
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.
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 |
#reset ⇒ void
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 |
#size ⇒ Integer
Number of known contexts.
122 123 124 |
# File 'lib/ferrum/contexts.rb', line 122 def size @contexts.size end |