Class: WebkitRemote::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/webkit_remote/event.rb,
lib/webkit_remote/client/dom_events.rb,
lib/webkit_remote/client/page_events.rb,
lib/webkit_remote/client/console_events.rb,
lib/webkit_remote/client/network_events.rb

Overview

An event received via a RPC notification from a Webkit remote debugger.

This is a generic super-class for events.

Defined Under Namespace

Classes: ConsoleCleared, ConsoleMessage, DomReset, NetworkCacheHit, NetworkData, NetworkFailure, NetworkLoad, NetworkRequest, NetworkResponse, PageDomReady, PageLoaded

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rpc_event, client) ⇒ Event

Wraps raw event data received via a RPC notification.

If at all possible, subclasses should avoid using the WebkitRemote::Client instance, to avoid tight coupling.

Parameters:

  • rpc_event (Hash<Symbol, Object>)

    event information yielded by a call to WebkitRemote::Rpc.each_event

  • the (WebkitRemote::Client)

    client that received this message



99
100
101
102
103
# File 'lib/webkit_remote/event.rb', line 99

def initialize(rpc_event, client)
  @name = rpc_event[:name]
  @domain = rpc_event[:name].split('.', 2).first
  @raw_data = rpc_event[:data] || {}
end

Instance Attribute Details

#domainString (readonly)

Returns event’s domain, e.g. “Page”, “DOM”.

Returns:

  • (String)

    event’s domain, e.g. “Page”, “DOM”.



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

def domain
  @domain
end

#nameString (readonly)

Returns event’s name, e.g. “Page.loadEventFired”.

Returns:

  • (String)

    event’s name, e.g. “Page.loadEventFired”.



11
12
13
# File 'lib/webkit_remote/event.rb', line 11

def name
  @name
end

#raw_dataHash<String, Object> (readonly)

Returns the raw event information provided by the RPC client.

Returns:

  • (Hash<String, Object>)

    the raw event information provided by the RPC client



15
16
17
# File 'lib/webkit_remote/event.rb', line 15

def raw_data
  @raw_data
end

Class Method Details

.can_reach?(client) ⇒ Boolean

Checks if a client is set up to receive an event of this class.

This method is overridden in Event sub-classes. For example, events in the Page domain can only be received if WebkitRemote::Client::Page#page_events is true.

Returns:

  • (Boolean)


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

def self.can_reach?(client)
  true
end

.can_receive?(client, conditions) ⇒ Boolean

Checks if a client can possibly meet an event meeting the given conditions.

Parameters:

  • client (WebkitRemote::Client)

    the client to be checked

  • conditions (Hash<Symbol, Object>)

    the conditions that must be met by an event to get out of the waiting loop

Returns:

  • (Boolean)

    false if calling WebkitRemote::Client#wait_for with the given conditions would get the client stuck



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/webkit_remote/event.rb', line 52

def self.can_receive?(client, conditions)
  conditions.all? do |key, value|
    case key
    when :class, :type
      value.can_reach?(client)
    when :name
      class_for(value).can_reach?(client)
    else
      true
    end
  end
end

.class_for(rpc_event_name) ⇒ Class

The WebkitRemote::Event subclass registered to handle an event.

Parameters:

  • rpc_event_name (String)

    the value of the ‘name’ property of an event notice received via the remote debugging RPC

Returns:

  • (Class)

    WebkitRemote::Event or one of its subclasses



84
85
86
# File 'lib/webkit_remote/event.rb', line 84

def self.class_for(rpc_event_name)
  @registry[rpc_event_name] || Event
end

.for(rpc_event, client) ⇒ WebkitRemote::Event

Wraps raw event data received via a RPC notification.

Parameters:

  • rpc_event (Hash<Symbol, Object>)

    event information yielded by a call to WebkitRemote::Rpc.each_event

  • the (WebkitRemote::Client)

    client that received this message

Returns:

  • (WebkitRemote::Event)

    an instance of an Event subclass that best represents the given event



72
73
74
75
# File 'lib/webkit_remote/event.rb', line 72

def self.for(rpc_event, client)
  klass = class_for rpc_event[:name]
  klass.new rpc_event, client
end

.register(name) ⇒ Class

Registers an Event sub-class for to be instantiated when parsing an event.

Parameters:

  • name (String)

    fully qualified event name, e.g. “Page.loadEventFired”

Returns:

  • (Class)

    self



111
112
113
114
# File 'lib/webkit_remote/event.rb', line 111

def self.register(name)
  WebkitRemote::Event.register_class self, name
  self
end

.register_class(klass, name) ⇒ Class

Registers an Event sub-class for to be instantiated when parsing an event.

Parameters:

  • klass (String)

    the Event subclass to be registered

  • name (String)

    fully qualified event name, e.g. “Page.loadEventFired”

Returns:

  • (Class)

    self



124
125
126
127
128
129
130
# File 'lib/webkit_remote/event.rb', line 124

def self.register_class(klass, name)
  if @registry.has_key? name
    raise ArgumentError, "#{@registry[name].name} already registered #{name}"
  end
  @registry[name] = klass
  self
end

Instance Method Details

#matches?(conditions) ⇒ Boolean

Checks if the event meets a set of conditions.

This is used in WebkitRemote::Client#wait_for.

Parameters:

  • conditions (Hash<Symbol, Object>)

    the conditions that must be met by an event to get out of the waiting loop

Options Hash (conditions):

  • class (Class)

    the class of events to wait for; this condition is met if the event’s class is a sub-class of the given class

  • type (Class)

    synonym for class that can be used with the Ruby 1.9 hash syntax

  • name (String)

    the event’s name, e.g. “Page.loadEventFired”

Returns:

  • (Boolean)

    true if this event matches all the given conditions



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

def matches?(conditions)
  conditions.all? do |key, value|
    case key
    when :class, :type
      kind_of? value
    when :name
      name == value
    else
      # Simple cop-out.
      send(key) == value
    end
  end
end