Class: WebkitRemote::Rpc

Inherits:
Object
  • Object
show all
Defined in:
lib/webkit_remote/rpc.rb

Overview

RPC client for the Webkit remote debugging protocol.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Rpc

Connects to the remote debugging server in a Webkit tab.

Parameters:

  • opts (Hash) (defaults to: {})

    info on the tab to connect to

Options Hash (opts):

  • tab (WebkitRemote::Tab)

    reference to the tab whose debugger server this RPC client connects to



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/webkit_remote/rpc.rb', line 13

def initialize(opts = {})
  unless tab = opts[:tab]
    raise ArgumentError, 'Target tab not specified'
  end
  @closed = false
  @next_id = 2
  @events = []

  @debug_url = tab.debug_url
  @web_socket = WsSyncClient.new @debug_url
end

Instance Attribute Details

#closedBoolean (readonly) Also known as: closed?

Returns if true, the connection to the remote debugging server has been closed, and this instance is mostly useless.

Returns:

  • (Boolean)

    if true, the connection to the remote debugging server has been closed, and this instance is mostly useless



85
86
87
# File 'lib/webkit_remote/rpc.rb', line 85

def closed
  @closed
end

#debug_urlString (readonly)

Returns points to this client’s Webkit remote debugging server.

Returns:

  • (String)

    points to this client’s Webkit remote debugging server



89
90
91
# File 'lib/webkit_remote/rpc.rb', line 89

def debug_url
  @debug_url
end

Instance Method Details

#call(method, params = nil) ⇒ Hash<String, Object>

Remote debugging RPC call.

See the following URL for implemented calls.

https://developers.google.com/chrome-developer-tools/docs/protocol/1.1/index

Parameters:

  • method (String)

    name of the RPC method to be invoked

  • params (Hash<String, Object>, nil) (defaults to: nil)

    parameters for the RPC method to be invoked

Returns:

  • (Hash<String, Object>)

    the return value of the RPC method



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/webkit_remote/rpc.rb', line 34

def call(method, params = nil)
  request_id = @next_id
  @next_id += 1
  request = {
    jsonrpc: '2.0',
    id: request_id,
    method: method,
  }
  request[:params] = params if params
  request_json = JSON.dump request
  @web_socket.send_frame request_json

  loop do
    result = receive_message request_id
    return result if result
  end
end

#closeWebkitRemote::Rpc

Closes the connection to the remote debugging server.

Call this method to avoid leaking resources.

Returns:



75
76
77
78
79
80
81
# File 'lib/webkit_remote/rpc.rb', line 75

def close
  return if @closed
  @closed = true
  @web_socket.close
  @web_socket = nil
  self
end

#each_event {|event| ... } ⇒ WebkitRemote::Rpc

Continuously reports events sent by the remote debugging server.

Yields:

  • once for each RPC event received from the remote debugger; break to stop the event listening loop

Yield Parameters:

  • event (Hash<Symbol, Object>)

    the name and information hash of the event, under the keys :name and :data

Returns:



59
60
61
62
63
64
65
66
67
68
# File 'lib/webkit_remote/rpc.rb', line 59

def each_event
  loop do
    if @events.empty?
      receive_message nil
    else
      yield @events.shift
    end
  end
  self
end