summaryrefslogtreecommitdiff
path: root/chromium/ui/events/event_processor.cc
blob: 6c43bf5146fbc85ebec362b8f9b83367313ac82f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/events/event_processor.h"

#include "ui/events/event_target.h"
#include "ui/events/event_targeter.h"

namespace ui {

EventProcessor::EventProcessor() : weak_ptr_factory_(this) {}

EventProcessor::~EventProcessor() {}

EventDispatchDetails EventProcessor::OnEventFromSource(Event* event) {
  base::WeakPtr<EventProcessor> weak_this = weak_ptr_factory_.GetWeakPtr();
  // If |event| is in the process of being dispatched or has already been
  // dispatched, then dispatch a copy of the event instead. We expect event
  // target to be already set if event phase is after EP_PREDISPATCH.
  bool dispatch_original_event = event->phase() == EP_PREDISPATCH;
  DCHECK(dispatch_original_event || event->target());
  Event* event_to_dispatch = event;
  std::unique_ptr<Event> event_copy;
  if (!dispatch_original_event) {
    event_copy = Event::Clone(*event);
    event_to_dispatch = event_copy.get();
  }

  EventDispatchDetails details;
  OnEventProcessingStarted(event_to_dispatch);
  // GetInitialEventTarget() may handle the event.
  EventTarget* initial_target = event_to_dispatch->handled()
                                    ? nullptr
                                    : GetInitialEventTarget(event_to_dispatch);
  if (!event_to_dispatch->handled()) {
    EventTarget* target = initial_target;
    EventTargeter* targeter = nullptr;

    if (!target) {
      EventTarget* root = GetRootForEvent(event_to_dispatch);
      DCHECK(root);
      targeter = root->GetEventTargeter();
      if (targeter) {
        target = targeter->FindTargetForEvent(root, event_to_dispatch);
      } else {
        targeter = GetDefaultEventTargeter();
        if (event_to_dispatch->target())
          target = root;
        else
          target = targeter->FindTargetForEvent(root, event_to_dispatch);
      }
      DCHECK(targeter);
    }

    while (target) {
      details = DispatchEvent(target, event_to_dispatch);

      if (!dispatch_original_event) {
        if (event_to_dispatch->stopped_propagation())
          event->StopPropagation();
        else if (event_to_dispatch->handled())
          event->SetHandled();
      }

      if (details.dispatcher_destroyed)
        return details;

      if (!weak_this) {
        details.dispatcher_destroyed = true;
        return details;
      }

      if (details.target_destroyed || event->handled() ||
          target == initial_target) {
        break;
      }

      DCHECK(targeter);
      target = targeter->FindNextBestTarget(target, event_to_dispatch);
    }
  }
  OnEventProcessingFinished(event);
  return details;
}

void EventProcessor::OnEventProcessingStarted(Event* event) {
}

void EventProcessor::OnEventProcessingFinished(Event* event) {
}

}  // namespace ui