summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/events/event_rewriter_controller.cc
blob: 62fe12a4197b050e4138f68ab931130a66b2559d (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
// Copyright 2014 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 "chrome/browser/chromeos/events/event_rewriter_controller.h"

#include "ash/shell.h"
#include "ui/aura/env.h"
#include "ui/aura/window_tree_host.h"
#include "ui/events/event_source.h"

namespace chromeos {

EventRewriterController::EventRewriterController() : initialized_(false) {
  // Add the controller as an observer for new root windows.
  aura::Env::GetInstance()->AddObserver(this);
}

EventRewriterController::~EventRewriterController() {
  aura::Env::GetInstance()->RemoveObserver(this);
  // Remove the rewriters from every root window EventSource and destroy them.
  for (EventRewriters::iterator rewriter_iter = rewriters_.begin();
       rewriter_iter != rewriters_.end();
       ++rewriter_iter) {
    aura::Window::Windows windows = ash::Shell::GetAllRootWindows();
    for (aura::Window::Windows::iterator window_iter = windows.begin();
         window_iter != windows.end();
         ++window_iter) {
      (*window_iter)->GetHost()->GetEventSource()->RemoveEventRewriter(
          *rewriter_iter);
    }
  }
  rewriters_.clear();
}

void EventRewriterController::AddEventRewriter(
    scoped_ptr<ui::EventRewriter> rewriter) {
  DCHECK(!initialized_);
  rewriters_.push_back(rewriter.release());
}

void EventRewriterController::Init() {
  DCHECK(!initialized_);
  initialized_ = true;
  // Add the rewriters to each existing root window EventSource.
  aura::Window::Windows windows = ash::Shell::GetAllRootWindows();
  for (aura::Window::Windows::iterator it = windows.begin();
       it != windows.end();
       ++it) {
    AddToEventSource((*it)->GetHost()->GetEventSource());
  }
}

void EventRewriterController::OnHostInitialized(aura::WindowTreeHost* host) {
  if (initialized_)
    AddToEventSource(host->GetEventSource());
}

void EventRewriterController::AddToEventSource(ui::EventSource* source) {
  DCHECK(source);
  for (EventRewriters::iterator it = rewriters_.begin(); it != rewriters_.end();
       ++it) {
    source->AddEventRewriter(*it);
  }
}

}  // namespace chromeos