Class: Puppeteer::CDPSession

Inherits:
Object
  • Object
show all
Includes:
DebugPrint, EventCallbackable
Defined in:
lib/puppeteer/cdp_session.rb

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EventCallbackable

#add_event_listener, #emit_event, #observe_first, #on_event, #remove_event_listener

Methods included from DebugPrint

#debug_print, #debug_puts

Constructor Details

#initialize(connection, target_type, session_id) ⇒ CDPSession

Returns a new instance of CDPSession.

Parameters:

  • connection (!Connection)
  • targetType (string)
  • sessionId (string)


11
12
13
14
15
16
# File 'lib/puppeteer/cdp_session.rb', line 11

def initialize(connection, target_type, session_id)
  @callbacks = Concurrent::Hash.new
  @connection = connection
  @target_type = target_type
  @session_id = session_id
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



18
19
20
# File 'lib/puppeteer/cdp_session.rb', line 18

def connection
  @connection
end

Instance Method Details

#async_send_message(method, params = {}) ⇒ Object

Parameters:

  • method (String)
  • params (Hash) (defaults to: {})


30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/puppeteer/cdp_session.rb', line 30

def async_send_message(method, params = {})
  if !@connection
    raise Error.new("Protocol error (#{method}): Session closed. Most likely the #{@target_type} has been closed.")
  end

  promise = resolvable_future

  @connection.generate_id do |id|
    @callbacks[id] = Puppeteer::Connection::MessageCallback.new(method: method, promise: promise)
    @connection.raw_send(id: id, message: { sessionId: @session_id, method: method, params: params })
  end

  promise
end

#detachObject



70
71
72
73
74
75
# File 'lib/puppeteer/cdp_session.rb', line 70

def detach
  if !@connection
    raise Error.new("Session already detarched. Most likely the #{@target_type} has been closed.")
  end
  @connection.send_message('Target.detachFromTarget',  sessionId: @session_id)
end

#handle_closedObject



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/puppeteer/cdp_session.rb', line 77

def handle_closed
  @callbacks.each_value do |callback|
    callback.reject(
      Puppeteer::Connection::ProtocolError.new(
        method: callback.method,
        error_message: 'Target Closed.'))
  end
  @callbacks.clear
  @connection = nil
  emit_event(CDPSessionEmittedEvents::Disconnected)
end

#handle_message(message) ⇒ Object

Parameters:

  • object ({id?: number, method: string, params: Object, error: {message: string, data: any}, result?: *})


46
47
48
49
50
51
52
53
54
55
56
# File 'lib/puppeteer/cdp_session.rb', line 46

def handle_message(message)
  if message['id']
    if callback = @callbacks.delete(message['id'])
      callback_with_message(callback, message)
    else
      raise Error.new("unknown id: #{message['id']}")
    end
  else
    emit_event(message['method'], message['params'])
  end
end

#on(event_name, &block) ⇒ Object

Parameters:

  • event_name (String)


90
91
92
# File 'lib/puppeteer/cdp_session.rb', line 90

def on(event_name, &block)
  add_event_listener(event_name, &block)
end

#once(event_name, &block) ⇒ Object

Parameters:

  • event_name (String)


95
96
97
# File 'lib/puppeteer/cdp_session.rb', line 95

def once(event_name, &block)
  observe_first(event_name, &block)
end

#send_message(method, params = {}) ⇒ Object

Parameters:

  • method (String)
  • params (Hash) (defaults to: {})


23
24
25
# File 'lib/puppeteer/cdp_session.rb', line 23

def send_message(method, params = {})
  await async_send_message(method, params)
end