Class: Puppeteer::Bidi::Core::UserContext

Inherits:
EventEmitter show all
Includes:
Disposable::DisposableMixin
Defined in:
lib/puppeteer/bidi/core/user_context.rb,
sig/puppeteer/bidi/core/user_context.rbs

Overview

UserContext represents an isolated browsing context (like an incognito session)

Constant Summary collapse

DEFAULT =

Returns:

  • (::String)
'default'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Disposable::DisposableMixin

#dispose

Methods inherited from EventEmitter

#dispose, #emit, #off, #on, #once, #remove_all_listeners

Constructor Details

#initialize(browser, id) ⇒ UserContext

Returns a new instance of UserContext.

Parameters:

  • browser (Object)
  • id (Object)


25
26
27
28
29
30
31
32
# File 'lib/puppeteer/bidi/core/user_context.rb', line 25

def initialize(browser, id)
  super()
  @browser = browser
  @id = id
  @reason = nil
  @disposables = Disposable::DisposableStack.new
  @browsing_contexts = {}
end

Instance Attribute Details

#browserObject (readonly)

Returns the value of attribute browser.

Returns:

  • (Object)


23
24
25
# File 'lib/puppeteer/bidi/core/user_context.rb', line 23

def browser
  @browser
end

#idObject (readonly)

Returns the value of attribute id.

Returns:

  • (Object)


23
24
25
# File 'lib/puppeteer/bidi/core/user_context.rb', line 23

def id
  @id
end

Class Method Details

.create(browser, id) ⇒ UserContext

Create a user context

Parameters:

Returns:



17
18
19
20
21
# File 'lib/puppeteer/bidi/core/user_context.rb', line 17

def self.create(browser, id)
  context = new(browser, id)
  context.send(:initialize_context)
  context
end

Instance Method Details

#browsing_contextsArray[BrowsingContext]

Get all browsing contexts in this user context

Returns:



43
44
45
# File 'lib/puppeteer/bidi/core/user_context.rb', line 43

def browsing_contexts
  @browsing_contexts.values
end

#closed?Object Also known as: disposed?

Check if the context is closed

Returns:

  • (Object)


35
36
37
# File 'lib/puppeteer/bidi/core/user_context.rb', line 35

def closed?
  !@reason.nil?
end

#create_browsing_context(type, reference_context: nil, background: nil) ⇒ BrowsingContext

Create a new browsing context (tab or window)

Parameters:

  • type (String)
  • reference_context: (BrowsingContext, nil) (defaults to: nil)
  • background: (Boolean, nil) (defaults to: nil)

Returns:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/puppeteer/bidi/core/user_context.rb', line 52

def create_browsing_context(type, reference_context: nil, background: nil)
  raise UserContextClosedError, @reason if closed?

  params = {
    type: type,
    userContext: @id
  }
  params[:referenceContext] = reference_context.id if reference_context
  params[:background] = background unless background.nil?

  result = session.async_send_command('browsingContext.create', params).wait
  context_id = result['context']

  # Since event handling might be async or not working properly,
  # check if the context was already created by the event handler
  browsing_context = @browsing_contexts[context_id]

  # If not created by event handler, create it manually
  if browsing_context.nil?
    info = fetch_browsing_context_info(context_id)
    browsing_context = BrowsingContext.from(
      self,
      nil, # parent
      context_id,
      info&.fetch('url', nil) || 'about:blank',
      info&.fetch('originalOpener', nil),
      info&.fetch('clientWindow', nil) || context_id
    )
    @browsing_contexts[context_id] = browsing_context

    browsing_context.once(:closed) do
      @browsing_contexts.delete(context_id)
    end
  end

  browsing_context
end

#dispose_context(reason) ⇒ Object

Parameters:

  • reason (Object)

Returns:

  • (Object)


208
209
210
211
# File 'lib/puppeteer/bidi/core/user_context.rb', line 208

def dispose_context(reason)
  @reason = reason
  dispose
end

#fetch_browsing_context_info(context_id) ⇒ Object

If the creation event races and is missed, query getTree and locate context info.

Parameters:

  • context_id (Object)

Returns:

  • (Object)


214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/puppeteer/bidi/core/user_context.rb', line 214

def fetch_browsing_context_info(context_id)
  result = session.async_send_command('browsingContext.getTree', {}).wait
  stack = result['contexts'] || []

  until stack.empty?
    info = stack.shift
    return info if info['context'] == context_id

    children = info['children']
    stack.concat(children) if children
  end

  nil
rescue StandardError
  nil
end

#get_cookies(**options) ⇒ Async::Task[Array[Hash[String, untyped]]]

Get cookies for this user context

Parameters:

  • options (Object)

Returns:



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/puppeteer/bidi/core/user_context.rb', line 104

def get_cookies(**options)
  raise UserContextClosedError, @reason if closed?

  source_origin = options.delete(:source_origin)
  params = options.dup
  params[:partition] = {
    type: 'storageKey',
    userContext: @id
  }
  params[:partition][:sourceOrigin] = source_origin if source_origin

  Async do
    result = session.async_send_command('storage.getCookies', params).wait
    result['cookies']
  end
end

#initialize_contextObject

Returns:

  • (Object)


172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/puppeteer/bidi/core/user_context.rb', line 172

def initialize_context
  # Listen for browser closure/disconnection
  @browser.once(:closed) do |reason|
    dispose_context("User context was closed: #{reason}")
  end

  @browser.once(:disconnected) do |reason|
    dispose_context("User context was closed: #{reason}")
  end

  # Listen for browsing context creation
  session.on(:'browsingContext.contextCreated') do |info|
    # Only handle top-level contexts (no parent)
    next if info['parent']
    next if info['userContext'] != @id

    browsing_context = BrowsingContext.from(
      self,
      nil, # parent
      info['context'],
      info['url'],
      info['originalOpener'],
      info['clientWindow']
    )

    @browsing_contexts[browsing_context.id] = browsing_context

    # Listen for context closure
    browsing_context.once(:closed) do
      @browsing_contexts.delete(browsing_context.id)
    end

    emit(:browsingcontext, browsing_context)
  end
end

#perform_disposeObject

Returns:

  • (Object)


159
160
161
162
163
164
# File 'lib/puppeteer/bidi/core/user_context.rb', line 159

def perform_dispose
  @reason ||= 'User context already closed, probably because the browser disconnected/closed.'
  emit(:closed, @reason)
  @disposables.dispose
  super
end

#removeObject

Remove this user context

Returns:

  • (Object)


91
92
93
94
95
96
97
98
99
# File 'lib/puppeteer/bidi/core/user_context.rb', line 91

def remove
  return if closed?

  begin
    session.async_send_command('browser.removeUserContext', { userContext: @id })
  ensure
    dispose_context('User context removed')
  end
end

#sessionObject

Returns:

  • (Object)


168
169
170
# File 'lib/puppeteer/bidi/core/user_context.rb', line 168

def session
  @browser.session
end

This method returns an undefined value.

Set a cookie in this user context

Parameters:

  • cookie (Hash[String, untyped])
  • options (Object)


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/puppeteer/bidi/core/user_context.rb', line 125

def set_cookie(cookie, **options)
  raise UserContextClosedError, @reason if closed?

  source_origin = options[:source_origin]
  params = {
    cookie: cookie,
    partition: {
      type: 'storageKey',
      userContext: @id
    }
  }
  params[:partition][:sourceOrigin] = source_origin if source_origin

  session.async_send_command('storage.setCookie', params)
end

#set_permissions(origin, descriptor, state) ⇒ void

This method returns an undefined value.

Set permissions for an origin

Parameters:

  • origin (String)
  • descriptor (Hash[String, untyped])
  • state (String)


146
147
148
149
150
151
152
153
154
155
# File 'lib/puppeteer/bidi/core/user_context.rb', line 146

def set_permissions(origin, descriptor, state)
  raise UserContextClosedError, @reason if closed?

  session.async_send_command('permissions.setPermission', {
    origin: origin,
    descriptor: descriptor,
    state: state,
    userContext: @id
  })
end