Class: WebkitRemote::Client::JsObject

Inherits:
Object
  • Object
show all
Defined in:
lib/webkit_remote/client/runtime.rb,
lib/webkit_remote/client/dom_runtime.rb

Overview

Mirrors a JsObject, defined in the Runtime domain.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_object, group) ⇒ JsObject

Wraps a remote JavaScript object



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/webkit_remote/client/runtime.rb', line 286

def initialize(raw_object, group)
  @group = group
  @client = group.client
  @released = false

  @raw_data = raw_object
  @remote_id = raw_object['objectId']
  @js_class_name = raw_object['className']
  @description = raw_object['description']
  @js_type = raw_object['type'].to_sym
  if raw_object['subtype']
    @js_subtype = raw_object['subtype'].to_sym
  else
    @js_subtype = nil
  end
  @value = raw_object['value']

  group.add self

  initialize_modules
end

Instance Attribute Details

#clientWebkitRemote::Client (readonly)

Returns remote debugging client for the browser tab that owns the objects in this group.

Returns:

  • (WebkitRemote::Client)

    remote debugging client for the browser tab that owns the objects in this group



164
165
166
# File 'lib/webkit_remote/client/runtime.rb', line 164

def client
  @client
end

#descriptionString (readonly)

Returns string that would be displayed in the Webkit console to represent this object.

Returns:

  • (String)

    string that would be displayed in the Webkit console to represent this object



148
149
150
# File 'lib/webkit_remote/client/runtime.rb', line 148

def description
  @description
end

#groupWebkitRemote::Client::JsObjectGroup (readonly)

Returns the group that contains this object; the object can be released by calling release_all on the group.

Returns:



169
170
171
# File 'lib/webkit_remote/client/runtime.rb', line 169

def group
  @group
end

#js_class_nameString (readonly)

Returns the class name computed by WebKit for this object.

Returns:

  • (String)

    the class name computed by WebKit for this object



137
138
139
# File 'lib/webkit_remote/client/runtime.rb', line 137

def js_class_name
  @js_class_name
end

#js_subtypeSymbol (readonly)

Returns an additional type hint for this object; documented values are :array, :date, :node, :null, :regexp.

Returns:

  • (Symbol)

    an additional type hint for this object; documented values are :array, :date, :node, :null, :regexp



144
145
146
# File 'lib/webkit_remote/client/runtime.rb', line 144

def js_subtype
  @js_subtype
end

#js_typeString (readonly)

Returns the return value of the JavaScript typeof operator.

Returns:

  • (String)

    the return value of the JavaScript typeof operator



140
141
142
# File 'lib/webkit_remote/client/runtime.rb', line 140

def js_type
  @js_type
end

#raw_dataHash<String, Object> (readonly)

Returns the raw info provided by the remote debugger RPC call; might be useful for accessing extended metadata that is not (yet) recognized by WebkitRemote.

Returns:

  • (Hash<String, Object>)

    the raw info provided by the remote debugger RPC call; might be useful for accessing extended metadata that is not (yet) recognized by WebkitRemote



156
157
158
# File 'lib/webkit_remote/client/runtime.rb', line 156

def raw_data
  @raw_data
end

#releasedBoolean (readonly) Also known as: released?

Returns true if the objects in this group were already released.

Returns:

  • (Boolean)

    true if the objects in this group were already released



159
160
161
# File 'lib/webkit_remote/client/runtime.rb', line 159

def released
  @released
end

#remote_idString (readonly)

Returns identifies this object in the remote debugger.

Returns:

  • (String)

    identifies this object in the remote debugger



173
174
175
# File 'lib/webkit_remote/client/runtime.rb', line 173

def remote_id
  @remote_id
end

#valueObject (readonly)

Returns primitive value for this object, if available.

Returns:

  • (Object)

    primitive value for this object, if available



151
152
153
# File 'lib/webkit_remote/client/runtime.rb', line 151

def value
  @value
end

Class Method Details

.for(raw_object, client, group_name) ⇒ WebkitRemote::Client::JsObject, ...

Wraps a raw object returned by the Webkit remote debugger RPC protocol.

Parameters:

  • raw_object (Hash<String, Object>)

    a JsObject instance, according to the Webkit remote debugging protocol; this is the return value of a ‘Runtime.evaluate’ RPC call

  • client (WebkitRemote::Client::Runtime)

    remote debugging client for the browser tab that owns this object

  • group_name (String)

    name of the object group that will hold this object; object groups work like memory pools

Returns:

  • (WebkitRemote::Client::JsObject, Boolean, Number, String)

    a Ruby wrapper for the given raw object; primitives get wrapped by standard Ruby classes, and objects get wrapped by JsObject instances

Raises:

  • (RuntimeError)


259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/webkit_remote/client/runtime.rb', line 259

def self.for(raw_object, client, group_name)
  if remote_id = raw_object['objectId']
    group = client.object_group group_name, true
    return group.get(remote_id) ||
           WebkitRemote::Client::JsObject.new(raw_object, group)
  else
    # primitive types
    case raw_object['type'] ? raw_object['type'].to_sym : nil
    when :boolean, :number, :string
      return raw_object['value']
    when :undefined
      return WebkitRemote::Client::Undefined
    when :object
      case raw_object['subtype'] ? raw_object['subtype'].to_sym : nil
      when :null
        return nil
      end
      # TODO(pwnall): Any other exceptions?
    end
  end
  raise RuntimeError, "Unable to parse #{raw_object.inspect}"
end

.initializer(name) ⇒ Object

Registers a module initializer.



313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/webkit_remote/client/runtime.rb', line 313

def self.initializer(name)
  before_name = :"initialize_modules_before_#{name}"
  alias_method before_name, :initialize_modules
  private before_name
  remove_method :initialize_modules
  eval "    def initialize_modules\n      \#{name}\n      \#{before_name.to_s}\n    end\n"
  private :initialize_modules
end

Instance Method Details

#bound_call(function_expression, *args) ⇒ WebkitRemote::Client::JsObject, ...

Calls a function with “this” bound to this object.

Parameters:

  • function_expression (String)

    a JavaScript expression that should evaluate to a function

  • args (Array<WebkitRemote::Client::Object, String, Number, Boolean, nil>)

    the arguments passed to the function

Returns:

  • (WebkitRemote::Client::JsObject, Boolean, Number, String, nil)

    a Ruby wrapper for the given raw object; primitives get wrapped by standard Ruby classes, and objects get wrapped by JsObject instances



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/webkit_remote/client/runtime.rb', line 222

def bound_call(function_expression, *args)
  call_args = args.map do |arg|
    if arg.kind_of? WebkitRemote::Client::JsObject
      { objectId: arg.remote_id }
    else
      { value: arg }
    end
  end
  result = @client.rpc.call 'Runtime.callFunctionOn', objectId: @remote_id,
      functionDeclaration: function_expression, arguments: call_args,
      returnByValue: false
  object = WebkitRemote::Client::JsObject.for result['result'], @client,
                                                  @group.name
  if result['wasThrown']
    # TODO(pwnall): some wrapper for exceptions?
    object
  else
    object
  end
end

#dom_nodeWebkitRemote::Client::DomNode

Returns the DOM node wrapped by this JavaScript object.

Returns:



8
9
10
# File 'lib/webkit_remote/client/dom_runtime.rb', line 8

def dom_node
  @dom_node ||= dom_node!
end

#dom_node!WebkitRemote::Client::DomNode

Fetches the wrapped DOM node, bypassing the object’s cache.

Returns:



16
17
18
19
20
21
22
23
# File 'lib/webkit_remote/client/dom_runtime.rb', line 16

def dom_node!
  result = @client.rpc.call 'DOM.requestNode', objectId: @remote_id
  @dom_node = if result['nodeId']
    @client.dom_node result['nodeId']
  else
    nil
  end
end

#initialize_domObject



26
27
28
# File 'lib/webkit_remote/client/dom_runtime.rb', line 26

def initialize_dom
  @dom_node = nil
end

#propertiesHash<String, Webkit::Client::JsProperty>

This object’s properties.

If the object’s properties have not been retrieved, this method retrieves them via a RPC call.

Returns:

  • (Hash<String, Webkit::Client::JsProperty>)

    frozen Hash containg the object’s properties



192
193
194
# File 'lib/webkit_remote/client/runtime.rb', line 192

def properties
  @properties || properties!
end

#properties!Hash<Symbol, Webkit::Client::JsProperty>

This object’s properties, guaranteed to be fresh.

This method always reloads the object’s properties via a RPC call.

Returns:

  • (Hash<Symbol, Webkit::Client::JsProperty>)

    frozen Hash containg the object’s properties



202
203
204
205
206
207
208
209
210
# File 'lib/webkit_remote/client/runtime.rb', line 202

def properties!
  result = @client.rpc.call 'Runtime.getProperties', objectId: @remote_id
  @properties = Hash[
    result['result'].map do |raw_property|
      property = WebkitRemote::Client::JsProperty.new raw_property, self
      [property.name, property]
    end
  ].freeze
end

#releaseWebkit::Client::JsObject

Releases this remote object on the browser side.

Returns:

  • (Webkit::Client::JsObject)

    self



178
179
180
181
182
183
# File 'lib/webkit_remote/client/runtime.rb', line 178

def release
  return if @released
  @client.rpc.call 'Runtime.releaseObject', objectId: @remote_id
  @group.remove self
  released!
end

#released!Object

Informs this object that it was released as part of a group release.



330
331
332
333
# File 'lib/webkit_remote/client/runtime.rb', line 330

def released!
  @released = true
  @group = nil
end