1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/events/event_rewriter_controller.h"
8 #include "ui/aura/env.h"
9 #include "ui/aura/window_tree_host.h"
10 #include "ui/events/event_source.h"
14 EventRewriterController::EventRewriterController() : initialized_(false) {
15 // Add the controller as an observer for new root windows.
16 aura::Env::GetInstance()->AddObserver(this);
19 EventRewriterController::~EventRewriterController() {
20 aura::Env::GetInstance()->RemoveObserver(this);
21 // Remove the rewriters from every root window EventSource and destroy them.
22 for (EventRewriters::iterator rewriter_iter
= rewriters_
.begin();
23 rewriter_iter
!= rewriters_
.end();
25 aura::Window::Windows windows
= ash::Shell::GetAllRootWindows();
26 for (aura::Window::Windows::iterator window_iter
= windows
.begin();
27 window_iter
!= windows
.end();
29 (*window_iter
)->GetHost()->GetEventSource()->RemoveEventRewriter(
36 void EventRewriterController::AddEventRewriter(
37 scoped_ptr
<ui::EventRewriter
> rewriter
) {
38 DCHECK(!initialized_
);
39 rewriters_
.push_back(rewriter
.release());
42 void EventRewriterController::Init() {
43 DCHECK(!initialized_
);
45 // Add the rewriters to each existing root window EventSource.
46 aura::Window::Windows windows
= ash::Shell::GetAllRootWindows();
47 for (aura::Window::Windows::iterator it
= windows
.begin();
50 AddToEventSource((*it
)->GetHost()->GetEventSource());
54 void EventRewriterController::OnHostInitialized(aura::WindowTreeHost
* host
) {
56 AddToEventSource(host
->GetEventSource());
59 void EventRewriterController::AddToEventSource(ui::EventSource
* source
) {
61 for (EventRewriters::iterator it
= rewriters_
.begin(); it
!= rewriters_
.end();
63 source
->AddEventRewriter(*it
);
67 } // namespace chromeos