blob: a31a414e50fc9a0dfe7d76d87330756ae98b2086 [file] [log] [blame]
// Copyright 2015 Google Inc. All Rights Reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
#include <string>
#include <vector>
#include "base/callback.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_vector.h"
#include "base/memory/weak_ptr.h"
#include "base/tracked_objects.h"
#include "cobalt/base/token.h"
#include "cobalt/base/tokens.h"
#include "cobalt/dom/event.h"
#include "cobalt/dom/event_listener.h"
#include "cobalt/script/exception_state.h"
#include "cobalt/script/script_value.h"
#include "cobalt/script/wrappable.h"
namespace cobalt {
namespace dom {
// The EventTarget interface represents an object that holds event listeners
// and possibly generates events.
// This interface describes methods and properties common to all kinds of
// EventTarget.
class EventTarget : public script::Wrappable,
public base::SupportsWeakPtr<EventTarget> {
typedef script::ScriptValue<EventListener> EventListenerScriptValue;
// Web API: EventTarget
void AddEventListener(const std::string& type,
const EventListenerScriptValue& listener,
bool use_capture);
void RemoveEventListener(const std::string& type,
const EventListenerScriptValue& listener,
bool use_capture);
bool DispatchEvent(const scoped_refptr<Event>& event,
script::ExceptionState* exception_state);
// Custom, not in any spec.
// This version of DispatchEvent is intended to be called inside C++ code. It
// won't raise any exception. Any error will be silently ignored.
virtual bool DispatchEvent(const scoped_refptr<Event>& event);
// Creates a new event with the given name and calls DispatchEvent with it,
// and runs dispatched_callback after finish.
void DispatchEventAndRunCallback(base::Token event_name,
const base::Closure& dispatched_callback);
// Posts a task on the current message loop to dispatch event. It does nothing
// if there is no current message loop.
void PostToDispatchEvent(const tracked_objects::Location& location,
base::Token event_name);
// Posts a task on the current message loop to dispatch event, and runs
// dispatched_callback after finish. It does nothing if there is no current
// message loop.
void PostToDispatchEventAndRunCallback(
const tracked_objects::Location& location, base::Token event_name,
const base::Closure& dispatched_callback);
// Web API: GlobalEventHandlers (implements)
// Many objects can have event handlers specified. These act as non-capture
// event listeners for the object on which they are specified.
const EventListenerScriptValue* onblur() {
return GetAttributeEventListener(base::Tokens::blur());
void set_onblur(const EventListenerScriptValue& event_listener) {
SetAttributeEventListener(base::Tokens::blur(), event_listener);
const EventListenerScriptValue* onerror() {
return GetAttributeEventListener(base::Tokens::error());
void set_onerror(const EventListenerScriptValue& event_listener) {
SetAttributeEventListener(base::Tokens::error(), event_listener);
const EventListenerScriptValue* onfocus() {
return GetAttributeEventListener(base::Tokens::focus());
void set_onfocus(const EventListenerScriptValue& event_listener) {
SetAttributeEventListener(base::Tokens::focus(), event_listener);
const EventListenerScriptValue* onload() {
return GetAttributeEventListener(base::Tokens::load());
void set_onload(const EventListenerScriptValue& event_listener) {
SetAttributeEventListener(base::Tokens::load(), event_listener);
const EventListenerScriptValue* onunload() {
return GetAttributeEventListener(base::Tokens::unload());
void set_onunload(const EventListenerScriptValue& event_listener) {
SetAttributeEventListener(base::Tokens::unload(), event_listener);
// Set an event listener assigned as an attribute. Overwrite the existing one
// if there is any.
void SetAttributeEventListener(base::Token type,
const EventListenerScriptValue& listener);
// Get the event listener currently assigned to an attribute, or NULL if
// there is none.
const EventListenerScriptValue* GetAttributeEventListener(
base::Token type) const;
// script::Wrappable
bool ShouldKeepWrapperAlive() OVERRIDE;
// Returns a string that represents the target for debug purpose.
virtual std::string GetDebugName() { return ""; }
void TraceMembers(script::Tracer* tracer) OVERRIDE;
// This function sends the event to the event listeners attached to the
// current event target. It takes stop immediate propagation flag into
// account. The caller should set the phase and target.
void FireEventOnListeners(const scoped_refptr<Event>& event);
struct EventListenerInfo {
EventListenerInfo(base::Token type, EventTarget* const event_target,
const EventListenerScriptValue& listener,
bool use_capture, EventListener::Type listener_type);
base::Token type;
script::ScriptValue<EventListener>::Reference listener;
bool use_capture;
EventListener::Type listener_type;
typedef ScopedVector<EventListenerInfo> EventListenerInfos;
void AddEventListenerInternal(base::Token type,
const EventListenerScriptValue& listener,
bool use_capture,
EventListener::Type listener_type);
EventListenerInfos event_listener_infos_;
} // namespace dom
} // namespace cobalt