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. 4 5 #include "chrome/browser/chromeos/events/event_rewriter_controller.h" 6 7 #include "ash/shell.h" 8 #include "ui/aura/env.h" 9 #include "ui/aura/window_tree_host.h" 10 #include "ui/events/event_source.h" 11 12 namespace chromeos { 13 14 EventRewriterController::EventRewriterController() : initialized_(false) { 15 // Add the controller as an observer for new root windows. 16 aura::Env::GetInstance()->AddObserver(this); 17 } 18 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(); 24 ++rewriter_iter) { 25 aura::Window::Windows windows = ash::Shell::GetAllRootWindows(); 26 for (aura::Window::Windows::iterator window_iter = windows.begin(); 27 window_iter != windows.end(); 28 ++window_iter) { 29 (*window_iter)->GetHost()->GetEventSource()->RemoveEventRewriter( 30 *rewriter_iter); 31 } 32 } 33 rewriters_.clear(); 34 } 35 36 void EventRewriterController::AddEventRewriter( 37 scoped_ptr<ui::EventRewriter> rewriter) { 38 DCHECK(!initialized_); 39 rewriters_.push_back(rewriter.release()); 40 } 41 42 void EventRewriterController::Init() { 43 DCHECK(!initialized_); 44 initialized_ = true; 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(); 48 it != windows.end(); 49 ++it) { 50 AddToEventSource((*it)->GetHost()->GetEventSource()); 51 } 52 } 53 54 void EventRewriterController::OnHostInitialized(aura::WindowTreeHost* host) { 55 if (initialized_) 56 AddToEventSource(host->GetEventSource()); 57 } 58 59 void EventRewriterController::AddToEventSource(ui::EventSource* source) { 60 DCHECK(source); 61 for (EventRewriters::iterator it = rewriters_.begin(); it != rewriters_.end(); 62 ++it) { 63 source->AddEventRewriter(*it); 64 } 65 } 66 67 } // namespace chromeos 68