1a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// found in the LICENSE file.
4a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
5a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "ui/events/event_source.h"
6a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
7effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include <algorithm>
8effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
9a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "ui/events/event_processor.h"
10effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "ui/events/event_rewriter.h"
11a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
12a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)namespace ui {
13a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
14effb81e5f8246d0db0270817048dc992db66e9fbBen MurdochEventSource::EventSource() {}
15effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
16effb81e5f8246d0db0270817048dc992db66e9fbBen MurdochEventSource::~EventSource() {}
17effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
18effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid EventSource::AddEventRewriter(EventRewriter* rewriter) {
19effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  DCHECK(rewriter);
20effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  DCHECK(rewriter_list_.end() ==
21effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch         std::find(rewriter_list_.begin(), rewriter_list_.end(), rewriter));
22effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  rewriter_list_.push_back(rewriter);
23effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
24effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
25effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochvoid EventSource::RemoveEventRewriter(EventRewriter* rewriter) {
26effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  EventRewriterList::iterator find =
27effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      std::find(rewriter_list_.begin(), rewriter_list_.end(), rewriter);
28effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (find != rewriter_list_.end())
29effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    rewriter_list_.erase(find);
30effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
31effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)EventDispatchDetails EventSource::SendEventToProcessor(Event* event) {
33effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  scoped_ptr<Event> rewritten_event;
34effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  EventRewriteStatus status = EVENT_REWRITE_CONTINUE;
35effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  EventRewriterList::const_iterator it = rewriter_list_.begin(),
36effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                    end = rewriter_list_.end();
37effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  for (; it != end; ++it) {
38effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    status = (*it)->RewriteEvent(*event, &rewritten_event);
39effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    if (status == EVENT_REWRITE_DISCARD) {
40effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      CHECK(!rewritten_event);
41effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      return EventDispatchDetails();
42effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    }
43effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    if (status == EVENT_REWRITE_CONTINUE) {
44effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      CHECK(!rewritten_event);
45effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      continue;
46effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    }
47effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    break;
48effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  }
49effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  CHECK((it == end && !rewritten_event) || rewritten_event);
50effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  EventDispatchDetails details =
51effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      DeliverEventToProcessor(rewritten_event ? rewritten_event.get() : event);
52effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  if (details.dispatcher_destroyed)
53effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return details;
54effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
55effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  while (status == EVENT_REWRITE_DISPATCH_ANOTHER) {
56effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    scoped_ptr<Event> new_event;
57effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    status = (*it)->NextDispatchEvent(*rewritten_event, &new_event);
58effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    if (status == EVENT_REWRITE_DISCARD)
59effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      return EventDispatchDetails();
60effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    CHECK_NE(EVENT_REWRITE_CONTINUE, status);
61effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    CHECK(new_event);
62effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    details = DeliverEventToProcessor(new_event.get());
63effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    if (details.dispatcher_destroyed)
64effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      return details;
65effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    rewritten_event.reset(new_event.release());
66effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  }
67effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  return EventDispatchDetails();
68effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
69effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
70effb81e5f8246d0db0270817048dc992db66e9fbBen MurdochEventDispatchDetails EventSource::DeliverEventToProcessor(Event* event) {
71a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  EventProcessor* processor = GetEventProcessor();
72a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  CHECK(processor);
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return processor->OnEventFromSource(event);
74a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
75a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
76a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}  // namespace ui
77