15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#ifndef UI_EVENTS_EVENT_DISPATCHER_H_
6d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#define UI_EVENTS_EVENT_DISPATCHER_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/auto_reset.h"
9d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "ui/events/event.h"
10d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "ui/events/event_constants.h"
11a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "ui/events/event_handler.h"
1268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#include "ui/events/events_export.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace ui {
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class EventDispatcher;
17a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)class EventTarget;
18a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)class EventTargeter;
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)struct EventDispatchDetails {
21f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EventDispatchDetails()
22f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      : dispatcher_destroyed(false),
23f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        target_destroyed(false) {}
24f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  bool dispatcher_destroyed;
25f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  bool target_destroyed;
26f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)};
27f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
2868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)class EVENTS_EXPORT EventDispatcherDelegate {
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EventDispatcherDelegate();
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ~EventDispatcherDelegate();
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns whether an event can still be dispatched to a target. (e.g. during
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // event dispatch, one of the handlers may have destroyed the target, in which
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // case the event can no longer be dispatched to the target).
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool CanDispatchToTarget(EventTarget* target) = 0;
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns the event being dispatched (or NULL if no event is being
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // dispatched).
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Event* current_event();
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
42a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Dispatches |event| to |target|. This calls |PreDispatchEvent()| before
43a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // dispatching the event, and |PostDispatchEvent()| after the event has been
44a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // dispatched.
45f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  EventDispatchDetails DispatchEvent(EventTarget* target, Event* event)
46f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      WARN_UNUSED_RESULT;
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
48a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) protected:
49a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // This is called once a target has been determined for an event, right before
50a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // the event is dispatched to the target. This function may modify |event| to
51a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // prepare it for dispatch (e.g. update event flags, location etc.).
52a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  virtual EventDispatchDetails PreDispatchEvent(
53a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      EventTarget* target,
54a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      Event* event) WARN_UNUSED_RESULT;
55a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
56a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // This is called right after the event dispatch is completed.
57a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // |target| is NULL if the target was deleted during dispatch.
58a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  virtual EventDispatchDetails PostDispatchEvent(
59a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      EventTarget* target,
60a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      const Event& event) WARN_UNUSED_RESULT;
61a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
63a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Dispatches the event to the target.
64a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  EventDispatchDetails DispatchEventToTarget(EventTarget* target,
65a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                             Event* event) WARN_UNUSED_RESULT;
66a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EventDispatcher* dispatcher_;
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(EventDispatcherDelegate);
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Dispatches events to appropriate targets.
7368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)class EVENTS_EXPORT EventDispatcher {
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  explicit EventDispatcher(EventDispatcherDelegate* delegate);
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ~EventDispatcher();
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void ProcessEvent(EventTarget* target, Event* event);
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const Event* current_event() const { return current_event_; }
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Event* current_event() { return current_event_; }
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool delegate_destroyed() const { return !delegate_; }
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void OnHandlerDestroyed(EventHandler* handler);
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void OnDispatcherDelegateDestroyed();
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
891e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  void DispatchEventToEventHandlers(EventHandlerList* list, Event* event);
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Dispatches an event, and makes sure it sets ER_CONSUMED on the
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // event-handling result if the dispatcher itself has been destroyed during
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // dispatching the event to the event handler.
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void DispatchEvent(EventHandler* handler, Event* event);
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EventDispatcherDelegate* delegate_;
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Event* current_event_;
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  EventHandlerList handler_list_;
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(EventDispatcher);
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace ui
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
107d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#endif  // UI_EVENTS_EVENT_DISPATCHER_H_
108