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, ConsoleMessageRepeated, 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.



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)



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

def domain
  @domain
end

#nameString (readonly)



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

def name
  @name
end

#raw_dataHash<String, Object> (readonly)



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.



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.



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.



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.



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.



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.



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.

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”



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