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



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/webkit_remote/client/runtime.rb', line 290

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



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

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



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

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:



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

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



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

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



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

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



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

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



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

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



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

def released
  @released
end

#remote_idString (readonly)

Returns identifies this object in the remote debugger.

Returns:

  • (String)

    identifies this object in the remote debugger



177
178
179
# File 'lib/webkit_remote/client/runtime.rb', line 177

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



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

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)


263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/webkit_remote/client/runtime.rb', line 263

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.



317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/webkit_remote/client/runtime.rb', line 317

def self.initializer(name)
  before_name = :"initialize_modules_before_#{name}"
  alias_method before_name, :initialize_modules
  private before_name
  remove_method :initialize_modules
  eval <<END_METHOD
    def initialize_modules
      #{name}
      #{before_name.to_s}
    end
END_METHOD
  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



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/webkit_remote/client/runtime.rb', line 226

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



196
197
198
# File 'lib/webkit_remote/client/runtime.rb', line 196

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



206
207
208
209
210
211
212
213
214
# File 'lib/webkit_remote/client/runtime.rb', line 206

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



182
183
184
185
186
187
# File 'lib/webkit_remote/client/runtime.rb', line 182

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.



334
335
336
337
# File 'lib/webkit_remote/client/runtime.rb', line 334

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