Class: Puppeteer::Bidi::Core::UserContext
- Inherits:
-
EventEmitter
- Object
- EventEmitter
- Puppeteer::Bidi::Core::UserContext
- 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 =
'default'
Instance Attribute Summary collapse
-
#browser ⇒ Object
readonly
Returns the value of attribute browser.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Class Method Summary collapse
-
.create(browser, id) ⇒ UserContext
Create a user context.
Instance Method Summary collapse
-
#browsing_contexts ⇒ Array[BrowsingContext]
Get all browsing contexts in this user context.
-
#closed? ⇒ Object
(also: #disposed?)
Check if the context is closed.
-
#create_browsing_context(type, reference_context: nil, background: nil) ⇒ BrowsingContext
Create a new browsing context (tab or window).
- #dispose_context(reason) ⇒ Object
-
#fetch_browsing_context_info(context_id) ⇒ Object
If the creation event races and is missed, query getTree and locate context info.
-
#get_cookies(**options) ⇒ Async::Task[Array[Hash[String, untyped]]]
Get cookies for this user context.
-
#initialize(browser, id) ⇒ UserContext
constructor
A new instance of UserContext.
- #initialize_context ⇒ Object
- #perform_dispose ⇒ Object
-
#remove ⇒ Object
Remove this user context.
- #session ⇒ Object
-
#set_cookie(cookie, **options) ⇒ void
Set a cookie in this user context.
-
#set_permissions(origin, descriptor, state) ⇒ void
Set permissions for an origin.
Methods included from Disposable::DisposableMixin
Methods inherited from EventEmitter
#dispose, #emit, #off, #on, #once, #remove_all_listeners
Constructor Details
#initialize(browser, id) ⇒ UserContext
Returns a new instance of UserContext.
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
#browser ⇒ Object (readonly)
Returns the value of attribute browser.
23 24 25 |
# File 'lib/puppeteer/bidi/core/user_context.rb', line 23 def browser @browser end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
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
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_contexts ⇒ Array[BrowsingContext]
Get all browsing contexts in this user context
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
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)
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
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.
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
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 (**) raise UserContextClosedError, @reason if closed? source_origin = .delete(:source_origin) params = .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_context ⇒ 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_dispose ⇒ 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 |
#remove ⇒ Object
Remove this user context
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 |
#session ⇒ Object
168 169 170 |
# File 'lib/puppeteer/bidi/core/user_context.rb', line 168 def session @browser.session end |
#set_cookie(cookie, **options) ⇒ void
This method returns an undefined value.
Set a cookie in this user context
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 (, **) raise UserContextClosedError, @reason if closed? source_origin = [:source_origin] params = { 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
146 147 148 149 150 151 152 153 154 155 |
# File 'lib/puppeteer/bidi/core/user_context.rb', line 146 def (origin, descriptor, state) raise UserContextClosedError, @reason if closed? session.async_send_command('permissions.setPermission', { origin: origin, descriptor: descriptor, state: state, userContext: @id }) end |